Bug 240 - NS Scaling Scale-out
[osm/SO.git] / models / openmano / python / rift / openmano / rift2openmano.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import argparse
20 import collections
21 import logging
22 import math
23 import os
24 import sys
25 import tempfile
26 import yaml
27
28 import gi
29 gi.require_version('RwYang', '1.0')
30 gi.require_version('RwVnfdYang', '1.0')
31 gi.require_version('RwNsdYang', '1.0')
32
33 from gi.repository import (
34 RwYang,
35 RwVnfdYang,
36 RwNsdYang,
37 )
38
39 import rift.package.store
40 import rift.package.cloud_init
41
42 logger = logging.getLogger("rift2openmano.py")
43
44
45 class VNFNotFoundError(Exception):
46 pass
47
48
49 class RiftNSD(object):
50 model = RwYang.Model.create_libncx()
51 model.load_module('nsd')
52 model.load_module('rw-nsd')
53
54 def __init__(self, descriptor):
55 self._nsd = descriptor
56
57 def __str__(self):
58 return str(self._nsd)
59
60 @property
61 def name(self):
62 return self._nsd.name
63
64 @property
65 def id(self):
66 return self._nsd.id
67
68 @property
69 def vnfd_ids(self):
70 return [c.vnfd_id_ref for c in self._nsd.constituent_vnfd]
71
72 @property
73 def constituent_vnfds(self):
74 return self._nsd.constituent_vnfd
75
76 @property
77 def vlds(self):
78 return self._nsd.vld
79
80 @property
81 def cps(self):
82 return self._nsd.connection_point
83
84 @property
85 def description(self):
86 return self._nsd.description
87
88 @classmethod
89 def from_xml_file_hdl(cls, hdl):
90 hdl.seek(0)
91 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
92 descriptor.from_xml_v2(RiftNSD.model, hdl.read())
93 return cls(descriptor)
94
95 @classmethod
96 def from_yaml_file_hdl(cls, hdl):
97 hdl.seek(0)
98 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
99 descriptor.from_yaml(RiftNSD.model, hdl.read())
100 return cls(descriptor)
101
102 @classmethod
103 def from_dict(cls, nsd_dict):
104 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd.from_dict(nsd_dict)
105 return cls(descriptor)
106
107
108 class RiftVNFD(object):
109 model = RwYang.Model.create_libncx()
110 model.load_module('vnfd')
111 model.load_module('rw-vnfd')
112
113 def __init__(self, descriptor):
114 self._vnfd = descriptor
115
116 def __str__(self):
117 return str(self._vnfd)
118
119 @property
120 def id(self):
121 return self._vnfd.id
122
123 @property
124 def name(self):
125 return self._vnfd.name
126
127 @property
128 def description(self):
129 return self._vnfd.description
130
131 @property
132 def cps(self):
133 return self._vnfd.connection_point
134
135 @property
136 def vdus(self):
137 return self._vnfd.vdu
138
139 @property
140 def internal_vlds(self):
141 return self._vnfd.internal_vld
142
143 @classmethod
144 def from_xml_file_hdl(cls, hdl):
145 hdl.seek(0)
146 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
147 descriptor.from_xml_v2(RiftVNFD.model, hdl.read())
148 return cls(descriptor)
149
150 @classmethod
151 def from_yaml_file_hdl(cls, hdl):
152 hdl.seek(0)
153 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
154 descriptor.from_yaml(RiftVNFD.model, hdl.read())
155 return cls(descriptor)
156
157 @classmethod
158 def from_dict(cls, vnfd_dict):
159 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd.from_dict(vnfd_dict)
160 return cls(descriptor)
161
162
163 def is_writable_directory(dir_path):
164 """ Returns True if dir_path is writable, False otherwise
165
166 Arguments:
167 dir_path - A directory path
168 """
169 if not os.path.exists(dir_path):
170 raise ValueError("Directory does not exist: %s", dir_path)
171
172 try:
173 testfile = tempfile.TemporaryFile(dir=dir_path)
174 testfile.close()
175 except OSError:
176 return False
177
178 return True
179
180
181 def create_vnfd_from_files(vnfd_file_hdls):
182 """ Create a list of RiftVNFD instances from xml/yaml file handles
183
184 Arguments:
185 vnfd_file_hdls - Rift VNFD XML/YAML file handles
186
187 Returns:
188 A list of RiftVNFD instances
189 """
190 vnfd_dict = {}
191 for vnfd_file_hdl in vnfd_file_hdls:
192 if vnfd_file_hdl.name.endswith("yaml") or vnfd_file_hdl.name.endswith("yaml"):
193 vnfd = RiftVNFD.from_yaml_file_hdl(vnfd_file_hdl)
194 else:
195 vnfd = RiftVNFD.from_xml_file_hdl(vnfd_file_hdl)
196 vnfd_dict[vnfd.id] = vnfd
197
198 return vnfd_dict
199
200
201 def create_nsd_from_file(nsd_file_hdl):
202 """ Create a list of RiftNSD instances from yaml/xml file handles
203
204 Arguments:
205 nsd_file_hdls - Rift NSD XML/yaml file handles
206
207 Returns:
208 A list of RiftNSD instances
209 """
210 if nsd_file_hdl.name.endswith("yaml") or nsd_file_hdl.name.endswith("yaml"):
211 nsd = RiftNSD.from_yaml_file_hdl(nsd_file_hdl)
212 else:
213 nsd = RiftNSD.from_xml_file_hdl(nsd_file_hdl)
214 return nsd
215
216
217 def ddict():
218 return collections.defaultdict(dict)
219
220 def convert_vnfd_name(vnfd_name, member_idx):
221 return vnfd_name + "__" + str(member_idx)
222
223
224 def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids, rift_vnfd_id=None):
225 if rift_vnfd_id is None:
226 for vnfd_id in rift_nsd.vnfd_ids:
227 if vnfd_id not in rift_vnfds:
228 raise VNFNotFoundError("VNF id %s not provided" % vnfd_id)
229
230 openmano = {}
231 openmano["name"] = rift_nsd.name
232 if rift_vnfd_id is not None:
233 openmano["name"] += "scale1"
234 openmano["description"] = rift_nsd.description
235 topology = {}
236 openmano["topology"] = topology
237
238 topology["nodes"] = {}
239 for vnfd in rift_nsd.constituent_vnfds:
240 vnfd_id = vnfd.vnfd_id_ref
241 if rift_vnfd_id is not None and rift_vnfd_id != vnfd_id:
242 continue
243 rift_vnfd = rift_vnfds[vnfd_id]
244 member_idx = vnfd.member_vnf_index
245 openmano_vnfd_id = openmano_vnfd_ids.get(vnfd_id,None)
246 if openmano_vnfd_id:
247 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
248 "type": "VNF",
249 "vnf_id": openmano_vnfd_id
250 }
251 else:
252 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
253 "type": "VNF",
254 "VNF model": rift_vnfd.name
255 }
256
257 for vld in rift_nsd.vlds:
258 # Openmano has both bridge_net and dataplane_net models for network types
259 # For now, since we are using openmano in developer mode lets just hardcode
260 # to bridge_net since it won't matter anyways.
261 # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"}
262 pass
263
264 topology["connections"] = {}
265 for vld in rift_nsd.vlds:
266
267 # Create a connections entry for each external VLD
268 topology["connections"][vld.name] = {}
269 topology["connections"][vld.name]["nodes"] = []
270
271 #if vld.vim_network_name:
272 if True:
273 if vld.name not in topology["nodes"]:
274 topology["nodes"][vld.name] = {
275 "type": "external_network",
276 "model": vld.name,
277 }
278
279 # Add the external network to the list of connection points
280 topology["connections"][vld.name]["nodes"].append(
281 {vld.name: "0"}
282 )
283 elif vld.provider_network.has_field("physical_network"):
284 # Add the external datacenter network to the topology
285 # node list if it isn't already added
286 ext_net_name = vld.provider_network.physical_network
287 ext_net_name_with_seg = ext_net_name
288 if vld.provider_network.has_field("segmentation_id"):
289 ext_net_name_with_seg += ":{}".format(vld.provider_network.segmentation_id)
290
291 if ext_net_name not in topology["nodes"]:
292 topology["nodes"][ext_net_name] = {
293 "type": "external_network",
294 "model": ext_net_name_with_seg,
295 }
296
297 # Add the external network to the list of connection points
298 topology["connections"][vld.name]["nodes"].append(
299 {ext_net_name: "0"}
300 )
301
302
303 for vnfd_cp in vld.vnfd_connection_point_ref:
304
305 # Get the RIFT VNF for this external VLD connection point
306 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
307
308 # For each VNF in this connection, use the same interface name
309 topology["connections"][vld.name]["type"] = "link"
310 # Vnf ref is the vnf name with the member_vnf_idx appended
311 member_idx = vnfd_cp.member_vnf_index_ref
312 vnf_ref = vnfd.name + "__" + str(member_idx)
313 topology["connections"][vld.name]["nodes"].append(
314 {
315 vnf_ref: vnfd_cp.vnfd_connection_point_ref
316 }
317 )
318 return openmano
319
320 def rift2openmano_vnfd_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids,rift_vnfd_id=None):
321
322
323 if rift_vnfd_id not in rift_vnfds:
324 print ("IDS", rift_vnfds)
325 raise VNFNotFoundError("VNF id %s not provided" % rift_vnfd_id)
326
327 openmano_vnfd_nsd = {}
328 openmano_vnfd_nsd["name"] = rift_vnfd_id+'__'+'scaling_group'
329 openmano_vnfd_nsd["description"] = "Scaling Group"
330 topology = {}
331 openmano_vnfd_nsd["topology"] = topology
332 topology["connections"] = {}
333 topology["nodes"] = {}
334 tst_index = []
335 openmano_vnfd_id = openmano_vnfd_ids.get(rift_vnfd_id,None)
336 logger.debug("CONSTVNF %s", rift_nsd.constituent_vnfds)
337 for rvnfd_id in rift_nsd.constituent_vnfds:
338 if rvnfd_id.vnfd_id_ref == rift_vnfd_id:
339 topology["nodes"][rift_vnfd_id+'__'+str(rvnfd_id.member_vnf_index)] = {
340 "type": "VNF",
341 "vnf_id": openmano_vnfd_id
342 }
343
344 for vld in rift_nsd.vlds:
345
346 # Create a connections entry for each external VLD
347 topology["connections"][vld.name] = {}
348 topology["connections"][vld.name]["nodes"] = []
349 if True:
350 if vld.name not in topology["nodes"]:
351 topology["nodes"][vld.name] = {
352 "type": "external_network",
353 "model": vld.name,
354 }
355 topology["connections"][vld.name]["nodes"].append(
356 {vld.name: "0"}
357 )
358
359
360
361 for vnfd_cp in vld.vnfd_connection_point_ref:
362 if not rift_vnfd_id in vnfd_cp.vnfd_id_ref:
363 continue
364 if rift_vnfd_id in vnfd_cp.vnfd_id_ref:
365
366 # Get the RIFT VNF for this external VLD connection point
367 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
368
369
370 # For each VNF in this connection, use the same interface name
371 topology["connections"][vld.name]["type"] = "link"
372 # Vnf ref is the vnf name with the member_vnf_idx appended
373 member_idx = vnfd_cp.member_vnf_index_ref
374 vnf_ref = rift_vnfd_id + "__" + str(member_idx)
375 topology["connections"][vld.name]["nodes"].append(
376 {
377 vnf_ref: vnfd_cp.vnfd_connection_point_ref
378 }
379 )
380 return openmano_vnfd_nsd
381
382
383 def cloud_init(rift_vnfd_id, vdu):
384 """ Populate cloud_init with script from
385 either the inline contents or from the file provided
386 """
387 vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger)
388
389 cloud_init_msg = None
390 if vdu.cloud_init is not None:
391 logger.debug("cloud_init script provided inline %s", vdu.cloud_init)
392 cloud_init_msg = vdu.cloud_init
393 elif vdu.cloud_init_file is not None:
394 # Get cloud-init script contents from the file provided in the cloud_init_file param
395 logger.debug("cloud_init script provided in file %s", vdu.cloud_init_file)
396 filename = vdu.cloud_init_file
397 vnfd_package_store.refresh()
398 stored_package = vnfd_package_store.get_package(rift_vnfd_id)
399 cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger)
400 try:
401 cloud_init_msg = cloud_init_extractor.read_script(stored_package, filename)
402 except rift.package.cloud_init.CloudInitExtractionError as e:
403 raise ValueError(e)
404 else:
405 logger.debug("VDU translation: cloud-init script not provided")
406 return
407
408 logger.debug("Current cloud init msg is {}".format(cloud_init_msg))
409 return cloud_init_msg
410
411 def config_file_init(rift_vnfd_id, vdu, cfg_file):
412 """ Populate config file init with file provided
413 """
414 vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger)
415
416 # Get script contents from the file provided in the cloud_init directory
417 logger.debug("config file script provided in file {}".format(cfg_file))
418 filename = cfg_file
419 vnfd_package_store.refresh()
420 stored_package = vnfd_package_store.get_package(rift_vnfd_id)
421 cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger)
422 try:
423 cfg_file_msg = cloud_init_extractor.read_script(stored_package, filename)
424 except rift.package.cloud_init.CloudInitExtractionError as e:
425 raise ValueError(e)
426
427 logger.debug("Current config file msg is {}".format(cfg_file_msg))
428 return cfg_file_msg
429
430 def rift2openmano_vnfd(rift_vnfd, rift_nsd):
431 openmano_vnf = {"vnf":{}}
432 vnf = openmano_vnf["vnf"]
433
434 vnf["name"] = rift_vnfd.name
435 vnf["description"] = rift_vnfd.description
436
437 vnf["external-connections"] = []
438
439 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name):
440 for vdu in rift_vnfd.vdus:
441 for ext_if in vdu.external_interface:
442 if ext_if.vnfd_connection_point_ref == cp_ref_name:
443 return vdu, ext_if
444
445 raise ValueError("External connection point reference %s not found" % cp_ref_name)
446
447 def find_vdu_and_int_if_by_cp_ref(cp_ref_id):
448 for vdu in rift_vnfd.vdus:
449 for int_if in vdu.internal_interface:
450 if int_if.vdu_internal_connection_point_ref == cp_ref_id:
451 return vdu, int_if
452
453 raise ValueError("Internal connection point reference %s not found" % cp_ref_id)
454
455 def rift2openmano_if_type(ext_if):
456
457 cp_ref_name = ext_if.vnfd_connection_point_ref
458 for vld in rift_nsd.vlds:
459
460 # if it is an explicit mgmt_network then check if the given
461 # cp_ref is a part of it
462 if not vld.mgmt_network:
463 continue
464
465 for vld_cp in vld.vnfd_connection_point_ref:
466 if vld_cp.vnfd_connection_point_ref == cp_ref_name:
467 return "mgmt"
468
469
470 rift_type = ext_if.virtual_interface.type_yang
471 # Retaining it for backward compatibility!
472 if rift_type == "OM_MGMT":
473 return "mgmt"
474 elif rift_type == "VIRTIO" or rift_type == "E1000":
475 return "bridge"
476 else:
477 return "data"
478
479 def rift2openmano_vif(rift_type):
480 if rift_type == "VIRTIO":
481 return "virtio"
482 elif rift_type == "E1000":
483 return "e1000"
484 else:
485 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type))
486
487 # Add all external connections
488 for cp in rift_vnfd.cps:
489 # Find the VDU and and external interface for this connection point
490 vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name)
491 connection = {
492 "name": cp.name,
493 "type": rift2openmano_if_type(ext_if),
494 "VNFC": vdu.name,
495 "local_iface_name": ext_if.name,
496 "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
497 }
498
499 vnf["external-connections"].append(connection)
500
501 # Add all internal networks
502 for vld in rift_vnfd.internal_vlds:
503 connection = {
504 "name": vld.name,
505 "description": vld.description,
506 "type": "bridge",
507 "elements": [],
508 }
509
510 # Add the specific VDU connection points
511 for int_cp in vld.internal_connection_point:
512 vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref)
513 connection["elements"].append({
514 "VNFC": vdu.name,
515 "local_iface_name": int_if.name,
516 })
517 if "internal-connections" not in vnf:
518 vnf["internal-connections"] = []
519
520 vnf["internal-connections"].append(connection)
521
522 # Add VDU's
523 vnf["VNFC"] = []
524 for vdu in rift_vnfd.vdus:
525 vnfc = {
526 "name": vdu.name,
527 "description": vdu.name,
528 "bridge-ifaces": [],
529 }
530
531 if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
532 vnfc["disk"] = vdu.vm_flavor.storage_gb
533
534 if vdu.has_field("image"):
535 if os.path.isabs(vdu.image):
536 vnfc["VNFC image"] = vdu.image
537 else:
538 vnfc["image name"] = vdu.image
539 if vdu.has_field("image_checksum"):
540 vnfc["image checksum"] = vdu.image_checksum
541
542 dedicated_int = False
543 for intf in list(vdu.internal_interface) + list(vdu.external_interface):
544 if intf.virtual_interface.type_yang in ["SR_IOV", "PCI_PASSTHROUGH"]:
545 dedicated_int = True
546 if vdu.guest_epa.has_field("numa_node_policy") or dedicated_int:
547 vnfc["numas"] = [{
548 "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
549 "interfaces":[],
550 }]
551 numa_node_policy = vdu.guest_epa.numa_node_policy
552 if numa_node_policy.has_field("node"):
553 numa_node = numa_node_policy.node[0]
554
555 if numa_node.has_field("paired_threads"):
556 if numa_node.paired_threads.has_field("num_paired_threads"):
557 vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
558 if len(numa_node.paired_threads.paired_thread_ids) > 0:
559 vnfc["numas"][0]["paired-threads-id"] = []
560 for pair in numa_node.paired_threads.paired_thread_ids:
561 vnfc["numas"][0]["paired-threads-id"].append(
562 [pair.thread_a, pair.thread_b]
563 )
564
565 else:
566 if vdu.vm_flavor.has_field("vcpu_count"):
567 vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
568
569 else:
570 if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count:
571 vnfc["vcpus"] = vdu.vm_flavor.vcpu_count
572
573 if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb:
574 vnfc["ram"] = vdu.vm_flavor.memory_mb
575
576
577 if vdu.has_field("hypervisor_epa"):
578 vnfc["hypervisor"] = {}
579 if vdu.hypervisor_epa.has_field("type"):
580 if vdu.hypervisor_epa.type_yang == "REQUIRE_KVM":
581 vnfc["hypervisor"]["type"] = "QEMU-kvm"
582
583 if vdu.hypervisor_epa.has_field("version"):
584 vnfc["hypervisor"]["version"] = vdu.hypervisor_epa.version
585
586 if vdu.has_field("host_epa"):
587 vnfc["processor"] = {}
588 if vdu.host_epa.has_field("om_cpu_model_string"):
589 vnfc["processor"]["model"] = vdu.host_epa.om_cpu_model_string
590 if vdu.host_epa.has_field("om_cpu_feature"):
591 vnfc["processor"]["features"] = []
592 for feature in vdu.host_epa.om_cpu_feature:
593 vnfc["processor"]["features"].append(feature.feature)
594
595 if vdu.has_field("volumes"):
596 vnfc["devices"] = []
597 # Sort volumes as device-list is implictly ordered by Openmano
598 newvollist = sorted(vdu.volumes, key=lambda k: k.name)
599 for iter_num, volume in enumerate(newvollist):
600 if iter_num == 0:
601 # Convert the first volume to vnfc.image
602 if os.path.isabs(volume.image):
603 vnfc["VNFC image"] = volume.image
604 else:
605 vnfc["image name"] = volume.image
606 if volume.has_field("image_checksum"):
607 vnfc["image checksum"] = volume.image_checksum
608 else:
609 # Add Openmano devices
610 device = {}
611 device["type"] = volume.device_type
612 device["image"] = volume.image
613 vnfc["devices"].append(device)
614
615 vnfc_boot_data_init = False
616 if vdu.has_field("cloud_init") or vdu.has_field("cloud_init_file"):
617 vnfc['boot-data'] = dict()
618 vnfc_boot_data_init = True
619 vnfc['boot-data']['user-data'] = cloud_init(rift_vnfd.id, vdu)
620
621 if vdu.has_field("supplemental_boot_data"):
622 if vdu.supplemental_boot_data.has_field('boot_data_drive'):
623 if vdu.supplemental_boot_data.boot_data_drive is True:
624 if vnfc_boot_data_init is False:
625 vnfc['boot-data'] = dict()
626 vnfc_boot_data_init = True
627 vnfc['boot-data']['boot-data-drive'] = vdu.supplemental_boot_data.boot_data_drive
628
629 if vdu.supplemental_boot_data.has_field('config_file'):
630 om_cfgfile_list = list()
631 for custom_config_file in vdu.supplemental_boot_data.config_file:
632 cfg_source = config_file_init(rift_vnfd.id, vdu, custom_config_file.source)
633 om_cfgfile_list.append({"dest":custom_config_file.dest, "content": cfg_source})
634 vnfc['boot-data']['config-files'] = om_cfgfile_list
635
636
637 vnf["VNFC"].append(vnfc)
638
639 for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
640 intf = {
641 "name": int_if.name,
642 }
643 if int_if.virtual_interface.has_field("vpci"):
644 intf["vpci"] = int_if.virtual_interface.vpci
645
646 if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]:
647 intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang)
648 vnfc["bridge-ifaces"].append(intf)
649
650 elif int_if.virtual_interface.type_yang in ["OM_MGMT"]:
651 vnfc["bridge-ifaces"].append(intf)
652
653 elif int_if.virtual_interface.type_yang == "SR_IOV":
654 intf["bandwidth"] = "10 Gbps"
655 intf["dedicated"] = "no"
656 vnfc["numas"][0]["interfaces"].append(intf)
657
658 elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH":
659 intf["bandwidth"] = "10 Gbps"
660 intf["dedicated"] = "yes"
661 if "interfaces" not in vnfc["numas"][0]:
662 vnfc["numas"][0]["interfaces"] = []
663 vnfc["numas"][0]["interfaces"].append(intf)
664 else:
665 raise ValueError("Interface type %s not supported" % int_if.virtual_interface)
666
667 if int_if.virtual_interface.has_field("bandwidth"):
668 if int_if.virtual_interface.bandwidth != 0:
669 bps = int_if.virtual_interface.bandwidth
670
671 # Calculate the bits per second conversion
672 for x in [('M', 1000000), ('G', 1000000000)]:
673 if bps/x[1] >= 1:
674 intf["bandwidth"] = "{} {}bps".format(math.ceil(bps/x[1]), x[0])
675
676 # Sort bridge-ifaces-list TODO sort others
677 newlist = sorted(vnfc["bridge-ifaces"], key=lambda k: k['name'])
678 vnfc["bridge-ifaces"] = newlist
679
680 return openmano_vnf
681
682
683 def parse_args(argv=sys.argv[1:]):
684 """ Parse the command line arguments
685
686 Arguments:
687 arv - The list of arguments to parse
688
689 Returns:
690 Argparse Namespace instance
691 """
692 parser = argparse.ArgumentParser()
693 parser.add_argument(
694 '-o', '--outdir',
695 default='-',
696 help="Directory to output converted descriptors. Default is stdout",
697 )
698
699 parser.add_argument(
700 '-n', '--nsd-file-hdl',
701 metavar="nsd_file",
702 type=argparse.FileType('r'),
703 help="Rift NSD Descriptor File",
704 )
705
706 parser.add_argument(
707 '-v', '--vnfd-file-hdls',
708 metavar="vnfd_file",
709 action='append',
710 type=argparse.FileType('r'),
711 help="Rift VNFD Descriptor File",
712 )
713
714 args = parser.parse_args(argv)
715
716 if not os.path.exists(args.outdir):
717 os.makedirs(args.outdir)
718
719 if not is_writable_directory(args.outdir):
720 logging.error("Directory %s is not writable", args.outdir)
721 sys.exit(1)
722
723 return args
724
725
726 def write_yaml_to_file(name, outdir, desc_dict):
727 file_name = "%s.yaml" % name
728 yaml_str = yaml.dump(desc_dict)
729 if outdir == "-":
730 sys.stdout.write(yaml_str)
731 return
732
733 file_path = os.path.join(outdir, file_name)
734 dir_path = os.path.dirname(file_path)
735 if not os.path.exists(dir_path):
736 os.makedirs(dir_path)
737
738 with open(file_path, "w") as hdl:
739 hdl.write(yaml_str)
740
741 logger.info("Wrote descriptor to %s", file_path)
742
743
744 def main(argv=sys.argv[1:]):
745 args = parse_args(argv)
746 nsd = None
747 openmano_vnfr_ids = dict()
748 vnf_dict = None
749 if args.vnfd_file_hdls is not None:
750 vnf_dict = create_vnfd_from_files(args.vnfd_file_hdls)
751
752 for vnfd in vnf_dict:
753 openmano_vnfr_ids[vnfd] = vnfd
754
755 if args.nsd_file_hdl is not None:
756 nsd = create_nsd_from_file(args.nsd_file_hdl)
757
758 openmano_nsd = rift2openmano_nsd(nsd, vnf_dict, openmano_vnfr_ids,rift_vnfd_id)
759 vnfd_nsd = rift2openmano_vnfd_nsd(nsd, vnf_dict, openmano_vnfr_ids, rift_vnfd_id)
760 write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd)
761 write_yaml_to_file(vnfd_nsd["name"], args.outdir, vnfd_nsd)
762 for vnf in vnf_dict.values():
763 openmano_vnf = rift2openmano_vnfd(vnf, nsd)
764 write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf)
765
766
767 if __name__ == "__main__":
768 logging.basicConfig(level=logging.WARNING)
769 main()