[cfggen] Make Jinja2 Template Python 3 Compatible

Jinja2 templates rendered using Python 3 interpreter, are required
to conform with Python 3 new semantics.

singed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
This commit is contained in:
Tamer Ahmed 2020-09-28 20:21:46 -07:00 committed by Tamer Ahmed
parent 9122d16dd2
commit 6754635010
11 changed files with 31 additions and 31 deletions

View File

@ -3,7 +3,7 @@
! Vnet BGP instance
{% set interfaces_in_vnets = [] %}
{% block vnet_bgp_instance %}
{% for vnet_name, vnet_metadata in VNET.iteritems() %}
{% for vnet_name, vnet_metadata in VNET.items() %}
router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} vrf {{ vnet_name }}
no bgp default ipv4-unicast
bgp log-neighbor-changes
@ -19,9 +19,9 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} vrf {{ vnet_name }}
{% endfor %}
{# Got interfaces that belong this vnet #}
{% set interfaces_in_vnet = [] %}
{% for (key, metadata) in INTERFACE.iteritems() %}
{% if metadata.has_key("vnet_name") and metadata["vnet_name"] == vnet_name %}
{% for (name_prefix_pair, metadata) in INTERFACE.iteritems() %}
{% for (key, metadata) in INTERFACE.items() %}
{% if "vnet_name" in metadata and metadata["vnet_name"] == vnet_name %}
{% for (name_prefix_pair, metadata) in INTERFACE.items() %}
{% if key == name_prefix_pair[0] %}
{% if interfaces_in_vnet.append( name_prefix_pair[1] | ip ) %}
{% endif %}
@ -32,8 +32,8 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} vrf {{ vnet_name }}
{% endif %}
{% endfor %}
{# Each bgp neighbors #}
{% for neighbor_addr, bgp_session in BGP_NEIGHBOR.iteritems() %}
{% if bgp_session.has_key("local_addr") and bgp_session["local_addr"] in interfaces_in_vnet %}
{% for neighbor_addr, bgp_session in BGP_NEIGHBOR.items() %}
{% if "local_addr" in bgp_session and bgp_session["local_addr"] in interfaces_in_vnet %}
{% if bgp_session['asn'] | int != 0 %}
neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }}
neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }}
@ -42,7 +42,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }} vrf {{ vnet_name }}
or (bgp_session['holdtime'] is defined and bgp_session['holdtime'] | int != 180) %}
neighbor {{ neighbor_addr }} timers {{ bgp_session['keepalive'] }} {{ bgp_session['holdtime'] }}
{% endif %}
{% if bgp_session.has_key('admin_status') and bgp_session['admin_status'] == 'down' or not bgp_session.has_key('admin_status') and DEVICE_METADATA['localhost'].has_key('default_bgp_status') and DEVICE_METADATA['localhost']['default_bgp_status'] == 'down' %}
{% if 'admin_status' in bgp_session and bgp_session['admin_status'] == 'down' or 'admin_status' not in bgp_session and 'default_bgp_status' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['default_bgp_status'] == 'down' %}
neighbor {{ neighbor_addr }} shutdown
{% endif %}
{% if neighbor_addr | ipv4 %}

View File

@ -25,8 +25,8 @@
{% macro get_vnet_interfaces(interfaces) -%}
{% set L = namespace(intfs=[]) %}
{% set vnet_intfs = [] %}
{% for (key, metadata) in interfaces.iteritems() %}
{% if metadata.has_key("vnet_name") %}
{% for (key, metadata) in interfaces.items() %}
{% if "vnet_name" in metadata %}
{% set vnet_intfs = vnet_intfs.append(key) %}
{% endif %}
{% endfor %}

View File

@ -1,7 +1,7 @@
!
{% block vrf %}
{% if VNET is defined %}
{% for vnet_name, vnet_metadata in VNET.iteritems() %}
{% for vnet_name, vnet_metadata in VNET.items() %}
vrf {{ vnet_name }}
vni {{ vnet_metadata['vni'] }}
!

View File

@ -14,7 +14,7 @@ log facility local4
! enable password {# {{ en_passwd }} TODO: param needed #}
{% endblock system_init %}
!
{% if DEVICE_METADATA['localhost'].has_key('bgp_asn') %}
{% if 'bgp_asn' in DEVICE_METADATA['localhost'] %}
{% block bgp_init %}
!
! bgp multiple-instance
@ -58,7 +58,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
{% endfor %}
{% endblock vlan_advertisement %}
{% block bgp_sessions %}
{% for neighbor_addr, bgp_session in BGP_NEIGHBOR.iteritems() %}
{% for neighbor_addr, bgp_session in BGP_NEIGHBOR.items() %}
{% if bgp_session['asn'] | int != 0 %}
neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }}
neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }}
@ -67,7 +67,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
or (bgp_session['holdtime'] is defined and bgp_session['holdtime'] | int != 180) %}
neighbor {{ neighbor_addr }} timers {{ bgp_session['keepalive'] }} {{ bgp_session['holdtime'] }}
{% endif %}
{% if bgp_session.has_key('admin_status') and bgp_session['admin_status'] == 'down' or not bgp_session.has_key('admin_status') and DEVICE_METADATA['localhost'].has_key('default_bgp_status') and DEVICE_METADATA['localhost']['default_bgp_status'] == 'down' %}
{% if 'admin_status' in bgp_session and bgp_session['admin_status'] == 'down' or 'admin_status' not in bgp_session and 'default_bgp_status' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['default_bgp_status'] == 'down' %}
neighbor {{ neighbor_addr }} shutdown
{% endif %}
{% if neighbor_addr | ipv4 %}
@ -131,7 +131,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
{% endif %}
{% endblock bgp_peers_with_range %}
!
{% if DEVICE_METADATA['localhost'].has_key('bgp_asn') %}
{% if 'bgp_asn' in DEVICE_METADATA['localhost'] %}
maximum-paths 64
!
route-map ISOLATE permit 10

