2 # -*- coding: utf-8 -*-
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
20 from __future__
import print_function
27 Tests the format of OSM VNFD and NSD descriptors
29 __author__
= "Alfonso Tierno, Guillermo Calvino"
30 __date__
= "2018-04-16"
32 version_date
= "Apr 2018"
35 class ArgumentParserError(Exception):
40 print("Usage: {} [options] FILE".format(sys
.argv
[0]))
41 print(" Validates vnfd, nsd and nst descriptors format")
42 print(" FILE: a yaml or json vnfd-catalog, nsd-catalog or nst descriptor")
44 print(" -v|--version: prints current version")
45 print(" -h|--help: shows this help")
46 print(" -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
49 def remove_prefix(desc
, prefix
):
51 Recursively removes prefix from keys
52 :param desc: dictionary or list to change
53 :param prefix: prefix to remove. Must
54 :return: None, param desc is changed
56 prefix_len
= len(prefix
)
57 if isinstance(desc
, dict):
59 for k
,v
in desc
.items():
60 if isinstance(v
, (list, tuple, dict)):
61 remove_prefix(v
, prefix
)
62 if isinstance(k
, str) and k
.startswith(prefix
) and k
!= prefix
:
63 prefixed_list
.append(k
)
64 for k
in prefixed_list
:
65 desc
[k
[prefix_len
:]] = desc
.pop(k
)
66 elif isinstance(desc
, (list, tuple)):
68 if isinstance(desc
, (list, tuple, dict)):
69 remove_prefix(i
, prefix
)
71 if __name__
=="__main__":
73 format_output_yaml
= True
74 input_file_name
= None
78 # load parameters and configuration
79 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvi:o:", ["input=", "help", "version",])
82 if o
in ("-v", "--version"):
83 print ("test descriptor version THREE " + __version__
+ ' ' + version_date
)
85 elif o
in ("-h", "--help"):
88 elif o
in ("-i", "--input"):
91 assert False, "Unhandled option"
92 if not input_file_name
:
94 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
95 input_file_name
= args
[0]
98 file_name
= input_file_name
99 with
open(file_name
, 'r') as f
:
100 descriptor_str
= f
.read()
103 if input_file_name
.endswith('.yaml') or input_file_name
.endswith('.yml') or not \
104 (input_file_name
.endswith('.json') or '\t' in descriptor_str
):
105 data
= yaml
.load(descriptor_str
)
107 data
= json
.loads(descriptor_str
)
108 format_output_yaml
= False
110 import osm_im
.vnfd
as vnfd_catalog
111 import osm_im
.nsd
as nsd_catalog
112 import osm_im
.nst
as nst_catalog
113 from pyangbind
.lib
.serialise
import pybindJSONDecoder
115 if "vnfd:vnfd-catalog" in data
or "vnfd-catalog" in data
:
117 # Check if mgmt-interface is defined:
118 remove_prefix(data
, "vnfd:")
119 vnfd_descriptor
= data
["vnfd-catalog"]
120 vnfd_list
= vnfd_descriptor
["vnfd"]
122 for vnfd
in vnfd_list
:
123 vdu_list
= vnfd
["vdu"]
126 external_interface_list
= vdu
.pop("external-interface", ())
127 for external_interface
in external_interface_list
:
128 if external_interface
.get("virtual-interface", {}).get("type") == "OM-MGMT":
130 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'PARAVIRT' instead")
131 interface_list
= vdu
.get("interface", ())
132 for interface
in interface_list
:
133 if interface
.get("virtual-interface", {}).get("type") == "OM-MGMT":
135 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'PARAVIRT' instead")
136 if vnfd
.get("mgmt-interface"):
138 if vnfd
["mgmt-interface"].get("vdu-id"):
139 raise KeyError("'mgmt-iface': Deprecated 'vdu-id' field. Please, use 'cp' field instead")
141 raise KeyError("'mgmt-iface' is a mandatory field and it is not defined")
142 myvnfd
= vnfd_catalog
.vnfd()
143 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=myvnfd
)
144 elif "nsd:nsd-catalog" in data
or "nsd-catalog" in data
:
146 mynsd
= nsd_catalog
.nsd()
147 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=mynsd
)
148 elif "nst:nst" in data
or "nst" in data
:
150 mynst
= nst_catalog
.nst()
151 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=mynst
)
154 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
157 except yaml
.YAMLError
as exc
:
159 if hasattr(exc
, 'problem_mark'):
160 mark
= exc
.problem_mark
161 error_pos
= "at line:%s column:%s" % (mark
.line
+ 1, mark
.column
+ 1)
162 print("Error loading file '{}'. yaml format error {}".format(input_file_name
, error_pos
), file=sys
.stderr
)
163 except ArgumentParserError
as e
:
164 print(str(e
), file=sys
.stderr
)
166 print("Error loading file '{}': {}".format(file_name
, e
), file=sys
.stderr
)
167 except ImportError as e
:
168 print ("Package python-osm-im not installed: {}".format(e
), file=sys
.stderr
)
169 except Exception as e
:
171 print("Error loading file '{}': {}".format(file_name
, str(e
)), file=sys
.stderr
)
174 print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor
, input_file_name
, str(e
)), file=sys
.stderr
)
176 print("Error. Invalid descriptor format in '{}': {}".format(input_file_name
, str(e
)), file=sys
.stderr
)