Bug 106 - Cannot instantiate Network services with VNFs having SR-IOV interfaces
[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 logger = logging.getLogger("rift2openmano.py")
40
41
42 class VNFNotFoundError(Exception):
43 pass
44
45
46 class RiftNSD(object):
47 model = RwYang.Model.create_libncx()
48 model.load_module('nsd')
49 model.load_module('rw-nsd')
50
51 def __init__(self, descriptor):
52 self._nsd = descriptor
53
54 def __str__(self):
55 return str(self._nsd)
56
57 @property
58 def name(self):
59 return self._nsd.name
60
61 @property
62 def id(self):
63 return self._nsd.id
64
65 @property
66 def vnfd_ids(self):
67 return [c.vnfd_id_ref for c in self._nsd.constituent_vnfd]
68
69 @property
70 def constituent_vnfds(self):
71 return self._nsd.constituent_vnfd
72
73 @property
74 def vlds(self):
75 return self._nsd.vld
76
77 @property
78 def cps(self):
79 return self._nsd.connection_point
80
81 @property
82 def description(self):
83 return self._nsd.description
84
85 @classmethod
86 def from_xml_file_hdl(cls, hdl):
87 hdl.seek(0)
88 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
89 descriptor.from_xml_v2(RiftNSD.model, hdl.read())
90 return cls(descriptor)
91
92 @classmethod
93 def from_yaml_file_hdl(cls, hdl):
94 hdl.seek(0)
95 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd()
96 descriptor.from_yaml(RiftNSD.model, hdl.read())
97 return cls(descriptor)
98
99 @classmethod
100 def from_dict(cls, nsd_dict):
101 descriptor = RwNsdYang.YangData_Nsd_NsdCatalog_Nsd.from_dict(nsd_dict)
102 return cls(descriptor)
103
104
105 class RiftVNFD(object):
106 model = RwYang.Model.create_libncx()
107 model.load_module('vnfd')
108 model.load_module('rw-vnfd')
109
110 def __init__(self, descriptor):
111 self._vnfd = descriptor
112
113 def __str__(self):
114 return str(self._vnfd)
115
116 @property
117 def id(self):
118 return self._vnfd.id
119
120 @property
121 def name(self):
122 return self._vnfd.name
123
124 @property
125 def description(self):
126 return self._vnfd.description
127
128 @property
129 def cps(self):
130 return self._vnfd.connection_point
131
132 @property
133 def vdus(self):
134 return self._vnfd.vdu
135
136 @property
137 def internal_vlds(self):
138 return self._vnfd.internal_vld
139
140 @classmethod
141 def from_xml_file_hdl(cls, hdl):
142 hdl.seek(0)
143 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
144 descriptor.from_xml_v2(RiftVNFD.model, hdl.read())
145 return cls(descriptor)
146
147 @classmethod
148 def from_yaml_file_hdl(cls, hdl):
149 hdl.seek(0)
150 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd()
151 descriptor.from_yaml(RiftVNFD.model, hdl.read())
152 return cls(descriptor)
153
154 @classmethod
155 def from_dict(cls, vnfd_dict):
156 descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd.from_dict(vnfd_dict)
157 return cls(descriptor)
158
159
160 def is_writable_directory(dir_path):
161 """ Returns True if dir_path is writable, False otherwise
162
163 Arguments:
164 dir_path - A directory path
165 """
166 if not os.path.exists(dir_path):
167 raise ValueError("Directory does not exist: %s", dir_path)
168
169 try:
170 testfile = tempfile.TemporaryFile(dir=dir_path)
171 testfile.close()
172 except OSError:
173 return False
174
175 return True
176
177
178 def create_vnfd_from_xml_files(vnfd_file_hdls):
179 """ Create a list of RiftVNFD instances from xml file handles
180
181 Arguments:
182 vnfd_file_hdls - Rift VNFD XML file handles
183
184 Returns:
185 A list of RiftVNFD instances
186 """
187 vnfd_dict = {}
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
191
192 return vnfd_dict
193
194 def create_vnfd_from_yaml_files(vnfd_file_hdls):
195 """ Create a list of RiftVNFD instances from xml file handles
196
197 Arguments:
198 vnfd_file_hdls - Rift VNFD YAML file handles
199
200 Returns:
201 A list of RiftVNFD instances
202 """
203 vnfd_dict = {}
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
207
208 return vnfd_dict
209
210
211 def create_nsd_from_xml_file(nsd_file_hdl):
212 """ Create a list of RiftNSD instances from xml file handles
213
214 Arguments:
215 nsd_file_hdls - Rift NSD XML file handles
216
217 Returns:
218 A list of RiftNSD instances
219 """
220 nsd = RiftNSD.from_xml_file_hdl(nsd_file_hdl)
221 return nsd
222
223 def create_nsd_from_yaml_file(nsd_file_hdl):
224 """ Create a list of RiftNSD instances from yaml file handles
225
226 Arguments:
227 nsd_file_hdls - Rift NSD XML file handles
228
229 Returns:
230 A list of RiftNSD instances
231 """
232 nsd = RiftNSD.from_yaml_file_hdl(nsd_file_hdl)
233 return nsd
234
235
236 def ddict():
237 return collections.defaultdict(dict)
238
239 def convert_vnfd_name(vnfd_name, member_idx):
240 return vnfd_name + "__" + str(member_idx)
241
242
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)
247
248 openmano = {}
249 openmano["name"] = rift_nsd.name
250 openmano["description"] = rift_nsd.description
251 topology = {}
252 openmano["topology"] = topology
253
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)
260 if openmano_vnfd_id:
261 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
262 "type": "VNF",
263 "vnf_id": openmano_vnfd_id
264 }
265 else:
266 topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = {
267 "type": "VNF",
268 "VNF model": rift_vnfd.name
269 }
270
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"}
276 pass
277
278 topology["connections"] = {}
279 for vld in rift_nsd.vlds:
280
281 # Create a connections entry for each external VLD
282 topology["connections"][vld.name] = {}
283 topology["connections"][vld.name]["nodes"] = []
284
285 #if vld.vim_network_name:
286 if True:
287 if vld.name not in topology["nodes"]:
288 topology["nodes"][vld.name] = {
289 "type": "external_network",
290 "model": vld.name,
291 }
292
293 # Add the external network to the list of connection points
294 topology["connections"][vld.name]["nodes"].append(
295 {vld.name: "0"}
296 )
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)
304
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,
309 }
310
311 # Add the external network to the list of connection points
312 topology["connections"][vld.name]["nodes"].append(
313 {ext_net_name: "0"}
314 )
315
316
317 for vnfd_cp in vld.vnfd_connection_point_ref:
318
319 # Get the RIFT VNF for this external VLD connection point
320 vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref]
321
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(
328 {
329 vnf_ref: vnfd_cp.vnfd_connection_point_ref
330 }
331 )
332
333 return openmano
334
335
336 def rift2openmano_vnfd(rift_vnfd):
337 openmano_vnf = {"vnf":{}}
338 vnf = openmano_vnf["vnf"]
339
340 vnf["name"] = rift_vnfd.name
341 vnf["description"] = rift_vnfd.description
342
343 vnf["external-connections"] = []
344
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:
349 return vdu, ext_if
350
351 raise ValueError("External connection point reference %s not found" % cp_ref_name)
352
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:
357 return vdu, int_if
358
359 raise ValueError("Internal connection point reference %s not found" % cp_ref_id)
360
361 def rift2openmano_if_type(rift_type):
362 if rift_type == "OM_MGMT":
363 return "mgmt"
364 elif rift_type == "VIRTIO" or rift_type == "E1000":
365 return "bridge"
366 else:
367 return "data"
368
369 def rift2openmano_vif(rift_type):
370 if rift_type == "VIRTIO":
371 return "virtio"
372 elif rift_type == "E1000":
373 return "e1000"
374 else:
375 raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type))
376
377 # Add all external connections
378 for cp in rift_vnfd.cps:
379 # Find the VDU and and external interface for this connection point
380 vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name)
381 connection = {
382 "name": cp.name,
383 "type": rift2openmano_if_type(ext_if.virtual_interface.type_yang),
384 "VNFC": vdu.name,
385 "local_iface_name": ext_if.name,
386 "description": "%s iface on VDU %s" % (ext_if.name, vdu.name),
387 }
388
389 vnf["external-connections"].append(connection)
390
391 # Add all internal networks
392 for vld in rift_vnfd.internal_vlds:
393 connection = {
394 "name": vld.name,
395 "description": vld.description,
396 "type": "data",
397 "elements": [],
398 }
399
400 # Add the specific VDU connection points
401 for int_cp in vld.internal_connection_point:
402 vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref)
403 connection["elements"].append({
404 "VNFC": vdu.name,
405 "local_iface_name": int_if.name,
406 })
407 if "internal-connections" not in vnf:
408 vnf["internal-connections"] = []
409
410 vnf["internal-connections"].append(connection)
411
412 # Add VDU's
413 vnf["VNFC"] = []
414 for vdu in rift_vnfd.vdus:
415 vnfc = {
416 "name": vdu.name,
417 "description": vdu.name,
418 "bridge-ifaces": [],
419 }
420
421 if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb:
422 vnfc["disk"] = vdu.vm_flavor.storage_gb
423
424 if os.path.isabs(vdu.image):
425 vnfc["VNFC image"] = vdu.image
426 else:
427 vnfc["image name"] = vdu.image
428 if vdu.has_field("image_checksum"):
429 vnfc["image checksum"] = vdu.image_checksum
430 dedicated_int = False
431 for intf in list(vdu.internal_interface) + list(vdu.external_interface):
432 if intf.virtual_interface.type_yang in ["SR_IOV", "PCI_PASSTHROUGH"]:
433 dedicated_int = True
434 if vdu.guest_epa.has_field("numa_node_policy") or dedicated_int:
435 vnfc["numas"] = [{
436 "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1),
437 "interfaces":[],
438 }]
439 numa_node_policy = vdu.guest_epa.numa_node_policy
440 if numa_node_policy.has_field("node"):
441 numa_node = numa_node_policy.node[0]
442
443 if numa_node.has_field("paired_threads"):
444 if numa_node.paired_threads.has_field("num_paired_threads"):
445 vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads
446 if len(numa_node.paired_threads.paired_thread_ids) > 0:
447 vnfc["numas"][0]["paired-threads-id"] = []
448 for pair in numa_node.paired_threads.paired_thread_ids:
449 vnfc["numas"][0]["paired-threads-id"].append(
450 [pair.thread_a, pair.thread_b]
451 )
452
453 else:
454 if vdu.vm_flavor.has_field("vcpu_count"):
455 vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1)
456
457 else:
458 if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count:
459 vnfc["vcpus"] = vdu.vm_flavor.vcpu_count
460
461 if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb:
462 vnfc["ram"] = vdu.vm_flavor.memory_mb
463
464
465 if vdu.has_field("hypervisor_epa"):
466 vnfc["hypervisor"] = {}
467 if vdu.hypervisor_epa.has_field("type"):
468 if vdu.hypervisor_epa.type_yang == "REQUIRE_KVM":
469 vnfc["hypervisor"]["type"] = "QEMU-kvm"
470
471 if vdu.hypervisor_epa.has_field("version"):
472 vnfc["hypervisor"]["version"] = vdu.hypervisor_epa.version
473
474 if vdu.has_field("host_epa"):
475 vnfc["processor"] = {}
476 if vdu.host_epa.has_field("om_cpu_model_string"):
477 vnfc["processor"]["model"] = vdu.host_epa.om_cpu_model_string
478 if vdu.host_epa.has_field("om_cpu_feature"):
479 vnfc["processor"]["features"] = []
480 for feature in vdu.host_epa.om_cpu_feature:
481 vnfc["processor"]["features"].append(feature.feature)
482
483
484 vnf["VNFC"].append(vnfc)
485
486 for int_if in list(vdu.internal_interface) + list(vdu.external_interface):
487 intf = {
488 "name": int_if.name,
489 }
490 if int_if.virtual_interface.has_field("vpci"):
491 intf["vpci"] = int_if.virtual_interface.vpci
492
493 if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]:
494 intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang)
495 vnfc["bridge-ifaces"].append(intf)
496
497 elif int_if.virtual_interface.type_yang in ["OM_MGMT"]:
498 vnfc["bridge-ifaces"].append(intf)
499
500 elif int_if.virtual_interface.type_yang == "SR_IOV":
501 intf["bandwidth"] = "10 Gbps"
502 intf["dedicated"] = "no"
503 vnfc["numas"][0]["interfaces"].append(intf)
504
505 elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH":
506 intf["bandwidth"] = "10 Gbps"
507 intf["dedicated"] = "yes"
508 if "interfaces" not in vnfc["numas"][0]:
509 vnfc["numas"][0]["interfaces"] = []
510 vnfc["numas"][0]["interfaces"].append(intf)
511 else:
512 raise ValueError("Interface type %s not supported" % int_if.virtual_interface)
513
514 if int_if.virtual_interface.has_field("bandwidth"):
515 if int_if.virtual_interface.bandwidth != 0:
516 bps = int_if.virtual_interface.bandwidth
517
518 # Calculate the bits per second conversion
519 for x in [('M', 1000000), ('G', 1000000000)]:
520 if bps/x[1] >= 1:
521 intf["bandwidth"] = "{} {}bps".format(math.ceil(bps/x[1]), x[0])
522
523
524 return openmano_vnf
525
526
527 def parse_args(argv=sys.argv[1:]):
528 """ Parse the command line arguments
529
530 Arguments:
531 arv - The list of arguments to parse
532
533 Returns:
534 Argparse Namespace instance
535 """
536 parser = argparse.ArgumentParser()
537 parser.add_argument(
538 '-o', '--outdir',
539 default='-',
540 help="Directory to output converted descriptors. Default is stdout",
541 )
542
543 parser.add_argument(
544 '-n', '--nsd-file-hdl',
545 metavar="nsd_xml_file",
546 type=argparse.FileType('r'),
547 help="Rift NSD Descriptor File",
548 )
549
550 parser.add_argument(
551 '-v', '--vnfd-file-hdls',
552 metavar="vnfd_xml_file",
553 action='append',
554 type=argparse.FileType('r'),
555 help="Rift VNFD Descriptor File",
556 )
557
558 args = parser.parse_args(argv)
559
560 if not os.path.exists(args.outdir):
561 os.makedirs(args.outdir)
562
563 if not is_writable_directory(args.outdir):
564 logging.error("Directory %s is not writable", args.outdir)
565 sys.exit(1)
566
567 return args
568
569
570 def write_yaml_to_file(name, outdir, desc_dict):
571 file_name = "%s.yaml" % name
572 yaml_str = yaml.dump(desc_dict)
573 if outdir == "-":
574 sys.stdout.write(yaml_str)
575 return
576
577 file_path = os.path.join(outdir, file_name)
578 dir_path = os.path.dirname(file_path)
579 if not os.path.exists(dir_path):
580 os.makedirs(dir_path)
581
582 with open(file_path, "w") as hdl:
583 hdl.write(yaml_str)
584
585 logger.info("Wrote descriptor to %s", file_path)
586
587
588 def main(argv=sys.argv[1:]):
589 args = parse_args(argv)
590
591 nsd = None
592 if args.vnfd_file_hdls is not None:
593 vnf_dict = create_vnfd_from_xml_files(args.vnfd_file_hdls)
594
595 if args.nsd_file_hdl is not None:
596 nsd = create_nsd_from_xml_file(args.nsd_file_hdl)
597
598 openmano_nsd = rift2openmano_nsd(nsd, vnf_dict)
599
600 write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd)
601
602 for vnf in vnf_dict.values():
603 openmano_vnf = rift2openmano_vnfd(vnf)
604 write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf)
605
606
607 if __name__ == "__main__":
608 logging.basicConfig(level=logging.WARNING)
609 main()