blob: 7334fbb3ac07a216400974f61aed3cf5305eef4e [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
17import json
18# import logging
19from osm_im.vnfd import vnfd as vnfd_im
20from osm_im.nsd import nsd as nsd_im
21from osm_im.nst import nst as nst_im
22from pyangbind.lib.serialise import pybindJSONDecoder
23import pyangbind.lib.pybindJSON as pybindJSON
24
25class ValidationException(Exception):
26 pass
27
garciadeblasc214d9e2019-11-08 14:23:04 +010028class Validation:
garciadeblas361145b2019-10-28 11:11:48 +010029
30 def pyangbind_validation(self, item, data, force=False):
31 '''
32 item: vnfd, nst, nsd
33 data: dict object loaded from the descriptor file
34 force: True to skip unknown fields in the descriptor
35 '''
36 if item == "vnfd":
37 myobj = vnfd_im()
38 elif item == "nsd":
39 myobj = nsd_im()
40 elif item == "nst":
41 myobj = nst_im()
42 else:
43 raise ValidationException("Not possible to validate '{}' item".format(item))
44
45 try:
46 pybindJSONDecoder.load_ietf_json(data, None, None, obj=myobj,
47 path_helper=True, skip_unknown=force)
48 out = pybindJSON.dumps(myobj, mode="ietf")
49 desc_out = yaml.safe_load(out)
50 return desc_out
51 except Exception as e:
52 raise ValidationException("Error in pyangbind validation: {}".format(str(e)))
53
54 def yaml_validation(self, descriptor):
55 try:
56 data = yaml.safe_load(descriptor)
57 except Exception as e:
garciadeblasc214d9e2019-11-08 14:23:04 +010058 raise ValidationException("Error in YAML validation. Not a proper YAML file: {}".format(e))
59 if 'vnfd:vnfd-catalog' in data or 'vnfd-catalog' in data:
garciadeblas361145b2019-10-28 11:11:48 +010060 item = "vnfd"
garciadeblasc214d9e2019-11-08 14:23:04 +010061 elif 'nsd:nsd-catalog' in data or 'nsd-catalog' in data:
garciadeblas361145b2019-10-28 11:11:48 +010062 item = "nsd"
63 elif 'nst' in data:
64 item = "nst"
65 else:
garciadeblasc214d9e2019-11-08 14:23:04 +010066 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 +010067
68 return item, data
69
70 def descriptor_validation(self, descriptor):
71 item, data = self.yaml_validation(descriptor)
72 self.pyangbind_validation(item, data)
73