View File

@ -1,7 +1,7 @@
{% if MGMT_INTERFACE %}
{# If MGMT port alias is available, use it for port ID subtype, otherwise use port name #}
{% set mgmt_port_name = MGMT_INTERFACE.keys()[0][0] %}
{% set ipv4 = MGMT_INTERFACE.keys()[0][1].split('/')[0] %}
{% set mgmt_port_name = (MGMT_INTERFACE.keys()|list)[0][0] %}
{% set ipv4 = (MGMT_INTERFACE.keys()|list)[0][1].split('/')[0] %}
{% if MGMT_PORT and MGMT_PORT[mgmt_port_name] and MGMT_PORT[mgmt_port_name].alias %}
configure ports eth0 lldp portidsubtype local {{ MGMT_PORT[mgmt_port_name].alias }}
{% else %}

View File

@ -2,7 +2,7 @@
{% set ports_with_speed_set=[] %}
{% if PORT %}
{% for port in PORT %}
{% if PORT[port].has_key('speed') %}
{% if 'speed' in PORT[port] %}
{%- if ports_with_speed_set.append(port) -%}{%- endif -%}
{%- endif -%}
{% endfor %}

View File

@ -2,7 +2,7 @@
"DEVICE_METADATA": {{ DEVICE_METADATA | tojson }},
{% set ns = {'firstPrinted': False} -%}
"PORT": {
{%- for key,value in PORT.iteritems() -%}
{%- for key,value in PORT.items() -%}
{%- if ns.firstPrinted %},{% endif %}
"{{ key }}": {

View File

@ -58,7 +58,7 @@ class TestCfgGen(TestCase):
self.assertEqual(output.strip(), 'ACS-MSN2700')
def test_device_desc_mgmt_ip(self):
argument = '-v "MGMT_INTERFACE.keys()[0]" -M "' + self.sample_device_desc + '"'
argument = '-v "(MGMT_INTERFACE.keys()|list)[0]" -M "' + self.sample_device_desc + '"'
output = self.run_script(argument)
self.assertEqual(output.strip(), "('eth0', '10.0.1.5/28')")
@ -185,7 +185,7 @@ class TestCfgGen(TestCase):
)
def test_minigraph_interfaces(self):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "INTERFACE.keys()"'
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "INTERFACE.keys()|list"'
output = self.run_script(argument)
self.assertEqual(output.strip(), "[('Ethernet0', '10.0.0.58/31'), 'Ethernet0', ('Ethernet0', 'FC00::75/126')]")
@ -210,7 +210,7 @@ class TestCfgGen(TestCase):
)
def test_minigraph_vlan_interfaces(self):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "VLAN_INTERFACE.keys()"'
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "VLAN_INTERFACE.keys()|list"'
output = self.run_script(argument)
self.assertEqual(output.strip(), "[('Vlan1000', '192.168.0.1/27'), 'Vlan1000']")
@ -231,7 +231,7 @@ class TestCfgGen(TestCase):
)
def test_minigraph_portchannel_members(self):
argument = '-m "' + self.sample_graph_pc_test + '" -p "' + self.port_config + '" -v "PORTCHANNEL_MEMBER.keys()"'
argument = '-m "' + self.sample_graph_pc_test + '" -p "' + self.port_config + '" -v "PORTCHANNEL_MEMBER.keys()|list"'
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),
@ -239,7 +239,7 @@ class TestCfgGen(TestCase):
)
def test_minigraph_portchannel_interfaces(self):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "PORTCHANNEL_INTERFACE.keys()"'
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v "PORTCHANNEL_INTERFACE.keys()|list"'
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),
@ -305,7 +305,7 @@ class TestCfgGen(TestCase):
)
def test_minigraph_peers_with_range(self):
argument = '-m "' + self.sample_graph_bgp_speaker + '" -p "' + self.port_config + '" -v BGP_PEER_RANGE.values\(\)'
argument = "-m " + self.sample_graph_bgp_speaker + " -p " + self.port_config + " -v \"BGP_PEER_RANGE.values()|list\""
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),

View File

