Skip to content

Commit d117172

Browse files
authored
sonic-cfggen supports hwsku parameter (#1631)
1 parent f7151e8 commit d117172

File tree

6 files changed

+171
-18
lines changed

6 files changed

+171
-18
lines changed

sonic-slave/Dockerfile

+10-8
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,12 @@ RUN apt-get update && apt-get install -y \
184184
libjs-sphinxdoc \
185185
libjs-underscore \
186186
python-docutils \
187-
python-jinja2 \
188187
python-markupsafe \
189188
python-pygments \
190189
python-roman \
191-
python-sphinx \
192190
sphinx-common \
193-
python3-sphinx \
194191
# For sonic config engine testing
195192
python-lxml \
196-
python-jinja2 \
197193
python-netaddr \
198194
python-ipaddr \
199195
python-yaml \
@@ -229,16 +225,22 @@ RUN export VERSION=1.8.3 \
229225
&& echo 'export GOROOT=/usr/local/go' >> /etc/bash.bashrc \
230226
&& echo 'export PATH=$PATH:$GOROOT/bin' >> /etc/bash.bashrc
231227

228+
# Upgrade pip2
229+
# Note: use pip2 specific version so jinja2 2.10 will install
230+
RUN python2 -m pip install -U pip==9.0.3
231+
232232
# For p4 build
233233
RUN pip install \
234234
ctypesgen \
235235
crc16
236236

237-
# For templating
238-
RUN pip install j2cli
239-
240237
# For sonic config engine testing
241-
RUN pip install pyangbind==0.5.10
238+
RUN pip install pyangbind==0.6.0
239+
# Note: force upgrade debian packaged jinja2, if installed
240+
RUN pip install --force-reinstall --upgrade jinja2>=2.10
241+
242+
# For templating (requiring jinja2)
243+
RUN pip install j2cli
242244

243245
# For sonic utilities testing
244246
RUN pip install click-default-group click natsort tabulate
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"DEVICE_METADATA": {{ DEVICE_METADATA | tojson }},
3+
"PORT": {{ PORT | tojson }},
4+
"VLAN": {
5+
"Vlan1000": {
6+
"vlanid": "1000"
7+
}
8+
},
9+
{% set ns = {'firstPrinted': False} -%}
10+
"VLAN_MEMBER": {
11+
{%- for port in PORT -%}
12+
{%- if ns.firstPrinted %},{% endif %}
13+
14+
"Vlan1000|{{ port }}": {
15+
"tagging_mode": "untagged"
16+
}
17+
{%- if ns.update({'firstPrinted': True}) %}{% endif -%}
18+
{%- endfor %}
19+
20+
}
21+
}

src/sonic-config-engine/setup.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from setuptools import setup
44
import os.path
55
import unittest
6+
import glob
67

