7db1ec6f119a010487947c00c8022fe63bcaf6c0
[osm/devops.git] / descriptor-packages / tools / upgrade_descriptor_version.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 ##
5 # All Rights Reserved.
6 #
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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
17 # under the License.
18 #
19 ##
20 from __future__ import print_function
21 import json
22 import yaml
23 # import logging
24 import sys
25 import getopt
26
27 """
28 Converts OSM VNFD, NSD descriptor from release TWO to release THREE format
29 """
30 __author__ = "Alfonso Tierno, Guillermo Calvino"
31 __date__ = "2017-10-14"
32 __version__ = "0.0.2"
33 version_date = "Nov 2017"
34
35
36 class ArgumentParserError(Exception):
37 pass
38
39
40 def usage():
41 print("Usage: {} [options] FILE".format(sys.argv[0]))
42 print(" EXPERIMENTAL: Upgrade vnfd, nsd descriptor from old versions to release THREE version")
43 print(" FILE: a yaml or json vnfd-catalog or nsd-catalog descriptor")
44 print(" OPTIONS:")
45 print(" -v|--version: prints current version")
46 print(" -h|--help: shows this help")
47 print(" -i|--input FILE: (same as param FILE) descriptor file to be upgraded")
48 print(" -o|--output FILE: where to write generated descriptor. By default stdout")
49 print(" --test: Content is tested to check wrong format or unknown keys")
50 return
51
52
53 def remove_prefix(desc, prefix):
54 """
55 Recursively removes prefix from keys
56 :param desc: dictionary or list to change
57 :param prefix: prefix to remove. Must
58 :return: None, param desc is changed
59 """
60 prefix_len = len(prefix)
61 if isinstance(desc, dict):
62 prefixed_list=[]
63 for k,v in desc.items():
64 if isinstance(v, (list, tuple, dict)):
65 remove_prefix(v, prefix)
66 if isinstance(k, str) and k.startswith(prefix) and k != prefix:
67 prefixed_list.append(k)
68 for k in prefixed_list:
69 desc[k[prefix_len:]] = desc.pop(k)
70 elif isinstance(desc, (list, tuple)):
71 for i in desc:
72 if isinstance(desc, (list, tuple, dict)):
73 remove_prefix(i, prefix)
74
75
76 if __name__=="__main__":
77 error_position = []
78 format_output_yaml = True
79 input_file_name = None
80 output_file_name = None
81 test_file = None
82 file_name = None
83 try:
84 # load parameters and configuration
85 opts, args = getopt.getopt(sys.argv[1:], "hvi:o:", ["input=", "help", "version", "output=", "test",])
86
87 for o, a in opts:
88 if o in ("-v", "--version"):
89 print ("upgrade descriptor version " + __version__ + ' ' + version_date)
90 sys.exit()
91 elif o in ("-h", "--help"):
92 usage()
93 sys.exit()
94 elif o in ("-i", "--input"):
95 input_file_name = a
96 elif o in ("-o", "--output"):
97 output_file_name = a
98 elif o == "--test":
99 test_file = True
100 else:
101 assert False, "Unhandled option"
102 if not input_file_name:
103 if not args:
104 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
105 input_file_name = args[0]
106
107 # Open files
108 file_name = input_file_name
109 with open(file_name, 'r') as f:
110 descriptor_str = f.read()
111 if output_file_name:
112 file_name = output_file_name
113 output = open(file_name, 'w')
114 else:
115 output = sys.stdout
116 file_name = None
117
118 if input_file_name.endswith('.yaml') or input_file_name.endswith('.yml') or not \
119 (input_file_name.endswith('.json') or '\t' in descriptor_str):
120 data = yaml.load(descriptor_str)
121 else: # json
122 data = json.loads(descriptor_str)
123 format_output_yaml = False
124
125 if test_file:
126 import osm_im.vnfd as vnfd_catalog
127 import osm_im.nsd as nsd_catalog
128 from pyangbind.lib.serialise import pybindJSONDecoder
129
130 if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
131 descriptor = "VNF"
132 #Check if mgmt-interface is defined:
133 remove_prefix(data, "vnfd:")
134 vnfd_descriptor = data["vnfd-catalog"]
135 vnfd_list = vnfd_descriptor["vnfd"]
136 mgmt_iface = False
137 for vnfd in vnfd_list:
138 vdu_list = vnfd["vdu"]
139 for vdu in vdu_list:
140 interface_list = []
141 external_interface_list = vdu.pop("external-interface", ())
142 for external_interface in external_interface_list:
143 if external_interface.get("virtual-interface", {}).get("type") == "OM-MGMT":
144 raise KeyError(
145 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'VIRTIO' instead")
146 interface_list = vdu.pop("interface", ())
147 for interface in interface_list:
148 if interface.get("virtual-interface", {}).get("type") == "OM-MGMT":
149 raise KeyError(
150 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'VIRTIO' instead")
151 if vnfd.get("mgmt-interface"):
152 mgmt_iface = True
153 if vnfd["mgmt-interface"].get("vdu-id"):
154 raise KeyError("'mgmt-iface': Deprecated 'vdu-id' field. Please, use 'cp' field instead")
155 if not mgmt_iface:
156 raise KeyError("'mgmt-iface' is a mandatory field and it is not defined")
157 myvnfd = vnfd_catalog.vnfd()
158 pybindJSONDecoder.load_ietf_json(data, None, None, obj=myvnfd)
159 elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
160 descriptor = "NS"
161 mynsd = nsd_catalog.nsd()
162 pybindJSONDecoder.load_ietf_json(data, None, None, obj=mynsd)
163 else:
164 descriptor = None
165 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
166 exit(0)
167
168 # Convert version
169 if "vnfd:vnfd-catalog" in data or "vnfd-catalog" in data:
170 remove_prefix(data, "vnfd:")
171 error_position.append("vnfd-catalog")
172 vnfd_descriptor = data["vnfd-catalog"]
173 vnfd_list = vnfd_descriptor["vnfd"]
174 error_position.append("vnfd")
175 for vnfd in vnfd_list:
176 error_position[-1] = "vnfd[{}]".format(vnfd["id"])
177 # Remove vnf-configuration:config-attributes
178 if "vnf-configuration" in vnfd and "config-attributes" in vnfd["vnf-configuration"]:
179 del vnfd["vnf-configuration"]["config-attributes"]
180 # Remove interval-vld:vendor
181 if "internal-vld" in vnfd:
182 internal_vld_list = vnfd.get("internal-vld", ())
183 for internal_vld in internal_vld_list:
184 if "vendor" in internal_vld:
185 del internal_vld["vendor"]
186 # Remove "rw-nsd:meta"
187 if "rw-vnfd:meta" in vnfd:
188 del vnfd["rw-vnfd:meta"]
189 # Change vnf-configuration:service-primitive into vnf-configuration:config-primitive
190 if "vnf-configuration" in vnfd and "service-primitive" in vnfd["vnf-configuration"]:
191 vnfd["vnf-configuration"]["config-primitive"] = vnfd["vnf-configuration"].pop("service-primitive")
192
193 # Convert to capital letters vnf-configuration:service-primitive:parameter:data-type
194 if "vnf-configuration" in vnfd and "config-primitive" in vnfd["vnf-configuration"]:
195 error_position.append("vnf-configuration")
196 error_position.append("config-primitive")
197 primitive_list = vnfd["vnf-configuration"].get("config-primitive", ())
198
199 for primitive in primitive_list:
200 if "parameter" in primitive:
201 parameter_list = primitive.get("parameter", ())
202 for parameter in parameter_list:
203 parameter["data-type"] = str(parameter["data-type"]).upper()
204
205 vnfd["vnf-configuration"]["config-primitive"] = primitive_list
206 error_position.pop()
207 error_position.pop()
208 # Iterate with vdu:interfaces
209 vdu_list = vnfd["vdu"]
210 error_position.append("vdu")
211 vdu2mgmt_cp = {} # internal dict to indicate management interface for each vdu
212 for vdu in vdu_list:
213 error_position[-1] = "vdu[{}]".format(vdu["id"])
214 # Change external/internal interface
215 interface_list = []
216 external_interface_list = vdu.pop("external-interface", ())
217 error_position.append("external-interface")
218 for external_interface in external_interface_list:
219 error_position[-1] = "external-interface[{}]".format(external_interface["name"])
220 if "rw-vnfd:floating-ip-needed" in external_interface:
221 del external_interface["rw-vnfd:floating-ip-needed"]
222 external_interface["type"] = "EXTERNAL"
223 external_interface["external-connection-point-ref"] = \
224 external_interface.pop("vnfd-connection-point-ref")
225 if external_interface.get("virtual-interface", {}).get("type") == "OM-MGMT":
226 external_interface["virtual-interface"]["type"] = "VIRTIO"
227 if vdu["id"] not in vdu2mgmt_cp:
228 vdu2mgmt_cp[vdu["id"]] = external_interface["external-connection-point-ref"]
229 interface_list.append(external_interface)
230 error_position.pop()
231 internal_interface_list = vdu.pop("internal-interface", ())
232 error_position.append("internal-interface")
233 for internal_interface in internal_interface_list:
234 error_position[-1] = "internal-interface[{}]".format(internal_interface["name"])
235 internal_interface["type"] = "INTERNAL"
236 internal_interface["internal-connection-point-ref"] = \
237 internal_interface.pop("vdu-internal-connection-point-ref")
238 interface_list.append(internal_interface)
239 error_position.pop()
240
241 #Removing "rw-vnfd:floating-ip-needed" items from V3 descriptors
242 interfaces = vdu.pop("interface", ())
243 for iface in interfaces:
244 if "rw-vnfd:floating-ip-needed" in iface:
245 del iface["rw-vnfd:floating-ip-needed"]
246 interface_list.append(iface)
247
248 # order interface alphabetically and set position
249 if interface_list:
250 interface_list = sorted(interface_list,
251 key=lambda k: k.get('external-connection-point-ref',
252 k.get('internal-connection-point-ref')))
253 index = 1
254 for i in interface_list:
255 i["position"] = str(index)
256 index += 1
257
258 vdu["interface"] = interface_list
259 error_position.pop()
260 # change mgmt-interface
261 if vnfd.get("mgmt-interface"):
262 error_position.append("mgmt-interface")
263 vdu_id = vnfd["mgmt-interface"].pop("vdu-id", None)
264 if vdu_id:
265 error_position.append("vdu-id")
266 vnfd["mgmt-interface"]["cp"] = vdu2mgmt_cp[vdu_id]
267 error_position.pop()
268 error_position.pop()
269 error_position = []
270 elif "nsd:nsd-catalog" in data or "nsd-catalog" in data:
271 remove_prefix(data, "nsd:")
272 error_position.append("nsd-catalog")
273 nsd_descriptor = data["nsd-catalog"]
274 nsd_list = nsd_descriptor["nsd"]
275 error_position.append("nsd")
276 for nsd in nsd_list:
277 error_position[-1] = "nsd[{}]".format(nsd["id"])
278 # set mgmt-network to true
279 error_position.append("vld")
280 vld_list = nsd.get("vld", ())
281 for vld in vld_list:
282 error_position[-1] = "vld[{}]".format(vld["id"])
283 if "mgmt" in vld["name"].lower() or "management" in vld["name"].lower():
284 vld['mgmt-network'] = 'true'
285 break
286 error_position.pop()
287 # Change initial-config-primitive into initial-service-primitive
288 if "initial-config-primitive" in nsd:
289 nsd['initial-service-primitive'] = nsd.pop("initial-config-primitive")
290 # Remove "rw-nsd:meta"
291 if "rw-nsd:meta" in nsd:
292 del nsd["rw-nsd:meta"]
293 # Remove "rw-meta"
294 if "rw-meta" in nsd:
295 del nsd["rw-meta"]
296 # Iterate with vld:id
297 error_position.append("vld")
298 vld_list = nsd.get("vld",())
299 for vld in vld_list:
300 error_position[-1] = "vld[{}]".format(vld["id"])
301 if "provider-network" in vld and "overlay-type" in vld["provider-network"]:
302 del vld["provider-network"]["overlay-type"]
303 error_position.pop()
304 if vld_list:
305 nsd["vld"] = vld_list
306 error_position = []
307 else:
308 error_position = ["global"]
309 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
310
311 if format_output_yaml:
312 yaml.dump(data, output, indent=4, default_flow_style=False)
313 else:
314 json.dump(data, output)
315 exit(0)
316
317 except yaml.YAMLError as exc:
318 error_pos = ""
319 if hasattr(exc, 'problem_mark'):
320 mark = exc.problem_mark
321 error_pos = "at line:%s column:%s" % (mark.line + 1, mark.column + 1)
322 print("Error loading file '{}'. yaml format error {}".format(input_file_name, error_pos), file=sys.stderr)
323
324 # except json.decoder.JSONDecodeError as e:
325 # print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name, message=str(e)),
326 # file=sys.stderr)
327 except ArgumentParserError as e:
328 print(str(e), file=sys.stderr)
329 except IOError as e:
330 print("Error loading file '{}': {}".format(file_name, e), file=sys.stderr)
331 except ImportError as e:
332 print ("Package python-osm-im not installed: {}".format(e), file=sys.stderr)
333 except Exception as e:
334 if test_file:
335 if descriptor:
336 print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor, input_file_name, str(e)), file=sys.stderr)
337 else:
338 print("Error. Invalid descriptor format in '{}': {}".format(input_file_name, str(e)),
339 file=sys.stderr)
340 elif error_position:
341 print("Descriptor error at '{}': {}".format(":".join(error_position), e), file=sys.stderr)
342 elif file_name:
343 print ("Error loading file '{}': {}".format(file_name, str(e)), file=sys.stderr)
344 else:
345 raise
346 # print("Unexpected exception {}".format(e), file=sys.stderr)
347 exit(1)