-
Notifications
You must be signed in to change notification settings - Fork 809
Add verification for override #2305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
4f79838
245d114
9f01b48
73f8b25
76c7efa
d37d35f
f7966fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
| import sys | ||
| import time | ||
| import itertools | ||
| import sonic_yang | ||
| import copy | ||
|
|
||
| from collections import OrderedDict | ||
| from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat | ||
|
|
@@ -77,6 +79,7 @@ | |
| DEFAULT_GOLDEN_CONFIG_DB_FILE = '/etc/sonic/golden_config_db.json' | ||
|
|
||
| INIT_CFG_FILE = '/etc/sonic/init_cfg.json' | ||
| YANG_DIR = '/usr/local/yang-models' | ||
|
|
||
| DEFAULT_NAMESPACE = '' | ||
| CFG_LOOPBACK_PREFIX = "Loopback" | ||
|
|
@@ -1852,27 +1855,71 @@ def override_config_table(db, input_config_db, dry_run): | |
|
|
||
| config_db = db.cfgdb | ||
|
|
||
| # Read config from configDB | ||
| current_config = config_db.get_config() | ||
| # Serialize to the same format as json input | ||
| sonic_cfggen.FormatConverter.to_serialized(current_config) | ||
|
|
||
| updated_config = update_config(current_config, config_input) | ||
|
|
||
| yang_enabled = is_yang_config_verification_enabled(config_db) | ||
| if yang_enabled: | ||
| sy = sonic_yang_with_loaded_models(YANG_DIR) | ||
| # Validate running config | ||
| validate_config_by_yang(sy, current_config) | ||
| # Validate input config | ||
| validate_config_by_yang(sy, config_input) | ||
| # Validate updated whole config | ||
| validate_config_by_yang(sy, updated_config) | ||
|
|
||
| if dry_run: | ||
| # Read config from configDB | ||
| current_config = config_db.get_config() | ||
| # Serialize to the same format as json input | ||
| sonic_cfggen.FormatConverter.to_serialized(current_config) | ||
| # Override current config with golden config | ||
| for table in config_input: | ||
| current_config[table] = config_input[table] | ||
| print(json.dumps(current_config, sort_keys=True, | ||
| print(json.dumps(updated_config, sort_keys=True, | ||
| indent=4, cls=minigraph_encoder)) | ||
| else: | ||
| # Deserialized golden config to DB recognized format | ||
| sonic_cfggen.FormatConverter.to_deserialized(config_input) | ||
| # Delete table from DB then mod_config to apply golden config | ||
| click.echo("Removing configDB overriden table first ...") | ||
| for table in config_input: | ||
| config_db.delete_table(table) | ||
| click.echo("Overriding input config to configDB ...") | ||
| data = sonic_cfggen.FormatConverter.output_to_db(config_input) | ||
| config_db.mod_config(data) | ||
| click.echo("Overriding completed. No service is restarted.") | ||
| override_config_db(config_db, config_input) | ||
|
|
||
|
|
||
| def is_yang_config_verification_enabled(config_db): | ||
|
||
| device_metadata = config_db.get_entry('DEVICE_METADATA', 'localhost') | ||
| return 'enable' == device_metadata.get('yang_config_validation', None) | ||
|
|
||
|
|
||
| def sonic_yang_with_loaded_models(yang_dir): | ||
| sy = sonic_yang.SonicYang(yang_dir) | ||
| sy.loadYangModel() | ||
| return sy | ||
|
|
||
|
|
||
| def validate_config_by_yang(sy, config_json): | ||
| try: | ||
| tmp_config_json = copy.deepcopy(config_json) | ||
| sy.loadData(tmp_config_json) | ||
| sy.validate_data_tree() | ||
| except sonic_yang.SonicYangException as ex: | ||
| click.secho("Failed to validate config. Error: {}".format(ex), fg="red") | ||
| sys.exit(1) | ||
wen587 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def update_config(current_config, config_input): | ||
| updated_config = copy.deepcopy(current_config) | ||
| # Override current config with golden config | ||
| for table in config_input: | ||
| updated_config[table] = config_input[table] | ||
| return updated_config | ||
|
|
||
|
|
||
| def override_config_db(config_db, config_input): | ||
| # Deserialized golden config to DB recognized format | ||
| sonic_cfggen.FormatConverter.to_deserialized(config_input) | ||
| # Delete table from DB then mod_config to apply golden config | ||
| click.echo("Removing configDB overriden table first ...") | ||
| for table in config_input: | ||
| config_db.delete_table(table) | ||
| click.echo("Overriding input config to configDB ...") | ||
| data = sonic_cfggen.FormatConverter.output_to_db(config_input) | ||
| config_db.mod_config(data) | ||
| click.echo("Overriding completed. No service is restarted.") | ||
|
|
||
|
|
||
| # | ||
| # 'hostname' command | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| { | ||
| "running_config": { | ||
| "ACL_TABLE": { | ||
| "DATAACL": { | ||
| "policy_desc": "DATAACL", | ||
| "ports": [ | ||
| "Ethernet4" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "L3" | ||
| }, | ||
| "NTP_ACL": { | ||
| "policy_desc": "NTP_ACL", | ||
| "services": [ | ||
| "NTP" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "CTRLPLANE" | ||
| } | ||
| }, | ||
| "AUTO_TECHSUPPORT_FEATURE": { | ||
| "bgp": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| }, | ||
| "database": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| } | ||
| }, | ||
| "PORT": { | ||
| "Ethernet4": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/4", | ||
| "description": "Servers0:eth0", | ||
| "index": "1", | ||
| "lanes": "29,30,31,32", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| }, | ||
| "Ethernet8": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/8", | ||
| "description": "Servers1:eth0", | ||
| "index": "2", | ||
| "lanes": "33,34,35,36", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| }, | ||
| "golden_config": { | ||
| "PORT": { | ||
| "Ethernet12": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/12", | ||
| "description": "Servers2:eth0", | ||
| "index": "3", | ||
| "lanes": "37,38,39,40", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| { | ||
| "running_config": { | ||
| "ACL_TABLE": { | ||
| "DATAACL": { | ||
| "policy_desc": "DATAACL", | ||
| "ports": [ | ||
| "Ethernet4" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "L3" | ||
| }, | ||
| "NTP_ACL": { | ||
| "policy_desc": "NTP_ACL", | ||
| "services": [ | ||
| "NTP" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "CTRLPLANE" | ||
| } | ||
| }, | ||
| "AUTO_TECHSUPPORT_FEATURE": { | ||
| "bgp": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| }, | ||
| "database": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| } | ||
| }, | ||
| "PORT": { | ||
| "Ethernet4": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/4", | ||
| "description": "Servers0:eth0", | ||
| "index": "1", | ||
| "lanes": "29,30,31,32", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| }, | ||
| "Ethernet8": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/8", | ||
| "description": "Servers1:eth0", | ||
| "index": "2", | ||
| "lanes": "33,34,35,36", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| }, | ||
| "golden_config": { | ||
| "ACL_TABLE": { | ||
| "EVERFLOWV6": { | ||
| "policy_desc": "EVERFLOWV6", | ||
| "ports": [ | ||
| "Ethernet0" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "MIRRORV6" | ||
| } | ||
| }, | ||
| "AUTO_TECHSUPPORT_FEATURE": { | ||
| "bgp": { | ||
| "state": "disabled" | ||
| }, | ||
| "database": { | ||
| "state": "disabled" | ||
| } | ||
| }, | ||
| "PORT": { | ||
| "Ethernet12": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/12", | ||
| "description": "Servers2:eth0", | ||
| "index": "3", | ||
| "lanes": "37,38,39,40", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| { | ||
| "running_config": { | ||
| "ACL_TABLE": { | ||
| "DATAACL": { | ||
| "policy_desc": "DATAACL", | ||
| "ports": [ | ||
| "Ethernet0" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "L3" | ||
| }, | ||
| "NTP_ACL": { | ||
| "policy_desc": "NTP_ACL", | ||
| "services": [ | ||
| "NTP" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "CTRLPLANE" | ||
| } | ||
| }, | ||
| "AUTO_TECHSUPPORT_FEATURE": { | ||
| "bgp": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| }, | ||
| "database": { | ||
| "rate_limit_interval": "600", | ||
| "state": "enabled" | ||
| } | ||
| }, | ||
| "PORT": { | ||
| "Ethernet4": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/4", | ||
| "description": "Servers0:eth0", | ||
| "index": "1", | ||
| "lanes": "29,30,31,32", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| }, | ||
| "Ethernet8": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/8", | ||
| "description": "Servers1:eth0", | ||
| "index": "2", | ||
| "lanes": "33,34,35,36", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| }, | ||
| "golden_config": { | ||
| "ACL_TABLE": { | ||
| "EVERFLOWV6": { | ||
| "policy_desc": "EVERFLOWV6", | ||
| "ports": [ | ||
| "Ethernet12" | ||
| ], | ||
| "stage": "ingress", | ||
| "type": "MIRRORV6" | ||
| } | ||
| }, | ||
| "AUTO_TECHSUPPORT_FEATURE": { | ||
| "bgp": { | ||
| "state": "disabled" | ||
| }, | ||
| "database": { | ||
| "state": "disabled" | ||
| } | ||
| }, | ||
| "PORT": { | ||
| "Ethernet12": { | ||
| "admin_status": "up", | ||
| "alias": "fortyGigE0/12", | ||
| "description": "Servers2:eth0", | ||
| "index": "3", | ||
| "lanes": "37,38,39,40", | ||
| "mtu": "9100", | ||
| "pfc_asym": "off", | ||
| "speed": "40000", | ||
| "tpid": "0x8100" | ||
| } | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.