# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
---

# BGP Validation tests

- name: Gather ip route summary and ArBGP state
  arista.eos.eos_command:
    commands: "show ip route summary"
  register: ip_route_summary
  tags:
    - bgp_check

- name: Validate ArBGP is configured and operating
  delegate_to: localhost
  ansible.builtin.assert:
    that:
      - ip_route_summary.stdout_lines[0][0] | regex_search('multi-agent')
      - ip_route_summary.stdout_lines[0][1] | regex_search('multi-agent')
    fail_msg: "{{ ip_route_summary.stdout_lines[0][0] | replace('\"', '') }}, {{ ip_route_summary.stdout_lines[0][1] | replace('\"', '') }}"
    quiet: yes
  when: (service_routing_protocols_model is defined and service_routing_protocols_model == 'multi-agent')
  ignore_errors: "{{ eos_validate_state_validation_mode_loose }}"
  register: arbgp_state_results
  tags:
    - bgp_check

- name: Gather bgp summary (ip and evpn)
  arista.eos.eos_command:
    commands:
      - command: "show ip bgp summary | json"
      - command: "show bgp evpn summary | json"
  register: bgp_summary
  when: |
    (arbgp_state_results)
  tags:
    - bgp_check

- name: Validate ip bgp neighbors peer state
  delegate_to: localhost
  ansible.builtin.assert:
    that:
      - bgp_summary.stdout[0].vrfs.default.peers[bgp_neighbor.ip_address].peerState | arista.avd.default('Not configured') == 'Established'
    fail_msg: "Session state {{ bgp_summary.stdout[0].vrfs.default.peers[bgp_neighbor.ip_address].peerState | arista.avd.default('Not configured') | replace('\"', '') }}"
    quiet: yes
  loop: "{{ router_bgp.neighbors | arista.avd.default({}) | arista.avd.convert_dicts('ip_address') }}"
  loop_control:
    loop_var: bgp_neighbor
  when: |
    ((router_bgp.peer_groups | arista.avd.default({}) | arista.avd.convert_dicts('name') | selectattr('name', 'eq', bgp_neighbor.peer_group) | first).type == 'ipv4' and arbgp_state_results)
  ignore_errors: "{{ eos_validate_state_validation_mode_loose }}"
  register: ip_bgp_peer_state_results
  tags:
    - bgp_check

- name: Validate bgp evpn neighbors peer state
  delegate_to: localhost
  ansible.builtin.assert:
    that:
      - bgp_summary.stdout[1].vrfs.default.peers[bgp_neighbor.ip_address].peerState | arista.avd.default('Not configured') == 'Established'
    fail_msg: "Session state: {{ bgp_summary.stdout[1].vrfs.default.peers[bgp_neighbor.ip_address].peerState | arista.avd.default('Not configured') | replace('\"', '') }}"
    quiet: yes
  loop: "{{ router_bgp.neighbors | arista.avd.default({}) | arista.avd.convert_dicts('ip_address') }}"
  loop_control:
    loop_var: bgp_neighbor
  when: |
    ((router_bgp.peer_groups | arista.avd.default({}) | arista.avd.convert_dicts('name') | selectattr('name', 'eq', bgp_neighbor.peer_group) | first).type == 'evpn'  and arbgp_state_results)
  ignore_errors: "{{ eos_validate_state_validation_mode_loose }}"
  register: bgp_evpn_peer_state_results
  tags:
    - bgp_check
