1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
22 osconnector implements all the methods to interact with openstack using the python-neutronclient.
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)
34 from http
.client
import HTTPException
37 from pprint
import pformat
41 from typing
import Dict
, List
, Optional
, Tuple
43 from cinderclient
import client
as cClient
44 import cinderclient
.exceptions
as cExceptions
45 from glanceclient
import client
as glClient
46 import glanceclient
.exc
as gl1Exceptions
47 from keystoneauth1
import session
48 from keystoneauth1
.identity
import v2
, v3
49 import keystoneclient
.exceptions
as ksExceptions
50 import keystoneclient
.v2_0
.client
as ksClient_v2
51 import keystoneclient
.v3
.client
as ksClient_v3
53 from neutronclient
.common
import exceptions
as neExceptions
54 from neutronclient
.neutron
import client
as neClient
55 from novaclient
import client
as nClient
, exceptions
as nvExceptions
56 from osm_ro_plugin
import vimconn
57 from requests
.exceptions
import ConnectionError
60 __author__
= "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
61 __date__
= "$22-sep-2017 23:59:59$"
63 """contain the openstack virtual machine status to openmano status"""
64 vmStatus2manoFormat
= {
67 "SUSPENDED": "SUSPENDED",
68 "SHUTOFF": "INACTIVE",
73 netStatus2manoFormat
= {
76 "INACTIVE": "INACTIVE",
82 supportedClassificationTypes
= ["legacy_flow_classifier"]
84 # global var to have a timeout creating and deleting volumes
89 def catch_any_exception(func
):
90 def format_exception(*args
, **kwargs
):
92 return func(*args
, *kwargs
)
93 except Exception as e
:
94 vimconnector
._format
_exception
(e
)
96 return format_exception
99 class SafeDumper(yaml
.SafeDumper
):
100 def represent_data(self
, data
):
101 # Openstack APIs use custom subclasses of dict and YAML safe dumper
102 # is designed to not handle that (reference issue 142 of pyyaml)
103 if isinstance(data
, dict) and data
.__class
__ != dict:
104 # A simple solution is to convert those items back to dicts
105 data
= dict(data
.items())
107 return super(SafeDumper
, self
).represent_data(data
)
110 class vimconnector(vimconn
.VimConnector
):
125 """using common constructor parameters. In this case
126 'url' is the keystone authorization url,
127 'url_admin' is not use
129 api_version
= config
.get("APIversion")
131 if api_version
and api_version
not in ("v3.3", "v2.0", "2", "3"):
132 raise vimconn
.VimConnException(
133 "Invalid value '{}' for config:APIversion. "
134 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version
)
137 vim_type
= config
.get("vim_type")
139 if vim_type
and vim_type
not in ("vio", "VIO"):
140 raise vimconn
.VimConnException(
141 "Invalid value '{}' for config:vim_type."
142 "Allowed values are 'vio' or 'VIO'".format(vim_type
)
145 if config
.get("dataplane_net_vlan_range") is not None:
146 # validate vlan ranges provided by user
147 self
._validate
_vlan
_ranges
(
148 config
.get("dataplane_net_vlan_range"), "dataplane_net_vlan_range"
151 if config
.get("multisegment_vlan_range") is not None:
152 # validate vlan ranges provided by user
153 self
._validate
_vlan
_ranges
(
154 config
.get("multisegment_vlan_range"), "multisegment_vlan_range"
157 vimconn
.VimConnector
.__init
__(
171 if self
.config
.get("insecure") and self
.config
.get("ca_cert"):
172 raise vimconn
.VimConnException(
173 "options insecure and ca_cert are mutually exclusive"
178 if self
.config
.get("insecure"):
181 if self
.config
.get("ca_cert"):
182 self
.verify
= self
.config
.get("ca_cert")
185 raise TypeError("url param can not be NoneType")
187 self
.persistent_info
= persistent_info
188 self
.availability_zone
= persistent_info
.get("availability_zone", None)
189 self
.storage_availability_zone
= None
190 self
.session
= persistent_info
.get("session", {"reload_client": True})
191 self
.my_tenant_id
= self
.session
.get("my_tenant_id")
192 self
.nova
= self
.session
.get("nova")
193 self
.neutron
= self
.session
.get("neutron")
194 self
.cinder
= self
.session
.get("cinder")
195 self
.glance
= self
.session
.get("glance")
196 # self.glancev1 = self.session.get("glancev1")
197 self
.keystone
= self
.session
.get("keystone")
198 self
.api_version3
= self
.session
.get("api_version3")
199 self
.vim_type
= self
.config
.get("vim_type")
202 self
.vim_type
= self
.vim_type
.upper()
204 if self
.config
.get("use_internal_endpoint"):
205 self
.endpoint_type
= "internalURL"
207 self
.endpoint_type
= None
209 logging
.getLogger("urllib3").setLevel(logging
.WARNING
)
210 logging
.getLogger("keystoneauth").setLevel(logging
.WARNING
)
211 logging
.getLogger("novaclient").setLevel(logging
.WARNING
)
212 self
.logger
= logging
.getLogger("ro.vim.openstack")
214 # allow security_groups to be a list or a single string
215 if isinstance(self
.config
.get("security_groups"), str):
216 self
.config
["security_groups"] = [self
.config
["security_groups"]]
218 self
.security_groups_id
= None
220 # ###### VIO Specific Changes #########
221 if self
.vim_type
== "VIO":
222 self
.logger
= logging
.getLogger("ro.vim.vio")
225 self
.logger
.setLevel(getattr(logging
, log_level
))
227 def __getitem__(self
, index
):
228 """Get individuals parameters.
230 if index
== "project_domain_id":
231 return self
.config
.get("project_domain_id")
232 elif index
== "user_domain_id":
233 return self
.config
.get("user_domain_id")
235 return vimconn
.VimConnector
.__getitem
__(self
, index
)
237 def __setitem__(self
, index
, value
):
238 """Set individuals parameters and it is marked as dirty so to force connection reload.
240 if index
== "project_domain_id":
241 self
.config
["project_domain_id"] = value
242 elif index
== "user_domain_id":
243 self
.config
["user_domain_id"] = value
245 vimconn
.VimConnector
.__setitem
__(self
, index
, value
)
247 self
.session
["reload_client"] = True
249 def serialize(self
, value
):
250 """Serialization of python basic types.
252 In the case value is not serializable a message will be logged and a
253 simple representation of the data that cannot be converted back to
256 if isinstance(value
, str):
261 value
, Dumper
=SafeDumper
, default_flow_style
=True, width
=256
263 except yaml
.representer
.RepresenterError
:
265 "The following entity cannot be serialized in YAML:\n\n%s\n\n",
272 def _reload_connection(self
):
273 """Called before any operation, it check if credentials has changed
274 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
276 # TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
277 if self
.session
["reload_client"]:
278 if self
.config
.get("APIversion"):
279 self
.api_version3
= (
280 self
.config
["APIversion"] == "v3.3"
281 or self
.config
["APIversion"] == "3"
283 else: # get from ending auth_url that end with v3 or with v2.0
284 self
.api_version3
= self
.url
.endswith("/v3") or self
.url
.endswith(
288 self
.session
["api_version3"] = self
.api_version3
290 if self
.api_version3
:
291 if self
.config
.get("project_domain_id") or self
.config
.get(
292 "project_domain_name"
294 project_domain_id_default
= None
296 project_domain_id_default
= "default"
298 if self
.config
.get("user_domain_id") or self
.config
.get(
301 user_domain_id_default
= None
303 user_domain_id_default
= "default"
307 password
=self
.passwd
,
308 project_name
=self
.tenant_name
,
309 project_id
=self
.tenant_id
,
310 project_domain_id
=self
.config
.get(
311 "project_domain_id", project_domain_id_default
313 user_domain_id
=self
.config
.get(
314 "user_domain_id", user_domain_id_default
316 project_domain_name
=self
.config
.get("project_domain_name"),
317 user_domain_name
=self
.config
.get("user_domain_name"),
323 password
=self
.passwd
,
324 tenant_name
=self
.tenant_name
,
325 tenant_id
=self
.tenant_id
,
328 sess
= session
.Session(auth
=auth
, verify
=self
.verify
)
329 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
330 # Titanium cloud and StarlingX
331 region_name
= self
.config
.get("region_name")
333 if self
.api_version3
:
334 self
.keystone
= ksClient_v3
.Client(
336 endpoint_type
=self
.endpoint_type
,
337 region_name
=region_name
,
340 self
.keystone
= ksClient_v2
.Client(
341 session
=sess
, endpoint_type
=self
.endpoint_type
344 self
.session
["keystone"] = self
.keystone
345 # In order to enable microversion functionality an explicit microversion must be specified in "config".
346 # This implementation approach is due to the warning message in
347 # https://developer.openstack.org/api-guide/compute/microversions.html
348 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
349 # always require an specific microversion.
350 # To be able to use "device role tagging" functionality define "microversion: 2.32" in datacenter config
351 version
= self
.config
.get("microversion")
356 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
357 # Titanium cloud and StarlingX
358 self
.nova
= self
.session
["nova"] = nClient
.Client(
361 endpoint_type
=self
.endpoint_type
,
362 region_name
=region_name
,
364 self
.neutron
= self
.session
["neutron"] = neClient
.Client(
367 endpoint_type
=self
.endpoint_type
,
368 region_name
=region_name
,
371 if sess
.get_all_version_data(service_type
="volumev2"):
372 self
.cinder
= self
.session
["cinder"] = cClient
.Client(
375 endpoint_type
=self
.endpoint_type
,
376 region_name
=region_name
,
379 self
.cinder
= self
.session
["cinder"] = cClient
.Client(
382 endpoint_type
=self
.endpoint_type
,
383 region_name
=region_name
,
387 self
.my_tenant_id
= self
.session
["my_tenant_id"] = sess
.get_project_id()
389 self
.logger
.error("Cannot get project_id from session", exc_info
=True)
391 if self
.endpoint_type
== "internalURL":
392 glance_service_id
= self
.keystone
.services
.list(name
="glance")[0].id
393 glance_endpoint
= self
.keystone
.endpoints
.list(
394 glance_service_id
, interface
="internal"
397 glance_endpoint
= None
399 self
.glance
= self
.session
["glance"] = glClient
.Client(
400 2, session
=sess
, endpoint
=glance_endpoint
402 # using version 1 of glance client in new_image()
403 # self.glancev1 = self.session["glancev1"] = glClient.Client("1", session=sess,
404 # endpoint=glance_endpoint)
405 self
.session
["reload_client"] = False
406 self
.persistent_info
["session"] = self
.session
407 # add availablity zone info inside self.persistent_info
408 self
._set
_availablity
_zones
()
409 self
.persistent_info
["availability_zone"] = self
.availability_zone
410 # force to get again security_groups_ids next time they are needed
411 self
.security_groups_id
= None
413 def __net_os2mano(self
, net_list_dict
):
414 """Transform the net openstack format to mano format
415 net_list_dict can be a list of dict or a single dict"""
416 if type(net_list_dict
) is dict:
417 net_list_
= (net_list_dict
,)
418 elif type(net_list_dict
) is list:
419 net_list_
= net_list_dict
421 raise TypeError("param net_list_dict must be a list or a dictionary")
422 for net
in net_list_
:
423 if net
.get("provider:network_type") == "vlan":
426 net
["type"] = "bridge"
428 def __classification_os2mano(self
, class_list_dict
):
429 """Transform the openstack format (Flow Classifier) to mano format
430 (Classification) class_list_dict can be a list of dict or a single dict
432 if isinstance(class_list_dict
, dict):
433 class_list_
= [class_list_dict
]
434 elif isinstance(class_list_dict
, list):
435 class_list_
= class_list_dict
437 raise TypeError("param class_list_dict must be a list or a dictionary")
438 for classification
in class_list_
:
439 id = classification
.pop("id")
440 name
= classification
.pop("name")
441 description
= classification
.pop("description")
442 project_id
= classification
.pop("project_id")
443 tenant_id
= classification
.pop("tenant_id")
444 original_classification
= copy
.deepcopy(classification
)
445 classification
.clear()
446 classification
["ctype"] = "legacy_flow_classifier"
447 classification
["definition"] = original_classification
448 classification
["id"] = id
449 classification
["name"] = name
450 classification
["description"] = description
451 classification
["project_id"] = project_id
452 classification
["tenant_id"] = tenant_id
454 def __sfi_os2mano(self
, sfi_list_dict
):
455 """Transform the openstack format (Port Pair) to mano format (SFI)
456 sfi_list_dict can be a list of dict or a single dict
458 if isinstance(sfi_list_dict
, dict):
459 sfi_list_
= [sfi_list_dict
]
460 elif isinstance(sfi_list_dict
, list):
461 sfi_list_
= sfi_list_dict
463 raise TypeError("param sfi_list_dict must be a list or a dictionary")
465 for sfi
in sfi_list_
:
466 sfi
["ingress_ports"] = []
467 sfi
["egress_ports"] = []
469 if sfi
.get("ingress"):
470 sfi
["ingress_ports"].append(sfi
["ingress"])
472 if sfi
.get("egress"):
473 sfi
["egress_ports"].append(sfi
["egress"])
477 params
= sfi
.get("service_function_parameters")
481 correlation
= params
.get("correlation")
486 sfi
["sfc_encap"] = sfc_encap
487 del sfi
["service_function_parameters"]
489 def __sf_os2mano(self
, sf_list_dict
):
490 """Transform the openstack format (Port Pair Group) to mano format (SF)
491 sf_list_dict can be a list of dict or a single dict
493 if isinstance(sf_list_dict
, dict):
494 sf_list_
= [sf_list_dict
]
495 elif isinstance(sf_list_dict
, list):
496 sf_list_
= sf_list_dict
498 raise TypeError("param sf_list_dict must be a list or a dictionary")
501 del sf
["port_pair_group_parameters"]
502 sf
["sfis"] = sf
["port_pairs"]
505 def __sfp_os2mano(self
, sfp_list_dict
):
506 """Transform the openstack format (Port Chain) to mano format (SFP)
507 sfp_list_dict can be a list of dict or a single dict
509 if isinstance(sfp_list_dict
, dict):
510 sfp_list_
= [sfp_list_dict
]
511 elif isinstance(sfp_list_dict
, list):
512 sfp_list_
= sfp_list_dict
514 raise TypeError("param sfp_list_dict must be a list or a dictionary")
516 for sfp
in sfp_list_
:
517 params
= sfp
.pop("chain_parameters")
521 correlation
= params
.get("correlation")
526 sfp
["sfc_encap"] = sfc_encap
527 sfp
["spi"] = sfp
.pop("chain_id")
528 sfp
["classifications"] = sfp
.pop("flow_classifiers")
529 sfp
["service_functions"] = sfp
.pop("port_pair_groups")
531 # placeholder for now; read TODO note below
532 def _validate_classification(self
, type, definition
):
533 # only legacy_flow_classifier Type is supported at this point
535 # TODO(igordcard): this method should be an abstract method of an
536 # abstract Classification class to be implemented by the specific
537 # Types. Also, abstract vimconnector should call the validation
538 # method before the implemented VIM connectors are called.
541 def _format_exception(exception
):
542 """Transform a keystone, nova, neutron exception into a vimconn exception discovering the cause"""
543 message_error
= str(exception
)
549 neExceptions
.NetworkNotFoundClient
,
550 nvExceptions
.NotFound
,
551 nvExceptions
.ResourceNotFound
,
552 ksExceptions
.NotFound
,
553 gl1Exceptions
.HTTPNotFound
,
554 cExceptions
.NotFound
,
557 raise vimconn
.VimConnNotFoundException(
558 type(exception
).__name
__ + ": " + message_error
564 gl1Exceptions
.HTTPException
,
565 gl1Exceptions
.CommunicationError
,
567 ksExceptions
.ConnectionError
,
568 neExceptions
.ConnectionFailed
,
569 cExceptions
.ConnectionError
,
572 if type(exception
).__name
__ == "SSLError":
573 tip
= " (maybe option 'insecure' must be added to the VIM)"
575 raise vimconn
.VimConnConnectionException(
576 "Invalid URL or credentials{}: {}".format(tip
, message_error
)
582 nvExceptions
.BadRequest
,
583 ksExceptions
.BadRequest
,
584 gl1Exceptions
.BadRequest
,
585 cExceptions
.BadRequest
,
588 if message_error
== "OS-EXT-SRV-ATTR:host":
589 tip
= " (If the user does not have non-admin credentials, this attribute will be missing)"
590 raise vimconn
.VimConnInsufficientCredentials(
591 type(exception
).__name
__ + ": " + message_error
+ tip
593 raise vimconn
.VimConnException(
594 type(exception
).__name
__ + ": " + message_error
600 nvExceptions
.ClientException
,
601 ksExceptions
.ClientException
,
602 neExceptions
.NeutronException
,
603 cExceptions
.ClientException
,
606 raise vimconn
.VimConnUnexpectedResponse(
607 type(exception
).__name
__ + ": " + message_error
609 elif isinstance(exception
, nvExceptions
.Conflict
):
610 raise vimconn
.VimConnConflictException(
611 type(exception
).__name
__ + ": " + message_error
613 elif isinstance(exception
, vimconn
.VimConnException
):
616 logger
= logging
.getLogger("ro.vim.openstack")
617 logger
.error("General Exception " + message_error
, exc_info
=True)
619 raise vimconn
.VimConnException(
620 type(exception
).__name
__ + ": " + message_error
623 def _get_ids_from_name(self
):
625 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
628 # get tenant_id if only tenant_name is supplied
629 self
._reload
_connection
()
631 if not self
.my_tenant_id
:
632 raise vimconn
.VimConnConnectionException(
633 "Error getting tenant information from name={} id={}".format(
634 self
.tenant_name
, self
.tenant_id
638 if self
.config
.get("security_groups") and not self
.security_groups_id
:
639 # convert from name to id
640 neutron_sg_list
= self
.neutron
.list_security_groups(
641 tenant_id
=self
.my_tenant_id
644 self
.security_groups_id
= []
645 for sg
in self
.config
.get("security_groups"):
646 for neutron_sg
in neutron_sg_list
:
647 if sg
in (neutron_sg
["id"], neutron_sg
["name"]):
648 self
.security_groups_id
.append(neutron_sg
["id"])
651 self
.security_groups_id
= None
653 raise vimconn
.VimConnConnectionException(
654 "Not found security group {} for this tenant".format(sg
)
657 def _find_nova_server(self
, vm_id
):
659 Returns the VM instance from Openstack and completes it with flavor ID
660 Do not call nova.servers.find directly, as it does not return flavor ID with microversion>=2.47
663 self
._reload
_connection
()
664 server
= self
.nova
.servers
.find(id=vm_id
)
665 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
666 server_dict
= server
.to_dict()
668 if server_dict
["flavor"].get("original_name"):
669 server_dict
["flavor"]["id"] = self
.nova
.flavors
.find(
670 name
=server_dict
["flavor"]["original_name"]
672 except nClient
.exceptions
.NotFound
as e
:
673 self
.logger
.warning(str(e
.message
))
676 ksExceptions
.ClientException
,
677 nvExceptions
.ClientException
,
678 nvExceptions
.NotFound
,
681 self
._format
_exception
(e
)
683 def check_vim_connectivity(self
):
684 # just get network list to check connectivity and credentials
685 self
.get_network_list(filter_dict
={})
687 def get_tenant_list(self
, filter_dict
={}):
688 """Obtain tenants of VIM
689 filter_dict can contain the following keys:
690 name: filter by tenant name
691 id: filter by tenant uuid/id
693 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
695 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
697 self
._reload
_connection
()
699 if self
.api_version3
:
700 project_class_list
= self
.keystone
.projects
.list(
701 name
=filter_dict
.get("name")
704 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
708 for project
in project_class_list
:
709 if filter_dict
.get("id") and filter_dict
["id"] != project
.id:
712 project_list
.append(project
.to_dict())
716 ksExceptions
.ConnectionError
,
717 ksExceptions
.ClientException
,
720 self
._format
_exception
(e
)
722 def new_tenant(self
, tenant_name
, tenant_description
):
723 """Adds a new tenant to openstack VIM. Returns the tenant identifier"""
724 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
726 self
._reload
_connection
()
728 if self
.api_version3
:
729 project
= self
.keystone
.projects
.create(
731 self
.config
.get("project_domain_id", "default"),
732 description
=tenant_description
,
736 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
740 ksExceptions
.ConnectionError
,
741 ksExceptions
.ClientException
,
742 ksExceptions
.BadRequest
,
745 self
._format
_exception
(e
)
747 def delete_tenant(self
, tenant_id
):
748 """Delete a tenant from openstack VIM. Returns the old tenant identifier"""
749 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
751 self
._reload
_connection
()
753 if self
.api_version3
:
754 self
.keystone
.projects
.delete(tenant_id
)
756 self
.keystone
.tenants
.delete(tenant_id
)
761 ksExceptions
.ConnectionError
,
762 ksExceptions
.ClientException
,
763 ksExceptions
.NotFound
,
766 self
._format
_exception
(e
)
774 provider_network_profile
=None,
776 """Adds a tenant network to VIM
778 'net_name': name of the network
780 'bridge': overlay isolated network
781 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
782 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
783 'ip_profile': is a dict containing the IP parameters of the network
784 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
785 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
786 'gateway_address': (Optional) ip_schema, that is X.X.X.X
787 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
788 'dhcp_enabled': True or False
789 'dhcp_start_address': ip_schema, first IP to grant
790 'dhcp_count': number of IPs to grant.
791 'shared': if this network can be seen/use by other tenants/organization
792 'provider_network_profile': (optional) contains {segmentation-id: vlan, network-type: vlan|vxlan,
793 physical-network: physnet-label}
794 Returns a tuple with the network identifier and created_items, or raises an exception on error
795 created_items can be None or a dictionary where this method can include key-values that will be passed to
796 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
797 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
801 "Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
803 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
808 if provider_network_profile
:
809 vlan
= provider_network_profile
.get("segmentation-id")
813 self
._reload
_connection
()
814 network_dict
= {"name": net_name
, "admin_state_up": True}
816 if net_type
in ("data", "ptp") or provider_network_profile
:
817 provider_physical_network
= None
819 if provider_network_profile
and provider_network_profile
.get(
822 provider_physical_network
= provider_network_profile
.get(
826 # provider-network must be one of the dataplane_physcial_netowrk if this is a list. If it is string
827 # or not declared, just ignore the checking
830 self
.config
.get("dataplane_physical_net"), (tuple, list)
832 and provider_physical_network
833 not in self
.config
["dataplane_physical_net"]
835 raise vimconn
.VimConnConflictException(
836 "Invalid parameter 'provider-network:physical-network' "
837 "for network creation. '{}' is not one of the declared "
838 "list at VIM_config:dataplane_physical_net".format(
839 provider_physical_network
843 # use the default dataplane_physical_net
844 if not provider_physical_network
:
845 provider_physical_network
= self
.config
.get(
846 "dataplane_physical_net"
849 # if it is non-empty list, use the first value. If it is a string use the value directly
851 isinstance(provider_physical_network
, (tuple, list))
852 and provider_physical_network
854 provider_physical_network
= provider_physical_network
[0]
856 if not provider_physical_network
:
857 raise vimconn
.VimConnConflictException(
858 "missing information needed for underlay networks. Provide "
859 "'dataplane_physical_net' configuration at VIM or use the NS "
860 "instantiation parameter 'provider-network.physical-network'"
864 if not self
.config
.get("multisegment_support"):
866 "provider:physical_network"
867 ] = provider_physical_network
870 provider_network_profile
871 and "network-type" in provider_network_profile
874 "provider:network_type"
875 ] = provider_network_profile
["network-type"]
877 network_dict
["provider:network_type"] = self
.config
.get(
878 "dataplane_network_type", "vlan"
882 network_dict
["provider:segmentation_id"] = vlan
887 "provider:physical_network": "",
888 "provider:network_type": "vxlan",
890 segment_list
.append(segment1_dict
)
892 "provider:physical_network": provider_physical_network
,
893 "provider:network_type": "vlan",
897 segment2_dict
["provider:segmentation_id"] = vlan
898 elif self
.config
.get("multisegment_vlan_range"):
899 vlanID
= self
._generate
_multisegment
_vlanID
()
900 segment2_dict
["provider:segmentation_id"] = vlanID
903 # raise vimconn.VimConnConflictException(
904 # "You must provide "multisegment_vlan_range" at config dict before creating a multisegment
906 segment_list
.append(segment2_dict
)
907 network_dict
["segments"] = segment_list
909 # VIO Specific Changes. It needs a concrete VLAN
910 if self
.vim_type
== "VIO" and vlan
is None:
911 if self
.config
.get("dataplane_net_vlan_range") is None:
912 raise vimconn
.VimConnConflictException(
913 "You must provide 'dataplane_net_vlan_range' in format "
914 "[start_ID - end_ID] at VIM_config for creating underlay "
918 network_dict
["provider:segmentation_id"] = self
._generate
_vlanID
()
920 network_dict
["shared"] = shared
922 if self
.config
.get("disable_network_port_security"):
923 network_dict
["port_security_enabled"] = False
925 if self
.config
.get("neutron_availability_zone_hints"):
926 hints
= self
.config
.get("neutron_availability_zone_hints")
928 if isinstance(hints
, str):
931 network_dict
["availability_zone_hints"] = hints
933 new_net
= self
.neutron
.create_network({"network": network_dict
})
935 # create subnetwork, even if there is no profile
940 if not ip_profile
.get("subnet_address"):
941 # Fake subnet is required
942 subnet_rand
= random
.SystemRandom().randint(0, 255)
943 ip_profile
["subnet_address"] = "192.168.{}.0/24".format(subnet_rand
)
945 if "ip_version" not in ip_profile
:
946 ip_profile
["ip_version"] = "IPv4"
949 "name": net_name
+ "-subnet",
950 "network_id": new_net
["network"]["id"],
951 "ip_version": 4 if ip_profile
["ip_version"] == "IPv4" else 6,
952 "cidr": ip_profile
["subnet_address"],
955 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
956 if ip_profile
.get("gateway_address"):
957 subnet
["gateway_ip"] = ip_profile
["gateway_address"]
959 subnet
["gateway_ip"] = None
961 if ip_profile
.get("dns_address"):
962 subnet
["dns_nameservers"] = ip_profile
["dns_address"].split(";")
964 if "dhcp_enabled" in ip_profile
:
965 subnet
["enable_dhcp"] = (
967 if ip_profile
["dhcp_enabled"] == "false"
968 or ip_profile
["dhcp_enabled"] is False
972 if ip_profile
.get("dhcp_start_address"):
973 subnet
["allocation_pools"] = []
974 subnet
["allocation_pools"].append(dict())
975 subnet
["allocation_pools"][0]["start"] = ip_profile
[
979 if ip_profile
.get("dhcp_count"):
980 # parts = ip_profile["dhcp_start_address"].split(".")
981 # ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
982 ip_int
= int(netaddr
.IPAddress(ip_profile
["dhcp_start_address"]))
983 ip_int
+= ip_profile
["dhcp_count"] - 1
984 ip_str
= str(netaddr
.IPAddress(ip_int
))
985 subnet
["allocation_pools"][0]["end"] = ip_str
988 ip_profile
.get("ipv6_address_mode")
989 and ip_profile
["ip_version"] != "IPv4"
991 subnet
["ipv6_address_mode"] = ip_profile
["ipv6_address_mode"]
992 # ipv6_ra_mode can be set to the same value for most use cases, see documentation:
993 # https://docs.openstack.org/neutron/latest/admin/config-ipv6.html#ipv6-ra-mode-and-ipv6-address-mode-combinations
994 subnet
["ipv6_ra_mode"] = ip_profile
["ipv6_address_mode"]
996 # self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
997 self
.neutron
.create_subnet({"subnet": subnet
})
999 if net_type
== "data" and self
.config
.get("multisegment_support"):
1000 if self
.config
.get("l2gw_support"):
1001 l2gw_list
= self
.neutron
.list_l2_gateways().get("l2_gateways", ())
1002 for l2gw
in l2gw_list
:
1004 "l2_gateway_id": l2gw
["id"],
1005 "network_id": new_net
["network"]["id"],
1006 "segmentation_id": str(vlanID
),
1008 new_l2gw_conn
= self
.neutron
.create_l2_gateway_connection(
1009 {"l2_gateway_connection": l2gw_conn
}
1013 + str(new_l2gw_conn
["l2_gateway_connection"]["id"])
1016 return new_net
["network"]["id"], created_items
1017 except Exception as e
:
1018 # delete l2gw connections (if any) before deleting the network
1019 for k
, v
in created_items
.items():
1020 if not v
: # skip already deleted
1024 k_item
, _
, k_id
= k
.partition(":")
1026 if k_item
== "l2gwconn":
1027 self
.neutron
.delete_l2_gateway_connection(k_id
)
1029 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e2
:
1031 "Error deleting l2 gateway connection: {}: {}".format(
1032 type(e2
).__name
__, e2
1035 self
._format
_exception
(e2
)
1036 except Exception as e2
:
1038 "Error deleting l2 gateway connection: {}: {}".format(
1039 type(e2
).__name
__, e2
1044 self
.neutron
.delete_network(new_net
["network"]["id"])
1046 self
._format
_exception
(e
)
1048 def get_network_list(self
, filter_dict
={}):
1049 """Obtain tenant networks of VIM
1055 admin_state_up: boolean
1057 Returns the network list of dictionaries
1059 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
1061 self
._reload
_connection
()
1062 filter_dict_os
= filter_dict
.copy()
1064 if self
.api_version3
and "tenant_id" in filter_dict_os
:
1066 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
1068 net_dict
= self
.neutron
.list_networks(**filter_dict_os
)
1069 net_list
= net_dict
["networks"]
1070 self
.__net
_os
2mano
(net_list
)
1074 neExceptions
.ConnectionFailed
,
1075 ksExceptions
.ClientException
,
1076 neExceptions
.NeutronException
,
1079 self
._format
_exception
(e
)
1081 def get_network(self
, net_id
):
1082 """Obtain details of network from VIM
1083 Returns the network information from a network id"""
1084 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
1085 filter_dict
= {"id": net_id
}
1086 net_list
= self
.get_network_list(filter_dict
)
1088 if len(net_list
) == 0:
1089 raise vimconn
.VimConnNotFoundException(
1090 "Network '{}' not found".format(net_id
)
1092 elif len(net_list
) > 1:
1093 raise vimconn
.VimConnConflictException(
1094 "Found more than one network with this criteria"
1099 for subnet_id
in net
.get("subnets", ()):
1101 subnet
= self
.neutron
.show_subnet(subnet_id
)
1102 except Exception as e
:
1104 "osconnector.get_network(): Error getting subnet %s %s"
1107 subnet
= {"id": subnet_id
, "fault": str(e
)}
1109 subnets
.append(subnet
)
1111 net
["subnets"] = subnets
1112 net
["encapsulation"] = net
.get("provider:network_type")
1113 net
["encapsulation_type"] = net
.get("provider:network_type")
1114 net
["segmentation_id"] = net
.get("provider:segmentation_id")
1115 net
["encapsulation_id"] = net
.get("provider:segmentation_id")
1119 @catch_any_exception
1120 def delete_network(self
, net_id
, created_items
=None):
1122 Removes a tenant network from VIM and its associated elements
1123 :param net_id: VIM identifier of the network, provided by method new_network
1124 :param created_items: dictionary with extra items to be deleted. provided by method new_network
1125 Returns the network identifier or raises an exception upon error or when network is not found
1127 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
1129 if created_items
is None:
1133 self
._reload
_connection
()
1134 # delete l2gw connections (if any) before deleting the network
1135 for k
, v
in created_items
.items():
1136 if not v
: # skip already deleted
1140 k_item
, _
, k_id
= k
.partition(":")
1141 if k_item
== "l2gwconn":
1142 self
.neutron
.delete_l2_gateway_connection(k_id
)
1144 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e
:
1146 "Error deleting l2 gateway connection: {}: {}".format(
1150 self
._format
_exception
(e
)
1151 except Exception as e
:
1153 "Error deleting l2 gateway connection: {}: {}".format(
1158 # delete VM ports attached to this networks before the network
1159 ports
= self
.neutron
.list_ports(network_id
=net_id
)
1160 for p
in ports
["ports"]:
1162 self
.neutron
.delete_port(p
["id"])
1164 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e
:
1165 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
1166 # If there is connection error, it raises.
1167 self
._format
_exception
(e
)
1168 except Exception as e
:
1169 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
1171 self
.neutron
.delete_network(net_id
)
1174 except (neExceptions
.NetworkNotFoundClient
, neExceptions
.NotFound
) as e
:
1175 # If network to be deleted is not found, it does not raise.
1176 self
.logger
.warning(
1177 f
"Error deleting network: {net_id} is not found, {str(e)}"
1180 def refresh_nets_status(self
, net_list
):
1181 """Get the status of the networks
1182 Params: the list of network identifiers
1183 Returns a dictionary with:
1184 net_id: #VIM id of this network
1185 status: #Mandatory. Text with one of:
1186 # DELETED (not found at vim)
1187 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1188 # OTHER (Vim reported other status not understood)
1189 # ERROR (VIM indicates an ERROR status)
1190 # ACTIVE, INACTIVE, DOWN (admin down),
1191 # BUILD (on building process)
1193 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1194 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1198 for net_id
in net_list
:
1202 net_vim
= self
.get_network(net_id
)
1204 if net_vim
["status"] in netStatus2manoFormat
:
1205 net
["status"] = netStatus2manoFormat
[net_vim
["status"]]
1207 net
["status"] = "OTHER"
1208 net
["error_msg"] = "VIM status reported " + net_vim
["status"]
1210 if net
["status"] == "ACTIVE" and not net_vim
["admin_state_up"]:
1211 net
["status"] = "DOWN"
1213 net
["vim_info"] = self
.serialize(net_vim
)
1215 if net_vim
.get("fault"): # TODO
1216 net
["error_msg"] = str(net_vim
["fault"])
1217 except vimconn
.VimConnNotFoundException
as e
:
1218 self
.logger
.error("Exception getting net status: %s", str(e
))
1219 net
["status"] = "DELETED"
1220 net
["error_msg"] = str(e
)
1221 except vimconn
.VimConnException
as e
:
1222 self
.logger
.error("Exception getting net status: %s", str(e
))
1223 net
["status"] = "VIM_ERROR"
1224 net
["error_msg"] = str(e
)
1225 net_dict
[net_id
] = net
1228 def get_flavor(self
, flavor_id
):
1229 """Obtain flavor details from the VIM. Returns the flavor dict details"""
1230 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
1232 self
._reload
_connection
()
1233 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
1234 return flavor
.to_dict()
1237 nvExceptions
.NotFound
,
1238 nvExceptions
.ClientException
,
1239 ksExceptions
.ClientException
,
1242 self
._format
_exception
(e
)
1244 def get_flavor_id_from_data(self
, flavor_dict
):
1245 """Obtain flavor id that match the flavor description
1246 Returns the flavor_id or raises a vimconnNotFoundException
1247 flavor_dict: contains the required ram, vcpus, disk
1248 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
1249 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
1250 vimconnNotFoundException is raised
1252 exact_match
= False if self
.config
.get("use_existing_flavors") else True
1255 self
._reload
_connection
()
1256 flavor_candidate_id
= None
1257 flavor_candidate_data
= (10000, 10000, 10000)
1260 flavor_dict
["vcpus"],
1261 flavor_dict
["disk"],
1262 flavor_dict
.get("ephemeral", 0),
1263 flavor_dict
.get("swap", 0),
1266 extended
= flavor_dict
.get("extended", {})
1269 raise vimconn
.VimConnNotFoundException(
1270 "Flavor with EPA still not implemented"
1272 # if len(numas) > 1:
1273 # raise vimconn.VimConnNotFoundException("Cannot find any flavor with more than one numa")
1275 # numas = extended.get("numas")
1276 for flavor
in self
.nova
.flavors
.list():
1277 epa
= flavor
.get_keys()
1288 flavor
.swap
if isinstance(flavor
.swap
, int) else 0,
1290 if flavor_data
== flavor_target
:
1294 and flavor_target
< flavor_data
< flavor_candidate_data
1296 flavor_candidate_id
= flavor
.id
1297 flavor_candidate_data
= flavor_data
1299 if not exact_match
and flavor_candidate_id
:
1300 return flavor_candidate_id
1302 raise vimconn
.VimConnNotFoundException(
1303 "Cannot find any flavor matching '{}'".format(flavor_dict
)
1306 nvExceptions
.NotFound
,
1307 nvExceptions
.BadRequest
,
1308 nvExceptions
.ClientException
,
1309 ksExceptions
.ClientException
,
1312 self
._format
_exception
(e
)
1315 def process_resource_quota(quota
: dict, prefix
: str, extra_specs
: dict) -> None:
1316 """Process resource quota and fill up extra_specs.
1318 quota (dict): Keeping the quota of resurces
1320 extra_specs (dict) Dict to be filled to be used during flavor creation
1323 if "limit" in quota
:
1324 extra_specs
["quota:" + prefix
+ "_limit"] = quota
["limit"]
1326 if "reserve" in quota
:
1327 extra_specs
["quota:" + prefix
+ "_reservation"] = quota
["reserve"]
1329 if "shares" in quota
:
1330 extra_specs
["quota:" + prefix
+ "_shares_level"] = "custom"
1331 extra_specs
["quota:" + prefix
+ "_shares_share"] = quota
["shares"]
1334 def process_numa_memory(
1335 numa
: dict, node_id
: Optional
[int], extra_specs
: dict
1337 """Set the memory in extra_specs.
1339 numa (dict): A dictionary which includes numa information
1340 node_id (int): ID of numa node
1341 extra_specs (dict): To be filled.
1344 if not numa
.get("memory"):
1346 memory_mb
= numa
["memory"] * 1024
1347 memory
= "hw:numa_mem.{}".format(node_id
)
1348 extra_specs
[memory
] = int(memory_mb
)
1351 def process_numa_vcpu(numa
: dict, node_id
: int, extra_specs
: dict) -> None:
1352 """Set the cpu in extra_specs.
1354 numa (dict): A dictionary which includes numa information
1355 node_id (int): ID of numa node
1356 extra_specs (dict): To be filled.
1359 if not numa
.get("vcpu"):
1362 cpu
= "hw:numa_cpus.{}".format(node_id
)
1363 vcpu
= ",".join(map(str, vcpu
))
1364 extra_specs
[cpu
] = vcpu
1367 def process_numa_paired_threads(numa
: dict, extra_specs
: dict) -> Optional
[int]:
1368 """Fill up extra_specs if numa has paired-threads.
1370 numa (dict): A dictionary which includes numa information
1371 extra_specs (dict): To be filled.
1374 threads (int) Number of virtual cpus
1377 if not numa
.get("paired-threads"):
1380 # cpu_thread_policy "require" implies that compute node must have an STM architecture
1381 threads
= numa
["paired-threads"] * 2
1382 extra_specs
["hw:cpu_thread_policy"] = "require"
1383 extra_specs
["hw:cpu_policy"] = "dedicated"
1387 def process_numa_cores(numa
: dict, extra_specs
: dict) -> Optional
[int]:
1388 """Fill up extra_specs if numa has cores.
1390 numa (dict): A dictionary which includes numa information
1391 extra_specs (dict): To be filled.
1394 cores (int) Number of virtual cpus
1397 # cpu_thread_policy "isolate" implies that the host must not have an SMT
1398 # architecture, or a non-SMT architecture will be emulated
1399 if not numa
.get("cores"):
1401 cores
= numa
["cores"]
1402 extra_specs
["hw:cpu_thread_policy"] = "isolate"
1403 extra_specs
["hw:cpu_policy"] = "dedicated"
1407 def process_numa_threads(numa
: dict, extra_specs
: dict) -> Optional
[int]:
1408 """Fill up extra_specs if numa has threads.
1410 numa (dict): A dictionary which includes numa information
1411 extra_specs (dict): To be filled.
1414 threads (int) Number of virtual cpus
1417 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT architecture
1418 if not numa
.get("threads"):
1420 threads
= numa
["threads"]
1421 extra_specs
["hw:cpu_thread_policy"] = "prefer"
1422 extra_specs
["hw:cpu_policy"] = "dedicated"
1425 def _process_numa_parameters_of_flavor(
1426 self
, numas
: List
, extra_specs
: Dict
1428 """Process numa parameters and fill up extra_specs.
1431 numas (list): List of dictionary which includes numa information
1432 extra_specs (dict): To be filled.
1435 numa_nodes
= len(numas
)
1436 extra_specs
["hw:numa_nodes"] = str(numa_nodes
)
1437 cpu_cores
, cpu_threads
= 0, 0
1439 if self
.vim_type
== "VIO":
1440 self
.process_vio_numa_nodes(numa_nodes
, extra_specs
)
1444 node_id
= numa
["id"]
1445 # overwrite ram and vcpus
1446 # check if key "memory" is present in numa else use ram value at flavor
1447 self
.process_numa_memory(numa
, node_id
, extra_specs
)
1448 self
.process_numa_vcpu(numa
, node_id
, extra_specs
)
1450 # See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/virt-driver-cpu-thread-pinning.html
1451 extra_specs
["hw:cpu_sockets"] = str(numa_nodes
)
1453 if "paired-threads" in numa
:
1454 threads
= self
.process_numa_paired_threads(numa
, extra_specs
)
1455 cpu_threads
+= threads
1457 elif "cores" in numa
:
1458 cores
= self
.process_numa_cores(numa
, extra_specs
)
1461 elif "threads" in numa
:
1462 threads
= self
.process_numa_threads(numa
, extra_specs
)
1463 cpu_threads
+= threads
1466 extra_specs
["hw:cpu_cores"] = str(cpu_cores
)
1468 extra_specs
["hw:cpu_threads"] = str(cpu_threads
)
1471 def process_vio_numa_nodes(numa_nodes
: int, extra_specs
: Dict
) -> None:
1472 """According to number of numa nodes, updates the extra_specs for VIO.
1476 numa_nodes (int): List keeps the numa node numbers
1477 extra_specs (dict): Extra specs dict to be updated
1480 # If there are several numas, we do not define specific affinity.
1481 extra_specs
["vmware:latency_sensitivity_level"] = "high"
1483 def _change_flavor_name(
1484 self
, name
: str, name_suffix
: int, flavor_data
: dict
1486 """Change the flavor name if the name already exists.
1489 name (str): Flavor name to be checked
1490 name_suffix (int): Suffix to be appended to name
1491 flavor_data (dict): Flavor dict
1494 name (str): New flavor name to be used
1498 fl
= self
.nova
.flavors
.list()
1499 fl_names
= [f
.name
for f
in fl
]
1501 while name
in fl_names
:
1503 name
= flavor_data
["name"] + "-" + str(name_suffix
)
1507 def _process_extended_config_of_flavor(
1508 self
, extended
: dict, extra_specs
: dict
1510 """Process the extended dict to fill up extra_specs.
1513 extended (dict): Keeping the extra specification of flavor
1514 extra_specs (dict) Dict to be filled to be used during flavor creation
1519 "mem-quota": "memory",
1521 "disk-io-quota": "disk_io",
1529 "PREFER_LARGE": "any",
1533 "cpu-pinning-policy": "hw:cpu_policy",
1534 "cpu-thread-pinning-policy": "hw:cpu_thread_policy",
1535 "mem-policy": "hw:numa_mempolicy",
1538 numas
= extended
.get("numas")
1540 self
._process
_numa
_parameters
_of
_flavor
(numas
, extra_specs
)
1542 for quota
, item
in quotas
.items():
1543 if quota
in extended
.keys():
1544 self
.process_resource_quota(extended
.get(quota
), item
, extra_specs
)
1546 # Set the mempage size as specified in the descriptor
1547 if extended
.get("mempage-size"):
1548 if extended
["mempage-size"] in page_sizes
.keys():
1549 extra_specs
["hw:mem_page_size"] = page_sizes
[extended
["mempage-size"]]
1551 # Normally, validations in NBI should not allow to this condition.
1553 "Invalid mempage-size %s. Will be ignored",
1554 extended
.get("mempage-size"),
1557 for policy
, hw_policy
in policies
.items():
1558 if extended
.get(policy
):
1559 extra_specs
[hw_policy
] = extended
[policy
].lower()
1562 def _get_flavor_details(flavor_data
: dict) -> Tuple
:
1563 """Returns the details of flavor
1565 flavor_data (dict): Dictionary that includes required flavor details
1568 ram, vcpus, extra_specs, extended (tuple): Main items of required flavor
1572 flavor_data
.get("ram", 64),
1573 flavor_data
.get("vcpus", 1),
1575 flavor_data
.get("extended"),
1578 @catch_any_exception
1579 def new_flavor(self
, flavor_data
: dict, change_name_if_used
: bool = True) -> str:
1580 """Adds a tenant flavor to openstack VIM.
1581 if change_name_if_used is True, it will change name in case of conflict,
1582 because it is not supported name repetition.
1585 flavor_data (dict): Flavor details to be processed
1586 change_name_if_used (bool): Change name in case of conflict
1589 flavor_id (str): flavor identifier
1592 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
1596 name
= flavor_data
["name"]
1597 while retry
< max_retries
:
1600 self
._reload
_connection
()
1602 if change_name_if_used
:
1603 name
= self
._change
_flavor
_name
(name
, name_suffix
, flavor_data
)
1605 ram
, vcpus
, extra_specs
, extended
= self
._get
_flavor
_details
(
1609 self
._process
_extended
_config
_of
_flavor
(extended
, extra_specs
)
1613 new_flavor
= self
.nova
.flavors
.create(
1617 disk
=flavor_data
.get("disk", 0),
1618 ephemeral
=flavor_data
.get("ephemeral", 0),
1619 swap
=flavor_data
.get("swap", 0),
1620 is_public
=flavor_data
.get("is_public", True),
1625 new_flavor
.set_keys(extra_specs
)
1627 return new_flavor
.id
1629 except nvExceptions
.Conflict
as e
:
1630 if change_name_if_used
and retry
< max_retries
:
1633 self
._format
_exception
(e
)
1635 @catch_any_exception
1636 def delete_flavor(self
, flavor_id
):
1637 """Deletes a tenant flavor from openstack VIM. Returns the old flavor_id"""
1639 self
._reload
_connection
()
1640 self
.nova
.flavors
.delete(flavor_id
)
1643 except (nvExceptions
.NotFound
, nvExceptions
.ResourceNotFound
) as e
:
1644 # If flavor is not found, it does not raise.
1645 self
.logger
.warning(
1646 f
"Error deleting flavor: {flavor_id} is not found, {str(e.message)}"
1649 def new_image(self
, image_dict
):
1651 Adds a tenant image to VIM. imge_dict is a dictionary with:
1653 disk_format: qcow2, vhd, vmdk, raw (by default), ...
1654 location: path or URI
1655 public: "yes" or "no"
1656 metadata: metadata of the image
1657 Returns the image_id
1662 while retry
< max_retries
:
1665 self
._reload
_connection
()
1667 # determine format http://docs.openstack.org/developer/glance/formats.html
1668 if "disk_format" in image_dict
:
1669 disk_format
= image_dict
["disk_format"]
1670 else: # autodiscover based on extension
1671 if image_dict
["location"].endswith(".qcow2"):
1672 disk_format
= "qcow2"
1673 elif image_dict
["location"].endswith(".vhd"):
1675 elif image_dict
["location"].endswith(".vmdk"):
1676 disk_format
= "vmdk"
1677 elif image_dict
["location"].endswith(".vdi"):
1679 elif image_dict
["location"].endswith(".iso"):
1681 elif image_dict
["location"].endswith(".aki"):
1683 elif image_dict
["location"].endswith(".ari"):
1685 elif image_dict
["location"].endswith(".ami"):
1691 "new_image: '%s' loading from '%s'",
1693 image_dict
["location"],
1695 if self
.vim_type
== "VIO":
1696 container_format
= "bare"
1697 if "container_format" in image_dict
:
1698 container_format
= image_dict
["container_format"]
1700 new_image
= self
.glance
.images
.create(
1701 name
=image_dict
["name"],
1702 container_format
=container_format
,
1703 disk_format
=disk_format
,
1706 new_image
= self
.glance
.images
.create(name
=image_dict
["name"])
1708 if image_dict
["location"].startswith("http"):
1709 # TODO there is not a method to direct download. It must be downloaded locally with requests
1710 raise vimconn
.VimConnNotImplemented("Cannot create image from URL")
1712 with
open(image_dict
["location"]) as fimage
:
1713 self
.glance
.images
.upload(new_image
.id, fimage
)
1714 # new_image = self.glancev1.images.create(name=image_dict["name"], is_public=
1715 # image_dict.get("public","yes")=="yes",
1716 # container_format="bare", data=fimage, disk_format=disk_format)
1718 metadata_to_load
= image_dict
.get("metadata")
1720 # TODO location is a reserved word for current openstack versions. fixed for VIO please check
1722 if self
.vim_type
== "VIO":
1723 metadata_to_load
["upload_location"] = image_dict
["location"]
1725 metadata_to_load
["location"] = image_dict
["location"]
1727 self
.glance
.images
.update(new_image
.id, **metadata_to_load
)
1732 gl1Exceptions
.HTTPException
,
1733 gl1Exceptions
.CommunicationError
,
1736 if retry
== max_retries
:
1739 self
._format
_exception
(e
)
1740 except IOError as e
: # can not open the file
1741 raise vimconn
.VimConnConnectionException(
1742 "{}: {} for {}".format(type(e
).__name
__, e
, image_dict
["location"]),
1743 http_code
=vimconn
.HTTP_Bad_Request
,
1745 except Exception as e
:
1746 self
._format
_exception
(e
)
1748 @catch_any_exception
1749 def delete_image(self
, image_id
):
1750 """Deletes a tenant image from openstack VIM. Returns the old id"""
1752 self
._reload
_connection
()
1753 self
.glance
.images
.delete(image_id
)
1756 except gl1Exceptions
.NotFound
as e
:
1757 # If image is not found, it does not raise.
1758 self
.logger
.warning(
1759 f
"Error deleting image: {image_id} is not found, {str(e)}"
1762 @catch_any_exception
1763 def get_image_id_from_path(self
, path
):
1764 """Get the image id from image path in the VIM database. Returns the image_id"""
1765 self
._reload
_connection
()
1766 images
= self
.glance
.images
.list()
1768 for image
in images
:
1769 if image
.metadata
.get("location") == path
:
1772 raise vimconn
.VimConnNotFoundException(
1773 "image with location '{}' not found".format(path
)
1776 def get_image_list(self
, filter_dict
={}):
1777 """Obtain tenant images from VIM
1781 checksum: image checksum
1782 Returns the image list of dictionaries:
1783 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1786 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
1788 self
._reload
_connection
()
1789 # filter_dict_os = filter_dict.copy()
1790 # First we filter by the available filter fields: name, id. The others are removed.
1791 image_list
= self
.glance
.images
.list()
1794 for image
in image_list
:
1796 if filter_dict
.get("name") and image
["name"] != filter_dict
["name"]:
1799 if filter_dict
.get("id") and image
["id"] != filter_dict
["id"]:
1803 filter_dict
.get("checksum")
1804 and image
["checksum"] != filter_dict
["checksum"]
1808 filtered_list
.append(image
.copy())
1809 except gl1Exceptions
.HTTPNotFound
:
1812 return filtered_list
1815 ksExceptions
.ClientException
,
1816 nvExceptions
.ClientException
,
1817 gl1Exceptions
.CommunicationError
,
1820 self
._format
_exception
(e
)
1822 def __wait_for_vm(self
, vm_id
, status
):
1823 """wait until vm is in the desired status and return True.
1824 If the VM gets in ERROR status, return false.
1825 If the timeout is reached generate an exception"""
1827 while elapsed_time
< server_timeout
:
1828 vm_status
= self
.nova
.servers
.get(vm_id
).status
1830 if vm_status
== status
:
1833 if vm_status
== "ERROR":
1839 # if we exceeded the timeout rollback
1840 if elapsed_time
>= server_timeout
:
1841 raise vimconn
.VimConnException(
1842 "Timeout waiting for instance " + vm_id
+ " to get " + status
,
1843 http_code
=vimconn
.HTTP_Request_Timeout
,
1846 def _get_openstack_availablity_zones(self
):
1848 Get from openstack availability zones available
1852 openstack_availability_zone
= self
.nova
.availability_zones
.list()
1853 openstack_availability_zone
= [
1855 for zone
in openstack_availability_zone
1856 if zone
.zoneName
!= "internal"
1859 return openstack_availability_zone
1863 def _set_availablity_zones(self
):
1865 Set vim availablity zone
1868 if "availability_zone" in self
.config
:
1869 vim_availability_zones
= self
.config
.get("availability_zone")
1871 if isinstance(vim_availability_zones
, str):
1872 self
.availability_zone
= [vim_availability_zones
]
1873 elif isinstance(vim_availability_zones
, list):
1874 self
.availability_zone
= vim_availability_zones
1876 self
.availability_zone
= self
._get
_openstack
_availablity
_zones
()
1877 if "storage_availability_zone" in self
.config
:
1878 self
.storage_availability_zone
= self
.config
.get(
1879 "storage_availability_zone"
1882 def _get_vm_availability_zone(
1883 self
, availability_zone_index
, availability_zone_list
1886 Return thge availability zone to be used by the created VM.
1887 :return: The VIM availability zone to be used or None
1889 if availability_zone_index
is None:
1890 if not self
.config
.get("availability_zone"):
1892 elif isinstance(self
.config
.get("availability_zone"), str):
1893 return self
.config
["availability_zone"]
1895 # TODO consider using a different parameter at config for default AV and AV list match
1896 return self
.config
["availability_zone"][0]
1898 vim_availability_zones
= self
.availability_zone
1899 # check if VIM offer enough availability zones describe in the VNFD
1900 if vim_availability_zones
and len(availability_zone_list
) <= len(
1901 vim_availability_zones
1903 # check if all the names of NFV AV match VIM AV names
1904 match_by_index
= False
1905 for av
in availability_zone_list
:
1906 if av
not in vim_availability_zones
:
1907 match_by_index
= True
1911 return vim_availability_zones
[availability_zone_index
]
1913 return availability_zone_list
[availability_zone_index
]
1915 raise vimconn
.VimConnConflictException(
1916 "No enough availability zones at VIM for this deployment"
1919 def _prepare_port_dict_security_groups(self
, net
: dict, port_dict
: dict) -> None:
1920 """Fill up the security_groups in the port_dict.
1923 net (dict): Network details
1924 port_dict (dict): Port details
1928 self
.config
.get("security_groups")
1929 and net
.get("port_security") is not False
1930 and not self
.config
.get("no_port_security_extension")
1932 if not self
.security_groups_id
:
1933 self
._get
_ids
_from
_name
()
1935 port_dict
["security_groups"] = self
.security_groups_id
1937 def _prepare_port_dict_binding(self
, net
: dict, port_dict
: dict) -> None:
1938 """Fill up the network binding depending on network type in the port_dict.
1941 net (dict): Network details
1942 port_dict (dict): Port details
1945 if not net
.get("type"):
1946 raise vimconn
.VimConnException("Type is missing in the network details.")
1948 if net
["type"] == "virtual":
1952 elif net
["type"] == "VF" or net
["type"] == "SR-IOV":
1953 port_dict
["binding:vnic_type"] = "direct"
1955 # VIO specific Changes
1956 if self
.vim_type
== "VIO":
1957 # Need to create port with port_security_enabled = False and no-security-groups
1958 port_dict
["port_security_enabled"] = False
1959 port_dict
["provider_security_groups"] = []
1960 port_dict
["security_groups"] = []
1963 # For PT PCI-PASSTHROUGH
1964 port_dict
["binding:vnic_type"] = "direct-physical"
1967 def _set_fixed_ip(new_port
: dict, net
: dict) -> None:
1968 """Set the "ip" parameter in net dictionary.
1971 new_port (dict): New created port
1972 net (dict): Network details
1975 fixed_ips
= new_port
["port"].get("fixed_ips")
1978 net
["ip"] = fixed_ips
[0].get("ip_address")
1983 def _prepare_port_dict_mac_ip_addr(net
: dict, port_dict
: dict) -> None:
1984 """Fill up the mac_address and fixed_ips in port_dict.
1987 net (dict): Network details
1988 port_dict (dict): Port details
1991 if net
.get("mac_address"):
1992 port_dict
["mac_address"] = net
["mac_address"]
1995 if ip_list
:= net
.get("ip_address"):
1996 if not isinstance(ip_list
, list):
1999 ip_dict
= {"ip_address": ip
}
2000 ip_dual_list
.append(ip_dict
)
2001 port_dict
["fixed_ips"] = ip_dual_list
2002 # TODO add "subnet_id": <subnet_id>
2004 def _create_new_port(self
, port_dict
: dict, created_items
: dict, net
: dict) -> Dict
:
2005 """Create new port using neutron.
2008 port_dict (dict): Port details
2009 created_items (dict): All created items
2010 net (dict): Network details
2013 new_port (dict): New created port
2016 new_port
= self
.neutron
.create_port({"port": port_dict
})
2017 created_items
["port:" + str(new_port
["port"]["id"])] = True
2018 net
["mac_address"] = new_port
["port"]["mac_address"]
2019 net
["vim_id"] = new_port
["port"]["id"]
2024 self
, net
: dict, name
: str, created_items
: dict
2025 ) -> Tuple
[dict, dict]:
2026 """Create port using net details.
2029 net (dict): Network details
2030 name (str): Name to be used as network name if net dict does not include name
2031 created_items (dict): All created items
2034 new_port, port New created port, port dictionary
2039 "network_id": net
["net_id"],
2040 "name": net
.get("name"),
2041 "admin_state_up": True,
2044 if not port_dict
["name"]:
2045 port_dict
["name"] = name
2047 self
._prepare
_port
_dict
_security
_groups
(net
, port_dict
)
2049 self
._prepare
_port
_dict
_binding
(net
, port_dict
)
2051 vimconnector
._prepare
_port
_dict
_mac
_ip
_addr
(net
, port_dict
)
2053 new_port
= self
._create
_new
_port
(port_dict
, created_items
, net
)
2055 vimconnector
._set
_fixed
_ip
(new_port
, net
)
2057 port
= {"port-id": new_port
["port"]["id"]}
2059 if float(self
.nova
.api_version
.get_string()) >= 2.32:
2060 port
["tag"] = new_port
["port"]["name"]
2062 return new_port
, port
2064 def _prepare_network_for_vminstance(
2068 created_items
: dict,
2070 external_network
: list,
2071 no_secured_ports
: list,
2073 """Create port and fill up net dictionary for new VM instance creation.
2076 name (str): Name of network
2077 net_list (list): List of networks
2078 created_items (dict): All created items belongs to a VM
2079 net_list_vim (list): List of ports
2080 external_network (list): List of external-networks
2081 no_secured_ports (list): Port security disabled ports
2084 self
._reload
_connection
()
2086 for net
in net_list
:
2087 # Skip non-connected iface
2088 if not net
.get("net_id"):
2091 new_port
, port
= self
._create
_port
(net
, name
, created_items
)
2093 net_list_vim
.append(port
)
2095 if net
.get("floating_ip", False):
2096 net
["exit_on_floating_ip_error"] = True
2097 external_network
.append(net
)
2099 elif net
["use"] == "mgmt" and self
.config
.get("use_floating_ip"):
2100 net
["exit_on_floating_ip_error"] = False
2101 external_network
.append(net
)
2102 net
["floating_ip"] = self
.config
.get("use_floating_ip")
2104 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic
2105 # is dropped. As a workaround we wait until the VM is active and then disable the port-security
2106 if net
.get("port_security") is False and not self
.config
.get(
2107 "no_port_security_extension"
2109 no_secured_ports
.append(
2111 new_port
["port"]["id"],
2112 net
.get("port_security_disable_strategy"),
2116 def _prepare_persistent_root_volumes(
2119 storage_av_zone
: list,
2121 base_disk_index
: int,
2122 block_device_mapping
: dict,
2123 existing_vim_volumes
: list,
2124 created_items
: dict,
2126 """Prepare persistent root volumes for new VM instance.
2129 name (str): Name of VM instance
2130 storage_av_zone (list): Storage of availability zones
2131 disk (dict): Disk details
2132 base_disk_index (int): Disk index
2133 block_device_mapping (dict): Block device details
2134 existing_vim_volumes (list): Existing disk details
2135 created_items (dict): All created items belongs to VM
2138 boot_volume_id (str): ID of boot volume
2141 # Disk may include only vim_volume_id or only vim_id."
2142 # Use existing persistent root volume finding with volume_id or vim_id
2143 key_id
= "vim_volume_id" if "vim_volume_id" in disk
.keys() else "vim_id"
2144 if disk
.get(key_id
):
2145 block_device_mapping
["vd" + chr(base_disk_index
)] = disk
[key_id
]
2146 existing_vim_volumes
.append({"id": disk
[key_id
]})
2148 # Create persistent root volume
2149 volume
= self
.cinder
.volumes
.create(
2151 name
=name
+ "vd" + chr(base_disk_index
),
2152 imageRef
=disk
["image_id"],
2153 # Make sure volume is in the same AZ as the VM to be attached to
2154 availability_zone
=storage_av_zone
,
2156 boot_volume_id
= volume
.id
2157 self
.update_block_device_mapping(
2159 block_device_mapping
=block_device_mapping
,
2160 base_disk_index
=base_disk_index
,
2162 created_items
=created_items
,
2165 return boot_volume_id
2168 def update_block_device_mapping(
2170 block_device_mapping
: dict,
2171 base_disk_index
: int,
2173 created_items
: dict,
2175 """Add volume information to block device mapping dict.
2177 volume (object): Created volume object
2178 block_device_mapping (dict): Block device details
2179 base_disk_index (int): Disk index
2180 disk (dict): Disk details
2181 created_items (dict): All created items belongs to VM
2184 raise vimconn
.VimConnException("Volume is empty.")
2186 if not hasattr(volume
, "id"):
2187 raise vimconn
.VimConnException(
2188 "Created volume is not valid, does not have id attribute."
2191 block_device_mapping
["vd" + chr(base_disk_index
)] = volume
.id
2192 if disk
.get("multiattach"): # multiattach volumes do not belong to VDUs
2194 volume_txt
= "volume:" + str(volume
.id)
2195 if disk
.get("keep"):
2196 volume_txt
+= ":keep"
2197 created_items
[volume_txt
] = True
2199 @catch_any_exception
2200 def new_shared_volumes(self
, shared_volume_data
) -> (str, str):
2201 availability_zone
= (
2202 self
.storage_availability_zone
2203 if self
.storage_availability_zone
2204 else self
._get
_vm
_availability
_zone
2206 volume
= self
.cinder
.volumes
.create(
2207 size
=shared_volume_data
["size"],
2208 name
=shared_volume_data
["name"],
2209 volume_type
="multiattach",
2210 availability_zone
=availability_zone
,
2212 return volume
.name
, volume
.id
2214 def _prepare_shared_volumes(
2218 base_disk_index
: int,
2219 block_device_mapping
: dict,
2220 existing_vim_volumes
: list,
2221 created_items
: dict,
2223 volumes
= {volume
.name
: volume
.id for volume
in self
.cinder
.volumes
.list()}
2224 if volumes
.get(disk
["name"]):
2225 sv_id
= volumes
[disk
["name"]]
2228 # If this is not the first VM to attach the volume, volume status may be "reserved" for a short time
2231 volume
= self
.cinder
.volumes
.get(sv_id
)
2232 vol_status
= volume
.status
2233 if volume
.status
not in ("in-use", "available"):
2236 self
.update_block_device_mapping(
2238 block_device_mapping
=block_device_mapping
,
2239 base_disk_index
=base_disk_index
,
2241 created_items
=created_items
,
2244 raise vimconn
.VimConnException(
2245 "Shared volume is not prepared, status is: {}".format(vol_status
),
2246 http_code
=vimconn
.HTTP_Internal_Server_Error
,
2249 def _prepare_non_root_persistent_volumes(
2253 storage_av_zone
: list,
2254 block_device_mapping
: dict,
2255 base_disk_index
: int,
2256 existing_vim_volumes
: list,
2257 created_items
: dict,
2259 """Prepare persistent volumes for new VM instance.
2262 name (str): Name of VM instance
2263 disk (dict): Disk details
2264 storage_av_zone (list): Storage of availability zones
2265 block_device_mapping (dict): Block device details
2266 base_disk_index (int): Disk index
2267 existing_vim_volumes (list): Existing disk details
2268 created_items (dict): All created items belongs to VM
2270 # Non-root persistent volumes
2271 # Disk may include only vim_volume_id or only vim_id."
2272 key_id
= "vim_volume_id" if "vim_volume_id" in disk
.keys() else "vim_id"
2273 if disk
.get(key_id
):
2274 # Use existing persistent volume
2275 block_device_mapping
["vd" + chr(base_disk_index
)] = disk
[key_id
]
2276 existing_vim_volumes
.append({"id": disk
[key_id
]})
2278 volume_name
= f
"{name}vd{chr(base_disk_index)}"
2279 volume
= self
.cinder
.volumes
.create(
2282 # Make sure volume is in the same AZ as the VM to be attached to
2283 availability_zone
=storage_av_zone
,
2285 self
.update_block_device_mapping(
2287 block_device_mapping
=block_device_mapping
,
2288 base_disk_index
=base_disk_index
,
2290 created_items
=created_items
,
2293 def _wait_for_created_volumes_availability(
2294 self
, elapsed_time
: int, created_items
: dict
2296 """Wait till created volumes become available.
2299 elapsed_time (int): Passed time while waiting
2300 created_items (dict): All created items belongs to VM
2303 elapsed_time (int): Time spent while waiting
2306 while elapsed_time
< volume_timeout
:
2307 for created_item
in created_items
:
2309 created_item
.split(":")[0],
2310 created_item
.split(":")[1],
2313 volume
= self
.cinder
.volumes
.get(volume_id
)
2315 volume
.volume_type
== "multiattach"
2316 and volume
.status
== "in-use"
2319 elif volume
.status
!= "available":
2322 # All ready: break from while
2330 def _wait_for_existing_volumes_availability(
2331 self
, elapsed_time
: int, existing_vim_volumes
: list
2333 """Wait till existing volumes become available.
2336 elapsed_time (int): Passed time while waiting
2337 existing_vim_volumes (list): Existing volume details
2340 elapsed_time (int): Time spent while waiting
2344 while elapsed_time
< volume_timeout
:
2345 for volume
in existing_vim_volumes
:
2346 v
= self
.cinder
.volumes
.get(volume
["id"])
2347 if v
.volume_type
== "multiattach" and v
.status
== "in-use":
2349 elif v
.status
!= "available":
2351 else: # all ready: break from while
2359 def _prepare_disk_for_vminstance(
2362 existing_vim_volumes
: list,
2363 created_items
: dict,
2364 storage_av_zone
: list,
2365 block_device_mapping
: dict,
2366 disk_list
: list = None,
2368 """Prepare all volumes for new VM instance.
2371 name (str): Name of Instance
2372 existing_vim_volumes (list): List of existing volumes
2373 created_items (dict): All created items belongs to VM
2374 storage_av_zone (list): Storage availability zone
2375 block_device_mapping (dict): Block devices to be attached to VM
2376 disk_list (list): List of disks
2379 # Create additional volumes in case these are present in disk_list
2380 base_disk_index
= ord("b")
2381 boot_volume_id
= None
2383 for disk
in disk_list
:
2384 if "image_id" in disk
:
2385 # Root persistent volume
2386 base_disk_index
= ord("a")
2387 boot_volume_id
= self
._prepare
_persistent
_root
_volumes
(
2389 storage_av_zone
=storage_av_zone
,
2391 base_disk_index
=base_disk_index
,
2392 block_device_mapping
=block_device_mapping
,
2393 existing_vim_volumes
=existing_vim_volumes
,
2394 created_items
=created_items
,
2396 elif disk
.get("multiattach"):
2397 self
._prepare
_shared
_volumes
(
2400 base_disk_index
=base_disk_index
,
2401 block_device_mapping
=block_device_mapping
,
2402 existing_vim_volumes
=existing_vim_volumes
,
2403 created_items
=created_items
,
2406 # Non-root persistent volume
2407 self
._prepare
_non
_root
_persistent
_volumes
(
2410 storage_av_zone
=storage_av_zone
,
2411 block_device_mapping
=block_device_mapping
,
2412 base_disk_index
=base_disk_index
,
2413 existing_vim_volumes
=existing_vim_volumes
,
2414 created_items
=created_items
,
2416 base_disk_index
+= 1
2418 # Wait until created volumes are with status available
2419 elapsed_time
= self
._wait
_for
_created
_volumes
_availability
(
2420 elapsed_time
, created_items
2422 # Wait until existing volumes in vim are with status available
2423 elapsed_time
= self
._wait
_for
_existing
_volumes
_availability
(
2424 elapsed_time
, existing_vim_volumes
2426 # If we exceeded the timeout rollback
2427 if elapsed_time
>= volume_timeout
:
2428 raise vimconn
.VimConnException(
2429 "Timeout creating volumes for instance " + name
,
2430 http_code
=vimconn
.HTTP_Request_Timeout
,
2433 self
.cinder
.volumes
.set_bootable(boot_volume_id
, True)
2435 def _find_the_external_network_for_floating_ip(self
):
2436 """Get the external network ip in order to create floating IP.
2439 pool_id (str): External network pool ID
2443 # Find the external network
2444 external_nets
= list()
2446 for net
in self
.neutron
.list_networks()["networks"]:
2447 if net
["router:external"]:
2448 external_nets
.append(net
)
2450 if len(external_nets
) == 0:
2451 raise vimconn
.VimConnException(
2452 "Cannot create floating_ip automatically since "
2453 "no external network is present",
2454 http_code
=vimconn
.HTTP_Conflict
,
2457 if len(external_nets
) > 1:
2458 raise vimconn
.VimConnException(
2459 "Cannot create floating_ip automatically since "
2460 "multiple external networks are present",
2461 http_code
=vimconn
.HTTP_Conflict
,
2465 return external_nets
[0].get("id")
2467 def _neutron_create_float_ip(self
, param
: dict, created_items
: dict) -> None:
2468 """Trigger neutron to create a new floating IP using external network ID.
2471 param (dict): Input parameters to create a floating IP
2472 created_items (dict): All created items belongs to new VM instance
2479 self
.logger
.debug("Creating floating IP")
2480 new_floating_ip
= self
.neutron
.create_floatingip(param
)
2481 free_floating_ip
= new_floating_ip
["floatingip"]["id"]
2482 created_items
["floating_ip:" + str(free_floating_ip
)] = True
2484 except Exception as e
:
2485 raise vimconn
.VimConnException(
2486 type(e
).__name
__ + ": Cannot create new floating_ip " + str(e
),
2487 http_code
=vimconn
.HTTP_Conflict
,
2490 def _create_floating_ip(
2491 self
, floating_network
: dict, server
: object, created_items
: dict
2493 """Get the available Pool ID and create a new floating IP.
2496 floating_network (dict): Dict including external network ID
2497 server (object): Server object
2498 created_items (dict): All created items belongs to new VM instance
2502 # Pool_id is available
2504 isinstance(floating_network
["floating_ip"], str)
2505 and floating_network
["floating_ip"].lower() != "true"
2507 pool_id
= floating_network
["floating_ip"]
2511 pool_id
= self
._find
_the
_external
_network
_for
_floating
_ip
()
2515 "floating_network_id": pool_id
,
2516 "tenant_id": server
.tenant_id
,
2520 self
._neutron
_create
_float
_ip
(param
, created_items
)
2522 def _find_floating_ip(
2526 floating_network
: dict,
2528 """Find the available free floating IPs if there are.
2531 server (object): Server object
2532 floating_ips (list): List of floating IPs
2533 floating_network (dict): Details of floating network such as ID
2536 free_floating_ip (str): Free floating ip address
2539 for fip
in floating_ips
:
2540 if fip
.get("port_id") or fip
.get("tenant_id") != server
.tenant_id
:
2543 if isinstance(floating_network
["floating_ip"], str):
2544 if fip
.get("floating_network_id") != floating_network
["floating_ip"]:
2549 def _assign_floating_ip(
2550 self
, free_floating_ip
: str, floating_network
: dict
2552 """Assign the free floating ip address to port.
2555 free_floating_ip (str): Floating IP to be assigned
2556 floating_network (dict): ID of floating network
2559 fip (dict) (dict): Floating ip details
2562 # The vim_id key contains the neutron.port_id
2563 self
.neutron
.update_floatingip(
2565 {"floatingip": {"port_id": floating_network
["vim_id"]}},
2567 # For race condition ensure not re-assigned to other VM after 5 seconds
2570 return self
.neutron
.show_floatingip(free_floating_ip
)
2572 def _get_free_floating_ip(
2573 self
, server
: object, floating_network
: dict
2575 """Get the free floating IP address.
2578 server (object): Server Object
2579 floating_network (dict): Floating network details
2582 free_floating_ip (str): Free floating ip addr
2586 floating_ips
= self
.neutron
.list_floatingips().get("floatingips", ())
2589 random
.shuffle(floating_ips
)
2591 return self
._find
_floating
_ip
(server
, floating_ips
, floating_network
)
2593 def _prepare_external_network_for_vminstance(
2595 external_network
: list,
2597 created_items
: dict,
2598 vm_start_time
: float,
2600 """Assign floating IP address for VM instance.
2603 external_network (list): ID of External network
2604 server (object): Server Object
2605 created_items (dict): All created items belongs to new VM instance
2606 vm_start_time (float): Time as a floating point number expressed in seconds since the epoch, in UTC
2612 for floating_network
in external_network
:
2615 floating_ip_retries
= 3
2616 # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry
2619 free_floating_ip
= self
._get
_free
_floating
_ip
(
2620 server
, floating_network
2623 if not free_floating_ip
:
2624 self
._create
_floating
_ip
(
2625 floating_network
, server
, created_items
2629 # For race condition ensure not already assigned
2630 fip
= self
.neutron
.show_floatingip(free_floating_ip
)
2632 if fip
["floatingip"].get("port_id"):
2635 # Assign floating ip
2636 fip
= self
._assign
_floating
_ip
(
2637 free_floating_ip
, floating_network
2640 if fip
["floatingip"]["port_id"] != floating_network
["vim_id"]:
2641 self
.logger
.warning(
2642 "floating_ip {} re-assigned to other port".format(
2649 "Assigned floating_ip {} to VM {}".format(
2650 free_floating_ip
, server
.id
2656 except Exception as e
:
2657 # Openstack need some time after VM creation to assign an IP. So retry if fails
2658 vm_status
= self
.nova
.servers
.get(server
.id).status
2660 if vm_status
not in ("ACTIVE", "ERROR"):
2661 if time
.time() - vm_start_time
< server_timeout
:
2664 elif floating_ip_retries
> 0:
2665 floating_ip_retries
-= 1
2668 raise vimconn
.VimConnException(
2669 "Cannot create floating_ip: {} {}".format(
2672 http_code
=vimconn
.HTTP_Conflict
,
2675 except Exception as e
:
2676 if not floating_network
["exit_on_floating_ip_error"]:
2677 self
.logger
.error("Cannot create floating_ip. %s", str(e
))
2682 def _update_port_security_for_vminstance(
2684 no_secured_ports
: list,
2687 """Updates the port security according to no_secured_ports list.
2690 no_secured_ports (list): List of ports that security will be disabled
2691 server (object): Server Object
2697 # Wait until the VM is active and then disable the port-security
2698 if no_secured_ports
:
2699 self
.__wait
_for
_vm
(server
.id, "ACTIVE")
2701 for port
in no_secured_ports
:
2703 "port": {"port_security_enabled": False, "security_groups": None}
2706 if port
[1] == "allow-address-pairs":
2708 "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}
2712 self
.neutron
.update_port(port
[0], port_update
)
2715 raise vimconn
.VimConnException(
2716 "It was not possible to disable port security for port {}".format(
2728 affinity_group_list
: list,
2732 availability_zone_index
=None,
2733 availability_zone_list
=None,
2735 """Adds a VM instance to VIM.
2738 name (str): name of VM
2739 description (str): description
2740 start (bool): indicates if VM must start or boot in pause mode. Ignored
2741 image_id (str) image uuid
2742 flavor_id (str) flavor uuid
2743 affinity_group_list (list): list of affinity groups, each one is a dictionary.Ignore if empty.
2744 net_list (list): list of interfaces, each one is a dictionary with:
2745 name: name of network
2746 net_id: network uuid to connect
2747 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
2748 model: interface model, ignored #TODO
2749 mac_address: used for SR-IOV ifaces #TODO for other types
2750 use: 'data', 'bridge', 'mgmt'
2751 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
2752 vim_id: filled/added by this function
2753 floating_ip: True/False (or it can be None)
2754 port_security: True/False
2755 cloud_config (dict): (optional) dictionary with:
2756 key-pairs: (optional) list of strings with the public key to be inserted to the default user
2757 users: (optional) list of users to be inserted, each item is a dict with:
2758 name: (mandatory) user name,
2759 key-pairs: (optional) list of strings with the public key to be inserted to the user
2760 user-data: (optional) string is a text script to be passed directly to cloud-init
2761 config-files: (optional). List of files to be transferred. Each item is a dict with:
2762 dest: (mandatory) string with the destination absolute path
2763 encoding: (optional, by default text). Can be one of:
2764 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
2765 content : (mandatory) string with the content of the file
2766 permissions: (optional) string with file permissions, typically octal notation '0644'
2767 owner: (optional) file owner, string with the format 'owner:group'
2768 boot-data-drive: boolean to indicate if user-data must be passed using a boot drive (hard disk)
2769 disk_list: (optional) list with additional disks to the VM. Each item is a dict with:
2770 image_id: (optional). VIM id of an existing image. If not provided an empty disk must be mounted
2771 size: (mandatory) string with the size of the disk in GB
2772 vim_id: (optional) should use this existing volume id
2773 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
2774 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
2775 availability_zone_index is None
2776 #TODO ip, security groups
2779 A tuple with the instance identifier and created_items or raises an exception on error
2780 created_items can be None or a dictionary where this method can include key-values that will be passed to
2781 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
2782 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
2787 "new_vminstance input: image='%s' flavor='%s' nics='%s'",
2795 # list of external networks to be connected to instance, later on used to create floating_ip
2796 external_network
= []
2797 # List of ports with port-security disabled
2798 no_secured_ports
= []
2799 block_device_mapping
= {}
2800 existing_vim_volumes
= []
2801 server_group_id
= None
2802 scheduller_hints
= {}
2805 # Check the Openstack Connection
2806 self
._reload
_connection
()
2808 # Prepare network list
2809 self
._prepare
_network
_for
_vminstance
(
2812 created_items
=created_items
,
2813 net_list_vim
=net_list_vim
,
2814 external_network
=external_network
,
2815 no_secured_ports
=no_secured_ports
,
2819 config_drive
, userdata
= self
._create
_user
_data
(cloud_config
)
2821 # Get availability Zone
2822 vm_av_zone
= self
._get
_vm
_availability
_zone
(
2823 availability_zone_index
, availability_zone_list
2827 self
.storage_availability_zone
2828 if self
.storage_availability_zone
2834 self
._prepare
_disk
_for
_vminstance
(
2836 existing_vim_volumes
=existing_vim_volumes
,
2837 created_items
=created_items
,
2838 storage_av_zone
=storage_av_zone
,
2839 block_device_mapping
=block_device_mapping
,
2840 disk_list
=disk_list
,
2843 if affinity_group_list
:
2844 # Only first id on the list will be used. Openstack restriction
2845 server_group_id
= affinity_group_list
[0]["affinity_group_id"]
2846 scheduller_hints
["group"] = server_group_id
2849 "nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
2850 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
2851 "block_device_mapping={}, server_group={})".format(
2856 self
.config
.get("security_groups"),
2858 self
.config
.get("keypair"),
2861 block_device_mapping
,
2866 server
= self
.nova
.servers
.create(
2871 security_groups
=self
.config
.get("security_groups"),
2872 # TODO remove security_groups in future versions. Already at neutron port
2873 availability_zone
=vm_av_zone
,
2874 key_name
=self
.config
.get("keypair"),
2876 config_drive
=config_drive
,
2877 block_device_mapping
=block_device_mapping
,
2878 scheduler_hints
=scheduller_hints
,
2881 vm_start_time
= time
.time()
2883 self
._update
_port
_security
_for
_vminstance
(no_secured_ports
, server
)
2885 self
._prepare
_external
_network
_for
_vminstance
(
2886 external_network
=external_network
,
2888 created_items
=created_items
,
2889 vm_start_time
=vm_start_time
,
2892 return server
.id, created_items
2894 except Exception as e
:
2897 server_id
= server
.id
2900 created_items
= self
.remove_keep_tag_from_persistent_volumes(
2904 self
.delete_vminstance(server_id
, created_items
)
2906 except Exception as e2
:
2907 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
2909 self
._format
_exception
(e
)
2912 def remove_keep_tag_from_persistent_volumes(created_items
: Dict
) -> Dict
:
2913 """Removes the keep flag from persistent volumes. So, those volumes could be removed.
2916 created_items (dict): All created items belongs to VM
2919 updated_created_items (dict): Dict which does not include keep flag for volumes.
2923 key
.replace(":keep", ""): value
for (key
, value
) in created_items
.items()
2926 def get_vminstance(self
, vm_id
):
2927 """Returns the VM instance information from VIM"""
2928 return self
._find
_nova
_server
(vm_id
)
2930 @catch_any_exception
2931 def get_vminstance_console(self
, vm_id
, console_type
="vnc"):
2933 Get a console for the virtual machine
2935 vm_id: uuid of the VM
2936 console_type, can be:
2937 "novnc" (by default), "xvpvnc" for VNC types,
2938 "rdp-html5" for RDP types, "spice-html5" for SPICE types
2939 Returns dict with the console parameters:
2940 protocol: ssh, ftp, http, https, ...
2941 server: usually ip address
2942 port: the http, ssh, ... port
2943 suffix: extra text, e.g. the http path and query string
2945 self
.logger
.debug("Getting VM CONSOLE from VIM")
2946 self
._reload
_connection
()
2947 server
= self
.nova
.servers
.find(id=vm_id
)
2949 if console_type
is None or console_type
== "novnc":
2950 console_dict
= server
.get_vnc_console("novnc")
2951 elif console_type
== "xvpvnc":
2952 console_dict
= server
.get_vnc_console(console_type
)
2953 elif console_type
== "rdp-html5":
2954 console_dict
= server
.get_rdp_console(console_type
)
2955 elif console_type
== "spice-html5":
2956 console_dict
= server
.get_spice_console(console_type
)
2958 raise vimconn
.VimConnException(
2959 "console type '{}' not allowed".format(console_type
),
2960 http_code
=vimconn
.HTTP_Bad_Request
,
2963 console_dict1
= console_dict
.get("console")
2966 console_url
= console_dict1
.get("url")
2970 protocol_index
= console_url
.find("//")
2972 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
2975 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
2980 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
2982 -vimconn
.HTTP_Internal_Server_Error
,
2983 "Unexpected response from VIM",
2987 "protocol": console_url
[0:protocol_index
],
2988 "server": console_url
[protocol_index
+ 2 : port_index
],
2989 "port": console_url
[port_index
:suffix_index
],
2990 "suffix": console_url
[suffix_index
+ 1 :],
2995 raise vimconn
.VimConnUnexpectedResponse("Unexpected response from VIM")
2997 def _delete_ports_by_id_wth_neutron(self
, k_id
: str) -> None:
2998 """Neutron delete ports by id.
3000 k_id (str): Port id in the VIM
3003 self
.neutron
.delete_port(k_id
)
3005 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e
:
3006 self
.logger
.error("Error deleting port: {}: {}".format(type(e
).__name
__, e
))
3007 # If there is connection error, raise.
3008 self
._format
_exception
(e
)
3009 except Exception as e
:
3010 self
.logger
.error("Error deleting port: {}: {}".format(type(e
).__name
__, e
))
3012 def delete_shared_volumes(self
, shared_volume_vim_id
: str) -> bool:
3013 """Cinder delete volume by id.
3015 shared_volume_vim_id (str): ID of shared volume in VIM
3019 while elapsed_time
< server_timeout
:
3020 vol_status
= self
.cinder
.volumes
.get(shared_volume_vim_id
).status
3021 if vol_status
== "available":
3022 self
.cinder
.volumes
.delete(shared_volume_vim_id
)
3028 if elapsed_time
>= server_timeout
:
3029 raise vimconn
.VimConnException(
3030 "Timeout waiting for volume "
3031 + shared_volume_vim_id
3032 + " to be available",
3033 http_code
=vimconn
.HTTP_Request_Timeout
,
3036 except Exception as e
:
3038 "Error deleting volume: {}: {}".format(type(e
).__name
__, e
)
3040 self
._format
_exception
(e
)
3042 def _delete_volumes_by_id_wth_cinder(
3043 self
, k
: str, k_id
: str, volumes_to_hold
: list, created_items
: dict
3045 """Cinder delete volume by id.
3047 k (str): Full item name in created_items
3048 k_id (str): ID of floating ip in VIM
3049 volumes_to_hold (list): Volumes not to delete
3050 created_items (dict): All created items belongs to VM
3053 if k_id
in volumes_to_hold
:
3056 if self
.cinder
.volumes
.get(k_id
).status
!= "available":
3060 self
.cinder
.volumes
.delete(k_id
)
3061 created_items
[k
] = None
3063 except (cExceptions
.ConnectionError
, ConnectionError
) as e
:
3065 "Error deleting volume: {}: {}".format(type(e
).__name
__, e
)
3067 self
._format
_exception
(e
)
3068 except Exception as e
:
3070 "Error deleting volume: {}: {}".format(type(e
).__name
__, e
)
3073 def _delete_floating_ip_by_id(self
, k
: str, k_id
: str, created_items
: dict) -> None:
3074 """Neutron delete floating ip by id.
3076 k (str): Full item name in created_items
3077 k_id (str): ID of floating ip in VIM
3078 created_items (dict): All created items belongs to VM
3081 self
.neutron
.delete_floatingip(k_id
)
3082 created_items
[k
] = None
3084 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e
:
3086 "Error deleting floating ip: {}: {}".format(type(e
).__name
__, e
)
3088 self
._format
_exception
(e
)
3089 except Exception as e
:
3091 "Error deleting floating ip: {}: {}".format(type(e
).__name
__, e
)
3095 def _get_item_name_id(k
: str) -> Tuple
[str, str]:
3096 k_item
, _
, k_id
= k
.partition(":")
3099 def _delete_vm_ports_attached_to_network(self
, created_items
: dict) -> None:
3100 """Delete VM ports attached to the networks before deleting virtual machine.
3102 created_items (dict): All created items belongs to VM
3105 for k
, v
in created_items
.items():
3106 if not v
: # skip already deleted
3110 k_item
, k_id
= self
._get
_item
_name
_id
(k
)
3111 if k_item
== "port":
3112 self
._delete
_ports
_by
_id
_wth
_neutron
(k_id
)
3114 except (neExceptions
.ConnectionFailed
, ConnectionError
) as e
:
3116 "Error deleting port: {}: {}".format(type(e
).__name
__, e
)
3118 self
._format
_exception
(e
)
3119 except Exception as e
:
3121 "Error deleting port: {}: {}".format(type(e
).__name
__, e
)
3124 def _delete_created_items(
3125 self
, created_items
: dict, volumes_to_hold
: list, keep_waiting
: bool
3127 """Delete Volumes and floating ip if they exist in created_items."""
3128 for k
, v
in created_items
.items():
3129 if not v
: # skip already deleted
3133 k_item
, k_id
= self
._get
_item
_name
_id
(k
)
3134 if k_item
== "volume":
3135 unavailable_vol
= self
._delete
_volumes
_by
_id
_wth
_cinder
(
3136 k
, k_id
, volumes_to_hold
, created_items
3142 elif k_item
== "floating_ip":
3143 self
._delete
_floating
_ip
_by
_id
(k
, k_id
, created_items
)
3146 cExceptions
.ConnectionError
,
3147 neExceptions
.ConnectionFailed
,
3152 self
.logger
.error("Error deleting {}: {}".format(k
, e
))
3153 self
._format
_exception
(e
)
3155 except Exception as e
:
3156 self
.logger
.error("Error deleting {}: {}".format(k
, e
))
3161 def _extract_items_wth_keep_flag_from_created_items(created_items
: dict) -> dict:
3162 """Remove the volumes which has key flag from created_items
3165 created_items (dict): All created items belongs to VM
3168 created_items (dict): Persistent volumes eliminated created_items
3172 for (key
, value
) in created_items
.items()
3173 if len(key
.split(":")) == 2
3176 @catch_any_exception
3177 def delete_vminstance(
3178 self
, vm_id
: str, created_items
: dict = None, volumes_to_hold
: list = None
3180 """Removes a VM instance from VIM. Returns the old identifier.
3182 vm_id (str): Identifier of VM instance
3183 created_items (dict): All created items belongs to VM
3184 volumes_to_hold (list): Volumes_to_hold
3186 if created_items
is None:
3188 if volumes_to_hold
is None:
3189 volumes_to_hold
= []
3192 created_items
= self
._extract
_items
_wth
_keep
_flag
_from
_created
_items
(
3196 self
._reload
_connection
()
3198 # Delete VM ports attached to the networks before the virtual machine
3200 self
._delete
_vm
_ports
_attached
_to
_network
(created_items
)
3203 self
.nova
.servers
.delete(vm_id
)
3205 # Although having detached, volumes should have in active status before deleting.
3206 # We ensure in this loop
3210 while keep_waiting
and elapsed_time
< volume_timeout
:
3211 keep_waiting
= False
3213 # Delete volumes and floating IP.
3214 keep_waiting
= self
._delete
_created
_items
(
3215 created_items
, volumes_to_hold
, keep_waiting
3221 except (nvExceptions
.NotFound
, nvExceptions
.ResourceNotFound
) as e
:
3222 # If VM does not exist, it does not raise
3223 self
.logger
.warning(f
"Error deleting VM: {vm_id} is not found, {str(e)}")
3225 def refresh_vms_status(self
, vm_list
):
3226 """Get the status of the virtual machines and their interfaces/ports
3227 Params: the list of VM identifiers
3228 Returns a dictionary with:
3229 vm_id: #VIM id of this Virtual Machine
3230 status: #Mandatory. Text with one of:
3231 # DELETED (not found at vim)
3232 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3233 # OTHER (Vim reported other status not understood)
3234 # ERROR (VIM indicates an ERROR status)
3235 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
3236 # CREATING (on building process), ERROR
3237 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
3239 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3240 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3242 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3243 mac_address: #Text format XX:XX:XX:XX:XX:XX
3244 vim_net_id: #network id where this interface is connected
3245 vim_interface_id: #interface/port VIM id
3246 ip_address: #null, or text with IPv4, IPv6 address
3247 compute_node: #identification of compute node where PF,VF interface is allocated
3248 pci: #PCI address of the NIC that hosts the PF,VF
3249 vlan: #physical VLAN used for VF
3253 "refresh_vms status: Getting tenant VM instance information from VIM"
3255 for vm_id
in vm_list
:
3259 vm_vim
= self
.get_vminstance(vm_id
)
3261 if vm_vim
["status"] in vmStatus2manoFormat
:
3262 vm
["status"] = vmStatus2manoFormat
[vm_vim
["status"]]
3264 vm
["status"] = "OTHER"
3265 vm
["error_msg"] = "VIM status reported " + vm_vim
["status"]
3267 vm_vim
.pop("OS-EXT-SRV-ATTR:user_data", None)
3268 vm_vim
.pop("user_data", None)
3269 vm
["vim_info"] = self
.serialize(vm_vim
)
3271 vm
["interfaces"] = []
3272 if vm_vim
.get("fault"):
3273 vm
["error_msg"] = str(vm_vim
["fault"])
3277 self
._reload
_connection
()
3278 port_dict
= self
.neutron
.list_ports(device_id
=vm_id
)
3280 for port
in port_dict
["ports"]:
3282 interface
["vim_info"] = self
.serialize(port
)
3283 interface
["mac_address"] = port
.get("mac_address")
3284 interface
["vim_net_id"] = port
["network_id"]
3285 interface
["vim_interface_id"] = port
["id"]
3286 # check if OS-EXT-SRV-ATTR:host is there,
3287 # in case of non-admin credentials, it will be missing
3289 if vm_vim
.get("OS-EXT-SRV-ATTR:host"):
3290 interface
["compute_node"] = vm_vim
["OS-EXT-SRV-ATTR:host"]
3292 interface
["pci"] = None
3294 # check if binding:profile is there,
3295 # in case of non-admin credentials, it will be missing
3296 if port
.get("binding:profile"):
3297 if port
["binding:profile"].get("pci_slot"):
3298 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting
3300 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
3301 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
3302 pci
= port
["binding:profile"]["pci_slot"]
3303 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
3304 interface
["pci"] = pci
3306 interface
["vlan"] = None
3308 if port
.get("binding:vif_details"):
3309 interface
["vlan"] = port
["binding:vif_details"].get("vlan")
3311 # Get vlan from network in case not present in port for those old openstacks and cases where
3312 # it is needed vlan at PT
3313 if not interface
["vlan"]:
3314 # if network is of type vlan and port is of type direct (sr-iov) then set vlan id
3315 network
= self
.neutron
.show_network(port
["network_id"])
3318 network
["network"].get("provider:network_type")
3321 # and port.get("binding:vnic_type") in ("direct", "direct-physical"):
3322 interface
["vlan"] = network
["network"].get(
3323 "provider:segmentation_id"
3327 # look for floating ip address
3329 floating_ip_dict
= self
.neutron
.list_floatingips(
3333 if floating_ip_dict
.get("floatingips"):
3335 floating_ip_dict
["floatingips"][0].get(
3336 "floating_ip_address"
3342 for subnet
in port
["fixed_ips"]:
3343 ips
.append(subnet
["ip_address"])
3345 interface
["ip_address"] = ";".join(ips
)
3346 vm
["interfaces"].append(interface
)
3347 except Exception as e
:
3349 "Error getting vm interface information {}: {}".format(
3354 except vimconn
.VimConnNotFoundException
as e
:
3355 self
.logger
.error("Exception getting vm status: %s", str(e
))
3356 vm
["status"] = "DELETED"
3357 vm
["error_msg"] = str(e
)
3358 except vimconn
.VimConnException
as e
:
3359 self
.logger
.error("Exception getting vm status: %s", str(e
))
3360 vm
["status"] = "VIM_ERROR"
3361 vm
["error_msg"] = str(e
)
3367 @catch_any_exception
3368 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
3369 """Send and action over a VM instance from VIM
3370 Returns None or the console dict if the action was successfully sent to the VIM
3372 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
3373 self
._reload
_connection
()
3374 server
= self
.nova
.servers
.find(id=vm_id
)
3375 if "start" in action_dict
:
3376 if action_dict
["start"] == "rebuild":
3379 if server
.status
== "PAUSED":
3381 elif server
.status
== "SUSPENDED":
3383 elif server
.status
== "SHUTOFF":
3387 "ERROR : Instance is not in SHUTOFF/PAUSE/SUSPEND state"
3389 raise vimconn
.VimConnException(
3390 "Cannot 'start' instance while it is in active state",
3391 http_code
=vimconn
.HTTP_Bad_Request
,
3393 elif "pause" in action_dict
:
3395 elif "resume" in action_dict
:
3397 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
3398 self
.logger
.debug("server status %s", server
.status
)
3399 if server
.status
== "ACTIVE":
3402 self
.logger
.debug("ERROR: VM is not in Active state")
3403 raise vimconn
.VimConnException(
3404 "VM is not in active state, stop operation is not allowed",
3405 http_code
=vimconn
.HTTP_Bad_Request
,
3407 elif "forceOff" in action_dict
:
3408 server
.stop() # TODO
3409 elif "terminate" in action_dict
:
3411 elif "createImage" in action_dict
:
3412 server
.create_image()
3413 # "path":path_schema,
3414 # "description":description_schema,
3415 # "name":name_schema,
3416 # "metadata":metadata_schema,
3417 # "imageRef": id_schema,
3418 # "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
3419 elif "rebuild" in action_dict
:
3420 server
.rebuild(server
.image
["id"])
3421 elif "reboot" in action_dict
:
3422 server
.reboot() # reboot_type="SOFT"
3423 elif "console" in action_dict
:
3424 console_type
= action_dict
["console"]
3426 if console_type
is None or console_type
== "novnc":
3427 console_dict
= server
.get_vnc_console("novnc")
3428 elif console_type
== "xvpvnc":
3429 console_dict
= server
.get_vnc_console(console_type
)
3430 elif console_type
== "rdp-html5":
3431 console_dict
= server
.get_rdp_console(console_type
)
3432 elif console_type
== "spice-html5":
3433 console_dict
= server
.get_spice_console(console_type
)
3435 raise vimconn
.VimConnException(
3436 "console type '{}' not allowed".format(console_type
),
3437 http_code
=vimconn
.HTTP_Bad_Request
,
3441 console_url
= console_dict
["console"]["url"]
3443 protocol_index
= console_url
.find("//")
3445 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
3448 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
3453 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
3454 raise vimconn
.VimConnException(
3455 "Unexpected response from VIM " + str(console_dict
)
3459 "protocol": console_url
[0:protocol_index
],
3460 "server": console_url
[protocol_index
+ 2 : port_index
],
3461 "port": int(console_url
[port_index
+ 1 : suffix_index
]),
3462 "suffix": console_url
[suffix_index
+ 1 :],
3465 return console_dict2
3467 raise vimconn
.VimConnException(
3468 "Unexpected response from VIM " + str(console_dict
)
3473 # ###### VIO Specific Changes #########
3474 def _generate_vlanID(self
):
3476 Method to get unused vlanID
3484 networks
= self
.get_network_list()
3486 for net
in networks
:
3487 if net
.get("provider:segmentation_id"):
3488 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
3490 used_vlanIDs
= set(usedVlanIDs
)
3492 # find unused VLAN ID
3493 for vlanID_range
in self
.config
.get("dataplane_net_vlan_range"):
3495 start_vlanid
, end_vlanid
= map(
3496 int, vlanID_range
.replace(" ", "").split("-")
3499 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
3500 if vlanID
not in used_vlanIDs
:
3502 except Exception as exp
:
3503 raise vimconn
.VimConnException(
3504 "Exception {} occurred while generating VLAN ID.".format(exp
)
3507 raise vimconn
.VimConnConflictException(
3508 "Unable to create the SRIOV VLAN network. All given Vlan IDs {} are in use.".format(
3509 self
.config
.get("dataplane_net_vlan_range")
3513 def _generate_multisegment_vlanID(self
):
3515 Method to get unused vlanID
3523 networks
= self
.get_network_list()
3524 for net
in networks
:
3525 if net
.get("provider:network_type") == "vlan" and net
.get(
3526 "provider:segmentation_id"
3528 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
3529 elif net
.get("segments"):
3530 for segment
in net
.get("segments"):
3531 if segment
.get("provider:network_type") == "vlan" and segment
.get(
3532 "provider:segmentation_id"
3534 usedVlanIDs
.append(segment
.get("provider:segmentation_id"))
3536 used_vlanIDs
= set(usedVlanIDs
)
3538 # find unused VLAN ID
3539 for vlanID_range
in self
.config
.get("multisegment_vlan_range"):
3541 start_vlanid
, end_vlanid
= map(
3542 int, vlanID_range
.replace(" ", "").split("-")
3545 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
3546 if vlanID
not in used_vlanIDs
:
3548 except Exception as exp
:
3549 raise vimconn
.VimConnException(
3550 "Exception {} occurred while generating VLAN ID.".format(exp
)
3553 raise vimconn
.VimConnConflictException(
3554 "Unable to create the VLAN segment. All VLAN IDs {} are in use.".format(
3555 self
.config
.get("multisegment_vlan_range")
3559 def _validate_vlan_ranges(self
, input_vlan_range
, text_vlan_range
):
3561 Method to validate user given vlanID ranges
3565 for vlanID_range
in input_vlan_range
:
3566 vlan_range
= vlanID_range
.replace(" ", "")
3568 vlanID_pattern
= r
"(\d)*-(\d)*$"
3569 match_obj
= re
.match(vlanID_pattern
, vlan_range
)
3571 raise vimconn
.VimConnConflictException(
3572 "Invalid VLAN range for {}: {}.You must provide "
3573 "'{}' in format [start_ID - end_ID].".format(
3574 text_vlan_range
, vlanID_range
, text_vlan_range
3578 start_vlanid
, end_vlanid
= map(int, vlan_range
.split("-"))
3579 if start_vlanid
<= 0:
3580 raise vimconn
.VimConnConflictException(
3581 "Invalid VLAN range for {}: {}. Start ID can not be zero. For VLAN "
3582 "networks valid IDs are 1 to 4094 ".format(
3583 text_vlan_range
, vlanID_range
3587 if end_vlanid
> 4094:
3588 raise vimconn
.VimConnConflictException(
3589 "Invalid VLAN range for {}: {}. End VLAN ID can not be "
3590 "greater than 4094. For VLAN networks valid IDs are 1 to 4094 ".format(
3591 text_vlan_range
, vlanID_range
3595 if start_vlanid
> end_vlanid
:
3596 raise vimconn
.VimConnConflictException(
3597 "Invalid VLAN range for {}: {}. You must provide '{}'"
3598 " in format start_ID - end_ID and start_ID < end_ID ".format(
3599 text_vlan_range
, vlanID_range
, text_vlan_range
3603 def get_hosts_info(self
):
3604 """Get the information of deployed hosts
3605 Returns the hosts content"""
3607 print("osconnector: Getting Host info from VIM")
3611 self
._reload
_connection
()
3612 hypervisors
= self
.nova
.hypervisors
.list()
3614 for hype
in hypervisors
:
3615 h_list
.append(hype
.to_dict())
3617 return 1, {"hosts": h_list
}
3618 except nvExceptions
.NotFound
as e
:
3619 error_value
= -vimconn
.HTTP_Not_Found
3620 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
3621 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
3622 error_value
= -vimconn
.HTTP_Bad_Request
3626 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
3629 # TODO insert exception vimconn.HTTP_Unauthorized
3630 # if reaching here is because an exception
3631 self
.logger
.debug("get_hosts_info " + error_text
)
3633 return error_value
, error_text
3635 def get_hosts(self
, vim_tenant
):
3636 """Get the hosts and deployed instances
3637 Returns the hosts content"""
3638 r
, hype_dict
= self
.get_hosts_info()
3643 hypervisors
= hype_dict
["hosts"]
3646 servers
= self
.nova
.servers
.list()
3647 for hype
in hypervisors
:
3648 for server
in servers
:
3650 server
.to_dict()["OS-EXT-SRV-ATTR:hypervisor_hostname"]
3651 == hype
["hypervisor_hostname"]
3654 hype
["vm"].append(server
.id)
3656 hype
["vm"] = [server
.id]
3659 except nvExceptions
.NotFound
as e
:
3660 error_value
= -vimconn
.HTTP_Not_Found
3661 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
3662 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
3663 error_value
= -vimconn
.HTTP_Bad_Request
3667 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
3670 # TODO insert exception vimconn.HTTP_Unauthorized
3671 # if reaching here is because an exception
3672 self
.logger
.debug("get_hosts " + error_text
)
3674 return error_value
, error_text
3676 @catch_any_exception
3677 def new_affinity_group(self
, affinity_group_data
):
3678 """Adds a server group to VIM
3679 affinity_group_data contains a dictionary with information, keys:
3680 name: name in VIM for the server group
3681 type: affinity or anti-affinity
3682 scope: Only nfvi-node allowed
3683 Returns the server group identifier"""
3684 self
.logger
.debug("Adding Server Group '%s'", str(affinity_group_data
))
3685 name
= affinity_group_data
["name"]
3686 policy
= affinity_group_data
["type"]
3687 self
._reload
_connection
()
3688 new_server_group
= self
.nova
.server_groups
.create(name
, policy
)
3689 return new_server_group
.id
3691 @catch_any_exception
3692 def get_affinity_group(self
, affinity_group_id
):
3693 """Obtain server group details from the VIM. Returns the server group detais as a dict"""
3694 self
.logger
.debug("Getting flavor '%s'", affinity_group_id
)
3695 self
._reload
_connection
()
3696 server_group
= self
.nova
.server_groups
.find(id=affinity_group_id
)
3697 return server_group
.to_dict()
3699 @catch_any_exception
3700 def delete_affinity_group(self
, affinity_group_id
):
3701 """Deletes a server group from the VIM. Returns the old affinity_group_id"""
3702 self
.logger
.debug("Getting server group '%s'", affinity_group_id
)
3703 self
._reload
_connection
()
3704 self
.nova
.server_groups
.delete(affinity_group_id
)
3705 return affinity_group_id
3707 @catch_any_exception
3708 def get_vdu_state(self
, vm_id
, host_is_required
=False) -> list:
3709 """Getting the state of a VDU.
3711 vm_id (str): ID of an instance
3712 host_is_required (Boolean): If the VIM account is non-admin, host info does not appear in server_dict
3713 and if this is set to True, it raises KeyError.
3715 vdu_data (list): VDU details including state, flavor, host_info, AZ
3717 self
.logger
.debug("Getting the status of VM")
3718 self
.logger
.debug("VIM VM ID %s", vm_id
)
3719 self
._reload
_connection
()
3720 server_dict
= self
._find
_nova
_server
(vm_id
)
3721 srv_attr
= "OS-EXT-SRV-ATTR:host"
3723 server_dict
[srv_attr
] if host_is_required
else server_dict
.get(srv_attr
)
3726 server_dict
["status"],
3727 server_dict
["flavor"]["id"],
3729 server_dict
["OS-EXT-AZ:availability_zone"],
3731 self
.logger
.debug("vdu_data %s", vdu_data
)
3734 def check_compute_availability(self
, host
, server_flavor_details
):
3735 self
._reload
_connection
()
3736 hypervisor_search
= self
.nova
.hypervisors
.search(
3737 hypervisor_match
=host
, servers
=True
3739 for hypervisor
in hypervisor_search
:
3740 hypervisor_id
= hypervisor
.to_dict()["id"]
3741 hypervisor_details
= self
.nova
.hypervisors
.get(hypervisor
=hypervisor_id
)
3742 hypervisor_dict
= hypervisor_details
.to_dict()
3743 hypervisor_temp
= json
.dumps(hypervisor_dict
)
3744 hypervisor_json
= json
.loads(hypervisor_temp
)
3745 resources_available
= [
3746 hypervisor_json
["free_ram_mb"],
3747 hypervisor_json
["disk_available_least"],
3748 hypervisor_json
["vcpus"] - hypervisor_json
["vcpus_used"],
3750 compute_available
= all(
3751 x
> y
for x
, y
in zip(resources_available
, server_flavor_details
)
3753 if compute_available
:
3756 def check_availability_zone(
3757 self
, old_az
, server_flavor_details
, old_host
, host
=None
3759 self
._reload
_connection
()
3760 az_check
= {"zone_check": False, "compute_availability": None}
3761 aggregates_list
= self
.nova
.aggregates
.list()
3762 for aggregate
in aggregates_list
:
3763 aggregate_details
= aggregate
.to_dict()
3764 aggregate_temp
= json
.dumps(aggregate_details
)
3765 aggregate_json
= json
.loads(aggregate_temp
)
3766 if aggregate_json
["availability_zone"] == old_az
:
3767 hosts_list
= aggregate_json
["hosts"]
3768 if host
is not None:
3769 if host
in hosts_list
:
3770 az_check
["zone_check"] = True
3771 available_compute_id
= self
.check_compute_availability(
3772 host
, server_flavor_details
3774 if available_compute_id
is not None:
3775 az_check
["compute_availability"] = available_compute_id
3777 for check_host
in hosts_list
:
3778 if check_host
!= old_host
:
3779 available_compute_id
= self
.check_compute_availability(
3780 check_host
, server_flavor_details
3782 if available_compute_id
is not None:
3783 az_check
["zone_check"] = True
3784 az_check
["compute_availability"] = available_compute_id
3787 az_check
["zone_check"] = True
3790 @catch_any_exception
3791 def migrate_instance(self
, vm_id
, compute_host
=None):
3795 vm_id: ID of an instance
3796 compute_host: Host to migrate the vdu to
3798 self
._reload
_connection
()
3800 instance_state
= self
.get_vdu_state(vm_id
, host_is_required
=True)
3801 server_flavor_id
= instance_state
[1]
3802 server_hypervisor_name
= instance_state
[2]
3803 server_availability_zone
= instance_state
[3]
3804 server_flavor
= self
.nova
.flavors
.find(id=server_flavor_id
).to_dict()
3805 server_flavor_details
= [
3806 server_flavor
["ram"],
3807 server_flavor
["disk"],
3808 server_flavor
["vcpus"],
3810 if compute_host
== server_hypervisor_name
:
3811 raise vimconn
.VimConnException(
3812 "Unable to migrate instance '{}' to the same host '{}'".format(
3815 http_code
=vimconn
.HTTP_Bad_Request
,
3817 az_status
= self
.check_availability_zone(
3818 server_availability_zone
,
3819 server_flavor_details
,
3820 server_hypervisor_name
,
3823 availability_zone_check
= az_status
["zone_check"]
3824 available_compute_id
= az_status
.get("compute_availability")
3826 if availability_zone_check
is False:
3827 raise vimconn
.VimConnException(
3828 "Unable to migrate instance '{}' to a different availability zone".format(
3831 http_code
=vimconn
.HTTP_Bad_Request
,
3833 if available_compute_id
is not None:
3834 # disk_over_commit parameter for live_migrate method is not valid for Nova API version >= 2.25
3835 self
.nova
.servers
.live_migrate(
3837 host
=available_compute_id
,
3838 block_migration
=True,
3841 changed_compute_host
= ""
3842 if state
== "MIGRATING":
3843 vm_state
= self
.__wait
_for
_vm
(vm_id
, "ACTIVE")
3844 changed_compute_host
= self
.get_vdu_state(vm_id
, host_is_required
=True)[
3847 if vm_state
and changed_compute_host
== available_compute_id
:
3849 "Instance '{}' migrated to the new compute host '{}'".format(
3850 vm_id
, changed_compute_host
3853 return state
, available_compute_id
3855 raise vimconn
.VimConnException(
3856 "Migration Failed. Instance '{}' not moved to the new host {}".format(
3857 vm_id
, available_compute_id
3859 http_code
=vimconn
.HTTP_Bad_Request
,
3862 raise vimconn
.VimConnException(
3863 "Compute '{}' not available or does not have enough resources to migrate the instance".format(
3864 available_compute_id
3866 http_code
=vimconn
.HTTP_Bad_Request
,
3869 @catch_any_exception
3870 def resize_instance(self
, vm_id
, new_flavor_id
):
3872 For resizing the vm based on the given
3875 vm_id : ID of an instance
3876 new_flavor_id : Flavor id to be resized
3877 Return the status of a resized instance
3879 self
._reload
_connection
()
3880 self
.logger
.debug("resize the flavor of an instance")
3881 instance_status
, old_flavor_id
, compute_host
, az
= self
.get_vdu_state(vm_id
)
3882 old_flavor_disk
= self
.nova
.flavors
.find(id=old_flavor_id
).to_dict()["disk"]
3883 new_flavor_disk
= self
.nova
.flavors
.find(id=new_flavor_id
).to_dict()["disk"]
3884 if instance_status
== "ACTIVE" or instance_status
== "SHUTOFF":
3885 if old_flavor_disk
> new_flavor_disk
:
3886 raise nvExceptions
.BadRequest(
3888 message
="Server disk resize failed. Resize to lower disk flavor is not allowed",
3891 self
.nova
.servers
.resize(server
=vm_id
, flavor
=new_flavor_id
)
3892 vm_state
= self
.__wait
_for
_vm
(vm_id
, "VERIFY_RESIZE")
3894 instance_resized_status
= self
.confirm_resize(vm_id
)
3895 return instance_resized_status
3897 raise nvExceptions
.BadRequest(
3899 message
="Cannot 'resize' vm_state is in ERROR",
3903 self
.logger
.debug("ERROR : Instance is not in ACTIVE or SHUTOFF state")
3904 raise nvExceptions
.BadRequest(
3906 message
="Cannot 'resize' instance while it is in vm_state resized",
3909 def confirm_resize(self
, vm_id
):
3911 Confirm the resize of an instance
3913 vm_id: ID of an instance
3915 self
._reload
_connection
()
3916 self
.nova
.servers
.confirm_resize(server
=vm_id
)
3917 if self
.get_vdu_state(vm_id
)[0] == "VERIFY_RESIZE":
3918 self
.__wait
_for
_vm
(vm_id
, "ACTIVE")
3919 instance_status
= self
.get_vdu_state(vm_id
)[0]
3920 return instance_status
3922 def get_monitoring_data(self
):
3924 self
.logger
.debug("Getting servers and ports data from Openstack VIMs.")
3925 self
._reload
_connection
()
3926 all_servers
= self
.nova
.servers
.list(detailed
=True)
3928 for server
in all_servers
:
3929 if server
.flavor
.get("original_name"):
3930 server
.flavor
["id"] = self
.nova
.flavors
.find(
3931 name
=server
.flavor
["original_name"]
3933 except nClient
.exceptions
.NotFound
as e
:
3934 self
.logger
.warning(str(e
.message
))
3935 all_ports
= self
.neutron
.list_ports()
3936 return all_servers
, all_ports
3937 except Exception as e
:
3938 raise vimconn
.VimConnException(
3939 f
"Exception in monitoring while getting VMs and ports status: {str(e)}"