[sonic-cfggen]: Add -p option and add teamd.j2 test (#414)

- Add -p --port-config option to feed sonic-cfggen with port_config.ini
  file when necessary.
- Update minigraph.py file to accept the -p option
- Add test_j2files.py test to test config.sh and all .j2 templates
  * Currently test_teamd is added to test both the config.sh and teamd.j2
    file works well with the t0 sample minigraph and sample port config
    file
  * The sample output is added to the folder sample_output for comparison

Signed-off-by: Shuotian Cheng <shuche@microsoft.com>
This commit is contained in:
Shuotian Cheng 2017-03-17 21:38:20 -07:00 committed by GitHub
parent 4212efde6c
commit 7b7a61693a
12 changed files with 470 additions and 9 deletions

View File

@ -11,10 +11,10 @@
"name": "ethtool"
},
"ports": {
{% for member in minigraph_portchannels[pc]['members'] %}
"{{member}}": {}{% if not loop.last %},{% endif %}
{% for member in minigraph_portchannels[pc]['members'] %}
"{{member}}": {}{% if not loop.last %},{% endif %}
{% endfor %}
{% endfor %}
}
}

View File

@ -328,8 +328,10 @@ def get_mgmt_info(devices, dev, port):
return ret_val
def get_alias_map_list(hwsku, platform=None):
def get_alias_map_list(hwsku, platform=None, port_config_file=None):
port_config_candidates = []
if port_config_file != None:
port_config_candidates.append(port_config_file)
port_config_candidates.append('/usr/share/sonic/hwsku/port_config.ini')
if platform != None:
port_config_candidates.append(os.path.join('/usr/share/sonic/device', platform, hwsku, 'port_config.ini'))
@ -354,7 +356,7 @@ def get_alias_map_list(hwsku, platform=None):
alias_map_list.append({'sonic': tokens[0], 'origin': tokens[2].strip()})
return alias_map_list
def parse_xml(filename, platform=None):
def parse_xml(filename, platform=None, port_config_file=None):
root = ET.parse(filename).getroot()
mini_graph_path = filename
@ -385,7 +387,7 @@ def parse_xml(filename, platform=None):
if child.tag == str(hostname_qn):
hostname = child.text
alias_map_list = get_alias_map_list(hwsku, platform)
alias_map_list = get_alias_map_list(hwsku, platform, port_config_file)
if alias_map_list != None:
for item in alias_map_list:
port_alias_map[item['origin']] = item['sonic']
@ -438,7 +440,6 @@ def parse_xml(filename, platform=None):
port_alias_map = {}
def print_parse_xml(filename):
results = parse_xml(filename)
print(json.dumps(results, indent=3, cls=minigraph_encoder))

0
src/sonic-config-engine/setup.py Normal file → Executable file
View File

View File

@ -60,6 +60,7 @@ def get_machine_info():
def main():
parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.")
parser.add_argument("-m", "--minigraph", help="minigraph xml file")
parser.add_argument("-p", "--port-config", help="port config file, used with -m")
parser.add_argument("-y", "--yaml", help="yaml file that contains addtional variables")
parser.add_argument("-a", "--additional-data", help="addition data, in json string")
group = parser.add_mutually_exclusive_group()
@ -83,9 +84,15 @@ def main():
if args.minigraph != None:
minigraph = args.minigraph
if data.has_key('platform'):
data.update(parse_xml(minigraph, data['platform']))
if args.port_config != None:
data.update(parse_xml(minigraph, data['platform'], args.port_config))
else:
data.update(parse_xml(minigraph, data['platform']))
else:
data.update(parse_xml(minigraph))
if args.port_config != None:
data.update(parse_xml(minigraph, port_config_file=args.port_config))
else:
data.update(parse_xml(minigraph))
if args.yaml != None:
with open(args.yaml, 'r') as stream:

View File

@ -0,0 +1 @@
__pycache__

View File

@ -0,0 +1,16 @@
{
"device": "PortChannel01",
"runner": {
"name": "lacp",
"active": true,
"min_ports": 1,
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
"Ethernet112": {}
}
}

View File

@ -0,0 +1,16 @@
{
"device": "PortChannel02",
"runner": {
"name": "lacp",
"active": true,
"min_ports": 1,
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
"Ethernet116": {}
}
}

View File

@ -0,0 +1,16 @@
{
"device": "PortChannel03",
"runner": {
"name": "lacp",
"active": true,
"min_ports": 1,
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
"Ethernet120": {}
}
}

View File

