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] Support parsing console ports #1898

Merged
merged 4 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,32 @@ def parse_png(png, hname):
mgmt_dev = ''
mgmt_port = ''
port_speeds = {}
console_ports = {}
for child in png:
if child.tag == str(QName(ns, "DeviceInterfaceLinks")):
for link in child.findall(str(QName(ns, "DeviceLinkBase"))):
linktype = link.find(str(QName(ns, "ElementType"))).text
if linktype == "DeviceSerialLink":
enddevice = link.find(str(QName(ns, "EndDevice"))).text
endport = link.find(str(QName(ns, "EndPort"))).text
startdevice = link.find(str(QName(ns, "StartDevice"))).text
startport = link.find(str(QName(ns, "StartPort"))).text
baudrate = link.find(str(QName(ns, "Bandwidth"))).text
flowcontrol = 1 if link.find(str(QName(ns, "FlowControl"))) is not None and link.find(str(QName(ns, "FlowControl"))).text == 'true' else 0
if enddevice.lower() == hname.lower():
console_ports[endport] = {
'remote_device': startdevice,
'baud_rate': baudrate,
'flow_control': flowcontrol
}
else:
console_ports[startport] = {
'remote_device': enddevice,
'baud_rate': baudrate,
'flow_control': flowcontrol
}
continue

if linktype != "DeviceInterfaceLink" and linktype != "UnderlayInterfaceLink":
continue

Expand Down Expand Up @@ -118,7 +140,7 @@ def parse_png(png, hname):
elif node.tag == str(QName(ns, "EndDevice")):
mgmt_dev = node.text

return (neighbors, devices, console_dev, console_port, mgmt_dev, mgmt_port, port_speeds)
return (neighbors, devices, console_dev, console_port, mgmt_dev, mgmt_port, port_speeds, console_ports)


def parse_dpg(dpg, hname):
Expand Down Expand Up @@ -399,6 +421,7 @@ def parse_xml(filename, platform=None, port_config_file=None):
port_speeds_default = {}
port_speed_png = {}
port_descriptions = {}
console_ports = {}
syslog_servers = []
dhcp_servers = []
ntp_servers = []
Expand All @@ -424,9 +447,9 @@ def parse_xml(filename, platform=None, port_config_file=None):
elif child.tag == str(QName(ns, "CpgDec")):
(bgp_sessions, bgp_asn, bgp_peers_with_range) = parse_cpg(child, hostname)
elif child.tag == str(QName(ns, "PngDec")):
(neighbors, devices, console_dev, console_port, mgmt_dev, mgmt_port, port_speed_png) = parse_png(child, hostname)
(neighbors, devices, console_dev, console_port, mgmt_dev, mgmt_port, port_speed_png, console_ports) = parse_png(child, hostname)
elif child.tag == str(QName(ns, "UngDec")):
(u_neighbors, u_devices, _, _, _, _, _) = parse_png(child, hostname)
(u_neighbors, u_devices, _, _, _, _, _, _) = parse_png(child, hostname)
elif child.tag == str(QName(ns, "MetadataDeclaration")):
(syslog_servers, dhcp_servers, ntp_servers, tacacs_servers, mgmt_routes, erspan_dst, deployment_id) = parse_meta(child, hostname)
elif child.tag == str(QName(ns, "DeviceInfos")):
Expand Down Expand Up @@ -494,6 +517,7 @@ def parse_xml(filename, platform=None, port_config_file=None):
ports.setdefault(port_name, {})['description'] = port_descriptions[port_name]

results['PORT'] = ports
results['CONSOLE_PORT'] = console_ports

if port_config_file:
port_set = set(ports.keys())
Expand Down
9 changes: 9 additions & 0 deletions src/sonic-config-engine/tests/simple-sample-graph-case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@
</DpgDec>
<PngDec>
<DeviceInterfaceLinks>
<DeviceLinkBase i:type="DeviceSerialLink">
<ElementType>DeviceSerialLink</ElementType>
<Bandwidth>9600</Bandwidth>
<EndDevice>switch-t0</EndDevice>
<EndPort>1</EndPort>
<FlowControl>true</FlowControl>
<StartDevice>managed_device</StartDevice>
<StartPort>console</StartPort>
</DeviceLinkBase>
<DeviceLinkBase i:type="DeviceInterfaceLink">
<ElementType>DeviceInterfaceLink</ElementType>
<AutoNegotiation>true</AutoNegotiation>
Expand Down
5 changes: 5 additions & 0 deletions src/sonic-config-engine/tests/test_minigraph_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ def test_minigraph_portchannels(self):
output = self.run_script(argument)
self.assertEqual(output.strip(), "{'PortChannel01': {'members': ['Ethernet4']}}")

def test_minigraph_console_port(self):
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v CONSOLE_PORT'
output = self.run_script(argument)
self.assertEqual(output.strip(), "{'1': {'baud_rate': '9600', 'remote_device': 'managed_device', 'flow_control': 1}}")

def test_minigraph_deployment_id(self):
argument = '-m "' + self.sample_graph + '" -p "' + self.port_config + '" -v "DEVICE_METADATA[\'localhost\'][\'deployment_id\']"'
output = self.run_script(argument)
Expand Down