Migrate DEVICE_METADATA to db (#919)

This commit is contained in:
Taoyu Li 2017-08-29 10:47:25 -07:00 committed by GitHub
parent 3e7c3e6ea3
commit 44502b217b
8 changed files with 37 additions and 25 deletions

View File

@ -7,7 +7,7 @@
{% endblock banner %} {% endblock banner %}
! !
{% block system_init %} {% block system_init %}
hostname {{ inventory_hostname }} hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
password zebra password zebra
log syslog informational log syslog informational
log facility local4 log facility local4
@ -51,7 +51,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
{% if bgp_session['asn'] != 0 %} {% if bgp_session['asn'] != 0 %}
neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }} neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }}
neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }} neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }}
{% if minigraph_devices[inventory_hostname]['type'] == 'ToRRouter' %} {% if DEVICE_METADATA['localhost']['type'] == 'ToRRouter' %}
neighbor {{ neighbor_addr }} allowas-in 1 neighbor {{ neighbor_addr }} allowas-in 1
{% endif %} {% endif %}
{% if neighbor_addr | ipv4 %} {% if neighbor_addr | ipv4 %}

View File

@ -7,7 +7,7 @@
{% endblock banner %} {% endblock banner %}
! !
{% block sys_init %} {% block sys_init %}
hostname {{ inventory_hostname }} hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
password zebra password zebra
enable password zebra enable password zebra
{% endblock sys_init %} {% endblock sys_init %}

View File

@ -7,7 +7,7 @@
{% endblock banner %} {% endblock banner %}
! !
{% block sys_init %} {% block sys_init %}
hostname {{ inventory_hostname }} hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
password zebra password zebra
enable password zebra enable password zebra
{% endblock sys_init %} {% endblock sys_init %}

View File