78
def get_test_suite():
89
test_loader = unittest.TestLoader()
@@ -17,6 +18,9 @@ def get_test_suite():
1718
url='https://github.com/Azure/sonic-buildimage',
1819
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'sonic_platform'],
1920
scripts=['sonic-cfggen'],
20-
install_requires=['lxml', 'jinja2', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'],
21+
install_requires=['lxml', 'jinja2>=2.10', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'],
2122
test_suite='setup.get_test_suite',
23+
data_files=[
24+
('/usr/share/sonic/templates', glob.glob('data/*')),
25+
],
2226
)

src/sonic-config-engine/sonic-cfggen

+20-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Examples:
1515
See usage string for detail description for arguments.
1616
"""
1717

18+
from __future__ import print_function
1819
import sys
1920
import os.path
2021
import argparse
@@ -26,6 +27,7 @@ from functools import partial
2627
from minigraph import minigraph_encoder
2728
from minigraph import parse_xml
2829
from minigraph import parse_device_desc_xml
30+
from portconfig import get_port_config
2931
from sonic_platform import get_machine_info
3032
from sonic_platform import get_platform_info
3133
from sonic_platform import get_system_mac
@@ -140,7 +142,8 @@ def main():
140142
group = parser.add_mutually_exclusive_group()
141143
group.add_argument("-m", "--minigraph", help="minigraph xml file", nargs='?', const='/etc/sonic/minigraph.xml')
142144
group.add_argument("-M", "--device-description", help="device description xml file")
143-
parser.add_argument("-p", "--port-config", help="port config file, used with -m")
145+
group.add_argument("-k", "--hwsku", help="HwSKU")
146+
parser.add_argument("-p", "--port-config", help="port config file, used with -m or -k", nargs='?', const=None)
144147
parser.add_argument("-y", "--yaml", help="yaml file that contains additional variables", action='append', default=[])
145148
parser.add_argument("-j", "--json", help="json file that contains additional variables", action='append', default=[])
146149
parser.add_argument("-a", "--additional-data", help="addition data, in json string")
@@ -162,7 +165,18 @@ def main():
162165
db_kwargs['unix_socket_path'] = args.redis_unix_sock_file
163166

164167
data = {}
168+
hwsku = args.hwsku
165169

170+
if hwsku is not None:
171+
hardware_data = {'DEVICE_METADATA': {'localhost': {
172+
'hwsku': hwsku
173+
}}}
174+
deep_update(data, hardware_data)
175+
(ports, _) = get_port_config(hwsku, platform, args.port_config)
176+
if not ports:
177+
print('Failed to get port config', file=sys.stderr)
178+
sys.exit(1)
179+
deep_update(data, {'PORT': ports})
166180

167181
if args.minigraph != None:
168182
minigraph = args.minigraph
@@ -172,10 +186,7 @@ def main():
172186
else:
173187
deep_update(data, parse_xml(minigraph, platform))
174188
else:
175-
if args.port_config != None:
176-
deep_update(data, parse_xml(minigraph, port_config_file=args.port_config))
177-
else:
178-
deep_update(data, parse_xml(minigraph))
189+
deep_update(data, parse_xml(minigraph, port_config_file=args.port_config))
179190

180191
if args.device_description != None:
181192
deep_update(data, parse_device_desc_xml(args.device_description))
@@ -216,22 +227,22 @@ def main():
216227
for attr in ['ip', 'network', 'prefixlen', 'netmask']:
217228
env.filters[attr] = partial(prefix_attr, attr)
218229
template = env.get_template(template_file)
219-
print template.render(sort_data(data))
230+
print(template.render(sort_data(data)))
220231

221232
if args.var != None:
222233
template = jinja2.Template('{{' + args.var + '}}')
223-
print template.render(data)
234+
print(template.render(data))
224235

225236
if args.var_json != None:
226-
print json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder)
237+
print(json.dumps(FormatConverter.to_serialized(data[args.var_json]), indent=4, cls=minigraph_encoder))
227238

228239
if args.write_to_db:
229240
configdb = ConfigDBConnector(**db_kwargs)
230241
configdb.connect(False)
231242
configdb.mod_config(FormatConverter.output_to_db(data))
232243

233244
if args.print_data:
234-
print json.dumps(FormatConverter.to_serialized(data), indent=4, cls=minigraph_encoder)
245+
print(json.dumps(FormatConverter.to_serialized(data), indent=4, cls=minigraph_encoder))
235246

236247

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

src/sonic-config-engine/tests/test_j2files.py

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ def test_msn27xx_32ports_buffers(self):
106106

107107
self.assertTrue(filecmp.cmp(sample_output_file, self.output_file))
108108

109+
def test_sku_render_template(self):
110+
argument = '-k Mellanox-SN2700 -t ' + os.path.join(self.test_dir, '../data/l2switch.j2') + ' -p ' + self.t0_port_config + ' > ' + self.output_file
111+
self.run_script(argument)
112+
113+
sample_output_file = os.path.join(self.test_dir, 'sample_output', 'l2switch.json')
114+
115+
self.assertTrue(filecmp.cmp(sample_output_file, self.output_file))
116+
109117
def tearDown(self):
110118
try:
111119
os.remove(self.output_file)

0 commit comments

Comments
 (0)