sonic-buildimage/src/sonic-config-engine/sonic_yang_cfg_generator.py
arlakshm 4d07bbbec6
[Yang][cfggen] update sonic-cfggen to generate config_db from Yang data (#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
2021-06-10 12:03:33 -07:00

61 lines
2.0 KiB
Python

"""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 {}