@ -7,7 +7,7 @@
{% endblock banner %} {% endblock banner %}
! !
{% block system_init %} {% block system_init %}
hostname {{ inventory_hostname }} hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
password zebra password zebra
log syslog informational log syslog informational
log facility local4 log facility local4
@ -27,7 +27,7 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
bgp log-neighbor-changes bgp log-neighbor-changes
bgp bestpath as-path multipath-relax bgp bestpath as-path multipath-relax
{# Advertise graceful restart capability for ToR #} {# Advertise graceful restart capability for ToR #}
{% if minigraph_devices[inventory_hostname]['type'] == 'ToRRouter' %} {% if DEVICE_METADATA['localhost']['type'] == 'ToRRouter' %}
bgp graceful-restart bgp graceful-restart
{% endif %} {% endif %}
{# TODO: use lo[0] for backward compatibility, will revisit the case with multiple lo interfaces #} {# TODO: use lo[0] for backward compatibility, will revisit the case with multiple lo interfaces #}
@ -64,13 +64,13 @@ router bgp {{ DEVICE_METADATA['localhost']['bgp_asn'] }}
neighbor {{ neighbor_addr }} shutdown neighbor {{ neighbor_addr }} shutdown
{% endif %} {% endif %}
{% if neighbor_addr | ipv4 %} {% if neighbor_addr | ipv4 %}
{% if minigraph_devices[inventory_hostname]['type'] == 'ToRRouter' %} {% if DEVICE_METADATA['localhost']['type'] == 'ToRRouter' %}
neighbor {{ neighbor_addr }} allowas-in 1 neighbor {{ neighbor_addr }} allowas-in 1
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if neighbor_addr | ipv6 %} {% if neighbor_addr | ipv6 %}
address-family ipv6 address-family ipv6
{% if minigraph_devices[inventory_hostname]['type'] == 'ToRRouter' %} {% if DEVICE_METADATA['locahost']['type'] == 'ToRRouter' %}
neighbor {{ neighbor_addr }} allowas-in 1 neighbor {{ neighbor_addr }} allowas-in 1
{% endif %} {% endif %}
neighbor {{ neighbor_addr }} activate neighbor {{ neighbor_addr }} activate

View File

@ -7,7 +7,7 @@
{% endblock banner %} {% endblock banner %}
! !
{% block sys_init %} {% block sys_init %}
hostname {{ inventory_hostname }} hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
password zebra password zebra
enable password zebra enable password zebra
{% endblock sys_init %} {% endblock sys_init %}

View File

@ -458,14 +458,15 @@ def parse_xml(filename, platform=None, port_config_file=None):
ethernet_interfaces = parse_deviceinfo(child, hwsku) ethernet_interfaces = parse_deviceinfo(child, hwsku)
results = {} results = {}
results['minigraph_hwsku'] = hwsku
# sorting by lambdas are not easily done without custom filters.
# TODO: add jinja2 filter to accept a lambda to sort a list of dictionaries by attribute.
# TODO: alternatively (preferred), implement class containers for multiple-attribute entries, enabling sort by attr
results['BGP_NEIGHBOR'] = bgp_sessions results['BGP_NEIGHBOR'] = bgp_sessions
results['DEVICE_METADATA'] = {'localhost': { 'bgp_asn': bgp_asn }} results['DEVICE_METADATA'] = {'localhost': {
'bgp_asn': bgp_asn,
'deployment_id': deployment_id,
'hostname': hostname,
'hwsku': hwsku,
'type': devices[hostname]['type']
}}
results['BGP_PEER_RANGE'] = bgp_peers_with_range results['BGP_PEER_RANGE'] = bgp_peers_with_range
# TODO: sort does not work properly on all interfaces of varying lengths. Need to sort by integer group(s).
phyport_intfs = [] phyport_intfs = []
vlan_intfs = [] vlan_intfs = []
@ -496,14 +497,11 @@ def parse_xml(filename, platform=None, port_config_file=None):
if devices != None: if devices != None:
results['minigraph_console'] = get_console_info(devices, console_dev, console_port) results['minigraph_console'] = get_console_info(devices, console_dev, console_port)
results['minigraph_mgmt'] = get_mgmt_info(devices, mgmt_dev, mgmt_port) results['minigraph_mgmt'] = get_mgmt_info(devices, mgmt_dev, mgmt_port)
results['minigraph_hostname'] = hostname
results['inventory_hostname'] = hostname
results['syslog_servers'] = syslog_servers results['syslog_servers'] = syslog_servers
results['dhcp_servers'] = dhcp_servers results['dhcp_servers'] = dhcp_servers
results['ntp_servers'] = ntp_servers results['ntp_servers'] = ntp_servers
results['forced_mgmt_routes'] = mgmt_routes results['forced_mgmt_routes'] = mgmt_routes
results['erspan_dst'] = erspan_dst results['erspan_dst'] = erspan_dst
results['deployment_id'] = deployment_id
results['ethernet_interfaces'] = ethernet_interfaces results['ethernet_interfaces'] = ethernet_interfaces
return results return results
@ -513,9 +511,10 @@ def parse_device_desc_xml(filename):
(lo_prefix, mgmt_prefix, hostname, hwsku, d_type) = parse_device(root) (lo_prefix, mgmt_prefix, hostname, hwsku, d_type) = parse_device(root)
results = {} results = {}
results['minigraph_hwsku'] = hwsku results['DEVICE_METADATA'] = {'localhost': {
results['minigraph_hostname'] = hostname 'hostname': hostname,
results['inventory_hostname'] = hostname 'hwsku': hwsku,
}}
lo_intfs = [] lo_intfs = []
ipn = ipaddress.IPNetwork(lo_prefix) ipn = ipaddress.IPNetwork(lo_prefix)

View File

@ -22,6 +22,7 @@ import yaml
import jinja2 import jinja2
import netaddr import netaddr
import json import json
from functools import partial
from minigraph import minigraph_encoder from minigraph import minigraph_encoder
from minigraph import parse_xml from minigraph import parse_xml
from minigraph import parse_device_desc_xml from minigraph import parse_device_desc_xml
@ -53,6 +54,16 @@ def is_ipv6(value):
return False return False
return addr.version == 6 return addr.version == 6
def prefix_attr(attr, value):
if not value:
return None
else:
try:
prefix = netaddr.IPNetwork(str(value))
except:
return None
return str(getattr(prefix, attr))
def unique_name(l): def unique_name(l):
name_list = [] name_list = []
new_list = [] new_list = []
@ -158,6 +169,8 @@ def main():
env.filters['ipv4'] = is_ipv4 env.filters['ipv4'] = is_ipv4
env.filters['ipv6'] = is_ipv6 env.filters['ipv6'] = is_ipv6
env.filters['unique_name'] = unique_name env.filters['unique_name'] = unique_name
for attr in ['ip', 'network', 'prefixlen', 'netmask']:
env.filters[attr] = partial(prefix_attr, attr)
template = env.get_template(template_file) template = env.get_template(template_file)
print template.render(data) print template.render(data)

View File

@ -31,7 +31,7 @@ class TestCfgGen(TestCase):
self.assertEqual(output, '') self.assertEqual(output, '')
def test_device_desc(self): def test_device_desc(self):
argument = '-v minigraph_hwsku -M "' + self.sample_device_desc + '"' argument = '-v "DEVICE_METADATA[\'localhost\'][\'hwsku\']" -M "' + self.sample_device_desc + '"'
output = self.run_script(argument) output = self.run_script(argument)
self.assertEqual(output.strip(), 'ACS-MSN2700') self.assertEqual(output.strip(), 'ACS-MSN2700')
@ -41,7 +41,7 @@ class TestCfgGen(TestCase):
self.assertEqual(output.strip(), '10.0.1.5') self.assertEqual(output.strip(), '10.0.1.5')
def test_minigraph_sku(self): def test_minigraph_sku(self):
argument = '-v minigraph_hwsku -m "' + self.sample_graph + '"' argument = '-v "DEVICE_METADATA[\'localhost\'][\'hwsku\']" -m "' + self.sample_graph + '"'
output = self.run_script(argument) output = self.run_script(argument)
self.assertEqual(output.strip(), 'Force10-Z9100') self.assertEqual(output.strip(), 'Force10-Z9100')
@ -51,7 +51,7 @@ class TestCfgGen(TestCase):
self.assertTrue(len(output.strip()) > 0) self.assertTrue(len(output.strip()) > 0)
def test_jinja_expression(self): def test_jinja_expression(self):
argument = '-m "' + self.sample_graph + '" -v "minigraph_devices[minigraph_hostname][\'type\']"' argument = '-m "' + self.sample_graph + '" -v "DEVICE_METADATA[\'localhost\'][\'type\']"'
output = self.run_script(argument) output = self.run_script(argument)
self.assertEqual(output.strip(), 'LeafRouter') self.assertEqual(output.strip(), 'LeafRouter')
@ -116,7 +116,7 @@ class TestCfgGen(TestCase):
self.assertEqual(output.strip(), "[{'name': 'BGPSLBPassive', 'ip_range': ['10.10.10.10/26', '100.100.100.100/26']}]") self.assertEqual(output.strip(), "[{'name': 'BGPSLBPassive', 'ip_range': ['10.10.10.10/26', '100.100.100.100/26']}]")
def test_minigraph_deployment_id(self): def test_minigraph_deployment_id(self):
argument = '-m "' + self.sample_graph_bgp_speaker + '" -p "' + self.port_config + '" -v deployment_id' argument = '-m "' + self.sample_graph_bgp_speaker + '" -p "' + self.port_config + '" -v "DEVICE_METADATA[\'localhost\'][\'deployment_id\']"'
output = self.run_script(argument) output = self.run_script(argument)
self.assertEqual(output.strip(), "1") self.assertEqual(output.strip(), "1")