{%- if configuration.asset.version -%}
version {{ configuration.asset.version | default('15.2') }}
{%- endif %}
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
service compress-config
!
{%- if configuration.system.config.hostname %}
hostname {{ configuration.system.config.hostname.value }}
{%- endif %}
!
boot-start-marker
boot-end-marker
!
!
{%- if configuration.system.logging -%}
{%-   if configuration.system.logging.config and configuration.system.logging.config.get('buffer_size') %}
logging buffered {{ configuration.system.logging.config['buffer_size'].value | default(4096) }}
{%-   endif %}
{%- endif %}
{%- for ni in configuration.network_instances.values() %}
{%-   if ni.name.value != "global" %}
vrf definition {{ ni.name.value }}
 !
 address-family ipv4
 exit-address-family
 !
 address-family ipv6
 exit-address-family
!
{%-   endif %}
{%- endfor %}
!
!
!
{%- if configuration.system.aaa.config is defined and configuration.system.aaa.config.enabled %}
aaa new-model
{%- else %}
no aaa new-model
{%- endif %}
no process cpu extended history
no process cpu autoprofile hog
!
!
!
!
!
!
!
!
{%- if configuration.system.config.domain_name is defined and configuration.system.config.domain_name %}
ip domain-name {{ configuration.system.config.domain_name.value }}
{%- endif %}
{%- if configuration.cef is not defined or configuration.cef %}
ip cef
{%- endif %}
no ipv6 cef
!
!
!
spanning-tree mode pvst
spanning-tree extend system-id
!
!
!
!
!
!
!
!
!
!
!
!
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "softwareLoopback" %}
interface Loopback{{ intf.index.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-  endif %}
{%- endfor %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ieee8023adLag" %}
interface {{ intf.name }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%- if 'ipv4' in intf %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%- endif %}
{%-  if intf.switchport and intf.switchport.value %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "access" %}
{%-      if intf.native_vlan %}
 switchport access vlan {{ intf.native_vlan.value }}
{%-      endif %}
 switchport mode access
{%-    endif %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "trunk" %}
{%-      if intf.trunk_allowed_vlans %}
 switchport trunk allowed vlan {{ intf.trunk_allowed_vlans.value | join(',') }}
{%-      endif %}
{%-      if intf.native_vlan %}
 switchport trunk native vlan {{ intf.native_vlan.value }}
{%-      endif %}
 switchport trunk encapsulation dot1q
 switchport mode trunk
{%-    endif %}
{%-  endif %}
{%-  if intf.enabled and not intf.enabled.value %}
 shutdown
{%-   endif %}
{%-  endif %}
{%-  if intf.type == "ieee8023adLag" %}
!
{%-  endif %}
{%- endfor %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ethernetCsmacd" %}
interface {{ intf.name }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%- if 'ipv4' in intf %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%- endif %}
{%-  if intf.switchport and intf.switchport.value %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "access" %}
{%-      if intf.native_vlan %}
 switchport access vlan {{ intf.native_vlan.value }}
{%-      endif %}
 switchport mode access
{%-    endif %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "trunk" %}
{%-      if intf.trunk_allowed_vlans %}
 switchport trunk allowed vlan {{ intf.trunk_allowed_vlans.value | join(',') }}
{%-      endif %}
{%-      if intf.native_vlan %}
 switchport trunk native vlan {{ intf.native_vlan.value }}
{%-      endif %}
 switchport trunk encapsulation dot1q
 switchport mode trunk
{%-  endif %}
{%-  if intf.lacp_enabled %}
 channel-group {{ intf.aggregate_id.value }} mode {{ intf.lacp_mode.value | default('active') }}
{%-    if intf.lacp_port_priority %}
 lacp port-priority {{ intf.lacp_port_priority.value }}
{%-    endif %}
{%-    if intf.lacp_interval %}
 lacp rate {{ intf.lacp_interval.value }}
{%-    endif %}
{%-  endif %}
{%-  endif %}
{%-  if intf.enabled and not intf.enabled.value %}
 shutdown
{%-   endif %}
{%-  endif %}
{%- if 'negotiation' in intf and intf.negotiation and intf.negotiation.value %}
 negotiation auto
{%- endif %}
{%-  if intf.type == "ethernetCsmacd" %}
!
{%-  endif %}
{%- endfor %}
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "l3ipvlan" and intf.vlan_id %}
interface Vlan{{ intf.vlan_id.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-    if intf.enabled and not intf.enabled.value %}
 shutdown
{%-     endif %}
{%-  endif %}
{%-  if intf.type == "l3ipvlan" %}
!
{%-  endif %}
{%- endfor %}
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ManagementInterface" and intf.name %}
interface {{ intf.name.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-    if intf.enabled and not intf.enabled.value %}
 shutdown
{%-     endif %}
{%-  endif %}
{%-  if intf.type == "ManagementInterface" and intf.name %}
!
{%-  endif %}
{%- endfor %}
{%- if configuration.system.config.default_gateway is defined and configuration.system.config.default_gateway %}
ip default-gateway {{ configuration.system.config.default_gateway }}
{%- endif %}
ip forward-protocol nd
!
ip http server
ip http secure-server
!
{%- for _,ni in configuration.network_instances.items() %}
{%-   if ni.protocols.static_routes is not none %}
{%-      for _,route in ni.protocols.static_routes.items() %}
{%-        if route.network_instance.value != "global" %}
{%-          for _,next_hop in route.next_hops.items() %}
{%-            if next_hop.metric.value is defined %}
ip route vrf {{ route.network_instance.value }} {{ route.prefix.value }} {{ next_hop.next_hop.value }} metric {{ next_hop.metric.value }}
{%-            else %}
ip route vrf {{ route.network_instance.value }} {{ route.prefix.value }} {{ next_hop.next_hop.value }}
{%-            endif %}
{%-          endfor %}
{%-        else %}
{%-          for _,next_hop in route.next_hops.items() %}
{%-            if next_hop.metric.value is defined %}
ip route {{ route.prefix.value }} {{ next_hop.next_hop.value }} metric {{ next_hop.metric.value }}
{%-            else %}
ip route {{ route.prefix.value }} {{ next_hop.next_hop.value }}
{%-            endif %}
{%-          endfor %}
{%-        endif %}
{%-      endfor %}
{%-   endif %}
{%- endfor %}
{%- set ssh = configuration.system.ssh.config -%}
{%- if ssh and ssh.get('enable') and ssh.get('enable').value %}
{%-   if ssh.get('source_interface') %}
ip ssh source-interface {{ ssh['source_interface'] }}
{%-   endif %}
{%-   if ssh.get('protocol_version') %}
ip ssh version {{ ssh.get('protocol_version').value | default(2) }}
{%-   endif %}
{%-   if ssh.get('timeout') %}
ip ssh time-out {{ ssh.get('timeout').value }}
{%-   endif %}
{%-   if ssh.get('auth_retries') %}
ip ssh authentication-retries {{ ssh.get('auth_retries').value }}
{%-   endif %}
{%- endif %}
ip ssh server algorithm encryption aes128-ctr aes192-ctr aes256-ctr
ip ssh client algorithm encryption aes128-ctr aes192-ctr aes256-ctr
!
{%- if "lacp" in configuration -%}
{%-   if configuration.lacp.config.system_priority %}
lacp system-priority {{ configuration.lacp.config.system_priority.value }}
{%-   endif %}
{%-   if configuration.lacp.config.system_id_mac %}
lacp system-id mac {{ configuration.lacp.config.system_id_mac.value }}
{%-   endif %}
{%-   if configuration.lacp.config.load_balance_algorithm %}
{%-     set alg = configuration.lacp.config.load_balance_algorithm.value %}
{%-     if alg is string %}
port-channel load-balance {{ alg }}
{%-     elif alg is sequence and alg | length >= 2 %}
port-channel load-balance extended {{ alg | join(' ') }}
{%-     elif alg is sequence and alg | length == 1 %}
port-channel load-balance {{ alg[0] }}
{%-     else %}
port-channel load-balance {{ alg }}
{%-     endif %}
{%-   endif %}
{%- endif %}
!
{%- if "snmp" in configuration -%}
{%- if configuration.snmp.community %}
snmp-server community {{ configuration.snmp.community }} {{ configuration.snmp.access | default('RO') }}
{%- endif %}
{%- if configuration.snmp.location %}
snmp-server location {{ configuration.snmp.location }}
{%- endif %}
{%- if configuration.snmp.contact %}
snmp-server contact {{ configuration.snmp.contact }}
{%- endif %}
{%- endif %}
{%- if configuration.system.logging -%}
{%-   if configuration.system.logging.get('remote_servers') %}
{%-     for server_group in configuration.system.logging.get('remote_servers').values() %}
{%-       for _, server in server_group.items() %}
{%-         if server.host and server.host.value %}
{%-           if server.source_address and server.source_address.value %}
logging source-interface {{ server.source_address.value }}
{%-           endif %}
logging host {{ server.host.value }}
{%-         endif %}
{%-       endfor %}
{%-     endfor %}
{%-   endif %}
{%- endif %}
!
!
!
!
control-plane
!
{%- if configuration.system.config.login_banner is not none and configuration.system.config.login_banner.value %}
banner login ^C
{{ configuration.system.config.login_banner.value }}
^C
{%- endif %}
{%- if configuration.system.config.motd_banner is not none and configuration.system.config.motd_banner.value %}
banner motd ^C{{ configuration.system.config.motd_banner.value  }}
^C
{%- endif %}
!
line con 0
line aux 0
line vty 0 4
 login local
 transport input ssh
!
{%- if configuration.system.ntp -%}
{%- for server in configuration.system.ntp.servers.values() %}
{%-   if server.prefer %}
ntp server {{ server.address.value }} prefer
{%-   else %}
ntp server {{ server.address.value }}
{%-   endif %}
{%- endfor %}
{%- endif %}
!
end