[minigraph.py]: Parse peer switch info from minigraph (#5869)
* Create new `PEER_SWITCH` table in config DB with info from minigraph * Add `subtype` field to `DEVICE_METADATA` table and set value to `DualToR` if device is in a dual ToR setup
This commit is contained in:
parent
7c17c58b83
commit
bf50562670
@ -65,6 +65,18 @@ class minigraph_encoder(json.JSONEncoder):
|
||||
return str(obj)
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
def get_peer_switch_info(link_metadata, devices):
|
||||
peer_switch_table = {}
|
||||
for data in link_metadata.values():
|
||||
if "PeerSwitch" in data:
|
||||
peer_hostname = data["PeerSwitch"]
|
||||
peer_lo_addr = devices[peer_hostname]["lo_addr"]
|
||||
peer_switch_table[peer_hostname] = {
|
||||
'address_ipv4': peer_lo_addr
|
||||
}
|
||||
|
||||
return peer_switch_table
|
||||
|
||||
def parse_device(device):
|
||||
lo_prefix = None
|
||||
mgmt_prefix = None
|
||||
@ -674,16 +686,31 @@ def parse_linkmeta(meta, hname):
|
||||
# Cannot find a matching hname, something went wrong
|
||||
continue
|
||||
|
||||
has_peer_switch = False
|
||||
upper_tor_hostname = ''
|
||||
lower_tor_hostname = ''
|
||||
|
||||
properties = linkmeta.find(str(QName(ns1, "Properties")))
|
||||
for device_property in properties.findall(str(QName(ns1, "DeviceProperty"))):
|
||||
name = device_property.find(str(QName(ns1, "Name"))).text
|
||||
value = device_property.find(str(QName(ns1, "Value"))).text
|
||||
if name == "FECDisabled":
|
||||
fec_disabled = value
|
||||
elif name == "GeminiPeeringLink":
|
||||
has_peer_switch = True
|
||||
elif name == "UpperTOR":
|
||||
upper_tor_hostname = value
|
||||
elif name == "LowerTOR":
|
||||
lower_tor_hostname = value
|
||||
|
||||
linkmetas[port] = {}
|
||||
if fec_disabled:
|
||||
linkmetas[port]["FECDisabled"] = fec_disabled
|
||||
if has_peer_switch:
|
||||
if upper_tor_hostname == hname:
|
||||
linkmetas[port]["PeerSwitch"] = lower_tor_hostname
|
||||
else:
|
||||
linkmetas[port]["PeerSwitch"] = upper_tor_hostname
|
||||
return linkmetas
|
||||
|
||||
|
||||
@ -1001,8 +1028,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
|
||||
}
|
||||
}
|
||||
|
||||
results['PEER_SWITCH'] = get_peer_switch_info(linkmetas, devices)
|
||||
|
||||
if bool(results['PEER_SWITCH']):
|
||||
results['DEVICE_METADATA']['localhost']['subtype'] = 'DualToR'
|
||||
|
||||
if is_storage_device:
|
||||
results['DEVICE_METADATA']['localhost']['storage_device'] = "true"
|
||||
|
||||
# for this hostname, if sub_role is defined, add sub_role in
|
||||
# device_metadata
|
||||
if sub_role is not None:
|
||||
|
@ -286,7 +286,7 @@
|
||||
<d3p1:Name i:nil="true" />
|
||||
<d3p1:Properties>
|
||||
<d3p1:DeviceProperty>
|
||||
<d3p1:Name>DevicePeeringLink</d3p1:Name>
|
||||
<d3p1:Name>GeminiPeeringLink</d3p1:Name>
|
||||
<d3p1:Reference i:nil="true" />
|
||||
<d3p1:Value>True</d3p1:Value>
|
||||
</d3p1:DeviceProperty>
|
||||
|
@ -52,6 +52,11 @@ class TestCfgGenCaseInsensitive(TestCase):
|
||||
output = self.run_script(argument)
|
||||
self.assertEqual(output.strip(), 'ToRRouter')
|
||||
|
||||
def test_minigraph_subtype(self):
|
||||
argument = '-m "' + self.sample_graph + '" -v "DEVICE_METADATA[\'localhost\'][\'subtype\']"'
|
||||
output = self.run_script(argument)
|
||||
self.assertEqual(output.strip(), 'DualToR')
|
||||
|
||||
def test_additional_json_data(self):
|
||||
argument = '-a \'{"key1":"value1"}\' -v key1'
|
||||
output = self.run_script(argument)
|
||||
@ -174,6 +179,20 @@ class TestCfgGenCaseInsensitive(TestCase):
|
||||
output = self.run_script(argument)
|
||||
self.assertEqual(output.strip(), "{}")
|
||||
|
||||
def test_minigraph_peer_switch(self):
|
||||
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "PEER_SWITCH"'
|
||||
expected_table = {
|
||||
'switch2-t0': {
|
||||
'address_ipv4': "25.1.1.10"
|
||||
}
|
||||
}
|
||||
|
||||
output = self.run_script(argument)
|
||||
self.assertEqual(
|
||||
utils.to_dict(output.strip()),
|
||||
expected_table
|
||||
)
|
||||
|
||||
def test_mux_cable_parsing(self):
|
||||
result = minigraph.parse_xml(self.sample_graph, port_config_file=self.port_config)
|
||||
|
||||
|
Reference in New Issue
Block a user