mirror of
https://github.com/Telecominfraproject/ols-nos.git
synced 2025-12-01 05:34:01 +00:00
[sonic-cfggen] Add support to generate sample t1 config (#2039)
* [sonic-cfggen] Add support to generate sample t1 config * Fix typo * Fix space issue * Add list of preset messages in help message * Utilize choice support of argparse * Fix missing module in setup.py
This commit is contained in:
66
src/sonic-config-engine/config_samples.py
Normal file
66
src/sonic-config-engine/config_samples.py
Normal file
@@ -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)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ setup(name='sonic-config-engine',
|
|||||||
author='Taoyu Li',
|
author='Taoyu Li',
|
||||||
author_email='taoyl@microsoft.com',
|
author_email='taoyl@microsoft.com',
|
||||||
url='https://github.com/Azure/sonic-buildimage',
|
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'],
|
scripts=['sonic-cfggen'],
|
||||||
install_requires=['lxml', 'jinja2>=2.10', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'],
|
install_requires=['lxml', 'jinja2>=2.10', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'],
|
||||||
test_suite='setup.get_test_suite',
|
test_suite='setup.get_test_suite',
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ from portconfig import get_port_config
|
|||||||
from sonic_platform import get_machine_info
|
from sonic_platform import get_machine_info
|
||||||
from sonic_platform import get_platform_info
|
from sonic_platform import get_platform_info
|
||||||
from sonic_platform import get_system_mac
|
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 swsssdk import ConfigDBConnector
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from natsort import natsorted
|
from natsort import natsorted
|
||||||
@@ -156,6 +158,7 @@ def main():
|
|||||||
group.add_argument("--var-json", help="print the value of a variable, in json format")
|
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("--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("--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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
platform = get_platform_info(get_machine_info())
|
platform = get_platform_info(get_machine_info())
|
||||||
@@ -244,6 +247,10 @@ def main():
|
|||||||
if args.print_data:
|
if args.print_data:
|
||||||
print(json.dumps(FormatConverter.to_serialized(data), indent=4, cls=minigraph_encoder))
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user