blob: 6cc4f0dfc61a1cff8e69720fac0db8140a5b6d71 [file] [log] [blame]
garciadeblas361145b2019-10-28 11:11:48 +01001# -*- coding: utf-8 -*-
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import yaml
garciaale76f6a622020-11-19 17:57:42 -030017import importlib
garciadeblas361145b2019-10-28 11:11:48 +010018from osm_im.vnfd import vnfd as vnfd_im
19from osm_im.nsd import nsd as nsd_im
20from osm_im.nst import nst as nst_im
garciaale76f6a622020-11-19 17:57:42 -030021etsi_nfv_vnfd = importlib.import_module("osm_im.etsi-nfv-vnfd")
22etsi_nfv_nsd = importlib.import_module("osm_im.etsi-nfv-nsd")
garciadeblas361145b2019-10-28 11:11:48 +010023from pyangbind.lib.serialise import pybindJSONDecoder
24import pyangbind.lib.pybindJSON as pybindJSON
25
26class ValidationException(Exception):
27 pass
28
garciadeblasc214d9e2019-11-08 14:23:04 +010029class Validation:
garciadeblas361145b2019-10-28 11:11:48 +010030
31 def pyangbind_validation(self, item, data, force=False):
32 '''
33 item: vnfd, nst, nsd
34 data: dict object loaded from the descriptor file
35 force: True to skip unknown fields in the descriptor
36 '''
37 if item == "vnfd":
38 myobj = vnfd_im()
39 elif item == "nsd":
40 myobj = nsd_im()
41 elif item == "nst":
42 myobj = nst_im()
garciaale76f6a622020-11-19 17:57:42 -030043 elif item == "etsi_nfv_vnfd":
44 myobj = etsi_nfv_vnfd.etsi_nfv_vnfd()
45 elif item == "etsi_nfv_nsd":
46 myobj = etsi_nfv_nsd.etsi_nfv_nsd()
garciadeblas361145b2019-10-28 11:11:48 +010047 else:
48 raise ValidationException("Not possible to validate '{}' item".format(item))
49
50 try:
51 pybindJSONDecoder.load_ietf_json(data, None, None, obj=myobj,
52 path_helper=True, skip_unknown=force)
53 out = pybindJSON.dumps(myobj, mode="ietf")
54 desc_out = yaml.safe_load(out)
55 return desc_out
56 except Exception as e:
57 raise ValidationException("Error in pyangbind validation: {}".format(str(e)))
58
59 def yaml_validation(self, descriptor):
60 try:
61 data = yaml.safe_load(descriptor)
62 except Exception as e:
garciadeblasc214d9e2019-11-08 14:23:04 +010063 raise ValidationException("Error in YAML validation. Not a proper YAML file: {}".format(e))
64 if 'vnfd:vnfd-catalog' in data or 'vnfd-catalog' in data:
garciadeblas361145b2019-10-28 11:11:48 +010065 item = "vnfd"
garciadeblasc214d9e2019-11-08 14:23:04 +010066 elif 'nsd:nsd-catalog' in data or 'nsd-catalog' in data:
garciadeblas361145b2019-10-28 11:11:48 +010067 item = "nsd"
68 elif 'nst' in data:
69 item = "nst"
garciaale76f6a622020-11-19 17:57:42 -030070 elif 'vnfd' in data:
71 item = "etsi_nfv_vnfd"
72 elif 'nsd' in data:
73 item = "etsi_nfv_nsd"
garciadeblas361145b2019-10-28 11:11:48 +010074 else:
garciadeblasc214d9e2019-11-08 14:23:04 +010075 raise ValidationException("Error in YAML validation. Not possible to determine the type of descriptor in the first line. Expected values: vnfd:vnfd-catalog, vnfd-catalog, nsd:nsd-catalog, nsd-catalog, nst")
garciadeblas361145b2019-10-28 11:11:48 +010076
77 return item, data
78
79 def descriptor_validation(self, descriptor):
80 item, data = self.yaml_validation(descriptor)
81 self.pyangbind_validation(item, data)
82