@ -0,0 +1,16 @@
{
"device": "PortChannel04",
"runner": {
"name": "lacp",
"active": true,
"min_ports": 1,
"tx_hash": ["eth", "ipv4", "ipv6"]
},
"link_watch": {
"name": "ethtool"
},
"ports": {
"Ethernet124": {}
}
}

View File

@ -0,0 +1,315 @@
<DeviceMiniGraph xmlns="Microsoft.Search.Autopilot.Evolution" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CpgDec>
<IsisRouters xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
<PeeringSessions>
<BGPSession>
<MacSec>false</MacSec>
<StartRouter>switch-t0</StartRouter>
<StartPeer>10.0.0.56</StartPeer>
<EndRouter>ARISTA01T1</EndRouter>
<EndPeer>10.0.0.57</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<StartRouter>switch-t0</StartRouter>
<StartPeer>FC00::71</StartPeer>
<EndRouter>ARISTA01T1</EndRouter>
<EndPeer>FC00::72</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<MacSec>false</MacSec>
<StartRouter>switch-t0</StartRouter>
<StartPeer>10.0.0.58</StartPeer>
<EndRouter>ARISTA02T1</EndRouter>
<EndPeer>10.0.0.59</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<StartRouter>switch-t0</StartRouter>
<StartPeer>FC00::75</StartPeer>
<EndRouter>ARISTA02T1</EndRouter>
<EndPeer>FC00::76</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<MacSec>false</MacSec>
<StartRouter>switch-t0</StartRouter>
<StartPeer>10.0.0.60</StartPeer>
<EndRouter>ARISTA03T1</EndRouter>
<EndPeer>10.0.0.61</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<StartRouter>switch-t0</StartRouter>
<StartPeer>FC00::79</StartPeer>
<EndRouter>ARISTA03T1</EndRouter>
<EndPeer>FC00::7A</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<MacSec>false</MacSec>
<StartRouter>switch-t0</StartRouter>
<StartPeer>10.0.0.62</StartPeer>
<EndRouter>ARISTA04T1</EndRouter>
<EndPeer>10.0.0.63</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
<BGPSession>
<StartRouter>switch-t0</StartRouter>
<StartPeer>FC00::7D</StartPeer>
<EndRouter>ARISTA04T1</EndRouter>
<EndPeer>FC00::7E</EndPeer>
<Multihop>1</Multihop>
<HoldTime>180</HoldTime>
<KeepAliveTime>60</KeepAliveTime>
</BGPSession>
</PeeringSessions>
<Routers xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:BGPRouterDeclaration>
<a:ASN>65100</a:ASN>
<a:Hostname>switch-t0</a:Hostname>
<a:Peers>
<BGPPeer>
<Address>10.0.0.57</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
<Vrf i:nil="true"/>
</BGPPeer>
<BGPPeer>
<Address>10.0.0.59</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
<Vrf i:nil="true"/>
</BGPPeer>
<BGPPeer>
<Address>10.0.0.61</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
<Vrf i:nil="true"/>
</BGPPeer>
<BGPPeer>
<Address>10.0.0.63</Address>
<RouteMapIn i:nil="true"/>
<RouteMapOut i:nil="true"/>
<Vrf i:nil="true"/>
</BGPPeer>
</a:Peers>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64600</a:ASN>
<a:Hostname>ARISTA01T1</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64600</a:ASN>
<a:Hostname>ARISTA02T1</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64600</a:ASN>
<a:Hostname>ARISTA03T1</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
<a:BGPRouterDeclaration>
<a:ASN>64600</a:ASN>
<a:Hostname>ARISTA04T1</a:Hostname>
<a:RouteMaps/>
</a:BGPRouterDeclaration>
</Routers>
</CpgDec>
<DpgDec>
<DeviceDataPlaneInfo>
<IPSecTunnels/>
<LoopbackIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:LoopbackIPInterface>
<Name>HostIP</Name>
<AttachTo>Loopback0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.Evolution">
<b:IPPrefix>10.1.0.32/32</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>10.1.0.32/32</a:PrefixStr>
</a:LoopbackIPInterface>
<a:LoopbackIPInterface>
<Name>HostIP1</Name>
<AttachTo>Loopback0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.Evolution">
<b:IPPrefix>FC00:1::32/128</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>FC00:1::32/128</a:PrefixStr>
</a:LoopbackIPInterface>
</LoopbackIPInterfaces>
<ManagementIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution">
<a:ManagementIPInterface>
<Name>HostIP</Name>
<AttachTo>eth0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.Evolution">
<b:IPPrefix>10.0.0.100/24</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>10.0.0.100/24</a:PrefixStr>
</a:ManagementIPInterface>
</ManagementIPInterfaces>
<ManagementVIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
<MplsInterfaces/>
<MplsTeInterfaces/>
<RsvpInterfaces/>
<Hostname>switch-t0</Hostname>
<PortChannelInterfaces>
<PortChannel>
<Name>PortChannel01</Name>
<AttachTo>fortyGigE0/112</AttachTo>
<SubInterface/>
</PortChannel>
<PortChannel>
<Name>PortChannel02</Name>
<AttachTo>fortyGigE0/116</AttachTo>
<SubInterface/>
</PortChannel>
<PortChannel>
<Name>PortChannel03</Name>
<AttachTo>fortyGigE0/120</AttachTo>
<SubInterface/>
</PortChannel>
<PortChannel>
<Name>PortChannel04</Name>
<AttachTo>fortyGigE0/124</AttachTo>
<SubInterface/>
</PortChannel>
</PortChannelInterfaces>
<VlanInterfaces>
<VlanInterface>
<Name>Vlan1000</Name>
<AttachTo>fortyGigE0/4;fortyGigE0/8;fortyGigE0/12;fortyGigE0/16;fortyGigE0/20;fortyGigE0/24;fortyGigE0/28;fortyGigE0/32;fortyGigE0/36;fortyGigE0/40;fortyGigE0/44;fortyGigE0/48;fortyGigE0/52;fortyGigE0/56;fortyGigE0/60;fortyGigE0/64;fortyGigE0/68;fortyGigE0/72;fortyGigE0/76;fortyGigE0/80;fortyGigE0/84;fortyGigE0/88;fortyGigE0/92;fortyGigE0/96</AttachTo>
<NoDhcpRelay>False</NoDhcpRelay>
<StaticDHCPRelay>0.0.0.0/0</StaticDHCPRelay>
<Type i:nil="true"/>
<VlanID>1000</VlanID>
<Tag>1000</Tag>
<Subnets>192.168.0.0/27</Subnets>
</VlanInterface>
</VlanInterfaces>
<IPInterfaces>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>PortChannel01</AttachTo>
<Prefix>10.0.0.56/31</Prefix>
</IPInterface>
<IPInterface>
<Name i:Name="true"/>
<AttachTo>PortChannel01</AttachTo>
<Prefix>FC00::71/126</Prefix>
</IPInterface>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>PortChannel02</AttachTo>
<Prefix>10.0.0.58/31</Prefix>
</IPInterface>
<IPInterface>
<Name i:Name="true"/>
<AttachTo>PortChannel02</AttachTo>
<Prefix>FC00::75/126</Prefix>
</IPInterface>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>PortChannel03</AttachTo>
<Prefix>10.0.0.60/31</Prefix>
</IPInterface>
<IPInterface>
<Name i:Name="true"/>
<AttachTo>PortChannel03</AttachTo>
<Prefix>FC00::79/126</Prefix>
</IPInterface>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>PortChannel04</AttachTo>
<Prefix>10.0.0.62/31</Prefix>
</IPInterface>
<IPInterface>
<Name i:Name="true"/>
<AttachTo>PortChannel04</AttachTo>
<Prefix>FC00::7D/126</Prefix>
</IPInterface>
<IPInterface>
<Name i:nil="true"/>
<AttachTo>Vlan1000</AttachTo>
<Prefix>192.168.0.1/27</Prefix>
</IPInterface>
</IPInterfaces>
<DataAcls/>
<AclInterfaces/>
<DownstreamSummaries/>
<DownstreamSummarySet xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
</DeviceDataPlaneInfo>
</DpgDec>
<PngDec>
<DeviceInterfaceLinks>
<DeviceLinkBase>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>ARISTA01T1</EndDevice>
<EndPort>Ethernet1/1</EndPort>
<StartDevice>switch-t0</StartDevice>
<StartPort>fortyGigE0/112</StartPort>
</DeviceLinkBase>
<DeviceLinkBase>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>ARISTA02T1</EndDevice>
<EndPort>Ethernet1/1</EndPort>
<StartDevice>switch-t0</StartDevice>
<StartPort>fortyGigE0/116</StartPort>
</DeviceLinkBase>
<DeviceLinkBase>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>ARISTA03T1</EndDevice>
<EndPort>Ethernet1/1</EndPort>
<StartDevice>switch-t0</StartDevice>
<StartPort>fortyGigE0/120</StartPort>
</DeviceLinkBase>
<DeviceLinkBase>
<ElementType>DeviceInterfaceLink</ElementType>
<EndDevice>ARISTA04T1</EndDevice>
<EndPort>Ethernet1/1</EndPort>
<StartDevice>switch-t0</StartDevice>
<StartPort>fortyGigE0/124</StartPort>
</DeviceLinkBase>
</DeviceInterfaceLinks>
<Devices>
<Device i:type="ToRRouter">
<Hostname>switch-t0</Hostname>
<HwSku>Force10-S6000</HwSku>
</Device>
<Device i:type="LeafRouter">
<Hostname>ARISTA01T1</Hostname>
<HwSku>Arista</HwSku>
</Device>
<Device i:type="LeafRouter">
<Hostname>ARISTA02T1</Hostname>
<HwSku>Arista</HwSku>
</Device>
<Device i:type="LeafRouter">
<Hostname>ARISTA03T1</Hostname>
<HwSku>Arista</HwSku>
</Device>
<Device i:type="LeafRouter">
<Hostname>ARISTA04T1</Hostname>
<HwSku>Arista</HwSku>
</Device>
</Devices>
</PngDec>
<Hostname>switch-t0</Hostname>
<HwSku>Force10-S6000</HwSku>
</DeviceMiniGraph>

