---
- debug: msg="START connection={{ ansible_connection }} nxos_bgp_neighbor_af sanity test"
- debug: msg="Using provider={{ connection.transport }}"
  when: ansible_connection == "local"

- set_fact: soft_reconfiguration_ina="always"
  when: imagetag and (imagetag is version_compare('D1', 'ne'))

- name: "Disable feature BGP"
  nxos_feature: &disable_bgp
    feature: bgp
    state: disabled
  ignore_errors: yes

- name: "Enable feature BGP"
  nxos_feature:
    feature: bgp
    state: enabled
  ignore_errors: yes

- block:
  - name: "Configure BGP neighbor address-family"
    nxos_bgp_neighbor_af: &configure1
      asn: 65535
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      additional_paths_receive: 'enable'
      additional_paths_send: 'enable'
      advertise_map_exist: ['ansible_rm', 'my_exist_map']
      allowas_in: True
      default_originate: True
      disable_peer_as_check: True
      filter_list_in: 'my_filter_list_in'
      filter_list_out: 'my_filter_list_out'
      max_prefix_limit: 100
      max_prefix_threshold: 50
      max_prefix_warning: 'true'
      next_hop_self: True
      next_hop_third_party: False
      prefix_list_in: 'pfx_in'
      prefix_list_out: 'pfx_out'
      send_community: 'both'
      soft_reconfiguration_in: 'enable'
      suppress_inactive: True
      unsuppress_map: 'unsup_map'
      weight: '30'
      state: present
    register: result

  - assert: &true
      that:
        - "result.changed == true"

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure1
    register: result

  - assert: &false
      that:
        - "result.changed == false"

  - name: "Configure BGP neighbor address-family def1"
    nxos_bgp_neighbor_af: &configure_def1
      asn: 65535
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      additional_paths_receive: 'inherit'
      additional_paths_send: 'inherit'
      advertise_map_exist: default
      allowas_in: False
      default_originate: False
      disable_peer_as_check: False
      filter_list_in: default
      filter_list_out: default
      max_prefix_limit: default
      max_prefix_threshold: default
      max_prefix_warning: False
      next_hop_self: False
      next_hop_third_party: True
      prefix_list_in: default
      prefix_list_out: default
      send_community: 'none'
      soft_reconfiguration_in: 'inherit'
      suppress_inactive: False
      unsuppress_map: default
      weight: default
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure_def1
    register: result

  - assert: *false

  - name: "Setup: Remove BGP config"
    nxos_bgp_neighbor_af: &removenaf
      asn: 65535
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      state: absent
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *removenaf
    register: result

  - assert: *false

  - name: "Configure BGP neighbor address-family"
    nxos_bgp_neighbor_af: &configure2
      asn: 65535
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      allowas_in_max: '5'
      advertise_map_non_exist: ['ansible_rm', 'my_non_exist_map']
      default_originate_route_map: 'my_route_map'
      max_prefix_limit: 100
      max_prefix_interval: 30
      max_prefix_threshold: 50
      route_map_in: 'rm_in'
      route_map_out: 'rm_out'
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure2
    register: result

  - assert: *false

  - name: "Configure BGP neighbor address-family def2"
    nxos_bgp_neighbor_af: &configure_def2
      asn: 65535
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      allowas_in_max: default
      advertise_map_non_exist: default
      default_originate_route_map: default
      max_prefix_limit: default
      max_prefix_interval: default
      max_prefix_threshold: default
      route_map_in: default
      route_map_out: default
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure_def2
    register: result

  - assert: *false

  - name: "Setup: Remove BGP config"
    nxos_bgp_neighbor_af: *removenaf
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *removenaf
    register: result

  - assert: *false

  - name: "Configure eBGP"
    nxos_bgp_neighbor:
      asn: 65535
      vrf: 'blue'
      neighbor: '3.3.3.3'
      remote_as: 2

  - name: "Configure BGP neighbor 3"
    nxos_bgp_neighbor_af: &configure3
      asn: 65535
      vrf: 'blue'
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      additional_paths_receive: 'disable'
      additional_paths_send: 'disable'
      as_override: 'true'
      send_community: 'standard'
      soft_reconfiguration_in: "{{soft_reconfiguration_ina|default(omit)}}"
      soo: '3:3'
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure3
    register: result

  - assert: *false

  - name: "Configure BGP neighbor def3"
    nxos_bgp_neighbor_af: &configure_def3
      asn: 65535
      vrf: 'blue'
      neighbor: '3.3.3.3'
      afi: ipv4
      safi: unicast
      additional_paths_receive: 'inherit'
      additional_paths_send: 'inherit'
      as_override: False
      send_community: default
      soo: default
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure_def3
    register: result

  - assert: *false

  - name: "Setup: Remove BGP config"
    nxos_bgp: &remove
      asn: 65535
      state: absent
    register: result

  - assert: *true

  - name: "Configure BGP neighbor af route_reflector_client"
    nxos_bgp_neighbor:
      asn: 65535
      neighbor: '2.2.2.2'
      remote_as: 65535

  - name: "Configure BGP neighbor 4"
    nxos_bgp_neighbor_af: &configure4
      asn: 65535
      neighbor: '2.2.2.2'
      afi: ipv4
      safi: unicast
      route_reflector_client: 'true'
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure4
    register: result

  - assert: *false

  - name: "Configure BGP neighbor def4"
    nxos_bgp_neighbor_af: &configure_def4
      asn: 65535
      neighbor: '2.2.2.2'
      afi: ipv4
      safi: unicast
      route_reflector_client: False
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp_neighbor_af: *configure_def4
    register: result

  - assert: *false

  always:
  - name: "Cleanup BGP"
    nxos_bgp: *remove
    ignore_errors: yes

  - name: "Disable feature bgp"
    nxos_feature: *disable_bgp

  - debug: msg="END connection={{ ansible_connection }} nxos_bgp_neighbor_af sanity test"
