def create_sonic_yang_with_loaded_models(self): # sonic_yang_with_loaded_models will only be initialized once the first time this method is called if self.sonic_yang_with_loaded_models is None: loaded_models_sy = sonic_yang.SonicYang(self.yang_dir) loaded_models_sy.loadYangModel() # This call takes a long time (100s of ms) because it reads files from disk self.sonic_yang_with_loaded_models = loaded_models_sy return copy.copy(self.sonic_yang_with_loaded_models)
def create_sonic_yang_with_loaded_models(self): # sonic_yang_with_loaded_models will only be initialized once the first time this method is called if self.sonic_yang_with_loaded_models is None: sonic_yang_print_log_enabled = genericUpdaterLogging.get_verbose() loaded_models_sy = sonic_yang.SonicYang(self.yang_dir, print_log_enabled=sonic_yang_print_log_enabled) loaded_models_sy.loadYangModel() # This call takes a long time (100s of ms) because it reads files from disk self.sonic_yang_with_loaded_models = loaded_models_sy return copy.copy(self.sonic_yang_with_loaded_models)
def crop_tables_without_yang(self, config_db_as_json): sy = sonic_yang.SonicYang(self.yang_dir) sy.loadYangModel() sy.jIn = copy.deepcopy(config_db_as_json) sy.tablesWithOutYang = dict() sy._cropConfigDB() return sy.jIn
def __init__(self, path=YANG_MODELS_DIR): """ sonic_yang only supports python3 """ if PY3x: import sonic_yang self.yang_parser = sonic_yang.SonicYang(path) self.yang_parser.loadYangModel() self.test_dir = os.path.dirname(os.path.realpath(__file__)) self.script_file = PYTHON_INTERPRETTER + ' ' + os.path.join( self.test_dir, '..', 'sonic-cfggen')
def convert_config_db_to_sonic_yang(self, config_db_as_json): sy = sonic_yang.SonicYang(self.yang_dir) sy.loadYangModel() # Crop config_db tables that do not have sonic yang models cropped_config_db_as_json = self.crop_tables_without_yang(config_db_as_json) sonic_yang_as_json = dict() sy._xlateConfigDBtoYang(cropped_config_db_as_json, sonic_yang_as_json) return sonic_yang_as_json
def validate_config_db_config(self, config_db_as_json): sy = sonic_yang.SonicYang(self.yang_dir) sy.loadYangModel() try: tmp_config_db_as_json = copy.deepcopy(config_db_as_json) sy.loadData(tmp_config_db_as_json) sy.validate_data_tree() return True except sonic_yang.SonicYangException as ex: return False
def validate_sonic_yang_config(self, sonic_yang_as_json): config_db_as_json = self.convert_sonic_yang_to_config_db(sonic_yang_as_json) sy = sonic_yang.SonicYang(self.yang_dir) sy.loadYangModel() try: sy.loadData(config_db_as_json) sy.validate_data_tree() return True except sonic_yang.SonicYangException as ex: return False
def sonic_yang_data(self): sonic_yang_dir = "../sonic-yang-models/yang-models/" sonic_yang_test_file = "../sonic-yang-models/tests/files/sample_config_db.json" syc = sy.SonicYang(sonic_yang_dir) syc.loadYangModel() sonic_yang_data = dict() sonic_yang_data['yang_dir'] = sonic_yang_dir sonic_yang_data['test_file'] = sonic_yang_test_file sonic_yang_data['syc'] = syc return sonic_yang_data
def get_module_name(yang_module_str): """ Read yangs module name from yang_module_str Parameters: yang_module_str(str): YANG module string. Returns: str: Module name """ # Instantiate new context since parse_module_mem() loads the module into context. sy = sonic_yang.SonicYang(YANG_DIR) module = sy.ctx.parse_module_mem(yang_module_str, ly.LYS_IN_YANG) return module.name()
def __init_sonic_yang(self): self.sy = sonic_yang.SonicYang(YANG_DIR, debug=self.DEBUG) # load yang models self.sy.loadYangModel() # load jIn from config DB or from config DB json file. if self.source.lower() == 'configdb': self.readConfigDB() # treat any other source as file input else: self.readConfigDBJson(self.source) # this will crop config, xlate and load. self.sy.loadData(self.configdbJsonIn) # Raise if tables without YANG models are not allowed but exist. if not self.allowTablesWithoutYang and len(self.sy.tablesWithOutYang): raise Exception('Config has tables without YANG models')
def __init__(self, source="configDB", debug=False, allowTablesWithoutYang=True): ''' Initialise the class, --read the config, --load in data tree. Parameters: source (str): source for input config, default configDb else file. debug (bool): verbose mode. allowTablesWithoutYang (bool): allow tables without yang model in config or not. Returns: void ''' try: self.configdbJsonIn = None self.configdbJsonOut = None self.allowTablesWithoutYang = allowTablesWithoutYang # logging vars self.SYSLOG_IDENTIFIER = "ConfigMgmt" self.DEBUG = debug self.sy = sonic_yang.SonicYang(YANG_DIR, debug=debug) # load yang models self.sy.loadYangModel() # load jIn from config DB or from config DB json file. if source.lower() == 'configdb': self.readConfigDB() # treat any other source as file input else: self.readConfigDBJson(source) # this will crop config, xlate and load. self.sy.loadData(self.configdbJsonIn) # Raise if tables without YANG models are not allowed but exist. if not allowTablesWithoutYang and len(self.sy.tablesWithOutYang): raise Exception('Config has tables without YANG models') except Exception as e: self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR, msg=str(e)) raise (Exception('ConfigMgmt Class creation failed')) return
def _find_leafref_paths(self, path, config): sy = sonic_yang.SonicYang(YANG_DIR) sy.loadYangModel() sy.loadData(config) xpath = self.convert_path_to_xpath(path, config, sy) leaf_xpaths = self._get_inner_leaf_xpaths(xpath, sy) ref_xpaths = [] for xpath in leaf_xpaths: ref_xpaths.extend(sy.find_data_dependencies(xpath)) ref_paths = [] for ref_xpath in ref_xpaths: ref_path = self.convert_xpath_to_path(ref_xpath, config, sy) ref_paths.append(ref_path) return set(ref_paths)
def convert_sonic_yang_to_config_db(self, sonic_yang_as_json): sy = sonic_yang.SonicYang(self.yang_dir) sy.loadYangModel() # replace container of the format 'module:table' with just 'table' new_sonic_yang_json = {} for module_top in sonic_yang_as_json: new_sonic_yang_json[module_top] = {} for container in sonic_yang_as_json[module_top]: tokens = container.split(':') if len(tokens) > 2: raise ValueError(f"Expecting '<module>:<table>' or '<table>', found {container}") table = container if len(tokens) == 1 else tokens[1] new_sonic_yang_json[module_top][table] = sonic_yang_as_json[module_top][container] config_db_as_json = dict() sy.xlateJson = new_sonic_yang_json sy.revXlateJson = config_db_as_json sy._revXlateYangtoConfigDB(new_sonic_yang_json, config_db_as_json) return config_db_as_json
def setUp(self): self.path_addressing = gu_common.PathAddressing(gu_common.ConfigWrapper()) self.sy_only_models = sonic_yang.SonicYang(gu_common.YANG_DIR) self.sy_only_models.loadYangModel()
def yang_s(self, data): yang_dir = str(data['yang_dir']) yang_s = sy.SonicYang(yang_dir) return yang_s
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()