6b46dc1197d4e4cb13fcdbd7331df0f04961cd05
[osm/RO.git] / RO-VIM-openstack / osm_rovim_openstack / vimconn_openstack.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 ##
20
21 """
22 osconnector implements all the methods to interact with openstack using the python-neutronclient.
23
24 For the VNF forwarding graph, The OpenStack VIM connector calls the
25 networking-sfc Neutron extension methods, whose resources are mapped
26 to the VIM connector's SFC resources as follows:
27 - Classification (OSM) -> Flow Classifier (Neutron)
28 - Service Function Instance (OSM) -> Port Pair (Neutron)
29 - Service Function (OSM) -> Port Pair Group (Neutron)
30 - Service Function Path (OSM) -> Port Chain (Neutron)
31 """
32
33 import copy
34 from http.client import HTTPException
35 import json
36 import logging
37 from pprint import pformat
38 import random
39 import re
40 import time
41
42 from cinderclient import client as cClient
43 from glanceclient import client as glClient
44 import glanceclient.exc as gl1Exceptions
45 from keystoneauth1 import session
46 from keystoneauth1.identity import v2, v3
47 import keystoneclient.exceptions as ksExceptions
48 import keystoneclient.v2_0.client as ksClient_v2
49 import keystoneclient.v3.client as ksClient_v3
50 import netaddr
51 from neutronclient.common import exceptions as neExceptions
52 from neutronclient.neutron import client as neClient
53 from novaclient import client as nClient, exceptions as nvExceptions
54 from osm_ro_plugin import vimconn
55 from requests.exceptions import ConnectionError
56 import yaml
57
58 __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
59 __date__ = "$22-sep-2017 23:59:59$"
60
61 """contain the openstack virtual machine status to openmano status"""
62 vmStatus2manoFormat = {
63 "ACTIVE": "ACTIVE",
64 "PAUSED": "PAUSED",
65 "SUSPENDED": "SUSPENDED",
66 "SHUTOFF": "INACTIVE",
67 "BUILD": "BUILD",
68 "ERROR": "ERROR",
69 "DELETED": "DELETED",
70 }
71 netStatus2manoFormat = {
72 "ACTIVE": "ACTIVE",
73 "PAUSED": "PAUSED",
74 "INACTIVE": "INACTIVE",
75 "BUILD": "BUILD",
76 "ERROR": "ERROR",
77 "DELETED": "DELETED",
78 }
79
80 supportedClassificationTypes = ["legacy_flow_classifier"]
81
82 # global var to have a timeout creating and deleting volumes
83 volume_timeout = 1800
84 server_timeout = 1800
85
86
87 class SafeDumper(yaml.SafeDumper):
88 def represent_data(self, data):
89 # Openstack APIs use custom subclasses of dict and YAML safe dumper
90 # is designed to not handle that (reference issue 142 of pyyaml)
91 if isinstance(data, dict) and data.__class__ != dict:
92 # A simple solution is to convert those items back to dicts
93 data = dict(data.items())
94
95 return super(SafeDumper, self).represent_data(data)
96
97
98 class vimconnector(vimconn.VimConnector):
99 def __init__(
100 self,
101 uuid,
102 name,
103 tenant_id,
104 tenant_name,
105 url,
106 url_admin=None,
107 user=None,
108 passwd=None,
109 log_level=None,
110 config={},
111 persistent_info={},
112 ):
113 """using common constructor parameters. In this case
114 'url' is the keystone authorization url,
115 'url_admin' is not use
116 """
117 api_version = config.get("APIversion")
118
119 if api_version and api_version not in ("v3.3", "v2.0", "2", "3"):
120 raise vimconn.VimConnException(
121 "Invalid value '{}' for config:APIversion. "
122 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version)
123 )
124
125 vim_type = config.get("vim_type")
126
127 if vim_type and vim_type not in ("vio", "VIO"):
128 raise vimconn.VimConnException(
129 "Invalid value '{}' for config:vim_type."
130 "Allowed values are 'vio' or 'VIO'".format(vim_type)
131 )
132
133 if config.get("dataplane_net_vlan_range") is not None:
134 # validate vlan ranges provided by user
135 self._validate_vlan_ranges(
136 config.get("dataplane_net_vlan_range"), "dataplane_net_vlan_range"
137 )
138
139 if config.get("multisegment_vlan_range") is not None:
140 # validate vlan ranges provided by user
141 self._validate_vlan_ranges(
142 config.get("multisegment_vlan_range"), "multisegment_vlan_range"
143 )
144
145 vimconn.VimConnector.__init__(
146 self,
147 uuid,
148 name,
149 tenant_id,
150 tenant_name,
151 url,
152 url_admin,
153 user,
154 passwd,
155 log_level,
156 config,
157 )
158
159 if self.config.get("insecure") and self.config.get("ca_cert"):
160 raise vimconn.VimConnException(
161 "options insecure and ca_cert are mutually exclusive"
162 )
163
164 self.verify = True
165
166 if self.config.get("insecure"):
167 self.verify = False
168
169 if self.config.get("ca_cert"):
170 self.verify = self.config.get("ca_cert")
171
172 if not url:
173 raise TypeError("url param can not be NoneType")
174
175 self.persistent_info = persistent_info
176 self.availability_zone = persistent_info.get("availability_zone", None)
177 self.session = persistent_info.get("session", {"reload_client": True})
178 self.my_tenant_id = self.session.get("my_tenant_id")
179 self.nova = self.session.get("nova")
180 self.neutron = self.session.get("neutron")
181 self.cinder = self.session.get("cinder")
182 self.glance = self.session.get("glance")
183 # self.glancev1 = self.session.get("glancev1")
184 self.keystone = self.session.get("keystone")
185 self.api_version3 = self.session.get("api_version3")
186 self.vim_type = self.config.get("vim_type")
187
188 if self.vim_type:
189 self.vim_type = self.vim_type.upper()
190
191 if self.config.get("use_internal_endpoint"):
192 self.endpoint_type = "internalURL"
193 else:
194 self.endpoint_type = None
195
196 logging.getLogger("urllib3").setLevel(logging.WARNING)
197 logging.getLogger("keystoneauth").setLevel(logging.WARNING)
198 logging.getLogger("novaclient").setLevel(logging.WARNING)
199 self.logger = logging.getLogger("ro.vim.openstack")
200
201 # allow security_groups to be a list or a single string
202 if isinstance(self.config.get("security_groups"), str):
203 self.config["security_groups"] = [self.config["security_groups"]]
204
205 self.security_groups_id = None
206
207 # ###### VIO Specific Changes #########
208 if self.vim_type == "VIO":
209 self.logger = logging.getLogger("ro.vim.vio")
210
211 if log_level:
212 self.logger.setLevel(getattr(logging, log_level))
213
214 def __getitem__(self, index):
215 """Get individuals parameters.
216 Throw KeyError"""
217 if index == "project_domain_id":
218 return self.config.get("project_domain_id")
219 elif index == "user_domain_id":
220 return self.config.get("user_domain_id")
221 else:
222 return vimconn.VimConnector.__getitem__(self, index)
223
224 def __setitem__(self, index, value):
225 """Set individuals parameters and it is marked as dirty so to force connection reload.
226 Throw KeyError"""
227 if index == "project_domain_id":
228 self.config["project_domain_id"] = value
229 elif index == "user_domain_id":
230 self.config["user_domain_id"] = value
231 else:
232 vimconn.VimConnector.__setitem__(self, index, value)
233
234 self.session["reload_client"] = True
235
236 def serialize(self, value):
237 """Serialization of python basic types.
238
239 In the case value is not serializable a message will be logged and a
240 simple representation of the data that cannot be converted back to
241 python is returned.
242 """
243 if isinstance(value, str):
244 return value
245
246 try:
247 return yaml.dump(
248 value, Dumper=SafeDumper, default_flow_style=True, width=256
249 )
250 except yaml.representer.RepresenterError:
251 self.logger.debug(
252 "The following entity cannot be serialized in YAML:\n\n%s\n\n",
253 pformat(value),
254 exc_info=True,
255 )
256
257 return str(value)
258
259 def _reload_connection(self):
260 """Called before any operation, it check if credentials has changed
261 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
262 """
263 # TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
264 if self.session["reload_client"]:
265 if self.config.get("APIversion"):
266 self.api_version3 = (
267 self.config["APIversion"] == "v3.3"
268 or self.config["APIversion"] == "3"
269 )
270 else: # get from ending auth_url that end with v3 or with v2.0
271 self.api_version3 = self.url.endswith("/v3") or self.url.endswith(
272 "/v3/"
273 )
274
275 self.session["api_version3"] = self.api_version3
276
277 if self.api_version3:
278 if self.config.get("project_domain_id") or self.config.get(
279 "project_domain_name"
280 ):
281 project_domain_id_default = None
282 else:
283 project_domain_id_default = "default"
284
285 if self.config.get("user_domain_id") or self.config.get(
286 "user_domain_name"
287 ):
288 user_domain_id_default = None
289 else:
290 user_domain_id_default = "default"
291 auth = v3.Password(
292 auth_url=self.url,
293 username=self.user,
294 password=self.passwd,
295 project_name=self.tenant_name,
296 project_id=self.tenant_id,
297 project_domain_id=self.config.get(
298 "project_domain_id", project_domain_id_default
299 ),
300 user_domain_id=self.config.get(
301 "user_domain_id", user_domain_id_default
302 ),
303 project_domain_name=self.config.get("project_domain_name"),
304 user_domain_name=self.config.get("user_domain_name"),
305 )
306 else:
307 auth = v2.Password(
308 auth_url=self.url,
309 username=self.user,
310 password=self.passwd,
311 tenant_name=self.tenant_name,
312 tenant_id=self.tenant_id,
313 )
314
315 sess = session.Session(auth=auth, verify=self.verify)
316 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
317 # Titanium cloud and StarlingX
318 region_name = self.config.get("region_name")
319
320 if self.api_version3:
321 self.keystone = ksClient_v3.Client(
322 session=sess,
323 endpoint_type=self.endpoint_type,
324 region_name=region_name,
325 )
326 else:
327 self.keystone = ksClient_v2.Client(
328 session=sess, endpoint_type=self.endpoint_type
329 )
330
331 self.session["keystone"] = self.keystone
332 # In order to enable microversion functionality an explicit microversion must be specified in "config".
333 # This implementation approach is due to the warning message in
334 # https://developer.openstack.org/api-guide/compute/microversions.html
335 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
336 # always require an specific microversion.
337 # To be able to use "device role tagging" functionality define "microversion: 2.32" in datacenter config
338 version = self.config.get("microversion")
339
340 if not version:
341 version = "2.1"
342
343 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
344 # Titanium cloud and StarlingX
345 self.nova = self.session["nova"] = nClient.Client(
346 str(version),
347 session=sess,
348 endpoint_type=self.endpoint_type,
349 region_name=region_name,
350 )
351 self.neutron = self.session["neutron"] = neClient.Client(
352 "2.0",
353 session=sess,
354 endpoint_type=self.endpoint_type,
355 region_name=region_name,
356 )
357 self.cinder = self.session["cinder"] = cClient.Client(
358 2,
359 session=sess,
360 endpoint_type=self.endpoint_type,
361 region_name=region_name,
362 )
363
364 try:
365 self.my_tenant_id = self.session["my_tenant_id"] = sess.get_project_id()
366 except Exception:
367 self.logger.error("Cannot get project_id from session", exc_info=True)
368
369 if self.endpoint_type == "internalURL":
370 glance_service_id = self.keystone.services.list(name="glance")[0].id
371 glance_endpoint = self.keystone.endpoints.list(
372 glance_service_id, interface="internal"
373 )[0].url
374 else:
375 glance_endpoint = None
376
377 self.glance = self.session["glance"] = glClient.Client(
378 2, session=sess, endpoint=glance_endpoint
379 )
380 # using version 1 of glance client in new_image()
381 # self.glancev1 = self.session["glancev1"] = glClient.Client("1", session=sess,
382 # endpoint=glance_endpoint)
383 self.session["reload_client"] = False
384 self.persistent_info["session"] = self.session
385 # add availablity zone info inside self.persistent_info
386 self._set_availablity_zones()
387 self.persistent_info["availability_zone"] = self.availability_zone
388 # force to get again security_groups_ids next time they are needed
389 self.security_groups_id = None
390
391 def __net_os2mano(self, net_list_dict):
392 """Transform the net openstack format to mano format
393 net_list_dict can be a list of dict or a single dict"""
394 if type(net_list_dict) is dict:
395 net_list_ = (net_list_dict,)
396 elif type(net_list_dict) is list:
397 net_list_ = net_list_dict
398 else:
399 raise TypeError("param net_list_dict must be a list or a dictionary")
400 for net in net_list_:
401 if net.get("provider:network_type") == "vlan":
402 net["type"] = "data"
403 else:
404 net["type"] = "bridge"
405
406 def __classification_os2mano(self, class_list_dict):
407 """Transform the openstack format (Flow Classifier) to mano format
408 (Classification) class_list_dict can be a list of dict or a single dict
409 """
410 if isinstance(class_list_dict, dict):
411 class_list_ = [class_list_dict]
412 elif isinstance(class_list_dict, list):
413 class_list_ = class_list_dict
414 else:
415 raise TypeError("param class_list_dict must be a list or a dictionary")
416 for classification in class_list_:
417 id = classification.pop("id")
418 name = classification.pop("name")
419 description = classification.pop("description")
420 project_id = classification.pop("project_id")
421 tenant_id = classification.pop("tenant_id")
422 original_classification = copy.deepcopy(classification)
423 classification.clear()
424 classification["ctype"] = "legacy_flow_classifier"
425 classification["definition"] = original_classification
426 classification["id"] = id
427 classification["name"] = name
428 classification["description"] = description
429 classification["project_id"] = project_id
430 classification["tenant_id"] = tenant_id
431
432 def __sfi_os2mano(self, sfi_list_dict):
433 """Transform the openstack format (Port Pair) to mano format (SFI)
434 sfi_list_dict can be a list of dict or a single dict
435 """
436 if isinstance(sfi_list_dict, dict):
437 sfi_list_ = [sfi_list_dict]
438 elif isinstance(sfi_list_dict, list):
439 sfi_list_ = sfi_list_dict
440 else:
441 raise TypeError("param sfi_list_dict must be a list or a dictionary")
442
443 for sfi in sfi_list_:
444 sfi["ingress_ports"] = []
445 sfi["egress_ports"] = []
446
447 if sfi.get("ingress"):
448 sfi["ingress_ports"].append(sfi["ingress"])
449
450 if sfi.get("egress"):
451 sfi["egress_ports"].append(sfi["egress"])
452
453 del sfi["ingress"]
454 del sfi["egress"]
455 params = sfi.get("service_function_parameters")
456 sfc_encap = False
457
458 if params:
459 correlation = params.get("correlation")
460
461 if correlation:
462 sfc_encap = True
463
464 sfi["sfc_encap"] = sfc_encap
465 del sfi["service_function_parameters"]
466
467 def __sf_os2mano(self, sf_list_dict):
468 """Transform the openstack format (Port Pair Group) to mano format (SF)
469 sf_list_dict can be a list of dict or a single dict
470 """
471 if isinstance(sf_list_dict, dict):
472 sf_list_ = [sf_list_dict]
473 elif isinstance(sf_list_dict, list):
474 sf_list_ = sf_list_dict
475 else:
476 raise TypeError("param sf_list_dict must be a list or a dictionary")
477
478 for sf in sf_list_:
479 del sf["port_pair_group_parameters"]
480 sf["sfis"] = sf["port_pairs"]
481 del sf["port_pairs"]
482
483 def __sfp_os2mano(self, sfp_list_dict):
484 """Transform the openstack format (Port Chain) to mano format (SFP)
485 sfp_list_dict can be a list of dict or a single dict
486 """
487 if isinstance(sfp_list_dict, dict):
488 sfp_list_ = [sfp_list_dict]
489 elif isinstance(sfp_list_dict, list):
490 sfp_list_ = sfp_list_dict
491 else:
492 raise TypeError("param sfp_list_dict must be a list or a dictionary")
493
494 for sfp in sfp_list_:
495 params = sfp.pop("chain_parameters")
496 sfc_encap = False
497
498 if params:
499 correlation = params.get("correlation")
500
501 if correlation:
502 sfc_encap = True
503
504 sfp["sfc_encap"] = sfc_encap
505 sfp["spi"] = sfp.pop("chain_id")
506 sfp["classifications"] = sfp.pop("flow_classifiers")
507 sfp["service_functions"] = sfp.pop("port_pair_groups")
508
509 # placeholder for now; read TODO note below
510 def _validate_classification(self, type, definition):
511 # only legacy_flow_classifier Type is supported at this point
512 return True
513 # TODO(igordcard): this method should be an abstract method of an
514 # abstract Classification class to be implemented by the specific
515 # Types. Also, abstract vimconnector should call the validation
516 # method before the implemented VIM connectors are called.
517
518 def _format_exception(self, exception):
519 """Transform a keystone, nova, neutron exception into a vimconn exception discovering the cause"""
520 message_error = str(exception)
521 tip = ""
522
523 if isinstance(
524 exception,
525 (
526 neExceptions.NetworkNotFoundClient,
527 nvExceptions.NotFound,
528 ksExceptions.NotFound,
529 gl1Exceptions.HTTPNotFound,
530 ),
531 ):
532 raise vimconn.VimConnNotFoundException(
533 type(exception).__name__ + ": " + message_error
534 )
535 elif isinstance(
536 exception,
537 (
538 HTTPException,
539 gl1Exceptions.HTTPException,
540 gl1Exceptions.CommunicationError,
541 ConnectionError,
542 ksExceptions.ConnectionError,
543 neExceptions.ConnectionFailed,
544 ),
545 ):
546 if type(exception).__name__ == "SSLError":
547 tip = " (maybe option 'insecure' must be added to the VIM)"
548
549 raise vimconn.VimConnConnectionException(
550 "Invalid URL or credentials{}: {}".format(tip, message_error)
551 )
552 elif isinstance(
553 exception,
554 (
555 KeyError,
556 nvExceptions.BadRequest,
557 ksExceptions.BadRequest,
558 ),
559 ):
560 raise vimconn.VimConnException(
561 type(exception).__name__ + ": " + message_error
562 )
563 elif isinstance(
564 exception,
565 (
566 nvExceptions.ClientException,
567 ksExceptions.ClientException,
568 neExceptions.NeutronException,
569 ),
570 ):
571 raise vimconn.VimConnUnexpectedResponse(
572 type(exception).__name__ + ": " + message_error
573 )
574 elif isinstance(exception, nvExceptions.Conflict):
575 raise vimconn.VimConnConflictException(
576 type(exception).__name__ + ": " + message_error
577 )
578 elif isinstance(exception, vimconn.VimConnException):
579 raise exception
580 else: # ()
581 self.logger.error("General Exception " + message_error, exc_info=True)
582
583 raise vimconn.VimConnConnectionException(
584 type(exception).__name__ + ": " + message_error
585 )
586
587 def _get_ids_from_name(self):
588 """
589 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
590 :return: None
591 """
592 # get tenant_id if only tenant_name is supplied
593 self._reload_connection()
594
595 if not self.my_tenant_id:
596 raise vimconn.VimConnConnectionException(
597 "Error getting tenant information from name={} id={}".format(
598 self.tenant_name, self.tenant_id
599 )
600 )
601
602 if self.config.get("security_groups") and not self.security_groups_id:
603 # convert from name to id
604 neutron_sg_list = self.neutron.list_security_groups(
605 tenant_id=self.my_tenant_id
606 )["security_groups"]
607
608 self.security_groups_id = []
609 for sg in self.config.get("security_groups"):
610 for neutron_sg in neutron_sg_list:
611 if sg in (neutron_sg["id"], neutron_sg["name"]):
612 self.security_groups_id.append(neutron_sg["id"])
613 break
614 else:
615 self.security_groups_id = None
616
617 raise vimconn.VimConnConnectionException(
618 "Not found security group {} for this tenant".format(sg)
619 )
620
621 def check_vim_connectivity(self):
622 # just get network list to check connectivity and credentials
623 self.get_network_list(filter_dict={})
624
625 def get_tenant_list(self, filter_dict={}):
626 """Obtain tenants of VIM
627 filter_dict can contain the following keys:
628 name: filter by tenant name
629 id: filter by tenant uuid/id
630 <other VIM specific>
631 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
632 """
633 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
634
635 try:
636 self._reload_connection()
637
638 if self.api_version3:
639 project_class_list = self.keystone.projects.list(
640 name=filter_dict.get("name")
641 )
642 else:
643 project_class_list = self.keystone.tenants.findall(**filter_dict)
644
645 project_list = []
646
647 for project in project_class_list:
648 if filter_dict.get("id") and filter_dict["id"] != project.id:
649 continue
650
651 project_list.append(project.to_dict())
652
653 return project_list
654 except (
655 ksExceptions.ConnectionError,
656 ksExceptions.ClientException,
657 ConnectionError,
658 ) as e:
659 self._format_exception(e)
660
661 def new_tenant(self, tenant_name, tenant_description):
662 """Adds a new tenant to openstack VIM. Returns the tenant identifier"""
663 self.logger.debug("Adding a new tenant name: %s", tenant_name)
664
665 try:
666 self._reload_connection()
667
668 if self.api_version3:
669 project = self.keystone.projects.create(
670 tenant_name,
671 self.config.get("project_domain_id", "default"),
672 description=tenant_description,
673 is_domain=False,
674 )
675 else:
676 project = self.keystone.tenants.create(tenant_name, tenant_description)
677
678 return project.id
679 except (
680 ksExceptions.ConnectionError,
681 ksExceptions.ClientException,
682 ksExceptions.BadRequest,
683 ConnectionError,
684 ) as e:
685 self._format_exception(e)
686
687 def delete_tenant(self, tenant_id):
688 """Delete a tenant from openstack VIM. Returns the old tenant identifier"""
689 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
690
691 try:
692 self._reload_connection()
693
694 if self.api_version3:
695 self.keystone.projects.delete(tenant_id)
696 else:
697 self.keystone.tenants.delete(tenant_id)
698
699 return tenant_id
700 except (
701 ksExceptions.ConnectionError,
702 ksExceptions.ClientException,
703 ksExceptions.NotFound,
704 ConnectionError,
705 ) as e:
706 self._format_exception(e)
707
708 def new_network(
709 self,
710 net_name,
711 net_type,
712 ip_profile=None,
713 shared=False,
714 provider_network_profile=None,
715 ):
716 """Adds a tenant network to VIM
717 Params:
718 'net_name': name of the network
719 'net_type': one of:
720 'bridge': overlay isolated network
721 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
722 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
723 'ip_profile': is a dict containing the IP parameters of the network
724 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
725 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
726 'gateway_address': (Optional) ip_schema, that is X.X.X.X
727 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
728 'dhcp_enabled': True or False
729 'dhcp_start_address': ip_schema, first IP to grant
730 'dhcp_count': number of IPs to grant.
731 'shared': if this network can be seen/use by other tenants/organization
732 'provider_network_profile': (optional) contains {segmentation-id: vlan, network-type: vlan|vxlan,
733 physical-network: physnet-label}
734 Returns a tuple with the network identifier and created_items, or raises an exception on error
735 created_items can be None or a dictionary where this method can include key-values that will be passed to
736 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
737 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
738 as not present.
739 """
740 self.logger.debug(
741 "Adding a new network to VIM name '%s', type '%s'", net_name, net_type
742 )
743 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
744
745 try:
746 vlan = None
747
748 if provider_network_profile:
749 vlan = provider_network_profile.get("segmentation-id")
750
751 new_net = None
752 created_items = {}
753 self._reload_connection()
754 network_dict = {"name": net_name, "admin_state_up": True}
755
756 if net_type in ("data", "ptp"):
757 provider_physical_network = None
758
759 if provider_network_profile and provider_network_profile.get(
760 "physical-network"
761 ):
762 provider_physical_network = provider_network_profile.get(
763 "physical-network"
764 )
765
766 # provider-network must be one of the dataplane_physcial_netowrk if this is a list. If it is string
767 # or not declared, just ignore the checking
768 if (
769 isinstance(
770 self.config.get("dataplane_physical_net"), (tuple, list)
771 )
772 and provider_physical_network
773 not in self.config["dataplane_physical_net"]
774 ):
775 raise vimconn.VimConnConflictException(
776 "Invalid parameter 'provider-network:physical-network' "
777 "for network creation. '{}' is not one of the declared "
778 "list at VIM_config:dataplane_physical_net".format(
779 provider_physical_network
780 )
781 )
782
783 # use the default dataplane_physical_net
784 if not provider_physical_network:
785 provider_physical_network = self.config.get(
786 "dataplane_physical_net"
787 )
788
789 # if it is non empty list, use the first value. If it is a string use the value directly
790 if (
791 isinstance(provider_physical_network, (tuple, list))
792 and provider_physical_network
793 ):
794 provider_physical_network = provider_physical_network[0]
795
796 if not provider_physical_network:
797 raise vimconn.VimConnConflictException(
798 "missing information needed for underlay networks. Provide "
799 "'dataplane_physical_net' configuration at VIM or use the NS "
800 "instantiation parameter 'provider-network.physical-network'"
801 " for the VLD"
802 )
803
804 if not self.config.get("multisegment_support"):
805 network_dict[
806 "provider:physical_network"
807 ] = provider_physical_network
808
809 if (
810 provider_network_profile
811 and "network-type" in provider_network_profile
812 ):
813 network_dict[
814 "provider:network_type"
815 ] = provider_network_profile["network-type"]
816 else:
817 network_dict["provider:network_type"] = self.config.get(
818 "dataplane_network_type", "vlan"
819 )
820
821 if vlan:
822 network_dict["provider:segmentation_id"] = vlan
823 else:
824 # Multi-segment case
825 segment_list = []
826 segment1_dict = {
827 "provider:physical_network": "",
828 "provider:network_type": "vxlan",
829 }
830 segment_list.append(segment1_dict)
831 segment2_dict = {
832 "provider:physical_network": provider_physical_network,
833 "provider:network_type": "vlan",
834 }
835
836 if vlan:
837 segment2_dict["provider:segmentation_id"] = vlan
838 elif self.config.get("multisegment_vlan_range"):
839 vlanID = self._generate_multisegment_vlanID()
840 segment2_dict["provider:segmentation_id"] = vlanID
841
842 # else
843 # raise vimconn.VimConnConflictException(
844 # "You must provide "multisegment_vlan_range" at config dict before creating a multisegment
845 # network")
846 segment_list.append(segment2_dict)
847 network_dict["segments"] = segment_list
848
849 # VIO Specific Changes. It needs a concrete VLAN
850 if self.vim_type == "VIO" and vlan is None:
851 if self.config.get("dataplane_net_vlan_range") is None:
852 raise vimconn.VimConnConflictException(
853 "You must provide 'dataplane_net_vlan_range' in format "
854 "[start_ID - end_ID] at VIM_config for creating underlay "
855 "networks"
856 )
857
858 network_dict["provider:segmentation_id"] = self._generate_vlanID()
859
860 network_dict["shared"] = shared
861
862 if self.config.get("disable_network_port_security"):
863 network_dict["port_security_enabled"] = False
864
865 if self.config.get("neutron_availability_zone_hints"):
866 hints = self.config.get("neutron_availability_zone_hints")
867
868 if isinstance(hints, str):
869 hints = [hints]
870
871 network_dict["availability_zone_hints"] = hints
872
873 new_net = self.neutron.create_network({"network": network_dict})
874 # print new_net
875 # create subnetwork, even if there is no profile
876
877 if not ip_profile:
878 ip_profile = {}
879
880 if not ip_profile.get("subnet_address"):
881 # Fake subnet is required
882 subnet_rand = random.randint(0, 255)
883 ip_profile["subnet_address"] = "192.168.{}.0/24".format(subnet_rand)
884
885 if "ip_version" not in ip_profile:
886 ip_profile["ip_version"] = "IPv4"
887
888 subnet = {
889 "name": net_name + "-subnet",
890 "network_id": new_net["network"]["id"],
891 "ip_version": 4 if ip_profile["ip_version"] == "IPv4" else 6,
892 "cidr": ip_profile["subnet_address"],
893 }
894
895 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
896 if ip_profile.get("gateway_address"):
897 subnet["gateway_ip"] = ip_profile["gateway_address"]
898 else:
899 subnet["gateway_ip"] = None
900
901 if ip_profile.get("dns_address"):
902 subnet["dns_nameservers"] = ip_profile["dns_address"].split(";")
903
904 if "dhcp_enabled" in ip_profile:
905 subnet["enable_dhcp"] = (
906 False
907 if ip_profile["dhcp_enabled"] == "false"
908 or ip_profile["dhcp_enabled"] is False
909 else True
910 )
911
912 if ip_profile.get("dhcp_start_address"):
913 subnet["allocation_pools"] = []
914 subnet["allocation_pools"].append(dict())
915 subnet["allocation_pools"][0]["start"] = ip_profile[
916 "dhcp_start_address"
917 ]
918
919 if ip_profile.get("dhcp_count"):
920 # parts = ip_profile["dhcp_start_address"].split(".")
921 # ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
922 ip_int = int(netaddr.IPAddress(ip_profile["dhcp_start_address"]))
923 ip_int += ip_profile["dhcp_count"] - 1
924 ip_str = str(netaddr.IPAddress(ip_int))
925 subnet["allocation_pools"][0]["end"] = ip_str
926
927 # self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
928 self.neutron.create_subnet({"subnet": subnet})
929
930 if net_type == "data" and self.config.get("multisegment_support"):
931 if self.config.get("l2gw_support"):
932 l2gw_list = self.neutron.list_l2_gateways().get("l2_gateways", ())
933 for l2gw in l2gw_list:
934 l2gw_conn = {
935 "l2_gateway_id": l2gw["id"],
936 "network_id": new_net["network"]["id"],
937 "segmentation_id": str(vlanID),
938 }
939 new_l2gw_conn = self.neutron.create_l2_gateway_connection(
940 {"l2_gateway_connection": l2gw_conn}
941 )
942 created_items[
943 "l2gwconn:"
944 + str(new_l2gw_conn["l2_gateway_connection"]["id"])
945 ] = True
946
947 return new_net["network"]["id"], created_items
948 except Exception as e:
949 # delete l2gw connections (if any) before deleting the network
950 for k, v in created_items.items():
951 if not v: # skip already deleted
952 continue
953
954 try:
955 k_item, _, k_id = k.partition(":")
956
957 if k_item == "l2gwconn":
958 self.neutron.delete_l2_gateway_connection(k_id)
959 except Exception as e2:
960 self.logger.error(
961 "Error deleting l2 gateway connection: {}: {}".format(
962 type(e2).__name__, e2
963 )
964 )
965
966 if new_net:
967 self.neutron.delete_network(new_net["network"]["id"])
968
969 self._format_exception(e)
970
971 def get_network_list(self, filter_dict={}):
972 """Obtain tenant networks of VIM
973 Filter_dict can be:
974 name: network name
975 id: network uuid
976 shared: boolean
977 tenant_id: tenant
978 admin_state_up: boolean
979 status: 'ACTIVE'
980 Returns the network list of dictionaries
981 """
982 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
983
984 try:
985 self._reload_connection()
986 filter_dict_os = filter_dict.copy()
987
988 if self.api_version3 and "tenant_id" in filter_dict_os:
989 # TODO check
990 filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id")
991
992 net_dict = self.neutron.list_networks(**filter_dict_os)
993 net_list = net_dict["networks"]
994 self.__net_os2mano(net_list)
995
996 return net_list
997 except (
998 neExceptions.ConnectionFailed,
999 ksExceptions.ClientException,
1000 neExceptions.NeutronException,
1001 ConnectionError,
1002 ) as e:
1003 self._format_exception(e)
1004
1005 def get_network(self, net_id):
1006 """Obtain details of network from VIM
1007 Returns the network information from a network id"""
1008 self.logger.debug(" Getting tenant network %s from VIM", net_id)
1009 filter_dict = {"id": net_id}
1010 net_list = self.get_network_list(filter_dict)
1011
1012 if len(net_list) == 0:
1013 raise vimconn.VimConnNotFoundException(
1014 "Network '{}' not found".format(net_id)
1015 )
1016 elif len(net_list) > 1:
1017 raise vimconn.VimConnConflictException(
1018 "Found more than one network with this criteria"
1019 )
1020
1021 net = net_list[0]
1022 subnets = []
1023 for subnet_id in net.get("subnets", ()):
1024 try:
1025 subnet = self.neutron.show_subnet(subnet_id)
1026 except Exception as e:
1027 self.logger.error(
1028 "osconnector.get_network(): Error getting subnet %s %s"
1029 % (net_id, str(e))
1030 )
1031 subnet = {"id": subnet_id, "fault": str(e)}
1032
1033 subnets.append(subnet)
1034
1035 net["subnets"] = subnets
1036 net["encapsulation"] = net.get("provider:network_type")
1037 net["encapsulation_type"] = net.get("provider:network_type")
1038 net["segmentation_id"] = net.get("provider:segmentation_id")
1039 net["encapsulation_id"] = net.get("provider:segmentation_id")
1040
1041 return net
1042
1043 def delete_network(self, net_id, created_items=None):
1044 """
1045 Removes a tenant network from VIM and its associated elements
1046 :param net_id: VIM identifier of the network, provided by method new_network
1047 :param created_items: dictionary with extra items to be deleted. provided by method new_network
1048 Returns the network identifier or raises an exception upon error or when network is not found
1049 """
1050 self.logger.debug("Deleting network '%s' from VIM", net_id)
1051
1052 if created_items is None:
1053 created_items = {}
1054
1055 try:
1056 self._reload_connection()
1057 # delete l2gw connections (if any) before deleting the network
1058 for k, v in created_items.items():
1059 if not v: # skip already deleted
1060 continue
1061
1062 try:
1063 k_item, _, k_id = k.partition(":")
1064 if k_item == "l2gwconn":
1065 self.neutron.delete_l2_gateway_connection(k_id)
1066 except Exception as e:
1067 self.logger.error(
1068 "Error deleting l2 gateway connection: {}: {}".format(
1069 type(e).__name__, e
1070 )
1071 )
1072
1073 # delete VM ports attached to this networks before the network
1074 ports = self.neutron.list_ports(network_id=net_id)
1075 for p in ports["ports"]:
1076 try:
1077 self.neutron.delete_port(p["id"])
1078 except Exception as e:
1079 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
1080
1081 self.neutron.delete_network(net_id)
1082
1083 return net_id
1084 except (
1085 neExceptions.ConnectionFailed,
1086 neExceptions.NetworkNotFoundClient,
1087 neExceptions.NeutronException,
1088 ksExceptions.ClientException,
1089 neExceptions.NeutronException,
1090 ConnectionError,
1091 ) as e:
1092 self._format_exception(e)
1093
1094 def refresh_nets_status(self, net_list):
1095 """Get the status of the networks
1096 Params: the list of network identifiers
1097 Returns a dictionary with:
1098 net_id: #VIM id of this network
1099 status: #Mandatory. Text with one of:
1100 # DELETED (not found at vim)
1101 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1102 # OTHER (Vim reported other status not understood)
1103 # ERROR (VIM indicates an ERROR status)
1104 # ACTIVE, INACTIVE, DOWN (admin down),
1105 # BUILD (on building process)
1106 #
1107 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1108 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1109 """
1110 net_dict = {}
1111
1112 for net_id in net_list:
1113 net = {}
1114
1115 try:
1116 net_vim = self.get_network(net_id)
1117
1118 if net_vim["status"] in netStatus2manoFormat:
1119 net["status"] = netStatus2manoFormat[net_vim["status"]]
1120 else:
1121 net["status"] = "OTHER"
1122 net["error_msg"] = "VIM status reported " + net_vim["status"]
1123
1124 if net["status"] == "ACTIVE" and not net_vim["admin_state_up"]:
1125 net["status"] = "DOWN"
1126
1127 net["vim_info"] = self.serialize(net_vim)
1128
1129 if net_vim.get("fault"): # TODO
1130 net["error_msg"] = str(net_vim["fault"])
1131 except vimconn.VimConnNotFoundException as e:
1132 self.logger.error("Exception getting net status: %s", str(e))
1133 net["status"] = "DELETED"
1134 net["error_msg"] = str(e)
1135 except vimconn.VimConnException as e:
1136 self.logger.error("Exception getting net status: %s", str(e))
1137 net["status"] = "VIM_ERROR"
1138 net["error_msg"] = str(e)
1139 net_dict[net_id] = net
1140 return net_dict
1141
1142 def get_flavor(self, flavor_id):
1143 """Obtain flavor details from the VIM. Returns the flavor dict details"""
1144 self.logger.debug("Getting flavor '%s'", flavor_id)
1145
1146 try:
1147 self._reload_connection()
1148 flavor = self.nova.flavors.find(id=flavor_id)
1149 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1150
1151 return flavor.to_dict()
1152 except (
1153 nvExceptions.NotFound,
1154 nvExceptions.ClientException,
1155 ksExceptions.ClientException,
1156 ConnectionError,
1157 ) as e:
1158 self._format_exception(e)
1159
1160 def get_flavor_id_from_data(self, flavor_dict):
1161 """Obtain flavor id that match the flavor description
1162 Returns the flavor_id or raises a vimconnNotFoundException
1163 flavor_dict: contains the required ram, vcpus, disk
1164 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
1165 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
1166 vimconnNotFoundException is raised
1167 """
1168 exact_match = False if self.config.get("use_existing_flavors") else True
1169
1170 try:
1171 self._reload_connection()
1172 flavor_candidate_id = None
1173 flavor_candidate_data = (10000, 10000, 10000)
1174 flavor_target = (
1175 flavor_dict["ram"],
1176 flavor_dict["vcpus"],
1177 flavor_dict["disk"],
1178 flavor_dict.get("ephemeral", 0),
1179 flavor_dict.get("swap", 0),
1180 )
1181 # numa=None
1182 extended = flavor_dict.get("extended", {})
1183 if extended:
1184 # TODO
1185 raise vimconn.VimConnNotFoundException(
1186 "Flavor with EPA still not implemented"
1187 )
1188 # if len(numas) > 1:
1189 # raise vimconn.VimConnNotFoundException("Cannot find any flavor with more than one numa")
1190 # numa=numas[0]
1191 # numas = extended.get("numas")
1192 for flavor in self.nova.flavors.list():
1193 epa = flavor.get_keys()
1194
1195 if epa:
1196 continue
1197 # TODO
1198
1199 flavor_data = (
1200 flavor.ram,
1201 flavor.vcpus,
1202 flavor.disk,
1203 flavor.ephemeral,
1204 flavor.swap if isinstance(flavor.swap, int) else 0,
1205 )
1206 if flavor_data == flavor_target:
1207 return flavor.id
1208 elif (
1209 not exact_match
1210 and flavor_target < flavor_data < flavor_candidate_data
1211 ):
1212 flavor_candidate_id = flavor.id
1213 flavor_candidate_data = flavor_data
1214
1215 if not exact_match and flavor_candidate_id:
1216 return flavor_candidate_id
1217
1218 raise vimconn.VimConnNotFoundException(
1219 "Cannot find any flavor matching '{}'".format(flavor_dict)
1220 )
1221 except (
1222 nvExceptions.NotFound,
1223 nvExceptions.ClientException,
1224 ksExceptions.ClientException,
1225 ConnectionError,
1226 ) as e:
1227 self._format_exception(e)
1228
1229 def process_resource_quota(self, quota, prefix, extra_specs):
1230 """
1231 :param prefix:
1232 :param extra_specs:
1233 :return:
1234 """
1235 if "limit" in quota:
1236 extra_specs["quota:" + prefix + "_limit"] = quota["limit"]
1237
1238 if "reserve" in quota:
1239 extra_specs["quota:" + prefix + "_reservation"] = quota["reserve"]
1240
1241 if "shares" in quota:
1242 extra_specs["quota:" + prefix + "_shares_level"] = "custom"
1243 extra_specs["quota:" + prefix + "_shares_share"] = quota["shares"]
1244
1245 def new_flavor(self, flavor_data, change_name_if_used=True):
1246 """Adds a tenant flavor to openstack VIM
1247 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name
1248 repetition
1249 Returns the flavor identifier
1250 """
1251 self.logger.debug("Adding flavor '%s'", str(flavor_data))
1252 retry = 0
1253 max_retries = 3
1254 name_suffix = 0
1255
1256 try:
1257 name = flavor_data["name"]
1258 while retry < max_retries:
1259 retry += 1
1260 try:
1261 self._reload_connection()
1262
1263 if change_name_if_used:
1264 # get used names
1265 fl_names = []
1266 fl = self.nova.flavors.list()
1267
1268 for f in fl:
1269 fl_names.append(f.name)
1270
1271 while name in fl_names:
1272 name_suffix += 1
1273 name = flavor_data["name"] + "-" + str(name_suffix)
1274
1275 ram = flavor_data.get("ram", 64)
1276 vcpus = flavor_data.get("vcpus", 1)
1277 extra_specs = {}
1278
1279 extended = flavor_data.get("extended")
1280 if extended:
1281 numas = extended.get("numas")
1282
1283 if numas:
1284 numa_nodes = len(numas)
1285
1286 if numa_nodes > 1:
1287 return -1, "Can not add flavor with more than one numa"
1288
1289 extra_specs["hw:numa_nodes"] = str(numa_nodes)
1290 extra_specs["hw:mem_page_size"] = "large"
1291 extra_specs["hw:cpu_policy"] = "dedicated"
1292 extra_specs["hw:numa_mempolicy"] = "strict"
1293
1294 if self.vim_type == "VIO":
1295 extra_specs[
1296 "vmware:extra_config"
1297 ] = '{"numa.nodeAffinity":"0"}'
1298 extra_specs["vmware:latency_sensitivity_level"] = "high"
1299
1300 for numa in numas:
1301 # overwrite ram and vcpus
1302 # check if key "memory" is present in numa else use ram value at flavor
1303 if "memory" in numa:
1304 ram = numa["memory"] * 1024
1305 # See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/
1306 # implemented/virt-driver-cpu-thread-pinning.html
1307 extra_specs["hw:cpu_sockets"] = 1
1308
1309 if "paired-threads" in numa:
1310 vcpus = numa["paired-threads"] * 2
1311 # cpu_thread_policy "require" implies that the compute node must have an
1312 # STM architecture
1313 extra_specs["hw:cpu_thread_policy"] = "require"
1314 extra_specs["hw:cpu_policy"] = "dedicated"
1315 elif "cores" in numa:
1316 vcpus = numa["cores"]
1317 # cpu_thread_policy "prefer" implies that the host must not have an SMT
1318 # architecture, or a non-SMT architecture will be emulated
1319 extra_specs["hw:cpu_thread_policy"] = "isolate"
1320 extra_specs["hw:cpu_policy"] = "dedicated"
1321 elif "threads" in numa:
1322 vcpus = numa["threads"]
1323 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT
1324 # architecture
1325 extra_specs["hw:cpu_thread_policy"] = "prefer"
1326 extra_specs["hw:cpu_policy"] = "dedicated"
1327 # for interface in numa.get("interfaces",() ):
1328 # if interface["dedicated"]=="yes":
1329 # raise vimconn.VimConnException("Passthrough interfaces are not supported
1330 # for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
1331 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"'
1332 # when a way to connect it is available
1333 elif extended.get("cpu-quota"):
1334 self.process_resource_quota(
1335 extended.get("cpu-quota"), "cpu", extra_specs
1336 )
1337
1338 if extended.get("mem-quota"):
1339 self.process_resource_quota(
1340 extended.get("mem-quota"), "memory", extra_specs
1341 )
1342
1343 if extended.get("vif-quota"):
1344 self.process_resource_quota(
1345 extended.get("vif-quota"), "vif", extra_specs
1346 )
1347
1348 if extended.get("disk-io-quota"):
1349 self.process_resource_quota(
1350 extended.get("disk-io-quota"), "disk_io", extra_specs
1351 )
1352
1353 # Set the mempage size as specified in the descriptor
1354 if extended.get("mempage-size"):
1355 if extended.get("mempage-size") == "LARGE":
1356 extra_specs["hw:mem_page_size"] = "large"
1357 elif extended.get("mempage-size") == "SMALL":
1358 extra_specs["hw:mem_page_size"] = "small"
1359 elif extended.get("mempage-size") == "SIZE_2MB":
1360 extra_specs["hw:mem_page_size"] = "2MB"
1361 elif extended.get("mempage-size") == "SIZE_1GB":
1362 extra_specs["hw:mem_page_size"] = "1GB"
1363 elif extended.get("mempage-size") == "PREFER_LARGE":
1364 extra_specs["hw:mem_page_size"] = "any"
1365 else:
1366 # The validations in NBI should make reaching here not possible.
1367 # If this message is shown, check validations
1368 self.logger.debug(
1369 "Invalid mempage-size %s. Will be ignored",
1370 extended.get("mempage-size"),
1371 )
1372
1373 # create flavor
1374 new_flavor = self.nova.flavors.create(
1375 name=name,
1376 ram=ram,
1377 vcpus=vcpus,
1378 disk=flavor_data.get("disk", 0),
1379 ephemeral=flavor_data.get("ephemeral", 0),
1380 swap=flavor_data.get("swap", 0),
1381 is_public=flavor_data.get("is_public", True),
1382 )
1383 # add metadata
1384 if extra_specs:
1385 new_flavor.set_keys(extra_specs)
1386
1387 return new_flavor.id
1388 except nvExceptions.Conflict as e:
1389 if change_name_if_used and retry < max_retries:
1390 continue
1391
1392 self._format_exception(e)
1393 # except nvExceptions.BadRequest as e:
1394 except (
1395 ksExceptions.ClientException,
1396 nvExceptions.ClientException,
1397 ConnectionError,
1398 KeyError,
1399 ) as e:
1400 self._format_exception(e)
1401
1402 def delete_flavor(self, flavor_id):
1403 """Deletes a tenant flavor from openstack VIM. Returns the old flavor_id"""
1404 try:
1405 self._reload_connection()
1406 self.nova.flavors.delete(flavor_id)
1407
1408 return flavor_id
1409 # except nvExceptions.BadRequest as e:
1410 except (
1411 nvExceptions.NotFound,
1412 ksExceptions.ClientException,
1413 nvExceptions.ClientException,
1414 ConnectionError,
1415 ) as e:
1416 self._format_exception(e)
1417
1418 def new_image(self, image_dict):
1419 """
1420 Adds a tenant image to VIM. imge_dict is a dictionary with:
1421 name: name
1422 disk_format: qcow2, vhd, vmdk, raw (by default), ...
1423 location: path or URI
1424 public: "yes" or "no"
1425 metadata: metadata of the image
1426 Returns the image_id
1427 """
1428 retry = 0
1429 max_retries = 3
1430
1431 while retry < max_retries:
1432 retry += 1
1433 try:
1434 self._reload_connection()
1435
1436 # determine format http://docs.openstack.org/developer/glance/formats.html
1437 if "disk_format" in image_dict:
1438 disk_format = image_dict["disk_format"]
1439 else: # autodiscover based on extension
1440 if image_dict["location"].endswith(".qcow2"):
1441 disk_format = "qcow2"
1442 elif image_dict["location"].endswith(".vhd"):
1443 disk_format = "vhd"
1444 elif image_dict["location"].endswith(".vmdk"):
1445 disk_format = "vmdk"
1446 elif image_dict["location"].endswith(".vdi"):
1447 disk_format = "vdi"
1448 elif image_dict["location"].endswith(".iso"):
1449 disk_format = "iso"
1450 elif image_dict["location"].endswith(".aki"):
1451 disk_format = "aki"
1452 elif image_dict["location"].endswith(".ari"):
1453 disk_format = "ari"
1454 elif image_dict["location"].endswith(".ami"):
1455 disk_format = "ami"
1456 else:
1457 disk_format = "raw"
1458
1459 self.logger.debug(
1460 "new_image: '%s' loading from '%s'",
1461 image_dict["name"],
1462 image_dict["location"],
1463 )
1464 if self.vim_type == "VIO":
1465 container_format = "bare"
1466 if "container_format" in image_dict:
1467 container_format = image_dict["container_format"]
1468
1469 new_image = self.glance.images.create(
1470 name=image_dict["name"],
1471 container_format=container_format,
1472 disk_format=disk_format,
1473 )
1474 else:
1475 new_image = self.glance.images.create(name=image_dict["name"])
1476
1477 if image_dict["location"].startswith("http"):
1478 # TODO there is not a method to direct download. It must be downloaded locally with requests
1479 raise vimconn.VimConnNotImplemented("Cannot create image from URL")
1480 else: # local path
1481 with open(image_dict["location"]) as fimage:
1482 self.glance.images.upload(new_image.id, fimage)
1483 # new_image = self.glancev1.images.create(name=image_dict["name"], is_public=
1484 # image_dict.get("public","yes")=="yes",
1485 # container_format="bare", data=fimage, disk_format=disk_format)
1486
1487 metadata_to_load = image_dict.get("metadata")
1488
1489 # TODO location is a reserved word for current openstack versions. fixed for VIO please check
1490 # for openstack
1491 if self.vim_type == "VIO":
1492 metadata_to_load["upload_location"] = image_dict["location"]
1493 else:
1494 metadata_to_load["location"] = image_dict["location"]
1495
1496 self.glance.images.update(new_image.id, **metadata_to_load)
1497
1498 return new_image.id
1499 except (
1500 nvExceptions.Conflict,
1501 ksExceptions.ClientException,
1502 nvExceptions.ClientException,
1503 ) as e:
1504 self._format_exception(e)
1505 except (
1506 HTTPException,
1507 gl1Exceptions.HTTPException,
1508 gl1Exceptions.CommunicationError,
1509 ConnectionError,
1510 ) as e:
1511 if retry == max_retries:
1512 continue
1513
1514 self._format_exception(e)
1515 except IOError as e: # can not open the file
1516 raise vimconn.VimConnConnectionException(
1517 "{}: {} for {}".format(type(e).__name__, e, image_dict["location"]),
1518 http_code=vimconn.HTTP_Bad_Request,
1519 )
1520
1521 def delete_image(self, image_id):
1522 """Deletes a tenant image from openstack VIM. Returns the old id"""
1523 try:
1524 self._reload_connection()
1525 self.glance.images.delete(image_id)
1526
1527 return image_id
1528 except (
1529 nvExceptions.NotFound,
1530 ksExceptions.ClientException,
1531 nvExceptions.ClientException,
1532 gl1Exceptions.CommunicationError,
1533 gl1Exceptions.HTTPNotFound,
1534 ConnectionError,
1535 ) as e: # TODO remove
1536 self._format_exception(e)
1537
1538 def get_image_id_from_path(self, path):
1539 """Get the image id from image path in the VIM database. Returns the image_id"""
1540 try:
1541 self._reload_connection()
1542 images = self.glance.images.list()
1543
1544 for image in images:
1545 if image.metadata.get("location") == path:
1546 return image.id
1547
1548 raise vimconn.VimConnNotFoundException(
1549 "image with location '{}' not found".format(path)
1550 )
1551 except (
1552 ksExceptions.ClientException,
1553 nvExceptions.ClientException,
1554 gl1Exceptions.CommunicationError,
1555 ConnectionError,
1556 ) as e:
1557 self._format_exception(e)
1558
1559 def get_image_list(self, filter_dict={}):
1560 """Obtain tenant images from VIM
1561 Filter_dict can be:
1562 id: image id
1563 name: image name
1564 checksum: image checksum
1565 Returns the image list of dictionaries:
1566 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1567 List can be empty
1568 """
1569 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
1570
1571 try:
1572 self._reload_connection()
1573 # filter_dict_os = filter_dict.copy()
1574 # First we filter by the available filter fields: name, id. The others are removed.
1575 image_list = self.glance.images.list()
1576 filtered_list = []
1577
1578 for image in image_list:
1579 try:
1580 if filter_dict.get("name") and image["name"] != filter_dict["name"]:
1581 continue
1582
1583 if filter_dict.get("id") and image["id"] != filter_dict["id"]:
1584 continue
1585
1586 if (
1587 filter_dict.get("checksum")
1588 and image["checksum"] != filter_dict["checksum"]
1589 ):
1590 continue
1591
1592 filtered_list.append(image.copy())
1593 except gl1Exceptions.HTTPNotFound:
1594 pass
1595
1596 return filtered_list
1597 except (
1598 ksExceptions.ClientException,
1599 nvExceptions.ClientException,
1600 gl1Exceptions.CommunicationError,
1601 ConnectionError,
1602 ) as e:
1603 self._format_exception(e)
1604
1605 def __wait_for_vm(self, vm_id, status):
1606 """wait until vm is in the desired status and return True.
1607 If the VM gets in ERROR status, return false.
1608 If the timeout is reached generate an exception"""
1609 elapsed_time = 0
1610 while elapsed_time < server_timeout:
1611 vm_status = self.nova.servers.get(vm_id).status
1612
1613 if vm_status == status:
1614 return True
1615
1616 if vm_status == "ERROR":
1617 return False
1618
1619 time.sleep(5)
1620 elapsed_time += 5
1621
1622 # if we exceeded the timeout rollback
1623 if elapsed_time >= server_timeout:
1624 raise vimconn.VimConnException(
1625 "Timeout waiting for instance " + vm_id + " to get " + status,
1626 http_code=vimconn.HTTP_Request_Timeout,
1627 )
1628
1629 def _get_openstack_availablity_zones(self):
1630 """
1631 Get from openstack availability zones available
1632 :return:
1633 """
1634 try:
1635 openstack_availability_zone = self.nova.availability_zones.list()
1636 openstack_availability_zone = [
1637 str(zone.zoneName)
1638 for zone in openstack_availability_zone
1639 if zone.zoneName != "internal"
1640 ]
1641
1642 return openstack_availability_zone
1643 except Exception:
1644 return None
1645
1646 def _set_availablity_zones(self):
1647 """
1648 Set vim availablity zone
1649 :return:
1650 """
1651 if "availability_zone" in self.config:
1652 vim_availability_zones = self.config.get("availability_zone")
1653
1654 if isinstance(vim_availability_zones, str):
1655 self.availability_zone = [vim_availability_zones]
1656 elif isinstance(vim_availability_zones, list):
1657 self.availability_zone = vim_availability_zones
1658 else:
1659 self.availability_zone = self._get_openstack_availablity_zones()
1660
1661 def _get_vm_availability_zone(
1662 self, availability_zone_index, availability_zone_list
1663 ):
1664 """
1665 Return thge availability zone to be used by the created VM.
1666 :return: The VIM availability zone to be used or None
1667 """
1668 if availability_zone_index is None:
1669 if not self.config.get("availability_zone"):
1670 return None
1671 elif isinstance(self.config.get("availability_zone"), str):
1672 return self.config["availability_zone"]
1673 else:
1674 # TODO consider using a different parameter at config for default AV and AV list match
1675 return self.config["availability_zone"][0]
1676
1677 vim_availability_zones = self.availability_zone
1678 # check if VIM offer enough availability zones describe in the VNFD
1679 if vim_availability_zones and len(availability_zone_list) <= len(
1680 vim_availability_zones
1681 ):
1682 # check if all the names of NFV AV match VIM AV names
1683 match_by_index = False
1684 for av in availability_zone_list:
1685 if av not in vim_availability_zones:
1686 match_by_index = True
1687 break
1688
1689 if match_by_index:
1690 return vim_availability_zones[availability_zone_index]
1691 else:
1692 return availability_zone_list[availability_zone_index]
1693 else:
1694 raise vimconn.VimConnConflictException(
1695 "No enough availability zones at VIM for this deployment"
1696 )
1697
1698 def new_vminstance(
1699 self,
1700 name,
1701 description,
1702 start,
1703 image_id,
1704 flavor_id,
1705 affinity_group_list,
1706 net_list,
1707 cloud_config=None,
1708 disk_list=None,
1709 availability_zone_index=None,
1710 availability_zone_list=None,
1711 ):
1712 """Adds a VM instance to VIM
1713 Params:
1714 start: indicates if VM must start or boot in pause mode. Ignored
1715 image_id,flavor_id: image and flavor uuid
1716 affinity_group_list: list of affinity groups, each one is a dictionary.
1717 Ignore if empty.
1718 net_list: list of interfaces, each one is a dictionary with:
1719 name:
1720 net_id: network uuid to connect
1721 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1722 model: interface model, ignored #TODO
1723 mac_address: used for SR-IOV ifaces #TODO for other types
1724 use: 'data', 'bridge', 'mgmt'
1725 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
1726 vim_id: filled/added by this function
1727 floating_ip: True/False (or it can be None)
1728 port_security: True/False
1729 'cloud_config': (optional) dictionary with:
1730 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1731 'users': (optional) list of users to be inserted, each item is a dict with:
1732 'name': (mandatory) user name,
1733 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1734 'user-data': (optional) string is a text script to be passed directly to cloud-init
1735 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1736 'dest': (mandatory) string with the destination absolute path
1737 'encoding': (optional, by default text). Can be one of:
1738 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1739 'content' (mandatory): string with the content of the file
1740 'permissions': (optional) string with file permissions, typically octal notation '0644'
1741 'owner': (optional) file owner, string with the format 'owner:group'
1742 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1743 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1744 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1745 'size': (mandatory) string with the size of the disk in GB
1746 'vim_id' (optional) should use this existing volume id
1747 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1748 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1749 availability_zone_index is None
1750 #TODO ip, security groups
1751 Returns a tuple with the instance identifier and created_items or raises an exception on error
1752 created_items can be None or a dictionary where this method can include key-values that will be passed to
1753 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1754 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1755 as not present.
1756 """
1757 self.logger.debug(
1758 "new_vminstance input: image='%s' flavor='%s' nics='%s'",
1759 image_id,
1760 flavor_id,
1761 str(net_list),
1762 )
1763
1764 try:
1765 server = None
1766 created_items = {}
1767 # metadata = {}
1768 net_list_vim = []
1769 external_network = []
1770 # ^list of external networks to be connected to instance, later on used to create floating_ip
1771 no_secured_ports = [] # List of port-is with port-security disabled
1772 self._reload_connection()
1773 # metadata_vpci = {} # For a specific neutron plugin
1774 block_device_mapping = None
1775
1776 for net in net_list:
1777 if not net.get("net_id"): # skip non connected iface
1778 continue
1779
1780 port_dict = {
1781 "network_id": net["net_id"],
1782 "name": net.get("name"),
1783 "admin_state_up": True,
1784 }
1785
1786 if (
1787 self.config.get("security_groups")
1788 and net.get("port_security") is not False
1789 and not self.config.get("no_port_security_extension")
1790 ):
1791 if not self.security_groups_id:
1792 self._get_ids_from_name()
1793
1794 port_dict["security_groups"] = self.security_groups_id
1795
1796 if net["type"] == "virtual":
1797 pass
1798 # if "vpci" in net:
1799 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1800 elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF
1801 # if "vpci" in net:
1802 # if "VF" not in metadata_vpci:
1803 # metadata_vpci["VF"]=[]
1804 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1805 port_dict["binding:vnic_type"] = "direct"
1806
1807 # VIO specific Changes
1808 if self.vim_type == "VIO":
1809 # Need to create port with port_security_enabled = False and no-security-groups
1810 port_dict["port_security_enabled"] = False
1811 port_dict["provider_security_groups"] = []
1812 port_dict["security_groups"] = []
1813 else: # For PT PCI-PASSTHROUGH
1814 # if "vpci" in net:
1815 # if "PF" not in metadata_vpci:
1816 # metadata_vpci["PF"]=[]
1817 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1818 port_dict["binding:vnic_type"] = "direct-physical"
1819
1820 if not port_dict["name"]:
1821 port_dict["name"] = name
1822
1823 if net.get("mac_address"):
1824 port_dict["mac_address"] = net["mac_address"]
1825
1826 if net.get("ip_address"):
1827 port_dict["fixed_ips"] = [{"ip_address": net["ip_address"]}]
1828 # TODO add "subnet_id": <subnet_id>
1829
1830 new_port = self.neutron.create_port({"port": port_dict})
1831 created_items["port:" + str(new_port["port"]["id"])] = True
1832 net["mac_adress"] = new_port["port"]["mac_address"]
1833 net["vim_id"] = new_port["port"]["id"]
1834 # if try to use a network without subnetwork, it will return a emtpy list
1835 fixed_ips = new_port["port"].get("fixed_ips")
1836
1837 if fixed_ips:
1838 net["ip"] = fixed_ips[0].get("ip_address")
1839 else:
1840 net["ip"] = None
1841
1842 port = {"port-id": new_port["port"]["id"]}
1843 if float(self.nova.api_version.get_string()) >= 2.32:
1844 port["tag"] = new_port["port"]["name"]
1845
1846 net_list_vim.append(port)
1847
1848 if net.get("floating_ip", False):
1849 net["exit_on_floating_ip_error"] = True
1850 external_network.append(net)
1851 elif net["use"] == "mgmt" and self.config.get("use_floating_ip"):
1852 net["exit_on_floating_ip_error"] = False
1853 external_network.append(net)
1854 net["floating_ip"] = self.config.get("use_floating_ip")
1855
1856 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic
1857 # is dropped.
1858 # As a workaround we wait until the VM is active and then disable the port-security
1859 if net.get("port_security") is False and not self.config.get(
1860 "no_port_security_extension"
1861 ):
1862 no_secured_ports.append(
1863 (
1864 new_port["port"]["id"],
1865 net.get("port_security_disable_strategy"),
1866 )
1867 )
1868
1869 # if metadata_vpci:
1870 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1871 # if len(metadata["pci_assignement"]) >255:
1872 # #limit the metadata size
1873 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1874 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1875 # metadata = {}
1876
1877 self.logger.debug(
1878 "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1879 name,
1880 image_id,
1881 flavor_id,
1882 str(net_list_vim),
1883 description,
1884 )
1885
1886 # cloud config
1887 config_drive, userdata = self._create_user_data(cloud_config)
1888
1889 # get availability Zone
1890 vm_av_zone = self._get_vm_availability_zone(
1891 availability_zone_index, availability_zone_list
1892 )
1893
1894 # Create additional volumes in case these are present in disk_list
1895 existing_vim_volumes = []
1896 base_disk_index = ord("b")
1897 boot_volume_id = None
1898 if disk_list:
1899 block_device_mapping = {}
1900 for disk in disk_list:
1901 if disk.get("vim_id"):
1902 block_device_mapping["_vd" + chr(base_disk_index)] = disk[
1903 "vim_id"
1904 ]
1905 existing_vim_volumes.append({"id": disk["vim_id"]})
1906 else:
1907 if "image_id" in disk:
1908 base_disk_index = ord("a")
1909 volume = self.cinder.volumes.create(
1910 size=disk["size"],
1911 name=name + "_vd" + chr(base_disk_index),
1912 imageRef=disk["image_id"],
1913 # Make sure volume is in the same AZ as the VM to be attached to
1914 availability_zone=vm_av_zone,
1915 )
1916 boot_volume_id = volume.id
1917 else:
1918 volume = self.cinder.volumes.create(
1919 size=disk["size"],
1920 name=name + "_vd" + chr(base_disk_index),
1921 # Make sure volume is in the same AZ as the VM to be attached to
1922 availability_zone=vm_av_zone,
1923 )
1924
1925 created_items["volume:" + str(volume.id)] = True
1926 block_device_mapping["_vd" + chr(base_disk_index)] = volume.id
1927
1928 base_disk_index += 1
1929
1930 # Wait until created volumes are with status available
1931 elapsed_time = 0
1932 while elapsed_time < volume_timeout:
1933 for created_item in created_items:
1934 v, _, volume_id = created_item.partition(":")
1935 if v == "volume":
1936 if self.cinder.volumes.get(volume_id).status != "available":
1937 break
1938 else: # all ready: break from while
1939 break
1940
1941 time.sleep(5)
1942 elapsed_time += 5
1943
1944 # Wait until existing volumes in vim are with status available
1945 while elapsed_time < volume_timeout:
1946 for volume in existing_vim_volumes:
1947 if self.cinder.volumes.get(volume["id"]).status != "available":
1948 break
1949 else: # all ready: break from while
1950 break
1951
1952 time.sleep(5)
1953 elapsed_time += 5
1954
1955 # If we exceeded the timeout rollback
1956 if elapsed_time >= volume_timeout:
1957 raise vimconn.VimConnException(
1958 "Timeout creating volumes for instance " + name,
1959 http_code=vimconn.HTTP_Request_Timeout,
1960 )
1961 if boot_volume_id:
1962 self.cinder.volumes.set_bootable(boot_volume_id, True)
1963
1964 # Manage affinity groups/server groups
1965 server_group_id = None
1966 scheduller_hints = {}
1967
1968 if affinity_group_list:
1969 # Only first id on the list will be used. Openstack restriction
1970 server_group_id = affinity_group_list[0]["affinity_group_id"]
1971 scheduller_hints["group"] = server_group_id
1972
1973 self.logger.debug(
1974 "nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1975 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1976 "block_device_mapping={}, server_group={})".format(
1977 name,
1978 image_id,
1979 flavor_id,
1980 net_list_vim,
1981 self.config.get("security_groups"),
1982 vm_av_zone,
1983 self.config.get("keypair"),
1984 userdata,
1985 config_drive,
1986 block_device_mapping,
1987 server_group_id,
1988 )
1989 )
1990 server = self.nova.servers.create(
1991 name,
1992 image_id,
1993 flavor_id,
1994 nics=net_list_vim,
1995 security_groups=self.config.get("security_groups"),
1996 # TODO remove security_groups in future versions. Already at neutron port
1997 availability_zone=vm_av_zone,
1998 key_name=self.config.get("keypair"),
1999 userdata=userdata,
2000 config_drive=config_drive,
2001 block_device_mapping=block_device_mapping,
2002 scheduler_hints=scheduller_hints,
2003 ) # , description=description)
2004
2005 vm_start_time = time.time()
2006 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
2007 if no_secured_ports:
2008 self.__wait_for_vm(server.id, "ACTIVE")
2009
2010 for port in no_secured_ports:
2011 port_update = {
2012 "port": {"port_security_enabled": False, "security_groups": None}
2013 }
2014
2015 if port[1] == "allow-address-pairs":
2016 port_update = {
2017 "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}
2018 }
2019
2020 try:
2021 self.neutron.update_port(port[0], port_update)
2022 except Exception:
2023 raise vimconn.VimConnException(
2024 "It was not possible to disable port security for port {}".format(
2025 port[0]
2026 )
2027 )
2028
2029 # print "DONE :-)", server
2030
2031 # pool_id = None
2032 for floating_network in external_network:
2033 try:
2034 assigned = False
2035 floating_ip_retries = 3
2036 # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry
2037 # several times
2038 while not assigned:
2039 floating_ips = self.neutron.list_floatingips().get(
2040 "floatingips", ()
2041 )
2042 random.shuffle(floating_ips) # randomize
2043 for fip in floating_ips:
2044 if (
2045 fip.get("port_id")
2046 or fip.get("tenant_id") != server.tenant_id
2047 ):
2048 continue
2049
2050 if isinstance(floating_network["floating_ip"], str):
2051 if (
2052 fip.get("floating_network_id")
2053 != floating_network["floating_ip"]
2054 ):
2055 continue
2056
2057 free_floating_ip = fip["id"]
2058 break
2059 else:
2060 if (
2061 isinstance(floating_network["floating_ip"], str)
2062 and floating_network["floating_ip"].lower() != "true"
2063 ):
2064 pool_id = floating_network["floating_ip"]
2065 else:
2066 # Find the external network
2067 external_nets = list()
2068
2069 for net in self.neutron.list_networks()["networks"]:
2070 if net["router:external"]:
2071 external_nets.append(net)
2072
2073 if len(external_nets) == 0:
2074 raise vimconn.VimConnException(
2075 "Cannot create floating_ip automatically since "
2076 "no external network is present",
2077 http_code=vimconn.HTTP_Conflict,
2078 )
2079
2080 if len(external_nets) > 1:
2081 raise vimconn.VimConnException(
2082 "Cannot create floating_ip automatically since "
2083 "multiple external networks are present",
2084 http_code=vimconn.HTTP_Conflict,
2085 )
2086
2087 pool_id = external_nets[0].get("id")
2088
2089 param = {
2090 "floatingip": {
2091 "floating_network_id": pool_id,
2092 "tenant_id": server.tenant_id,
2093 }
2094 }
2095
2096 try:
2097 # self.logger.debug("Creating floating IP")
2098 new_floating_ip = self.neutron.create_floatingip(param)
2099 free_floating_ip = new_floating_ip["floatingip"]["id"]
2100 created_items[
2101 "floating_ip:" + str(free_floating_ip)
2102 ] = True
2103 except Exception as e:
2104 raise vimconn.VimConnException(
2105 type(e).__name__
2106 + ": Cannot create new floating_ip "
2107 + str(e),
2108 http_code=vimconn.HTTP_Conflict,
2109 )
2110
2111 try:
2112 # for race condition ensure not already assigned
2113 fip = self.neutron.show_floatingip(free_floating_ip)
2114
2115 if fip["floatingip"]["port_id"]:
2116 continue
2117
2118 # the vim_id key contains the neutron.port_id
2119 self.neutron.update_floatingip(
2120 free_floating_ip,
2121 {"floatingip": {"port_id": floating_network["vim_id"]}},
2122 )
2123 # for race condition ensure not re-assigned to other VM after 5 seconds
2124 time.sleep(5)
2125 fip = self.neutron.show_floatingip(free_floating_ip)
2126
2127 if (
2128 fip["floatingip"]["port_id"]
2129 != floating_network["vim_id"]
2130 ):
2131 self.logger.error(
2132 "floating_ip {} re-assigned to other port".format(
2133 free_floating_ip
2134 )
2135 )
2136 continue
2137
2138 self.logger.debug(
2139 "Assigned floating_ip {} to VM {}".format(
2140 free_floating_ip, server.id
2141 )
2142 )
2143 assigned = True
2144 except Exception as e:
2145 # openstack need some time after VM creation to assign an IP. So retry if fails
2146 vm_status = self.nova.servers.get(server.id).status
2147
2148 if vm_status not in ("ACTIVE", "ERROR"):
2149 if time.time() - vm_start_time < server_timeout:
2150 time.sleep(5)
2151 continue
2152 elif floating_ip_retries > 0:
2153 floating_ip_retries -= 1
2154 continue
2155
2156 raise vimconn.VimConnException(
2157 "Cannot create floating_ip: {} {}".format(
2158 type(e).__name__, e
2159 ),
2160 http_code=vimconn.HTTP_Conflict,
2161 )
2162
2163 except Exception as e:
2164 if not floating_network["exit_on_floating_ip_error"]:
2165 self.logger.error("Cannot create floating_ip. %s", str(e))
2166 continue
2167
2168 raise
2169
2170 return server.id, created_items
2171 # except nvExceptions.NotFound as e:
2172 # error_value=-vimconn.HTTP_Not_Found
2173 # error_text= "vm instance %s not found" % vm_id
2174 # except TypeError as e:
2175 # raise vimconn.VimConnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
2176
2177 except Exception as e:
2178 server_id = None
2179 if server:
2180 server_id = server.id
2181
2182 try:
2183 self.delete_vminstance(server_id, created_items)
2184 except Exception as e2:
2185 self.logger.error("new_vminstance rollback fail {}".format(e2))
2186
2187 self._format_exception(e)
2188
2189 def get_vminstance(self, vm_id):
2190 """Returns the VM instance information from VIM"""
2191 # self.logger.debug("Getting VM from VIM")
2192 try:
2193 self._reload_connection()
2194 server = self.nova.servers.find(id=vm_id)
2195 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
2196
2197 return server.to_dict()
2198 except (
2199 ksExceptions.ClientException,
2200 nvExceptions.ClientException,
2201 nvExceptions.NotFound,
2202 ConnectionError,
2203 ) as e:
2204 self._format_exception(e)
2205
2206 def get_vminstance_console(self, vm_id, console_type="vnc"):
2207 """
2208 Get a console for the virtual machine
2209 Params:
2210 vm_id: uuid of the VM
2211 console_type, can be:
2212 "novnc" (by default), "xvpvnc" for VNC types,
2213 "rdp-html5" for RDP types, "spice-html5" for SPICE types
2214 Returns dict with the console parameters:
2215 protocol: ssh, ftp, http, https, ...
2216 server: usually ip address
2217 port: the http, ssh, ... port
2218 suffix: extra text, e.g. the http path and query string
2219 """
2220 self.logger.debug("Getting VM CONSOLE from VIM")
2221
2222 try:
2223 self._reload_connection()
2224 server = self.nova.servers.find(id=vm_id)
2225
2226 if console_type is None or console_type == "novnc":
2227 console_dict = server.get_vnc_console("novnc")
2228 elif console_type == "xvpvnc":
2229 console_dict = server.get_vnc_console(console_type)
2230 elif console_type == "rdp-html5":
2231 console_dict = server.get_rdp_console(console_type)
2232 elif console_type == "spice-html5":
2233 console_dict = server.get_spice_console(console_type)
2234 else:
2235 raise vimconn.VimConnException(
2236 "console type '{}' not allowed".format(console_type),
2237 http_code=vimconn.HTTP_Bad_Request,
2238 )
2239
2240 console_dict1 = console_dict.get("console")
2241
2242 if console_dict1:
2243 console_url = console_dict1.get("url")
2244
2245 if console_url:
2246 # parse console_url
2247 protocol_index = console_url.find("//")
2248 suffix_index = (
2249 console_url[protocol_index + 2 :].find("/") + protocol_index + 2
2250 )
2251 port_index = (
2252 console_url[protocol_index + 2 : suffix_index].find(":")
2253 + protocol_index
2254 + 2
2255 )
2256
2257 if protocol_index < 0 or port_index < 0 or suffix_index < 0:
2258 return (
2259 -vimconn.HTTP_Internal_Server_Error,
2260 "Unexpected response from VIM",
2261 )
2262
2263 console_dict = {
2264 "protocol": console_url[0:protocol_index],
2265 "server": console_url[protocol_index + 2 : port_index],
2266 "port": console_url[port_index:suffix_index],
2267 "suffix": console_url[suffix_index + 1 :],
2268 }
2269 protocol_index += 2
2270
2271 return console_dict
2272 raise vimconn.VimConnUnexpectedResponse("Unexpected response from VIM")
2273 except (
2274 nvExceptions.NotFound,
2275 ksExceptions.ClientException,
2276 nvExceptions.ClientException,
2277 nvExceptions.BadRequest,
2278 ConnectionError,
2279 ) as e:
2280 self._format_exception(e)
2281
2282 def delete_vminstance(self, vm_id, created_items=None, volumes_to_hold=None):
2283 """Removes a VM instance from VIM. Returns the old identifier"""
2284 # print "osconnector: Getting VM from VIM"
2285 if created_items is None:
2286 created_items = {}
2287
2288 try:
2289 self._reload_connection()
2290 # delete VM ports attached to this networks before the virtual machine
2291 for k, v in created_items.items():
2292 if not v: # skip already deleted
2293 continue
2294
2295 try:
2296 k_item, _, k_id = k.partition(":")
2297 if k_item == "port":
2298 self.neutron.delete_port(k_id)
2299 except Exception as e:
2300 self.logger.error(
2301 "Error deleting port: {}: {}".format(type(e).__name__, e)
2302 )
2303
2304 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
2305 # #dettach volumes attached
2306 # server = self.nova.servers.get(vm_id)
2307 # volumes_attached_dict = server._info["os-extended-volumes:volumes_attached"] #volume["id"]
2308 # #for volume in volumes_attached_dict:
2309 # # self.cinder.volumes.detach(volume["id"])
2310
2311 if vm_id:
2312 self.nova.servers.delete(vm_id)
2313
2314 # delete volumes. Although having detached, they should have in active status before deleting
2315 # we ensure in this loop
2316 keep_waiting = True
2317 elapsed_time = 0
2318
2319 while keep_waiting and elapsed_time < volume_timeout:
2320 keep_waiting = False
2321
2322 for k, v in created_items.items():
2323 if not v: # skip already deleted
2324 continue
2325
2326 try:
2327 k_item, _, k_id = k.partition(":")
2328 if k_item == "volume":
2329 if self.cinder.volumes.get(k_id).status != "available":
2330 keep_waiting = True
2331 else:
2332 if k_id not in volumes_to_hold:
2333 self.cinder.volumes.delete(k_id)
2334 created_items[k] = None
2335 elif k_item == "floating_ip": # floating ip
2336 self.neutron.delete_floatingip(k_id)
2337 created_items[k] = None
2338
2339 except Exception as e:
2340 self.logger.error("Error deleting {}: {}".format(k, e))
2341
2342 if keep_waiting:
2343 time.sleep(1)
2344 elapsed_time += 1
2345
2346 return None
2347 except (
2348 nvExceptions.NotFound,
2349 ksExceptions.ClientException,
2350 nvExceptions.ClientException,
2351 ConnectionError,
2352 ) as e:
2353 self._format_exception(e)
2354
2355 def refresh_vms_status(self, vm_list):
2356 """Get the status of the virtual machines and their interfaces/ports
2357 Params: the list of VM identifiers
2358 Returns a dictionary with:
2359 vm_id: #VIM id of this Virtual Machine
2360 status: #Mandatory. Text with one of:
2361 # DELETED (not found at vim)
2362 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
2363 # OTHER (Vim reported other status not understood)
2364 # ERROR (VIM indicates an ERROR status)
2365 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
2366 # CREATING (on building process), ERROR
2367 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2368 #
2369 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
2370 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2371 interfaces:
2372 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2373 mac_address: #Text format XX:XX:XX:XX:XX:XX
2374 vim_net_id: #network id where this interface is connected
2375 vim_interface_id: #interface/port VIM id
2376 ip_address: #null, or text with IPv4, IPv6 address
2377 compute_node: #identification of compute node where PF,VF interface is allocated
2378 pci: #PCI address of the NIC that hosts the PF,VF
2379 vlan: #physical VLAN used for VF
2380 """
2381 vm_dict = {}
2382 self.logger.debug(
2383 "refresh_vms status: Getting tenant VM instance information from VIM"
2384 )
2385
2386 for vm_id in vm_list:
2387 vm = {}
2388
2389 try:
2390 vm_vim = self.get_vminstance(vm_id)
2391
2392 if vm_vim["status"] in vmStatus2manoFormat:
2393 vm["status"] = vmStatus2manoFormat[vm_vim["status"]]
2394 else:
2395 vm["status"] = "OTHER"
2396 vm["error_msg"] = "VIM status reported " + vm_vim["status"]
2397
2398 vm_vim.pop("OS-EXT-SRV-ATTR:user_data", None)
2399 vm_vim.pop("user_data", None)
2400 vm["vim_info"] = self.serialize(vm_vim)
2401
2402 vm["interfaces"] = []
2403 if vm_vim.get("fault"):
2404 vm["error_msg"] = str(vm_vim["fault"])
2405
2406 # get interfaces
2407 try:
2408 self._reload_connection()
2409 port_dict = self.neutron.list_ports(device_id=vm_id)
2410
2411 for port in port_dict["ports"]:
2412 interface = {}
2413 interface["vim_info"] = self.serialize(port)
2414 interface["mac_address"] = port.get("mac_address")
2415 interface["vim_net_id"] = port["network_id"]
2416 interface["vim_interface_id"] = port["id"]
2417 # check if OS-EXT-SRV-ATTR:host is there,
2418 # in case of non-admin credentials, it will be missing
2419
2420 if vm_vim.get("OS-EXT-SRV-ATTR:host"):
2421 interface["compute_node"] = vm_vim["OS-EXT-SRV-ATTR:host"]
2422
2423 interface["pci"] = None
2424
2425 # check if binding:profile is there,
2426 # in case of non-admin credentials, it will be missing
2427 if port.get("binding:profile"):
2428 if port["binding:profile"].get("pci_slot"):
2429 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting
2430 # the slot to 0x00
2431 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
2432 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
2433 pci = port["binding:profile"]["pci_slot"]
2434 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
2435 interface["pci"] = pci
2436
2437 interface["vlan"] = None
2438
2439 if port.get("binding:vif_details"):
2440 interface["vlan"] = port["binding:vif_details"].get("vlan")
2441
2442 # Get vlan from network in case not present in port for those old openstacks and cases where
2443 # it is needed vlan at PT
2444 if not interface["vlan"]:
2445 # if network is of type vlan and port is of type direct (sr-iov) then set vlan id
2446 network = self.neutron.show_network(port["network_id"])
2447
2448 if (
2449 network["network"].get("provider:network_type")
2450 == "vlan"
2451 ):
2452 # and port.get("binding:vnic_type") in ("direct", "direct-physical"):
2453 interface["vlan"] = network["network"].get(
2454 "provider:segmentation_id"
2455 )
2456
2457 ips = []
2458 # look for floating ip address
2459 try:
2460 floating_ip_dict = self.neutron.list_floatingips(
2461 port_id=port["id"]
2462 )
2463
2464 if floating_ip_dict.get("floatingips"):
2465 ips.append(
2466 floating_ip_dict["floatingips"][0].get(
2467 "floating_ip_address"
2468 )
2469 )
2470 except Exception:
2471 pass
2472
2473 for subnet in port["fixed_ips"]:
2474 ips.append(subnet["ip_address"])
2475
2476 interface["ip_address"] = ";".join(ips)
2477 vm["interfaces"].append(interface)
2478 except Exception as e:
2479 self.logger.error(
2480 "Error getting vm interface information {}: {}".format(
2481 type(e).__name__, e
2482 ),
2483 exc_info=True,
2484 )
2485 except vimconn.VimConnNotFoundException as e:
2486 self.logger.error("Exception getting vm status: %s", str(e))
2487 vm["status"] = "DELETED"
2488 vm["error_msg"] = str(e)
2489 except vimconn.VimConnException as e:
2490 self.logger.error("Exception getting vm status: %s", str(e))
2491 vm["status"] = "VIM_ERROR"
2492 vm["error_msg"] = str(e)
2493
2494 vm_dict[vm_id] = vm
2495
2496 return vm_dict
2497
2498 def action_vminstance(self, vm_id, action_dict, created_items={}):
2499 """Send and action over a VM instance from VIM
2500 Returns None or the console dict if the action was successfully sent to the VIM"""
2501 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
2502
2503 try:
2504 self._reload_connection()
2505 server = self.nova.servers.find(id=vm_id)
2506
2507 if "start" in action_dict:
2508 if action_dict["start"] == "rebuild":
2509 server.rebuild()
2510 else:
2511 if server.status == "PAUSED":
2512 server.unpause()
2513 elif server.status == "SUSPENDED":
2514 server.resume()
2515 elif server.status == "SHUTOFF":
2516 server.start()
2517 elif "pause" in action_dict:
2518 server.pause()
2519 elif "resume" in action_dict:
2520 server.resume()
2521 elif "shutoff" in action_dict or "shutdown" in action_dict:
2522 server.stop()
2523 elif "forceOff" in action_dict:
2524 server.stop() # TODO
2525 elif "terminate" in action_dict:
2526 server.delete()
2527 elif "createImage" in action_dict:
2528 server.create_image()
2529 # "path":path_schema,
2530 # "description":description_schema,
2531 # "name":name_schema,
2532 # "metadata":metadata_schema,
2533 # "imageRef": id_schema,
2534 # "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
2535 elif "rebuild" in action_dict:
2536 server.rebuild(server.image["id"])
2537 elif "reboot" in action_dict:
2538 server.reboot() # reboot_type="SOFT"
2539 elif "console" in action_dict:
2540 console_type = action_dict["console"]
2541
2542 if console_type is None or console_type == "novnc":
2543 console_dict = server.get_vnc_console("novnc")
2544 elif console_type == "xvpvnc":
2545 console_dict = server.get_vnc_console(console_type)
2546 elif console_type == "rdp-html5":
2547 console_dict = server.get_rdp_console(console_type)
2548 elif console_type == "spice-html5":
2549 console_dict = server.get_spice_console(console_type)
2550 else:
2551 raise vimconn.VimConnException(
2552 "console type '{}' not allowed".format(console_type),
2553 http_code=vimconn.HTTP_Bad_Request,
2554 )
2555
2556 try:
2557 console_url = console_dict["console"]["url"]
2558 # parse console_url
2559 protocol_index = console_url.find("//")
2560 suffix_index = (
2561 console_url[protocol_index + 2 :].find("/") + protocol_index + 2
2562 )
2563 port_index = (
2564 console_url[protocol_index + 2 : suffix_index].find(":")
2565 + protocol_index
2566 + 2
2567 )
2568
2569 if protocol_index < 0 or port_index < 0 or suffix_index < 0:
2570 raise vimconn.VimConnException(
2571 "Unexpected response from VIM " + str(console_dict)
2572 )
2573
2574 console_dict2 = {
2575 "protocol": console_url[0:protocol_index],
2576 "server": console_url[protocol_index + 2 : port_index],
2577 "port": int(console_url[port_index + 1 : suffix_index]),
2578 "suffix": console_url[suffix_index + 1 :],
2579 }
2580
2581 return console_dict2
2582 except Exception:
2583 raise vimconn.VimConnException(
2584 "Unexpected response from VIM " + str(console_dict)
2585 )
2586
2587 return None
2588 except (
2589 ksExceptions.ClientException,
2590 nvExceptions.ClientException,
2591 nvExceptions.NotFound,
2592 ConnectionError,
2593 ) as e:
2594 self._format_exception(e)
2595 # TODO insert exception vimconn.HTTP_Unauthorized
2596
2597 # ###### VIO Specific Changes #########
2598 def _generate_vlanID(self):
2599 """
2600 Method to get unused vlanID
2601 Args:
2602 None
2603 Returns:
2604 vlanID
2605 """
2606 # Get used VLAN IDs
2607 usedVlanIDs = []
2608 networks = self.get_network_list()
2609
2610 for net in networks:
2611 if net.get("provider:segmentation_id"):
2612 usedVlanIDs.append(net.get("provider:segmentation_id"))
2613
2614 used_vlanIDs = set(usedVlanIDs)
2615
2616 # find unused VLAN ID
2617 for vlanID_range in self.config.get("dataplane_net_vlan_range"):
2618 try:
2619 start_vlanid, end_vlanid = map(
2620 int, vlanID_range.replace(" ", "").split("-")
2621 )
2622
2623 for vlanID in range(start_vlanid, end_vlanid + 1):
2624 if vlanID not in used_vlanIDs:
2625 return vlanID
2626 except Exception as exp:
2627 raise vimconn.VimConnException(
2628 "Exception {} occurred while generating VLAN ID.".format(exp)
2629 )
2630 else:
2631 raise vimconn.VimConnConflictException(
2632 "Unable to create the SRIOV VLAN network. All given Vlan IDs {} are in use.".format(
2633 self.config.get("dataplane_net_vlan_range")
2634 )
2635 )
2636
2637 def _generate_multisegment_vlanID(self):
2638 """
2639 Method to get unused vlanID
2640 Args:
2641 None
2642 Returns:
2643 vlanID
2644 """
2645 # Get used VLAN IDs
2646 usedVlanIDs = []
2647 networks = self.get_network_list()
2648 for net in networks:
2649 if net.get("provider:network_type") == "vlan" and net.get(
2650 "provider:segmentation_id"
2651 ):
2652 usedVlanIDs.append(net.get("provider:segmentation_id"))
2653 elif net.get("segments"):
2654 for segment in net.get("segments"):
2655 if segment.get("provider:network_type") == "vlan" and segment.get(
2656 "provider:segmentation_id"
2657 ):
2658 usedVlanIDs.append(segment.get("provider:segmentation_id"))
2659
2660 used_vlanIDs = set(usedVlanIDs)
2661
2662 # find unused VLAN ID
2663 for vlanID_range in self.config.get("multisegment_vlan_range"):
2664 try:
2665 start_vlanid, end_vlanid = map(
2666 int, vlanID_range.replace(" ", "").split("-")
2667 )
2668
2669 for vlanID in range(start_vlanid, end_vlanid + 1):
2670 if vlanID not in used_vlanIDs:
2671 return vlanID
2672 except Exception as exp:
2673 raise vimconn.VimConnException(
2674 "Exception {} occurred while generating VLAN ID.".format(exp)
2675 )
2676 else:
2677 raise vimconn.VimConnConflictException(
2678 "Unable to create the VLAN segment. All VLAN IDs {} are in use.".format(
2679 self.config.get("multisegment_vlan_range")
2680 )
2681 )
2682
2683 def _validate_vlan_ranges(self, input_vlan_range, text_vlan_range):
2684 """
2685 Method to validate user given vlanID ranges
2686 Args: None
2687 Returns: None
2688 """
2689 for vlanID_range in input_vlan_range:
2690 vlan_range = vlanID_range.replace(" ", "")
2691 # validate format
2692 vlanID_pattern = r"(\d)*-(\d)*$"
2693 match_obj = re.match(vlanID_pattern, vlan_range)
2694 if not match_obj:
2695 raise vimconn.VimConnConflictException(
2696 "Invalid VLAN range for {}: {}.You must provide "
2697 "'{}' in format [start_ID - end_ID].".format(
2698 text_vlan_range, vlanID_range, text_vlan_range
2699 )
2700 )
2701
2702 start_vlanid, end_vlanid = map(int, vlan_range.split("-"))
2703 if start_vlanid <= 0:
2704 raise vimconn.VimConnConflictException(
2705 "Invalid VLAN range for {}: {}. Start ID can not be zero. For VLAN "
2706 "networks valid IDs are 1 to 4094 ".format(
2707 text_vlan_range, vlanID_range
2708 )
2709 )
2710
2711 if end_vlanid > 4094:
2712 raise vimconn.VimConnConflictException(
2713 "Invalid VLAN range for {}: {}. End VLAN ID can not be "
2714 "greater than 4094. For VLAN networks valid IDs are 1 to 4094 ".format(
2715 text_vlan_range, vlanID_range
2716 )
2717 )
2718
2719 if start_vlanid > end_vlanid:
2720 raise vimconn.VimConnConflictException(
2721 "Invalid VLAN range for {}: {}. You must provide '{}'"
2722 " in format start_ID - end_ID and start_ID < end_ID ".format(
2723 text_vlan_range, vlanID_range, text_vlan_range
2724 )
2725 )
2726
2727 # NOT USED FUNCTIONS
2728
2729 def new_external_port(self, port_data):
2730 """Adds a external port to VIM
2731 Returns the port identifier"""
2732 # TODO openstack if needed
2733 return (
2734 -vimconn.HTTP_Internal_Server_Error,
2735 "osconnector.new_external_port() not implemented",
2736 )
2737
2738 def connect_port_network(self, port_id, network_id, admin=False):
2739 """Connects a external port to a network
2740 Returns status code of the VIM response"""
2741 # TODO openstack if needed
2742 return (
2743 -vimconn.HTTP_Internal_Server_Error,
2744 "osconnector.connect_port_network() not implemented",
2745 )
2746
2747 def new_user(self, user_name, user_passwd, tenant_id=None):
2748 """Adds a new user to openstack VIM
2749 Returns the user identifier"""
2750 self.logger.debug("osconnector: Adding a new user to VIM")
2751
2752 try:
2753 self._reload_connection()
2754 user = self.keystone.users.create(
2755 user_name, password=user_passwd, default_project=tenant_id
2756 )
2757 # self.keystone.tenants.add_user(self.k_creds["username"], #role)
2758
2759 return user.id
2760 except ksExceptions.ConnectionError as e:
2761 error_value = -vimconn.HTTP_Bad_Request
2762 error_text = (
2763 type(e).__name__
2764 + ": "
2765 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2766 )
2767 except ksExceptions.ClientException as e: # TODO remove
2768 error_value = -vimconn.HTTP_Bad_Request
2769 error_text = (
2770 type(e).__name__
2771 + ": "
2772 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2773 )
2774
2775 # TODO insert exception vimconn.HTTP_Unauthorized
2776 # if reaching here is because an exception
2777 self.logger.debug("new_user " + error_text)
2778
2779 return error_value, error_text
2780
2781 def delete_user(self, user_id):
2782 """Delete a user from openstack VIM
2783 Returns the user identifier"""
2784 if self.debug:
2785 print("osconnector: Deleting a user from VIM")
2786
2787 try:
2788 self._reload_connection()
2789 self.keystone.users.delete(user_id)
2790
2791 return 1, user_id
2792 except ksExceptions.ConnectionError as e:
2793 error_value = -vimconn.HTTP_Bad_Request
2794 error_text = (
2795 type(e).__name__
2796 + ": "
2797 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2798 )
2799 except ksExceptions.NotFound as e:
2800 error_value = -vimconn.HTTP_Not_Found
2801 error_text = (
2802 type(e).__name__
2803 + ": "
2804 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2805 )
2806 except ksExceptions.ClientException as e: # TODO remove
2807 error_value = -vimconn.HTTP_Bad_Request
2808 error_text = (
2809 type(e).__name__
2810 + ": "
2811 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2812 )
2813
2814 # TODO insert exception vimconn.HTTP_Unauthorized
2815 # if reaching here is because an exception
2816 self.logger.debug("delete_tenant " + error_text)
2817
2818 return error_value, error_text
2819
2820 def get_hosts_info(self):
2821 """Get the information of deployed hosts
2822 Returns the hosts content"""
2823 if self.debug:
2824 print("osconnector: Getting Host info from VIM")
2825
2826 try:
2827 h_list = []
2828 self._reload_connection()
2829 hypervisors = self.nova.hypervisors.list()
2830
2831 for hype in hypervisors:
2832 h_list.append(hype.to_dict())
2833
2834 return 1, {"hosts": h_list}
2835 except nvExceptions.NotFound as e:
2836 error_value = -vimconn.HTTP_Not_Found
2837 error_text = str(e) if len(e.args) == 0 else str(e.args[0])
2838 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
2839 error_value = -vimconn.HTTP_Bad_Request
2840 error_text = (
2841 type(e).__name__
2842 + ": "
2843 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2844 )
2845
2846 # TODO insert exception vimconn.HTTP_Unauthorized
2847 # if reaching here is because an exception
2848 self.logger.debug("get_hosts_info " + error_text)
2849
2850 return error_value, error_text
2851
2852 def get_hosts(self, vim_tenant):
2853 """Get the hosts and deployed instances
2854 Returns the hosts content"""
2855 r, hype_dict = self.get_hosts_info()
2856
2857 if r < 0:
2858 return r, hype_dict
2859
2860 hypervisors = hype_dict["hosts"]
2861
2862 try:
2863 servers = self.nova.servers.list()
2864 for hype in hypervisors:
2865 for server in servers:
2866 if (
2867 server.to_dict()["OS-EXT-SRV-ATTR:hypervisor_hostname"]
2868 == hype["hypervisor_hostname"]
2869 ):
2870 if "vm" in hype:
2871 hype["vm"].append(server.id)
2872 else:
2873 hype["vm"] = [server.id]
2874
2875 return 1, hype_dict
2876 except nvExceptions.NotFound as e:
2877 error_value = -vimconn.HTTP_Not_Found
2878 error_text = str(e) if len(e.args) == 0 else str(e.args[0])
2879 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
2880 error_value = -vimconn.HTTP_Bad_Request
2881 error_text = (
2882 type(e).__name__
2883 + ": "
2884 + (str(e) if len(e.args) == 0 else str(e.args[0]))
2885 )
2886
2887 # TODO insert exception vimconn.HTTP_Unauthorized
2888 # if reaching here is because an exception
2889 self.logger.debug("get_hosts " + error_text)
2890
2891 return error_value, error_text
2892
2893 def new_classification(self, name, ctype, definition):
2894 self.logger.debug(
2895 "Adding a new (Traffic) Classification to VIM, named %s", name
2896 )
2897
2898 try:
2899 new_class = None
2900 self._reload_connection()
2901
2902 if ctype not in supportedClassificationTypes:
2903 raise vimconn.VimConnNotSupportedException(
2904 "OpenStack VIM connector does not support provided "
2905 "Classification Type {}, supported ones are: {}".format(
2906 ctype, supportedClassificationTypes
2907 )
2908 )
2909
2910 if not self._validate_classification(ctype, definition):
2911 raise vimconn.VimConnException(
2912 "Incorrect Classification definition for the type specified."
2913 )
2914
2915 classification_dict = definition
2916 classification_dict["name"] = name
2917 new_class = self.neutron.create_sfc_flow_classifier(
2918 {"flow_classifier": classification_dict}
2919 )
2920
2921 return new_class["flow_classifier"]["id"]
2922 except (
2923 neExceptions.ConnectionFailed,
2924 ksExceptions.ClientException,
2925 neExceptions.NeutronException,
2926 ConnectionError,
2927 ) as e:
2928 self.logger.error("Creation of Classification failed.")
2929 self._format_exception(e)
2930
2931 def get_classification(self, class_id):
2932 self.logger.debug(" Getting Classification %s from VIM", class_id)
2933 filter_dict = {"id": class_id}
2934 class_list = self.get_classification_list(filter_dict)
2935
2936 if len(class_list) == 0:
2937 raise vimconn.VimConnNotFoundException(
2938 "Classification '{}' not found".format(class_id)
2939 )
2940 elif len(class_list) > 1:
2941 raise vimconn.VimConnConflictException(
2942 "Found more than one Classification with this criteria"
2943 )
2944
2945 classification = class_list[0]
2946
2947 return classification
2948
2949 def get_classification_list(self, filter_dict={}):
2950 self.logger.debug(
2951 "Getting Classifications from VIM filter: '%s'", str(filter_dict)
2952 )
2953
2954 try:
2955 filter_dict_os = filter_dict.copy()
2956 self._reload_connection()
2957
2958 if self.api_version3 and "tenant_id" in filter_dict_os:
2959 filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id")
2960
2961 classification_dict = self.neutron.list_sfc_flow_classifiers(
2962 **filter_dict_os
2963 )
2964 classification_list = classification_dict["flow_classifiers"]
2965 self.__classification_os2mano(classification_list)
2966
2967 return classification_list
2968 except (
2969 neExceptions.ConnectionFailed,
2970 ksExceptions.ClientException,
2971 neExceptions.NeutronException,
2972 ConnectionError,
2973 ) as e:
2974 self._format_exception(e)
2975
2976 def delete_classification(self, class_id):
2977 self.logger.debug("Deleting Classification '%s' from VIM", class_id)
2978
2979 try:
2980 self._reload_connection()
2981 self.neutron.delete_sfc_flow_classifier(class_id)
2982
2983 return class_id
2984 except (
2985 neExceptions.ConnectionFailed,
2986 neExceptions.NeutronException,
2987 ksExceptions.ClientException,
2988 neExceptions.NeutronException,
2989 ConnectionError,
2990 ) as e:
2991 self._format_exception(e)
2992
2993 def new_sfi(self, name, ingress_ports, egress_ports, sfc_encap=True):
2994 self.logger.debug(
2995 "Adding a new Service Function Instance to VIM, named '%s'", name
2996 )
2997
2998 try:
2999 new_sfi = None
3000 self._reload_connection()
3001 correlation = None
3002
3003 if sfc_encap:
3004 correlation = "nsh"
3005
3006 if len(ingress_ports) != 1:
3007 raise vimconn.VimConnNotSupportedException(
3008 "OpenStack VIM connector can only have 1 ingress port per SFI"
3009 )
3010
3011 if len(egress_ports) != 1:
3012 raise vimconn.VimConnNotSupportedException(
3013 "OpenStack VIM connector can only have 1 egress port per SFI"
3014 )
3015
3016 sfi_dict = {
3017 "name": name,
3018 "ingress": ingress_ports[0],
3019 "egress": egress_ports[0],
3020 "service_function_parameters": {"correlation": correlation},
3021 }
3022 new_sfi = self.neutron.create_sfc_port_pair({"port_pair": sfi_dict})
3023
3024 return new_sfi["port_pair"]["id"]
3025 except (
3026 neExceptions.ConnectionFailed,
3027 ksExceptions.ClientException,
3028 neExceptions.NeutronException,
3029 ConnectionError,
3030 ) as e:
3031 if new_sfi:
3032 try:
3033 self.neutron.delete_sfc_port_pair(new_sfi["port_pair"]["id"])
3034 except Exception:
3035 self.logger.error(
3036 "Creation of Service Function Instance failed, with "
3037 "subsequent deletion failure as well."
3038 )
3039
3040 self._format_exception(e)
3041
3042 def get_sfi(self, sfi_id):
3043 self.logger.debug("Getting Service Function Instance %s from VIM", sfi_id)
3044 filter_dict = {"id": sfi_id}
3045 sfi_list = self.get_sfi_list(filter_dict)
3046
3047 if len(sfi_list) == 0:
3048 raise vimconn.VimConnNotFoundException(
3049 "Service Function Instance '{}' not found".format(sfi_id)
3050 )
3051 elif len(sfi_list) > 1:
3052 raise vimconn.VimConnConflictException(
3053 "Found more than one Service Function Instance with this criteria"
3054 )
3055
3056 sfi = sfi_list[0]
3057
3058 return sfi
3059
3060 def get_sfi_list(self, filter_dict={}):
3061 self.logger.debug(
3062 "Getting Service Function Instances from VIM filter: '%s'", str(filter_dict)
3063 )
3064
3065 try:
3066 self._reload_connection()
3067 filter_dict_os = filter_dict.copy()
3068
3069 if self.api_version3 and "tenant_id" in filter_dict_os:
3070 filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id")
3071
3072 sfi_dict = self.neutron.list_sfc_port_pairs(**filter_dict_os)
3073 sfi_list = sfi_dict["port_pairs"]
3074 self.__sfi_os2mano(sfi_list)
3075
3076 return sfi_list
3077 except (
3078 neExceptions.ConnectionFailed,
3079 ksExceptions.ClientException,
3080 neExceptions.NeutronException,
3081 ConnectionError,
3082 ) as e:
3083 self._format_exception(e)
3084
3085 def delete_sfi(self, sfi_id):
3086 self.logger.debug("Deleting Service Function Instance '%s' from VIM", sfi_id)
3087
3088 try:
3089 self._reload_connection()
3090 self.neutron.delete_sfc_port_pair(sfi_id)
3091
3092 return sfi_id
3093 except (
3094 neExceptions.ConnectionFailed,
3095 neExceptions.NeutronException,
3096 ksExceptions.ClientException,
3097 neExceptions.NeutronException,
3098 ConnectionError,
3099 ) as e:
3100 self._format_exception(e)
3101
3102 def new_sf(self, name, sfis, sfc_encap=True):
3103 self.logger.debug("Adding a new Service Function to VIM, named '%s'", name)
3104
3105 try:
3106 new_sf = None
3107 self._reload_connection()
3108 # correlation = None
3109 # if sfc_encap:
3110 # correlation = "nsh"
3111
3112 for instance in sfis:
3113 sfi = self.get_sfi(instance)
3114
3115 if sfi.get("sfc_encap") != sfc_encap:
3116 raise vimconn.VimConnNotSupportedException(
3117 "OpenStack VIM connector requires all SFIs of the "
3118 "same SF to share the same SFC Encapsulation"
3119 )
3120
3121 sf_dict = {"name": name, "port_pairs": sfis}
3122 new_sf = self.neutron.create_sfc_port_pair_group(
3123 {"port_pair_group": sf_dict}
3124 )
3125
3126 return new_sf["port_pair_group"]["id"]
3127 except (
3128 neExceptions.ConnectionFailed,
3129 ksExceptions.ClientException,
3130 neExceptions.NeutronException,
3131 ConnectionError,
3132 ) as e:
3133 if new_sf:
3134 try:
3135 self.neutron.delete_sfc_port_pair_group(
3136 new_sf["port_pair_group"]["id"]
3137 )
3138 except Exception:
3139 self.logger.error(
3140 "Creation of Service Function failed, with "
3141 "subsequent deletion failure as well."
3142 )
3143
3144 self._format_exception(e)
3145
3146 def get_sf(self, sf_id):
3147 self.logger.debug("Getting Service Function %s from VIM", sf_id)
3148 filter_dict = {"id": sf_id}
3149 sf_list = self.get_sf_list(filter_dict)
3150
3151 if len(sf_list) == 0:
3152 raise vimconn.VimConnNotFoundException(
3153 "Service Function '{}' not found".format(sf_id)
3154 )
3155 elif len(sf_list) > 1:
3156 raise vimconn.VimConnConflictException(
3157 "Found more than one Service Function with this criteria"
3158 )
3159
3160 sf = sf_list[0]
3161
3162 return sf
3163
3164 def get_sf_list(self, filter_dict={}):
3165 self.logger.debug(
3166 "Getting Service Function from VIM filter: '%s'", str(filter_dict)
3167 )
3168
3169 try:
3170 self._reload_connection()
3171 filter_dict_os = filter_dict.copy()
3172
3173 if self.api_version3 and "tenant_id" in filter_dict_os:
3174 filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id")
3175
3176 sf_dict = self.neutron.list_sfc_port_pair_groups(**filter_dict_os)
3177 sf_list = sf_dict["port_pair_groups"]
3178 self.__sf_os2mano(sf_list)
3179
3180 return sf_list
3181 except (
3182 neExceptions.ConnectionFailed,
3183 ksExceptions.ClientException,
3184 neExceptions.NeutronException,
3185 ConnectionError,
3186 ) as e:
3187 self._format_exception(e)
3188
3189 def delete_sf(self, sf_id):
3190 self.logger.debug("Deleting Service Function '%s' from VIM", sf_id)
3191
3192 try:
3193 self._reload_connection()
3194 self.neutron.delete_sfc_port_pair_group(sf_id)
3195
3196 return sf_id
3197 except (
3198 neExceptions.ConnectionFailed,
3199 neExceptions.NeutronException,
3200 ksExceptions.ClientException,
3201 neExceptions.NeutronException,
3202 ConnectionError,
3203 ) as e:
3204 self._format_exception(e)
3205
3206 def new_sfp(self, name, classifications, sfs, sfc_encap=True, spi=None):
3207 self.logger.debug("Adding a new Service Function Path to VIM, named '%s'", name)
3208
3209 try:
3210 new_sfp = None
3211 self._reload_connection()
3212 # In networking-sfc the MPLS encapsulation is legacy
3213 # should be used when no full SFC Encapsulation is intended
3214 correlation = "mpls"
3215
3216 if sfc_encap:
3217 correlation = "nsh"
3218
3219 sfp_dict = {
3220 "name": name,
3221 "flow_classifiers": classifications,
3222 "port_pair_groups": sfs,
3223 "chain_parameters": {"correlation": correlation},
3224 }
3225
3226 if spi:
3227 sfp_dict["chain_id"] = spi
3228
3229 new_sfp = self.neutron.create_sfc_port_chain({"port_chain": sfp_dict})
3230
3231 return new_sfp["port_chain"]["id"]
3232 except (
3233 neExceptions.ConnectionFailed,
3234 ksExceptions.ClientException,
3235 neExceptions.NeutronException,
3236 ConnectionError,
3237 ) as e:
3238 if new_sfp:
3239 try:
3240 self.neutron.delete_sfc_port_chain(new_sfp["port_chain"]["id"])
3241 except Exception:
3242 self.logger.error(
3243 "Creation of Service Function Path failed, with "
3244 "subsequent deletion failure as well."
3245 )
3246
3247 self._format_exception(e)
3248
3249 def get_sfp(self, sfp_id):
3250 self.logger.debug(" Getting Service Function Path %s from VIM", sfp_id)
3251
3252 filter_dict = {"id": sfp_id}
3253 sfp_list = self.get_sfp_list(filter_dict)
3254
3255 if len(sfp_list) == 0:
3256 raise vimconn.VimConnNotFoundException(
3257 "Service Function Path '{}' not found".format(sfp_id)
3258 )
3259 elif len(sfp_list) > 1:
3260 raise vimconn.VimConnConflictException(
3261 "Found more than one Service Function Path with this criteria"
3262 )
3263
3264 sfp = sfp_list[0]
3265
3266 return sfp
3267
3268 def get_sfp_list(self, filter_dict={}):
3269 self.logger.debug(
3270 "Getting Service Function Paths from VIM filter: '%s'", str(filter_dict)
3271 )
3272
3273 try:
3274 self._reload_connection()
3275 filter_dict_os = filter_dict.copy()
3276
3277 if self.api_version3 and "tenant_id" in filter_dict_os:
3278 filter_dict_os["project_id"] = filter_dict_os.pop("tenant_id")
3279
3280 sfp_dict = self.neutron.list_sfc_port_chains(**filter_dict_os)
3281 sfp_list = sfp_dict["port_chains"]
3282 self.__sfp_os2mano(sfp_list)
3283
3284 return sfp_list
3285 except (
3286 neExceptions.ConnectionFailed,
3287 ksExceptions.ClientException,
3288 neExceptions.NeutronException,
3289 ConnectionError,
3290 ) as e:
3291 self._format_exception(e)
3292
3293 def delete_sfp(self, sfp_id):
3294 self.logger.debug("Deleting Service Function Path '%s' from VIM", sfp_id)
3295
3296 try:
3297 self._reload_connection()
3298 self.neutron.delete_sfc_port_chain(sfp_id)
3299
3300 return sfp_id
3301 except (
3302 neExceptions.ConnectionFailed,
3303 neExceptions.NeutronException,
3304 ksExceptions.ClientException,
3305 neExceptions.NeutronException,
3306 ConnectionError,
3307 ) as e:
3308 self._format_exception(e)
3309
3310 def refresh_sfps_status(self, sfp_list):
3311 """Get the status of the service function path
3312 Params: the list of sfp identifiers
3313 Returns a dictionary with:
3314 vm_id: #VIM id of this service function path
3315 status: #Mandatory. Text with one of:
3316 # DELETED (not found at vim)
3317 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3318 # OTHER (Vim reported other status not understood)
3319 # ERROR (VIM indicates an ERROR status)
3320 # ACTIVE,
3321 # CREATING (on building process)
3322 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3323 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
3324 """
3325 sfp_dict = {}
3326 self.logger.debug(
3327 "refresh_sfps status: Getting tenant SFP information from VIM"
3328 )
3329
3330 for sfp_id in sfp_list:
3331 sfp = {}
3332
3333 try:
3334 sfp_vim = self.get_sfp(sfp_id)
3335
3336 if sfp_vim["spi"]:
3337 sfp["status"] = vmStatus2manoFormat["ACTIVE"]
3338 else:
3339 sfp["status"] = "OTHER"
3340 sfp["error_msg"] = "VIM status reported " + sfp["status"]
3341
3342 sfp["vim_info"] = self.serialize(sfp_vim)
3343
3344 if sfp_vim.get("fault"):
3345 sfp["error_msg"] = str(sfp_vim["fault"])
3346 except vimconn.VimConnNotFoundException as e:
3347 self.logger.error("Exception getting sfp status: %s", str(e))
3348 sfp["status"] = "DELETED"
3349 sfp["error_msg"] = str(e)
3350 except vimconn.VimConnException as e:
3351 self.logger.error("Exception getting sfp status: %s", str(e))
3352 sfp["status"] = "VIM_ERROR"
3353 sfp["error_msg"] = str(e)
3354
3355 sfp_dict[sfp_id] = sfp
3356
3357 return sfp_dict
3358
3359 def refresh_sfis_status(self, sfi_list):
3360 """Get the status of the service function instances
3361 Params: the list of sfi identifiers
3362 Returns a dictionary with:
3363 vm_id: #VIM id of this service function instance
3364 status: #Mandatory. Text with one of:
3365 # DELETED (not found at vim)
3366 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3367 # OTHER (Vim reported other status not understood)
3368 # ERROR (VIM indicates an ERROR status)
3369 # ACTIVE,
3370 # CREATING (on building process)
3371 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3372 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3373 """
3374 sfi_dict = {}
3375 self.logger.debug(
3376 "refresh_sfis status: Getting tenant sfi information from VIM"
3377 )
3378
3379 for sfi_id in sfi_list:
3380 sfi = {}
3381
3382 try:
3383 sfi_vim = self.get_sfi(sfi_id)
3384
3385 if sfi_vim:
3386 sfi["status"] = vmStatus2manoFormat["ACTIVE"]
3387 else:
3388 sfi["status"] = "OTHER"
3389 sfi["error_msg"] = "VIM status reported " + sfi["status"]
3390
3391 sfi["vim_info"] = self.serialize(sfi_vim)
3392
3393 if sfi_vim.get("fault"):
3394 sfi["error_msg"] = str(sfi_vim["fault"])
3395 except vimconn.VimConnNotFoundException as e:
3396 self.logger.error("Exception getting sfi status: %s", str(e))
3397 sfi["status"] = "DELETED"
3398 sfi["error_msg"] = str(e)
3399 except vimconn.VimConnException as e:
3400 self.logger.error("Exception getting sfi status: %s", str(e))
3401 sfi["status"] = "VIM_ERROR"
3402 sfi["error_msg"] = str(e)
3403
3404 sfi_dict[sfi_id] = sfi
3405
3406 return sfi_dict
3407
3408 def refresh_sfs_status(self, sf_list):
3409 """Get the status of the service functions
3410 Params: the list of sf identifiers
3411 Returns a dictionary with:
3412 vm_id: #VIM id of this service function
3413 status: #Mandatory. Text with one of:
3414 # DELETED (not found at vim)
3415 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3416 # OTHER (Vim reported other status not understood)
3417 # ERROR (VIM indicates an ERROR status)
3418 # ACTIVE,
3419 # CREATING (on building process)
3420 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3421 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3422 """
3423 sf_dict = {}
3424 self.logger.debug("refresh_sfs status: Getting tenant sf information from VIM")
3425
3426 for sf_id in sf_list:
3427 sf = {}
3428
3429 try:
3430 sf_vim = self.get_sf(sf_id)
3431
3432 if sf_vim:
3433 sf["status"] = vmStatus2manoFormat["ACTIVE"]
3434 else:
3435 sf["status"] = "OTHER"
3436 sf["error_msg"] = "VIM status reported " + sf_vim["status"]
3437
3438 sf["vim_info"] = self.serialize(sf_vim)
3439
3440 if sf_vim.get("fault"):
3441 sf["error_msg"] = str(sf_vim["fault"])
3442 except vimconn.VimConnNotFoundException as e:
3443 self.logger.error("Exception getting sf status: %s", str(e))
3444 sf["status"] = "DELETED"
3445 sf["error_msg"] = str(e)
3446 except vimconn.VimConnException as e:
3447 self.logger.error("Exception getting sf status: %s", str(e))
3448 sf["status"] = "VIM_ERROR"
3449 sf["error_msg"] = str(e)
3450
3451 sf_dict[sf_id] = sf
3452
3453 return sf_dict
3454
3455 def refresh_classifications_status(self, classification_list):
3456 """Get the status of the classifications
3457 Params: the list of classification identifiers
3458 Returns a dictionary with:
3459 vm_id: #VIM id of this classifier
3460 status: #Mandatory. Text with one of:
3461 # DELETED (not found at vim)
3462 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3463 # OTHER (Vim reported other status not understood)
3464 # ERROR (VIM indicates an ERROR status)
3465 # ACTIVE,
3466 # CREATING (on building process)
3467 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3468 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3469 """
3470 classification_dict = {}
3471 self.logger.debug(
3472 "refresh_classifications status: Getting tenant classification information from VIM"
3473 )
3474
3475 for classification_id in classification_list:
3476 classification = {}
3477
3478 try:
3479 classification_vim = self.get_classification(classification_id)
3480
3481 if classification_vim:
3482 classification["status"] = vmStatus2manoFormat["ACTIVE"]
3483 else:
3484 classification["status"] = "OTHER"
3485 classification["error_msg"] = (
3486 "VIM status reported " + classification["status"]
3487 )
3488
3489 classification["vim_info"] = self.serialize(classification_vim)
3490
3491 if classification_vim.get("fault"):
3492 classification["error_msg"] = str(classification_vim["fault"])
3493 except vimconn.VimConnNotFoundException as e:
3494 self.logger.error("Exception getting classification status: %s", str(e))
3495 classification["status"] = "DELETED"
3496 classification["error_msg"] = str(e)
3497 except vimconn.VimConnException as e:
3498 self.logger.error("Exception getting classification status: %s", str(e))
3499 classification["status"] = "VIM_ERROR"
3500 classification["error_msg"] = str(e)
3501
3502 classification_dict[classification_id] = classification
3503
3504 return classification_dict
3505
3506 def new_affinity_group(self, affinity_group_data):
3507 """Adds a server group to VIM
3508 affinity_group_data contains a dictionary with information, keys:
3509 name: name in VIM for the server group
3510 type: affinity or anti-affinity
3511 scope: Only nfvi-node allowed
3512 Returns the server group identifier"""
3513 self.logger.debug("Adding Server Group '%s'", str(affinity_group_data))
3514
3515 try:
3516 name = affinity_group_data["name"]
3517 policy = affinity_group_data["type"]
3518
3519 self._reload_connection()
3520 new_server_group = self.nova.server_groups.create(name, policy)
3521
3522 return new_server_group.id
3523 except (
3524 ksExceptions.ClientException,
3525 nvExceptions.ClientException,
3526 ConnectionError,
3527 KeyError,
3528 ) as e:
3529 self._format_exception(e)
3530
3531 def get_affinity_group(self, affinity_group_id):
3532 """Obtain server group details from the VIM. Returns the server group detais as a dict"""
3533 self.logger.debug("Getting flavor '%s'", affinity_group_id)
3534 try:
3535 self._reload_connection()
3536 server_group = self.nova.server_groups.find(id=affinity_group_id)
3537
3538 return server_group.to_dict()
3539 except (
3540 nvExceptions.NotFound,
3541 nvExceptions.ClientException,
3542 ksExceptions.ClientException,
3543 ConnectionError,
3544 ) as e:
3545 self._format_exception(e)
3546
3547 def delete_affinity_group(self, affinity_group_id):
3548 """Deletes a server group from the VIM. Returns the old affinity_group_id"""
3549 self.logger.debug("Getting server group '%s'", affinity_group_id)
3550 try:
3551 self._reload_connection()
3552 self.nova.server_groups.delete(affinity_group_id)
3553
3554 return affinity_group_id
3555 except (
3556 nvExceptions.NotFound,
3557 ksExceptions.ClientException,
3558 nvExceptions.ClientException,
3559 ConnectionError,
3560 ) as e:
3561 self._format_exception(e)
3562
3563 def get_vdu_state(self, vm_id):
3564 """
3565 Getting the state of a vdu
3566 param:
3567 vm_id: ID of an instance
3568 """
3569 self.logger.debug("Getting the status of VM")
3570 self.logger.debug("VIM VM ID %s", vm_id)
3571 self._reload_connection()
3572 server = self.nova.servers.find(id=vm_id)
3573 server_dict = server.to_dict()
3574 vdu_data = [
3575 server_dict["status"],
3576 server_dict["flavor"]["id"],
3577 server_dict["OS-EXT-SRV-ATTR:host"],
3578 server_dict["OS-EXT-AZ:availability_zone"],
3579 ]
3580 self.logger.debug("vdu_data %s", vdu_data)
3581 return vdu_data
3582
3583 def check_compute_availability(self, host, server_flavor_details):
3584 self._reload_connection()
3585 hypervisor_search = self.nova.hypervisors.search(
3586 hypervisor_match=host, servers=True
3587 )
3588 for hypervisor in hypervisor_search:
3589 hypervisor_id = hypervisor.to_dict()["id"]
3590 hypervisor_details = self.nova.hypervisors.get(hypervisor=hypervisor_id)
3591 hypervisor_dict = hypervisor_details.to_dict()
3592 hypervisor_temp = json.dumps(hypervisor_dict)
3593 hypervisor_json = json.loads(hypervisor_temp)
3594 resources_available = [
3595 hypervisor_json["free_ram_mb"],
3596 hypervisor_json["disk_available_least"],
3597 hypervisor_json["vcpus"] - hypervisor_json["vcpus_used"],
3598 ]
3599 compute_available = all(
3600 x > y for x, y in zip(resources_available, server_flavor_details)
3601 )
3602 if compute_available:
3603 return host
3604
3605 def check_availability_zone(
3606 self, old_az, server_flavor_details, old_host, host=None
3607 ):
3608 self._reload_connection()
3609 az_check = {"zone_check": False, "compute_availability": None}
3610 aggregates_list = self.nova.aggregates.list()
3611 for aggregate in aggregates_list:
3612 aggregate_details = aggregate.to_dict()
3613 aggregate_temp = json.dumps(aggregate_details)
3614 aggregate_json = json.loads(aggregate_temp)
3615 if aggregate_json["availability_zone"] == old_az:
3616 hosts_list = aggregate_json["hosts"]
3617 if host is not None:
3618 if host in hosts_list:
3619 az_check["zone_check"] = True
3620 available_compute_id = self.check_compute_availability(
3621 host, server_flavor_details
3622 )
3623 if available_compute_id is not None:
3624 az_check["compute_availability"] = available_compute_id
3625 else:
3626 for check_host in hosts_list:
3627 if check_host != old_host:
3628 available_compute_id = self.check_compute_availability(
3629 check_host, server_flavor_details
3630 )
3631 if available_compute_id is not None:
3632 az_check["zone_check"] = True
3633 az_check["compute_availability"] = available_compute_id
3634 break
3635 else:
3636 az_check["zone_check"] = True
3637 return az_check
3638
3639 def migrate_instance(self, vm_id, compute_host=None):
3640 """
3641 Migrate a vdu
3642 param:
3643 vm_id: ID of an instance
3644 compute_host: Host to migrate the vdu to
3645 """
3646 self._reload_connection()
3647 vm_state = False
3648 instance_state = self.get_vdu_state(vm_id)
3649 server_flavor_id = instance_state[1]
3650 server_hypervisor_name = instance_state[2]
3651 server_availability_zone = instance_state[3]
3652 try:
3653 server_flavor = self.nova.flavors.find(id=server_flavor_id).to_dict()
3654 server_flavor_details = [
3655 server_flavor["ram"],
3656 server_flavor["disk"],
3657 server_flavor["vcpus"],
3658 ]
3659 if compute_host == server_hypervisor_name:
3660 raise vimconn.VimConnException(
3661 "Unable to migrate instance '{}' to the same host '{}'".format(
3662 vm_id, compute_host
3663 ),
3664 http_code=vimconn.HTTP_Bad_Request,
3665 )
3666 az_status = self.check_availability_zone(
3667 server_availability_zone,
3668 server_flavor_details,
3669 server_hypervisor_name,
3670 compute_host,
3671 )
3672 availability_zone_check = az_status["zone_check"]
3673 available_compute_id = az_status.get("compute_availability")
3674
3675 if availability_zone_check is False:
3676 raise vimconn.VimConnException(
3677 "Unable to migrate instance '{}' to a different availability zone".format(
3678 vm_id
3679 ),
3680 http_code=vimconn.HTTP_Bad_Request,
3681 )
3682 if available_compute_id is not None:
3683 self.nova.servers.live_migrate(
3684 server=vm_id,
3685 host=available_compute_id,
3686 block_migration=True,
3687 disk_over_commit=False,
3688 )
3689 state = "MIGRATING"
3690 changed_compute_host = ""
3691 if state == "MIGRATING":
3692 vm_state = self.__wait_for_vm(vm_id, "ACTIVE")
3693 changed_compute_host = self.get_vdu_state(vm_id)[2]
3694 if vm_state and changed_compute_host == available_compute_id:
3695 self.logger.debug(
3696 "Instance '{}' migrated to the new compute host '{}'".format(
3697 vm_id, changed_compute_host
3698 )
3699 )
3700 return state, available_compute_id
3701 else:
3702 raise vimconn.VimConnException(
3703 "Migration Failed. Instance '{}' not moved to the new host {}".format(
3704 vm_id, available_compute_id
3705 ),
3706 http_code=vimconn.HTTP_Bad_Request,
3707 )
3708 else:
3709 raise vimconn.VimConnException(
3710 "Compute '{}' not available or does not have enough resources to migrate the instance".format(
3711 available_compute_id
3712 ),
3713 http_code=vimconn.HTTP_Bad_Request,
3714 )
3715 except (
3716 nvExceptions.BadRequest,
3717 nvExceptions.ClientException,
3718 nvExceptions.NotFound,
3719 ) as e:
3720 self._format_exception(e)