<template name="cisco_ios_bgp_neighbors" results="per_template">
<doc>
Parse Cisco IOS `show ip bgp all neighbors` and
`show running-config | section bgp` for the `bgp_neighbors` getter.

Returns one normalized record per VRF and neighbor address, merging address
families across repeated neighbor sections. Session state, peering type,
addresses, remote ASN, router ID, description, timers, BGP uptime, policies,
and prefix counts are collected when present. Unreported fields are null or
empty lists. Local AS comes from direct neighbor or inherited peer-group
`local-as`, falling back to the router BGP ASN. Configuration also supplies
peer groups, descriptions, update sources, and route maps. Address families
come from section headers, rather than advertised capability lines.
</doc>

<input>
commands = ["show ip bgp all neighbors", "show running-config | section bgp"]
platform = ["cisco_ios", "ios", "cisco_iosxe"]
</input>

<macro>
def transform(payload):
    from ttp_templates.utils.cisco_ios_process_show_ip_bgp_all_neighbors import transform_bgp_neighbors
    return transform_bgp_neighbors(payload)
</macro>

<group name="address_families*">
For address family: {{ afi_name | ORPHRASE | _start_ }}
  <group name="neighbors*">
BGP neighbor is {{ remote_address | _start_ }}, remote AS {{ remote_as | to_int }}, {{ peering_type }} link
BGP neighbor is {{ remote_address | _start_ }}, vrf {{ vrf }}, remote AS {{ remote_as | to_int }}, {{ peering_type }} link
 Description: {{ description | ORPHRASE }}
  BGP version {{ ignore }}, remote router ID {{ router_id }}
  BGP state = {{ bgp_state | ORPHRASE }}
  Last read {{ ignore }}, last write {{ ignore }}, hold time is {{ hold_time | to_int }}, keepalive interval is {{ keepalive | to_int }} seconds
  Interface associated: {{ local_interface }} {{ _interface_details | ORPHRASE }}
  Interface associated: {{ local_interface }}
  Member of peer-group {{ peer_group }} for session parameters
  Route map for incoming advertisements is {{ import_policy }}
  Route map for outgoing advertisements is {{ export_policy }}
  {{ prefixes_received | to_int }} accepted prefixes, {{ _prefix_details | ORPHRASE }}
  {{ prefixes_received | to_int }} accepted prefixes
  {{ prefixes_sent | to_int }} announced prefixes
Local host: {{ local_address }}, Local port: {{ ignore }}
  </group>
</group>

<group name="bgp_config">
router bgp {{ router_asn | _start_ }}
 <group name="statements*">
 neighbor {{ peer | _start_ }} peer-group
 neighbor {{ peer | _start_ }} peer-group {{ peer_group }}
 neighbor {{ peer | _start_ }} local-as {{ local_as }}
 neighbor {{ peer | _start_ }} local-as {{ local_as }} {{ _options | ORPHRASE }}
 neighbor {{ peer | _start_ }} description {{ description | ORPHRASE }}
 neighbor {{ peer | _start_ }} update-source {{ update_source }}
 neighbor {{ peer | _start_ }} route-map {{ policy }} {{ direction }}
 </group>
 <group name="families*">
 address-family {{ afi | _start_ }}
 address-family {{ afi | _start_ }} {{ safi }}
 address-family {{ afi | _start_ }} vrf {{ vrf }}
 address-family {{ afi | _start_ }} {{ safi }} vrf {{ vrf }}
  <group name="statements*">
  neighbor {{ peer | _start_ }} peer-group
  neighbor {{ peer | _start_ }} peer-group {{ peer_group }}
  neighbor {{ peer | _start_ }} local-as {{ local_as }}
  neighbor {{ peer | _start_ }} local-as {{ local_as }} {{ _options | ORPHRASE }}
  neighbor {{ peer | _start_ }} description {{ description | ORPHRASE }}
  neighbor {{ peer | _start_ }} update-source {{ update_source }}
  neighbor {{ peer | _start_ }} route-map {{ policy }} {{ direction }}
  </group>
 exit-address-family {{ _end_ }}
 </group>
</group>

<output macro="transform"/>
</template>
