<template name="Cisco_NXOS" results="per_template">
<doc>
Sample structure produced by this template: 
```
{'Cisco_NXOS': {'switch-1': {'cdp_peers': [{'source': 'switch-1',
                                           'src_label': 'Ge4/6',
                                           'target': {'bottom_label': 'cisco WS-C6509',
                                                      'id': 'switch-2',
                                                      'top_label': '10.2.2.2'},
                                           'trgt_label': 'Ge1/5'},
                                          {'source': 'switch-1',
                                           'src_label': 'Ge4/7',
                                           'target': {'bottom_label': 'cisco WS-C6509',
                                                      'id': 'switch-2',
                                                      'top_label': '10.2.2.2'},
                                           'trgt_label': 'Ge1/6'}],
                            'interfaces': {'Ge1/1': {'description': 'switch-3:Gi0/1',
                                                     'is_l2': True,
                                                     'l2_mode': 'trunk',
                                                     'lag_id': '11',
                                                     'lag_mode': 'active',
                                                     'mtu': '9216',
                                                     'state': {'admin': 'up',
                                                               'bw_kbits': '10000000',
                                                               'description': 'switch-3:Gi0/1',
                                                               'duplex': 'Full',
                                                               'hardware': 'Ten Gigabit Ethernet '
                                                                           'Port',
                                                               'is_physical_port': True,
                                                               'line': 'up',
                                                               'line_status': 'connected',
                                                               'link_speed': '10Gb/s',
                                                               'link_type': 'auto',
                                                               'mac': 'a89d.2163.1111',
                                                               'media_type': '10GBase-LR',
                                                               'mtu': '9216'},
                                                     'trunk_vlans': '101'}
                                           'Ge4/6': {'description': 'switch-2: trunk',
                                                     'is_l2': True,
                                                     'l2_mode': 'trunk',
                                                     'lag_id': '3',
                                                     'lag_mode': 'active',
                                                     'trunk_vlans': '200,201,202,203,204,205'}
                                           'LAG11': {'description': 'switch-3: trunk LAG',
                                                     'is_l2': True,
                                                     'l2_mode': 'trunk',
                                                     'trunk_vlans': '101'},
                                           'LAG3': {'description': 'switch-2: trunk LAG',
                                                    'is_l2': True,
                                                    'l2_mode': 'trunk',
                                                    'state': {'admin': 'up',
                                                              'bw_kbits': '20000000',
                                                              'description': 'switch-2: trunk LAG',
                                                              'duplex': 'Full',
                                                              'hardware': 'EtherChannel',
                                                              'lag_members': 'Ge4/6 Ge4/7',
                                                              'line': 'up',
                                                              'line_status': 'connected',
                                                              'link_type': '10Gb/s',
                                                              'mac': 'a89d.2163.3333',
                                                              'media_type': 'N/A',
                                                              'mtu': '1500'},
                            'node_facts': {'vlans': {'101': 'test_vlan', '200': 'ProdVMS'}}},
               'switch-2': {'cdp_peers': [{'source': 'switch-2',
                                           'src_label': 'Ge1/5',
                                           'target': {'bottom_label': 'cisco WS-C6509',
                                                      'id': 'switch-1',
                                                      'top_label': '10.1.1.1'},
                                           'trgt_label': 'Ge4/6'},
                                          {'source': 'switch-2',
                                           'src_label': 'Ge1/6',
                                           'target': {'bottom_label': 'cisco WS-C6509',
                                                      'id': 'switch-1',
                                                      'top_label': '10.1.1.1'},
                                           'trgt_label': 'Ge4/7'}],
                            'interfaces': {'Ge1/5': {'description': 'switch-1: trunk',
                                                     'is_l2': True,
                                                     'l2_mode': 'trunk',
                                                     'lag_id': '3',
                                                     'lag_mode': 'active',
                                                     'trunk_vlans': '200,201,202,203,204,205'}
                            'node_facts': {'vlans': {'101': 'test_vlan', '200': 'ProdVMS'}}}}}
```
</doc>

<vars>local_hostname="gethostname"</vars>

<macro>
def process_vlans(data):
    return {data["vid"]: data["name"]}
    
