Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minigraph.py]: Parse peer switch info from minigraph #5869

Merged
merged 7 commits into from
Nov 13, 2020
33 changes: 33 additions & 0 deletions src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def default(self, obj):
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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-config-engine/tests/simple-sample-graph-case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
19 changes: 19 additions & 0 deletions src/sonic-config-engine/tests/test_minigraph_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_jinja_expression(self):
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)
Expand Down Expand Up @@ -174,6 +179,20 @@ def test_minigraph_bgp_mon(self):
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)

Expand Down