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
28 Converts OSM VNFD, NSD descriptor from release TWO to release THREE format
30 __author__
= "Alfonso Tierno, Guillermo Calvino"
31 __date__
= "2017-10-14"
33 version_date
= "Nov 2017"
36 class ArgumentParserError(Exception):
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")
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")
53 def remove_prefix(desc
, prefix
):
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
60 prefix_len
= len(prefix
)
61 if isinstance(desc
, dict):
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)):
72 if isinstance(desc
, (list, tuple, dict)):
73 remove_prefix(i
, prefix
)
76 if __name__
=="__main__":
78 format_output_yaml
= True
79 input_file_name
= None
80 output_file_name
= None
84 # load parameters and configuration
85 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvi:o:", ["input=", "help", "version", "output=", "test",])
88 if o
in ("-v", "--version"):
89 print ("upgrade descriptor version " + __version__
+ ' ' + version_date
)
91 elif o
in ("-h", "--help"):
94 elif o
in ("-i", "--input"):
96 elif o
in ("-o", "--output"):
101 assert False, "Unhandled option"
102 if not input_file_name
:
104 raise ArgumentParserError("missing DESCRIPTOR_FILE parameter. Type --help for more info")
105 input_file_name
= args
[0]
108 file_name
= input_file_name
109 with
open(file_name
, 'r') as f
:
110 descriptor_str
= f
.read()
112 file_name
= output_file_name
113 output
= open(file_name
, 'w')
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
)
122 data
= json
.loads(descriptor_str
)
123 format_output_yaml
= False
126 import osm_im
.vnfd
as vnfd_catalog
127 import osm_im
.nsd
as nsd_catalog
128 from pyangbind
.lib
.serialise
import pybindJSONDecoder
130 if "vnfd:vnfd-catalog" in data
or "vnfd-catalog" in data
:
132 #Check if mgmt-interface is defined:
133 remove_prefix(data
, "vnfd:")
134 vnfd_descriptor
= data
["vnfd-catalog"]
135 vnfd_list
= vnfd_descriptor
["vnfd"]
137 for vnfd
in vnfd_list
:
138 vdu_list
= vnfd
["vdu"]
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":
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":
150 "Wrong 'Virtual-interface type': Deprecated 'OM-MGMT' value. Please, use 'VIRTIO' instead")
151 if vnfd
.get("mgmt-interface"):
153 if vnfd
["mgmt-interface"].get("vdu-id"):
154 raise KeyError("'mgmt-iface': Deprecated 'vdu-id' field. Please, use 'cp' field instead")
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
:
161 mynsd
= nsd_catalog
.nsd()
162 pybindJSONDecoder
.load_ietf_json(data
, None, None, obj
=mynsd
)
165 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
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")
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", ())
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()
205 vnfd
["vnf-configuration"]["config-primitive"] = primitive_list
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
213 error_position
[-1] = "vdu[{}]".format(vdu
["id"])
214 # Change external/internal interface
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
)
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
)
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
)
248 # order interface alphabetically and set position
250 interface_list
= sorted(interface_list
,
251 key
=lambda k
: k
.get('external-connection-point-ref',
252 k
.get('internal-connection-point-ref')))
254 for i
in interface_list
:
255 i
["position"] = str(index
)
258 vdu
["interface"] = interface_list
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)
265 error_position
.append("vdu-id")
266 vnfd
["mgmt-interface"]["cp"] = vdu2mgmt_cp
[vdu_id
]
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")
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", ())
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'
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"]
296 # Iterate with vld:id
297 error_position
.append("vld")
298 vld_list
= nsd
.get("vld",())
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"]
305 nsd
["vld"] = vld_list
308 error_position
= ["global"]
309 raise KeyError("This is not neither nsd-catalog nor vnfd-catalog descriptor")
311 if format_output_yaml
:
312 yaml
.dump(data
, output
, indent
=4, default_flow_style
=False)
314 json
.dump(data
, output
)
317 except yaml
.YAMLError
as exc
:
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
)
324 # except json.decoder.JSONDecodeError as e:
325 # print("Invalid field at configuration file '{file}' {message}".format(file=input_file_name, message=str(e)),
327 except ArgumentParserError
as e
:
328 print(str(e
), file=sys
.stderr
)
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
:
336 print("Error. Invalid {} descriptor format in '{}': {}".format(descriptor
, input_file_name
, str(e
)), file=sys
.stderr
)
338 print("Error. Invalid descriptor format in '{}': {}".format(input_file_name
, str(e
)),
341 print("Descriptor error at '{}': {}".format(":".join(error_position
), e
), file=sys
.stderr
)
343 print ("Error loading file '{}': {}".format(file_name
, str(e
)), file=sys
.stderr
)
346 # print("Unexpected exception {}".format(e), file=sys.stderr)