<template name="juniper_junos_bgp_asn" results="per_template">
<doc>
Template to parse unique BGP ASNs from Juniper Junos configuration.

This template requires output of:

- `show configuration | display set | match "autonomous-system|local-as|peer-as"`

ASNs are collected from global and routing-instance `autonomous-system`,
`local-as`, and `peer-as` statements. For BGP group and neighbor statements,
the group name is used as the description. `local_asn` is true for
`autonomous-system` and `local-as` ASNs, and false for `peer-as` ASNs. Results
are deduplicated by ASN, preserving the first occurrence and its description
while keeping `local_asn` true if any occurrence marks the ASN as local.

Returns a normalized list of dictionaries with these keys:

- `asn` - AS number integer
- `description` - BGP group name or `null`
- `local_asn` - boolean indicating ASN belongs to the device

Example normalized output (YAML):

```yaml
- asn: 1234
  description: null
  local_asn: true
- asn: 4321
  description: GROUP_1
  local_asn: false
```

</doc>

<input>
commands = [
    'show configuration | display set | match "autonomous-system|local-as|peer-as"'
]
platform = [
    "juniper_junos",
    "junos",
]
</input>

<macro>
def transform_bgp_asns_to_records(data):
    from ttp_templates.utils.juniper_junos_process_show_configuration_pipe_display_set_pipe_match_autonomous_system import transform_bgp_asns

    return transform_bgp_asns(data)
</macro>

<group name="statements*">
set routing-options autonomous-system {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }} {{ ignore(ORPHRASE) }}
set routing-options autonomous-system {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }}
set routing-instances {{ vrf }} routing-options autonomous-system {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }} {{ ignore(ORPHRASE) }}
set routing-instances {{ vrf }} routing-options autonomous-system {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }}
set protocols bgp group {{ description }} peer-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", False) }}
set protocols bgp group {{ description }} local-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }} {{ ignore(ORPHRASE) }}
set protocols bgp group {{ description }} local-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }}
set protocols bgp group {{ description }} neighbor {{ neighbor }} peer-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", False) }}
set routing-instances {{ vrf }} protocols bgp group {{ description }} peer-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", False) }}
set routing-instances {{ vrf }} protocols bgp group {{ description }} local-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }} {{ ignore(ORPHRASE) }}
set routing-instances {{ vrf }} protocols bgp group {{ description }} local-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", True) }}
set routing-instances {{ vrf }} protocols bgp group {{ description }} neighbor {{ neighbor }} peer-as {{ asn | _start_ | DIGIT | to_int | let("local_asn", False) }}
</group>

<output macro="transform_bgp_asns_to_records"/>

</template>