View File

@ -0,0 +1,33 @@
# name lanes alias
Ethernet0 29,30,31,32 fortyGigE0/0
Ethernet4 25,26,27,28 fortyGigE0/4
Ethernet8 37,38,39,40 fortyGigE0/8
Ethernet12 33,34,35,36 fortyGigE0/12
Ethernet16 41,42,43,44 fortyGigE0/16
Ethernet20 45,46,47,48 fortyGigE0/20
Ethernet24 5,6,7,8 fortyGigE0/24
Ethernet28 1,2,3,4 fortyGigE0/28
Ethernet32 9,10,11,12 fortyGigE0/32
Ethernet36 13,14,15,16 fortyGigE0/36
Ethernet40 21,22,23,24 fortyGigE0/40
Ethernet44 17,18,19,20 fortyGigE0/44
Ethernet48 49,50,51,52 fortyGigE0/48
Ethernet52 53,54,55,56 fortyGigE0/52
Ethernet56 61,62,63,64 fortyGigE0/56
Ethernet60 57,58,59,60 fortyGigE0/60
Ethernet64 65,66,67,68 fortyGigE0/64
Ethernet68 69,70,71,72 fortyGigE0/68
Ethernet72 77,78,79,80 fortyGigE0/72
Ethernet76 73,74,75,76 fortyGigE0/76
Ethernet80 105,106,107,108 fortyGigE0/80
Ethernet84 109,110,111,112 fortyGigE0/84
Ethernet88 117,118,119,120 fortyGigE0/88
Ethernet92 113,114,115,116 fortyGigE0/92
Ethernet96 121,122,123,124 fortyGigE0/96
Ethernet100 125,126,127,128 fortyGigE0/100
Ethernet104 85,86,87,88 fortyGigE0/104
Ethernet108 81,82,83,84 fortyGigE0/108
Ethernet112 89,90,91,92 fortyGigE0/112
Ethernet116 93,94,95,96 fortyGigE0/116
Ethernet120 97,98,99,100 fortyGigE0/120
Ethernet124 101,102,103,104 fortyGigE0/124

