diff --git a/build_debian.sh b/build_debian.sh index 666e140416c9..009b0306e73d 100755 --- a/build_debian.sh +++ b/build_debian.sh @@ -413,6 +413,10 @@ sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install 'docker sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install gcc libpython2.7-dev sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install 'netifaces==0.10.7' +# Get package to support Dynamic Port Breakout +sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install xmltodict +sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install jsondiff + ## Create /var/run/redis folder for docker-database to mount sudo mkdir -p $FILESYSTEM_ROOT/var/run/redis diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 4e81593298f6..139d2692e192 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -110,6 +110,15 @@ sudo cp {{swsssdk_py2_wheel_path}} $FILESYSTEM_ROOT/$SWSSSDK_PY2_WHEEL_NAME sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $SWSSSDK_PY2_WHEEL_NAME sudo rm -rf $FILESYSTEM_ROOT/$SWSSSDK_PY2_WHEEL_NAME +# Install sonic-yang-mgmt Python 2 package, install dependencies +sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/libyang_*.deb +sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/libyang-cpp_*.deb +sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/python2-yang_*.deb +SONIC_YANG_MGMT_PY2_WHEEL_NAME=$(basename {{sonic_yang_mgmt_py2_wheel_path}}) +sudo cp {{sonic_yang_mgmt_py2_wheel_path}} $FILESYSTEM_ROOT/$SONIC_YANG_MGMT_PY2_WHEEL_NAME +sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $SONIC_YANG_MGMT_PY2_WHEEL_NAME +sudo rm -rf $FILESYSTEM_ROOT/$SONIC_YANG_MGMT_PY2_WHEEL_NAME + # Install sonic-platform-common Python 2 package PLATFORM_COMMON_PY2_WHEEL_NAME=$(basename {{platform_common_py2_wheel_path}}) sudo cp {{platform_common_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2_WHEEL_NAME diff --git a/rules/sonic-yang-mgmt-py2.mk b/rules/sonic-yang-mgmt-py2.mk new file mode 100644 index 000000000000..c62dedd5b637 --- /dev/null +++ b/rules/sonic-yang-mgmt-py2.mk @@ -0,0 +1,8 @@ +# sonic-yang-mgmt python2 wheel + +SONIC_YANG_MGMT_PY2 = sonic_yang_mgmt-1.0-py2-none-any.whl +$(SONIC_YANG_MGMT_PY2)_SRC_PATH = $(SRC_PATH)/sonic-yang-mgmt +$(SONIC_YANG_MGMT_PY2)_PYTHON_VERSION = 2 +$(SONIC_YANG_MGMT_PY2)_DEBS_DEPENDS = $(LIBYANG) + +SONIC_PYTHON_WHEELS += $(SONIC_YANG_MGMT_PY2) diff --git a/slave.mk b/slave.mk index e518f1e4d8a5..239eec880f84 100644 --- a/slave.mk +++ b/slave.mk @@ -441,7 +441,8 @@ SONIC_TARGET_LIST += $(addprefix $(PYTHON_DEBS_PATH)/, $(SONIC_PYTHON_STDEB_DEBS # $(SOME_NEW_WHL)_PYTHON_VERSION = 2 (or 3) # $(SOME_NEW_WHL)_DEPENDS = $(SOME_OTHER_WHL1) $(SOME_OTHER_WHL2) ... # SONIC_PYTHON_WHEELS += $(SOME_NEW_WHL) -$(addprefix $(PYTHON_WHEELS_PATH)/, $(SONIC_PYTHON_WHEELS)) : $(PYTHON_WHEELS_PATH)/% : .platform $$(addsuffix -install,$$(addprefix $(PYTHON_WHEELS_PATH)/,$$($$*_DEPENDS))) +$(addprefix $(PYTHON_WHEELS_PATH)/, $(SONIC_PYTHON_WHEELS)) : $(PYTHON_WHEELS_PATH)/% : .platform $$(addsuffix -install,$$(addprefix $(PYTHON_WHEELS_PATH)/,$$($$*_DEPENDS))) \ + $$(addsuffix -install,$$(addprefix $(DEBS_PATH)/,$$($$*_DEBS_DEPENDS))) $(HEADER) pushd $($*_SRC_PATH) $(LOG) # apply series of patches if exist @@ -626,7 +627,8 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \ $(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_CONFIG_ENGINE)) \ $(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2)) \ $(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2)) \ - $(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2)) + $(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2)) \ + $(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_YANG_MGMT_PY2)) $(HEADER) # Pass initramfs and linux kernel explicitly. They are used for all platforms export debs_path="$(STRETCH_DEBS_PATH)" @@ -652,6 +654,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \ export platform_common_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2))" export redis_dump_load_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2))" export install_debug_image="$(INSTALL_DEBUG_TOOLS)" + export sonic_yang_mgmt_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_YANG_MGMT_PY2))" $(foreach docker, $($*_DOCKERS),\ export docker_image="$(docker)" diff --git a/sonic-slave-jessie/Dockerfile.j2 b/sonic-slave-jessie/Dockerfile.j2 index 7ed7c4eb7096..c082acc9e2b8 100644 --- a/sonic-slave-jessie/Dockerfile.j2 +++ b/sonic-slave-jessie/Dockerfile.j2 @@ -320,6 +320,9 @@ RUN pip install mockredispy==2.9.3 RUN pip install pytest-runner==4.4 RUN pip install setuptools==40.8.0 +# For sonic_yang_mgmt build +RUN pip install ijson + # Install dependencies for isc-dhcp-relay build RUN apt-get -y build-dep isc-dhcp diff --git a/sonic-slave-stretch/Dockerfile.j2 b/sonic-slave-stretch/Dockerfile.j2 index f52bd943b0a7..66cf42545339 100644 --- a/sonic-slave-stretch/Dockerfile.j2 +++ b/sonic-slave-stretch/Dockerfile.j2 @@ -352,6 +352,9 @@ RUN pip install mockredispy==2.9.3 RUN pip install pytest-runner==4.4 RUN pip install setuptools==40.8.0 +# For sonic_yang_mgmt build +RUN pip install ijson + # Install dependencies for isc-dhcp-relay build RUN apt-get -y build-dep isc-dhcp diff --git a/src/sonic-yang-mgmt/AUTHORS.rst b/src/sonic-yang-mgmt/AUTHORS.rst new file mode 100644 index 000000000000..b5530bc13df3 --- /dev/null +++ b/src/sonic-yang-mgmt/AUTHORS.rst @@ -0,0 +1,15 @@ +======= +Credits +======= + +Development Lead +---------------- + +LNOS-CODERS +MSFT-LINUX-DEV + +Contributors +------------ + +Praveen Chaudhary +Ping Mao diff --git a/src/sonic-yang-mgmt/LICENSE b/src/sonic-yang-mgmt/LICENSE new file mode 100644 index 000000000000..cf593b111eab --- /dev/null +++ b/src/sonic-yang-mgmt/LICENSE @@ -0,0 +1,13 @@ +Copyright 2019 Microsoft, Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/src/sonic-yang-mgmt/README.rst b/src/sonic-yang-mgmt/README.rst new file mode 100644 index 000000000000..31326b96b2d4 --- /dev/null +++ b/src/sonic-yang-mgmt/README.rst @@ -0,0 +1,5 @@ +This Package will contain YANG models for sonic which are written with guidelines mentioned in +https://github.com/Azure/SONiC/blob/master/doc/mgmt/SONiC_YANG_Model_Guidelines.md. + +This package will include python yang libraries which will be used with sonic utilities +pacakge to validate the config. diff --git a/src/sonic-yang-mgmt/_sonic_yang_ext.py b/src/sonic-yang-mgmt/_sonic_yang_ext.py new file mode 100644 index 000000000000..ddfc6010171c --- /dev/null +++ b/src/sonic-yang-mgmt/_sonic_yang_ext.py @@ -0,0 +1,628 @@ +# This script is used as extension of sonic_yang class. It has methods of +# class sonic_yang. A separate file is used to avoid a single large file. + +import yang as ly +import re +import pprint + +from json import dump, load, dumps, loads +from xmltodict import parse +from os import listdir, walk, path +from os.path import isfile, join, splitext +from glob import glob + +# class sonic_yang methods + +""" +load all YANG models, create JSON of yang models +""" +def loadYangModel(self): + + try: + yangDir = self.yang_dir + self.yangFiles = glob(yangDir +"/*.yang") + for file in self.yangFiles: + if (self.load_schema_module(file) == False): + return False + + # keep only modules name in self.yangFiles + self.yangFiles = [f.split('/')[-1] for f in self.yangFiles] + self.yangFiles = [f.split('.')[0] for f in self.yangFiles] + print('Loaded below Yang Models') + print(self.yangFiles) + + # load json for each yang model + self.loadJsonYangModel() + # create a map from config DB table to yang container + self.createDBTableToModuleMap() + + except Exception as e: + print("Yang Models Load failed") + raise e + + return True + +""" +load JSON schema format from yang models +""" +def loadJsonYangModel(self): + + try: + for f in self.yangFiles: + m = self.ctx.get_module(f) + if m is not None: + xml = m.print_mem(ly.LYD_JSON, ly.LYP_FORMAT) + self.yJson.append(parse(xml)) + except Exception as e: + print('JSON conversion for yang models failed') + raise e + + return + +""" +Create a map from config DB tables to container in yang model +This module name and topLevelContainer are fetched considering YANG models are +written using below Guidelines: +https://github.com/Azure/SONiC/blob/master/doc/mgmt/SONiC_YANG_Model_Guidelines.md. +""" +def createDBTableToModuleMap(self): + + for j in self.yJson: + # get module name + moduleName = j['module']['@name'] + if "sonic-head" in moduleName or "sonic-common" in moduleName: + continue; + # get all top level container + topLevelContainer = j['module']['container'] + if topLevelContainer is None: + raise Exception("topLevelContainer not found") + + assert topLevelContainer['@name'] == moduleName + + container = topLevelContainer['container'] + # container is a list + if isinstance(container, list): + for c in container: + self.confDbYangMap[c['@name']] = { + "module" : moduleName, + "topLevelContainer": topLevelContainer['@name'], + "container": c + } + # container is a dict + else: + self.confDbYangMap[container['@name']] = { + "module" : moduleName, + "topLevelContainer": topLevelContainer['@name'], + "container": container + } + return + +""" +Get module, topLevelContainer(TLC) and json container for a config DB table +""" +def get_module_TLC_container(self, table): + cmap = self.confDbYangMap + m = cmap[table]['module'] + t = cmap[table]['topLevelContainer'] + c = cmap[table]['container'] + return m, t, c + +""" +Crop config as per yang models, +This Function crops from config only those TABLEs, for which yang models is +provided. +""" +def cropConfigDB(self, croppedFile=None): + + for table in self.jIn.keys(): + if table not in self.confDbYangMap: + del self.jIn[table] + + if croppedFile: + with open(croppedFile, 'w') as f: + dump(self.jIn, f, indent=4) + + return + +""" +Extract keys from table entry in Config DB and return in a dict +For Example: regex = | and tableKey = "Vlan111|2a04:5555:45:6709::1/64" + +1.) first code will extract key list from regex, i.e. vlan_name and ip_prefix. +2.) then will create another regex(regexV) to extract Values from tableKey by + replacing " --> extractor i.e. (.*?)" in regex. +3.) Then will extract values from tableKey with regexV. +4.) Resulting Dict will be: +KeyDict = {"vlan_name": "Vlan111", "ip-prefix": "2a04:5555:45:6709::1/64"} +""" +def extractKey(self, tableKey, regex): + + # get the keys from regex of key extractor + keyList = re.findall(r'<(.*?)>', regex) + # create a regex to get values from tableKey + # and change separator to text in regexV + regexV = re.sub('<.*?>', '(.*?)', regex) + regexV = re.sub('\|', '\\|', regexV) + # get the value groups + value = re.match(r'^'+regexV+'$', tableKey) + # create the keyDict + i = 1 + keyDict = dict() + for k in keyList: + if value.group(i): + keyDict[k] = value.group(i) + else: + raise Exception("Value not found for {} in {}".format(k, tableKey)) + i = i + 1 + + return keyDict + +""" +Fill the dict based on leaf as a list or dict @model yang model object +""" +def fillLeafDict(self, leafs, leafDict, isleafList=False): + + if leafs == None: + return + + # fill default values + def fillSteps(leaf): + leaf['__isleafList'] = isleafList + leafDict[leaf['@name']] = leaf + return + + if isinstance(leafs, list): + for leaf in leafs: + #print("{}:{}".format(leaf['@name'], leaf)) + fillSteps(leaf) + else: + #print("{}:{}".format(leaf['@name'], leaf)) + fillSteps(leafs) + + return + +""" +create a dict to map each key under primary key with a dict yang model. +This is done to improve performance of mapping from values of TABLEs in +config DB to leaf in YANG LIST. +""" +def createLeafDict(self, model): + + leafDict = dict() + #Iterate over leaf, choices and leaf-list. + self.fillLeafDict(model.get('leaf'), leafDict) + + #choices, this is tricky, since leafs are under cases in tree. + choices = model.get('choice') + if choices: + for choice in choices: + cases = choice['case'] + for case in cases: + self.fillLeafDict(case.get('leaf'), leafDict) + + # leaf-lists + self.fillLeafDict(model.get('leaf-list'), leafDict, True) + + return leafDict + +""" +Convert a string from Config DB value to Yang Value based on type of the +key in Yang model. +@model : A List of Leafs in Yang model list +""" +def findYangTypedValue(self, key, value, leafDict): + + # convert config DB string to yang Type + def yangConvert(val): + # Convert everything to string + val = str(val) + # find type of this key from yang leaf + type = leafDict[key]['type']['@name'] + + if 'uint' in type: + vValue = int(val, 10) + # TODO: find type of leafref from schema node + elif 'leafref' in type: + vValue = val + #TODO: find type in sonic-head, as of now, all are enumeration + elif 'head:' in type: + vValue = val + else: + vValue = val + return vValue + + # if it is a leaf-list do it for each element + if leafDict[key]['__isleafList']: + vValue = list() + for v in value: + vValue.append(yangConvert(v)) + else: + vValue = yangConvert(value) + + return vValue + +""" +Xlate a list +This function will xlate from a dict in config DB to a Yang JSON list +using yang model. Output will be go in self.xlateJson +""" +def xlateList(self, model, yang, config, table): + + # TODO: define a keyExt dict as of now, but we should be able to extract + # this from YANG model extentions. + keyExt = { + "VLAN_INTERFACE": "|", + "ACL_RULE": "|", + "VLAN": "", + "VLAN_MEMBER": "|", + "ACL_TABLE": "", + "INTERFACE": "|", + "PORT": "" + } + #create a dict to map each key under primary key with a dict yang model. + #This is done to improve performance of mapping from values of TABLEs in + #config DB to leaf in YANG LIST. + + leafDict = self.createLeafDict(model) + + # Find and extracts key from each dict in config + for pkey in config: + try: + keyDict = self.extractKey(pkey, keyExt[table]) + # fill rest of the values in keyDict + for vKey in config[pkey]: + keyDict[vKey] = self.findYangTypedValue(vKey, \ + config[pkey][vKey], leafDict) + yang.append(keyDict) + except Exception as e: + print("Exception while Config DB --> YANG: pkey:{}, "\ + "vKey:{}, value: {}".format(pkey, vKey, config[pkey][vKey])) + raise e + + return + +""" +Xlate a container +This function will xlate from a dict in config DB to a Yang JSON container +using yang model. Output will be stored in self.xlateJson +""" +def xlateContainer(self, model, yang, config, table): + + # if container contains single list with containerName_LIST and + # config is not empty then xLate the list + clist = model.get('list') + if clist and isinstance(clist, dict) and \ + clist['@name'] == model['@name']+"_LIST" and bool(config): + #print(clist['@name']) + yang[clist['@name']] = list() + self.xlateList(model['list'], yang[clist['@name']], \ + config, table) + #print(yang[clist['@name']]) + + # TODO: Handle mupltiple list and rest of the field in Container. + # We do not have any such instance in Yang model today. + + return + +""" +xlate ConfigDB json to Yang json +""" +def xlateConfigDBtoYang(self, jIn, yangJ): + + # find top level container for each table, and run the xlate_container. + for table in jIn.keys(): + cmap = self.confDbYangMap[table] + # create top level containers + key = cmap['module']+":"+cmap['topLevelContainer'] + subkey = cmap['topLevelContainer']+":"+cmap['container']['@name'] + # Add new top level container for first table in this container + yangJ[key] = dict() if yangJ.get(key) is None else yangJ[key] + yangJ[key][subkey] = dict() + self.xlateContainer(cmap['container'], yangJ[key][subkey], \ + jIn[table], table) + + return + +""" +Read config file and crop it as per yang models +""" +def xlateConfigDB(self, xlateFile=None): + + jIn= self.jIn + yangJ = self.xlateJson + # xlation is written in self.xlateJson + self.xlateConfigDBtoYang(jIn, yangJ) + + if xlateFile: + with open(xlateFile, 'w') as f: + dump(self.xlateJson, f, indent=4) + + return + +""" +create config DB table key from entry in yang JSON +""" +def createKey(self, entry, regex): + + keyDict = dict() + keyV = regex + # get the keys from regex of key extractor + keyList = re.findall(r'<(.*?)>', regex) + for key in keyList: + val = entry.get(key) + if val: + #print("pair: {} {}".format(key, val)) + keyDict[key] = sval = str(val) + keyV = re.sub(r'<'+key+'>', sval, keyV) + #print("VAL: {} {}".format(regex, keyV)) + else: + raise Exception("key {} not found in entry".format(key)) + #print("kDict {}".format(keyDict)) + return keyV, keyDict + +""" +Convert a string from Config DB value to Yang Value based on type of the +key in Yang model. +@model : A List of Leafs in Yang model list +""" +def revFindYangTypedValue(self, key, value, leafDict): + + # convert yang Type to config DB string + def revYangConvert(val): + # config DB has only strings, thank god for that :), wait not yet!!! + return str(val) + + # if it is a leaf-list do it for each element + if leafDict[key]['__isleafList']: + vValue = list() + for v in value: + vValue.append(revYangConvert(v)) + else: + vValue = revYangConvert(value) + + return vValue + + +""" +Rev xlate from _LIST to table in config DB +""" +def revXlateList(self, model, yang, config, table): + + # TODO: define a keyExt dict as of now, but we should be able to + # extract this from YANG model extentions. + keyExt = { + "VLAN_INTERFACE": "|", + "ACL_RULE": "|", + "VLAN": "", + "VLAN_MEMBER": "|", + "ACL_TABLE": "", + "INTERFACE": "|", + "PORT": "" + } + + # create a dict to map each key under primary key with a dict yang model. + # This is done to improve performance of mapping from values of TABLEs in + # config DB to leaf in YANG LIST. + leafDict = self.createLeafDict(model) + + # list with name
_LIST should be removed, + # right now we have only this instance of LIST + if model['@name'] == table + "_LIST": + for entry in yang: + # create key of config DB table + pkey, pkeydict = self.createKey(entry, keyExt[table]) + config[pkey]= dict() + # fill rest of the entries + for key in entry: + if key not in pkeydict: + config[pkey][key] = self.revFindYangTypedValue(key, \ + entry[key], leafDict) + + return + +""" +Rev xlate from yang container to table in config DB +""" +def revXlateContainer(self, model, yang, config, table): + + # Note: right now containers has only LISTs. + # IF container has only one list + if isinstance(model['list'], dict): + modelList = model['list'] + # Pass matching list from Yang Json + self.revXlateList(modelList, yang[modelList['@name']], config, table) + else: + # TODO: Container[TABLE] contains multiple lists. [Test Pending] + # No instance now. + for modelList in model['list']: + self.revXlateList(modelList, yang[modelList['@name']], config, table) + + return + +""" +rev xlate ConfigDB json to Yang json +""" +def revXlateYangtoConfigDB(self, yangJ, cDbJson): + + yangJ = self.xlateJson + cDbJson = self.revXlateJson + + # find table in config DB, use name as a KEY + for module_top in yangJ.keys(): + # module _top will be of from module:top + for container in yangJ[module_top].keys(): + #table = container.split(':')[1] + table = container + #print("revXlate " + table) + cmap = self.confDbYangMap[table] + cDbJson[table] = dict() + #print(key + "--" + subkey) + self.revXlateContainer(cmap['container'], yangJ[module_top][container], \ + cDbJson[table], table) + + return + +""" +Reverse Translate tp config DB +""" +def revXlateConfigDB(self, revXlateFile=None): + + yangJ = self.xlateJson + cDbJson = self.revXlateJson + # xlation is written in self.xlateJson + self.revXlateYangtoConfigDB(yangJ, cDbJson) + + if revXlateFile: + with open(revXlateFile, 'w') as f: + dump(self.revXlateJson, f, indent=4) + + return + +""" +Find a list in YANG Container +c = container +l = list name +return: list if found else None +""" +def findYangList(self, container, listName): + + if isinstance(container['list'], dict): + clist = container['list'] + if clist['@name'] == listName: + return clist + + elif isinstance(container['list'], list): + clist = [l for l in container['list'] if l['@name'] == listName] + return clist[0] + + return None + +""" +Find xpath of the PORT Leaf in PORT container/list. Xpath of Leaf is needed, +because only leaf can have leafrefs depend on them. +""" +def findXpathPortLeaf(self, portName): + + try: + table = "PORT" + xpath = self.findXpathPort(portName) + module, topc, container = self.get_module_TLC_container(table) + list = self.findYangList(container, table+"_LIST") + xpath = xpath + "/" + list['key']['@value'].split()[0] + except Exception as e: + print("find xpath of port Leaf failed") + raise e + + return xpath + + +""" +Find xpath of PORT +""" +def findXpathPort(self, portName): + + try: + table = "PORT" + module, topc, container = self.get_module_TLC_container(table) + xpath = "/" + module + ":" + topc + "/" + table + + list = self.findYangList(container, table+"_LIST") + xpath = self.findXpathList(xpath, list, [portName]) + except Exception as e: + print("find xpath of port failed") + raise e + + return xpath + +""" +Find xpath of a YANG LIST from keys, +xpath: xpath till list +list: YANG List +keys: list of keys in YANG LIST +""" +def findXpathList(self, xpath, list, keys): + + try: + # add list name in xpath + xpath = xpath + "/" + list['@name'] + listKeys = list['key']['@value'].split() + i = 0; + for listKey in listKeys: + xpath = xpath + '['+listKey+'=\''+keys[i]+'\']' + i = i + 1 + except Exception as e: + print("find xpath of list failed") + raise e + + return xpath + +""" +load_data: load Config DB, crop, xlate and create data tree from it. +input: data +returns: True - success False - failed +""" +def load_data(self, configdbJson): + + try: + self.jIn = configdbJson + # reset xlate + self.xlateJson = dict() + # self.jIn will be cropped + self.cropConfigDB("cropped.json") + # xlated result will be in self.xlateJson + self.xlateConfigDB() + #print(self.xlateJson) + self.root = self.ctx.parse_data_mem(dumps(self.xlateJson), \ + ly.LYD_JSON, ly.LYD_OPT_CONFIG|ly.LYD_OPT_STRICT) + + except Exception as e: + self.root = None + print("Data Loading Failed") + raise e + + return True + +""" +Get data from Data tree, data tree will be assigned in self.xlateJson +""" +def get_data(self): + + try: + self.xlateJson = loads(self.print_data_mem('JSON')) + # reset reverse xlate + self.revXlateJson = dict() + # result will be stored self.revXlateJson + self.revXlateConfigDB() + + except Exception as e: + print("Get Data Tree Failed") + raise e + + return self.revXlateJson + +""" +Delete a node from data tree, if this is LEAF and KEY Delete the Parent +""" +def delete_node(self, xpath): + + # These MACROS used only here, can we get it from Libyang Header ? + LYS_LEAF = 4 + node = self.find_data_node(xpath) + if node is None: + raise('Node {} not found'.format(xpath)) + + snode = node.schema() + # check for a leaf if it is a key. If yes delete the parent + if (snode.nodetype() == LYS_LEAF): + leaf = ly.Schema_Node_Leaf(snode) + if leaf.is_key(): + # try to delete parent + nodeP = self.find_parent_node(xpath) + xpathP = nodeP.path() + return self._delete_node(xpath=xpathP, node=nodeP) + else: + return self._delete_node(xpath=xpath, node=node) + + return True + +# End of class sonic_yang diff --git a/src/sonic-yang-mgmt/setup.py b/src/sonic-yang-mgmt/setup.py new file mode 100644 index 000000000000..f12f7177c2af --- /dev/null +++ b/src/sonic-yang-mgmt/setup.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""The setup script.""" + +from setuptools import setup, find_packages +from setuptools.command.build_py import build_py +from os import system +from sys import exit +import pytest + +# important reuirements parameters +build_requirements = ['../../target/debs/stretch/libyang_1.0.73_amd64.deb', + '../../target/debs/stretch/libyang-cpp_1.0.73_amd64.deb', + '../../target/debs/stretch/python2-yang_1.0.73_amd64.deb'] + +install_requirements = [] + +setup_requirements = ['pytest-runner',] + +test_requirements = ['pytest>=3',] + +# read me +with open('README.rst') as readme_file: + readme = readme_file.read() + +# class for prerequisites to build this package +class pkgBuild(build_py): + """Custom Build PLY""" + + def run (self): + # json file for YANG model test cases. + test_yangJson_file = './tests/yang-model-tests/yangTest.json' + # YANG models are in below dir + yang_model_dir = './yang-models/' + # yang model tester python module + yang_test_py = './tests/yang-model-tests/yangModelTesting.py' + # install libyang, it will be used for testing a build time + for req in build_requirements: + if 'target/debs'in req: + pkg_install_cmd = "sudo dpkg -i {}".format(req) + if (system(pkg_install_cmd)): + print("{} installed failed".format(req)) + exit(1) + else: + print("{} installed".format(req)) + + # run tests for SONiC yang models + test_yang_cmd = "python {} -f {} -y {}".format(yang_test_py, test_yangJson_file, yang_model_dir) + if (system(test_yang_cmd)): + print("YANG Tests failed\n") + exit(1) + else: + print("YANG Tests passed\n") + + # run pytest for libyang python APIs, PLY tests runs on sample yang models + self.pytest_args = [] + errno = pytest.main(self.pytest_args) + if (errno): + exit(errno) + + # Continue usual build steps + build_py.run(self) + +setup( + cmdclass={ + 'build_py': pkgBuild, + }, + author="lnos-coders", + author_email='lnos-coders@linkedin.com', + python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', + 'Natural Language :: English', + "Programming Language :: Python :: 2", + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + ], + description="Package contains YANG models for sonic.", + install_requires=install_requirements, + tests_require = test_requirements, + license="GNU General Public License v3", + long_description=readme + '\n\n', + include_package_data=True, + keywords='sonic_yang_mgmt', + name='sonic_yang_mgmt', + py_modules=['sonic_yang', '_sonic_yang_ext'], + packages=find_packages(), + setup_requires=setup_requirements, + version='1.0', + data_files=[ + ('yang-models', ['./yang-models/sonic-head.yang', + './yang-models/sonic-acl.yang', + './yang-models/sonic-interface.yang', + './yang-models/sonic-port.yang', + './yang-models/sonic-portchannel.yang', + './yang-models/sonic-vlan.yang']), + ], + zip_safe=False, +) diff --git a/src/sonic-yang-mgmt/sonic_yang.py b/src/sonic-yang-mgmt/sonic_yang.py new file mode 100644 index 000000000000..5e52a16c2be1 --- /dev/null +++ b/src/sonic-yang-mgmt/sonic_yang.py @@ -0,0 +1,513 @@ +import yang as ly +from json import dump +from glob import glob + +""" +Yang schema and data tree python APIs based on libyang python +""" +class sonic_yang: + def __init__(self, yang_dir): + self.yang_dir = yang_dir + self.ctx = None + self.module = None + self.root = None + + # yang model files, need this map it to module + self.yangFiles = list() + # map from TABLE in config DB to container and module + self.confDbYangMap = dict() + # JSON format of yang model [similar to pyang conversion] + self.yJson = list() + # config DB json input, will be cropped as yang models + self.jIn = dict() + # YANG JSON, this is traslated from config DB json + self.xlateJson = dict() + # reverse translation from yang JSON, == config db json + self.revXlateJson = dict() + + try: + self.ctx = ly.Context(yang_dir) + except Exception as e: + self.fail(e) + + def fail(self, e): + print(e) + raise e + + """ + import all function from extension file + """ + from _sonic_yang_ext import * + + """ + load_schema_module(): load a Yang model file + input: yang_file - full path of a Yang model file + returns: Exception if error + """ + def load_schema_module(self, yang_file): + try: + self.ctx.parse_module_path(yang_file, ly.LYS_IN_YANG) + except Exception as e: + print("Failed to load yang module file: " + yang_file) + self.fail(e) + + """ + load_schema_module_list(): load all Yang model files in the list + input: yang_files - a list of Yang model file full path + returns: Exception if error + """ + def load_schema_module_list(self, yang_files): + for file in yang_files: + try: + self.load_schema_module(file) + except Exception as e: + self.fail(e) + + """ + load_schema_modules(): load all Yang model files in the directory + input: yang_dir - the directory of the yang model files to be loaded + returns: Exception if error + """ + def load_schema_modules(self, yang_dir): + py = glob(yang_dir+"/*.yang") + for file in py: + try: + self.load_schema_module(file) + except Exception as e: + self.fail(e) + + """ + load_schema_modules_ctx(): load all Yang model files in the directory to context: ctx + input: yang_dir, context + returns: Exception if error, returrns context object if no error + """ + def load_schema_modules_ctx(self, yang_dir=None): + if not yang_dir: + yang_dir = self.yang_dir + + ctx = ly.Context(yang_dir) + + py = glob(yang_dir+"/*.yang") + for file in py: + try: + ctx.parse_module_path(str(file), ly.LYS_IN_YANG) + except Exception as e: + print("Failed to parse yang module file: " + file) + self.fail(e) + + return ctx + + """ + load_data_file(): load a Yang data json file + input: data_file - the full path of the yang json data file to be loaded + returns: Exception if error + """ + def load_data_file(self, data_file): + try: + node = self.ctx.parse_data_path(data_file, ly.LYD_JSON, ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT) + except Exception as e: + print("Failed to load data file: " + str(data_file)) + self.fail(e) + else: + self.root = node + + """ + get module name from xpath + input: path + returns: module name + """ + def get_module_name(self, schema_xpath): + module_name = schema_xpath.split(':')[0].strip('/') + return module_name + + """ + get_module(): get module object from Yang module name + input: yang module name + returns: Schema_Node object + """ + def get_module(self, module_name): + mod = self.ctx.get_module(module_name) + return mod + + """ + load_data_model(): load both Yang module fileis and data json files + input: yang directory, list of yang files and list of data files (full path) + returns: returns (context, root) if no error, or Exception if failed + """ + def load_data_model (self, yang_dir, yang_files, data_files, output=None): + if (self.ctx is None): + self.ctx = ly.Context(yang_dir) + + try: + self.load_schema_module_list(yang_files) + if len(data_files) == 0: + return (self.ctx, self.root) + + self.load_data_file(data_files[0]) + + for i in range(2, len(data_files)): + self.merge_data(data_files[i]) + except Exception as e: + print("Failed to load data files") + self.fail(e) + return + + if output is not None: + self.print_data_mem(output) + + return (self.ctx, self.root) + + """ + print_data_mem(): print the data tree + input: option: "JSON" or "XML" + """ + def print_data_mem (self, option): + if (option == "JSON"): + mem = self.root.print_mem(ly.LYD_JSON, ly.LYP_WITHSIBLINGS | ly.LYP_FORMAT) + else: + mem = self.root.print_mem(ly.LYD_XML, ly.LYP_WITHSIBLINGS | ly.LYP_FORMAT) + + return mem + + """ + save_data_file_json(): save the data tree in memory into json file + input: outfile - full path of the file to save the data tree to + """ + def save_data_file_json(self, outfile): + mem = self.root.print_mem(ly.LYD_JSON, ly.LYP_FORMAT) + with open(outfile, 'w') as out: + dump(mem, out, indent=4) + + """ + get_module_tree(): get yang module tree in JSON or XMAL format + input: module name + returns: JSON or XML format of the input yang module schema tree + """ + def get_module_tree(self, module_name, format): + result = None + + try: + module = self.ctx.get_module(str(module_name)) + except Exception as e: + print("Cound not get module: " + str(module_name)) + self.fail(e) + else: + if (module is not None): + if (format == "XML"): + #libyang bug with format + result = module.print_mem(ly.LYD_JSON, ly.LYP_FORMAT) + else: + result = module.print_mem(ly.LYD_XML, ly.LYP_FORMAT) + + return result + + """ + validate_data(): validate data tree + input: + node: root of the data tree + ctx: context + returns: Exception if failed + """ + def validate_data (self, node=None, ctx=None): + if not node: + node = self.root + + if not ctx: + ctx = self.ctx + + try: + rc = node.validate(ly.LYD_OPT_CONFIG, ctx) + except Exception as e: + self.fail(e) + + """ + validate_data_tree(): validate the data tree + returns: Exception if failed + """ + def validate_data_tree (self): + try: + self.validate_data(self.root, self.ctx) + except Exception as e: + print("Failed to validate data tree") + self.fail(e) + + """ + find_parent_node(): find the parent node object + input: data_xpath - xpath of the data node + returns: parent node + """ + def find_parent_node (self, data_xpath): + if (self.root is None): + print("data not loaded") + return None + try: + node = self.find_data_node(data_xpath) + except Exception as e: + print("Failed to find data node from xpath: " + str(data_xpath)) + self.fail(e) + else: + if node is not None: + return node.parent() + + return None + + """ + get_parent_xpath(): find the parent node xpath + input: data_xpath - xpathof the data node + returns: - xpath of parent node + - Exception if error + """ + def get_parent_xpath (self, data_xpath): + path="" + try: + node = self.find_parent_node(data_xpath) + except Exception as e: + print("Failed to find parent node from xpath: " + str(data_xpath)) + self.fail(e) + else: + if (node is not None): + path = node.path() + return path + + """ + new_node(): create a new data node in the data tree + input: + xpath: xpath of the new node + value: value of the new node + returns: new Data_Node object if success, Exception if falied + """ + def new_node(self, xpath, value): + val = str(value) + try: + node = self.root.new_path(self.ctx, xpath, val, 0, 0) + except Exception as e: + print("Failed to add data node for path: " + str(xpath)) + self.fail(e) + else: + return node + + """ + find_data_node(): find the data node from xpath + input: data_xpath: xpath of the data node + returns - Data_Node object if found + - None if not exist + - Exception if there is error + """ + def find_data_node(self, data_xpath): + try: + set = self.root.find_path(data_xpath) + except Exception as e: + print("Failed to find data node from xpath: " + str(data_xpath)) + self.fail(e) + else: + if set is not None: + for node in set.data(): + if (data_xpath == node.path()): + return node + return None + """ + find_schema_node(): find the schema node from schema xpath + example schema xpath: + "/sonic-port:sonic-port/sonic-port:PORT/sonic-port:PORT_LIST/sonic-port:port_name" + input: xpath of the node + returns: Schema_Node oject or None if not found + """ + def find_schema_node(self, schema_xpath): + try: + schema_set = self.ctx.find_path(schema_xpath) + for snode in schema_set.schema(): + if (schema_xpath == snode.path()): + return snode + except Exception as e: + self.fail(e) + return None + else: + for snode in schema_set.schema(): + if schema_xapth == snode.path(): + return snode + return None + """ + find_node_schema_xpath(): find the xpath of the schema node from data xpath + data xpath example: + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet0']/port_name" + input: data_xpath - xpath of the data node + returns: - xpath of the schema node if success + - Exception if error + """ + def find_node_schema_xpath(self, data_xpath): + path = "" + try: + set = self.root.find_path(data_xpath) + except Exception as e: + self.fail(e) + else: + for node in set.data(): + if data_xpath == node.path(): + return node.schema().path() + return path + + """ + add_node(): add a node to Yang schema or data tree + input: xpath and value of the node to be added + returns: Exception if failed + """ + def add_node(self, xpath, value): + try: + node = self.new_node(xpath, value) + #check if the node added to the data tree + self.find_data_node(xpath) + except Exception as e: + print("add_node(): Failed to add data node for xpath: " + str(data_xpath)) + self.fail(e) + + """ + merge_data(): merge a data file to the existing data tree + input: yang model directory and full path of the data json file to be merged + returns: Exception if failed + """ + def merge_data(self, data_file, yang_dir=None): + #load all yang models to ctx + if not yang_dir: + yang_dir = self.yang_dir + + try: + ctx = self.load_schema_modules_ctx(yang_dir) + + #source data node + source_node = ctx.parse_data_path(str(data_file), ly.LYD_JSON, ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT) + + #merge + self.root.merge(source_node, 0) + except Exception as e: + self.fail(e) + + """ + _delete_node(): delete a node from the schema/data tree, internal function + input: xpath of the schema/data node + returns: True - success False - failed + """ + def _delete_node(self, xpath=None, node=None): + if node is None: + node = self.find_data_node(xpath) + + if (node): + node.unlink() + dnode = self.find_data_node(xpath) + if (dnode is None): + #deleted node not found + return True + else: + print('Could not delete Node') + return False + else: + print("failed to find node, xpath: " + xpath) + + return False + + """ + find_node_value(): find the value of a node from the schema/data tree + input: data_xpath of the data node + returns: value string of the node + """ + def find_node_value(self, data_xpath): + output = "" + try: + node = self.find_data_node(data_xpath) + except Exception as e: + print("find_node_value(): Failed to find data node from xpath: {}".format(data_xpath)) + self.fail(e) + else: + if (node is not None): + subtype = node.subtype() + if (subtype is not None): + value = subtype.value_str() + return value + return output + + """ + set the value of a node in the data tree + input: xpath of the data node + returns: Exception if failed + """ + def set_dnode_value(self, data_xpath, value): + try: + node = self.root.new_path(self.ctx, data_xpath, str(value), ly.LYD_ANYDATA_STRING, ly.LYD_PATH_OPT_UPDATE) + except Exception as e: + print("set data node value failed for xpath: " + str(data_xpath)) + self.fail(e) + + """ + find_data_nodes(): find the set of nodes for the xpath + input: xpath of the data node + returns: list of xpath of the dataset + """ + def find_data_nodes(self, data_xpath): + list = [] + node = self.root.child() + try: + node_set = node.find_path(data_xpath); + except Exception as e: + self.fail(e) + else: + if node_set is None: + raise Exception('data node not found') + + for data_set in node_set.data(): + schema = data_set.schema() + list.append(data_set.path()) + return list + + """ + find_schema_dependencies(): find the schema dependencies from schema xpath + input: schema_xpath of the schema node + returns: - list of xpath of the dependencies + - Exception if schema node not found + """ + def find_schema_dependencies (self, schema_xpath): + ref_list = [] + node = self.root + try: + schema_node = self.find_schema_node(schema_xpath) + except Exception as e: + print("Cound not find the schema node from xpath: " + str(schema_xpath)) + self.fail(e) + return ref_list + + snode = ly.Schema_Node_Leaf(schema_node) + backlinks = snode.backlinks() + if backlinks.number() > 0: + for link in backlinks.schema(): + print("backlink schema: {}".format(link.path())) + ref_list.append(link.path()) + return ref_list + + """ + find_data_dependencies(): find the data dependencies from data xpath + input: data_xpath - xpath of data node + returns: - list of xpath + - Exception if error + """ + def find_data_dependencies (self, data_xpath): + ref_list = [] + node = self.root + try: + data_node = self.find_data_node(data_xpath) + except Exception as e: + print("find_data_dependencies(): Failed to find data node from xpath: {}".format(data_xapth)) + self.fail(e) + return ref_list + + value = str(self.find_node_value(data_xpath)) + + schema_node = ly.Schema_Node_Leaf(data_node.schema()) + backlinks = schema_node.backlinks() + if backlinks.number() > 0: + for link in backlinks.schema(): + node_set = node.find_path(link.path()) + for data_set in node_set.data(): + schema = data_set.schema() + casted = data_set.subtype() + if value == casted.value_str(): + ref_list.append(data_set.path()) + + return ref_list diff --git a/src/sonic-yang-mgmt/tests/__init__.py b/src/sonic-yang-mgmt/tests/__init__.py new file mode 100644 index 000000000000..ec983d6590c7 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +"""Unit test package for sonic_yang_mgmt.""" diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-acl.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-acl.yang new file mode 100644 index 000000000000..1aab4d7de0b1 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-acl.yang @@ -0,0 +1,274 @@ +module sonic-acl { + + yang-version 1.1; + + namespace "http://github.com/Azure/sonic-acl"; + prefix acl; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + import sonic-portchannel { + prefix lag; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "ACL YANG Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-acl { + + container ACL_RULE { + + description "ACL_RULE part of config_db.json"; + + list ACL_RULE_LIST { + + key "ACL_TABLE_NAME RULE_NAME"; + + leaf ACL_TABLE_NAME { + type leafref { + path "/acl:sonic-acl/acl:ACL_TABLE/acl:ACL_TABLE_LIST/acl:ACL_TABLE_NAME"; + } + } + + leaf RULE_NAME { + type string { + length 1..255; + } + } + + leaf PACKET_ACTION { + type head:packet_action; + } + + leaf IP_TYPE { + type head:ip_type; + } + + leaf PRIORITY { + type uint32 { + range 0..999999; + } + } + + choice ip_prefix { + + case ip4_prefix { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV4' or .='IPV4ANY' or .='ARP'])"; + leaf SRC_IP { + type inet:ipv4-prefix; + } + + leaf DST_IP { + type inet:ipv4-prefix; + } + } + + case ip6_prefix { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV6' or .='IPV6ANY'])"; + leaf SRC_IPV6 { + type inet:ipv6-prefix; + } + + leaf DST_IPV6 { + type inet:ipv6-prefix; + } + } + } + + leaf-list IN_PORTS { + /* Values in leaf list are UNIQUE */ + type uint16; + } + + leaf-list OUT_PORTS { + /* Values in leaf list are UNIQUE */ + type uint16; + } + + choice src_port { + case l4_src_port { + leaf L4_SRC_PORT { + type uint16; + } + } + + case l4_src_port_range { + leaf L4_SRC_PORT_RANGE { + type string { + pattern '([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])-([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])'; + } + } + } + } + + choice dst_port { + case l4_dst_port { + leaf L4_DST_PORT { + type uint16; + } + } + + case l4_dst_port_range { + leaf L4_DST_PORT_RANGE { + type string { + pattern '([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])-([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])'; + } + } + } + } + + leaf ETHER_TYPE { + type string { + pattern "(0x88CC|0x8100|0x8915|0x0806|0x0800|0x86DD|0x8847)"; + } + } + + leaf IP_PROTOCOL { + type uint8 { + range 1..143; + } + } + + leaf TCP_FLAGS { + type string { + pattern '0[x][0-9a-fA-F]{1,2}|0[X][0-9a-fA-F]{1,2}'; + } + } + + leaf DSCP { + type uint8; + } + + leaf TC { + type uint8; + } + + choice icmp { + + case icmp4 { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV4' or .='IPV4ANY' or .='ARP'])"; + leaf ICMP_TYPE { + type uint8 { + range 1..44; + } + } + + leaf ICMP_CODE { + type uint8 { + range 1..16; + } + } + } + + case icmp6 { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV6' or .='IPV6ANY'])"; + leaf ICMPV6_TYPE { + type uint8 { + range 1..44; + } + } + + leaf ICMPV6_CODE { + type uint8 { + range 1..16; + } + } + } + } + + leaf INNER_ETHER_TYPE { + type string { + pattern "(0x88CC|0x8100|0x8915|0x0806|0x0800|0x86DD|0x8847)"; + } + } + + leaf INNER_IP_PROTOCOL { + type uint8 { + range 1..143; + } + } + + leaf INNER_L4_SRC_PORT { + type uint16; + } + + leaf INNER_L4_DST_PORT { + type uint16; + } + } + /* end of ACL_RULE_LIST */ + } + /* end of container ACL_RULE */ + + container ACL_TABLE { + + description "ACL_TABLE part of config_db.json"; + + list ACL_TABLE_LIST { + + key "ACL_TABLE_NAME"; + + leaf ACL_TABLE_NAME { + type string; + } + + leaf policy_desc { + type string { + length 1..255; + } + } + + leaf type { + type head:acl_table_type; + } + + leaf stage { + type enumeration { + enum INGRESS; + enum EGRESS; + } + } + + leaf-list ports { + /* union of leafref is allowed in YANG 1.1 */ + type union { + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + type leafref { + path /lag:sonic-portchannel/lag:PORTCHANNEL/lag:PORTCHANNEL_LIST/lag:portchannel_name; + } + } + } + } + /* end of ACL_TABLE_LIST */ + } + /* end of container ACL_TABLE */ + } + /* end of container sonic-acl */ +} +/* end of module sonic-acl */ diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-head.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-head.yang new file mode 100644 index 000000000000..dd0da92c7152 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-head.yang @@ -0,0 +1,72 @@ +module sonic-head { + + namespace "http://sonic-head"; + prefix sonic-head; + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "Head yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + typedef ip-family { + type enumeration { + enum IPv4; + enum IPv6; + } + } + + typedef admin_status { + type enumeration { + enum up; + enum down; + } + } + + typedef packet_action{ + type enumeration { + enum DROP; + enum FORWARD; + enum REDIRECT; + } + } + + typedef ip_type { + type enumeration { + enum ANY; + enum IP; + enum NON_IP; + enum IPV4; + enum IPV6; + enum IPV4ANY; + enum NON_IPv4; + enum IPV6ANY; + enum NON_IPv6; + enum ARP; + } + } + + typedef acl_table_type { + type enumeration { + enum L2; + enum L3; + enum L3V6; + enum MIRROR; + enum MIRRORV6; + enum MIRROR_DSCP; + enum CTRLPLANE; + } + } + + typedef vlan_tagging_mode { + type enumeration { + enum tagged; + enum untagged; + enum priority_tagged; + } + } +} \ No newline at end of file diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-interface.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-interface.yang new file mode 100644 index 000000000000..dc8bc7389ea1 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-interface.yang @@ -0,0 +1,78 @@ +module sonic-interface { + + namespace "http://github.com/Azure/sonic-interface"; + prefix intf; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "INTERFACE yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-interface { + container INTERFACE { + + description "INTERFACE part of config_db.json"; + + list INTERFACE_LIST { + + key "interface ip-prefix"; + + leaf interface { + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf ip-prefix { + type inet:ip-prefix; + } + + leaf scope { + type enumeration { + enum global; + enum local; + } + } + + leaf family { + + /* family leaf needed for backward compatibility + Both ip4 and ip6 address are string in IETF RFC 6021, + so must statement can check based on : or ., family + should be IPv4 or IPv6 according. + */ + + must "(contains(../ip-prefix, ':') and current()='IPv6') or + (contains(../ip-prefix, '.') and current()='IPv4')"; + type head:ip-family; + } + } + /* end of INTERFACE_LIST */ + + } + /* end of INTERFACE container */ + } +} diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-module.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-module.yang new file mode 100644 index 000000000000..687bb478aacb --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-module.yang @@ -0,0 +1,144 @@ +module sonic-module { + yang-version 1.1; + namespace "urn:ietf:params:xml:ns:yang:sonic-module"; + + prefix tm; + + import ietf-inet-types { + prefix inet; + } + + organization "organization"; + description + "example yang module"; + contact + "example@example.org"; + + container ports { + list port { + config true; + key "name"; + + leaf name { + type string; + } + + leaf lanes { + //choice + type uint8; + } + +/* + leaf fec { + type string; + } + + leaf mtu { + type uint16; + description + "Set the max transmission unit size in octets + for the physical interface. If this is not set, the mtu is + set to the operational default -- e.g., 1514 bytes on an + Ethernet interface."; + } + leaf admin_status { + type enumeration { + enum UP { + description + "Ready to pass packets."; + } + enum DOWN { + description + "Not ready to pass packets and not in some test mode."; + } + enum TESTING { + //TODO: This is generally not supported as a configured + //admin state, though it's in the standard interfaces MIB. + //Consider removing it. + description + "In some test mode."; + } + } + } +*/ + leaf alias { + type string; + } + + leaf speed { + type string; + units "bits/second"; +/* + description + "An estimate of the interface's current bandwidth in bits + per second. For interfaces that do not vary in + bandwidth or for those where no accurate estimation can + be made, this node should contain the nominal bandwidth. + For interfaces that have no concept of bandwidth, this + node is not present."; + reference + "RFC 2863: The Interfaces Group MIB - + ifSpeed, ifHighSpeed"; +*/ + } + + } + } + + container vlans { + list vlan { + config true; + + key "name"; + + leaf name { + type string; + } + + leaf vlanid { + type string; + } + + leaf admin_status { + type string; +/* + type enumeration { + enum UP { + description + "Ready to pass packets."; + } + enum DOWN { + description + "Not ready to pass packets and not in some test mode."; + } + enum TESTING { + //TODO: This is generally not supported as a configured + //admin state, though it's in the standard interfaces MIB. + //Consider removing it. + description + "In some test mode."; + } + } +*/ + } + + leaf description { + type string; + } + + leaf mtu { + type uint16; + description + "Set the max transmission unit size in octets + for the physical interface. If this is not set, the mtu is + set to the operational default -- e.g., 1514 bytes on an + Ethernet interface."; + } + leaf-list members { + type leafref { + path "../../../ports/port/name"; + } + } + } + } +} diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-port.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-port.yang new file mode 100644 index 000000000000..be04a8aea4bd --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-port.yang @@ -0,0 +1,84 @@ +module sonic-port{ + + namespace "http://github.com/Azure/sonic-port"; + prefix port; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "PORT yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-port{ + container PORT { + + description "PORT part of config_db.json"; + + list PORT_LIST { + + key "port_name"; + + leaf port_name { + type string { + length 1..128; + } + } + + leaf alias { + type string { + length 1..128; + } + } + + leaf lanes { + type string { + length 1..128; + } + } + + leaf description { + type string { + length 1..255; + } + } + + leaf speed { + type uint32 { + range 1..100000; + } + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + } /* end of list PORT_LIST */ + + } /* end of container PORT */ + + } /* end of container sonic-port */ + +} /* end of module sonic-port */ diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-portchannel.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-portchannel.yang new file mode 100644 index 000000000000..656351f4e635 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-portchannel.yang @@ -0,0 +1,85 @@ +module sonic-portchannel { + + namespace "http://github.com/Azure/sonic-portchannel"; + prefix lag; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "PORTCHANNEL yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-portchannel { + container PORTCHANNEL { + + description "PORTCHANNEL part of config_db.json"; + + list PORTCHANNEL_LIST { + + key "portchannel_name"; + + leaf portchannel_name { + type string { + length 1..128; + pattern 'PortChannel[0-9]{1,4}'; + } + } + + leaf-list members { + /* leaf-list members are unique by default */ + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf min_links { + type uint8 { + range 1..128; + } + } + + leaf description { + type string { + length 1..255; + } + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + } /* end of list PORTCHANNEL_LIST */ + + } /* end of container PORTCHANNEL */ + + } /* end of container sonic-portchannel */ + +} /* end of module sonic-port */ diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-vlan.yang b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-vlan.yang new file mode 100644 index 000000000000..6058ce3f1bfc --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/sonic-vlan.yang @@ -0,0 +1,158 @@ +module sonic-vlan { + + namespace "http://github.com/Azure/sonic-vlan"; + prefix vlan; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "VLAN yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-vlan { + container VLAN_INTERFACE { + + description "VLAN_INTERFACE part of config_db.json"; + + list VLAN_INTERFACE_LIST { + + key "vlanid ip-prefix"; + + leaf vlanid { + type leafref { + path ../../../VLAN/VLAN_LIST/vlanid; + } + } + + leaf ip-prefix { + mandatory true; + type inet:ip-prefix; + } + + leaf scope { + type enumeration { + enum global; + enum local; + } + } + + leaf family { + + /* family leaf needed for backward compatibility + Both ip4 and ip6 address are string in IETF RFC 6021, + so must statement can check based on : or ., family + should be IPv4 or IPv6 according. + */ + + must "(contains(../ip-prefix, ':') and current()='IPv6') or + (contains(../ip-prefix, '.') and current()='IPv4')"; + type head:ip-family; + } + } + /* end of VLAN_INTERFACE_LIST */ + } + /* end of VLAN_INTERFACE container */ + + container VLAN { + + description "VLAN part of config_db.json"; + + list VLAN_LIST { + + key "vlanid"; + + leaf vlanid { + type uint16 { + range 1..4094; + } + } + + leaf description { + type string { + length 1..255; + } + } + + leaf-list dhcp_servers { + type inet:ip-address; + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + + leaf-list members { + /* leaf-list members are unique by default */ + + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + } + /* end of VLAN_LIST */ + } + /* end of container VLAN */ + + container VLAN_MEMBER { + + description "VLAN_MEMBER part of config_db.json"; + + list VLAN_MEMBER_LIST { + + key "vlanid port"; + + leaf vlanid { + type leafref { + path ../../../VLAN/VLAN_LIST/vlanid; + } + } + + leaf port { + /* key elements are mandatory by default */ + mandatory true; + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf tagging_mode { + mandatory true; + type head:vlan_tagging_mode; + } + } + /* end of list VLAN_MEMBER_LIST */ + } + /* end of container VLAN_MEMBER */ + } + /* end of container sonic-vlan */ +} +/* end of module sonic-vlan */ diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data.json b/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data.json new file mode 100644 index 000000000000..f7de6902c5d0 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data.json @@ -0,0 +1,276 @@ +{ + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_INTERFACE": { + "VLAN_INTERFACE_LIST": [{ + "vlanid": 111, + "ip-prefix": "2000:f500:45:6709::1/64", + "scope": "global", + "family": "IPv6" + }, + { + "vlanid": 111, + "ip-prefix": "10.1.1.65/26", + "scope": "global", + "family": "IPv4" + }, + { + "vlanid": 111, + "ip-prefix": "fe80::1/10", + "scope": "local", + "family": "IPv6" + }, + { + "vlanid": 555, + "ip-prefix": "2000:f500:41:4e9::1/64", + "scope": "global", + "family": "IPv6" + }, + { + "vlanid": 555, + "ip-prefix": "10.1.5.65/26", + "scope": "global", + "family": "IPv4" + }, + { + "vlanid": 555, + "ip-prefix": "fe80::1/10", + "scope": "local", + "family": "IPv6" + } + ] + }, + + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlanid": 111, + "description": "server_vlan", + "dhcp_servers": [ + "10.1.7.116" + ], + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet8", + "Ethernet3", + "Ethernet0", + "Ethernet1", + "Ethernet6", + "Ethernet4", + "Ethernet5", + "Ethernet9", + "Ethernet2" + ] + }, + { + "vlanid": 555, + "description": "ipmi_vlan", + "dhcp_servers": [ + "10.1.7.116" + ], + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet9", + "Ethernet2", + "Ethernet8" + ] + } + ] + }, + + "sonic-vlan:VLAN_MEMBER": { + "VLAN_MEMBER_LIST": [{ + "vlanid": 111, + "port": "Ethernet0", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet1", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet2", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet3", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet4", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet5", + "tagging_mode": "tagged" + }, + { + "vlanid": 111, + "port": "Ethernet6", + "tagging_mode": "tagged" + } + ] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet2", + "alias": "eth2", + "description": "Ethernet2", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet3", + "alias": "eth2", + "description": "Ethernet3", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet4", + "alias": "eth4", + "description": "Ethernet4", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet5", + "alias": "eth5", + "description": "Ethernet5", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet6", + "alias": "eth6", + "description": "Ethernet6", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet7", + "alias": "eth7", + "description": "Ethernet7", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet8", + "alias": "eth8", + "description": "Ethernet8", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet9", + "alias": "eth9", + "description": "Ethernet9", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + }, + + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "PACL-V4", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.1.72.0/26", + "SRC_IP": "10.1.0.0/15", + "PRIORITY": "999980", + "IP_TYPE": "IPV4ANY" + }, + { + "ACL_TABLE_NAME": "PACL-V4", + "RULE_NAME": "Rule_40", + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.1.72.64/26", + "SRC_IP": "10.1.0.0/15", + "PRIORITY": "999960", + "IP_TYPE": "IPV4ANY", + "INNER_ETHER_TYPE": "0x88CC" + }, + { + "ACL_TABLE_NAME": "PACL-V6", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IP", + "SRC_IPV6": "2000:f500:41::/48", + "PRIORITY": "999980", + "DST_IPV6": "2000:f500:43:320::/64", + "L4_SRC_PORT_RANGE": "653-1053" + } + ] + }, + + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "PACL-V6", + "policy_desc": "Filter IPv6", + "type": "L3V6", + "stage": "EGRESS", + "ports": ["Ethernet7", "Ethernet9", "Ethernet8"] + }, + { + "ACL_TABLE_NAME": "PACL-V4", + "policy_desc": "Filter IPv6", + "type": "L3", + "stage": "INGRESS", + "ports": ["Ethernet2", "Ethernet0", "Ethernet1"] + } + ] + } + }, + + "sonic-interface:sonic-interface": { + "sonic-interface:INTERFACE": { + "INTERFACE_LIST": [{ + "interface": "Ethernet8", + "ip-prefix": "10.1.1.65/26", + "scope": "global", + "family": "IPv4" + }, + { + "interface": "Ethernet8", + "ip-prefix": "2000:f500:40:a749::2/126", + "scope": "global", + "family": "IPv6" + } + ] + } + } +} diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data_merge.json b/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data_merge.json new file mode 100644 index 000000000000..3bb4f0f5954b --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data_merge.json @@ -0,0 +1,177 @@ +{ + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_INTERFACE": { + "VLAN_INTERFACE_LIST": [{ + "vlanid": 111, + "ip-prefix": "2000:f500:45:6709::1/64", + "scope": "global", + "family": "IPv6" + }, + { + "vlanid": 111, + "ip-prefix": "10.1.1.64/26", + "scope": "global", + "family": "IPv4" + }, + { + "vlanid": 200, + "ip-prefix": "2000:f500:45:6708::1/64", + "scope": "global", + "family": "IPv6" + }, + { + "vlanid": 200, + "ip-prefix": "2000:f500:45:6709::1/64", + "scope": "global", + "family": "IPv6" + } + ] + }, + + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlanid": 200, + "description": "server_vlan", + "dhcp_servers": [ + "10.1.72.116" + ], + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet8", + "Ethernet3", + "Ethernet0", + "Ethernet1", + "Ethernet6", + "Ethernet4", + "Ethernet5", + "Ethernet9", + "Ethernet2" + ] + }, + { + "vlanid": 111, + "description": "server_vlan", + "dhcp_servers": [ + "10.1.72.116" + ], + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet8", + "Ethernet3", + "Ethernet0", + "Ethernet1", + "Ethernet6", + "Ethernet4", + "Ethernet5", + "Ethernet9", + "Ethernet2" + ] + } + ] + }, + + "sonic-vlan:VLAN_MEMBER": { + "VLAN_MEMBER_LIST": [{ + "vlanid": 200, + "port": "Ethernet0", + "tagging_mode": "tagged" + } + ] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet2", + "alias": "eth2", + "description": "Ethernet2", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet3", + "alias": "eth2", + "description": "Ethernet3", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet4", + "alias": "eth4", + "description": "Ethernet4", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet5", + "alias": "eth5", + "description": "Ethernet5", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet6", + "alias": "eth6", + "description": "Ethernet6", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet7", + "alias": "eth7", + "description": "Ethernet7", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet8", + "alias": "eth8", + "description": "Ethernet8", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet9", + "alias": "eth9", + "description": "Ethernet9", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet10", + "alias": "eth10", + "description": "Ethernet10", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } +} diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_SonicYang.json b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_SonicYang.json new file mode 100644 index 000000000000..fb13168a466c --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_SonicYang.json @@ -0,0 +1,103 @@ +{ + "yang_dir":"/sonic/src/sonic-yang-mgmt/tests/libyang-python-tests/sample-yang-models/", + "data_file":"/sonic/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data.json", + "data_merge_file":"/sonic/src/sonic-yang-mgmt/tests/libyang-python-tests/sonic_config_data_merge.json", + "modules":[ + {"file":"sonic-head.yang", "module":"sonic-head"}, + {"file":"sonic-port.yang", "module":"sonic-port"}, + {"file":"sonic-acl.yang", "module":"sonic-acl"}, + {"file":"sonic-interface.yang", "module":"sonic-interface"}, + {"file":"sonic-portchannel.yang", "module":"sonic-portchannel"}, + {"file":"sonic-vlan.yang", "module":"sonic-vlan"} + ], + + "new_nodes":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet12']/alias", "value":"Ethernet10_alias"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet12']/speed", "value":"5000"}, + {"xpath":"/sonic-acl:sonic-acl/ACL_RULE/ACL_RULE_LIST[ACL_TABLE_NAME='PACL-test'][RULE_NAME='rule_20']/RULE_NAME", + "value":"rule_20"} + ], + + "data_nodes":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/alias", "valid":"True"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet20']/alias", "valid":"False"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE", "valid":"True"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST", "valid":"False"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']", "valid":"True"} + ], + + "set_nodes":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet10']/speed", "value":"10000"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/mtu", "value":"1500"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN/VLAN_LIST[vlanid='111']/description", "value":"server_vlan111"} + ], + + "node_values":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/speed", "value":"25000"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']/family", + "value":"IPv6"} + ], + + "schema_nodes":[ + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/family", + "value":"/sonic-vlan:sonic-vlan/sonic-vlan:VLAN_INTERFACE/sonic-vlan:VLAN_INTERFACE_LIST/sonic-vlan:family"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/speed", + "value":"/sonic-port:sonic-port/sonic-port:PORT/sonic-port:PORT_LIST/sonic-port:speed"} + ], + + "delete_nodes":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet10']/speed", "valid":"False"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/mtu", "valid":"True"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet20']/mtu", "valid":"False"} + ], + + "dependencies":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet8']/port_name", + "dependencies": + ["/sonic-vlan:sonic-vlan/VLAN/VLAN_LIST[vlanid='111']/members[.='Ethernet8']", + "/sonic-vlan:sonic-vlan/VLAN/VLAN_LIST[vlanid='555']/members[.='Ethernet8']", + "/sonic-acl:sonic-acl/ACL_TABLE/ACL_TABLE_LIST[ACL_TABLE_NAME='PACL-V6']/ports[.='Ethernet8']", + "/sonic-interface:sonic-interface/INTERFACE/INTERFACE_LIST[interface='Ethernet8'][ip-prefix='10.1.1.64/26']/interface", + "/sonic-interface:sonic-interface/INTERFACE/INTERFACE_LIST[interface='Ethernet8'][ip-prefix='2000:f500:40:a749::/126']/interface"]} + ], + "schema_dependencies":[ + {"xpath":"/sonic-port:sonic-port/sonic-port:PORT/sonic-port:PORT_LIST/sonic-port:port_name", + "schema_dependencies": + ["/sonic-acl:sonic-acl/sonic-acl:ACL_TABLE/sonic-acl:ACL_TABLE_LIST/sonic-acl:ports", + "/sonic-portchannel:sonic-portchannel/sonic-portchannel:PORTCHANNEL/sonic-portchannel:PORTCHANNEL_LIST/sonic-portchannel:members", + "/sonic-interface:sonic-interface/sonic-interface:INTERFACE/sonic-interface:INTERFACE_LIST/sonic-interface:interface", + "/sonic-vlan:sonic-vlan/sonic-vlan:VLAN/sonic-vlan:VLAN_LIST/sonic-vlan:members", + "/sonic-vlan:sonic-vlan/sonic-vlan:VLAN_MEMBER/sonic-vlan:VLAN_MEMBER_LIST/sonic-vlan:port"]} + ], + "members":[ + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST", + "members": + ["/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet0']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet1']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet2']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet3']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet4']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet5']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet6']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet7']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet8']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet10']", + "/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet12']"]} + ], + + "parents":[ + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']/family", + "parent":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='2000:f500:45:6709::/64']"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/scope", + "parent":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/vlanid", + "parent":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/ip-prefix", + "parent":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']"}, + {"xpath":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']/family", + "parent":"/sonic-vlan:sonic-vlan/VLAN_INTERFACE/VLAN_INTERFACE_LIST[vlanid='111'][ip-prefix='10.1.1.64/26']"}, + {"xpath":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']/speed", + "parent":"/sonic-port:sonic-port/PORT/PORT_LIST[port_name='Ethernet9']"} + ] +} diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py new file mode 100644 index 000000000000..dc7fc268179a --- /dev/null +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py @@ -0,0 +1,246 @@ +import sys +import os +import pytest +import yang as ly +import sonic_yang as sy +import json +import getopt +import subprocess +import glob +from ijson import items as ijson_itmes + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +sys.path.insert(0, modules_path) + +class Test_SonicYang(object): + # class vars + yang_test_file = "/sonic/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json" + + @pytest.fixture(autouse=True, scope='class') + def data(self): + test_file = "/sonic/src/sonic-yang-mgmt/tests/libyang-python-tests/test_SonicYang.json" + data = self.jsonTestParser(test_file) + return data + + @pytest.fixture(autouse=True, scope='class') + def yang_s(self, data): + yang_dir = str(data['yang_dir']) + data_file = str(data['data_file']) + yang_s = sy.sonic_yang(yang_dir) + return yang_s + + def jsonTestParser(self, file): + """ + Open the json test file + """ + with open(file) as data_file: + data = json.load(data_file) + return data + + """ + Get the JSON input based on func name + and return jsonInput + """ + def readIjsonInput(self, test): + try: + # load test specific Dictionary, using Key = func + # this is to avoid loading very large JSON in memory + print(" Read JSON Section: " + test) + jInput = "" + with open(self.yang_test_file, 'rb') as f: + jInst = ijson_itmes(f, test) + for it in jInst: + jInput = jInput + json.dumps(it) + except Exception as e: + print("Reading Ijson failed") + raise(e) + return jInput + + def setup_class(cls): + pass + + def load_yang_model_file(self, yang_s, yang_dir, yang_file, module_name): + yfile = yang_dir + yang_file + try: + yang_s.load_schema_module(str(yfile)) + except Exception as e: + print(e) + raise + + #test load and get yang module + def test_load_yang_model_files(self, data, yang_s): + yang_dir = data['yang_dir'] + for module in data['modules']: + file = str(module['file']) + module = str(module['module']) + + self.load_yang_model_file(yang_s, yang_dir, file, module) + assert yang_s.get_module(module) is not None + + #test load non-exist yang module file + def test_load_invalid_model_files(self, data, yang_s): + yang_dir = data['yang_dir'] + file = "invalid.yang" + module = "invalid" + + with pytest.raises(Exception): + assert self.load_yang_model_file(yang_s, yang_dir, file, module) + + #test load yang modules in directory + def test_load_yang_model_dir(self, data, yang_s): + yang_dir = data['yang_dir'] + yang_s.load_schema_modules(str(yang_dir)) + + for module_name in data['modules']: + assert yang_s.get_module(str(module_name['module'])) is not None + + #test load yang modules and data files + def test_load_yang_model_data(self, data, yang_s): + yang_dir = str(data['yang_dir']) + yang_files = glob.glob(yang_dir+"/*.yang") + data_file = str(data['data_file']) + data_merge_file = str(data['data_merge_file']) + + data_files = [] + data_files.append(data_file) + data_files.append(data_merge_file) + print(yang_files) + yang_s.load_data_model(yang_dir, yang_files, data_files) + + #test load data file + def test_load_data_file(self, data, yang_s): + data_file = str(data['data_file']) + yang_s.load_data_file(data_file) + + #test_validate_data_tree(): + def test_validate_data_tree(self, data, yang_s): + yang_s.validate_data_tree() + + #test find node + def test_find_node(self, data, yang_s): + for node in data['data_nodes']: + expected = node['valid'] + xpath = str(node['xpath']) + dnode = yang_s.find_data_node(xpath) + + if(expected == "True"): + assert dnode is not None + assert dnode.path() == xpath + else: + assert dnode == None + + #test add node + def test_add_node(self, data, yang_s): + for node in data['new_nodes']: + xpath = str(node['xpath']) + value = node['value'] + status = yang_s.add_node(xpath, str(value)) + + node = yang_s.find_data_node(xpath) + assert node is not None + + #test find node value + def test_find_node_value(self, data, yang_s): + for node in data['node_values']: + xpath = str(node['xpath']) + value = str(node['value']) + print(xpath) + print(value) + val = yang_s.find_node_value(xpath) + assert str(val) == str(value) + + #test delete data node + def test_delete_node(self, data, yang_s): + for node in data['delete_nodes']: + expected = node['valid'] + xpath = str(node['xpath']) + yang_s._delete_node(xpath) + + #test set node's value + def test_set_datanode_value(self, data, yang_s): + for node in data['set_nodes']: + xpath = str(node['xpath']) + value = node['value'] + yang_s.set_dnode_value(xpath, value) + + val = yang_s.find_node_value(xpath) + assert str(val) == str(value) + + #test list of members + def test_find_members(self, yang_s, data): + for node in data['members']: + members = node['members'] + xpath = str(node['xpath']) + list = yang_s.find_data_nodes(xpath) + assert list.sort() == members.sort() + + #get parent xpath + def test_get_parent_xpath(self, yang_s, data): + for node in data['parents']: + xpath = str(node['xpath']) + expected_xpath = str(node['parent']) + path = yang_s.get_parent_xpath(xpath) + assert path == expected_xpath + + #test find_node_schema_xpath + def test_find_node_schema_xpath(self, yang_s, data): + for node in data['schema_nodes']: + xpath = str(node['xpath']) + schema_xpath = str(node['value']) + path = yang_s.find_node_schema_xpath(xpath) + assert path == schema_xpath + + #test data dependencies + def test_find_data_dependencies(self, yang_s, data): + for node in data['dependencies']: + xpath = str(node['xpath']) + list = node['dependencies'] + depend = yang_s.find_data_dependencies(xpath) + assert set(depend) == set(list) + + #test data dependencies + def test_find_schema_dependencies(self, yang_s, data): + for node in data['schema_dependencies']: + xpath = str(node['xpath']) + list = node['schema_dependencies'] + depend = yang_s.find_schema_dependencies(xpath) + assert set(depend) == set(list) + + #test merge data tree + def test_merge_data_tree(self, data, yang_s): + data_merge_file = data['data_merge_file'] + yang_dir = str(data['yang_dir']) + yang_s.merge_data(data_merge_file, yang_dir) + #yang_s.root.print_mem(ly.LYD_JSON, ly.LYP_FORMAT) + + def test_xlate_rev_xlate(self): + # This Test is with Sonic YANG model, so create class from start + # read the config + yang_dir = "/sonic/src/sonic-yang-mgmt/yang-models/" + jIn = self.readIjsonInput('SAMPLE_CONFIG_DB_JSON') + # load yang models + syc = sy.sonic_yang(yang_dir) + + syc.loadYangModel() + + syc.load_data(json.loads(jIn)) + + syc.get_data() + + if syc.jIn == syc.revXlateJson: + print("Xlate and Rev Xlate Passed") + else: + # Right now, interface and vlan_interface will have default diff due to ip_prefix + from jsondiff import diff + configDiff = diff(syc.jIn, syc.revXlateJson, syntax='symmetric') + for key in configDiff.keys(): + if 'INTERFACE' not in key: + print("Xlate and Rev Xlate failed") + sys.exit(1) + print("Xlate and Rev Xlate Passed") + + return + + def teardown_class(cls): + pass diff --git a/src/sonic-yang-mgmt/tests/test_sonic_yang_mgmt.py b/src/sonic-yang-mgmt/tests/test_sonic_yang_mgmt.py new file mode 100644 index 000000000000..5e7924c83616 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/test_sonic_yang_mgmt.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Tests for `sonic_yang_mgmt` package.""" + +import pytest + +@pytest.fixture +def response(): + """Sample pytest fixture. + See more at: http://doc.pytest.org/en/latest/fixture.html + """ + # import requests + # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') + + +def test_content(response): + """Sample pytest test function with the pytest fixture as an argument.""" + # from bs4 import BeautifulSoup + # assert 'GitHub' in BeautifulSoup(response.content).title.string diff --git a/src/sonic-yang-mgmt/tests/yang-model-tests/yangModelTesting.py b/src/sonic-yang-mgmt/tests/yang-model-tests/yangModelTesting.py new file mode 100644 index 000000000000..4c4be8c6e48b --- /dev/null +++ b/src/sonic-yang-mgmt/tests/yang-model-tests/yangModelTesting.py @@ -0,0 +1,275 @@ +# This script is used to + +import yang as ly +import logging +import argparse +import sys +import ijson +import json +#import sonic_yang as sy +from os import listdir +from os.path import isfile, join, splitext + +#Globals vars +PASS = 0 +FAIL = 1 +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger("YANG-TEST") +log.setLevel(logging.INFO) +log.addHandler(logging.NullHandler()) + +# Global functions +def printExceptionDetails(): + try: + excType, excObj, traceBack = sys.exc_info() + fileName = traceBack.tb_frame.f_code.co_filename + lineNumber = traceBack.tb_lineno + log.error(" Exception >{}< in {}:{}".format(excObj, fileName, lineNumber)) + except Exception as e: + log.error(" Exception in printExceptionDetails") + return + +# class for YANG Model YangModelTesting +# Run function will run all the tests +# from a user given list. + +class YangModelTesting: + + def __init__(self, tests, yangDir, jsonFile): + self.defaultYANGFailure = { + 'Must': ['Must condition', 'not satisfied'], + 'InvalidValue': ['Invalid value'], + 'LeafRef': ['Leafref', 'non-existing'], + 'When': ['When condition', 'not satisfied'], + 'Pattern': ['pattern', 'does not satisfy'] + } + + self.ExceptionTests = { + 'WRONG_FAMILY_WITH_IP_PREFIX': { + 'desc': 'Configure Wrong family with ip-prefix for VLAN_Interface Table', + 'eStr': self.defaultYANGFailure['Must'] + }, + 'DHCP_SERVER_INCORRECT_FORMAT': { + 'desc': 'Add dhcp_server which is not in correct ip-prefix format.', + 'eStr': self.defaultYANGFailure['InvalidValue'] + ['dhcp_servers'] + }, + 'VLAN_WITH_NON_EXIST_PORT': { + 'desc': 'Configure a member port in VLAN_MEMBER table which does not exist.', + 'eStr': self.defaultYANGFailure['LeafRef'] + }, + 'VLAN_MEMEBER_WITH_NON_EXIST_VLAN': { + 'desc': 'Configure vlan-id in VLAN_MEMBER table which does not exist in VLAN table.', + 'eStr': self.defaultYANGFailure['LeafRef'] + }, + 'TAGGING_MODE_WRONG_VALUE': { + 'desc': 'Configure wrong value for tagging_mode.', + 'eStr': self.defaultYANGFailure['InvalidValue'] + ['tagging_mode'] + }, + 'INTERFACE_IP_PREFIX_EMPTY_STRING': { + 'desc': 'Configure empty string as ip-prefix in INTERFACE table.', + 'eStr': self.defaultYANGFailure['InvalidValue'] + ['ip-prefix'] + }, + 'ACL_RULE_UNDEFINED_PACKET_ACTION': { + 'desc': 'Configure undefined packet_action in ACL_RULE table.', + 'eStr': self.defaultYANGFailure['InvalidValue'] + ['PACKET_ACTION'] + }, + 'ACL_TABLE_UNDEFINED_TABLE_TYPE': { + 'desc': 'Configure undefined acl_table_type in ACL_TABLE table.', + 'eStr': self.defaultYANGFailure['InvalidValue'] + ['type'] + }, + 'ACL_RULE_WITH_NON_EXIST_ACL_TABLE': { + 'desc': 'Configure non-existing ACL_TABLE in ACL_RULE.', + 'eStr': self.defaultYANGFailure['LeafRef'] + }, + 'ACL_RULE_IP_TYPE_SRC_IPV6_MISMATCH': { + 'desc': 'Configure IP_TYPE as ipv4any and SRC_IPV6 in ACL_RULE.', + 'eStr': self.defaultYANGFailure['When'] + ['IP_TYPE'] + }, + 'ACL_RULE_ARP_TYPE_DST_IPV6_MISMATCH': { + 'desc': 'Configure IP_TYPE as ARP and DST_IPV6 in ACL_RULE.', + 'eStr': self.defaultYANGFailure['When'] + ['IP_TYPE'] + }, + 'ACL_RULE_WRONG_L4_SRC_PORT_RANGE': { + 'desc': 'Configure l4_src_port_range as 99999-99999 in ACL_RULE', + 'eStr': self.defaultYANGFailure['Pattern'] + }, + 'ACL_RULE_ARP_TYPE_ICMPV6_CODE_MISMATCH': { + 'desc': 'Configure IP_TYPE as ARP and ICMPV6_CODE in ACL_RULE.', + 'eStr': self.defaultYANGFailure['When'] + ['IP_TYPE'] + }, + 'ACL_RULE_WRONG_INNER_ETHER_TYPE': { + 'desc': 'Configure INNER_ETHER_TYPE as 0x080C in ACL_RULE.', + 'eStr': self.defaultYANGFailure['Pattern'] + } + } + + self.tests = tests + if (self.tests == None): + self.tests = self.ExceptionTests.keys() + self.yangDir = yangDir + self.jsonFile = jsonFile + self.testNum = 1 + # other class vars + # self.ctx + return + + """ + load all YANG models before test run + """ + def loadYangModel(self, yangDir): + try: + # get all files + yangFiles = [f for f in listdir(yangDir) if isfile(join(yangDir, f))] + # get all yang files + yangFiles = [f for f in yangFiles if splitext(f)[-1].lower()==".yang"] + yangFiles = [f.split('.')[0] for f in yangFiles] + # load yang mdoules + self.ctx = ly.Context(yangDir) + log.debug(yangFiles) + for f in yangFiles: + # load a module + log.debug(f) + m = self.ctx.get_module(f) + if m is not None: + log.error("Could not get module: {}".format(m.name())) + else: + m = self.ctx.load_module(f) + if m is not None: + log.info("module: {} is loaded successfully".format(m.name())) + else: + return + except Exception as e: + printExceptionDetails() + raise e + return + + """ + Run all tests from list self.tests + """ + def run(self): + try: + self.loadYangModel(self.yangDir) + ret = 0 + for test in self.tests: + test = test.strip() + if test in self.ExceptionTests: + ret = ret + self.runExceptionTest(test); + except Exception as e: + printExceptionDetails() + raise e + return ret + + """ + Get the JSON input based on func name + and return jsonInput + """ + def readJsonInput(self, test): + try: + # load test specific Dictionary, using Key = func + # this is to avoid loading very large JSON in memory + log.debug(" Read JSON Section: " + test) + jInput = "" + with open(self.jsonFile, 'rb') as f: + jInst = ijson.items(f, test) + for it in jInst: + jInput = jInput + json.dumps(it) + log.debug(jInput) + except Exception as e: + printExceptionDetails() + return jInput + + """ + Log the start of a test + """ + def logStartTest(self, desc): + log.info("\n------------------- Test "+ str(self.testNum) +\ + ": " + desc + "---------------------") + self.testNum = self.testNum + 1 + return + + """ + Load Config Data and return Exception as String + """ + def loadConfigData(self, jInput): + s = "" + try: + node = self.ctx.parse_data_mem(jInput, ly.LYD_JSON, \ + ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT) + except Exception as e: + s = str(e) + log.debug(s) + return s + + """ + Run Exception Test + """ + def runExceptionTest(self, test): + try: + desc = self.ExceptionTests[test]['desc'] + self.logStartTest(desc) + jInput = self.readJsonInput(test) + # load the data, expect a exception with must condition failure + s = self.loadConfigData(jInput) + eStr = self.ExceptionTests[test]['eStr'] + log.debug(eStr) + if (sum(1 for str in eStr if str not in s) == 0): + log.info(desc + " Passed\n") + return PASS + except Exception as e: + printExceptionDetails() + log.info(desc + " Failed\n") + return FAIL + +# End of Class + +""" + Start Here +""" +def main(): + parser = argparse.ArgumentParser(description='Script to run YANG model tests', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" +Usage: +python yangModelTesting.py -h +""") + parser.add_argument('-t', '--tests', type=str, \ + help='tests to run separated by comma') + parser.add_argument('-f', '--json-file', type=str, \ + help='JSON input for tests ', required=True) + parser.add_argument('-y', '--yang-dir', type=str, \ + help='Path to YANG models', required=True) + parser.add_argument('-v', '--verbose-level', \ + help='Verbose mode', action='store_true') + parser.add_argument('-l', '--list-tests', \ + help='list all tests', action='store_true') + + args = parser.parse_args() + try: + tests = args.tests + jsonFile = args.json_file + yangDir = args.yang_dir + logLevel = args.verbose_level + listTests = args.list_tests + if logLevel: + log.setLevel(logging.DEBUG) + # Make a list + if (tests): + tests = tests.split(",") + + yTest = YangModelTesting(tests, yangDir, jsonFile) + if (listTests): + log.info(yTest.ExceptionTests.keys()) + sys.exit(0) + + ret = yTest.run() + if ret == 0: + log.info("All Test Passed") + sys.exit(ret) + + except Exception as e: + printExceptionDetails() + sys.exit(1) + + return +if __name__ == '__main__': + main() diff --git a/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json b/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json new file mode 100644 index 000000000000..0e79baf09b57 --- /dev/null +++ b/src/sonic-yang-mgmt/tests/yang-model-tests/yangTest.json @@ -0,0 +1,1174 @@ +{ + "WRONG_FAMILY_WITH_IP_PREFIX": { + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_INTERFACE": { + "VLAN_INTERFACE_LIST": [{ + "vlan_name": "Vlan100", + "ip-prefix": "2a04:5555:66:7777::1/64", + "scope": "global", + "family": "IPv4" + }] + }, + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlan_name": "Vlan100", + "description": "server_vlan" + }] + } + } + }, + + "DHCP_SERVER_INCORRECT_FORMAT": { + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlan_name": "Vlan100", + "description": "server_vlan", + "dhcp_servers": [ + "10.186.72.566" + ], + "mtu": "9216", + "admin_status": "up" + }] + } + } + }, + + "VLAN_WITH_NON_EXIST_PORT": { + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_MEMBER": { + "VLAN_MEMBER_LIST": [{ + "vlan_name": "Vlan100", + "port": "Ethernet156", + "tagging_mode": "tagged" + }] + }, + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlan_name": "Vlan100", + "description": "server_vlan" + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "VLAN_MEMEBER_WITH_NON_EXIST_VLAN": { + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_MEMBER": { + "VLAN_MEMBER_LIST": [{ + "vlan_name": "Vlan200", + "port": "Ethernet0", + "tagging_mode": "tagged" + }] + }, + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlan_name": "Vlan100", + "description": "server_vlan" + }, + { + "vlan_name": "Vlan300", + "description": "ipmi_vlan" + } + ] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }] + } + } + }, + + "TAGGING_MODE_WRONG_VALUE": { + "sonic-vlan:sonic-vlan": { + "sonic-vlan:VLAN_MEMBER": { + "VLAN_MEMBER_LIST": [{ + "vlan_name": 100, + "port": "Ethernet0", + "tagging_mode": "non-tagged" + }] + }, + "sonic-vlan:VLAN": { + "VLAN_LIST": [{ + "vlan_name": "Vlan100", + "description": "server_vlan" + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }] + } + } + }, + + "INTERFACE_IP_PREFIX_EMPTY_STRING": { + "sonic-interface:sonic-interface": { + "sonic-interface:INTERFACE": { + "INTERFACE_LIST": [{ + "interface": "Ethernet8", + "ip-prefix": "", + "scope": "global", + "family": "IPv4" + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet8", + "alias": "eth8", + "description": "Ethernet8", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }] + } + } + }, + + "ACL_RULE_UNDEFINED_PACKET_ACTION": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "SEND", + "DST_IP": "10.186.72.0/26", + "SRC_IP": "10.176.0.0/15", + "PRIORITY": 999980, + "IP_TYPE": "IPV4ANY" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "policy_desc": "Filter IPv4", + "type": "L3", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_TABLE_UNDEFINED_TABLE_TYPE": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V6", + "policy_desc": "Filter IPv6", + "type": "LAYER3V4", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_WITH_NON_EXIST_ACL_TABLE": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NOT-EXIST", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.186.72.0/26", + "SRC_IP": "10.176.0.0/15", + "PRIORITY": 999980, + "IP_TYPE": "IPV4ANY" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "policy_desc": "Filter IPv6", + "type": "L3", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_IP_TYPE_SRC_IPV6_MISMATCH": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "SRC_IPV6": "2001::1/64", + "PRIORITY": 999980, + "IP_TYPE": "IPV4ANY" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "policy_desc": "Filter IPv4", + "type": "L3", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_ARP_TYPE_DST_IPV6_MISMATCH": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V6", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "DST_IPV6": "2001::2/64", + "PRIORITY": 999980, + "IP_TYPE": "ARP" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V6", + "policy_desc": "Filter IPv6", + "type": "L3V6", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_WRONG_L4_SRC_PORT_RANGE": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V6", + "RULE_NAME": "Rule_20", + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IP", + "SRC_IPV6": "2a04:f547:41::/48", + "PRIORITY": 999980, + "DST_IPV6": "2a04:f547:43:320::/64", + "L4_SRC_PORT_RANGE": "99999-99999" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V6", + "policy_desc": "Filter IPv6", + "type": "L3V6", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_ARP_TYPE_ICMPV6_CODE_MISMATCH": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "RULE_NAME": "Rule_40", + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.186.72.64/26", + "SRC_IP": "10.176.0.0/15", + "PRIORITY": 999960, + "ICMPV6_CODE": 5, + "IP_TYPE": "ARP", + "INNER_ETHER_TYPE": "0x88CC" + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "policy_desc": "Filter IPv4", + "type": "L3", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "ACL_RULE_WRONG_INNER_ETHER_TYPE": { + "sonic-acl:sonic-acl": { + "sonic-acl:ACL_RULE": { + "ACL_RULE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "RULE_NAME": "Rule_40", + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.186.72.64/26", + "SRC_IP": "10.176.0.0/15", + "PRIORITY": 999960, + "IP_TYPE": "ARP", + "INNER_ETHER_TYPE": "0x080C" + + }] + }, + "sonic-acl:ACL_TABLE": { + "ACL_TABLE_LIST": [{ + "ACL_TABLE_NAME": "NO-NSW-PACL-V4", + "policy_desc": "Filter IPv4", + "type": "L3", + "stage": "EGRESS", + "ports": ["Ethernet0", "Ethernet1"] + }] + } + }, + "sonic-port:sonic-port": { + "sonic-port:PORT": { + "PORT_LIST": [{ + "port_name": "Ethernet0", + "alias": "eth0", + "description": "Ethernet0", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + }, + { + "port_name": "Ethernet1", + "alias": "eth1", + "description": "Ethernet1", + "speed": 25000, + "mtu": 9000, + "admin_status": "up" + } + ] + } + } + }, + + "SAMPLE_CONFIG_DB_JSON": { + "VLAN_INTERFACE": { + "Vlan111|2a04:5555:45:6709::1/64": { + "scope": "global", + "family": "IPv6" + }, + "Vlan111|10.222.10.65/26": { + "scope": "global", + "family": "IPv4" + }, + "Vlan111|fe80::1/10": { + "scope": "local", + "family": "IPv6" + }, + "Vlan777|2a04:5555:41:4e9::1/64": { + "scope": "global", + "family": "IPv6" + }, + "Vlan777|10.111.58.65/26": { + "scope": "global", + "family": "IPv4" + }, + "Vlan777|fe80::1/10": { + "scope": "local", + "family": "IPv6" + } + }, + "ACL_RULE": { + "V4-ACL-TABLE|DEFAULT_DENY": { + "PACKET_ACTION": "DROP", + "IP_TYPE": "IPv4ANY", + "PRIORITY": "0" + }, + "V4-ACL-TABLE|Rule_20": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.222.72.0/26", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777780", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_40": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.222.72.64/26", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777760", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_60": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.222.80.0/26", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777740", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_80": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.222.80.64/26", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777720", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_111": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.152.17.52/32", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777700", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_120": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.252.208.41/32", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777880", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_140": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.148.128.245/32", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777860", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_160": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.222.1.245/32", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777840", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_180": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "10.252.222.21/32", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "777820", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_9000": { + "PACKET_ACTION": "DROP", + "DST_IP": "0.0.0.0/0", + "SRC_IP": "10.222.0.0/15", + "PRIORITY": "991110", + "IP_TYPE": "IPv4ANY" + }, + "V4-ACL-TABLE|Rule_11100": { + "PACKET_ACTION": "FORWARD", + "DST_IP": "0.0.0.0/0", + "SRC_IP": "0.0.0.0/0", + "PRIORITY": "990000", + "IP_TYPE": "IPv4ANY" + }, + "V6-ACL-TBLE|DEFAULT_DENY": { + "PACKET_ACTION": "DROP", + "IP_TYPE": "IPv6ANY", + "PRIORITY": "0" + }, + "V6-ACL-TBLE|Rule_20": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "777780", + "DST_IPV6": "2a04:5555:43:320::/64" + }, + "V6-ACL-TBLE|Rule_40": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "777760", + "DST_IPV6": "2a04:5555:43:321::/64" + }, + "V6-ACL-TBLE|Rule_60": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "777740", + "DST_IPV6": "2a04:5555:43:340::/64" + }, + "V6-ACL-TBLE|Rule_80": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "777720", + "DST_IPV6": "2a04:5555:43:341::/64" + }, + "V6-ACL-TBLE|Rule_111": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "777700", + "DST_IPV6": "2a04:5555:32:12::/64" + }, + "V6-ACL-TBLE|Rule_9000": { + "PACKET_ACTION": "DROP", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "2a04:5555:41::/48", + "PRIORITY": "991110", + "DST_IPV6": "::/0" + }, + "V6-ACL-TBLE|Rule_11100": { + "PACKET_ACTION": "FORWARD", + "IP_TYPE": "IPv6ANY", + "SRC_IPV6": "::/0", + "PRIORITY": "990000", + "DST_IPV6": "::/0" + } + }, + "DEVICE_METADATA": { + "localhost": { + "type": "ToR", + "mac": "00:11:22:33:dd:5a", + "hostname": "asw.dc", + "bgp_asn": "64850", + "hwsku": "Stone" + } + }, + "VLAN": { + "Vlan111": { + "description": "svlan", + "dhcp_servers": [ + "10.222.72.116" + ], + "vlanid": "111", + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet8", + "Ethernet3", + "Ethernet0", + "Ethernet1", + "Ethernet6", + "Ethernet4", + "Ethernet5", + "Ethernet9", + "Ethernet2", + "Ethernet7", + "Ethernet32", + "Ethernet30", + "Ethernet31", + "Ethernet36", + "Ethernet34", + "Ethernet33", + "Ethernet35", + "Ethernet29", + "Ethernet21", + "Ethernet20", + "Ethernet23", + "Ethernet22", + "Ethernet27", + "Ethernet26", + "Ethernet18", + "Ethernet19", + "Ethernet14", + "Ethernet15", + "Ethernet16", + "Ethernet17", + "Ethernet10", + "Ethernet11", + "Ethernet12", + "Ethernet13", + "Ethernet28" + ] + }, + "Vlan777": { + "description": "pvlan", + "dhcp_servers": [ + "10.222.72.116" + ], + "vlanid": "777", + "mtu": "9216", + "admin_status": "up", + "members": [ + "Ethernet9", + "Ethernet2", + "Ethernet8", + "Ethernet27", + "Ethernet14", + "Ethernet35" + ] + } + }, + "DEVICE_NEIGHBOR": { + "Ethernet112": { + "name": "dccsw01.nw", + "port": "Eth18" + }, + "Ethernet114": { + "name": "dccsw02.nw", + "port": "Eth18" + }, + "Ethernet116": { + "name": "dccsw03.nw", + "port": "Eth18" + }, + "Ethernet118": { + "name": "dccsw04.nw", + "port": "Eth18" + } + }, + "PORT": { + "Ethernet0": { + "alias": "Eth1/1", + "lanes": "65", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet1": { + "alias": "Eth1/2", + "lanes": "66", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet2": { + "alias": "Eth1/3", + "lanes": "67", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet3": { + "alias": "Eth1/4", + "lanes": "68", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet4": { + "alias": "Eth2/1", + "lanes": "69", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet5": { + "alias": "Eth2/2", + "lanes": "70", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet6": { + "alias": "Eth2/3", + "lanes": "71", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet7": { + "alias": "Eth2/4", + "lanes": "72", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet8": { + "alias": "Eth3/1", + "lanes": "73", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet9": { + "alias": "Eth3/2", + "lanes": "74", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet10": { + "alias": "Eth3/3", + "lanes": "75", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet11": { + "alias": "Eth3/4", + "lanes": "76", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet12": { + "alias": "Eth4/1", + "lanes": "77", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet13": { + "alias": "Eth4/2", + "lanes": "78", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet14": { + "alias": "Eth4/3", + "lanes": "79", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet15": { + "alias": "Eth4/4", + "lanes": "80", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet16": { + "alias": "Eth5/1", + "lanes": "33", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet17": { + "alias": "Eth5/2", + "lanes": "34", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet18": { + "alias": "Eth5/3", + "lanes": "35", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet19": { + "alias": "Eth5/4", + "lanes": "36", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet20": { + "alias": "Eth6/1", + "lanes": "37", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet21": { + "alias": "Eth6/2", + "lanes": "38", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet22": { + "alias": "Eth6/3", + "lanes": "39", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet23": { + "alias": "Eth6/4", + "lanes": "40", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet24": { + "alias": "Eth7/1", + "lanes": "41", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet25": { + "alias": "Eth7/2", + "lanes": "42", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet26": { + "alias": "Eth7/3", + "lanes": "43", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet27": { + "alias": "Eth7/4", + "lanes": "44", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet28": { + "alias": "Eth8/1", + "lanes": "45", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet29": { + "alias": "Eth8/2", + "lanes": "46", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet30": { + "alias": "Eth8/3", + "lanes": "47", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet31": { + "alias": "Eth8/4", + "lanes": "48", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet32": { + "alias": "Eth9/1", + "lanes": "49", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet33": { + "alias": "Eth9/2", + "lanes": "50", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet34": { + "alias": "Eth9/3", + "lanes": "51", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet35": { + "alias": "Eth9/4", + "lanes": "52", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet36": { + "alias": "Eth10/1", + "lanes": "53", + "description": "", + "speed": "11100", + "admin_status": "up" + }, + "Ethernet112": { + "alias": "Eth29/1", + "lanes": "113,114", + "description": "50G|dccsw01.nw|Eth18", + "fec": "fc", + "admin_status": "up" + } + }, + "ACL_TABLE": { + "V4-ACL-TABLE": { + "type": "L3", + "policy_desc": "V4-ACL-TABLE", + "ports": [ + "Ethernet26", + "Ethernet27", + "Ethernet24" + ] + }, + "V6-ACL-TBLE": { + "type": "L3V6", + "policy_desc": "V6-ACL-TBLE", + "ports": [ + "Ethernet14", + "Ethernet15", + "Ethernet23", + "Ethernet30", + "Ethernet31", + "Ethernet18", + "Ethernet19", + "Ethernet25", + "Ethernet24" + ] + } + }, + "INTERFACE": { + "Ethernet112|2a04:5555:40:a709::2/126": { + "scope": "global", + "family": "IPv6" + }, + "Ethernet112|10.184.228.211/31": { + "scope": "global", + "family": "IPv4" + }, + "Ethernet14|2a04:5555:40:a749::2/126": { + "scope": "global", + "family": "IPv6" + }, + "Ethernet14|10.184.229.211/31": { + "scope": "global", + "family": "IPv4" + }, + "Ethernet16|2a04:5555:40:a789::2/126": { + "scope": "global", + "family": "IPv6" + }, + "Ethernet16|10.184.230.211/31": { + "scope": "global", + "family": "IPv4" + }, + "Ethernet18|2a04:5555:40:a7c9::2/126": { + "scope": "global", + "family": "IPv6" + }, + "Ethernet18|10.184.231.211/31": { + "scope": "global", + "family": "IPv4" + } + }, + "VLAN_MEMBER": { + "Vlan111|Ethernet0": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet1": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet2": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet3": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet4": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet5": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet6": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet29": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet30": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet31": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet32": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet33": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet34": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet35": { + "tagging_mode": "untagged" + }, + "Vlan111|Ethernet36": { + "tagging_mode": "untagged" + } + }, + "LOOPBACK_INTERFACE": { + "Loopback0|2a04:5555:40:4::4e9/128": { + "scope": "global", + "family": "IPv6" + }, + "Loopback0|10.184.8.233/32": { + "scope": "global", + "family": "IPv4" + } + }, + "CRM": { + "Config": { + "polling_interval": "0" + } + } + } +} diff --git a/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree b/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree new file mode 100644 index 000000000000..35ccb0bad2df --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/Sonic Yang Tree @@ -0,0 +1,103 @@ +module: sonic-acl + +--rw sonic-acl + +--rw ACL_RULE + | +--rw ACL_RULE_LIST* [ACL_TABLE_NAME RULE_NAME] + | +--rw ACL_TABLE_NAME -> /sonic-acl/ACL_TABLE/ACL_TABLE_LIST/ACL_TABLE_NAME + | +--rw RULE_NAME string + | +--rw PACKET_ACTION? head:packet_action + | +--rw IP_TYPE? head:ip_type + | +--rw PRIORITY? uint32 + | +--rw (ip_prefix)? + | | +--:(ip4_prefix) + | | | +--rw SRC_IP? inet:ipv4-prefix + | | | +--rw DST_IP? inet:ipv4-prefix + | | +--:(ip6_prefix) + | | +--rw SRC_IPV6? inet:ipv6-prefix + | | +--rw DST_IPV6? inet:ipv6-prefix + | +--rw IN_PORTS* uint16 + | +--rw OUT_PORTS* uint16 + | +--rw (src_port)? + | | +--:(l4_src_port) + | | | +--rw L4_SRC_PORT? uint16 + | | +--:(l4_src_port_range) + | | +--rw L4_SRC_PORT_RANGE? string + | +--rw (dst_port)? + | | +--:(l4_dst_port) + | | | +--rw L4_DST_PORT? uint16 + | | +--:(l4_dst_port_range) + | | +--rw L4_DST_PORT_RANGE? string + | +--rw ETHER_TYPE? string + | +--rw IP_PROTOCOL? uint8 + | +--rw TCP_FLAGS? string + | +--rw DSCP? uint8 + | +--rw TC? uint8 + | +--rw (icmp)? + | | +--:(icmp4) + | | | +--rw ICMP_TYPE? uint8 + | | | +--rw ICMP_CODE? uint8 + | | +--:(icmp6) + | | +--rw ICMPV6_TYPE? uint8 + | | +--rw ICMPV6_CODE? uint8 + | +--rw INNER_ETHER_TYPE? string + | +--rw INNER_IP_PROTOCOL? uint8 + | +--rw INNER_L4_SRC_PORT? uint16 + | +--rw INNER_L4_DST_PORT? uint16 + +--rw ACL_TABLE + +--rw ACL_TABLE_LIST* [ACL_TABLE_NAME] + +--rw ACL_TABLE_NAME string + +--rw policy_desc? string + +--rw type? head:acl_table_type + +--rw stage? enumeration + +--rw ports* union +module: sonic-interface + +--rw sonic-interface + +--rw INTERFACE + +--rw INTERFACE_LIST* [interface ip-prefix] + +--rw interface -> /port:sonic-port/PORT/PORT_LIST/port_name + +--rw ip-prefix inet:ip-prefix + +--rw scope? enumeration + +--rw family? head:ip-family +module: sonic-port + +--rw sonic-port + +--rw PORT + +--rw PORT_LIST* [port_name] + +--rw port_name string + +--rw alias? string + +--rw lanes? string + +--rw description? string + +--rw speed? uint32 + +--rw mtu? uint16 + +--rw admin_status head:admin_status + +--rw fec? string +module: sonic-portchannel + +--rw sonic-portchannel + +--rw PORTCHANNEL + +--rw PORTCHANNEL_LIST* [portchannel_name] + +--rw portchannel_name string + +--rw members* -> /port:sonic-port/PORT/PORT_LIST/port_name + +--rw min_links? uint8 + +--rw description? string + +--rw mtu? uint16 + +--rw admin_status head:admin_status +module: sonic-vlan + +--rw sonic-vlan + +--rw VLAN_INTERFACE + | +--rw VLAN_INTERFACE_LIST* [vlan_name ip-prefix] + | +--rw vlan_name -> /sonic-vlan/VLAN/VLAN_LIST/vlan_name + | +--rw ip-prefix inet:ip-prefix + | +--rw scope? enumeration + | +--rw family? head:ip-family + +--rw VLAN + | +--rw VLAN_LIST* [vlan_name] + | +--rw vlan_name string + | +--rw vlanid? uint16 + | +--rw description? string + | +--rw dhcp_servers* inet:ip-address + | +--rw mtu? uint16 + | +--rw admin_status head:admin_status + | +--rw members* -> /port:sonic-port/PORT/PORT_LIST/port_name + +--rw VLAN_MEMBER + +--rw VLAN_MEMBER_LIST* [vlan_name port] + +--rw vlan_name -> /sonic-vlan/VLAN/VLAN_LIST/vlan_name + +--rw port -> /port:sonic-port/PORT/PORT_LIST/port_name + +--rw tagging_mode head:vlan_tagging_mode diff --git a/src/sonic-yang-mgmt/yang-models/sonic-acl.yang b/src/sonic-yang-mgmt/yang-models/sonic-acl.yang new file mode 100644 index 000000000000..290744a0633e --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-acl.yang @@ -0,0 +1,274 @@ +module sonic-acl { + + yang-version 1.1; + + namespace "http://github.com/Azure/sonic-acl"; + prefix acl; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + import sonic-portchannel { + prefix lag; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "ACL YANG Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-acl { + + container ACL_RULE { + + description "ACL_RULE part of config_db.json"; + + list ACL_RULE_LIST { + + key "ACL_TABLE_NAME RULE_NAME"; + + leaf ACL_TABLE_NAME { + type leafref { + path "/acl:sonic-acl/acl:ACL_TABLE/acl:ACL_TABLE_LIST/acl:ACL_TABLE_NAME"; + } + } + + leaf RULE_NAME { + type string { + length 1..255; + } + } + + leaf PACKET_ACTION { + type head:packet_action; + } + + leaf IP_TYPE { + type head:ip_type; + } + + leaf PRIORITY { + type uint32 { + range 0..999999; + } + } + + choice ip_prefix { + + case ip4_prefix { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV4' or .='IPv4ANY' or .='ARP'])"; + leaf SRC_IP { + type inet:ipv4-prefix; + } + + leaf DST_IP { + type inet:ipv4-prefix; + } + } + + case ip6_prefix { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV6' or .='IPv6ANY'])"; + leaf SRC_IPV6 { + type inet:ipv6-prefix; + } + + leaf DST_IPV6 { + type inet:ipv6-prefix; + } + } + } + + leaf-list IN_PORTS { + /* Values in leaf list are UNIQUE */ + type uint16; + } + + leaf-list OUT_PORTS { + /* Values in leaf list are UNIQUE */ + type uint16; + } + + choice src_port { + case l4_src_port { + leaf L4_SRC_PORT { + type uint16; + } + } + + case l4_src_port_range { + leaf L4_SRC_PORT_RANGE { + type string { + pattern '([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])-([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])'; + } + } + } + } + + choice dst_port { + case l4_dst_port { + leaf L4_DST_PORT { + type uint16; + } + } + + case l4_dst_port_range { + leaf L4_DST_PORT_RANGE { + type string { + pattern '([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])-([0-9]{1,4}|[0-5][0-9]{4}|[6][0-4][0-9]{3}|[6][5][0-2][0-9]{2}|[6][5][3][0-5]{2}|[6][5][3][6][0-5])'; + } + } + } + } + + leaf ETHER_TYPE { + type string { + pattern "(0x88CC|0x8100|0x8915|0x0806|0x0800|0x86DD|0x8847)"; + } + } + + leaf IP_PROTOCOL { + type uint8 { + range 1..143; + } + } + + leaf TCP_FLAGS { + type string { + pattern '0[x][0-9a-fA-F]{1,2}|0[X][0-9a-fA-F]{1,2}'; + } + } + + leaf DSCP { + type uint8; + } + + leaf TC { + type uint8; + } + + choice icmp { + + case icmp4 { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV4' or .='IPv4ANY' or .='ARP'])"; + leaf ICMP_TYPE { + type uint8 { + range 1..44; + } + } + + leaf ICMP_CODE { + type uint8 { + range 1..16; + } + } + } + + case icmp6 { + when "boolean(IP_TYPE[.='ANY' or .='IP' or .='IPV6' or .='IPv6ANY'])"; + leaf ICMPV6_TYPE { + type uint8 { + range 1..44; + } + } + + leaf ICMPV6_CODE { + type uint8 { + range 1..16; + } + } + } + } + + leaf INNER_ETHER_TYPE { + type string { + pattern "(0x88CC|0x8100|0x8915|0x0806|0x0800|0x86DD|0x8847)"; + } + } + + leaf INNER_IP_PROTOCOL { + type uint8 { + range 1..143; + } + } + + leaf INNER_L4_SRC_PORT { + type uint16; + } + + leaf INNER_L4_DST_PORT { + type uint16; + } + } + /* end of ACL_RULE_LIST */ + } + /* end of container ACL_RULE */ + + container ACL_TABLE { + + description "ACL_TABLE part of config_db.json"; + + list ACL_TABLE_LIST { + + key "ACL_TABLE_NAME"; + + leaf ACL_TABLE_NAME { + type string; + } + + leaf policy_desc { + type string { + length 1..255; + } + } + + leaf type { + type head:acl_table_type; + } + + leaf stage { + type enumeration { + enum INGRESS; + enum EGRESS; + } + } + + leaf-list ports { + /* union of leafref is allowed in YANG 1.1 */ + type union { + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + type leafref { + path /lag:sonic-portchannel/lag:PORTCHANNEL/lag:PORTCHANNEL_LIST/lag:portchannel_name; + } + } + } + } + /* end of ACL_TABLE_LIST */ + } + /* end of container ACL_TABLE */ + } + /* end of container sonic-acl */ +} +/* end of module sonic-acl */ diff --git a/src/sonic-yang-mgmt/yang-models/sonic-head.yang b/src/sonic-yang-mgmt/yang-models/sonic-head.yang new file mode 100644 index 000000000000..45822f8b7e8d --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-head.yang @@ -0,0 +1,72 @@ +module sonic-head { + + namespace "http://sonic-head"; + prefix sonic-head; + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "Head yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + typedef ip-family { + type enumeration { + enum IPv4; + enum IPv6; + } + } + + typedef admin_status { + type enumeration { + enum up; + enum down; + } + } + + typedef packet_action{ + type enumeration { + enum DROP; + enum FORWARD; + enum REDIRECT; + } + } + + typedef ip_type { + type enumeration { + enum ANY; + enum IP; + enum NON_IP; + enum IPV4; + enum IPV6; + enum IPv4ANY; + enum NON_IP4; + enum IPv6ANY; + enum NON_IPv6; + enum ARP; + } + } + + typedef acl_table_type { + type enumeration { + enum L2; + enum L3; + enum L3V6; + enum MIRROR; + enum MIRRORV6; + enum MIRROR_DSCP; + enum CTRLPLANE; + } + } + + typedef vlan_tagging_mode { + type enumeration { + enum tagged; + enum untagged; + enum priority_tagged; + } + } +} diff --git a/src/sonic-yang-mgmt/yang-models/sonic-interface.yang b/src/sonic-yang-mgmt/yang-models/sonic-interface.yang new file mode 100644 index 000000000000..dd2dea1b802d --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-interface.yang @@ -0,0 +1,79 @@ +module sonic-interface { + + namespace "http://github.com/Azure/sonic-interface"; + prefix intf; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "INTERFACE yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-interface { + container INTERFACE { + + description "INTERFACE part of config_db.json"; + + list INTERFACE_LIST { + + key "interface ip-prefix"; + + leaf interface { + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf ip-prefix { + /* TODO: this should be custom, not inet:ip-prefix.*/ + type inet:ip-prefix; + } + + leaf scope { + type enumeration { + enum global; + enum local; + } + } + + leaf family { + + /* family leaf needed for backward compatibility + Both ip4 and ip6 address are string in IETF RFC 6021, + so must statement can check based on : or ., family + should be IPv4 or IPv6 according. + */ + + must "(contains(../ip-prefix, ':') and current()='IPv6') or + (contains(../ip-prefix, '.') and current()='IPv4')"; + type head:ip-family; + } + } + /* end of INTERFACE_LIST */ + + } + /* end of INTERFACE container */ + } +} diff --git a/src/sonic-yang-mgmt/yang-models/sonic-port.yang b/src/sonic-yang-mgmt/yang-models/sonic-port.yang new file mode 100644 index 000000000000..07e6aa73ab50 --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-port.yang @@ -0,0 +1,90 @@ +module sonic-port{ + + namespace "http://github.com/Azure/sonic-port"; + prefix port; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "PORT yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-port{ + container PORT { + + description "PORT part of config_db.json"; + + list PORT_LIST { + + key "port_name"; + + leaf port_name { + type string { + length 1..128; + } + } + + leaf alias { + type string { + length 1..128; + } + } + + leaf lanes { + type string { + length 1..128; + } + } + + leaf description { + type string { + length 0..255; + } + } + + leaf speed { + type uint32 { + range 1..100000; + } + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + + leaf fec { + type string { + pattern "rc|fc|None"; + } + } + } /* end of list PORT_LIST */ + + } /* end of container PORT */ + + } /* end of container sonic-port */ + +} /* end of module sonic-port */ diff --git a/src/sonic-yang-mgmt/yang-models/sonic-portchannel.yang b/src/sonic-yang-mgmt/yang-models/sonic-portchannel.yang new file mode 100644 index 000000000000..656351f4e635 --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-portchannel.yang @@ -0,0 +1,85 @@ +module sonic-portchannel { + + namespace "http://github.com/Azure/sonic-portchannel"; + prefix lag; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "PORTCHANNEL yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-portchannel { + container PORTCHANNEL { + + description "PORTCHANNEL part of config_db.json"; + + list PORTCHANNEL_LIST { + + key "portchannel_name"; + + leaf portchannel_name { + type string { + length 1..128; + pattern 'PortChannel[0-9]{1,4}'; + } + } + + leaf-list members { + /* leaf-list members are unique by default */ + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf min_links { + type uint8 { + range 1..128; + } + } + + leaf description { + type string { + length 1..255; + } + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + } /* end of list PORTCHANNEL_LIST */ + + } /* end of container PORTCHANNEL */ + + } /* end of container sonic-portchannel */ + +} /* end of module sonic-port */ diff --git a/src/sonic-yang-mgmt/yang-models/sonic-vlan.yang b/src/sonic-yang-mgmt/yang-models/sonic-vlan.yang new file mode 100644 index 000000000000..cba8008f860f --- /dev/null +++ b/src/sonic-yang-mgmt/yang-models/sonic-vlan.yang @@ -0,0 +1,164 @@ +module sonic-vlan { + + namespace "http://github.com/Azure/sonic-vlan"; + prefix vlan; + + import ietf-yang-types { + prefix yang; + } + + import ietf-inet-types { + prefix inet; + } + + import sonic-head { + prefix head; + revision-date 2019-07-01; + } + + import sonic-port { + prefix port; + revision-date 2019-07-01; + } + + organization "Linkedin Corporation"; + + contact "lnos_coders@linkedin.com"; + + description "VLAN yang Module for SONiC OS"; + + revision 2019-07-01 { + description "First Revision"; + } + + container sonic-vlan { + container VLAN_INTERFACE { + + description "VLAN_INTERFACE part of config_db.json"; + + list VLAN_INTERFACE_LIST { + + key "vlan_name ip-prefix"; + + leaf vlan_name { + type leafref { + path "/vlan:sonic-vlan/vlan:VLAN/vlan:VLAN_LIST/vlan:vlan_name"; + } + } + + leaf ip-prefix { + mandatory true; + type inet:ip-prefix; + } + + leaf scope { + type enumeration { + enum global; + enum local; + } + } + + leaf family { + + /* family leaf needed for backward compatibility + Both ip4 and ip6 address are string in IETF RFC 6021, + so must statement can check based on : or ., family + should be IPv4 or IPv6 according. + */ + + must "(contains(../ip-prefix, ':') and current()='IPv6') or + (contains(../ip-prefix, '.') and current()='IPv4')"; + type head:ip-family; + } + } + /* end of VLAN_INTERFACE_LIST */ + } + /* end of VLAN_INTERFACE container */ + + container VLAN { + + description "VLAN part of config_db.json"; + + list VLAN_LIST { + + key "vlan_name"; + + leaf vlan_name { + type string { + pattern 'Vlan([0-9]{1,3}|[0-3][0-9]{4}|[4][0][0-8][0-9]|[4][0][9][0-4])'; + } + } + + leaf vlanid { + type uint16 { + range 1..4094; + } + } + + leaf description { + type string { + length 1..255; + } + } + + leaf-list dhcp_servers { + type inet:ip-address; + } + + leaf mtu { + type uint16 { + range 1..9216; + } + } + + leaf admin_status { + mandatory true; + type head:admin_status; + } + + leaf-list members { + /* leaf-list members are unique by default */ + + type leafref { + path "/port:sonic-port/port:PORT/port:PORT_LIST/port:port_name"; + } + } + } + /* end of VLAN_LIST */ + } + /* end of container VLAN */ + + container VLAN_MEMBER { + + description "VLAN_MEMBER part of config_db.json"; + + list VLAN_MEMBER_LIST { + + key "vlan_name port"; + + leaf vlan_name { + type leafref { + path "/vlan:sonic-vlan/vlan:VLAN/vlan:VLAN_LIST/vlan:vlan_name"; + } + } + + leaf port { + /* key elements are mandatory by default */ + mandatory true; + type leafref { + path /port:sonic-port/port:PORT/port:PORT_LIST/port:port_name; + } + } + + leaf tagging_mode { + mandatory true; + type head:vlan_tagging_mode; + } + } + /* end of list VLAN_MEMBER_LIST */ + } + /* end of container VLAN_MEMBER */ + } + /* end of container sonic-vlan */ +} +/* end of module sonic-vlan */