| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| Philip Joseph | 4f97bde | 2016-09-30 19:24:38 +0530 | [diff] [blame] | 3 | # |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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 | |
| Akshay Singhal | c5aabae | 2016-09-30 14:40:14 -0400 | [diff] [blame] | 28 | import gi |
| 29 | gi.require_version('RwYang', '1.0') |
| Philip Joseph | 4f810f2 | 2017-03-07 23:09:10 +0530 | [diff] [blame] | 30 | gi.require_version('RwProjectVnfdYang', '1.0') |
| 31 | gi.require_version('RwProjectNsdYang', '1.0') |
| Akshay Singhal | c5aabae | 2016-09-30 14:40:14 -0400 | [diff] [blame] | 32 | |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 33 | from gi.repository import ( |
| 34 | RwYang, |
| Philip Joseph | 4f810f2 | 2017-03-07 23:09:10 +0530 | [diff] [blame] | 35 | RwProjectVnfdYang as RwVnfdYang, |
| 36 | RwProjectNsdYang as RwNsdYang, |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 37 | ) |
| 38 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 39 | import rift.package.store |
| 40 | import rift.package.cloud_init |
| 41 | |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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) |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 91 | descriptor = RwNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd() |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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) |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 98 | descriptor = RwNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd() |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 99 | descriptor.from_yaml(RiftNSD.model, hdl.read()) |
| 100 | return cls(descriptor) |
| 101 | |
| 102 | @classmethod |
| 103 | def from_dict(cls, nsd_dict): |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 104 | descriptor = RwNsdYang.YangData_RwProject_Project_NsdCatalog_Nsd.from_dict(nsd_dict) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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) |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 146 | descriptor = RwVnfdYang.YangData_RwProject_Project_VnfdCatalog_Vnfd() |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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) |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 153 | descriptor = RwVnfdYang.YangData_RwProject_Project_VnfdCatalog_Vnfd() |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 154 | descriptor.from_yaml(RiftVNFD.model, hdl.read()) |
| 155 | return cls(descriptor) |
| 156 | |
| 157 | @classmethod |
| 158 | def from_dict(cls, vnfd_dict): |
| Philip Joseph | f493757 | 2017-03-03 01:55:37 +0530 | [diff] [blame] | 159 | descriptor = RwVnfdYang.YangData_RwProject_Project_VnfdCatalog_Vnfd.from_dict(vnfd_dict) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 181 | def create_vnfd_from_files(vnfd_file_hdls): |
| 182 | """ Create a list of RiftVNFD instances from xml/yaml file handles |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 183 | |
| 184 | Arguments: |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 185 | vnfd_file_hdls - Rift VNFD XML/YAML file handles |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 186 | |
| 187 | Returns: |
| 188 | A list of RiftVNFD instances |
| 189 | """ |
| 190 | vnfd_dict = {} |
| 191 | for vnfd_file_hdl in vnfd_file_hdls: |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 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) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 196 | vnfd_dict[vnfd.id] = vnfd |
| 197 | |
| 198 | return vnfd_dict |
| 199 | |
| 200 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 201 | def create_nsd_from_file(nsd_file_hdl): |
| 202 | """ Create a list of RiftNSD instances from yaml/xml file handles |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 203 | |
| 204 | Arguments: |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 205 | nsd_file_hdls - Rift NSD XML/yaml file handles |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 206 | |
| 207 | Returns: |
| 208 | A list of RiftNSD instances |
| 209 | """ |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 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) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 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 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 224 | def rift2openmano_nsd(rift_nsd, rift_vnfds, openmano_vnfd_ids): |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 225 | for vnfd_id in rift_nsd.vnfd_ids: |
| 226 | if vnfd_id not in rift_vnfds: |
| 227 | raise VNFNotFoundError("VNF id %s not provided" % vnfd_id) |
| 228 | |
| 229 | openmano = {} |
| 230 | openmano["name"] = rift_nsd.name |
| 231 | openmano["description"] = rift_nsd.description |
| 232 | topology = {} |
| 233 | openmano["topology"] = topology |
| 234 | |
| 235 | topology["nodes"] = {} |
| 236 | for vnfd in rift_nsd.constituent_vnfds: |
| 237 | vnfd_id = vnfd.vnfd_id_ref |
| 238 | rift_vnfd = rift_vnfds[vnfd_id] |
| 239 | member_idx = vnfd.member_vnf_index |
| balakrishn | 699e141 | 2016-09-19 06:41:01 -0400 | [diff] [blame] | 240 | openmano_vnfd_id = openmano_vnfd_ids.get(vnfd_id,None) |
| 241 | if openmano_vnfd_id: |
| 242 | topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = { |
| 243 | "type": "VNF", |
| 244 | "vnf_id": openmano_vnfd_id |
| 245 | } |
| 246 | else: |
| 247 | topology["nodes"][rift_vnfd.name + "__" + str(member_idx)] = { |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 248 | "type": "VNF", |
| 249 | "VNF model": rift_vnfd.name |
| 250 | } |
| 251 | |
| 252 | for vld in rift_nsd.vlds: |
| 253 | # Openmano has both bridge_net and dataplane_net models for network types |
| 254 | # For now, since we are using openmano in developer mode lets just hardcode |
| 255 | # to bridge_net since it won't matter anyways. |
| 256 | # topology["nodes"][vld.name] = {"type": "network", "model": "bridge_net"} |
| 257 | pass |
| 258 | |
| 259 | topology["connections"] = {} |
| 260 | for vld in rift_nsd.vlds: |
| 261 | |
| 262 | # Create a connections entry for each external VLD |
| 263 | topology["connections"][vld.name] = {} |
| 264 | topology["connections"][vld.name]["nodes"] = [] |
| 265 | |
| Suresh Balakrishnan | 6b732c7 | 2016-09-11 05:33:19 -0400 | [diff] [blame] | 266 | #if vld.vim_network_name: |
| 267 | if True: |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 268 | if vld.name not in topology["nodes"]: |
| 269 | topology["nodes"][vld.name] = { |
| 270 | "type": "external_network", |
| 271 | "model": vld.name, |
| 272 | } |
| 273 | |
| 274 | # Add the external network to the list of connection points |
| 275 | topology["connections"][vld.name]["nodes"].append( |
| 276 | {vld.name: "0"} |
| 277 | ) |
| 278 | elif vld.provider_network.has_field("physical_network"): |
| 279 | # Add the external datacenter network to the topology |
| 280 | # node list if it isn't already added |
| 281 | ext_net_name = vld.provider_network.physical_network |
| 282 | ext_net_name_with_seg = ext_net_name |
| 283 | if vld.provider_network.has_field("segmentation_id"): |
| 284 | ext_net_name_with_seg += ":{}".format(vld.provider_network.segmentation_id) |
| 285 | |
| 286 | if ext_net_name not in topology["nodes"]: |
| 287 | topology["nodes"][ext_net_name] = { |
| 288 | "type": "external_network", |
| 289 | "model": ext_net_name_with_seg, |
| 290 | } |
| 291 | |
| 292 | # Add the external network to the list of connection points |
| 293 | topology["connections"][vld.name]["nodes"].append( |
| 294 | {ext_net_name: "0"} |
| 295 | ) |
| 296 | |
| 297 | |
| 298 | for vnfd_cp in vld.vnfd_connection_point_ref: |
| 299 | |
| 300 | # Get the RIFT VNF for this external VLD connection point |
| 301 | vnfd = rift_vnfds[vnfd_cp.vnfd_id_ref] |
| 302 | |
| 303 | # For each VNF in this connection, use the same interface name |
| 304 | topology["connections"][vld.name]["type"] = "link" |
| 305 | # Vnf ref is the vnf name with the member_vnf_idx appended |
| 306 | member_idx = vnfd_cp.member_vnf_index_ref |
| 307 | vnf_ref = vnfd.name + "__" + str(member_idx) |
| 308 | topology["connections"][vld.name]["nodes"].append( |
| 309 | { |
| 310 | vnf_ref: vnfd_cp.vnfd_connection_point_ref |
| 311 | } |
| 312 | ) |
| 313 | |
| 314 | return openmano |
| 315 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 316 | def cloud_init(rift_vnfd_id, vdu): |
| chamarty | edb4310 | 2017-01-11 01:45:58 +0000 | [diff] [blame] | 317 | """ Populate cloud_init with script from |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 318 | either the inline contents or from the file provided |
| 319 | """ |
| 320 | vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger) |
| 321 | |
| 322 | cloud_init_msg = None |
| 323 | if vdu.cloud_init is not None: |
| 324 | logger.debug("cloud_init script provided inline %s", vdu.cloud_init) |
| 325 | cloud_init_msg = vdu.cloud_init |
| 326 | elif vdu.cloud_init_file is not None: |
| 327 | # Get cloud-init script contents from the file provided in the cloud_init_file param |
| 328 | logger.debug("cloud_init script provided in file %s", vdu.cloud_init_file) |
| 329 | filename = vdu.cloud_init_file |
| 330 | vnfd_package_store.refresh() |
| 331 | stored_package = vnfd_package_store.get_package(rift_vnfd_id) |
| 332 | cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger) |
| 333 | try: |
| 334 | cloud_init_msg = cloud_init_extractor.read_script(stored_package, filename) |
| 335 | except rift.package.cloud_init.CloudInitExtractionError as e: |
| 336 | raise ValueError(e) |
| 337 | else: |
| 338 | logger.debug("VDU translation: cloud-init script not provided") |
| 339 | return |
| 340 | |
| 341 | logger.debug("Current cloud init msg is {}".format(cloud_init_msg)) |
| chamarty | edb4310 | 2017-01-11 01:45:58 +0000 | [diff] [blame] | 342 | return cloud_init_msg |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 343 | |
| chamarty | b45f1ae | 2017-02-02 17:39:43 +0000 | [diff] [blame] | 344 | def config_file_init(rift_vnfd_id, vdu, cfg_file): |
| 345 | """ Populate config file init with file provided |
| 346 | """ |
| 347 | vnfd_package_store = rift.package.store.VnfdPackageFilesystemStore(logger) |
| 348 | |
| 349 | # Get script contents from the file provided in the cloud_init directory |
| 350 | logger.debug("config file script provided in file {}".format(cfg_file)) |
| 351 | filename = cfg_file |
| 352 | vnfd_package_store.refresh() |
| 353 | stored_package = vnfd_package_store.get_package(rift_vnfd_id) |
| 354 | cloud_init_extractor = rift.package.cloud_init.PackageCloudInitExtractor(logger) |
| 355 | try: |
| 356 | cfg_file_msg = cloud_init_extractor.read_script(stored_package, filename) |
| 357 | except rift.package.cloud_init.CloudInitExtractionError as e: |
| 358 | raise ValueError(e) |
| 359 | |
| 360 | logger.debug("Current config file msg is {}".format(cfg_file_msg)) |
| 361 | return cfg_file_msg |
| 362 | |
| Varun Prasad | d9f88e4 | 2016-11-28 02:25:42 -0500 | [diff] [blame] | 363 | def rift2openmano_vnfd(rift_vnfd, rift_nsd): |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 364 | openmano_vnf = {"vnf":{}} |
| 365 | vnf = openmano_vnf["vnf"] |
| 366 | |
| 367 | vnf["name"] = rift_vnfd.name |
| 368 | vnf["description"] = rift_vnfd.description |
| 369 | |
| 370 | vnf["external-connections"] = [] |
| 371 | |
| 372 | def find_vdu_and_ext_if_by_cp_ref(cp_ref_name): |
| 373 | for vdu in rift_vnfd.vdus: |
| 374 | for ext_if in vdu.external_interface: |
| 375 | if ext_if.vnfd_connection_point_ref == cp_ref_name: |
| 376 | return vdu, ext_if |
| 377 | |
| 378 | raise ValueError("External connection point reference %s not found" % cp_ref_name) |
| 379 | |
| 380 | def find_vdu_and_int_if_by_cp_ref(cp_ref_id): |
| 381 | for vdu in rift_vnfd.vdus: |
| 382 | for int_if in vdu.internal_interface: |
| 383 | if int_if.vdu_internal_connection_point_ref == cp_ref_id: |
| 384 | return vdu, int_if |
| 385 | |
| 386 | raise ValueError("Internal connection point reference %s not found" % cp_ref_id) |
| 387 | |
| Varun Prasad | d9f88e4 | 2016-11-28 02:25:42 -0500 | [diff] [blame] | 388 | def rift2openmano_if_type(ext_if): |
| 389 | |
| 390 | cp_ref_name = ext_if.vnfd_connection_point_ref |
| 391 | for vld in rift_nsd.vlds: |
| 392 | |
| 393 | # if it is an explicit mgmt_network then check if the given |
| 394 | # cp_ref is a part of it |
| 395 | if not vld.mgmt_network: |
| 396 | continue |
| 397 | |
| 398 | for vld_cp in vld.vnfd_connection_point_ref: |
| 399 | if vld_cp.vnfd_connection_point_ref == cp_ref_name: |
| 400 | return "mgmt" |
| 401 | |
| 402 | |
| 403 | rift_type = ext_if.virtual_interface.type_yang |
| 404 | # Retaining it for backward compatibility! |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 405 | if rift_type == "OM_MGMT": |
| 406 | return "mgmt" |
| Akshay Singhal | c5aabae | 2016-09-30 14:40:14 -0400 | [diff] [blame] | 407 | elif rift_type == "VIRTIO" or rift_type == "E1000": |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 408 | return "bridge" |
| 409 | else: |
| 410 | return "data" |
| 411 | |
| Akshay Singhal | c5aabae | 2016-09-30 14:40:14 -0400 | [diff] [blame] | 412 | def rift2openmano_vif(rift_type): |
| 413 | if rift_type == "VIRTIO": |
| 414 | return "virtio" |
| 415 | elif rift_type == "E1000": |
| 416 | return "e1000" |
| 417 | else: |
| 418 | raise ValueError("VDU Virtual Interface type {} not supported".format(rift_type)) |
| 419 | |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 420 | # Add all external connections |
| 421 | for cp in rift_vnfd.cps: |
| 422 | # Find the VDU and and external interface for this connection point |
| 423 | vdu, ext_if = find_vdu_and_ext_if_by_cp_ref(cp.name) |
| 424 | connection = { |
| 425 | "name": cp.name, |
| Varun Prasad | d9f88e4 | 2016-11-28 02:25:42 -0500 | [diff] [blame] | 426 | "type": rift2openmano_if_type(ext_if), |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 427 | "VNFC": vdu.name, |
| 428 | "local_iface_name": ext_if.name, |
| 429 | "description": "%s iface on VDU %s" % (ext_if.name, vdu.name), |
| 430 | } |
| 431 | |
| 432 | vnf["external-connections"].append(connection) |
| 433 | |
| 434 | # Add all internal networks |
| 435 | for vld in rift_vnfd.internal_vlds: |
| 436 | connection = { |
| 437 | "name": vld.name, |
| 438 | "description": vld.description, |
| chamarty | 30118f2 | 2017-01-26 14:14:30 +0000 | [diff] [blame] | 439 | "type": "bridge", |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 440 | "elements": [], |
| 441 | } |
| 442 | |
| 443 | # Add the specific VDU connection points |
| KIRAN KASHALKAR | f6914d7 | 2016-10-21 15:31:59 +0000 | [diff] [blame] | 444 | for int_cp in vld.internal_connection_point: |
| 445 | vdu, int_if = find_vdu_and_int_if_by_cp_ref(int_cp.id_ref) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 446 | connection["elements"].append({ |
| 447 | "VNFC": vdu.name, |
| 448 | "local_iface_name": int_if.name, |
| 449 | }) |
| 450 | if "internal-connections" not in vnf: |
| 451 | vnf["internal-connections"] = [] |
| 452 | |
| 453 | vnf["internal-connections"].append(connection) |
| 454 | |
| 455 | # Add VDU's |
| 456 | vnf["VNFC"] = [] |
| 457 | for vdu in rift_vnfd.vdus: |
| 458 | vnfc = { |
| 459 | "name": vdu.name, |
| 460 | "description": vdu.name, |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 461 | "bridge-ifaces": [], |
| 462 | } |
| 463 | |
| Rajesh Velandy | 4a34ba7 | 2016-10-20 20:07:52 +0000 | [diff] [blame] | 464 | if vdu.vm_flavor.has_field("storage_gb") and vdu.vm_flavor.storage_gb: |
| 465 | vnfc["disk"] = vdu.vm_flavor.storage_gb |
| 466 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 467 | if vdu.has_field("image"): |
| 468 | if os.path.isabs(vdu.image): |
| 469 | vnfc["VNFC image"] = vdu.image |
| 470 | else: |
| 471 | vnfc["image name"] = vdu.image |
| 472 | if vdu.has_field("image_checksum"): |
| 473 | vnfc["image checksum"] = vdu.image_checksum |
| 474 | |
| Rajesh Velandy | 54627a4 | 2016-11-20 20:25:19 +0000 | [diff] [blame] | 475 | dedicated_int = False |
| 476 | for intf in list(vdu.internal_interface) + list(vdu.external_interface): |
| 477 | if intf.virtual_interface.type_yang in ["SR_IOV", "PCI_PASSTHROUGH"]: |
| 478 | dedicated_int = True |
| 479 | if vdu.guest_epa.has_field("numa_node_policy") or dedicated_int: |
| Rajesh Velandy | 4a34ba7 | 2016-10-20 20:07:52 +0000 | [diff] [blame] | 480 | vnfc["numas"] = [{ |
| 481 | "memory": max(int(vdu.vm_flavor.memory_mb/1024), 1), |
| 482 | "interfaces":[], |
| 483 | }] |
| 484 | numa_node_policy = vdu.guest_epa.numa_node_policy |
| 485 | if numa_node_policy.has_field("node"): |
| 486 | numa_node = numa_node_policy.node[0] |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 487 | |
| Rajesh Velandy | 4a34ba7 | 2016-10-20 20:07:52 +0000 | [diff] [blame] | 488 | if numa_node.has_field("paired_threads"): |
| 489 | if numa_node.paired_threads.has_field("num_paired_threads"): |
| 490 | vnfc["numas"][0]["paired-threads"] = numa_node.paired_threads.num_paired_threads |
| 491 | if len(numa_node.paired_threads.paired_thread_ids) > 0: |
| 492 | vnfc["numas"][0]["paired-threads-id"] = [] |
| 493 | for pair in numa_node.paired_threads.paired_thread_ids: |
| 494 | vnfc["numas"][0]["paired-threads-id"].append( |
| 495 | [pair.thread_a, pair.thread_b] |
| 496 | ) |
| 497 | |
| 498 | else: |
| 499 | if vdu.vm_flavor.has_field("vcpu_count"): |
| 500 | vnfc["numas"][0]["cores"] = max(vdu.vm_flavor.vcpu_count, 1) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 501 | |
| Jithin Jose | 1ed7050 | 2017-04-03 10:32:58 +0000 | [diff] [blame] | 502 | if vdu.vm_flavor.has_field("vcpu_count") and vdu.vm_flavor.vcpu_count: |
| 503 | vnfc["vcpus"] = vdu.vm_flavor.vcpu_count |
| Rajesh Velandy | 4a34ba7 | 2016-10-20 20:07:52 +0000 | [diff] [blame] | 504 | |
| Jithin Jose | 1ed7050 | 2017-04-03 10:32:58 +0000 | [diff] [blame] | 505 | if vdu.vm_flavor.has_field("memory_mb") and vdu.vm_flavor.memory_mb: |
| 506 | vnfc["ram"] = vdu.vm_flavor.memory_mb |
| Rajesh Velandy | 4a34ba7 | 2016-10-20 20:07:52 +0000 | [diff] [blame] | 507 | |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 508 | |
| 509 | if vdu.has_field("hypervisor_epa"): |
| 510 | vnfc["hypervisor"] = {} |
| 511 | if vdu.hypervisor_epa.has_field("type"): |
| 512 | if vdu.hypervisor_epa.type_yang == "REQUIRE_KVM": |
| 513 | vnfc["hypervisor"]["type"] = "QEMU-kvm" |
| 514 | |
| 515 | if vdu.hypervisor_epa.has_field("version"): |
| 516 | vnfc["hypervisor"]["version"] = vdu.hypervisor_epa.version |
| 517 | |
| 518 | if vdu.has_field("host_epa"): |
| 519 | vnfc["processor"] = {} |
| 520 | if vdu.host_epa.has_field("om_cpu_model_string"): |
| 521 | vnfc["processor"]["model"] = vdu.host_epa.om_cpu_model_string |
| 522 | if vdu.host_epa.has_field("om_cpu_feature"): |
| 523 | vnfc["processor"]["features"] = [] |
| 524 | for feature in vdu.host_epa.om_cpu_feature: |
| KIRAN KASHALKAR | f6914d7 | 2016-10-21 15:31:59 +0000 | [diff] [blame] | 525 | vnfc["processor"]["features"].append(feature.feature) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 526 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 527 | if vdu.has_field("volumes"): |
| 528 | vnfc["devices"] = [] |
| 529 | # Sort volumes as device-list is implictly ordered by Openmano |
| 530 | newvollist = sorted(vdu.volumes, key=lambda k: k.name) |
| 531 | for iter_num, volume in enumerate(newvollist): |
| 532 | if iter_num == 0: |
| 533 | # Convert the first volume to vnfc.image |
| 534 | if os.path.isabs(volume.image): |
| 535 | vnfc["VNFC image"] = volume.image |
| 536 | else: |
| 537 | vnfc["image name"] = volume.image |
| 538 | if volume.has_field("image_checksum"): |
| 539 | vnfc["image checksum"] = volume.image_checksum |
| 540 | else: |
| 541 | # Add Openmano devices |
| 542 | device = {} |
| chamarty | af80441 | 2017-01-08 21:55:03 +0000 | [diff] [blame] | 543 | device["type"] = volume.device_type |
| Chamarty | 4185956 | 2017-03-31 16:11:17 +0000 | [diff] [blame] | 544 | device["image name"] = volume.image |
| 545 | if volume.has_field("image_checksum"): |
| 546 | device["image checksum"] = volume.image_checksum |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 547 | vnfc["devices"].append(device) |
| 548 | |
| chamarty | edb4310 | 2017-01-11 01:45:58 +0000 | [diff] [blame] | 549 | vnfc_boot_data_init = False |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 550 | if vdu.has_field("cloud_init") or vdu.has_field("cloud_init_file"): |
| chamarty | edb4310 | 2017-01-11 01:45:58 +0000 | [diff] [blame] | 551 | vnfc['boot-data'] = dict() |
| 552 | vnfc_boot_data_init = True |
| 553 | vnfc['boot-data']['user-data'] = cloud_init(rift_vnfd.id, vdu) |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 554 | |
| chamarty | af80441 | 2017-01-08 21:55:03 +0000 | [diff] [blame] | 555 | if vdu.has_field("supplemental_boot_data"): |
| 556 | if vdu.supplemental_boot_data.has_field('boot_data_drive'): |
| 557 | if vdu.supplemental_boot_data.boot_data_drive is True: |
| chamarty | edb4310 | 2017-01-11 01:45:58 +0000 | [diff] [blame] | 558 | if vnfc_boot_data_init is False: |
| 559 | vnfc['boot-data'] = dict() |
| 560 | vnfc_boot_data_init = True |
| 561 | vnfc['boot-data']['boot-data-drive'] = vdu.supplemental_boot_data.boot_data_drive |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 562 | |
| chamarty | b45f1ae | 2017-02-02 17:39:43 +0000 | [diff] [blame] | 563 | if vdu.supplemental_boot_data.has_field('config_file'): |
| 564 | om_cfgfile_list = list() |
| 565 | for custom_config_file in vdu.supplemental_boot_data.config_file: |
| 566 | cfg_source = config_file_init(rift_vnfd.id, vdu, custom_config_file.source) |
| 567 | om_cfgfile_list.append({"dest":custom_config_file.dest, "content": cfg_source}) |
| 568 | vnfc['boot-data']['config-files'] = om_cfgfile_list |
| 569 | |
| 570 | |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 571 | vnf["VNFC"].append(vnfc) |
| 572 | |
| 573 | for int_if in list(vdu.internal_interface) + list(vdu.external_interface): |
| 574 | intf = { |
| 575 | "name": int_if.name, |
| 576 | } |
| 577 | if int_if.virtual_interface.has_field("vpci"): |
| 578 | intf["vpci"] = int_if.virtual_interface.vpci |
| 579 | |
| Akshay Singhal | c5aabae | 2016-09-30 14:40:14 -0400 | [diff] [blame] | 580 | if int_if.virtual_interface.type_yang in ["VIRTIO", "E1000"]: |
| 581 | intf["model"] = rift2openmano_vif(int_if.virtual_interface.type_yang) |
| 582 | vnfc["bridge-ifaces"].append(intf) |
| 583 | |
| 584 | elif int_if.virtual_interface.type_yang in ["OM_MGMT"]: |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 585 | vnfc["bridge-ifaces"].append(intf) |
| 586 | |
| Rajesh Velandy | 54627a4 | 2016-11-20 20:25:19 +0000 | [diff] [blame] | 587 | elif int_if.virtual_interface.type_yang == "SR_IOV": |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 588 | intf["bandwidth"] = "10 Gbps" |
| Rajesh Velandy | 54627a4 | 2016-11-20 20:25:19 +0000 | [diff] [blame] | 589 | intf["dedicated"] = "no" |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 590 | vnfc["numas"][0]["interfaces"].append(intf) |
| 591 | |
| 592 | elif int_if.virtual_interface.type_yang == "PCI_PASSTHROUGH": |
| 593 | intf["bandwidth"] = "10 Gbps" |
| 594 | intf["dedicated"] = "yes" |
| 595 | if "interfaces" not in vnfc["numas"][0]: |
| 596 | vnfc["numas"][0]["interfaces"] = [] |
| 597 | vnfc["numas"][0]["interfaces"].append(intf) |
| 598 | else: |
| 599 | raise ValueError("Interface type %s not supported" % int_if.virtual_interface) |
| 600 | |
| 601 | if int_if.virtual_interface.has_field("bandwidth"): |
| 602 | if int_if.virtual_interface.bandwidth != 0: |
| 603 | bps = int_if.virtual_interface.bandwidth |
| 604 | |
| 605 | # Calculate the bits per second conversion |
| 606 | for x in [('M', 1000000), ('G', 1000000000)]: |
| 607 | if bps/x[1] >= 1: |
| 608 | intf["bandwidth"] = "{} {}bps".format(math.ceil(bps/x[1]), x[0]) |
| 609 | |
| chamarty | 6844a40 | 2017-01-26 16:32:24 +0000 | [diff] [blame] | 610 | # Sort bridge-ifaces-list TODO sort others |
| 611 | newlist = sorted(vnfc["bridge-ifaces"], key=lambda k: k['name']) |
| 612 | vnfc["bridge-ifaces"] = newlist |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 613 | |
| 614 | return openmano_vnf |
| 615 | |
| 616 | |
| 617 | def parse_args(argv=sys.argv[1:]): |
| 618 | """ Parse the command line arguments |
| 619 | |
| 620 | Arguments: |
| 621 | arv - The list of arguments to parse |
| 622 | |
| 623 | Returns: |
| 624 | Argparse Namespace instance |
| 625 | """ |
| 626 | parser = argparse.ArgumentParser() |
| 627 | parser.add_argument( |
| 628 | '-o', '--outdir', |
| 629 | default='-', |
| 630 | help="Directory to output converted descriptors. Default is stdout", |
| 631 | ) |
| 632 | |
| 633 | parser.add_argument( |
| 634 | '-n', '--nsd-file-hdl', |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 635 | metavar="nsd_file", |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 636 | type=argparse.FileType('r'), |
| 637 | help="Rift NSD Descriptor File", |
| 638 | ) |
| 639 | |
| 640 | parser.add_argument( |
| 641 | '-v', '--vnfd-file-hdls', |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 642 | metavar="vnfd_file", |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 643 | action='append', |
| 644 | type=argparse.FileType('r'), |
| 645 | help="Rift VNFD Descriptor File", |
| 646 | ) |
| 647 | |
| 648 | args = parser.parse_args(argv) |
| 649 | |
| 650 | if not os.path.exists(args.outdir): |
| 651 | os.makedirs(args.outdir) |
| 652 | |
| 653 | if not is_writable_directory(args.outdir): |
| 654 | logging.error("Directory %s is not writable", args.outdir) |
| 655 | sys.exit(1) |
| 656 | |
| 657 | return args |
| 658 | |
| 659 | |
| 660 | def write_yaml_to_file(name, outdir, desc_dict): |
| 661 | file_name = "%s.yaml" % name |
| 662 | yaml_str = yaml.dump(desc_dict) |
| 663 | if outdir == "-": |
| 664 | sys.stdout.write(yaml_str) |
| 665 | return |
| 666 | |
| 667 | file_path = os.path.join(outdir, file_name) |
| 668 | dir_path = os.path.dirname(file_path) |
| 669 | if not os.path.exists(dir_path): |
| 670 | os.makedirs(dir_path) |
| 671 | |
| 672 | with open(file_path, "w") as hdl: |
| 673 | hdl.write(yaml_str) |
| 674 | |
| 675 | logger.info("Wrote descriptor to %s", file_path) |
| 676 | |
| 677 | |
| 678 | def main(argv=sys.argv[1:]): |
| 679 | args = parse_args(argv) |
| 680 | |
| 681 | nsd = None |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 682 | openmano_vnfr_ids = dict() |
| 683 | vnf_dict = None |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 684 | if args.vnfd_file_hdls is not None: |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 685 | vnf_dict = create_vnfd_from_files(args.vnfd_file_hdls) |
| 686 | |
| 687 | for vnfd in vnf_dict: |
| 688 | openmano_vnfr_ids[vnfd] = vnfd |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 689 | |
| 690 | if args.nsd_file_hdl is not None: |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 691 | nsd = create_nsd_from_file(args.nsd_file_hdl) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 692 | |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 693 | openmano_nsd = rift2openmano_nsd(nsd, vnf_dict, openmano_vnfr_ids) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 694 | |
| 695 | write_yaml_to_file(openmano_nsd["name"], args.outdir, openmano_nsd) |
| 696 | |
| 697 | for vnf in vnf_dict.values(): |
| chamarty | 9c4eca9 | 2016-12-16 23:12:00 +0000 | [diff] [blame] | 698 | openmano_vnf = rift2openmano_vnfd(vnf, nsd) |
| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 699 | write_yaml_to_file(openmano_vnf["vnf"]["name"], args.outdir, openmano_vnf) |
| 700 | |
| 701 | |
| 702 | if __name__ == "__main__": |
| 703 | logging.basicConfig(level=logging.WARNING) |
| 704 | main() |