View File

@ -0,0 +1,40 @@
import filecmp
import os
import subprocess
from unittest import TestCase
class TestJ2Files(TestCase):
def setUp(self):
self.test_dir = os.path.dirname(os.path.realpath(__file__))
self.script_file = os.path.join(self.test_dir, '..', 'sonic-cfggen')
self.t0_minigraph = os.path.join(self.test_dir, 't0-sample-graph.xml')
self.t0_port_config = os.path.join(self.test_dir, 't0-sample-port-config.ini')
self.output_file = os.path.join(self.test_dir, 'output')
def run_script(self, argument):
print 'CMD: sonic-cfggen ' + argument
return subprocess.check_output(self.script_file + ' ' + argument, shell=True)
def test_teamd(self):
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -v "minigraph_portchannels.keys() | join(\' \')"'
output = self.run_script(argument) # Mock the output via config.sh in docker-teamd
pc_list = output.split()
def test_render_teamd(self, pc):
teamd_file = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-teamd', 'teamd.j2')
sample_output_file = os.path.join(self.test_dir, 'sample_output',pc + '.conf')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -a \'{\"pc\":\"' + pc + '\"}\' -t ' + teamd_file + ' > ' + self.output_file
self.run_script(argument)
assert filecmp.cmp(sample_output_file, self.output_file)
for i in range(1, 5):
pc_name = 'PortChannel0' + str(i)
assert pc_name in pc_list
test_render_teamd(self, pc_name)
def tearDown(self):
try:
os.remove(self.output_file)
except OSError:
pass