-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Yang][cfggen] update sonic-cfggen to generate config_db from Yang da…
…ta (#7712) Why I did it This PR adds changes in sonic-config-engine to consume configuration data in SONiC Yang schema and generate config_db entries How I did it Add a new file sonic_yang_cfg_generator . This file has the functions to parse yang data json and convert them in config_db json format. Validate the converted config_db entries to make sure all the dependencies and constraints are met. Add a new option -Y to the sonic-cfggen command for this purpose Add unit tests This capability is support only in sonic-config-engine Python3 package only
- Loading branch information
Showing
11 changed files
with
800 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""sonic_yang_cfg_generator | ||
version_added: "1.0" | ||
author: Arvindsrinivasan Lakshmi naraismhan (arlakshm@microsoft.com) | ||
short_description: Parse sonic_yang data and generate the config_db entries | ||
""" | ||
|
||
from __future__ import print_function | ||
|
||
import json | ||
import sys | ||
|
||
|
||
import sonic_yang | ||
# TODO: Remove this once we no longer support Python 2 | ||
if sys.version_info.major == 3: | ||
UNICODE_TYPE = str | ||
else: | ||
UNICODE_TYPE = unicode | ||
|
||
YANG_MODELS_DIR = "/usr/local/yang-models" | ||
DEFAULT_YANG_DATA_FILE = "/etc/sonic/config_yang.json" | ||
|
||
|
||
class SonicYangCfgDbGenerator: | ||
|
||
def __init__(self, yang_models_dir=YANG_MODELS_DIR): | ||
self.yang_models_dir = yang_models_dir | ||
self.yang_parser = sonic_yang.SonicYang(self.yang_models_dir) | ||
self.yang_parser.loadYangModel() | ||
|
||
def get_config_db_from_yang_data(self, | ||
yang_data_file=DEFAULT_YANG_DATA_FILE): | ||
self.yang_data_file = yang_data_file | ||
config_db_json = dict() | ||
with open(self.yang_data_file, "r") as yang_file: | ||
try: | ||
self.yang_data = json.load(yang_file) | ||
config_db_json = self.yang_parser.XlateYangToConfigDB( | ||
yang_data=self.yang_data) | ||
except json.JSONDecodeError as e: | ||
print("Unable to parse Yang data file {} Error: {}".format( | ||
yang_data_file, e)) | ||
return config_db_json | ||
|
||
def validate_config_db_json(self, config_db_json): | ||
self.yang_parser.loadData(configdbJson=config_db_json) | ||
try: | ||
self.yang_parser.validate_data_tree() | ||
return True | ||
except sonic_yang.SonicYangException as e: | ||
print("yang data in {} is not valid".format(self.yang_data_file)) | ||
return False | ||
|
||
def generate_config(self, yang_data_file=DEFAULT_YANG_DATA_FILE): | ||
config_db_json = self.get_config_db_from_yang_data( | ||
yang_data_file=yang_data_file) | ||
if self.validate_config_db_json(config_db_json): | ||
return config_db_json | ||
else: | ||
return {} |
Oops, something went wrong.