@ -46,7 +46,7 @@ class TestCfgGenPlatformJson(TestCase):
# Check whether all interfaces present or not as per platform.json
def test_platform_json_interfaces_keys(self):
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT.keys()"'
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT.keys()|list"'
output = self.run_script(argument)
expected = "['Ethernet8', 'Ethernet9', 'Ethernet36', 'Ethernet98', 'Ethernet0', 'Ethernet6', 'Ethernet4', 'Ethernet109', 'Ethernet108', 'Ethernet18', 'Ethernet100', 'Ethernet34', 'Ethernet104', 'Ethernet106', 'Ethernet94', 'Ethernet126', 'Ethernet96', 'Ethernet124', 'Ethernet90', 'Ethernet91', 'Ethernet92', 'Ethernet93', 'Ethernet50', 'Ethernet51', 'Ethernet52', 'Ethernet53', 'Ethernet54', 'Ethernet99', 'Ethernet56', 'Ethernet113', 'Ethernet76', 'Ethernet74', 'Ethernet39', 'Ethernet72', 'Ethernet73', 'Ethernet70', 'Ethernet71', 'Ethernet32', 'Ethernet33', 'Ethernet16', 'Ethernet111', 'Ethernet10', 'Ethernet11', 'Ethernet12', 'Ethernet13', 'Ethernet58', 'Ethernet19', 'Ethernet59', 'Ethernet38', 'Ethernet78', 'Ethernet68', 'Ethernet14', 'Ethernet89', 'Ethernet88', 'Ethernet118', 'Ethernet119', 'Ethernet116', 'Ethernet114', 'Ethernet80', 'Ethernet112', 'Ethernet86', 'Ethernet110', 'Ethernet84', 'Ethernet31', 'Ethernet49', 'Ethernet48', 'Ethernet46', 'Ethernet30', 'Ethernet29', 'Ethernet40', 'Ethernet120', 'Ethernet28', 'Ethernet66', 'Ethernet60', 'Ethernet64', 'Ethernet44', 'Ethernet20', 'Ethernet79', 'Ethernet69', 'Ethernet24', 'Ethernet26']"

View File

@ -70,7 +70,7 @@ class TestCfgGenCaseInsensitive(TestCase):
# self.assertEqual(output.strip(), "{'everflow0': {'src_ip': '10.1.0.32', 'dst_ip': '10.0.100.1'}}")
def test_minigraph_interfaces(self):
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v \'INTERFACE.keys()\''
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v \'INTERFACE.keys()|list\''
output = self.run_script(argument)
self.assertEqual(output.strip(), "[('Ethernet0', '10.0.0.58/31'), 'Ethernet0', ('Ethernet0', 'FC00::75/126')]")
@ -88,7 +88,7 @@ class TestCfgGenCaseInsensitive(TestCase):
self.assertEqual(output.strip(), "{('Vlan1000', 'Ethernet8'): {'tagging_mode': 'untagged'}}")
def test_minigraph_vlan_interfaces(self):
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "VLAN_INTERFACE.keys()"'
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "VLAN_INTERFACE.keys()|list"'
output = self.run_script(argument)
self.assertEqual(output.strip(), "[('Vlan1000', '192.168.0.1/27'), 'Vlan1000']")

View File

@ -139,7 +139,7 @@ class TestMultiNpuCfgGen(TestCase):
'PortChannel4014': {'admin_status': 'up', 'min_links': '2', 'members': ['Ethernet-BP392', 'Ethernet-BP396'], 'mtu': '9100'}})
def test_frontend_asic_portchannel_mem(self):
argument = "-m {} -p {} -n asic0 --var-json \"PORTCHANNEL_MEMBER\"".format(self.sample_graph, self.port_config[0])
argument = "-m {} -p {} -n asic0 -v \"PORTCHANNEL_MEMBER.keys()|list\"".format(self.sample_graph, self.port_config[0])
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),
@ -147,7 +147,7 @@ class TestMultiNpuCfgGen(TestCase):
)
def test_backend_asic_portchannels_mem(self):
argument = "-m {} -p {} -n asic3 --var-json \"PORTCHANNEL_MEMBER\"".format(self.sample_graph, self.port_config[3])
argument = "-m {} -p {} -n asic3 -v \"PORTCHANNEL_MEMBER.keys()|list\"".format(self.sample_graph, self.port_config[3])
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),
@ -155,7 +155,7 @@ class TestMultiNpuCfgGen(TestCase):
)
def test_frontend_asic_portchannel_intf(self):
argument = "-m {} -p {} -n asic0 --var-json \"PORTCHANNEL_INTERFACE\"".format(self.sample_graph, self.port_config[0])
argument = "-m {} -p {} -n asic0 -v \"PORTCHANNEL_INTERFACE.keys()|list\"".format(self.sample_graph, self.port_config[0])
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),
@ -163,7 +163,7 @@ class TestMultiNpuCfgGen(TestCase):
)
def test_backend_asic_portchannel_intf(self):
argument = "-m {} -p {} -n asic3 --var-json \"PORTCHANNEL_INTERFACE\"".format(self.sample_graph, self.port_config[3])
argument = "-m {} -p {} -n asic3 -v \"PORTCHANNEL_INTERFACE.keys()|list\"".format(self.sample_graph, self.port_config[3])
output = self.run_script(argument)
self.assertEqual(
utils.liststr_to_dict(output.strip()),