[cfggen] Conform With Python 3 Syntax (#5154)
Preparing sonic-cfggen for migration to Python 3. signed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
This commit is contained in:
parent
f5746f3189
commit
ec11308a66
@ -13,7 +13,7 @@ def generate_t1_sample_config(data):
|
||||
data['INTERFACE'] = {}
|
||||
port_count = 0
|
||||
total_port_amount = len(data['PORT'])
|
||||
for port in natsorted(data['PORT'].keys()):
|
||||
for port in natsorted(data['PORT']):
|
||||
data['PORT'][port]['admin_status'] = 'up'
|
||||
data['PORT'][port]['mtu'] = '9100'
|
||||
local_addr = '10.0.{}.{}'.format(2 * port_count / 256, 2 * port_count % 256)
|
||||
@ -35,22 +35,22 @@ def generate_t1_sample_config(data):
|
||||
|
||||
def generate_empty_config(data):
|
||||
new_data = {'DEVICE_METADATA': data['DEVICE_METADATA']}
|
||||
if not new_data['DEVICE_METADATA']['localhost'].has_key('hostname'):
|
||||
if 'hostname' not in new_data['DEVICE_METADATA']['localhost']:
|
||||
new_data['DEVICE_METADATA']['localhost']['hostname'] = 'sonic'
|
||||
if not new_data['DEVICE_METADATA']['localhost'].has_key('type'):
|
||||
if 'type' not in new_data['DEVICE_METADATA']['localhost']:
|
||||
new_data['DEVICE_METADATA']['localhost']['type'] = 'LeafRouter'
|
||||
return new_data
|
||||
|
||||
def generate_l2_config(data):
|
||||
if not data['DEVICE_METADATA']['localhost'].has_key('hostname'):
|
||||
if 'hostname' not in data['DEVICE_METADATA']['localhost']:
|
||||
data['DEVICE_METADATA']['localhost']['hostname'] = 'sonic'
|
||||
if not data['DEVICE_METADATA']['localhost'].has_key('type'):
|
||||
if 'type' not in data['DEVICE_METADATA']['localhost']:
|
||||
data['DEVICE_METADATA']['localhost']['type'] = 'ToRRouter'
|
||||
data['VLAN'] = {'Vlan1000': {'vlanid': '1000'}}
|
||||
vp = natsorted(data['PORT'].keys())
|
||||
vp = natsorted(list(data['PORT'].keys()))
|
||||
data['VLAN']['Vlan1000'].setdefault('members', vp)
|
||||
data['VLAN_MEMBER'] = {}
|
||||
for port in natsorted(data['PORT'].keys()):
|
||||
for port in natsorted(data['PORT']):
|
||||
data['PORT'][port].setdefault('admin_status', 'up')
|
||||
data['VLAN_MEMBER']['Vlan1000|{}'.format(port)] = {'tagging_mode': 'untagged'}
|
||||
return data
|
||||
@ -62,7 +62,7 @@ _sample_generators = {
|
||||
}
|
||||
|
||||
def get_available_config():
|
||||
return _sample_generators.keys()
|
||||
return list(_sample_generators.keys())
|
||||
|
||||
def generate_sample_config(data, setting_name):
|
||||
return _sample_generators[setting_name.lower()](data)
|
||||
|
@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import calendar
|
||||
import math
|
||||
import os
|
||||
@ -124,13 +125,13 @@ def parse_png(png, hname):
|
||||
bandwidth_node = link.find(str(QName(ns, "Bandwidth")))
|
||||
bandwidth = bandwidth_node.text if bandwidth_node is not None else None
|
||||
if enddevice.lower() == hname.lower():
|
||||
if port_alias_map.has_key(endport):
|
||||
if endport in port_alias_map:
|
||||
endport = port_alias_map[endport]
|
||||
neighbors[endport] = {'name': startdevice, 'port': startport}
|
||||
if bandwidth:
|
||||
port_speeds[endport] = bandwidth
|
||||
elif startdevice.lower() == hname.lower():
|
||||
if port_alias_map.has_key(startport):
|
||||
if startport in port_alias_map:
|
||||
startport = port_alias_map[startport]
|
||||
neighbors[startport] = {'name': enddevice, 'port': endport}
|
||||
if bandwidth:
|
||||
@ -175,14 +176,14 @@ def parse_asic_external_link(link, asic_name, hostname):
|
||||
# if chassis internal is false, the interface name will be
|
||||
# interface alias which should be converted to asic port name
|
||||
if (enddevice.lower() == hostname.lower()):
|
||||
if ((port_alias_asic_map.has_key(endport)) and
|
||||
if ((endport in port_alias_asic_map) and
|
||||
(asic_name.lower() in port_alias_asic_map[endport].lower())):
|
||||
endport = port_alias_asic_map[endport]
|
||||
neighbors[port_alias_map[endport]] = {'name': startdevice, 'port': startport}
|
||||
if bandwidth:
|
||||
port_speeds[port_alias_map[endport]] = bandwidth
|
||||
elif (startdevice.lower() == hostname.lower()):
|
||||
if ((port_alias_asic_map.has_key(startport)) and
|
||||
if ((startport in port_alias_asic_map) and
|
||||
(asic_name.lower() in port_alias_asic_map[startport].lower())):
|
||||
startport = port_alias_asic_map[startport]
|
||||
neighbors[port_alias_map[startport]] = {'name': enddevice, 'port': endport}
|
||||
@ -202,14 +203,14 @@ def parse_asic_internal_link(link, asic_name, hostname):
|
||||
bandwidth = bandwidth_node.text if bandwidth_node is not None else None
|
||||
if ((enddevice.lower() == asic_name.lower()) and
|
||||
(startdevice.lower() != hostname.lower())):
|
||||
if port_alias_map.has_key(endport):
|
||||
if endport in port_alias_map:
|
||||
endport = port_alias_map[endport]
|
||||
neighbors[endport] = {'name': startdevice, 'port': startport}
|
||||
if bandwidth:
|
||||
port_speeds[endport] = bandwidth
|
||||
elif ((startdevice.lower() == asic_name.lower()) and
|
||||
(enddevice.lower() != hostname.lower())):
|
||||
if port_alias_map.has_key(startport):
|
||||
if startport in port_alias_map:
|
||||
startport = port_alias_map[startport]
|
||||
neighbors[startport] = {'name': enddevice, 'port': endport}
|
||||
if bandwidth:
|
||||
@ -288,7 +289,7 @@ def parse_dpg(dpg, hname):
|
||||
if vni_element.text.isdigit():
|
||||
vni = int(vni_element.text)
|
||||
else:
|
||||
print >> sys.stderr, "VNI must be an integer (use default VNI %d instead)" % vni_default
|
||||
print("VNI must be an integer (use default VNI %d instead)" % vni_default, file=sys.stderr)
|
||||
|
||||
ipintfs = child.find(str(QName(ns, "IPInterfaces")))
|
||||
intfs = {}
|
||||
@ -391,18 +392,18 @@ def parse_dpg(dpg, hname):
|
||||
# decide an ACL is a Control Plane ACL if acl_intfs is empty below.
|
||||
for member in aclattach:
|
||||
member = member.strip()
|
||||
if pcs.has_key(member):
|
||||
if member in pcs:
|
||||
# If try to attach ACL to a LAG interface then we shall add the LAG to
|
||||
# to acl_intfs directly instead of break it into member ports, ACL attach
|
||||
# to LAG will be applied to all the LAG members internally by SAI/SDK
|
||||
acl_intfs.append(member)
|
||||
elif vlans.has_key(member):
|
||||
elif member in vlans:
|
||||
acl_intfs.append(member)
|
||||
elif port_alias_map.has_key(member):
|
||||
elif member in port_alias_map:
|
||||
acl_intfs.append(port_alias_map[member])
|
||||
# Give a warning if trying to attach ACL to a LAG member interface, correct way is to attach ACL to the LAG interface
|
||||
if port_alias_map[member] in intfs_inpc:
|
||||
print >> sys.stderr, "Warning: ACL " + aclname + " is attached to a LAG member interface " + port_alias_map[member] + ", instead of LAG interface"
|
||||
print("Warning: ACL " + aclname + " is attached to a LAG member interface " + port_alias_map[member] + ", instead of LAG interface", file=sys.stderr)
|
||||
elif member.lower().startswith('erspan') or member.lower().startswith('egress_erspan'):
|
||||
if member.lower().startswith('erspanv6') or member.lower().startswith('egress_erspanv6'):
|
||||
is_mirror_v6 = True
|
||||
@ -439,9 +440,9 @@ def parse_dpg(dpg, hname):
|
||||
# append the service to our list of services
|
||||
if aclname in acls:
|
||||
if acls[aclname]['type'] != 'CTRLPLANE':
|
||||
print >> sys.stderr, "Warning: ACL '%s' type mismatch. Not updating ACL." % aclname
|
||||
print("Warning: ACL '%s' type mismatch. Not updating ACL." % aclname, file=sys.stderr)
|
||||
elif acls[aclname]['services'] == aclservice:
|
||||
print >> sys.stderr, "Warning: ACL '%s' already contains service '%s'. Not updating ACL." % (aclname, aclservice)
|
||||
print("Warning: ACL '%s' already contains service '%s'. Not updating ACL." % (aclname, aclservice), file=sys.stderr)
|
||||
else:
|
||||
acls[aclname]['services'].append(aclservice)
|
||||
else:
|
||||
@ -450,7 +451,7 @@ def parse_dpg(dpg, hname):
|
||||
'stage': stage,
|
||||
'services': [aclservice]}
|
||||
except:
|
||||
print >> sys.stderr, "Warning: Ignoring Control Plane ACL %s without type" % aclname
|
||||
print("Warning: Ignoring Control Plane ACL %s without type" % aclname, file=sys.stderr)
|
||||
|
||||
return intfs, lo_intfs, mvrf, mgmt_intf, vlans, vlan_members, pcs, pc_members, acls, vni
|
||||
return None, None, None, None, None, None, None, None, None, None
|
||||
@ -530,8 +531,8 @@ def parse_cpg(cpg, hname):
|
||||
if hostname.lower() == bgp_session['name'].lower():
|
||||
bgp_session['asn'] = asn
|
||||
|
||||
bgp_monitors = { key: bgp_sessions[key] for key in bgp_sessions if bgp_sessions[key].has_key('asn') and bgp_sessions[key]['name'] == 'BGPMonitor' }
|
||||
bgp_sessions = { key: bgp_sessions[key] for key in bgp_sessions if bgp_sessions[key].has_key('asn') and int(bgp_sessions[key]['asn']) != 0 }
|
||||
bgp_monitors = { key: bgp_sessions[key] for key in bgp_sessions if 'asn' in bgp_sessions[key] and bgp_sessions[key]['name'] == 'BGPMonitor' }
|
||||
bgp_sessions = { key: bgp_sessions[key] for key in bgp_sessions if 'asn' in bgp_sessions[key] and int(bgp_sessions[key]['asn']) != 0 }
|
||||
|
||||
return bgp_sessions, myasn, bgp_peers_with_range, bgp_monitors
|
||||
|
||||
@ -696,7 +697,7 @@ def parse_spine_chassis_fe(results, vni, lo_intfs, phyport_intfs, pc_intfs, pc_m
|
||||
break
|
||||
|
||||
if intf_name == None:
|
||||
print >> sys.stderr, 'Warning: cannot find any interfaces that belong to %s' % (pc_intf)
|
||||
print('Warning: cannot find any interfaces that belong to %s' % (pc_intf), file=sys.stderr)
|
||||
continue
|
||||
|
||||
# Get the neighbor router of this port channel interface
|
||||
@ -734,7 +735,7 @@ def filter_acl_table_bindings(acls, neighbors, port_channels, sub_role):
|
||||
if not backend_port_channel:
|
||||
front_port_channel_intf.append(port_channel_intf)
|
||||
|
||||
for acl_table, group_params in acls.iteritems():
|
||||
for acl_table, group_params in acls.items():
|
||||
group_type = group_params.get('type', None)
|
||||
filter_acls[acl_table] = acls[acl_table]
|
||||
|
||||
@ -763,7 +764,7 @@ def filter_acl_table_bindings(acls, neighbors, port_channels, sub_role):
|
||||
active_ports = [port for port in front_panel_ports if port in neighbors.keys() or port in front_port_channel_intf]
|
||||
|
||||
if not active_ports:
|
||||
print >> sys.stderr, 'Warning: mirror table {} in ACL_TABLE does not have any ports bound to it'.format(acl_table)
|
||||
print('Warning: mirror table {} in ACL_TABLE does not have any ports bound to it'.format(acl_table), file=sys.stderr)
|
||||
|
||||
filter_acls[acl_table]['ports'] = active_ports
|
||||
|
||||
@ -921,14 +922,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
results['BGP_PEER_RANGE'] = bgp_peers_with_range
|
||||
if mgmt_routes:
|
||||
# TODO: differentiate v4 and v6
|
||||
mgmt_intf.itervalues().next()['forced_mgmt_routes'] = mgmt_routes
|
||||
iter(mgmt_intf.values()).next()['forced_mgmt_routes'] = mgmt_routes
|
||||
results['MGMT_PORT'] = {}
|
||||
results['MGMT_INTERFACE'] = {}
|
||||
mgmt_intf_count = 0
|
||||
mgmt_alias_reverse_mapping = {}
|
||||
for key in mgmt_intf:
|
||||
alias = key[0]
|
||||
if mgmt_alias_reverse_mapping.has_key(alias):
|
||||
if alias in mgmt_alias_reverse_mapping:
|
||||
name = mgmt_alias_reverse_mapping[alias]
|
||||
else:
|
||||
name = 'eth' + str(mgmt_intf_count)
|
||||
@ -953,14 +954,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
phyport_intfs = {}
|
||||
vlan_intfs = {}
|
||||
pc_intfs = {}
|
||||
vlan_invert_mapping = { v['alias']:k for k,v in vlans.items() if v.has_key('alias') }
|
||||
vlan_invert_mapping = { v['alias']:k for k,v in vlans.items() if 'alias' in v }
|
||||
vlan_sub_intfs = {}
|
||||
|
||||
for intf in intfs:
|
||||
if intf[0][0:4] == 'Vlan':
|
||||
vlan_intfs[intf] = {}
|
||||
vlan_intfs[intf[0]] = {}
|
||||
elif vlan_invert_mapping.has_key(intf[0]):
|
||||
elif intf[0] in vlan_invert_mapping:
|
||||
vlan_intfs[(vlan_invert_mapping[intf[0]], intf[1])] = {}
|
||||
vlan_intfs[vlan_invert_mapping[intf[0]]] = {}
|
||||
elif intf[0][0:11] == 'PortChannel':
|
||||
@ -975,7 +976,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
|
||||
for port_name in port_speeds_default:
|
||||
# ignore port not in port_config.ini
|
||||
if not ports.has_key(port_name):
|
||||
if port_name not in ports:
|
||||
continue
|
||||
|
||||
ports.setdefault(port_name, {})['speed'] = port_speeds_default[port_name]
|
||||
@ -985,12 +986,12 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
# If no port_config_file is found ports is empty so ignore this error
|
||||
if port_config_file is not None:
|
||||
if port_name not in ports:
|
||||
print >> sys.stderr, "Warning: ignore interface '%s' as it is not in the port_config.ini" % port_name
|
||||
print("Warning: ignore interface '%s' as it is not in the port_config.ini" % port_name, file=sys.stderr)
|
||||
continue
|
||||
|
||||
ports.setdefault(port_name, {})['speed'] = port_speed_png[port_name]
|
||||
|
||||
for port_name, port in ports.items():
|
||||
for port_name, port in list(ports.items()):
|
||||
# get port alias from port_config.ini
|
||||
alias = port.get('alias', port_name)
|
||||
# generate default 100G FEC
|
||||
@ -1001,14 +1002,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
# set port description if parsed from deviceinfo
|
||||
for port_name in port_descriptions:
|
||||
# ignore port not in port_config.ini
|
||||
if not ports.has_key(port_name):
|
||||
if port_name not in ports:
|
||||
continue
|
||||
|
||||
ports.setdefault(port_name, {})['description'] = port_descriptions[port_name]
|
||||
|
||||
for port_name, port in ports.items():
|
||||
if not port.get('description'):
|
||||
if neighbors.has_key(port_name):
|
||||
if port_name in neighbors:
|
||||
# for the ports w/o description set it to neighbor name:port
|
||||
port['description'] = "%s:%s" % (neighbors[port_name]['name'], neighbors[port_name]['port'])
|
||||
else:
|
||||
@ -1016,11 +1017,11 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
port['description'] = port.get('alias', port_name)
|
||||
|
||||
# set default port MTU as 9100
|
||||
for port in ports.itervalues():
|
||||
for port in ports.values():
|
||||
port['mtu'] = '9100'
|
||||
|
||||
# asymmetric PFC is disabled by default
|
||||
for port in ports.itervalues():
|
||||
for port in ports.values():
|
||||
port['pfc_asym'] = 'off'
|
||||
|
||||
# set physical port default admin status up
|
||||
@ -1028,7 +1029,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
if port[0] in ports:
|
||||
ports.get(port[0])['admin_status'] = 'up'
|
||||
|
||||
for member in pc_members.keys() + vlan_members.keys():
|
||||
for member in list(pc_members.keys()) + list(vlan_members.keys()):
|
||||
port = ports.get(member[1])
|
||||
if port:
|
||||
port['admin_status'] = 'up'
|
||||
@ -1047,11 +1048,11 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
# remove portchannels that contain ports not existing in port_config.ini
|
||||
# when port_config.ini exists
|
||||
if not set(mbr_map['members']).issubset(port_set):
|
||||
print >> sys.stderr, "Warning: ignore '%s' as part of its member interfaces is not in the port_config.ini" % pc_name
|
||||
print("Warning: ignore '%s' as part of its member interfaces is not in the port_config.ini" % pc_name, file=sys.stderr)
|
||||
del pcs[pc_name]
|
||||
|
||||
# set default port channel MTU as 9100 and admin status up
|
||||
for pc in pcs.itervalues():
|
||||
for pc in pcs.values():
|
||||
pc['mtu'] = '9100'
|
||||
pc['admin_status'] = 'up'
|
||||
|
||||
@ -1061,7 +1062,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
for pc_intf in pc_intfs.keys():
|
||||
# remove portchannels not in PORTCHANNEL dictionary
|
||||
if isinstance(pc_intf, tuple) and pc_intf[0] not in pcs:
|
||||
print >> sys.stderr, "Warning: ignore '%s' interface '%s' as '%s' is not in the valid PortChannel list" % (pc_intf[0], pc_intf[1], pc_intf[0])
|
||||
print("Warning: ignore '%s' interface '%s' as '%s' is not in the valid PortChannel list" % (pc_intf[0], pc_intf[1], pc_intf[0]), file=sys.stderr)
|
||||
del pc_intfs[pc_intf]
|
||||
pc_intfs.pop(pc_intf[0], None)
|
||||
|
||||
@ -1100,7 +1101,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
# remove port not in port_config.ini
|
||||
if nghbr not in ports:
|
||||
if port_config_file is not None:
|
||||
print >> sys.stderr, "Warning: ignore interface '%s' in DEVICE_NEIGHBOR as it is not in the port_config.ini" % nghbr
|
||||
print("Warning: ignore interface '%s' in DEVICE_NEIGHBOR as it is not in the port_config.ini" % nghbr, file=sys.stderr)
|
||||
del neighbors[nghbr]
|
||||
results['DEVICE_NEIGHBOR'] = neighbors
|
||||
if asic_name is None:
|
||||
@ -1198,4 +1199,4 @@ port_alias_asic_map = {}
|
||||
|
||||
def print_parse_xml(filename):
|
||||
results = parse_xml(filename)
|
||||
print(json.dumps(results, indent=3, cls=minigraph_encoder))
|
||||
print((json.dumps(results, indent=3, cls=minigraph_encoder)))
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -42,7 +42,7 @@ def readJson(filename):
|
||||
data_dict = ast.literal_eval(json.dumps(data))
|
||||
return data_dict
|
||||
except Exception as e:
|
||||
print("error occurred while parsing json:", sys.exc_info()[1])
|
||||
print("error occurred while parsing json: {}".format(sys.exc_info()[1]))
|
||||
return None
|
||||
|
||||
def db_connect_configdb():
|
||||
|
@ -6,29 +6,53 @@ import unittest
|
||||
import glob
|
||||
|
||||
def get_test_suite():
|
||||
test_loader = unittest.TestLoader()
|
||||
test_suite = test_loader.discover('tests', pattern='*.py')
|
||||
return test_suite
|
||||
test_loader = unittest.TestLoader()
|
||||
test_suite = test_loader.discover('tests', pattern='*.py')
|
||||
return test_suite
|
||||
|
||||
setup(name='sonic-config-engine',
|
||||
version='1.0',
|
||||
description='Utilities for generating SONiC configuration files',
|
||||
author='Taoyu Li',
|
||||
author_email='taoyl@microsoft.com',
|
||||
url='https://github.com/Azure/sonic-buildimage',
|
||||
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'config_samples', 'redis_bcc', 'lazy_re'],
|
||||
scripts=['sonic-cfggen'],
|
||||
install_requires=[
|
||||
'ipaddr',
|
||||
'jinja2>=2.10',
|
||||
'lxml',
|
||||
'netaddr',
|
||||
'pyyaml',
|
||||
'pyangbind==0.6.0',
|
||||
'sonic-py-common'
|
||||
],
|
||||
test_suite='setup.get_test_suite',
|
||||
data_files=[
|
||||
setup(
|
||||
name = 'sonic-config-engine',
|
||||
version = '1.0',
|
||||
description = 'Utilities for generating SONiC configuration files',
|
||||
author='Taoyu Li',
|
||||
author_email='taoyl@microsoft.com',
|
||||
url = 'https://github.com/Azure/sonic-buildimage',
|
||||
py_modules = [
|
||||
'portconfig',
|
||||
'minigraph',
|
||||
'openconfig_acl',
|
||||
'config_samples',
|
||||
'redis_bcc',
|
||||
'lazy_re',
|
||||
],
|
||||
scripts = [
|
||||
'sonic-cfggen',
|
||||
],
|
||||
install_requires = [
|
||||
'future',
|
||||
'ipaddr',
|
||||
'jinja2>=2.10',
|
||||
'lxml',
|
||||
'netaddr',
|
||||
'pyyaml',
|
||||
'pyangbind==0.6.0',
|
||||
'sonic-py-common',
|
||||
],
|
||||
test_suite = 'setup.get_test_suite',
|
||||
data_files = [
|
||||
('/usr/share/sonic/templates', glob.glob('data/*')),
|
||||
],
|
||||
)
|
||||
],
|
||||
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',
|
||||
classifiers=[
|
||||
'Intended Audience :: Developers',
|
||||
'Natural Language :: English',
|
||||
"Programming Language :: Python :: 2",
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -51,6 +51,10 @@ from redis_bcc import RedisBytecodeCache
|
||||
from collections import OrderedDict
|
||||
from natsort import natsorted
|
||||
|
||||
#TODO: Remove STR_TYPE once SONiC moves to Python 3.x
|
||||
PY3x = sys.version_info >= (3, 0)
|
||||
STR_TYPE = str if PY3x else unicode
|
||||
|
||||
def sort_by_port_index(value):
|
||||
if not value:
|
||||
return
|
||||
@ -167,7 +171,7 @@ TODO(taoyl): Current version of config db only supports BGP admin states.
|
||||
if lookup_key != None:
|
||||
newData = {}
|
||||
for key in data.keys():
|
||||
if ((type(key) is unicode and lookup_key == key) or (type(key) is tuple and lookup_key in key)):
|
||||
if ((type(key) is STR_TYPE and lookup_key == key) or (type(key) is tuple and lookup_key in key)):
|
||||
newData[ConfigDBConnector.serialize_key(key)] = data.pop(key)
|
||||
break
|
||||
return newData
|
||||
@ -191,7 +195,7 @@ TODO(taoyl): Current version of config db only supports BGP admin states.
|
||||
|
||||
|
||||
def deep_update(dst, src):
|
||||
for key, value in src.iteritems():
|
||||
for key, value in src.items():
|
||||
if isinstance(value, dict):
|
||||
node = dst.setdefault(key, {})
|
||||
deep_update(node, value)
|
||||
|
@ -31,7 +31,7 @@ class TestCfgGen(TestCase):
|
||||
pass
|
||||
|
||||
def run_script(self, argument, check_stderr=False):
|
||||
print '\n Running sonic-cfggen ' + argument
|
||||
print('\n Running sonic-cfggen ' + argument)
|
||||
if check_stderr:
|
||||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -39,9 +39,9 @@ class TestCfgGen(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def test_dummy_run(self):
|
||||
@ -380,7 +380,7 @@ class TestCfgGen(TestCase):
|
||||
|
||||
def test_minigraph_sub_port_interfaces(self, check_stderr=True):
|
||||
try:
|
||||
print '\n Change device type to %s' % (BACKEND_TOR_ROUTER)
|
||||
print('\n Change device type to %s' % (BACKEND_TOR_ROUTER))
|
||||
if check_stderr:
|
||||
output = subprocess.check_output("sed -i \'s/%s/%s/g\' %s" % (TOR_ROUTER, BACKEND_TOR_ROUTER, self.sample_graph_simple), stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -413,7 +413,7 @@ class TestCfgGen(TestCase):
|
||||
# VLAN_SUB_INTERFACE
|
||||
argument = '-m "' + self.sample_graph_simple + '" -p "' + self.port_config + '" -v VLAN_SUB_INTERFACE'
|
||||
output = self.run_script(argument)
|
||||
print output.strip()
|
||||
print(output.strip())
|
||||
self.assertEqual(output.strip(), \
|
||||
"{('PortChannel01.10', '10.0.0.56/31'): {}, "
|
||||
"'Ethernet0.10': {'admin_status': 'up'}, "
|
||||
@ -423,7 +423,7 @@ class TestCfgGen(TestCase):
|
||||
"('Ethernet0.10', 'FC00::75/126'): {}}")
|
||||
|
||||
finally:
|
||||
print '\n Change device type back to %s' % (TOR_ROUTER)
|
||||
print('\n Change device type back to %s' % (TOR_ROUTER))
|
||||
if check_stderr:
|
||||
output = subprocess.check_output("sed -i \'s/%s/%s/g\' %s" % (BACKEND_TOR_ROUTER, TOR_ROUTER, self.sample_graph_simple), stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
|
@ -17,7 +17,7 @@ class TestCfgGenPlatformJson(TestCase):
|
||||
self.hwsku_json = os.path.join(self.test_dir, 'sample_hwsku.json')
|
||||
|
||||
def run_script(self, argument, check_stderr=False):
|
||||
print '\n Running sonic-cfggen ' + argument
|
||||
print('\n Running sonic-cfggen ' + argument)
|
||||
if check_stderr:
|
||||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -25,9 +25,9 @@ class TestCfgGenPlatformJson(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def test_dummy_run(self):
|
||||
|
@ -13,7 +13,7 @@ class TestCfgGenT2ChassisFe(TestCase):
|
||||
self.t2_chassis_fe_port_config = os.path.join(self.test_dir, 't2-chassis-fe-port-config.ini')
|
||||
|
||||
def run_script(self, argument, check_stderr=False):
|
||||
print '\n Running sonic-cfggen ' + argument
|
||||
print('\n Running sonic-cfggen ' + argument)
|
||||
if check_stderr:
|
||||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -21,9 +21,9 @@ class TestCfgGenT2ChassisFe(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def test_minigraph_t2_chassis_fe_type(self):
|
||||
|
@ -28,9 +28,9 @@ class TestCfgGen(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def run_diff(self, file1, file2):
|
||||
|
@ -22,7 +22,7 @@ class TestJ2Files(TestCase):
|
||||
self.output_file = os.path.join(self.test_dir, 'output')
|
||||
|
||||
def run_script(self, argument):
|
||||
print 'CMD: sonic-cfggen ' + argument
|
||||
print('CMD: sonic-cfggen ' + argument)
|
||||
return subprocess.check_output(self.script_file + ' ' + argument, shell=True)
|
||||
|
||||
def run_diff(self, file1, file2):
|
||||
|
@ -23,7 +23,7 @@ class TestJ2FilesT2ChassisFe(TestCase):
|
||||
pass
|
||||
|
||||
def run_script(self, argument):
|
||||
print 'CMD: sonic-cfggen ' + argument
|
||||
print('CMD: sonic-cfggen ' + argument)
|
||||
return subprocess.check_output(self.script_file + ' ' + argument, shell=True)
|
||||
|
||||
def run_diff(self, file1, file2):
|
||||
|
@ -11,7 +11,7 @@ class TestCfgGenCaseInsensitive(TestCase):
|
||||
self.port_config = os.path.join(self.test_dir, 't0-sample-port-config.ini')
|
||||
|
||||
def run_script(self, argument, check_stderr=False):
|
||||
print '\n Running sonic-cfggen ' + argument
|
||||
print('\n Running sonic-cfggen ' + argument)
|
||||
if check_stderr:
|
||||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -19,9 +19,9 @@ class TestCfgGenCaseInsensitive(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def test_dummy_run(self):
|
||||
|
@ -24,7 +24,7 @@ class TestMultiNpuCfgGen(TestCase):
|
||||
self.port_config.append(os.path.join(self.test_data_dir, "sample_port_config-{}.ini".format(asic)))
|
||||
|
||||
def run_script(self, argument, check_stderr=False):
|
||||
print '\n Running sonic-cfggen ' + argument
|
||||
print('\n Running sonic-cfggen ' + argument)
|
||||
if check_stderr:
|
||||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
|
||||
else:
|
||||
@ -32,9 +32,9 @@ class TestMultiNpuCfgGen(TestCase):
|
||||
|
||||
linecount = output.strip().count('\n')
|
||||
if linecount <= 0:
|
||||
print ' Output: ' + output.strip()
|
||||
print(' Output: ' + output.strip())
|
||||
else:
|
||||
print ' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output))
|
||||
print(' Output: ({0} lines, {1} bytes)'.format(linecount + 1, len(output)))
|
||||
return output
|
||||
|
||||
def run_diff(self, file1, file2):
|
||||
@ -108,7 +108,7 @@ class TestMultiNpuCfgGen(TestCase):
|
||||
#NTP data is present only in the host config
|
||||
for asic in range(NUM_ASIC):
|
||||
output = json.loads(self.run_script_for_asic(argument, asic, self.port_config[asic]))
|
||||
print "Log:asic{} sku {}".format(asic,output)
|
||||
print("Log:asic{} sku {}".format(asic,output))
|
||||
self.assertDictEqual(output, {})
|
||||
|
||||
def test_mgmt_port(self):
|
||||
@ -137,25 +137,25 @@ class TestMultiNpuCfgGen(TestCase):
|
||||
def test_frontend_asic_portchannel_mem(self):
|
||||
argument = "-m {} -p {} -n asic0 --var-json \"PORTCHANNEL_MEMBER\"".format(self.sample_graph, self.port_config[0])
|
||||
output = json.loads(self.run_script(argument))
|
||||
self.assertListEqual(output.keys(), \
|
||||
self.assertListEqual(list(output.keys()), \
|
||||
['PortChannel4002|Ethernet-BP8', 'PortChannel0002|Ethernet0', 'PortChannel0002|Ethernet4', 'PortChannel4002|Ethernet-BP12', 'PortChannel4001|Ethernet-BP0', 'PortChannel4001|Ethernet-BP4'])
|
||||
|
||||
def test_backend_asic_portchannels_mem(self):
|
||||
argument = "-m {} -p {} -n asic3 --var-json \"PORTCHANNEL_MEMBER\"".format(self.sample_graph, self.port_config[3])
|
||||
output = json.loads(self.run_script(argument))
|
||||
self.assertListEqual(output.keys(), \
|
||||
self.assertListEqual(list(output.keys()), \
|
||||
['PortChannel4013|Ethernet-BP384', 'PortChannel4014|Ethernet-BP392', 'PortChannel4014|Ethernet-BP396', 'PortChannel4013|Ethernet-BP388'])
|
||||
|
||||
def test_frontend_asic_portchannel_intf(self):
|
||||
argument = "-m {} -p {} -n asic0 --var-json \"PORTCHANNEL_INTERFACE\"".format(self.sample_graph, self.port_config[0])
|
||||
output = json.loads(self.run_script(argument))
|
||||
self.assertListEqual(output.keys(), \
|
||||
self.assertListEqual(list(output.keys()), \
|
||||
['PortChannel4001|10.1.0.1/31', 'PortChannel0002|FC00::1/126', 'PortChannel4002|10.1.0.3/31', 'PortChannel0002', 'PortChannel0002|10.0.0.0/31', 'PortChannel4001', 'PortChannel4002'])
|
||||
|
||||
def test_backend_asic_portchannel_intf(self):
|
||||
argument = "-m {} -p {} -n asic3 --var-json \"PORTCHANNEL_INTERFACE\"".format(self.sample_graph, self.port_config[3])
|
||||
output = json.loads(self.run_script(argument))
|
||||
self.assertListEqual(output.keys(), \
|
||||
self.assertListEqual(list(output.keys()), \
|
||||
['PortChannel4013', 'PortChannel4013|10.1.0.2/31', 'PortChannel4014', 'PortChannel4014|10.1.0.6/31'])
|
||||
|
||||
def test_frontend_asic_ports(self):
|
||||
|
Loading…
Reference in New Issue
Block a user