Skip to content
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
66 changes: 66 additions & 0 deletions src/sonic-config-engine/config_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
import os
import sys
from natsort import natsorted

def generate_t1_sample_config(data):
data['DEVICE_METADATA']['localhost']['hostname'] = 'sonic'
data['DEVICE_METADATA']['localhost']['type'] = 'LeafRouter'
data['DEVICE_METADATA']['localhost']['bgp_asn'] = '65100'
data['LOOPBACK_INTERFACE'] = {"Loopback0|10.1.0.1/32": {}}
data['BGP_NEIGHBOR'] = {}
data['DEVICE_NEIGHBOR'] = {}
data['INTERFACE'] = {}
port_count = 0
total_port_amount = len(data['PORT'])
for port in natsorted(data['PORT'].keys()):
data['PORT'][port]['admin_status'] = 'up'
data['PORT'][port]['mtu'] = '9100'
local_addr = '10.0.{}.{}'.format(2 * port_count / 256, 2 * port_count % 256)
peer_addr = '10.0.{}.{}'.format(2 * port_count / 256, 2 * port_count % 256 + 1)
peer_name='ARISTA{0:02d}{1}'.format(1+port_count%(total_port_amount/2), 'T2' if port_count < (total_port_amount/2) else 'T0')
peer_asn = 65200 if port_count < total_port_amount/2 else 64001 + port_count - total_port_amount/2
data['INTERFACE']['{}|{}/31'.format(port, local_addr)] = {}
data['BGP_NEIGHBOR'][peer_addr] = {
'rrclient': 0,
'name': peer_name,
'local_addr': local_addr,
'nhopself': 0,
'holdtime': '180',
'asn': str(peer_asn),
'keepalive': '60'
}
port_count += 1
return data;

def generate_empty_config(data):
new_data = {'DEVICE_METADATA': data['DEVICE_METADATA']}
if not new_data['DEVICE_METADATA']['localhost'].has_key('hostname'):
new_data['DEVICE_METADATA']['localhost']['hostname'] = 'sonic'
if not new_data['DEVICE_METADATA']['localhost'].has_key('type'):
new_data['DEVICE_METADATA']['localhost']['type'] = 'LeafRouter'
return new_data

def generate_l2_config(data):
if not data['DEVICE_METADATA']['localhost'].has_key('hostname'):
data['DEVICE_METADATA']['localhost']['hostname'] = 'sonic'
if not data['DEVICE_METADATA']['localhost'].has_key('type'):
data['DEVICE_METADATA']['localhost']['type'] = 'ToRRouter'
data['VLAN'] = {'Vlan1000': {'vlanid': '1000'}}
data['VLAN_MEMBER'] = {}
for port in natsorted(data['PORT'].keys()):
data['VLAN_MEMBER']['Vlan1000|{}'.format(port)] = {'tagging_mode': 'untagged'}
return data

_sample_generators = {
't1': generate_t1_sample_config,
'l2': generate_l2_config,
'empty': generate_empty_config
}

def get_available_config():
return _sample_generators.keys()

def generate_sample_config(data, setting_name):
return _sample_generators[setting_name.lower()](data)

2 changes: 1 addition & 1 deletion src/sonic-config-engine/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_test_suite():
author='Taoyu Li',
author_email='taoyl@microsoft.com',
url='https://github.com/Azure/sonic-buildimage',
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'sonic_platform'],
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'sonic_platform', 'config_samples'],
scripts=['sonic-cfggen'],
install_requires=['lxml', 'jinja2>=2.10', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'],
test_suite='setup.get_test_suite',
Expand Down
7 changes: 7 additions & 0 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ from portconfig import get_port_config
from sonic_platform import get_machine_info
from sonic_platform import get_platform_info
from sonic_platform import get_system_mac
from config_samples import generate_sample_config
from config_samples import get_available_config
from swsssdk import ConfigDBConnector
from collections import OrderedDict
from natsort import natsorted
Expand Down Expand Up @@ -156,6 +158,7 @@ def main():
group.add_argument("--var-json", help="print the value of a variable, in json format")
group.add_argument("--write-to-db", help="write config into configdb", action='store_true')
group.add_argument("--print-data", help="print all data", action='store_true')
group.add_argument("--preset", help="generate sample configuration from a preset template", choices=get_available_config())
args = parser.parse_args()

platform = get_platform_info(get_machine_info())
Expand Down Expand Up @@ -244,6 +247,10 @@ def main():
if args.print_data:
print(json.dumps(FormatConverter.to_serialized(data), indent=4, cls=minigraph_encoder))

if args.preset != None:
data = generate_sample_config(data, args.preset)
print(json.dumps(FormatConverter.to_serialized(data), indent=4, cls=minigraph_encoder))


if __name__ == "__main__":
main()