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