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 topology
["nodes"][rift_vnfd_id
+'__'+str(rvnfd_id
.member_vnf_index
)] = {
344 "vnf_id": openmano_vnfd_id
347 for vld
in rift_nsd
.vlds
:
349 # Create a connections entry for each external VLD
350 topology
["connections"][vld
.name
] = {}
351 topology
["connections"][vld
.name
]["nodes"] = []
353 if vld
.name
not in topology
["nodes"]:
354 topology
["nodes"][vld
.name
] = {
355 "type": "external_network",
358 topology
["connections"][vld
.name
]["nodes"].append(
364 for vnfd_cp
in vld
.vnfd_connection_point_ref
:
365 if not rift_vnfd_id
in vnfd_cp
.vnfd_id_ref
:
367 if rift_vnfd_id
in vnfd_cp
.vnfd_id_ref
:
369 # Get the RIFT VNF for this external VLD connection point
370 vnfd
= rift_vnfds
[vnfd_cp
.vnfd_id_ref
]
373 # For each VNF in this connection, use the same interface name
374 topology
["connections"][vld
.name
]["type"] = "link"
375 # Vnf ref is the vnf name with the member_vnf_idx appended
376 member_idx
= vnfd_cp
.member_vnf_index_ref
377 vnf_ref
= rift_vnfd_id
+ "__" + str(member_idx
)
378 topology
["connections"][vld
.name
]["nodes"].append(
380 vnf_ref
: vnfd_cp
.vnfd_connection_point_ref
383 return openmano_vnfd_nsd
386 def cloud_init(rift_vnfd_id
, vdu
):
387 """ Populate cloud_init with script from
388 either the inline contents or from the file provided
390 vnfd_package_store
= rift
.package
.store
.VnfdPackageFilesystemStore(logger
)
392 cloud_init_msg
= None
393 if vdu
.cloud_init
is not None:
394 logger
.debug("cloud_init script provided inline %s", vdu
.cloud_init
)
395 cloud_init_msg
= vdu
.cloud_init
396 elif vdu
.cloud_init_file
is not None:
397 # Get cloud-init script contents from the file provided in the cloud_init_file param
398 logger
.debug("cloud_init script provided in file %s", vdu
.cloud_init_file
)
399 filename
= vdu
.cloud_init_file
400 vnfd_package_store
.refresh()
401 stored_package
= vnfd_package_store
.get_package(rift_vnfd_id
)
402 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(logger
)
404 cloud_init_msg
= cloud_init_extractor
.read_script(stored_package
, filename
)
405 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
408 logger
.debug("VDU translation: cloud-init script not provided")
411 logger
.debug("Current cloud init msg is {}".format(cloud_init_msg
))
412 return cloud_init_msg
414 def config_file_init(rift_vnfd_id
, vdu
, cfg_file
):
415 """ Populate config file init with file provided
417 vnfd_package_store
= rift
.package
.store
.VnfdPackageFilesystemStore(logger
)
419 # Get script contents from the file provided in the cloud_init directory
420 logger
.debug("config file script provided in file {}".format(cfg_file
))
422 vnfd_package_store
.refresh()
423 stored_package
= vnfd_package_store
.get_package(rift_vnfd_id
)
424 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(logger
)
426 cfg_file_msg
= cloud_init_extractor
.read_script(stored_package
, filename
)
427 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
430 logger
.debug("Current config file msg is {}".format(cfg_file_msg
))
433 def rift2openmano_vnfd(rift_vnfd
, rift_nsd
):
434 openmano_vnf
= {"vnf":{}}
435 vnf
= openmano_vnf
["vnf"]
437 vnf
["name"] = rift_vnfd
.name
438 vnf
["description"] = rift_vnfd
.description
440 vnf
["external-connections"] = []
442 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name
):
443 for vdu
in rift_vnfd
.vdus
:
444 for ext_if
in vdu
.external_interface
:
445 if ext_if
.vnfd_connection_point_ref
== cp_ref_name
:
448 raise ValueError("External connection point reference %s not found" % cp_ref_name
)
450 def find_vdu_and_int_if_by_cp_ref(cp_ref_id
):
451 for vdu
in rift_vnfd
.vdus
:
452 for int_if
in vdu
.internal_interface
:
453 if int_if
.vdu_internal_connection_point_ref
== cp_ref_id
:
456 raise ValueError("Internal connection point reference %s not found" % cp_ref_id
)
458 def rift2openmano_if_type(ext_if
):
460 cp_ref_name
= ext_if
.vnfd_connection_point_ref
461 for vld
in rift_nsd
.vlds
:
463 # if it is an explicit mgmt_network then check if the given
464 # cp_ref is a part of it
465 if not vld
.mgmt_network
:
468 for vld_cp
in vld
.vnfd_connection_point_ref
:
469 if vld_cp
.vnfd_connection_point_ref
== cp_ref_name
:
473 rift_type
= ext_if
.virtual_interface
.type_yang
474 # Retaining it for backward compatibility!
475 if rift_type
== "OM_MGMT":
477 elif rift_type
== "VIRTIO" or rift_type
== "E1000":
482 def rift2openmano_vif(rift_type
):
483 if rift_type
== "VIRTIO":
485 elif rift_type
== "E1000":
488 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type
))
490 # Add all external connections
491 cp_to_port_security_map
= {}
493 for cp
in rift_vnfd
.cps
:
494 # Find the VDU and and external interface for this connection point
495 vdu
, ext_if
= find_vdu_and_ext_if_by_cp_ref(cp
.name
)
498 "type": rift2openmano_if_type(ext_if
),
500 "local_iface_name": ext_if
.name
,
501 "description": "%s iface on VDU %s" % (ext_if
.name
, vdu
.name
),
504 if cp
.has_field('port_security_enabled'):
505 cp_to_port_security_map
[cp
.name
] = cp
.port_security_enabled
506 vnf
["external-connections"].append(connection
)
508 # Add all internal networks
509 for vld
in rift_vnfd
.internal_vlds
:
512 "description": vld
.description
,
517 # Add the specific VDU connection points
518 for int_cp
in vld
.internal_connection_point
:
519 vdu
, int_if
= find_vdu_and_int_if_by_cp_ref(int_cp
.id_ref
)
520 connection
["elements"].append({
522 "local_iface_name": int_if
.name
,
524 if "internal-connections" not in vnf
:
525 vnf
["internal-connections"] = []
527 vnf
["internal-connections"].append(connection
)
531 for vdu
in rift_vnfd
.vdus
:
534 "description": vdu
.name
,
538 if vdu
.vm_flavor
.has_field("storage_gb") and vdu
.vm_flavor
.storage_gb
:
539 vnfc
["disk"] = vdu
.vm_flavor
.storage_gb
541 if vdu
.has_field("image"):
542 if os
.path
.isabs(vdu
.image
):
543 vnfc
["VNFC image"] = vdu
.image
545 vnfc
["image name"] = vdu
.image
546 if vdu
.has_field("image_checksum"):
547 vnfc
["image checksum"] = vdu
.image_checksum
549 dedicated_int
= False
550 for intf
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
551 if intf
.virtual_interface
.type_yang
in ["SR_IOV", "PCI_PASSTHROUGH"]:
553 if vdu
.guest_epa
.has_field("numa_node_policy") or dedicated_int
:
555 "memory": max(int(vdu
.vm_flavor
.memory_mb
/1024), 1),
558 numa_node_policy
= vdu
.guest_epa
.numa_node_policy
559 if numa_node_policy
.has_field("node"):
560 numa_node
= numa_node_policy
.node
[0]
562 if numa_node
.has_field("paired_threads"):
563 if numa_node
.paired_threads
.has_field("num_paired_threads"):
564 vnfc
["numas"][0]["paired-threads"] = numa_node
.paired_threads
.num_paired_threads
565 if len(numa_node
.paired_threads
.paired_thread_ids
) > 0:
566 vnfc
["numas"][0]["paired-threads-id"] = []
567 for pair
in numa_node
.paired_threads
.paired_thread_ids
:
568 vnfc
["numas"][0]["paired-threads-id"].append(
569 [pair
.thread_a
, pair
.thread_b
]
573 if vdu
.vm_flavor
.has_field("vcpu_count"):
574 vnfc
["numas"][0]["cores"] = max(vdu
.vm_flavor
.vcpu_count
, 1)
576 if vdu
.vm_flavor
.has_field("vcpu_count") and vdu
.vm_flavor
.vcpu_count
:
577 vnfc
["vcpus"] = vdu
.vm_flavor
.vcpu_count
579 if vdu
.vm_flavor
.has_field("memory_mb") and vdu
.vm_flavor
.memory_mb
:
580 vnfc
["ram"] = vdu
.vm_flavor
.memory_mb
583 if vdu
.has_field("hypervisor_epa"):
584 vnfc
["hypervisor"] = {}
585 if vdu
.hypervisor_epa
.has_field("type"):
586 if vdu
.hypervisor_epa
.type_yang
== "REQUIRE_KVM":
587 vnfc
["hypervisor"]["type"] = "QEMU-kvm"
589 if vdu
.hypervisor_epa
.has_field("version"):
590 vnfc
["hypervisor"]["version"] = vdu
.hypervisor_epa
.version
592 if vdu
.has_field("host_epa"):
593 vnfc
["processor"] = {}
594 if vdu
.host_epa
.has_field("om_cpu_model_string"):
595 vnfc
["processor"]["model"] = vdu
.host_epa
.om_cpu_model_string
596 if vdu
.host_epa
.has_field("om_cpu_feature"):
597 vnfc
["processor"]["features"] = []
598 for feature
in vdu
.host_epa
.om_cpu_feature
:
599 vnfc
["processor"]["features"].append(feature
.feature
)
601 if vdu
.has_field("volumes"):
603 # Sort volumes as device-list is implictly ordered by Openmano
604 newvollist
= sorted(vdu
.volumes
, key
=lambda k
: k
.name
)
605 for iter_num
, volume
in enumerate(newvollist
):
607 # Convert the first volume to vnfc.image
608 if os
.path
.isabs(volume
.image
):
609 vnfc
["VNFC image"] = volume
.image
611 vnfc
["image name"] = volume
.image
612 if volume
.has_field("image_checksum"):
613 vnfc
["image checksum"] = volume
.image_checksum
615 # Add Openmano devices
617 device
["type"] = volume
.device_type
618 if volume
.has_field("size"):
619 device
["size"] = volume
.size
620 if volume
.has_field("image"):
621 device
["image name"] = volume
.image
622 if volume
.has_field("image_checksum"):
623 device
["image checksum"] = volume
.image_checksum
624 vnfc
["devices"].append(device
)
626 vnfc_boot_data_init
= False
627 if vdu
.has_field("cloud_init") or vdu
.has_field("cloud_init_file"):
628 vnfc
['boot-data'] = dict()
629 vnfc_boot_data_init
= True
630 vnfc
['boot-data']['user-data'] = cloud_init(rift_vnfd
.id, vdu
)
632 if vdu
.has_field("supplemental_boot_data"):
633 if vdu
.supplemental_boot_data
.has_field('boot_data_drive'):
634 if vdu
.supplemental_boot_data
.boot_data_drive
is True:
635 if vnfc_boot_data_init
is False:
636 vnfc
['boot-data'] = dict()
637 vnfc_boot_data_init
= True
638 vnfc
['boot-data']['boot-data-drive'] = vdu
.supplemental_boot_data
.boot_data_drive
640 if vdu
.supplemental_boot_data
.has_field('config_file'):
641 om_cfgfile_list
= list()
642 for custom_config_file
in vdu
.supplemental_boot_data
.config_file
:
643 cfg_source
= config_file_init(rift_vnfd
.id, vdu
, custom_config_file
.source
)
644 om_cfgfile_list
.append({"dest":custom_config_file
.dest
, "content": cfg_source
})
645 vnfc
['boot-data']['config-files'] = om_cfgfile_list
648 vnf
["VNFC"].append(vnfc
)
650 for int_if
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
654 if int_if
.virtual_interface
.has_field("vpci"):
655 intf
["vpci"] = int_if
.virtual_interface
.vpci
657 if int_if
.virtual_interface
.type_yang
in ["VIRTIO", "E1000"]:
658 intf
["model"] = rift2openmano_vif(int_if
.virtual_interface
.type_yang
)
659 vnfc
["bridge-ifaces"].append(intf
)
661 elif int_if
.virtual_interface
.type_yang
in ["OM_MGMT"]:
662 vnfc
["bridge-ifaces"].append(intf
)
664 elif int_if
.virtual_interface
.type_yang
== "SR_IOV":
665 intf
["bandwidth"] = "10 Gbps"
666 intf
["dedicated"] = "no"
667 vnfc
["numas"][0]["interfaces"].append(intf
)
669 elif int_if
.virtual_interface
.type_yang
== "PCI_PASSTHROUGH":
670 intf
["bandwidth"] = "10 Gbps"
671 intf
["dedicated"] = "yes"
672 if "interfaces" not in vnfc
["numas"][0]:
673 vnfc
["numas"][0]["interfaces"] = []
674 vnfc
["numas"][0]["interfaces"].append(intf
)
676 raise ValueError("Interface type %s not supported" % int_if
.virtual_interface
)
678 if int_if
.virtual_interface
.has_field("bandwidth"):
679 if int_if
.virtual_interface
.bandwidth
!= 0:
680 bps
= int_if
.virtual_interface
.bandwidth
682 # Calculate the bits per second conversion
683 for x
in [('M', 1000000), ('G', 1000000000)]:
685 intf
["bandwidth"] = "{} {}bps".format(math
.ceil(bps
/x
[1]), x
[0])
687 for bridge_iface
in vnfc
["bridge-ifaces"]:
688 if bridge_iface
['name'] in cp_to_port_security_map
:
689 bridge_iface
['port-security'] = cp_to_port_security_map
[bridge_iface
['name']]
690 # Sort bridge-ifaces-list TODO sort others
691 newlist
= sorted(vnfc
["bridge-ifaces"], key
=lambda k
: k
['name'])
692 vnfc
["bridge-ifaces"] = newlist
697 def parse_args(argv
=sys
.argv
[1:]):
698 """ Parse the command line arguments
701 arv - The list of arguments to parse
704 Argparse Namespace instance
706 parser
= argparse
.ArgumentParser()
710 help="Directory to output converted descriptors. Default is stdout",
714 '-n', '--nsd-file-hdl',
716 type=argparse
.FileType('r'),
717 help="Rift NSD Descriptor File",
721 '-v', '--vnfd-file-hdls',
724 type=argparse
.FileType('r'),
725 help="Rift VNFD Descriptor File",
728 args
= parser
.parse_args(argv
)
730 if not os
.path
.exists(args
.outdir
):
731 os
.makedirs(args
.outdir
)
733 if not is_writable_directory(args
.outdir
):
734 logging
.error("Directory %s is not writable", args
.outdir
)
740 def write_yaml_to_file(name
, outdir
, desc_dict
):
741 file_name
= "%s.yaml" % name
742 yaml_str
= yaml
.dump(desc_dict
)
744 sys
.stdout
.write(yaml_str
)
747 file_path
= os
.path
.join(outdir
, file_name
)
748 dir_path
= os
.path
.dirname(file_path
)
749 if not os
.path
.exists(dir_path
):
750 os
.makedirs(dir_path
)
752 with
open(file_path
, "w") as hdl
:
755 logger
.info("Wrote descriptor to %s", file_path
)
758 def main(argv
=sys
.argv
[1:]):
759 args
= parse_args(argv
)
761 openmano_vnfr_ids
= dict()
763 if args
.vnfd_file_hdls
is not None:
764 vnf_dict
= create_vnfd_from_files(args
.vnfd_file_hdls
)
766 for vnfd
in vnf_dict
:
767 openmano_vnfr_ids
[vnfd
] = vnfd
769 if args
.nsd_file_hdl
is not None:
770 nsd
= create_nsd_from_file(args
.nsd_file_hdl
)
772 openmano_nsd
= rift2openmano_nsd(nsd
, vnf_dict
, openmano_vnfr_ids
)
773 vnfd_nsd
= rift2openmano_vnfd_nsd(nsd
, vnf_dict
, openmano_vnfr_ids
)
774 write_yaml_to_file(openmano_nsd
["name"], args
.outdir
, openmano_nsd
)
775 write_yaml_to_file(vnfd_nsd
["name"], args
.outdir
, vnfd_nsd
)
776 for vnf
in vnf_dict
.values():
777 openmano_vnf
= rift2openmano_vnfd(vnf
, nsd
)
778 write_yaml_to_file(openmano_vnf
["vnf"]["name"], args
.outdir
, openmano_vnf
)
781 if __name__
== "__main__":
782 logging
.basicConfig(level
=logging
.WARNING
)