Skip to content
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

Add verification for override #2305

Merged
merged 7 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
83 changes: 65 additions & 18 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
wen587 marked this conversation as resolved.
Show resolved Hide resolved
# 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):
Copy link
Contributor

@qiluo-msft qiluo-msft Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_yang_config_verification_enabled

This may be reused by @isabelmsft. Find a better place? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@qiluo-msft qiluo-msft Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you call the function in that PR now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Current call that funtion will fail PR checker. Because the VM being tested now has not included that PR.

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
Expand Down
71 changes: 71 additions & 0 deletions tests/config_override_input/final_config_yang_failure.json
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"
}
}
}
}
89 changes: 89 additions & 0 deletions tests/config_override_input/golden_input_yang_failure.json
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"
}
}
}
}
89 changes: 89 additions & 0 deletions tests/config_override_input/running_config_yang_failure.json
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"
}
}
}
}
Loading