def check_is_physical_port(data):
    for item in _ttp_["vars"]["physical_ports"]:
        if data.startswith(item) and not "." in data:
            return data, {"is_physical_port": True}
    return data
</macro>

<macro>
def check_lldp_peer_name(data):
    undef_names = ["not advertised", "null"]
    if data.get("target.id", "").lower() in undef_names:
        data["target.id"] = data["data.chassis_id"]
    return data
</macro>

<input>url = "./Cisco_NXOS/"</input>

<!-- Interfaces configuration group -->
<group name="{{ local_hostname }}.interfaces**.{{ interface }}**">
interface {{ interface | resuball(IfsNormalize) }}
  description {{ description | re(".+") }}
  switchport {{ is_l2 | set(True) }}
  switchport access vlan {{ access_vlan }}
  switchport mode {{ l2_mode }}
  switchport trunk allowed vlan {{ trunk_vlans | unrange("-", ",") | joinmatches(",") }}
  switchport trunk allowed vlan add {{ trunk_vlans | unrange("-", ",") | joinmatches(",") }}
  channel-group {{ lag_id | DIGIT }} mode {{ lag_mode }}
  vpc {{ mlag_id | DIGIT }}
  mtu {{ mtu }}
  vrf member {{ vrf }}
  ip address {{ ip | joinmatches(",") }} 
  ip address {{ ip | joinmatches(",") }} secondary 
</group>

<group name="{{ local_hostname }}.interfaces**.{{ interface }}**.state">
{{ interface | _start_ | resuball(IfsNormalize) | macro("check_is_physical_port") }} is {{ admin | ORPHRASE }}, line protocol is {{ line }}, autostate enabled
{{ interface | _start_ | resuball(IfsNormalize) | macro("check_is_physical_port") }} is {{ line | ORPHRASE }}
admin state is {{ admin | ORPHRASE }},{{ ignore(".*") }}
admin state is {{ admin }}
  Belongs to {{ parent_lag }}
  Description: {{ description | re(".+") }} 
  Hardware: {{ hardware | ORPHRASE }}, address: {{ mac }} (bia {{ bia }})
  Hardware is {{ hardware }}, address is  {{ mac }}
  MTU {{ mtu }} bytes, BW {{ bw_kbits }} Kbit, DLY 10 usec
  Port mode is {{ mode }}
  {{ duplex }}-duplex, {{ link_speed | PHRASE }}, media type is {{ media_type }}
  {{ duplex }}-duplex, {{ link_speed | PHRASE | exclude(",media") }}
  Members in this channel: {{ lag_members | ORPHRASE }}
</group>

<!-- node_facts VLANs group -->
<group name="{{ local_hostname }}.node_facts.vlans**" macro="process_vlans">
vlan {{ vid | exclude(",") }}
  name {{ name | ORPHRASE | default("no name") }}
</group>

<!-- LLDP peers group -->
<group name="{{ local_hostname }}.lldp_peers*" chain="macro('check_lldp_peer_name') | expand()">
Chassis id: {{ data.chassis_id }}
Port id: {{ trgt_label | ORPHRASE | resuball(IfsNormalize) }}
Local Port id: {{ src_label | resuball(IfsNormalize) }}
Port Description: {{ data.peer_port_description | re(".+") }}
System Name: {{ target.id | ORPHRASE | split(".") | item(0) | split("(") | item(0) }}
System Description: {{ data.peer_system | ORPHRASE}}
System Capabilities: {{ data.peer_capabilities | ORPHRASE }}
Management Address: {{ target.top_label }}
{{ source | set("local_hostname") }}
</group>

<!-- CDP peers group -->
<group name="{{ local_hostname }}.cdp_peers*" expand="">
Device ID:{{ target.id | split(".") | item(0) | split("(") | item(0) }}
    IPv4 Address: {{ target.top_label }}
Platform: {{ target.bottom_label | ORPHRASE }}, Capabilities: {{ data.peer_capabilities | ORPHRASE }}
Interface: {{ src_label | resuball(IfsNormalize) }}, Port ID (outgoing port): {{ trgt_label | ORPHRASE | resuball(IfsNormalize) }}
MTU: {{ data.peer_mtu }}
Physical Location: {{ data.peer_location | ORPHRASE }}
{{ source | set("local_hostname") }}
</group>
</template>