1 # -*- coding: utf-8 -*-
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from osm_im
.vnfd
import vnfd
as vnfd_im
19 from osm_im
.nsd
import nsd
as nsd_im
20 from osm_im
.nst
import nst
as nst_im
21 from osm_im
import etsi_nfv_vnfd
, etsi_nfv_nsd
22 from pyangbind
.lib
.serialise
import pybindJSONDecoder
23 import pyangbind
.lib
.pybindJSON
as pybindJSON
25 class ValidationException(Exception):
30 def pyangbind_validation(self
, item
, data
, force
=False):
33 data: dict object loaded from the descriptor file
34 force: True to skip unknown fields in the descriptor
42 elif item
== "etsi_nfv_vnfd":
43 myobj
= etsi_nfv_vnfd
.etsi_nfv_vnfd()
44 elif item
== "etsi_nfv_nsd":
45 myobj
= etsi_nfv_nsd
.etsi_nfv_nsd()
47 raise ValidationException("Not possible to validate '{}' item".format(item
))
50 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=myobj
,
51 path_helper
=True, skip_unknown
=force
)
52 out
= pybindJSON
.dumps(myobj
, mode
="ietf")
53 desc_out
= yaml
.safe_load(out
)
55 except Exception as e
:
56 raise ValidationException("Error in pyangbind validation: {}".format(str(e
)))
58 def yaml_validation(self
, descriptor
):
60 data
= yaml
.safe_load(descriptor
)
61 except Exception as e
:
62 raise ValidationException("Error in YAML validation. Not a proper YAML file: {}".format(e
))
63 if 'vnfd:vnfd-catalog' in data
or 'vnfd-catalog' in data
:
65 elif 'nsd:nsd-catalog' in data
or 'nsd-catalog' in data
:
70 item
= "etsi_nfv_vnfd"
74 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")
78 def descriptor_validation(self
, descriptor
):
79 item
, data
= self
.yaml_validation(descriptor
)
80 self
.pyangbind_validation(item
, data
)