From 361145b8cad9af57ec8102e5e7d83f2e7b22508c Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Mon, 28 Oct 2019 11:11:48 +0100 Subject: [PATCH 1/1] Added validation class to IM to be used by other modules Change-Id: I219dac271de3a760ec34fc4b79d681fd4b823bbb Signed-off-by: garciadeblas --- .gitignore | 14 ++++++++- Makefile | 10 ++---- osm_im/__init__.py | 13 ++++++++ osm_im/validation.py | 73 ++++++++++++++++++++++++++++++++++++++++++++ setup.py | 5 +-- 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 osm_im/__init__.py create mode 100644 osm_im/validation.py diff --git a/.gitignore b/.gitignore index 3e381c6..4eb4eb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,16 @@ +# 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. + *.pyc .cache @@ -9,7 +22,6 @@ debian/osm-imdocs.install .eggs *egg-info *.gz -osm_im osm_im_trees dists pool diff --git a/Makefile b/Makefile index 8256a67..e4e8fef 100644 --- a/Makefile +++ b/Makefile @@ -45,14 +45,10 @@ trees: $(YANG_DESC_TREES) $(YANG_DESC_JSTREES) $(YANG_RECORD_TREES) $(YANG_RECOR openapi_schemas: $(OPENAPI_SCHEMAS) -$(OUT_DIR): - $(Q)mkdir -p $(OUT_DIR) - $(Q)touch $(OUT_DIR)/__init__.py - $(TREES_DIR): $(Q)mkdir -p $(TREES_DIR) -%.py: $(OUT_DIR) yang-ietf +%.py: yang-ietf $(Q)echo generating $@ from $*.yang $(Q)pyang $(PYANG_OPTIONS) --path $(MODEL_DIR) --plugindir $(PYBINDPLUGIN) -f pybind -o $(OUT_DIR)/$@ $(MODEL_DIR)/$*.yang @@ -78,7 +74,7 @@ $(TREES_DIR): $(Q)sed -r -i 's|||g' $(TREES_DIR)/$@ $(Q)mv $(TREES_DIR)/$@ $(TREES_DIR)/$*.html -osm.yaml: $(OUT_DIR) yang-ietf yang2swagger +osm.yaml: yang-ietf yang2swagger $(Q)echo generating $@ $(Q)$(JAVA) -jar ${HOME}/.m2/repository/com/mrv/yangtools/swagger-generator-cli/1.1.11/swagger-generator-cli-1.1.11-executable.jar -yang-dir $(MODEL_DIR) -output $(OUT_DIR)/$@ @@ -108,4 +104,4 @@ deps: $(Q)cp -n ~/.m2/settings.xml{,.orig} ; wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/master/settings.xml > ~/.m2/settings.xml clean: - $(Q)rm -rf dist osm_im.egg-info deb deb_dist *.gz osm-imdocs* yang2swagger $(OUT_DIR) $(TREES_DIR) + $(Q)rm -rf dist osm_im.egg-info deb deb_dist *.gz osm-imdocs* yang2swagger $(TREES_DIR) diff --git a/osm_im/__init__.py b/osm_im/__init__.py new file mode 100644 index 0000000..7284a2b --- /dev/null +++ b/osm_im/__init__.py @@ -0,0 +1,13 @@ +## +# 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/osm_im/validation.py b/osm_im/validation.py new file mode 100644 index 0000000..0863434 --- /dev/null +++ b/osm_im/validation.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- + +# 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. + +import yaml +import json +# import logging +from osm_im.vnfd import vnfd as vnfd_im +from osm_im.nsd import nsd as nsd_im +from osm_im.nst import nst as nst_im +from pyangbind.lib.serialise import pybindJSONDecoder +import pyangbind.lib.pybindJSON as pybindJSON + +class ValidationException(Exception): + pass + +class Validation(): + + def pyangbind_validation(self, item, data, force=False): + ''' + item: vnfd, nst, nsd + data: dict object loaded from the descriptor file + force: True to skip unknown fields in the descriptor + ''' + if item == "vnfd": + myobj = vnfd_im() + elif item == "nsd": + myobj = nsd_im() + elif item == "nst": + myobj = nst_im() + else: + raise ValidationException("Not possible to validate '{}' item".format(item)) + + try: + pybindJSONDecoder.load_ietf_json(data, None, None, obj=myobj, + path_helper=True, skip_unknown=force) + out = pybindJSON.dumps(myobj, mode="ietf") + desc_out = yaml.safe_load(out) + return desc_out + except Exception as e: + raise ValidationException("Error in pyangbind validation: {}".format(str(e))) + + def yaml_validation(self, descriptor): + try: + data = yaml.safe_load(descriptor) + except Exception as e: + raise ValidationException("Error in YAML validation. Not a proper YAML file") + if 'vnfd:vnfd-catalog' in data: + item = "vnfd" + elif 'nsd:nsd-catalog' in data: + item = "nsd" + elif 'nst' in data: + item = "nst" + else: + raise ValidationException("Error in YAML validation. Not possible to determine the type of descriptor in the first line. Expected values: vnfd:vnfd-catalog, nsd:nsd-catalog, nst") + + return item, data + + def descriptor_validation(self, descriptor): + item, data = self.yaml_validation(descriptor) + self.pyangbind_validation(item, data) + diff --git a/setup.py b/setup.py index ef8da90..f59e790 100644 --- a/setup.py +++ b/setup.py @@ -33,11 +33,8 @@ class Install_osm_im(install): self.pipinstall('pyang') self.pipinstall('pyangbind') import pyangbind - print("Creating dir {}/{} for python artifacts".format(os.getcwd(), self.im_dir)) + print("Using dir {}/{} for python artifacts".format(os.getcwd(), self.im_dir)) path = "{}/{}".format(os.getcwd(), self.im_dir) - if not os.path.exists(path): - os.makedirs(path) - open("{}/{}/__init__.py".format(os.getcwd(), self.im_dir), 'a').close() for files_item in ['vnfd', 'nsd', 'nst']: protoc_command = ["pyang", "-Werror", -- 2.17.1