4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
29 gi
.require_version('RwYang', '1.0')
30 gi
.require_version('RwVnfdYang', '1.0')
31 gi
.require_version('RwNsdYang', '1.0')
33 from gi
.repository
import (
39 import rift
.package
.store
40 import rift
.package
.cloud_init
42 logger
= logging
.getLogger("rift2openmano.py")
45 class VNFNotFoundError(Exception):
49 class RiftNSD(object):
50 model
= RwYang
.Model
.create_libncx()
51 model
.load_module('nsd')
52 model
.load_module('rw-nsd')
54 def __init__(self
, descriptor
):
55 self
._nsd
= descriptor
70 return [c
.vnfd_id_ref
for c
in self
._nsd
.constituent_vnfd
]
73 def constituent_vnfds(self
):
74 return self
._nsd
.constituent_vnfd
77 def scaling_group_descriptor(self
):
78 return self
._nsd
.scaling_group_descriptor
86 return self
._nsd
.connection_point
89 def description(self
):
90 return self
._nsd
.description
93 def from_xml_file_hdl(cls
, hdl
):
95 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor
.from_xml_v2(RiftNSD
.model
, hdl
.read())
97 return cls(descriptor
)
100 def from_yaml_file_hdl(cls
, hdl
):
102 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
103 descriptor
.from_yaml(RiftNSD
.model
, hdl
.read())
104 return cls(descriptor
)
107 def from_dict(cls
, nsd_dict
):
108 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
.from_dict(nsd_dict
)
109 return cls(descriptor
)
112 class RiftVNFD(object):
113 model
= RwYang
.Model
.create_libncx()
114 model
.load_module('vnfd')
115 model
.load_module('rw-vnfd')
117 def __init__(self
, descriptor
):
118 self
._vnfd
= descriptor
121 return str(self
._vnfd
)
129 return self
._vnfd
.name
132 def description(self
):
133 return self
._vnfd
.description
137 return self
._vnfd
.connection_point
141 return self
._vnfd
.vdu
144 def internal_vlds(self
):
145 return self
._vnfd
.internal_vld
148 def from_xml_file_hdl(cls
, hdl
):
150 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor
.from_xml_v2(RiftVNFD
.model
, hdl
.read())
152 return cls(descriptor
)
155 def from_yaml_file_hdl(cls
, hdl
):
157 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
158 descriptor
.from_yaml(RiftVNFD
.model
, hdl
.read())
159 return cls(descriptor
)
162 def from_dict(cls
, vnfd_dict
):
163 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
.from_dict(vnfd_dict
)
164 return cls(descriptor
)
167 def is_writable_directory(dir_path
):
168 """ Returns True if dir_path is writable, False otherwise
171 dir_path - A directory path
173 if not os
.path
.exists(dir_path
):
174 raise ValueError("Directory does not exist: %s", dir_path
)
177 testfile
= tempfile
.TemporaryFile(dir=dir_path
)
185 def create_vnfd_from_files(vnfd_file_hdls
):
186 """ Create a list of RiftVNFD instances from xml/yaml file handles
189 vnfd_file_hdls - Rift VNFD XML/YAML file handles
192 A list of RiftVNFD instances
195 for vnfd_file_hdl
in vnfd_file_hdls
:
196 if vnfd_file_hdl
.name
.endswith("yaml") or vnfd_file_hdl
.name
.endswith("yaml"):
197 vnfd
= RiftVNFD
.from_yaml_file_hdl(vnfd_file_hdl
)
199 vnfd
= RiftVNFD
.from_xml_file_hdl(vnfd_file_hdl
)
200 vnfd_dict
[vnfd
.id] = vnfd
205 def create_nsd_from_file(nsd_file_hdl
):
206 """ Create a list of RiftNSD instances from yaml/xml file handles
209 nsd_file_hdls - Rift NSD XML/yaml file handles
212 A list of RiftNSD instances
214 if nsd_file_hdl
.name
.endswith("yaml") or nsd_file_hdl
.name
.endswith("yaml"):
215 nsd
= RiftNSD
.from_yaml_file_hdl(nsd_file_hdl
)
217 nsd
= RiftNSD
.from_xml_file_hdl(nsd_file_hdl
)
222 return collections
.defaultdict(dict)
224 def convert_vnfd_name(vnfd_name
, member_idx
):
225 return vnfd_name
+ "__" + str(member_idx
)
228 def rift2openmano_nsd(rift_nsd
, rift_vnfds
, openmano_vnfd_ids
, rift_vnfd_id
=None):
229 if rift_vnfd_id
is None:
230 for vnfd_id
in rift_nsd
.vnfd_ids
:
231 if vnfd_id
not in rift_vnfds
:
232 raise VNFNotFoundError("VNF id %s not provided" % vnfd_id
)
235 openmano
["name"] = rift_nsd
.name
236 if rift_vnfd_id
is not None:
237 for scaling_groups
in rift_nsd
.scaling_group_descriptor
:
238 openmano
["name"] += scaling_groups
.name
239 openmano
["description"] = rift_nsd
.description
241 openmano
["topology"] = topology
243 topology
["nodes"] = {}
244 for vnfd
in rift_nsd
.constituent_vnfds
:
245 vnfd_id
= vnfd
.vnfd_id_ref
246 if rift_vnfd_id
is not None and rift_vnfd_id
!= vnfd_id
:
248 rift_vnfd
= rift_vnfds
[vnfd_id
]
249 member_idx
= vnfd
.member_vnf_index
250 openmano_vnfd_id
= openmano_vnfd_ids
.get(vnfd_id
,None)
252 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
254 "vnf_id": openmano_vnfd_id
257 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
259 "VNF model": rift_vnfd
.name
262 for vld
in rift_nsd
.vlds
:
263 # Openmano has both bridge_net and dataplane_net models for network types
264 # For now, since we are using openmano in developer mode lets just hardcode
265 # to bridge_net since it won't matter anyways.
266 # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"}
269 topology
["connections"] = {}
270 for vld
in rift_nsd
.vlds
:
272 # Create a connections entry for each external VLD
273 topology
["connections"][vld
.name
] = {}
274 topology
["connections"][vld
.name
]["nodes"] = []
276 #if vld.vim_network_name:
278 if vld
.name
not in topology
["nodes"]:
279 topology
["nodes"][vld
.name
] = {
280 "type": "external_network",
284 # Add the external network to the list of connection points
285 topology
["connections"][vld
.name
]["nodes"].append(
288 elif vld
.provider_network
.has_field("physical_network"):
289 # Add the external datacenter network to the topology
290 # node list if it isn't already added
291 ext_net_name
= vld
.provider_network
.physical_network
292 ext_net_name_with_seg
= ext_net_name
293 if vld
.provider_network
.has_field("segmentation_id"):
294 ext_net_name_with_seg
+= ":{}".format(vld
.provider_network
.segmentation_id
)
296 if ext_net_name
not in topology
["nodes"]:
297 topology
["nodes"][ext_net_name
] = {
298 "type": "external_network",
299 "model": ext_net_name_with_seg
,
302 # Add the external network to the list of connection points
303 topology
["connections"][vld
.name
]["nodes"].append(
308 for vnfd_cp
in vld
.vnfd_connection_point_ref
:
310 # Get the RIFT VNF for this external VLD connection point
311 vnfd
= rift_vnfds
[vnfd_cp
.vnfd_id_ref
]
313 # For each VNF in this connection, use the same interface name
314 topology
["connections"][vld
.name
]["type"] = "link"
315 # Vnf ref is the vnf name with the member_vnf_idx appended
316 member_idx
= vnfd_cp
.member_vnf_index_ref
317 vnf_ref
= vnfd
.name
+ "__" + str(member_idx
)
318 topology
["connections"][vld
.name
]["nodes"].append(
320 vnf_ref
: vnfd_cp
.vnfd_connection_point_ref
325 def rift2openmano_vnfd_nsd(rift_nsd
, rift_vnfds
, openmano_vnfd_ids
,rift_vnfd_id
=None):
327 if rift_vnfd_id
not in rift_vnfds
:
328 raise VNFNotFoundError("VNF id %s not provided" % rift_vnfd_id
)
330 openmano_vnfd_nsd
= {}
331 for groups
in rift_nsd
.scaling_group_descriptor
:
332 openmano_vnfd_nsd
["name"] = rift_vnfd_id
+'__'+'scaling_group'+'__'+groups
.name
333 openmano_vnfd_nsd
["description"] = "Scaling Group"
335 openmano_vnfd_nsd
["topology"] = topology
336 topology
["connections"] = {}
337 topology
["nodes"] = {}
339 openmano_vnfd_id
= openmano_vnfd_ids
.get(rift_vnfd_id
,None)
340 for rvnfd_id
in rift_nsd
.constituent_vnfds
:
341 if rvnfd_id
.vnfd_id_ref
== rift_vnfd_id
:
342 rift_vnfd
= rift_vnfds
[rift_vnfd_id
]
343 topology
["nodes"][rift_vnfd
.name
+'__'+str(rvnfd_id
.member_vnf_index
)] = {
345 "vnf_id": openmano_vnfd_id
348 for vld
in rift_nsd
.vlds
:
350 # Create a connections entry for each external VLD
351 topology
["connections"][vld
.name
] = {}
352 topology
["connections"][vld
.name
]["nodes"] = []
354 if vld
.name
not in topology
["nodes"]:
355 topology
["nodes"][vld
.name
] = {
356 "type": "external_network",
359 topology
["connections"][vld
.name
]["nodes"].append(
365 for vnfd_cp
in vld
.vnfd_connection_point_ref
:
366 if not rift_vnfd_id
in vnfd_cp
.vnfd_id_ref
:
368 if rift_vnfd_id
in vnfd_cp
.vnfd_id_ref
:
370 # Get the RIFT VNF for this external VLD connection point
371 vnfd
= rift_vnfds
[vnfd_cp
.vnfd_id_ref
]
374 # For each VNF in this connection, use the same interface name
375 topology
["connections"][vld
.name
]["type"] = "link"
376 # Vnf ref is the vnf name with the member_vnf_idx appended
377 member_idx
= vnfd_cp
.member_vnf_index_ref
378 vnf_ref
= vnfd
.name
+ "__" + str(member_idx
)
379 topology
["connections"][vld
.name
]["nodes"].append(
381 vnf_ref
: vnfd_cp
.vnfd_connection_point_ref
384 return openmano_vnfd_nsd
387 def cloud_init(rift_vnfd_id
, vdu
):
388 """ Populate cloud_init with script from
389 either the inline contents or from the file provided
391 vnfd_package_store
= rift
.package
.store
.VnfdPackageFilesystemStore(logger
)
393 cloud_init_msg
= None
394 if vdu
.cloud_init
is not None:
395 logger
.debug("cloud_init script provided inline %s", vdu
.cloud_init
)
396 cloud_init_msg
= vdu
.cloud_init
397 elif vdu
.cloud_init_file
is not None:
398 # Get cloud-init script contents from the file provided in the cloud_init_file param
399 logger
.debug("cloud_init script provided in file %s", vdu
.cloud_init_file
)
400 filename
= vdu
.cloud_init_file
401 vnfd_package_store
.refresh()
402 stored_package
= vnfd_package_store
.get_package(rift_vnfd_id
)
403 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(logger
)
405 cloud_init_msg
= cloud_init_extractor
.read_script(stored_package
, filename
)
406 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
409 logger
.debug("VDU translation: cloud-init script not provided")
412 logger
.debug("Current cloud init msg is {}".format(cloud_init_msg
))
413 return cloud_init_msg
415 def config_file_init(rift_vnfd_id
, vdu
, cfg_file
):
416 """ Populate config file init with file provided
418 vnfd_package_store
= rift
.package
.store
.VnfdPackageFilesystemStore(logger
)
420 # Get script contents from the file provided in the cloud_init directory
421 logger
.debug("config file script provided in file {}".format(cfg_file
))
423 vnfd_package_store
.refresh()
424 stored_package
= vnfd_package_store
.get_package(rift_vnfd_id
)
425 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(logger
)
427 cfg_file_msg
= cloud_init_extractor
.read_script(stored_package
, filename
)
428 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
431 logger
.debug("Current config file msg is {}".format(cfg_file_msg
))
434 def rift2openmano_vnfd(rift_vnfd
, rift_nsd
):
435 openmano_vnf
= {"vnf":{}}
436 vnf
= openmano_vnf
["vnf"]
438 vnf
["name"] = rift_vnfd
.name
439 vnf
["description"] = rift_vnfd
.description
441 vnf
["external-connections"] = []
443 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name
):
444 for vdu
in rift_vnfd
.vdus
:
445 for ext_if
in vdu
.external_interface
:
446 if ext_if
.vnfd_connection_point_ref
== cp_ref_name
:
449 raise ValueError("External connection point reference %s not found" % cp_ref_name
)
451 def find_vdu_and_int_if_by_cp_ref(cp_ref_id
):
452 for vdu
in rift_vnfd
.vdus
:
453 for int_if
in vdu
.internal_interface
:
454 if int_if
.vdu_internal_connection_point_ref
== cp_ref_id
:
457 raise ValueError("Internal connection point reference %s not found" % cp_ref_id
)
459 def rift2openmano_if_type(ext_if
):
461 cp_ref_name
= ext_if
.vnfd_connection_point_ref
462 for vld
in rift_nsd
.vlds
:
464 # if it is an explicit mgmt_network then check if the given
465 # cp_ref is a part of it
466 if not vld
.mgmt_network
:
469 for vld_cp
in vld
.vnfd_connection_point_ref
:
470 if vld_cp
.vnfd_connection_point_ref
== cp_ref_name
:
474 rift_type
= ext_if
.virtual_interface
.type_yang
475 # Retaining it for backward compatibility!
476 if rift_type
== "OM_MGMT":
478 elif rift_type
== "VIRTIO" or rift_type
== "E1000":
483 def rift2openmano_vif(rift_type
):
484 if rift_type
== "VIRTIO":
486 elif rift_type
== "E1000":
489 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type
))
491 # Add all external connections
492 cp_to_port_security_map
= {}
494 for cp
in rift_vnfd
.cps
:
495 # Find the VDU and and external interface for this connection point
496 vdu
, ext_if
= find_vdu_and_ext_if_by_cp_ref(cp
.name
)
499 "type": rift2openmano_if_type(ext_if
),
501 "local_iface_name": ext_if
.name
,
502 "description": "%s iface on VDU %s" % (ext_if
.name
, vdu
.name
),
505 if cp
.has_field('port_security_enabled'):
506 cp_to_port_security_map
[cp
.name
] = cp
.port_security_enabled
507 vnf
["external-connections"].append(connection
)
509 # Add all internal networks
510 for vld
in rift_vnfd
.internal_vlds
:
513 "description": vld
.description
,
518 # Add the specific VDU connection points
519 for int_cp
in vld
.internal_connection_point
:
520 vdu
, int_if
= find_vdu_and_int_if_by_cp_ref(int_cp
.id_ref
)
521 connection
["elements"].append({
523 "local_iface_name": int_if
.name
,
525 if "internal-connections" not in vnf
:
526 vnf
["internal-connections"] = []
528 vnf
["internal-connections"].append(connection
)
532 for vdu
in rift_vnfd
.vdus
:
535 "description": vdu
.name
,
539 if vdu
.vm_flavor
.has_field("storage_gb") and vdu
.vm_flavor
.storage_gb
:
540 vnfc
["disk"] = vdu
.vm_flavor
.storage_gb
542 if vdu
.has_field("image"):
543 if os
.path
.isabs(vdu
.image
):
544 vnfc
["VNFC image"] = vdu
.image
546 vnfc
["image name"] = vdu
.image
547 if vdu
.has_field("image_checksum"):
548 vnfc
["image checksum"] = vdu
.image_checksum
550 dedicated_int
= False
551 for intf
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
552 if intf
.virtual_interface
.type_yang
in ["SR_IOV", "PCI_PASSTHROUGH"]:
554 if vdu
.guest_epa
.has_field("numa_node_policy") or dedicated_int
:
556 "memory": max(int(vdu
.vm_flavor
.memory_mb
/1024), 1),
559 numa_node_policy
= vdu
.guest_epa
.numa_node_policy
560 if numa_node_policy
.has_field("node"):
561 numa_node
= numa_node_policy
.node
[0]
563 if numa_node
.has_field("num_cores"):
564 vnfc
["numas"][0]["cores"] = numa_node
.num_cores
566 if numa_node
.has_field("paired_threads"):
567 if numa_node
.paired_threads
.has_field("num_paired_threads"):
568 vnfc
["numas"][0]["paired-threads"] = numa_node
.paired_threads
.num_paired_threads
569 if len(numa_node
.paired_threads
.paired_thread_ids
) > 0:
570 vnfc
["numas"][0]["paired-threads-id"] = []
571 for pair
in numa_node
.paired_threads
.paired_thread_ids
:
572 vnfc
["numas"][0]["paired-threads-id"].append(
573 [pair
.thread_a
, pair
.thread_b
]
576 if numa_node
.has_field("num_threads"):
577 vnfc
["numas"][0]["threads"] = numa_node
.num_threads
579 if vdu
.vm_flavor
.has_field("vcpu_count"):
580 vnfc
["numas"][0]["cores"] = max(vdu
.vm_flavor
.vcpu_count
, 1)
582 if vdu
.vm_flavor
.has_field("vcpu_count") and vdu
.vm_flavor
.vcpu_count
:
583 vnfc
["vcpus"] = vdu
.vm_flavor
.vcpu_count
585 if vdu
.vm_flavor
.has_field("memory_mb") and vdu
.vm_flavor
.memory_mb
:
586 vnfc
["ram"] = vdu
.vm_flavor
.memory_mb
589 if vdu
.has_field("hypervisor_epa"):
590 vnfc
["hypervisor"] = {}
591 if vdu
.hypervisor_epa
.has_field("type"):
592 if vdu
.hypervisor_epa
.type_yang
== "REQUIRE_KVM":
593 vnfc
["hypervisor"]["type"] = "QEMU-kvm"
595 if vdu
.hypervisor_epa
.has_field("version"):
596 vnfc
["hypervisor"]["version"] = vdu
.hypervisor_epa
.version
598 if vdu
.has_field("host_epa"):
599 vnfc
["processor"] = {}
600 if vdu
.host_epa
.has_field("om_cpu_model_string"):
601 vnfc
["processor"]["model"] = vdu
.host_epa
.om_cpu_model_string
602 if vdu
.host_epa
.has_field("om_cpu_feature"):
603 vnfc
["processor"]["features"] = []
604 for feature
in vdu
.host_epa
.om_cpu_feature
:
605 vnfc
["processor"]["features"].append(feature
.feature
)
607 if vdu
.has_field("volumes"):
609 # Sort volumes as device-list is implictly ordered by Openmano
610 newvollist
= sorted(vdu
.volumes
, key
=lambda k
: k
.name
)
611 for iter_num
, volume
in enumerate(newvollist
):
613 # Convert the first volume to vnfc.image
614 if os
.path
.isabs(volume
.image
):
615 vnfc
["VNFC image"] = volume
.image
617 vnfc
["image name"] = volume
.image
618 if volume
.has_field("image_checksum"):
619 vnfc
["image checksum"] = volume
.image_checksum
621 # Add Openmano devices
623 device
["type"] = volume
.device_type
624 if volume
.has_field("size"):
625 device
["size"] = volume
.size
626 if volume
.has_field("image"):
627 device
["image name"] = volume
.image
628 if volume
.has_field("image_checksum"):
629 device
["image checksum"] = volume
.image_checksum
630 vnfc
["devices"].append(device
)
632 vnfc_boot_data_init
= False
633 if vdu
.has_field("cloud_init") or vdu
.has_field("cloud_init_file"):
634 vnfc
['boot-data'] = dict()
635 vnfc_boot_data_init
= True
636 vnfc
['boot-data']['user-data'] = cloud_init(rift_vnfd
.id, vdu
)
638 if vdu
.has_field("supplemental_boot_data"):
639 if vdu
.supplemental_boot_data
.has_field('boot_data_drive'):
640 if vdu
.supplemental_boot_data
.boot_data_drive
is True:
641 if vnfc_boot_data_init
is False:
642 vnfc
['boot-data'] = dict()
643 vnfc_boot_data_init
= True
644 vnfc
['boot-data']['boot-data-drive'] = vdu
.supplemental_boot_data
.boot_data_drive
646 if vdu
.supplemental_boot_data
.has_field('config_file'):
647 om_cfgfile_list
= list()
648 for custom_config_file
in vdu
.supplemental_boot_data
.config_file
:
649 cfg_source
= config_file_init(rift_vnfd
.id, vdu
, custom_config_file
.source
)
650 om_cfgfile_list
.append({"dest":custom_config_file
.dest
, "content": cfg_source
})
651 vnfc
['boot-data']['config-files'] = om_cfgfile_list
654 vnf
["VNFC"].append(vnfc
)
656 for int_if
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
660 if int_if
.virtual_interface
.has_field("vpci"):
661 intf
["vpci"] = int_if
.virtual_interface
.vpci
663 if int_if
.virtual_interface
.type_yang
in ["VIRTIO", "E1000"]:
664 intf
["model"] = rift2openmano_vif(int_if
.virtual_interface
.type_yang
)
665 vnfc
["bridge-ifaces"].append(intf
)
667 elif int_if
.virtual_interface
.type_yang
in ["OM_MGMT"]:
668 vnfc
["bridge-ifaces"].append(intf
)
670 elif int_if
.virtual_interface
.type_yang
== "SR_IOV":
671 intf
["bandwidth"] = "10 Gbps"
672 intf
["dedicated"] = "no"
673 vnfc
["numas"][0]["interfaces"].append(intf
)
675 elif int_if
.virtual_interface
.type_yang
== "PCI_PASSTHROUGH":
676 intf
["bandwidth"] = "10 Gbps"
677 intf
["dedicated"] = "yes"
678 if "interfaces" not in vnfc
["numas"][0]:
679 vnfc
["numas"][0]["interfaces"] = []
680 vnfc
["numas"][0]["interfaces"].append(intf
)
682 raise ValueError("Interface type %s not supported" % int_if
.virtual_interface
)
684 if int_if
.virtual_interface
.has_field("bandwidth"):
685 if int_if
.virtual_interface
.bandwidth
!= 0:
686 bps
= int_if
.virtual_interface
.bandwidth
688 # Calculate the bits per second conversion
689 for x
in [('M', 1000000), ('G', 1000000000)]:
691 intf
["bandwidth"] = "{} {}bps".format(math
.ceil(bps
/x
[1]), x
[0])
693 for bridge_iface
in vnfc
["bridge-ifaces"]:
694 if bridge_iface
['name'] in cp_to_port_security_map
:
695 bridge_iface
['port-security'] = cp_to_port_security_map
[bridge_iface
['name']]
696 # Sort bridge-ifaces-list TODO sort others
697 newlist
= sorted(vnfc
["bridge-ifaces"], key
=lambda k
: k
['name'])
698 vnfc
["bridge-ifaces"] = newlist
703 def parse_args(argv
=sys
.argv
[1:]):
704 """ Parse the command line arguments
707 arv - The list of arguments to parse
710 Argparse Namespace instance
712 parser
= argparse
.ArgumentParser()
716 help="Directory to output converted descriptors. Default is stdout",
720 '-n', '--nsd-file-hdl',
722 type=argparse
.FileType('r'),
723 help="Rift NSD Descriptor File",
727 '-v', '--vnfd-file-hdls',
730 type=argparse
.FileType('r'),
731 help="Rift VNFD Descriptor File",
734 args
= parser
.parse_args(argv
)
736 if not os
.path
.exists(args
.outdir
):
737 os
.makedirs(args
.outdir
)
739 if not is_writable_directory(args
.outdir
):
740 logging
.error("Directory %s is not writable", args
.outdir
)
746 def write_yaml_to_file(name
, outdir
, desc_dict
):
747 file_name
= "%s.yaml" % name
748 yaml_str
= yaml
.dump(desc_dict
)
750 sys
.stdout
.write(yaml_str
)
753 file_path
= os
.path
.join(outdir
, file_name
)
754 dir_path
= os
.path
.dirname(file_path
)
755 if not os
.path
.exists(dir_path
):
756 os
.makedirs(dir_path
)
758 with
open(file_path
, "w") as hdl
:
761 logger
.info("Wrote descriptor to %s", file_path
)
764 def main(argv
=sys
.argv
[1:]):
765 args
= parse_args(argv
)
767 openmano_vnfr_ids
= dict()
769 if args
.vnfd_file_hdls
is not None:
770 vnf_dict
= create_vnfd_from_files(args
.vnfd_file_hdls
)
772 for vnfd
in vnf_dict
:
773 openmano_vnfr_ids
[vnfd
] = vnfd
775 if args
.nsd_file_hdl
is not None:
776 nsd
= create_nsd_from_file(args
.nsd_file_hdl
)
778 openmano_nsd
= rift2openmano_nsd(nsd
, vnf_dict
, openmano_vnfr_ids
)
779 vnfd_nsd
= rift2openmano_vnfd_nsd(nsd
, vnf_dict
, openmano_vnfr_ids
)
780 write_yaml_to_file(openmano_nsd
["name"], args
.outdir
, openmano_nsd
)
781 write_yaml_to_file(vnfd_nsd
["name"], args
.outdir
, vnfd_nsd
)
782 for vnf
in vnf_dict
.values():
783 openmano_vnf
= rift2openmano_vnfd(vnf
, nsd
)
784 write_yaml_to_file(openmano_vnf
["vnf"]["name"], args
.outdir
, openmano_vnf
)
787 if __name__
== "__main__":
788 logging
.basicConfig(level
=logging
.WARNING
)