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 logger
= logging
.getLogger("rift2openmano.py")
42 class VNFNotFoundError(Exception):
46 class RiftNSD(object):
47 model
= RwYang
.Model
.create_libncx()
48 model
.load_module('nsd')
49 model
.load_module('rw-nsd')
51 def __init__(self
, descriptor
):
52 self
._nsd
= descriptor
67 return [c
.vnfd_id_ref
for c
in self
._nsd
.constituent_vnfd
]
70 def constituent_vnfds(self
):
71 return self
._nsd
.constituent_vnfd
79 return self
._nsd
.connection_point
82 def description(self
):
83 return self
._nsd
.description
86 def from_xml_file_hdl(cls
, hdl
):
88 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
89 descriptor
.from_xml_v2(RiftNSD
.model
, hdl
.read())
90 return cls(descriptor
)
93 def from_yaml_file_hdl(cls
, hdl
):
95 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor
.from_yaml(RiftNSD
.model
, hdl
.read())
97 return cls(descriptor
)
100 def from_dict(cls
, nsd_dict
):
101 descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
.from_dict(nsd_dict
)
102 return cls(descriptor
)
105 class RiftVNFD(object):
106 model
= RwYang
.Model
.create_libncx()
107 model
.load_module('vnfd')
108 model
.load_module('rw-vnfd')
110 def __init__(self
, descriptor
):
111 self
._vnfd
= descriptor
114 return str(self
._vnfd
)
122 return self
._vnfd
.name
125 def description(self
):
126 return self
._vnfd
.description
130 return self
._vnfd
.connection_point
134 return self
._vnfd
.vdu
137 def internal_vlds(self
):
138 return self
._vnfd
.internal_vld
141 def from_xml_file_hdl(cls
, hdl
):
143 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
144 descriptor
.from_xml_v2(RiftVNFD
.model
, hdl
.read())
145 return cls(descriptor
)
148 def from_yaml_file_hdl(cls
, hdl
):
150 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor
.from_yaml(RiftVNFD
.model
, hdl
.read())
152 return cls(descriptor
)
155 def from_dict(cls
, vnfd_dict
):
156 descriptor
= RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
.from_dict(vnfd_dict
)
157 return cls(descriptor
)
160 def is_writable_directory(dir_path
):
161 """ Returns True if dir_path is writable, False otherwise
164 dir_path - A directory path
166 if not os
.path
.exists(dir_path
):
167 raise ValueError("Directory does not exist: %s", dir_path
)
170 testfile
= tempfile
.TemporaryFile(dir=dir_path
)
178 def create_vnfd_from_xml_files(vnfd_file_hdls
):
179 """ Create a list of RiftVNFD instances from xml file handles
182 vnfd_file_hdls - Rift VNFD XML file handles
185 A list of RiftVNFD instances
188 for vnfd_file_hdl
in vnfd_file_hdls
:
189 vnfd
= RiftVNFD
.from_xml_file_hdl(vnfd_file_hdl
)
190 vnfd_dict
[vnfd
.id] = vnfd
194 def create_vnfd_from_yaml_files(vnfd_file_hdls
):
195 """ Create a list of RiftVNFD instances from xml file handles
198 vnfd_file_hdls - Rift VNFD YAML file handles
201 A list of RiftVNFD instances
204 for vnfd_file_hdl
in vnfd_file_hdls
:
205 vnfd
= RiftVNFD
.from_yaml_file_hdl(vnfd_file_hdl
)
206 vnfd_dict
[vnfd
.id] = vnfd
211 def create_nsd_from_xml_file(nsd_file_hdl
):
212 """ Create a list of RiftNSD instances from xml file handles
215 nsd_file_hdls - Rift NSD XML file handles
218 A list of RiftNSD instances
220 nsd
= RiftNSD
.from_xml_file_hdl(nsd_file_hdl
)
223 def create_nsd_from_yaml_file(nsd_file_hdl
):
224 """ Create a list of RiftNSD instances from yaml file handles
227 nsd_file_hdls - Rift NSD XML file handles
230 A list of RiftNSD instances
232 nsd
= RiftNSD
.from_yaml_file_hdl(nsd_file_hdl
)
237 return collections
.defaultdict(dict)
239 def convert_vnfd_name(vnfd_name
, member_idx
):
240 return vnfd_name
+ "__" + str(member_idx
)
243 def rift2openmano_nsd(rift_nsd
, rift_vnfds
,openmano_vnfd_ids
):
244 for vnfd_id
in rift_nsd
.vnfd_ids
:
245 if vnfd_id
not in rift_vnfds
:
246 raise VNFNotFoundError("VNF id %s not provided" % vnfd_id
)
249 openmano
["name"] = rift_nsd
.name
250 openmano
["description"] = rift_nsd
.description
252 openmano
["topology"] = topology
254 topology
["nodes"] = {}
255 for vnfd
in rift_nsd
.constituent_vnfds
:
256 vnfd_id
= vnfd
.vnfd_id_ref
257 rift_vnfd
= rift_vnfds
[vnfd_id
]
258 member_idx
= vnfd
.member_vnf_index
259 openmano_vnfd_id
= openmano_vnfd_ids
.get(vnfd_id
,None)
261 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
263 "vnf_id": openmano_vnfd_id
266 topology
["nodes"][rift_vnfd
.name
+ "__" + str(member_idx
)] = {
268 "VNF model": rift_vnfd
.name
271 for vld
in rift_nsd
.vlds
:
272 # Openmano has both bridge_net and dataplane_net models for network types
273 # For now, since we are using openmano in developer mode lets just hardcode
274 # to bridge_net since it won't matter anyways.
275 # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"}
278 topology
["connections"] = {}
279 for vld
in rift_nsd
.vlds
:
281 # Create a connections entry for each external VLD
282 topology
["connections"][vld
.name
] = {}
283 topology
["connections"][vld
.name
]["nodes"] = []
285 #if vld.vim_network_name:
287 if vld
.name
not in topology
["nodes"]:
288 topology
["nodes"][vld
.name
] = {
289 "type": "external_network",
293 # Add the external network to the list of connection points
294 topology
["connections"][vld
.name
]["nodes"].append(
297 elif vld
.provider_network
.has_field("physical_network"):
298 # Add the external datacenter network to the topology
299 # node list if it isn't already added
300 ext_net_name
= vld
.provider_network
.physical_network
301 ext_net_name_with_seg
= ext_net_name
302 if vld
.provider_network
.has_field("segmentation_id"):
303 ext_net_name_with_seg
+= ":{}".format(vld
.provider_network
.segmentation_id
)
305 if ext_net_name
not in topology
["nodes"]:
306 topology
["nodes"][ext_net_name
] = {
307 "type": "external_network",
308 "model": ext_net_name_with_seg
,
311 # Add the external network to the list of connection points
312 topology
["connections"][vld
.name
]["nodes"].append(
317 for vnfd_cp
in vld
.vnfd_connection_point_ref
:
319 # Get the RIFT VNF for this external VLD connection point
320 vnfd
= rift_vnfds
[vnfd_cp
.vnfd_id_ref
]
322 # For each VNF in this connection, use the same interface name
323 topology
["connections"][vld
.name
]["type"] = "link"
324 # Vnf ref is the vnf name with the member_vnf_idx appended
325 member_idx
= vnfd_cp
.member_vnf_index_ref
326 vnf_ref
= vnfd
.name
+ "__" + str(member_idx
)
327 topology
["connections"][vld
.name
]["nodes"].append(
329 vnf_ref
: vnfd_cp
.vnfd_connection_point_ref
336 def rift2openmano_vnfd(rift_vnfd
, rift_nsd
):
337 openmano_vnf
= {"vnf":{}}
338 vnf
= openmano_vnf
["vnf"]
340 vnf
["name"] = rift_vnfd
.name
341 vnf
["description"] = rift_vnfd
.description
343 vnf
["external-connections"] = []
345 def find_vdu_and_ext_if_by_cp_ref(cp_ref_name
):
346 for vdu
in rift_vnfd
.vdus
:
347 for ext_if
in vdu
.external_interface
:
348 if ext_if
.vnfd_connection_point_ref
== cp_ref_name
:
351 raise ValueError("External connection point reference %s not found" % cp_ref_name
)
353 def find_vdu_and_int_if_by_cp_ref(cp_ref_id
):
354 for vdu
in rift_vnfd
.vdus
:
355 for int_if
in vdu
.internal_interface
:
356 if int_if
.vdu_internal_connection_point_ref
== cp_ref_id
:
359 raise ValueError("Internal connection point reference %s not found" % cp_ref_id
)
361 def rift2openmano_if_type(ext_if
):
363 cp_ref_name
= ext_if
.vnfd_connection_point_ref
364 for vld
in rift_nsd
.vlds
:
366 # if it is an explicit mgmt_network then check if the given
367 # cp_ref is a part of it
368 if not vld
.mgmt_network
:
371 for vld_cp
in vld
.vnfd_connection_point_ref
:
372 if vld_cp
.vnfd_connection_point_ref
== cp_ref_name
:
376 rift_type
= ext_if
.virtual_interface
.type_yang
377 # Retaining it for backward compatibility!
378 if rift_type
== "OM_MGMT":
380 elif rift_type
== "VIRTIO" or rift_type
== "E1000":
385 def rift2openmano_vif(rift_type
):
386 if rift_type
== "VIRTIO":
388 elif rift_type
== "E1000":
391 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type
))
393 # Add all external connections
394 for cp
in rift_vnfd
.cps
:
395 # Find the VDU and and external interface for this connection point
396 vdu
, ext_if
= find_vdu_and_ext_if_by_cp_ref(cp
.name
)
399 "type": rift2openmano_if_type(ext_if
),
401 "local_iface_name": ext_if
.name
,
402 "description": "%s iface on VDU %s" % (ext_if
.name
, vdu
.name
),
405 vnf
["external-connections"].append(connection
)
407 # Add all internal networks
408 for vld
in rift_vnfd
.internal_vlds
:
411 "description": vld
.description
,
416 # Add the specific VDU connection points
417 for int_cp
in vld
.internal_connection_point
:
418 vdu
, int_if
= find_vdu_and_int_if_by_cp_ref(int_cp
.id_ref
)
419 connection
["elements"].append({
421 "local_iface_name": int_if
.name
,
423 if "internal-connections" not in vnf
:
424 vnf
["internal-connections"] = []
426 vnf
["internal-connections"].append(connection
)
430 for vdu
in rift_vnfd
.vdus
:
433 "description": vdu
.name
,
437 if vdu
.vm_flavor
.has_field("storage_gb") and vdu
.vm_flavor
.storage_gb
:
438 vnfc
["disk"] = vdu
.vm_flavor
.storage_gb
440 if os
.path
.isabs(vdu
.image
):
441 vnfc
["VNFC image"] = vdu
.image
443 vnfc
["image name"] = vdu
.image
444 if vdu
.has_field("image_checksum"):
445 vnfc
["image checksum"] = vdu
.image_checksum
446 dedicated_int
= False
447 for intf
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
448 if intf
.virtual_interface
.type_yang
in ["SR_IOV", "PCI_PASSTHROUGH"]:
450 if vdu
.guest_epa
.has_field("numa_node_policy") or dedicated_int
:
452 "memory": max(int(vdu
.vm_flavor
.memory_mb
/1024), 1),
455 numa_node_policy
= vdu
.guest_epa
.numa_node_policy
456 if numa_node_policy
.has_field("node"):
457 numa_node
= numa_node_policy
.node
[0]
459 if numa_node
.has_field("paired_threads"):
460 if numa_node
.paired_threads
.has_field("num_paired_threads"):
461 vnfc
["numas"][0]["paired-threads"] = numa_node
.paired_threads
.num_paired_threads
462 if len(numa_node
.paired_threads
.paired_thread_ids
) > 0:
463 vnfc
["numas"][0]["paired-threads-id"] = []
464 for pair
in numa_node
.paired_threads
.paired_thread_ids
:
465 vnfc
["numas"][0]["paired-threads-id"].append(
466 [pair
.thread_a
, pair
.thread_b
]
470 if vdu
.vm_flavor
.has_field("vcpu_count"):
471 vnfc
["numas"][0]["cores"] = max(vdu
.vm_flavor
.vcpu_count
, 1)
474 if vdu
.vm_flavor
.has_field("vcpu_count") and vdu
.vm_flavor
.vcpu_count
:
475 vnfc
["vcpus"] = vdu
.vm_flavor
.vcpu_count
477 if vdu
.vm_flavor
.has_field("memory_mb") and vdu
.vm_flavor
.memory_mb
:
478 vnfc
["ram"] = vdu
.vm_flavor
.memory_mb
481 if vdu
.has_field("hypervisor_epa"):
482 vnfc
["hypervisor"] = {}
483 if vdu
.hypervisor_epa
.has_field("type"):
484 if vdu
.hypervisor_epa
.type_yang
== "REQUIRE_KVM":
485 vnfc
["hypervisor"]["type"] = "QEMU-kvm"
487 if vdu
.hypervisor_epa
.has_field("version"):
488 vnfc
["hypervisor"]["version"] = vdu
.hypervisor_epa
.version
490 if vdu
.has_field("host_epa"):
491 vnfc
["processor"] = {}
492 if vdu
.host_epa
.has_field("om_cpu_model_string"):
493 vnfc
["processor"]["model"] = vdu
.host_epa
.om_cpu_model_string
494 if vdu
.host_epa
.has_field("om_cpu_feature"):
495 vnfc
["processor"]["features"] = []
496 for feature
in vdu
.host_epa
.om_cpu_feature
:
497 vnfc
["processor"]["features"].append(feature
.feature
)
500 vnf
["VNFC"].append(vnfc
)
502 for int_if
in list(vdu
.internal_interface
) + list(vdu
.external_interface
):
506 if int_if
.virtual_interface
.has_field("vpci"):
507 intf
["vpci"] = int_if
.virtual_interface
.vpci
509 if int_if
.virtual_interface
.type_yang
in ["VIRTIO", "E1000"]:
510 intf
["model"] = rift2openmano_vif(int_if
.virtual_interface
.type_yang
)
511 vnfc
["bridge-ifaces"].append(intf
)
513 elif int_if
.virtual_interface
.type_yang
in ["OM_MGMT"]:
514 vnfc
["bridge-ifaces"].append(intf
)
516 elif int_if
.virtual_interface
.type_yang
== "SR_IOV":
517 intf
["bandwidth"] = "10 Gbps"
518 intf
["dedicated"] = "no"
519 vnfc
["numas"][0]["interfaces"].append(intf
)
521 elif int_if
.virtual_interface
.type_yang
== "PCI_PASSTHROUGH":
522 intf
["bandwidth"] = "10 Gbps"
523 intf
["dedicated"] = "yes"
524 if "interfaces" not in vnfc
["numas"][0]:
525 vnfc
["numas"][0]["interfaces"] = []
526 vnfc
["numas"][0]["interfaces"].append(intf
)
528 raise ValueError("Interface type %s not supported" % int_if
.virtual_interface
)
530 if int_if
.virtual_interface
.has_field("bandwidth"):
531 if int_if
.virtual_interface
.bandwidth
!= 0:
532 bps
= int_if
.virtual_interface
.bandwidth
534 # Calculate the bits per second conversion
535 for x
in [('M', 1000000), ('G', 1000000000)]:
537 intf
["bandwidth"] = "{} {}bps".format(math
.ceil(bps
/x
[1]), x
[0])
543 def parse_args(argv
=sys
.argv
[1:]):
544 """ Parse the command line arguments
547 arv - The list of arguments to parse
550 Argparse Namespace instance
552 parser
= argparse
.ArgumentParser()
556 help="Directory to output converted descriptors. Default is stdout",
560 '-n', '--nsd-file-hdl',
561 metavar
="nsd_xml_file",
562 type=argparse
.FileType('r'),
563 help="Rift NSD Descriptor File",
567 '-v', '--vnfd-file-hdls',
568 metavar
="vnfd_xml_file",
570 type=argparse
.FileType('r'),
571 help="Rift VNFD Descriptor File",
574 args
= parser
.parse_args(argv
)
576 if not os
.path
.exists(args
.outdir
):
577 os
.makedirs(args
.outdir
)
579 if not is_writable_directory(args
.outdir
):
580 logging
.error("Directory %s is not writable", args
.outdir
)
586 def write_yaml_to_file(name
, outdir
, desc_dict
):
587 file_name
= "%s.yaml" % name
588 yaml_str
= yaml
.dump(desc_dict
)
590 sys
.stdout
.write(yaml_str
)
593 file_path
= os
.path
.join(outdir
, file_name
)
594 dir_path
= os
.path
.dirname(file_path
)
595 if not os
.path
.exists(dir_path
):
596 os
.makedirs(dir_path
)
598 with
open(file_path
, "w") as hdl
:
601 logger
.info("Wrote descriptor to %s", file_path
)
604 def main(argv
=sys
.argv
[1:]):
605 args
= parse_args(argv
)
608 if args
.vnfd_file_hdls
is not None:
609 vnf_dict
= create_vnfd_from_xml_files(args
.vnfd_file_hdls
)
611 if args
.nsd_file_hdl
is not None:
612 nsd
= create_nsd_from_xml_file(args
.nsd_file_hdl
)
614 openmano_nsd
= rift2openmano_nsd(nsd
, vnf_dict
)
616 write_yaml_to_file(openmano_nsd
["name"], args
.outdir
, openmano_nsd
)
618 for vnf
in vnf_dict
.values():
619 openmano_vnf
= rift2openmano_vnfd(vnf
)
620 write_yaml_to_file(openmano_vnf
["vnf"]["name"], args
.outdir
, openmano_vnf
)
623 if __name__
== "__main__":
624 logging
.basicConfig(level
=logging
.WARNING
)