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
36 from pprint
import pformat
41 from cinderclient
import client
as cClient
42 from glanceclient
import client
as glClient
43 import glanceclient
.exc
as gl1Exceptions
44 from keystoneauth1
import session
45 from keystoneauth1
.identity
import v2
, v3
46 import keystoneclient
.exceptions
as ksExceptions
47 import keystoneclient
.v2_0
.client
as ksClient_v2
48 import keystoneclient
.v3
.client
as ksClient_v3
50 from neutronclient
.common
import exceptions
as neExceptions
51 from neutronclient
.neutron
import client
as neClient
52 from novaclient
import client
as nClient
, exceptions
as nvExceptions
53 from osm_ro_plugin
import vimconn
54 from requests
.exceptions
import ConnectionError
57 __author__
= "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
58 __date__
= "$22-sep-2017 23:59:59$"
60 """contain the openstack virtual machine status to openmano status"""
61 vmStatus2manoFormat
= {
64 "SUSPENDED": "SUSPENDED",
65 "SHUTOFF": "INACTIVE",
70 netStatus2manoFormat
= {
73 "INACTIVE": "INACTIVE",
79 supportedClassificationTypes
= ["legacy_flow_classifier"]
81 # global var to have a timeout creating and deleting volumes
86 class SafeDumper(yaml
.SafeDumper
):
87 def represent_data(self
, data
):
88 # Openstack APIs use custom subclasses of dict and YAML safe dumper
89 # is designed to not handle that (reference issue 142 of pyyaml)
90 if isinstance(data
, dict) and data
.__class
__ != dict:
91 # A simple solution is to convert those items back to dicts
92 data
= dict(data
.items())
94 return super(SafeDumper
, self
).represent_data(data
)
97 class vimconnector(vimconn
.VimConnector
):
112 """using common constructor parameters. In this case
113 'url' is the keystone authorization url,
114 'url_admin' is not use
116 api_version
= config
.get("APIversion")
118 if api_version
and api_version
not in ("v3.3", "v2.0", "2", "3"):
119 raise vimconn
.VimConnException(
120 "Invalid value '{}' for config:APIversion. "
121 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version
)
124 vim_type
= config
.get("vim_type")
126 if vim_type
and vim_type
not in ("vio", "VIO"):
127 raise vimconn
.VimConnException(
128 "Invalid value '{}' for config:vim_type."
129 "Allowed values are 'vio' or 'VIO'".format(vim_type
)
132 if config
.get("dataplane_net_vlan_range") is not None:
133 # validate vlan ranges provided by user
134 self
._validate
_vlan
_ranges
(
135 config
.get("dataplane_net_vlan_range"), "dataplane_net_vlan_range"
138 if config
.get("multisegment_vlan_range") is not None:
139 # validate vlan ranges provided by user
140 self
._validate
_vlan
_ranges
(
141 config
.get("multisegment_vlan_range"), "multisegment_vlan_range"
144 vimconn
.VimConnector
.__init
__(
158 if self
.config
.get("insecure") and self
.config
.get("ca_cert"):
159 raise vimconn
.VimConnException(
160 "options insecure and ca_cert are mutually exclusive"
165 if self
.config
.get("insecure"):
168 if self
.config
.get("ca_cert"):
169 self
.verify
= self
.config
.get("ca_cert")
172 raise TypeError("url param can not be NoneType")
174 self
.persistent_info
= persistent_info
175 self
.availability_zone
= persistent_info
.get("availability_zone", None)
176 self
.session
= persistent_info
.get("session", {"reload_client": True})
177 self
.my_tenant_id
= self
.session
.get("my_tenant_id")
178 self
.nova
= self
.session
.get("nova")
179 self
.neutron
= self
.session
.get("neutron")
180 self
.cinder
= self
.session
.get("cinder")
181 self
.glance
= self
.session
.get("glance")
182 # self.glancev1 = self.session.get("glancev1")
183 self
.keystone
= self
.session
.get("keystone")
184 self
.api_version3
= self
.session
.get("api_version3")
185 self
.vim_type
= self
.config
.get("vim_type")
188 self
.vim_type
= self
.vim_type
.upper()
190 if self
.config
.get("use_internal_endpoint"):
191 self
.endpoint_type
= "internalURL"
193 self
.endpoint_type
= None
195 logging
.getLogger("urllib3").setLevel(logging
.WARNING
)
196 logging
.getLogger("keystoneauth").setLevel(logging
.WARNING
)
197 logging
.getLogger("novaclient").setLevel(logging
.WARNING
)
198 self
.logger
= logging
.getLogger("ro.vim.openstack")
200 # allow security_groups to be a list or a single string
201 if isinstance(self
.config
.get("security_groups"), str):
202 self
.config
["security_groups"] = [self
.config
["security_groups"]]
204 self
.security_groups_id
= None
206 # ###### VIO Specific Changes #########
207 if self
.vim_type
== "VIO":
208 self
.logger
= logging
.getLogger("ro.vim.vio")
211 self
.logger
.setLevel(getattr(logging
, log_level
))
213 def __getitem__(self
, index
):
214 """Get individuals parameters.
216 if index
== "project_domain_id":
217 return self
.config
.get("project_domain_id")
218 elif index
== "user_domain_id":
219 return self
.config
.get("user_domain_id")
221 return vimconn
.VimConnector
.__getitem
__(self
, index
)
223 def __setitem__(self
, index
, value
):
224 """Set individuals parameters and it is marked as dirty so to force connection reload.
226 if index
== "project_domain_id":
227 self
.config
["project_domain_id"] = value
228 elif index
== "user_domain_id":
229 self
.config
["user_domain_id"] = value
231 vimconn
.VimConnector
.__setitem
__(self
, index
, value
)
233 self
.session
["reload_client"] = True
235 def serialize(self
, value
):
236 """Serialization of python basic types.
238 In the case value is not serializable a message will be logged and a
239 simple representation of the data that cannot be converted back to
242 if isinstance(value
, str):
247 value
, Dumper
=SafeDumper
, default_flow_style
=True, width
=256
249 except yaml
.representer
.RepresenterError
:
251 "The following entity cannot be serialized in YAML:\n\n%s\n\n",
258 def _reload_connection(self
):
259 """Called before any operation, it check if credentials has changed
260 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
262 # TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
263 if self
.session
["reload_client"]:
264 if self
.config
.get("APIversion"):
265 self
.api_version3
= (
266 self
.config
["APIversion"] == "v3.3"
267 or self
.config
["APIversion"] == "3"
269 else: # get from ending auth_url that end with v3 or with v2.0
270 self
.api_version3
= self
.url
.endswith("/v3") or self
.url
.endswith(
274 self
.session
["api_version3"] = self
.api_version3
276 if self
.api_version3
:
277 if self
.config
.get("project_domain_id") or self
.config
.get(
278 "project_domain_name"
280 project_domain_id_default
= None
282 project_domain_id_default
= "default"
284 if self
.config
.get("user_domain_id") or self
.config
.get(
287 user_domain_id_default
= None
289 user_domain_id_default
= "default"
293 password
=self
.passwd
,
294 project_name
=self
.tenant_name
,
295 project_id
=self
.tenant_id
,
296 project_domain_id
=self
.config
.get(
297 "project_domain_id", project_domain_id_default
299 user_domain_id
=self
.config
.get(
300 "user_domain_id", user_domain_id_default
302 project_domain_name
=self
.config
.get("project_domain_name"),
303 user_domain_name
=self
.config
.get("user_domain_name"),
309 password
=self
.passwd
,
310 tenant_name
=self
.tenant_name
,
311 tenant_id
=self
.tenant_id
,
314 sess
= session
.Session(auth
=auth
, verify
=self
.verify
)
315 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
316 # Titanium cloud and StarlingX
317 region_name
= self
.config
.get("region_name")
319 if self
.api_version3
:
320 self
.keystone
= ksClient_v3
.Client(
322 endpoint_type
=self
.endpoint_type
,
323 region_name
=region_name
,
326 self
.keystone
= ksClient_v2
.Client(
327 session
=sess
, endpoint_type
=self
.endpoint_type
330 self
.session
["keystone"] = self
.keystone
331 # In order to enable microversion functionality an explicit microversion must be specified in "config".
332 # This implementation approach is due to the warning message in
333 # https://developer.openstack.org/api-guide/compute/microversions.html
334 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
335 # always require an specific microversion.
336 # To be able to use "device role tagging" functionality define "microversion: 2.32" in datacenter config
337 version
= self
.config
.get("microversion")
342 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
343 # Titanium cloud and StarlingX
344 self
.nova
= self
.session
["nova"] = nClient
.Client(
347 endpoint_type
=self
.endpoint_type
,
348 region_name
=region_name
,
350 self
.neutron
= self
.session
["neutron"] = neClient
.Client(
353 endpoint_type
=self
.endpoint_type
,
354 region_name
=region_name
,
356 self
.cinder
= self
.session
["cinder"] = cClient
.Client(
359 endpoint_type
=self
.endpoint_type
,
360 region_name
=region_name
,
364 self
.my_tenant_id
= self
.session
["my_tenant_id"] = sess
.get_project_id()
366 self
.logger
.error("Cannot get project_id from session", exc_info
=True)
368 if self
.endpoint_type
== "internalURL":
369 glance_service_id
= self
.keystone
.services
.list(name
="glance")[0].id
370 glance_endpoint
= self
.keystone
.endpoints
.list(
371 glance_service_id
, interface
="internal"
374 glance_endpoint
= None
376 self
.glance
= self
.session
["glance"] = glClient
.Client(
377 2, session
=sess
, endpoint
=glance_endpoint
379 # using version 1 of glance client in new_image()
380 # self.glancev1 = self.session["glancev1"] = glClient.Client("1", session=sess,
381 # endpoint=glance_endpoint)
382 self
.session
["reload_client"] = False
383 self
.persistent_info
["session"] = self
.session
384 # add availablity zone info inside self.persistent_info
385 self
._set
_availablity
_zones
()
386 self
.persistent_info
["availability_zone"] = self
.availability_zone
387 # force to get again security_groups_ids next time they are needed
388 self
.security_groups_id
= None
390 def __net_os2mano(self
, net_list_dict
):
391 """Transform the net openstack format to mano format
392 net_list_dict can be a list of dict or a single dict"""
393 if type(net_list_dict
) is dict:
394 net_list_
= (net_list_dict
,)
395 elif type(net_list_dict
) is list:
396 net_list_
= net_list_dict
398 raise TypeError("param net_list_dict must be a list or a dictionary")
399 for net
in net_list_
:
400 if net
.get("provider:network_type") == "vlan":
403 net
["type"] = "bridge"
405 def __classification_os2mano(self
, class_list_dict
):
406 """Transform the openstack format (Flow Classifier) to mano format
407 (Classification) class_list_dict can be a list of dict or a single dict
409 if isinstance(class_list_dict
, dict):
410 class_list_
= [class_list_dict
]
411 elif isinstance(class_list_dict
, list):
412 class_list_
= class_list_dict
414 raise TypeError("param class_list_dict must be a list or a dictionary")
415 for classification
in class_list_
:
416 id = classification
.pop("id")
417 name
= classification
.pop("name")
418 description
= classification
.pop("description")
419 project_id
= classification
.pop("project_id")
420 tenant_id
= classification
.pop("tenant_id")
421 original_classification
= copy
.deepcopy(classification
)
422 classification
.clear()
423 classification
["ctype"] = "legacy_flow_classifier"
424 classification
["definition"] = original_classification
425 classification
["id"] = id
426 classification
["name"] = name
427 classification
["description"] = description
428 classification
["project_id"] = project_id
429 classification
["tenant_id"] = tenant_id
431 def __sfi_os2mano(self
, sfi_list_dict
):
432 """Transform the openstack format (Port Pair) to mano format (SFI)
433 sfi_list_dict can be a list of dict or a single dict
435 if isinstance(sfi_list_dict
, dict):
436 sfi_list_
= [sfi_list_dict
]
437 elif isinstance(sfi_list_dict
, list):
438 sfi_list_
= sfi_list_dict
440 raise TypeError("param sfi_list_dict must be a list or a dictionary")
442 for sfi
in sfi_list_
:
443 sfi
["ingress_ports"] = []
444 sfi
["egress_ports"] = []
446 if sfi
.get("ingress"):
447 sfi
["ingress_ports"].append(sfi
["ingress"])
449 if sfi
.get("egress"):
450 sfi
["egress_ports"].append(sfi
["egress"])
454 params
= sfi
.get("service_function_parameters")
458 correlation
= params
.get("correlation")
463 sfi
["sfc_encap"] = sfc_encap
464 del sfi
["service_function_parameters"]
466 def __sf_os2mano(self
, sf_list_dict
):
467 """Transform the openstack format (Port Pair Group) to mano format (SF)
468 sf_list_dict can be a list of dict or a single dict
470 if isinstance(sf_list_dict
, dict):
471 sf_list_
= [sf_list_dict
]
472 elif isinstance(sf_list_dict
, list):
473 sf_list_
= sf_list_dict
475 raise TypeError("param sf_list_dict must be a list or a dictionary")
478 del sf
["port_pair_group_parameters"]
479 sf
["sfis"] = sf
["port_pairs"]
482 def __sfp_os2mano(self
, sfp_list_dict
):
483 """Transform the openstack format (Port Chain) to mano format (SFP)
484 sfp_list_dict can be a list of dict or a single dict
486 if isinstance(sfp_list_dict
, dict):
487 sfp_list_
= [sfp_list_dict
]
488 elif isinstance(sfp_list_dict
, list):
489 sfp_list_
= sfp_list_dict
491 raise TypeError("param sfp_list_dict must be a list or a dictionary")
493 for sfp
in sfp_list_
:
494 params
= sfp
.pop("chain_parameters")
498 correlation
= params
.get("correlation")
503 sfp
["sfc_encap"] = sfc_encap
504 sfp
["spi"] = sfp
.pop("chain_id")
505 sfp
["classifications"] = sfp
.pop("flow_classifiers")
506 sfp
["service_functions"] = sfp
.pop("port_pair_groups")
508 # placeholder for now; read TODO note below
509 def _validate_classification(self
, type, definition
):
510 # only legacy_flow_classifier Type is supported at this point
512 # TODO(igordcard): this method should be an abstract method of an
513 # abstract Classification class to be implemented by the specific
514 # Types. Also, abstract vimconnector should call the validation
515 # method before the implemented VIM connectors are called.
517 def _format_exception(self
, exception
):
518 """Transform a keystone, nova, neutron exception into a vimconn exception discovering the cause"""
519 message_error
= str(exception
)
525 neExceptions
.NetworkNotFoundClient
,
526 nvExceptions
.NotFound
,
527 ksExceptions
.NotFound
,
528 gl1Exceptions
.HTTPNotFound
,
531 raise vimconn
.VimConnNotFoundException(
532 type(exception
).__name
__ + ": " + message_error
538 gl1Exceptions
.HTTPException
,
539 gl1Exceptions
.CommunicationError
,
541 ksExceptions
.ConnectionError
,
542 neExceptions
.ConnectionFailed
,
545 if type(exception
).__name
__ == "SSLError":
546 tip
= " (maybe option 'insecure' must be added to the VIM)"
548 raise vimconn
.VimConnConnectionException(
549 "Invalid URL or credentials{}: {}".format(tip
, message_error
)
555 nvExceptions
.BadRequest
,
556 ksExceptions
.BadRequest
,
559 raise vimconn
.VimConnException(
560 type(exception
).__name
__ + ": " + message_error
565 nvExceptions
.ClientException
,
566 ksExceptions
.ClientException
,
567 neExceptions
.NeutronException
,
570 raise vimconn
.VimConnUnexpectedResponse(
571 type(exception
).__name
__ + ": " + message_error
573 elif isinstance(exception
, nvExceptions
.Conflict
):
574 raise vimconn
.VimConnConflictException(
575 type(exception
).__name
__ + ": " + message_error
577 elif isinstance(exception
, vimconn
.VimConnException
):
580 self
.logger
.error("General Exception " + message_error
, exc_info
=True)
582 raise vimconn
.VimConnConnectionException(
583 type(exception
).__name
__ + ": " + message_error
586 def _get_ids_from_name(self
):
588 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
591 # get tenant_id if only tenant_name is supplied
592 self
._reload
_connection
()
594 if not self
.my_tenant_id
:
595 raise vimconn
.VimConnConnectionException(
596 "Error getting tenant information from name={} id={}".format(
597 self
.tenant_name
, self
.tenant_id
601 if self
.config
.get("security_groups") and not self
.security_groups_id
:
602 # convert from name to id
603 neutron_sg_list
= self
.neutron
.list_security_groups(
604 tenant_id
=self
.my_tenant_id
607 self
.security_groups_id
= []
608 for sg
in self
.config
.get("security_groups"):
609 for neutron_sg
in neutron_sg_list
:
610 if sg
in (neutron_sg
["id"], neutron_sg
["name"]):
611 self
.security_groups_id
.append(neutron_sg
["id"])
614 self
.security_groups_id
= None
616 raise vimconn
.VimConnConnectionException(
617 "Not found security group {} for this tenant".format(sg
)
620 def check_vim_connectivity(self
):
621 # just get network list to check connectivity and credentials
622 self
.get_network_list(filter_dict
={})
624 def get_tenant_list(self
, filter_dict
={}):
625 """Obtain tenants of VIM
626 filter_dict can contain the following keys:
627 name: filter by tenant name
628 id: filter by tenant uuid/id
630 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
632 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
635 self
._reload
_connection
()
637 if self
.api_version3
:
638 project_class_list
= self
.keystone
.projects
.list(
639 name
=filter_dict
.get("name")
642 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
646 for project
in project_class_list
:
647 if filter_dict
.get("id") and filter_dict
["id"] != project
.id:
650 project_list
.append(project
.to_dict())
654 ksExceptions
.ConnectionError
,
655 ksExceptions
.ClientException
,
658 self
._format
_exception
(e
)
660 def new_tenant(self
, tenant_name
, tenant_description
):
661 """Adds a new tenant to openstack VIM. Returns the tenant identifier"""
662 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
665 self
._reload
_connection
()
667 if self
.api_version3
:
668 project
= self
.keystone
.projects
.create(
670 self
.config
.get("project_domain_id", "default"),
671 description
=tenant_description
,
675 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
679 ksExceptions
.ConnectionError
,
680 ksExceptions
.ClientException
,
681 ksExceptions
.BadRequest
,
684 self
._format
_exception
(e
)
686 def delete_tenant(self
, tenant_id
):
687 """Delete a tenant from openstack VIM. Returns the old tenant identifier"""
688 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
691 self
._reload
_connection
()
693 if self
.api_version3
:
694 self
.keystone
.projects
.delete(tenant_id
)
696 self
.keystone
.tenants
.delete(tenant_id
)
700 ksExceptions
.ConnectionError
,
701 ksExceptions
.ClientException
,
702 ksExceptions
.NotFound
,
705 self
._format
_exception
(e
)
713 provider_network_profile
=None,
715 """Adds a tenant network to VIM
717 'net_name': name of the network
719 'bridge': overlay isolated network
720 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
721 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
722 'ip_profile': is a dict containing the IP parameters of the network
723 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
724 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
725 'gateway_address': (Optional) ip_schema, that is X.X.X.X
726 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
727 'dhcp_enabled': True or False
728 'dhcp_start_address': ip_schema, first IP to grant
729 'dhcp_count': number of IPs to grant.
730 'shared': if this network can be seen/use by other tenants/organization
731 'provider_network_profile': (optional) contains {segmentation-id: vlan, network-type: vlan|vxlan,
732 physical-network: physnet-label}
733 Returns a tuple with the network identifier and created_items, or raises an exception on error
734 created_items can be None or a dictionary where this method can include key-values that will be passed to
735 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
736 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
740 "Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
742 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
747 if provider_network_profile
:
748 vlan
= provider_network_profile
.get("segmentation-id")
752 self
._reload
_connection
()
753 network_dict
= {"name": net_name
, "admin_state_up": True}
755 if net_type
in ("data", "ptp"):
756 provider_physical_network
= None
758 if provider_network_profile
and provider_network_profile
.get(
761 provider_physical_network
= provider_network_profile
.get(
765 # provider-network must be one of the dataplane_physcial_netowrk if this is a list. If it is string
766 # or not declared, just ignore the checking
769 self
.config
.get("dataplane_physical_net"), (tuple, list)
771 and provider_physical_network
772 not in self
.config
["dataplane_physical_net"]
774 raise vimconn
.VimConnConflictException(
775 "Invalid parameter 'provider-network:physical-network' "
776 "for network creation. '{}' is not one of the declared "
777 "list at VIM_config:dataplane_physical_net".format(
778 provider_physical_network
782 # use the default dataplane_physical_net
783 if not provider_physical_network
:
784 provider_physical_network
= self
.config
.get(
785 "dataplane_physical_net"
788 # if it is non empty list, use the first value. If it is a string use the value directly
790 isinstance(provider_physical_network
, (tuple, list))
791 and provider_physical_network
793 provider_physical_network
= provider_physical_network
[0]
795 if not provider_physical_network
:
796 raise vimconn
.VimConnConflictException(
797 "missing information needed for underlay networks. Provide "
798 "'dataplane_physical_net' configuration at VIM or use the NS "
799 "instantiation parameter 'provider-network.physical-network'"
803 if not self
.config
.get("multisegment_support"):
805 "provider:physical_network"
806 ] = provider_physical_network
809 provider_network_profile
810 and "network-type" in provider_network_profile
813 "provider:network_type"
814 ] = provider_network_profile
["network-type"]
816 network_dict
["provider:network_type"] = self
.config
.get(
817 "dataplane_network_type", "vlan"
821 network_dict
["provider:segmentation_id"] = vlan
826 "provider:physical_network": "",
827 "provider:network_type": "vxlan",
829 segment_list
.append(segment1_dict
)
831 "provider:physical_network": provider_physical_network
,
832 "provider:network_type": "vlan",
836 segment2_dict
["provider:segmentation_id"] = vlan
837 elif self
.config
.get("multisegment_vlan_range"):
838 vlanID
= self
._generate
_multisegment
_vlanID
()
839 segment2_dict
["provider:segmentation_id"] = vlanID
842 # raise vimconn.VimConnConflictException(
843 # "You must provide "multisegment_vlan_range" at config dict before creating a multisegment
845 segment_list
.append(segment2_dict
)
846 network_dict
["segments"] = segment_list
848 # VIO Specific Changes. It needs a concrete VLAN
849 if self
.vim_type
== "VIO" and vlan
is None:
850 if self
.config
.get("dataplane_net_vlan_range") is None:
851 raise vimconn
.VimConnConflictException(
852 "You must provide 'dataplane_net_vlan_range' in format "
853 "[start_ID - end_ID] at VIM_config for creating underlay "
857 network_dict
["provider:segmentation_id"] = self
._generate
_vlanID
()
859 network_dict
["shared"] = shared
861 if self
.config
.get("disable_network_port_security"):
862 network_dict
["port_security_enabled"] = False
864 if self
.config
.get("neutron_availability_zone_hints"):
865 hints
= self
.config
.get("neutron_availability_zone_hints")
867 if isinstance(hints
, str):
870 network_dict
["availability_zone_hints"] = hints
872 new_net
= self
.neutron
.create_network({"network": network_dict
})
874 # create subnetwork, even if there is no profile
879 if not ip_profile
.get("subnet_address"):
880 # Fake subnet is required
881 subnet_rand
= random
.randint(0, 255)
882 ip_profile
["subnet_address"] = "192.168.{}.0/24".format(subnet_rand
)
884 if "ip_version" not in ip_profile
:
885 ip_profile
["ip_version"] = "IPv4"
888 "name": net_name
+ "-subnet",
889 "network_id": new_net
["network"]["id"],
890 "ip_version": 4 if ip_profile
["ip_version"] == "IPv4" else 6,
891 "cidr": ip_profile
["subnet_address"],
894 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
895 if ip_profile
.get("gateway_address"):
896 subnet
["gateway_ip"] = ip_profile
["gateway_address"]
898 subnet
["gateway_ip"] = None
900 if ip_profile
.get("dns_address"):
901 subnet
["dns_nameservers"] = ip_profile
["dns_address"].split(";")
903 if "dhcp_enabled" in ip_profile
:
904 subnet
["enable_dhcp"] = (
906 if ip_profile
["dhcp_enabled"] == "false"
907 or ip_profile
["dhcp_enabled"] is False
911 if ip_profile
.get("dhcp_start_address"):
912 subnet
["allocation_pools"] = []
913 subnet
["allocation_pools"].append(dict())
914 subnet
["allocation_pools"][0]["start"] = ip_profile
[
918 if ip_profile
.get("dhcp_count"):
919 # parts = ip_profile["dhcp_start_address"].split(".")
920 # ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
921 ip_int
= int(netaddr
.IPAddress(ip_profile
["dhcp_start_address"]))
922 ip_int
+= ip_profile
["dhcp_count"] - 1
923 ip_str
= str(netaddr
.IPAddress(ip_int
))
924 subnet
["allocation_pools"][0]["end"] = ip_str
926 # self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
927 self
.neutron
.create_subnet({"subnet": subnet
})
929 if net_type
== "data" and self
.config
.get("multisegment_support"):
930 if self
.config
.get("l2gw_support"):
931 l2gw_list
= self
.neutron
.list_l2_gateways().get("l2_gateways", ())
932 for l2gw
in l2gw_list
:
934 "l2_gateway_id": l2gw
["id"],
935 "network_id": new_net
["network"]["id"],
936 "segmentation_id": str(vlanID
),
938 new_l2gw_conn
= self
.neutron
.create_l2_gateway_connection(
939 {"l2_gateway_connection": l2gw_conn
}
943 + str(new_l2gw_conn
["l2_gateway_connection"]["id"])
946 return new_net
["network"]["id"], created_items
947 except Exception as e
:
948 # delete l2gw connections (if any) before deleting the network
949 for k
, v
in created_items
.items():
950 if not v
: # skip already deleted
954 k_item
, _
, k_id
= k
.partition(":")
956 if k_item
== "l2gwconn":
957 self
.neutron
.delete_l2_gateway_connection(k_id
)
958 except Exception as e2
:
960 "Error deleting l2 gateway connection: {}: {}".format(
961 type(e2
).__name
__, e2
966 self
.neutron
.delete_network(new_net
["network"]["id"])
968 self
._format
_exception
(e
)
970 def get_network_list(self
, filter_dict
={}):
971 """Obtain tenant networks of VIM
977 admin_state_up: boolean
979 Returns the network list of dictionaries
981 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
984 self
._reload
_connection
()
985 filter_dict_os
= filter_dict
.copy()
987 if self
.api_version3
and "tenant_id" in filter_dict_os
:
989 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
991 net_dict
= self
.neutron
.list_networks(**filter_dict_os
)
992 net_list
= net_dict
["networks"]
993 self
.__net
_os
2mano
(net_list
)
997 neExceptions
.ConnectionFailed
,
998 ksExceptions
.ClientException
,
999 neExceptions
.NeutronException
,
1002 self
._format
_exception
(e
)
1004 def get_network(self
, net_id
):
1005 """Obtain details of network from VIM
1006 Returns the network information from a network id"""
1007 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
1008 filter_dict
= {"id": net_id
}
1009 net_list
= self
.get_network_list(filter_dict
)
1011 if len(net_list
) == 0:
1012 raise vimconn
.VimConnNotFoundException(
1013 "Network '{}' not found".format(net_id
)
1015 elif len(net_list
) > 1:
1016 raise vimconn
.VimConnConflictException(
1017 "Found more than one network with this criteria"
1022 for subnet_id
in net
.get("subnets", ()):
1024 subnet
= self
.neutron
.show_subnet(subnet_id
)
1025 except Exception as e
:
1027 "osconnector.get_network(): Error getting subnet %s %s"
1030 subnet
= {"id": subnet_id
, "fault": str(e
)}
1032 subnets
.append(subnet
)
1034 net
["subnets"] = subnets
1035 net
["encapsulation"] = net
.get("provider:network_type")
1036 net
["encapsulation_type"] = net
.get("provider:network_type")
1037 net
["segmentation_id"] = net
.get("provider:segmentation_id")
1038 net
["encapsulation_id"] = net
.get("provider:segmentation_id")
1042 def delete_network(self
, net_id
, created_items
=None):
1044 Removes a tenant network from VIM and its associated elements
1045 :param net_id: VIM identifier of the network, provided by method new_network
1046 :param created_items: dictionary with extra items to be deleted. provided by method new_network
1047 Returns the network identifier or raises an exception upon error or when network is not found
1049 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
1051 if created_items
is None:
1055 self
._reload
_connection
()
1056 # delete l2gw connections (if any) before deleting the network
1057 for k
, v
in created_items
.items():
1058 if not v
: # skip already deleted
1062 k_item
, _
, k_id
= k
.partition(":")
1063 if k_item
== "l2gwconn":
1064 self
.neutron
.delete_l2_gateway_connection(k_id
)
1065 except Exception as e
:
1067 "Error deleting l2 gateway connection: {}: {}".format(
1072 # delete VM ports attached to this networks before the network
1073 ports
= self
.neutron
.list_ports(network_id
=net_id
)
1074 for p
in ports
["ports"]:
1076 self
.neutron
.delete_port(p
["id"])
1077 except Exception as e
:
1078 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
1080 self
.neutron
.delete_network(net_id
)
1084 neExceptions
.ConnectionFailed
,
1085 neExceptions
.NetworkNotFoundClient
,
1086 neExceptions
.NeutronException
,
1087 ksExceptions
.ClientException
,
1088 neExceptions
.NeutronException
,
1091 self
._format
_exception
(e
)
1093 def refresh_nets_status(self
, net_list
):
1094 """Get the status of the networks
1095 Params: the list of network identifiers
1096 Returns a dictionary with:
1097 net_id: #VIM id of this network
1098 status: #Mandatory. Text with one of:
1099 # DELETED (not found at vim)
1100 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1101 # OTHER (Vim reported other status not understood)
1102 # ERROR (VIM indicates an ERROR status)
1103 # ACTIVE, INACTIVE, DOWN (admin down),
1104 # BUILD (on building process)
1106 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1107 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1111 for net_id
in net_list
:
1115 net_vim
= self
.get_network(net_id
)
1117 if net_vim
["status"] in netStatus2manoFormat
:
1118 net
["status"] = netStatus2manoFormat
[net_vim
["status"]]
1120 net
["status"] = "OTHER"
1121 net
["error_msg"] = "VIM status reported " + net_vim
["status"]
1123 if net
["status"] == "ACTIVE" and not net_vim
["admin_state_up"]:
1124 net
["status"] = "DOWN"
1126 net
["vim_info"] = self
.serialize(net_vim
)
1128 if net_vim
.get("fault"): # TODO
1129 net
["error_msg"] = str(net_vim
["fault"])
1130 except vimconn
.VimConnNotFoundException
as e
:
1131 self
.logger
.error("Exception getting net status: %s", str(e
))
1132 net
["status"] = "DELETED"
1133 net
["error_msg"] = str(e
)
1134 except vimconn
.VimConnException
as e
:
1135 self
.logger
.error("Exception getting net status: %s", str(e
))
1136 net
["status"] = "VIM_ERROR"
1137 net
["error_msg"] = str(e
)
1138 net_dict
[net_id
] = net
1141 def get_flavor(self
, flavor_id
):
1142 """Obtain flavor details from the VIM. Returns the flavor dict details"""
1143 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
1146 self
._reload
_connection
()
1147 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
1148 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1150 return flavor
.to_dict()
1152 nvExceptions
.NotFound
,
1153 nvExceptions
.ClientException
,
1154 ksExceptions
.ClientException
,
1157 self
._format
_exception
(e
)
1159 def get_flavor_id_from_data(self
, flavor_dict
):
1160 """Obtain flavor id that match the flavor description
1161 Returns the flavor_id or raises a vimconnNotFoundException
1162 flavor_dict: contains the required ram, vcpus, disk
1163 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
1164 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
1165 vimconnNotFoundException is raised
1167 exact_match
= False if self
.config
.get("use_existing_flavors") else True
1170 self
._reload
_connection
()
1171 flavor_candidate_id
= None
1172 flavor_candidate_data
= (10000, 10000, 10000)
1175 flavor_dict
["vcpus"],
1176 flavor_dict
["disk"],
1177 flavor_dict
.get("ephemeral", 0),
1178 flavor_dict
.get("swap", 0),
1181 extended
= flavor_dict
.get("extended", {})
1184 raise vimconn
.VimConnNotFoundException(
1185 "Flavor with EPA still not implemented"
1187 # if len(numas) > 1:
1188 # raise vimconn.VimConnNotFoundException("Cannot find any flavor with more than one numa")
1190 # numas = extended.get("numas")
1191 for flavor
in self
.nova
.flavors
.list():
1192 epa
= flavor
.get_keys()
1203 flavor
.swap
if isinstance(flavor
.swap
, int) else 0,
1205 if flavor_data
== flavor_target
:
1209 and flavor_target
< flavor_data
< flavor_candidate_data
1211 flavor_candidate_id
= flavor
.id
1212 flavor_candidate_data
= flavor_data
1214 if not exact_match
and flavor_candidate_id
:
1215 return flavor_candidate_id
1217 raise vimconn
.VimConnNotFoundException(
1218 "Cannot find any flavor matching '{}'".format(flavor_dict
)
1221 nvExceptions
.NotFound
,
1222 nvExceptions
.ClientException
,
1223 ksExceptions
.ClientException
,
1226 self
._format
_exception
(e
)
1228 def process_resource_quota(self
, quota
, prefix
, extra_specs
):
1234 if "limit" in quota
:
1235 extra_specs
["quota:" + prefix
+ "_limit"] = quota
["limit"]
1237 if "reserve" in quota
:
1238 extra_specs
["quota:" + prefix
+ "_reservation"] = quota
["reserve"]
1240 if "shares" in quota
:
1241 extra_specs
["quota:" + prefix
+ "_shares_level"] = "custom"
1242 extra_specs
["quota:" + prefix
+ "_shares_share"] = quota
["shares"]
1244 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
1245 """Adds a tenant flavor to openstack VIM
1246 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name
1248 Returns the flavor identifier
1250 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
1256 name
= flavor_data
["name"]
1257 while retry
< max_retries
:
1260 self
._reload
_connection
()
1262 if change_name_if_used
:
1265 fl
= self
.nova
.flavors
.list()
1268 fl_names
.append(f
.name
)
1270 while name
in fl_names
:
1272 name
= flavor_data
["name"] + "-" + str(name_suffix
)
1274 ram
= flavor_data
.get("ram", 64)
1275 vcpus
= flavor_data
.get("vcpus", 1)
1278 extended
= flavor_data
.get("extended")
1280 numas
= extended
.get("numas")
1283 numa_nodes
= len(numas
)
1286 return -1, "Can not add flavor with more than one numa"
1288 extra_specs
["hw:numa_nodes"] = str(numa_nodes
)
1289 extra_specs
["hw:mem_page_size"] = "large"
1290 extra_specs
["hw:cpu_policy"] = "dedicated"
1291 extra_specs
["hw:numa_mempolicy"] = "strict"
1293 if self
.vim_type
== "VIO":
1294 extra_specs
["vmware:latency_sensitivity_level"] = "high"
1297 # overwrite ram and vcpus
1298 # check if key "memory" is present in numa else use ram value at flavor
1299 if "memory" in numa
:
1300 ram
= numa
["memory"] * 1024
1301 # See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/
1302 # implemented/virt-driver-cpu-thread-pinning.html
1303 extra_specs
["hw:cpu_sockets"] = 1
1305 if "paired-threads" in numa
:
1306 vcpus
= numa
["paired-threads"] * 2
1307 # cpu_thread_policy "require" implies that the compute node must have an
1309 extra_specs
["hw:cpu_thread_policy"] = "require"
1310 extra_specs
["hw:cpu_policy"] = "dedicated"
1311 elif "cores" in numa
:
1312 vcpus
= numa
["cores"]
1313 # cpu_thread_policy "prefer" implies that the host must not have an SMT
1314 # architecture, or a non-SMT architecture will be emulated
1315 extra_specs
["hw:cpu_thread_policy"] = "isolate"
1316 extra_specs
["hw:cpu_policy"] = "dedicated"
1317 elif "threads" in numa
:
1318 vcpus
= numa
["threads"]
1319 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT
1321 extra_specs
["hw:cpu_thread_policy"] = "prefer"
1322 extra_specs
["hw:cpu_policy"] = "dedicated"
1323 # for interface in numa.get("interfaces",() ):
1324 # if interface["dedicated"]=="yes":
1325 # raise vimconn.VimConnException("Passthrough interfaces are not supported
1326 # for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
1327 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"'
1328 # when a way to connect it is available
1329 elif extended
.get("cpu-quota"):
1330 self
.process_resource_quota(
1331 extended
.get("cpu-quota"), "cpu", extra_specs
1334 if extended
.get("mem-quota"):
1335 self
.process_resource_quota(
1336 extended
.get("mem-quota"), "memory", extra_specs
1339 if extended
.get("vif-quota"):
1340 self
.process_resource_quota(
1341 extended
.get("vif-quota"), "vif", extra_specs
1344 if extended
.get("disk-io-quota"):
1345 self
.process_resource_quota(
1346 extended
.get("disk-io-quota"), "disk_io", extra_specs
1349 # Set the mempage size as specified in the descriptor
1350 if extended
.get("mempage-size"):
1351 if extended
.get("mempage-size") == "LARGE":
1352 extra_specs
["hw:mem_page_size"] = "large"
1353 elif extended
.get("mempage-size") == "SMALL":
1354 extra_specs
["hw:mem_page_size"] = "small"
1355 elif extended
.get("mempage-size") == "SIZE_2MB":
1356 extra_specs
["hw:mem_page_size"] = "2MB"
1357 elif extended
.get("mempage-size") == "SIZE_1GB":
1358 extra_specs
["hw:mem_page_size"] = "1GB"
1359 elif extended
.get("mempage-size") == "PREFER_LARGE":
1360 extra_specs
["hw:mem_page_size"] = "any"
1362 # The validations in NBI should make reaching here not possible.
1363 # If this message is shown, check validations
1365 "Invalid mempage-size %s. Will be ignored",
1366 extended
.get("mempage-size"),
1370 new_flavor
= self
.nova
.flavors
.create(
1374 disk
=flavor_data
.get("disk", 0),
1375 ephemeral
=flavor_data
.get("ephemeral", 0),
1376 swap
=flavor_data
.get("swap", 0),
1377 is_public
=flavor_data
.get("is_public", True),
1381 new_flavor
.set_keys(extra_specs
)
1383 return new_flavor
.id
1384 except nvExceptions
.Conflict
as e
:
1385 if change_name_if_used
and retry
< max_retries
:
1388 self
._format
_exception
(e
)
1389 # except nvExceptions.BadRequest as e:
1391 ksExceptions
.ClientException
,
1392 nvExceptions
.ClientException
,
1396 self
._format
_exception
(e
)
1398 def delete_flavor(self
, flavor_id
):
1399 """Deletes a tenant flavor from openstack VIM. Returns the old flavor_id"""
1401 self
._reload
_connection
()
1402 self
.nova
.flavors
.delete(flavor_id
)
1405 # except nvExceptions.BadRequest as e:
1407 nvExceptions
.NotFound
,
1408 ksExceptions
.ClientException
,
1409 nvExceptions
.ClientException
,
1412 self
._format
_exception
(e
)
1414 def new_image(self
, image_dict
):
1416 Adds a tenant image to VIM. imge_dict is a dictionary with:
1418 disk_format: qcow2, vhd, vmdk, raw (by default), ...
1419 location: path or URI
1420 public: "yes" or "no"
1421 metadata: metadata of the image
1422 Returns the image_id
1427 while retry
< max_retries
:
1430 self
._reload
_connection
()
1432 # determine format http://docs.openstack.org/developer/glance/formats.html
1433 if "disk_format" in image_dict
:
1434 disk_format
= image_dict
["disk_format"]
1435 else: # autodiscover based on extension
1436 if image_dict
["location"].endswith(".qcow2"):
1437 disk_format
= "qcow2"
1438 elif image_dict
["location"].endswith(".vhd"):
1440 elif image_dict
["location"].endswith(".vmdk"):
1441 disk_format
= "vmdk"
1442 elif image_dict
["location"].endswith(".vdi"):
1444 elif image_dict
["location"].endswith(".iso"):
1446 elif image_dict
["location"].endswith(".aki"):
1448 elif image_dict
["location"].endswith(".ari"):
1450 elif image_dict
["location"].endswith(".ami"):
1456 "new_image: '%s' loading from '%s'",
1458 image_dict
["location"],
1460 if self
.vim_type
== "VIO":
1461 container_format
= "bare"
1462 if "container_format" in image_dict
:
1463 container_format
= image_dict
["container_format"]
1465 new_image
= self
.glance
.images
.create(
1466 name
=image_dict
["name"],
1467 container_format
=container_format
,
1468 disk_format
=disk_format
,
1471 new_image
= self
.glance
.images
.create(name
=image_dict
["name"])
1473 if image_dict
["location"].startswith("http"):
1474 # TODO there is not a method to direct download. It must be downloaded locally with requests
1475 raise vimconn
.VimConnNotImplemented("Cannot create image from URL")
1477 with
open(image_dict
["location"]) as fimage
:
1478 self
.glance
.images
.upload(new_image
.id, fimage
)
1479 # new_image = self.glancev1.images.create(name=image_dict["name"], is_public=
1480 # image_dict.get("public","yes")=="yes",
1481 # container_format="bare", data=fimage, disk_format=disk_format)
1483 metadata_to_load
= image_dict
.get("metadata")
1485 # TODO location is a reserved word for current openstack versions. fixed for VIO please check
1487 if self
.vim_type
== "VIO":
1488 metadata_to_load
["upload_location"] = image_dict
["location"]
1490 metadata_to_load
["location"] = image_dict
["location"]
1492 self
.glance
.images
.update(new_image
.id, **metadata_to_load
)
1496 nvExceptions
.Conflict
,
1497 ksExceptions
.ClientException
,
1498 nvExceptions
.ClientException
,
1500 self
._format
_exception
(e
)
1503 gl1Exceptions
.HTTPException
,
1504 gl1Exceptions
.CommunicationError
,
1507 if retry
== max_retries
:
1510 self
._format
_exception
(e
)
1511 except IOError as e
: # can not open the file
1512 raise vimconn
.VimConnConnectionException(
1513 "{}: {} for {}".format(type(e
).__name
__, e
, image_dict
["location"]),
1514 http_code
=vimconn
.HTTP_Bad_Request
,
1517 def delete_image(self
, image_id
):
1518 """Deletes a tenant image from openstack VIM. Returns the old id"""
1520 self
._reload
_connection
()
1521 self
.glance
.images
.delete(image_id
)
1525 nvExceptions
.NotFound
,
1526 ksExceptions
.ClientException
,
1527 nvExceptions
.ClientException
,
1528 gl1Exceptions
.CommunicationError
,
1529 gl1Exceptions
.HTTPNotFound
,
1531 ) as e
: # TODO remove
1532 self
._format
_exception
(e
)
1534 def get_image_id_from_path(self
, path
):
1535 """Get the image id from image path in the VIM database. Returns the image_id"""
1537 self
._reload
_connection
()
1538 images
= self
.glance
.images
.list()
1540 for image
in images
:
1541 if image
.metadata
.get("location") == path
:
1544 raise vimconn
.VimConnNotFoundException(
1545 "image with location '{}' not found".format(path
)
1548 ksExceptions
.ClientException
,
1549 nvExceptions
.ClientException
,
1550 gl1Exceptions
.CommunicationError
,
1553 self
._format
_exception
(e
)
1555 def get_image_list(self
, filter_dict
={}):
1556 """Obtain tenant images from VIM
1560 checksum: image checksum
1561 Returns the image list of dictionaries:
1562 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1565 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
1568 self
._reload
_connection
()
1569 # filter_dict_os = filter_dict.copy()
1570 # First we filter by the available filter fields: name, id. The others are removed.
1571 image_list
= self
.glance
.images
.list()
1574 for image
in image_list
:
1576 if filter_dict
.get("name") and image
["name"] != filter_dict
["name"]:
1579 if filter_dict
.get("id") and image
["id"] != filter_dict
["id"]:
1583 filter_dict
.get("checksum")
1584 and image
["checksum"] != filter_dict
["checksum"]
1588 filtered_list
.append(image
.copy())
1589 except gl1Exceptions
.HTTPNotFound
:
1592 return filtered_list
1594 ksExceptions
.ClientException
,
1595 nvExceptions
.ClientException
,
1596 gl1Exceptions
.CommunicationError
,
1599 self
._format
_exception
(e
)
1601 def __wait_for_vm(self
, vm_id
, status
):
1602 """wait until vm is in the desired status and return True.
1603 If the VM gets in ERROR status, return false.
1604 If the timeout is reached generate an exception"""
1606 while elapsed_time
< server_timeout
:
1607 vm_status
= self
.nova
.servers
.get(vm_id
).status
1609 if vm_status
== status
:
1612 if vm_status
== "ERROR":
1618 # if we exceeded the timeout rollback
1619 if elapsed_time
>= server_timeout
:
1620 raise vimconn
.VimConnException(
1621 "Timeout waiting for instance " + vm_id
+ " to get " + status
,
1622 http_code
=vimconn
.HTTP_Request_Timeout
,
1625 def _get_openstack_availablity_zones(self
):
1627 Get from openstack availability zones available
1631 openstack_availability_zone
= self
.nova
.availability_zones
.list()
1632 openstack_availability_zone
= [
1634 for zone
in openstack_availability_zone
1635 if zone
.zoneName
!= "internal"
1638 return openstack_availability_zone
1642 def _set_availablity_zones(self
):
1644 Set vim availablity zone
1647 if "availability_zone" in self
.config
:
1648 vim_availability_zones
= self
.config
.get("availability_zone")
1650 if isinstance(vim_availability_zones
, str):
1651 self
.availability_zone
= [vim_availability_zones
]
1652 elif isinstance(vim_availability_zones
, list):
1653 self
.availability_zone
= vim_availability_zones
1655 self
.availability_zone
= self
._get
_openstack
_availablity
_zones
()
1657 def _get_vm_availability_zone(
1658 self
, availability_zone_index
, availability_zone_list
1661 Return thge availability zone to be used by the created VM.
1662 :return: The VIM availability zone to be used or None
1664 if availability_zone_index
is None:
1665 if not self
.config
.get("availability_zone"):
1667 elif isinstance(self
.config
.get("availability_zone"), str):
1668 return self
.config
["availability_zone"]
1670 # TODO consider using a different parameter at config for default AV and AV list match
1671 return self
.config
["availability_zone"][0]
1673 vim_availability_zones
= self
.availability_zone
1674 # check if VIM offer enough availability zones describe in the VNFD
1675 if vim_availability_zones
and len(availability_zone_list
) <= len(
1676 vim_availability_zones
1678 # check if all the names of NFV AV match VIM AV names
1679 match_by_index
= False
1680 for av
in availability_zone_list
:
1681 if av
not in vim_availability_zones
:
1682 match_by_index
= True
1686 return vim_availability_zones
[availability_zone_index
]
1688 return availability_zone_list
[availability_zone_index
]
1690 raise vimconn
.VimConnConflictException(
1691 "No enough availability zones at VIM for this deployment"
1701 affinity_group_list
,
1705 availability_zone_index
=None,
1706 availability_zone_list
=None,
1708 """Adds a VM instance to VIM
1710 start: indicates if VM must start or boot in pause mode. Ignored
1711 image_id,flavor_id: image and flavor uuid
1712 affinity_group_list: list of affinity groups, each one is a dictionary.
1714 net_list: list of interfaces, each one is a dictionary with:
1716 net_id: network uuid to connect
1717 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1718 model: interface model, ignored #TODO
1719 mac_address: used for SR-IOV ifaces #TODO for other types
1720 use: 'data', 'bridge', 'mgmt'
1721 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
1722 vim_id: filled/added by this function
1723 floating_ip: True/False (or it can be None)
1724 port_security: True/False
1725 'cloud_config': (optional) dictionary with:
1726 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1727 'users': (optional) list of users to be inserted, each item is a dict with:
1728 'name': (mandatory) user name,
1729 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1730 'user-data': (optional) string is a text script to be passed directly to cloud-init
1731 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1732 'dest': (mandatory) string with the destination absolute path
1733 'encoding': (optional, by default text). Can be one of:
1734 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1735 'content' (mandatory): string with the content of the file
1736 'permissions': (optional) string with file permissions, typically octal notation '0644'
1737 'owner': (optional) file owner, string with the format 'owner:group'
1738 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1739 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1740 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1741 'size': (mandatory) string with the size of the disk in GB
1742 'vim_id' (optional) should use this existing volume id
1743 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1744 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1745 availability_zone_index is None
1746 #TODO ip, security groups
1747 Returns a tuple with the instance identifier and created_items or raises an exception on error
1748 created_items can be None or a dictionary where this method can include key-values that will be passed to
1749 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1750 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1754 "new_vminstance input: image='%s' flavor='%s' nics='%s'",
1765 external_network
= []
1766 # ^list of external networks to be connected to instance, later on used to create floating_ip
1767 no_secured_ports
= [] # List of port-is with port-security disabled
1768 self
._reload
_connection
()
1769 # metadata_vpci = {} # For a specific neutron plugin
1770 block_device_mapping
= None
1772 for net
in net_list
:
1773 if not net
.get("net_id"): # skip non connected iface
1777 "network_id": net
["net_id"],
1778 "name": net
.get("name"),
1779 "admin_state_up": True,
1783 self
.config
.get("security_groups")
1784 and net
.get("port_security") is not False
1785 and not self
.config
.get("no_port_security_extension")
1787 if not self
.security_groups_id
:
1788 self
._get
_ids
_from
_name
()
1790 port_dict
["security_groups"] = self
.security_groups_id
1792 if net
["type"] == "virtual":
1795 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1796 elif net
["type"] == "VF" or net
["type"] == "SR-IOV": # for VF
1798 # if "VF" not in metadata_vpci:
1799 # metadata_vpci["VF"]=[]
1800 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1801 port_dict
["binding:vnic_type"] = "direct"
1803 # VIO specific Changes
1804 if self
.vim_type
== "VIO":
1805 # Need to create port with port_security_enabled = False and no-security-groups
1806 port_dict
["port_security_enabled"] = False
1807 port_dict
["provider_security_groups"] = []
1808 port_dict
["security_groups"] = []
1809 else: # For PT PCI-PASSTHROUGH
1811 # if "PF" not in metadata_vpci:
1812 # metadata_vpci["PF"]=[]
1813 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1814 port_dict
["binding:vnic_type"] = "direct-physical"
1816 if not port_dict
["name"]:
1817 port_dict
["name"] = name
1819 if net
.get("mac_address"):
1820 port_dict
["mac_address"] = net
["mac_address"]
1822 if net
.get("ip_address"):
1823 port_dict
["fixed_ips"] = [{"ip_address": net
["ip_address"]}]
1824 # TODO add "subnet_id": <subnet_id>
1826 new_port
= self
.neutron
.create_port({"port": port_dict
})
1827 created_items
["port:" + str(new_port
["port"]["id"])] = True
1828 net
["mac_adress"] = new_port
["port"]["mac_address"]
1829 net
["vim_id"] = new_port
["port"]["id"]
1830 # if try to use a network without subnetwork, it will return a emtpy list
1831 fixed_ips
= new_port
["port"].get("fixed_ips")
1834 net
["ip"] = fixed_ips
[0].get("ip_address")
1838 port
= {"port-id": new_port
["port"]["id"]}
1839 if float(self
.nova
.api_version
.get_string()) >= 2.32:
1840 port
["tag"] = new_port
["port"]["name"]
1842 net_list_vim
.append(port
)
1844 if net
.get("floating_ip", False):
1845 net
["exit_on_floating_ip_error"] = True
1846 external_network
.append(net
)
1847 elif net
["use"] == "mgmt" and self
.config
.get("use_floating_ip"):
1848 net
["exit_on_floating_ip_error"] = False
1849 external_network
.append(net
)
1850 net
["floating_ip"] = self
.config
.get("use_floating_ip")
1852 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic
1854 # As a workaround we wait until the VM is active and then disable the port-security
1855 if net
.get("port_security") is False and not self
.config
.get(
1856 "no_port_security_extension"
1858 no_secured_ports
.append(
1860 new_port
["port"]["id"],
1861 net
.get("port_security_disable_strategy"),
1866 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1867 # if len(metadata["pci_assignement"]) >255:
1868 # #limit the metadata size
1869 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1870 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1874 "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1883 config_drive
, userdata
= self
._create
_user
_data
(cloud_config
)
1885 # get availability Zone
1886 vm_av_zone
= self
._get
_vm
_availability
_zone
(
1887 availability_zone_index
, availability_zone_list
1890 # Create additional volumes in case these are present in disk_list
1891 base_disk_index
= ord("b")
1892 boot_volume_id
= None
1894 block_device_mapping
= {}
1895 for disk
in disk_list
:
1896 if disk
.get("vim_id"):
1897 block_device_mapping
["_vd" + chr(base_disk_index
)] = disk
[
1901 if "image_id" in disk
:
1902 base_disk_index
= ord("a")
1903 volume
= self
.cinder
.volumes
.create(
1905 name
=name
+ "_vd" + chr(base_disk_index
),
1906 imageRef
=disk
["image_id"],
1907 # Make sure volume is in the same AZ as the VM to be attached to
1908 availability_zone
=vm_av_zone
,
1910 boot_volume_id
= volume
.id
1912 volume
= self
.cinder
.volumes
.create(
1914 name
=name
+ "_vd" + chr(base_disk_index
),
1915 # Make sure volume is in the same AZ as the VM to be attached to
1916 availability_zone
=vm_av_zone
,
1919 created_items
["volume:" + str(volume
.id)] = True
1920 block_device_mapping
["_vd" + chr(base_disk_index
)] = volume
.id
1922 base_disk_index
+= 1
1924 # Wait until created volumes are with status available
1926 while elapsed_time
< volume_timeout
:
1927 for created_item
in created_items
:
1928 v
, _
, volume_id
= created_item
.partition(":")
1930 if self
.cinder
.volumes
.get(volume_id
).status
!= "available":
1932 else: # all ready: break from while
1938 # If we exceeded the timeout rollback
1939 if elapsed_time
>= volume_timeout
:
1940 raise vimconn
.VimConnException(
1941 "Timeout creating volumes for instance " + name
,
1942 http_code
=vimconn
.HTTP_Request_Timeout
,
1945 self
.cinder
.volumes
.set_bootable(boot_volume_id
, True)
1947 # Manage affinity groups/server groups
1948 server_group_id
= None
1949 scheduller_hints
= {}
1951 if affinity_group_list
:
1952 # Only first id on the list will be used. Openstack restriction
1953 server_group_id
= affinity_group_list
[0]["affinity_group_id"]
1954 scheduller_hints
["group"] = server_group_id
1957 "nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1958 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1959 "block_device_mapping={}, server_group={})".format(
1964 self
.config
.get("security_groups"),
1966 self
.config
.get("keypair"),
1969 block_device_mapping
,
1973 server
= self
.nova
.servers
.create(
1978 security_groups
=self
.config
.get("security_groups"),
1979 # TODO remove security_groups in future versions. Already at neutron port
1980 availability_zone
=vm_av_zone
,
1981 key_name
=self
.config
.get("keypair"),
1983 config_drive
=config_drive
,
1984 block_device_mapping
=block_device_mapping
,
1985 scheduler_hints
=scheduller_hints
,
1986 ) # , description=description)
1988 vm_start_time
= time
.time()
1989 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1990 if no_secured_ports
:
1991 self
.__wait
_for
_vm
(server
.id, "ACTIVE")
1993 for port
in no_secured_ports
:
1995 "port": {"port_security_enabled": False, "security_groups": None}
1998 if port
[1] == "allow-address-pairs":
2000 "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}
2004 self
.neutron
.update_port(port
[0], port_update
)
2006 raise vimconn
.VimConnException(
2007 "It was not possible to disable port security for port {}".format(
2012 # print "DONE :-)", server
2015 for floating_network
in external_network
:
2018 floating_ip_retries
= 3
2019 # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry
2022 floating_ips
= self
.neutron
.list_floatingips().get(
2025 random
.shuffle(floating_ips
) # randomize
2026 for fip
in floating_ips
:
2029 or fip
.get("tenant_id") != server
.tenant_id
2033 if isinstance(floating_network
["floating_ip"], str):
2035 fip
.get("floating_network_id")
2036 != floating_network
["floating_ip"]
2040 free_floating_ip
= fip
["id"]
2044 isinstance(floating_network
["floating_ip"], str)
2045 and floating_network
["floating_ip"].lower() != "true"
2047 pool_id
= floating_network
["floating_ip"]
2049 # Find the external network
2050 external_nets
= list()
2052 for net
in self
.neutron
.list_networks()["networks"]:
2053 if net
["router:external"]:
2054 external_nets
.append(net
)
2056 if len(external_nets
) == 0:
2057 raise vimconn
.VimConnException(
2058 "Cannot create floating_ip automatically since "
2059 "no external network is present",
2060 http_code
=vimconn
.HTTP_Conflict
,
2063 if len(external_nets
) > 1:
2064 raise vimconn
.VimConnException(
2065 "Cannot create floating_ip automatically since "
2066 "multiple external networks are present",
2067 http_code
=vimconn
.HTTP_Conflict
,
2070 pool_id
= external_nets
[0].get("id")
2074 "floating_network_id": pool_id
,
2075 "tenant_id": server
.tenant_id
,
2080 # self.logger.debug("Creating floating IP")
2081 new_floating_ip
= self
.neutron
.create_floatingip(param
)
2082 free_floating_ip
= new_floating_ip
["floatingip"]["id"]
2084 "floating_ip:" + str(free_floating_ip
)
2086 except Exception as e
:
2087 raise vimconn
.VimConnException(
2089 + ": Cannot create new floating_ip "
2091 http_code
=vimconn
.HTTP_Conflict
,
2095 # for race condition ensure not already assigned
2096 fip
= self
.neutron
.show_floatingip(free_floating_ip
)
2098 if fip
["floatingip"]["port_id"]:
2101 # the vim_id key contains the neutron.port_id
2102 self
.neutron
.update_floatingip(
2104 {"floatingip": {"port_id": floating_network
["vim_id"]}},
2106 # for race condition ensure not re-assigned to other VM after 5 seconds
2108 fip
= self
.neutron
.show_floatingip(free_floating_ip
)
2111 fip
["floatingip"]["port_id"]
2112 != floating_network
["vim_id"]
2115 "floating_ip {} re-assigned to other port".format(
2122 "Assigned floating_ip {} to VM {}".format(
2123 free_floating_ip
, server
.id
2127 except Exception as e
:
2128 # openstack need some time after VM creation to assign an IP. So retry if fails
2129 vm_status
= self
.nova
.servers
.get(server
.id).status
2131 if vm_status
not in ("ACTIVE", "ERROR"):
2132 if time
.time() - vm_start_time
< server_timeout
:
2135 elif floating_ip_retries
> 0:
2136 floating_ip_retries
-= 1
2139 raise vimconn
.VimConnException(
2140 "Cannot create floating_ip: {} {}".format(
2143 http_code
=vimconn
.HTTP_Conflict
,
2146 except Exception as e
:
2147 if not floating_network
["exit_on_floating_ip_error"]:
2148 self
.logger
.error("Cannot create floating_ip. %s", str(e
))
2153 return server
.id, created_items
2154 # except nvExceptions.NotFound as e:
2155 # error_value=-vimconn.HTTP_Not_Found
2156 # error_text= "vm instance %s not found" % vm_id
2157 # except TypeError as e:
2158 # raise vimconn.VimConnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
2160 except Exception as e
:
2163 server_id
= server
.id
2166 self
.delete_vminstance(server_id
, created_items
)
2167 except Exception as e2
:
2168 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
2170 self
._format
_exception
(e
)
2172 def get_vminstance(self
, vm_id
):
2173 """Returns the VM instance information from VIM"""
2174 # self.logger.debug("Getting VM from VIM")
2176 self
._reload
_connection
()
2177 server
= self
.nova
.servers
.find(id=vm_id
)
2178 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
2180 return server
.to_dict()
2182 ksExceptions
.ClientException
,
2183 nvExceptions
.ClientException
,
2184 nvExceptions
.NotFound
,
2187 self
._format
_exception
(e
)
2189 def get_vminstance_console(self
, vm_id
, console_type
="vnc"):
2191 Get a console for the virtual machine
2193 vm_id: uuid of the VM
2194 console_type, can be:
2195 "novnc" (by default), "xvpvnc" for VNC types,
2196 "rdp-html5" for RDP types, "spice-html5" for SPICE types
2197 Returns dict with the console parameters:
2198 protocol: ssh, ftp, http, https, ...
2199 server: usually ip address
2200 port: the http, ssh, ... port
2201 suffix: extra text, e.g. the http path and query string
2203 self
.logger
.debug("Getting VM CONSOLE from VIM")
2206 self
._reload
_connection
()
2207 server
= self
.nova
.servers
.find(id=vm_id
)
2209 if console_type
is None or console_type
== "novnc":
2210 console_dict
= server
.get_vnc_console("novnc")
2211 elif console_type
== "xvpvnc":
2212 console_dict
= server
.get_vnc_console(console_type
)
2213 elif console_type
== "rdp-html5":
2214 console_dict
= server
.get_rdp_console(console_type
)
2215 elif console_type
== "spice-html5":
2216 console_dict
= server
.get_spice_console(console_type
)
2218 raise vimconn
.VimConnException(
2219 "console type '{}' not allowed".format(console_type
),
2220 http_code
=vimconn
.HTTP_Bad_Request
,
2223 console_dict1
= console_dict
.get("console")
2226 console_url
= console_dict1
.get("url")
2230 protocol_index
= console_url
.find("//")
2232 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
2235 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
2240 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
2242 -vimconn
.HTTP_Internal_Server_Error
,
2243 "Unexpected response from VIM",
2247 "protocol": console_url
[0:protocol_index
],
2248 "server": console_url
[protocol_index
+ 2 : port_index
],
2249 "port": console_url
[port_index
:suffix_index
],
2250 "suffix": console_url
[suffix_index
+ 1 :],
2255 raise vimconn
.VimConnUnexpectedResponse("Unexpected response from VIM")
2257 nvExceptions
.NotFound
,
2258 ksExceptions
.ClientException
,
2259 nvExceptions
.ClientException
,
2260 nvExceptions
.BadRequest
,
2263 self
._format
_exception
(e
)
2265 def delete_vminstance(self
, vm_id
, created_items
=None):
2266 """Removes a VM instance from VIM. Returns the old identifier"""
2267 # print "osconnector: Getting VM from VIM"
2268 if created_items
is None:
2272 self
._reload
_connection
()
2273 # delete VM ports attached to this networks before the virtual machine
2274 for k
, v
in created_items
.items():
2275 if not v
: # skip already deleted
2279 k_item
, _
, k_id
= k
.partition(":")
2280 if k_item
== "port":
2281 self
.neutron
.delete_port(k_id
)
2282 except Exception as e
:
2284 "Error deleting port: {}: {}".format(type(e
).__name
__, e
)
2287 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
2288 # #dettach volumes attached
2289 # server = self.nova.servers.get(vm_id)
2290 # volumes_attached_dict = server._info["os-extended-volumes:volumes_attached"] #volume["id"]
2291 # #for volume in volumes_attached_dict:
2292 # # self.cinder.volumes.detach(volume["id"])
2295 self
.nova
.servers
.delete(vm_id
)
2297 # delete volumes. Although having detached, they should have in active status before deleting
2298 # we ensure in this loop
2302 while keep_waiting
and elapsed_time
< volume_timeout
:
2303 keep_waiting
= False
2305 for k
, v
in created_items
.items():
2306 if not v
: # skip already deleted
2310 k_item
, _
, k_id
= k
.partition(":")
2311 if k_item
== "volume":
2312 if self
.cinder
.volumes
.get(k_id
).status
!= "available":
2315 self
.cinder
.volumes
.delete(k_id
)
2316 created_items
[k
] = None
2317 elif k_item
== "floating_ip": # floating ip
2318 self
.neutron
.delete_floatingip(k_id
)
2319 created_items
[k
] = None
2321 except Exception as e
:
2322 self
.logger
.error("Error deleting {}: {}".format(k
, e
))
2330 nvExceptions
.NotFound
,
2331 ksExceptions
.ClientException
,
2332 nvExceptions
.ClientException
,
2335 self
._format
_exception
(e
)
2337 def refresh_vms_status(self
, vm_list
):
2338 """Get the status of the virtual machines and their interfaces/ports
2339 Params: the list of VM identifiers
2340 Returns a dictionary with:
2341 vm_id: #VIM id of this Virtual Machine
2342 status: #Mandatory. Text with one of:
2343 # DELETED (not found at vim)
2344 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
2345 # OTHER (Vim reported other status not understood)
2346 # ERROR (VIM indicates an ERROR status)
2347 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
2348 # CREATING (on building process), ERROR
2349 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2351 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
2352 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2354 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2355 mac_address: #Text format XX:XX:XX:XX:XX:XX
2356 vim_net_id: #network id where this interface is connected
2357 vim_interface_id: #interface/port VIM id
2358 ip_address: #null, or text with IPv4, IPv6 address
2359 compute_node: #identification of compute node where PF,VF interface is allocated
2360 pci: #PCI address of the NIC that hosts the PF,VF
2361 vlan: #physical VLAN used for VF
2365 "refresh_vms status: Getting tenant VM instance information from VIM"
2368 for vm_id
in vm_list
:
2372 vm_vim
= self
.get_vminstance(vm_id
)
2374 if vm_vim
["status"] in vmStatus2manoFormat
:
2375 vm
["status"] = vmStatus2manoFormat
[vm_vim
["status"]]
2377 vm
["status"] = "OTHER"
2378 vm
["error_msg"] = "VIM status reported " + vm_vim
["status"]
2380 vm_vim
.pop("OS-EXT-SRV-ATTR:user_data", None)
2381 vm_vim
.pop("user_data", None)
2382 vm
["vim_info"] = self
.serialize(vm_vim
)
2384 vm
["interfaces"] = []
2385 if vm_vim
.get("fault"):
2386 vm
["error_msg"] = str(vm_vim
["fault"])
2390 self
._reload
_connection
()
2391 port_dict
= self
.neutron
.list_ports(device_id
=vm_id
)
2393 for port
in port_dict
["ports"]:
2395 interface
["vim_info"] = self
.serialize(port
)
2396 interface
["mac_address"] = port
.get("mac_address")
2397 interface
["vim_net_id"] = port
["network_id"]
2398 interface
["vim_interface_id"] = port
["id"]
2399 # check if OS-EXT-SRV-ATTR:host is there,
2400 # in case of non-admin credentials, it will be missing
2402 if vm_vim
.get("OS-EXT-SRV-ATTR:host"):
2403 interface
["compute_node"] = vm_vim
["OS-EXT-SRV-ATTR:host"]
2405 interface
["pci"] = None
2407 # check if binding:profile is there,
2408 # in case of non-admin credentials, it will be missing
2409 if port
.get("binding:profile"):
2410 if port
["binding:profile"].get("pci_slot"):
2411 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting
2413 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
2414 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
2415 pci
= port
["binding:profile"]["pci_slot"]
2416 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
2417 interface
["pci"] = pci
2419 interface
["vlan"] = None
2421 if port
.get("binding:vif_details"):
2422 interface
["vlan"] = port
["binding:vif_details"].get("vlan")
2424 # Get vlan from network in case not present in port for those old openstacks and cases where
2425 # it is needed vlan at PT
2426 if not interface
["vlan"]:
2427 # if network is of type vlan and port is of type direct (sr-iov) then set vlan id
2428 network
= self
.neutron
.show_network(port
["network_id"])
2431 network
["network"].get("provider:network_type")
2434 # and port.get("binding:vnic_type") in ("direct", "direct-physical"):
2435 interface
["vlan"] = network
["network"].get(
2436 "provider:segmentation_id"
2440 # look for floating ip address
2442 floating_ip_dict
= self
.neutron
.list_floatingips(
2446 if floating_ip_dict
.get("floatingips"):
2448 floating_ip_dict
["floatingips"][0].get(
2449 "floating_ip_address"
2455 for subnet
in port
["fixed_ips"]:
2456 ips
.append(subnet
["ip_address"])
2458 interface
["ip_address"] = ";".join(ips
)
2459 vm
["interfaces"].append(interface
)
2460 except Exception as e
:
2462 "Error getting vm interface information {}: {}".format(
2467 except vimconn
.VimConnNotFoundException
as e
:
2468 self
.logger
.error("Exception getting vm status: %s", str(e
))
2469 vm
["status"] = "DELETED"
2470 vm
["error_msg"] = str(e
)
2471 except vimconn
.VimConnException
as e
:
2472 self
.logger
.error("Exception getting vm status: %s", str(e
))
2473 vm
["status"] = "VIM_ERROR"
2474 vm
["error_msg"] = str(e
)
2480 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
2481 """Send and action over a VM instance from VIM
2482 Returns None or the console dict if the action was successfully sent to the VIM
2484 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
2487 self
._reload
_connection
()
2488 server
= self
.nova
.servers
.find(id=vm_id
)
2490 if "start" in action_dict
:
2491 if action_dict
["start"] == "rebuild":
2494 if server
.status
== "PAUSED":
2496 elif server
.status
== "SUSPENDED":
2498 elif server
.status
== "SHUTOFF":
2500 elif "pause" in action_dict
:
2502 elif "resume" in action_dict
:
2504 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
2506 elif "forceOff" in action_dict
:
2507 server
.stop() # TODO
2508 elif "terminate" in action_dict
:
2510 elif "createImage" in action_dict
:
2511 server
.create_image()
2512 # "path":path_schema,
2513 # "description":description_schema,
2514 # "name":name_schema,
2515 # "metadata":metadata_schema,
2516 # "imageRef": id_schema,
2517 # "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
2518 elif "rebuild" in action_dict
:
2519 server
.rebuild(server
.image
["id"])
2520 elif "reboot" in action_dict
:
2521 server
.reboot() # reboot_type="SOFT"
2522 elif "console" in action_dict
:
2523 console_type
= action_dict
["console"]
2525 if console_type
is None or console_type
== "novnc":
2526 console_dict
= server
.get_vnc_console("novnc")
2527 elif console_type
== "xvpvnc":
2528 console_dict
= server
.get_vnc_console(console_type
)
2529 elif console_type
== "rdp-html5":
2530 console_dict
= server
.get_rdp_console(console_type
)
2531 elif console_type
== "spice-html5":
2532 console_dict
= server
.get_spice_console(console_type
)
2534 raise vimconn
.VimConnException(
2535 "console type '{}' not allowed".format(console_type
),
2536 http_code
=vimconn
.HTTP_Bad_Request
,
2540 console_url
= console_dict
["console"]["url"]
2542 protocol_index
= console_url
.find("//")
2544 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
2547 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
2552 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
2553 raise vimconn
.VimConnException(
2554 "Unexpected response from VIM " + str(console_dict
)
2558 "protocol": console_url
[0:protocol_index
],
2559 "server": console_url
[protocol_index
+ 2 : port_index
],
2560 "port": int(console_url
[port_index
+ 1 : suffix_index
]),
2561 "suffix": console_url
[suffix_index
+ 1 :],
2564 return console_dict2
2566 raise vimconn
.VimConnException(
2567 "Unexpected response from VIM " + str(console_dict
)
2572 ksExceptions
.ClientException
,
2573 nvExceptions
.ClientException
,
2574 nvExceptions
.NotFound
,
2577 self
._format
_exception
(e
)
2578 # TODO insert exception vimconn.HTTP_Unauthorized
2580 # ###### VIO Specific Changes #########
2581 def _generate_vlanID(self
):
2583 Method to get unused vlanID
2591 networks
= self
.get_network_list()
2593 for net
in networks
:
2594 if net
.get("provider:segmentation_id"):
2595 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
2597 used_vlanIDs
= set(usedVlanIDs
)
2599 # find unused VLAN ID
2600 for vlanID_range
in self
.config
.get("dataplane_net_vlan_range"):
2602 start_vlanid
, end_vlanid
= map(
2603 int, vlanID_range
.replace(" ", "").split("-")
2606 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
2607 if vlanID
not in used_vlanIDs
:
2609 except Exception as exp
:
2610 raise vimconn
.VimConnException(
2611 "Exception {} occurred while generating VLAN ID.".format(exp
)
2614 raise vimconn
.VimConnConflictException(
2615 "Unable to create the SRIOV VLAN network. All given Vlan IDs {} are in use.".format(
2616 self
.config
.get("dataplane_net_vlan_range")
2620 def _generate_multisegment_vlanID(self
):
2622 Method to get unused vlanID
2630 networks
= self
.get_network_list()
2631 for net
in networks
:
2632 if net
.get("provider:network_type") == "vlan" and net
.get(
2633 "provider:segmentation_id"
2635 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
2636 elif net
.get("segments"):
2637 for segment
in net
.get("segments"):
2638 if segment
.get("provider:network_type") == "vlan" and segment
.get(
2639 "provider:segmentation_id"
2641 usedVlanIDs
.append(segment
.get("provider:segmentation_id"))
2643 used_vlanIDs
= set(usedVlanIDs
)
2645 # find unused VLAN ID
2646 for vlanID_range
in self
.config
.get("multisegment_vlan_range"):
2648 start_vlanid
, end_vlanid
= map(
2649 int, vlanID_range
.replace(" ", "").split("-")
2652 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
2653 if vlanID
not in used_vlanIDs
:
2655 except Exception as exp
:
2656 raise vimconn
.VimConnException(
2657 "Exception {} occurred while generating VLAN ID.".format(exp
)
2660 raise vimconn
.VimConnConflictException(
2661 "Unable to create the VLAN segment. All VLAN IDs {} are in use.".format(
2662 self
.config
.get("multisegment_vlan_range")
2666 def _validate_vlan_ranges(self
, input_vlan_range
, text_vlan_range
):
2668 Method to validate user given vlanID ranges
2672 for vlanID_range
in input_vlan_range
:
2673 vlan_range
= vlanID_range
.replace(" ", "")
2675 vlanID_pattern
= r
"(\d)*-(\d)*$"
2676 match_obj
= re
.match(vlanID_pattern
, vlan_range
)
2678 raise vimconn
.VimConnConflictException(
2679 "Invalid VLAN range for {}: {}.You must provide "
2680 "'{}' in format [start_ID - end_ID].".format(
2681 text_vlan_range
, vlanID_range
, text_vlan_range
2685 start_vlanid
, end_vlanid
= map(int, vlan_range
.split("-"))
2686 if start_vlanid
<= 0:
2687 raise vimconn
.VimConnConflictException(
2688 "Invalid VLAN range for {}: {}. Start ID can not be zero. For VLAN "
2689 "networks valid IDs are 1 to 4094 ".format(
2690 text_vlan_range
, vlanID_range
2694 if end_vlanid
> 4094:
2695 raise vimconn
.VimConnConflictException(
2696 "Invalid VLAN range for {}: {}. End VLAN ID can not be "
2697 "greater than 4094. For VLAN networks valid IDs are 1 to 4094 ".format(
2698 text_vlan_range
, vlanID_range
2702 if start_vlanid
> end_vlanid
:
2703 raise vimconn
.VimConnConflictException(
2704 "Invalid VLAN range for {}: {}. You must provide '{}'"
2705 " in format start_ID - end_ID and start_ID < end_ID ".format(
2706 text_vlan_range
, vlanID_range
, text_vlan_range
2710 # NOT USED FUNCTIONS
2712 def new_external_port(self
, port_data
):
2713 """Adds a external port to VIM
2714 Returns the port identifier"""
2715 # TODO openstack if needed
2717 -vimconn
.HTTP_Internal_Server_Error
,
2718 "osconnector.new_external_port() not implemented",
2721 def connect_port_network(self
, port_id
, network_id
, admin
=False):
2722 """Connects a external port to a network
2723 Returns status code of the VIM response"""
2724 # TODO openstack if needed
2726 -vimconn
.HTTP_Internal_Server_Error
,
2727 "osconnector.connect_port_network() not implemented",
2730 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
2731 """Adds a new user to openstack VIM
2732 Returns the user identifier"""
2733 self
.logger
.debug("osconnector: Adding a new user to VIM")
2736 self
._reload
_connection
()
2737 user
= self
.keystone
.users
.create(
2738 user_name
, password
=user_passwd
, default_project
=tenant_id
2740 # self.keystone.tenants.add_user(self.k_creds["username"], #role)
2743 except ksExceptions
.ConnectionError
as e
:
2744 error_value
= -vimconn
.HTTP_Bad_Request
2748 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2750 except ksExceptions
.ClientException
as e
: # TODO remove
2751 error_value
= -vimconn
.HTTP_Bad_Request
2755 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2758 # TODO insert exception vimconn.HTTP_Unauthorized
2759 # if reaching here is because an exception
2760 self
.logger
.debug("new_user " + error_text
)
2762 return error_value
, error_text
2764 def delete_user(self
, user_id
):
2765 """Delete a user from openstack VIM
2766 Returns the user identifier"""
2768 print("osconnector: Deleting a user from VIM")
2771 self
._reload
_connection
()
2772 self
.keystone
.users
.delete(user_id
)
2775 except ksExceptions
.ConnectionError
as e
:
2776 error_value
= -vimconn
.HTTP_Bad_Request
2780 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2782 except ksExceptions
.NotFound
as e
:
2783 error_value
= -vimconn
.HTTP_Not_Found
2787 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2789 except ksExceptions
.ClientException
as e
: # TODO remove
2790 error_value
= -vimconn
.HTTP_Bad_Request
2794 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2797 # TODO insert exception vimconn.HTTP_Unauthorized
2798 # if reaching here is because an exception
2799 self
.logger
.debug("delete_tenant " + error_text
)
2801 return error_value
, error_text
2803 def get_hosts_info(self
):
2804 """Get the information of deployed hosts
2805 Returns the hosts content"""
2807 print("osconnector: Getting Host info from VIM")
2811 self
._reload
_connection
()
2812 hypervisors
= self
.nova
.hypervisors
.list()
2814 for hype
in hypervisors
:
2815 h_list
.append(hype
.to_dict())
2817 return 1, {"hosts": h_list
}
2818 except nvExceptions
.NotFound
as e
:
2819 error_value
= -vimconn
.HTTP_Not_Found
2820 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
2821 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
2822 error_value
= -vimconn
.HTTP_Bad_Request
2826 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2829 # TODO insert exception vimconn.HTTP_Unauthorized
2830 # if reaching here is because an exception
2831 self
.logger
.debug("get_hosts_info " + error_text
)
2833 return error_value
, error_text
2835 def get_hosts(self
, vim_tenant
):
2836 """Get the hosts and deployed instances
2837 Returns the hosts content"""
2838 r
, hype_dict
= self
.get_hosts_info()
2843 hypervisors
= hype_dict
["hosts"]
2846 servers
= self
.nova
.servers
.list()
2847 for hype
in hypervisors
:
2848 for server
in servers
:
2850 server
.to_dict()["OS-EXT-SRV-ATTR:hypervisor_hostname"]
2851 == hype
["hypervisor_hostname"]
2854 hype
["vm"].append(server
.id)
2856 hype
["vm"] = [server
.id]
2859 except nvExceptions
.NotFound
as e
:
2860 error_value
= -vimconn
.HTTP_Not_Found
2861 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
2862 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
2863 error_value
= -vimconn
.HTTP_Bad_Request
2867 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2870 # TODO insert exception vimconn.HTTP_Unauthorized
2871 # if reaching here is because an exception
2872 self
.logger
.debug("get_hosts " + error_text
)
2874 return error_value
, error_text
2876 def new_classification(self
, name
, ctype
, definition
):
2878 "Adding a new (Traffic) Classification to VIM, named %s", name
2883 self
._reload
_connection
()
2885 if ctype
not in supportedClassificationTypes
:
2886 raise vimconn
.VimConnNotSupportedException(
2887 "OpenStack VIM connector does not support provided "
2888 "Classification Type {}, supported ones are: {}".format(
2889 ctype
, supportedClassificationTypes
2893 if not self
._validate
_classification
(ctype
, definition
):
2894 raise vimconn
.VimConnException(
2895 "Incorrect Classification definition for the type specified."
2898 classification_dict
= definition
2899 classification_dict
["name"] = name
2900 new_class
= self
.neutron
.create_sfc_flow_classifier(
2901 {"flow_classifier": classification_dict
}
2904 return new_class
["flow_classifier"]["id"]
2906 neExceptions
.ConnectionFailed
,
2907 ksExceptions
.ClientException
,
2908 neExceptions
.NeutronException
,
2911 self
.logger
.error("Creation of Classification failed.")
2912 self
._format
_exception
(e
)
2914 def get_classification(self
, class_id
):
2915 self
.logger
.debug(" Getting Classification %s from VIM", class_id
)
2916 filter_dict
= {"id": class_id
}
2917 class_list
= self
.get_classification_list(filter_dict
)
2919 if len(class_list
) == 0:
2920 raise vimconn
.VimConnNotFoundException(
2921 "Classification '{}' not found".format(class_id
)
2923 elif len(class_list
) > 1:
2924 raise vimconn
.VimConnConflictException(
2925 "Found more than one Classification with this criteria"
2928 classification
= class_list
[0]
2930 return classification
2932 def get_classification_list(self
, filter_dict
={}):
2934 "Getting Classifications from VIM filter: '%s'", str(filter_dict
)
2938 filter_dict_os
= filter_dict
.copy()
2939 self
._reload
_connection
()
2941 if self
.api_version3
and "tenant_id" in filter_dict_os
:
2942 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
2944 classification_dict
= self
.neutron
.list_sfc_flow_classifiers(
2947 classification_list
= classification_dict
["flow_classifiers"]
2948 self
.__classification
_os
2mano
(classification_list
)
2950 return classification_list
2952 neExceptions
.ConnectionFailed
,
2953 ksExceptions
.ClientException
,
2954 neExceptions
.NeutronException
,
2957 self
._format
_exception
(e
)
2959 def delete_classification(self
, class_id
):
2960 self
.logger
.debug("Deleting Classification '%s' from VIM", class_id
)
2963 self
._reload
_connection
()
2964 self
.neutron
.delete_sfc_flow_classifier(class_id
)
2968 neExceptions
.ConnectionFailed
,
2969 neExceptions
.NeutronException
,
2970 ksExceptions
.ClientException
,
2971 neExceptions
.NeutronException
,
2974 self
._format
_exception
(e
)
2976 def new_sfi(self
, name
, ingress_ports
, egress_ports
, sfc_encap
=True):
2978 "Adding a new Service Function Instance to VIM, named '%s'", name
2983 self
._reload
_connection
()
2989 if len(ingress_ports
) != 1:
2990 raise vimconn
.VimConnNotSupportedException(
2991 "OpenStack VIM connector can only have 1 ingress port per SFI"
2994 if len(egress_ports
) != 1:
2995 raise vimconn
.VimConnNotSupportedException(
2996 "OpenStack VIM connector can only have 1 egress port per SFI"
3001 "ingress": ingress_ports
[0],
3002 "egress": egress_ports
[0],
3003 "service_function_parameters": {"correlation": correlation
},
3005 new_sfi
= self
.neutron
.create_sfc_port_pair({"port_pair": sfi_dict
})
3007 return new_sfi
["port_pair"]["id"]
3009 neExceptions
.ConnectionFailed
,
3010 ksExceptions
.ClientException
,
3011 neExceptions
.NeutronException
,
3016 self
.neutron
.delete_sfc_port_pair(new_sfi
["port_pair"]["id"])
3019 "Creation of Service Function Instance failed, with "
3020 "subsequent deletion failure as well."
3023 self
._format
_exception
(e
)
3025 def get_sfi(self
, sfi_id
):
3026 self
.logger
.debug("Getting Service Function Instance %s from VIM", sfi_id
)
3027 filter_dict
= {"id": sfi_id
}
3028 sfi_list
= self
.get_sfi_list(filter_dict
)
3030 if len(sfi_list
) == 0:
3031 raise vimconn
.VimConnNotFoundException(
3032 "Service Function Instance '{}' not found".format(sfi_id
)
3034 elif len(sfi_list
) > 1:
3035 raise vimconn
.VimConnConflictException(
3036 "Found more than one Service Function Instance with this criteria"
3043 def get_sfi_list(self
, filter_dict
={}):
3045 "Getting Service Function Instances from VIM filter: '%s'", str(filter_dict
)
3049 self
._reload
_connection
()
3050 filter_dict_os
= filter_dict
.copy()
3052 if self
.api_version3
and "tenant_id" in filter_dict_os
:
3053 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
3055 sfi_dict
= self
.neutron
.list_sfc_port_pairs(**filter_dict_os
)
3056 sfi_list
= sfi_dict
["port_pairs"]
3057 self
.__sfi
_os
2mano
(sfi_list
)
3061 neExceptions
.ConnectionFailed
,
3062 ksExceptions
.ClientException
,
3063 neExceptions
.NeutronException
,
3066 self
._format
_exception
(e
)
3068 def delete_sfi(self
, sfi_id
):
3069 self
.logger
.debug("Deleting Service Function Instance '%s' from VIM", sfi_id
)
3072 self
._reload
_connection
()
3073 self
.neutron
.delete_sfc_port_pair(sfi_id
)
3077 neExceptions
.ConnectionFailed
,
3078 neExceptions
.NeutronException
,
3079 ksExceptions
.ClientException
,
3080 neExceptions
.NeutronException
,
3083 self
._format
_exception
(e
)
3085 def new_sf(self
, name
, sfis
, sfc_encap
=True):
3086 self
.logger
.debug("Adding a new Service Function to VIM, named '%s'", name
)
3090 self
._reload
_connection
()
3091 # correlation = None
3093 # correlation = "nsh"
3095 for instance
in sfis
:
3096 sfi
= self
.get_sfi(instance
)
3098 if sfi
.get("sfc_encap") != sfc_encap
:
3099 raise vimconn
.VimConnNotSupportedException(
3100 "OpenStack VIM connector requires all SFIs of the "
3101 "same SF to share the same SFC Encapsulation"
3104 sf_dict
= {"name": name
, "port_pairs": sfis
}
3105 new_sf
= self
.neutron
.create_sfc_port_pair_group(
3106 {"port_pair_group": sf_dict
}
3109 return new_sf
["port_pair_group"]["id"]
3111 neExceptions
.ConnectionFailed
,
3112 ksExceptions
.ClientException
,
3113 neExceptions
.NeutronException
,
3118 self
.neutron
.delete_sfc_port_pair_group(
3119 new_sf
["port_pair_group"]["id"]
3123 "Creation of Service Function failed, with "
3124 "subsequent deletion failure as well."
3127 self
._format
_exception
(e
)
3129 def get_sf(self
, sf_id
):
3130 self
.logger
.debug("Getting Service Function %s from VIM", sf_id
)
3131 filter_dict
= {"id": sf_id
}
3132 sf_list
= self
.get_sf_list(filter_dict
)
3134 if len(sf_list
) == 0:
3135 raise vimconn
.VimConnNotFoundException(
3136 "Service Function '{}' not found".format(sf_id
)
3138 elif len(sf_list
) > 1:
3139 raise vimconn
.VimConnConflictException(
3140 "Found more than one Service Function with this criteria"
3147 def get_sf_list(self
, filter_dict
={}):
3149 "Getting Service Function from VIM filter: '%s'", str(filter_dict
)
3153 self
._reload
_connection
()
3154 filter_dict_os
= filter_dict
.copy()
3156 if self
.api_version3
and "tenant_id" in filter_dict_os
:
3157 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
3159 sf_dict
= self
.neutron
.list_sfc_port_pair_groups(**filter_dict_os
)
3160 sf_list
= sf_dict
["port_pair_groups"]
3161 self
.__sf
_os
2mano
(sf_list
)
3165 neExceptions
.ConnectionFailed
,
3166 ksExceptions
.ClientException
,
3167 neExceptions
.NeutronException
,
3170 self
._format
_exception
(e
)
3172 def delete_sf(self
, sf_id
):
3173 self
.logger
.debug("Deleting Service Function '%s' from VIM", sf_id
)
3176 self
._reload
_connection
()
3177 self
.neutron
.delete_sfc_port_pair_group(sf_id
)
3181 neExceptions
.ConnectionFailed
,
3182 neExceptions
.NeutronException
,
3183 ksExceptions
.ClientException
,
3184 neExceptions
.NeutronException
,
3187 self
._format
_exception
(e
)
3189 def new_sfp(self
, name
, classifications
, sfs
, sfc_encap
=True, spi
=None):
3190 self
.logger
.debug("Adding a new Service Function Path to VIM, named '%s'", name
)
3194 self
._reload
_connection
()
3195 # In networking-sfc the MPLS encapsulation is legacy
3196 # should be used when no full SFC Encapsulation is intended
3197 correlation
= "mpls"
3204 "flow_classifiers": classifications
,
3205 "port_pair_groups": sfs
,
3206 "chain_parameters": {"correlation": correlation
},
3210 sfp_dict
["chain_id"] = spi
3212 new_sfp
= self
.neutron
.create_sfc_port_chain({"port_chain": sfp_dict
})
3214 return new_sfp
["port_chain"]["id"]
3216 neExceptions
.ConnectionFailed
,
3217 ksExceptions
.ClientException
,
3218 neExceptions
.NeutronException
,
3223 self
.neutron
.delete_sfc_port_chain(new_sfp
["port_chain"]["id"])
3226 "Creation of Service Function Path failed, with "
3227 "subsequent deletion failure as well."
3230 self
._format
_exception
(e
)
3232 def get_sfp(self
, sfp_id
):
3233 self
.logger
.debug(" Getting Service Function Path %s from VIM", sfp_id
)
3235 filter_dict
= {"id": sfp_id
}
3236 sfp_list
= self
.get_sfp_list(filter_dict
)
3238 if len(sfp_list
) == 0:
3239 raise vimconn
.VimConnNotFoundException(
3240 "Service Function Path '{}' not found".format(sfp_id
)
3242 elif len(sfp_list
) > 1:
3243 raise vimconn
.VimConnConflictException(
3244 "Found more than one Service Function Path with this criteria"
3251 def get_sfp_list(self
, filter_dict
={}):
3253 "Getting Service Function Paths from VIM filter: '%s'", str(filter_dict
)
3257 self
._reload
_connection
()
3258 filter_dict_os
= filter_dict
.copy()
3260 if self
.api_version3
and "tenant_id" in filter_dict_os
:
3261 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
3263 sfp_dict
= self
.neutron
.list_sfc_port_chains(**filter_dict_os
)
3264 sfp_list
= sfp_dict
["port_chains"]
3265 self
.__sfp
_os
2mano
(sfp_list
)
3269 neExceptions
.ConnectionFailed
,
3270 ksExceptions
.ClientException
,
3271 neExceptions
.NeutronException
,
3274 self
._format
_exception
(e
)
3276 def delete_sfp(self
, sfp_id
):
3277 self
.logger
.debug("Deleting Service Function Path '%s' from VIM", sfp_id
)
3280 self
._reload
_connection
()
3281 self
.neutron
.delete_sfc_port_chain(sfp_id
)
3285 neExceptions
.ConnectionFailed
,
3286 neExceptions
.NeutronException
,
3287 ksExceptions
.ClientException
,
3288 neExceptions
.NeutronException
,
3291 self
._format
_exception
(e
)
3293 def refresh_sfps_status(self
, sfp_list
):
3294 """Get the status of the service function path
3295 Params: the list of sfp identifiers
3296 Returns a dictionary with:
3297 vm_id: #VIM id of this service function path
3298 status: #Mandatory. Text with one of:
3299 # DELETED (not found at vim)
3300 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3301 # OTHER (Vim reported other status not understood)
3302 # ERROR (VIM indicates an ERROR status)
3304 # CREATING (on building process)
3305 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3306 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
3310 "refresh_sfps status: Getting tenant SFP information from VIM"
3313 for sfp_id
in sfp_list
:
3317 sfp_vim
= self
.get_sfp(sfp_id
)
3320 sfp
["status"] = vmStatus2manoFormat
["ACTIVE"]
3322 sfp
["status"] = "OTHER"
3323 sfp
["error_msg"] = "VIM status reported " + sfp
["status"]
3325 sfp
["vim_info"] = self
.serialize(sfp_vim
)
3327 if sfp_vim
.get("fault"):
3328 sfp
["error_msg"] = str(sfp_vim
["fault"])
3329 except vimconn
.VimConnNotFoundException
as e
:
3330 self
.logger
.error("Exception getting sfp status: %s", str(e
))
3331 sfp
["status"] = "DELETED"
3332 sfp
["error_msg"] = str(e
)
3333 except vimconn
.VimConnException
as e
:
3334 self
.logger
.error("Exception getting sfp status: %s", str(e
))
3335 sfp
["status"] = "VIM_ERROR"
3336 sfp
["error_msg"] = str(e
)
3338 sfp_dict
[sfp_id
] = sfp
3342 def refresh_sfis_status(self
, sfi_list
):
3343 """Get the status of the service function instances
3344 Params: the list of sfi identifiers
3345 Returns a dictionary with:
3346 vm_id: #VIM id of this service function instance
3347 status: #Mandatory. Text with one of:
3348 # DELETED (not found at vim)
3349 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3350 # OTHER (Vim reported other status not understood)
3351 # ERROR (VIM indicates an ERROR status)
3353 # CREATING (on building process)
3354 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3355 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3359 "refresh_sfis status: Getting tenant sfi information from VIM"
3362 for sfi_id
in sfi_list
:
3366 sfi_vim
= self
.get_sfi(sfi_id
)
3369 sfi
["status"] = vmStatus2manoFormat
["ACTIVE"]
3371 sfi
["status"] = "OTHER"
3372 sfi
["error_msg"] = "VIM status reported " + sfi
["status"]
3374 sfi
["vim_info"] = self
.serialize(sfi_vim
)
3376 if sfi_vim
.get("fault"):
3377 sfi
["error_msg"] = str(sfi_vim
["fault"])
3378 except vimconn
.VimConnNotFoundException
as e
:
3379 self
.logger
.error("Exception getting sfi status: %s", str(e
))
3380 sfi
["status"] = "DELETED"
3381 sfi
["error_msg"] = str(e
)
3382 except vimconn
.VimConnException
as e
:
3383 self
.logger
.error("Exception getting sfi status: %s", str(e
))
3384 sfi
["status"] = "VIM_ERROR"
3385 sfi
["error_msg"] = str(e
)
3387 sfi_dict
[sfi_id
] = sfi
3391 def refresh_sfs_status(self
, sf_list
):
3392 """Get the status of the service functions
3393 Params: the list of sf identifiers
3394 Returns a dictionary with:
3395 vm_id: #VIM id of this service function
3396 status: #Mandatory. Text with one of:
3397 # DELETED (not found at vim)
3398 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3399 # OTHER (Vim reported other status not understood)
3400 # ERROR (VIM indicates an ERROR status)
3402 # CREATING (on building process)
3403 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3404 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3407 self
.logger
.debug("refresh_sfs status: Getting tenant sf information from VIM")
3409 for sf_id
in sf_list
:
3413 sf_vim
= self
.get_sf(sf_id
)
3416 sf
["status"] = vmStatus2manoFormat
["ACTIVE"]
3418 sf
["status"] = "OTHER"
3419 sf
["error_msg"] = "VIM status reported " + sf_vim
["status"]
3421 sf
["vim_info"] = self
.serialize(sf_vim
)
3423 if sf_vim
.get("fault"):
3424 sf
["error_msg"] = str(sf_vim
["fault"])
3425 except vimconn
.VimConnNotFoundException
as e
:
3426 self
.logger
.error("Exception getting sf status: %s", str(e
))
3427 sf
["status"] = "DELETED"
3428 sf
["error_msg"] = str(e
)
3429 except vimconn
.VimConnException
as e
:
3430 self
.logger
.error("Exception getting sf status: %s", str(e
))
3431 sf
["status"] = "VIM_ERROR"
3432 sf
["error_msg"] = str(e
)
3438 def refresh_classifications_status(self
, classification_list
):
3439 """Get the status of the classifications
3440 Params: the list of classification identifiers
3441 Returns a dictionary with:
3442 vm_id: #VIM id of this classifier
3443 status: #Mandatory. Text with one of:
3444 # DELETED (not found at vim)
3445 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3446 # OTHER (Vim reported other status not understood)
3447 # ERROR (VIM indicates an ERROR status)
3449 # CREATING (on building process)
3450 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3451 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3453 classification_dict
= {}
3455 "refresh_classifications status: Getting tenant classification information from VIM"
3458 for classification_id
in classification_list
:
3462 classification_vim
= self
.get_classification(classification_id
)
3464 if classification_vim
:
3465 classification
["status"] = vmStatus2manoFormat
["ACTIVE"]
3467 classification
["status"] = "OTHER"
3468 classification
["error_msg"] = (
3469 "VIM status reported " + classification
["status"]
3472 classification
["vim_info"] = self
.serialize(classification_vim
)
3474 if classification_vim
.get("fault"):
3475 classification
["error_msg"] = str(classification_vim
["fault"])
3476 except vimconn
.VimConnNotFoundException
as e
:
3477 self
.logger
.error("Exception getting classification status: %s", str(e
))
3478 classification
["status"] = "DELETED"
3479 classification
["error_msg"] = str(e
)
3480 except vimconn
.VimConnException
as e
:
3481 self
.logger
.error("Exception getting classification status: %s", str(e
))
3482 classification
["status"] = "VIM_ERROR"
3483 classification
["error_msg"] = str(e
)
3485 classification_dict
[classification_id
] = classification
3487 return classification_dict
3489 def new_affinity_group(self
, affinity_group_data
):
3490 """Adds a server group to VIM
3491 affinity_group_data contains a dictionary with information, keys:
3492 name: name in VIM for the server group
3493 type: affinity or anti-affinity
3494 scope: Only nfvi-node allowed
3495 Returns the server group identifier"""
3496 self
.logger
.debug("Adding Server Group '%s'", str(affinity_group_data
))
3499 name
= affinity_group_data
["name"]
3500 policy
= affinity_group_data
["type"]
3502 self
._reload
_connection
()
3503 new_server_group
= self
.nova
.server_groups
.create(name
, policy
)
3505 return new_server_group
.id
3507 ksExceptions
.ClientException
,
3508 nvExceptions
.ClientException
,
3512 self
._format
_exception
(e
)
3514 def get_affinity_group(self
, affinity_group_id
):
3515 """Obtain server group details from the VIM. Returns the server group detais as a dict"""
3516 self
.logger
.debug("Getting flavor '%s'", affinity_group_id
)
3518 self
._reload
_connection
()
3519 server_group
= self
.nova
.server_groups
.find(id=affinity_group_id
)
3521 return server_group
.to_dict()
3523 nvExceptions
.NotFound
,
3524 nvExceptions
.ClientException
,
3525 ksExceptions
.ClientException
,
3528 self
._format
_exception
(e
)
3530 def delete_affinity_group(self
, affinity_group_id
):
3531 """Deletes a server group from the VIM. Returns the old affinity_group_id"""
3532 self
.logger
.debug("Getting server group '%s'", affinity_group_id
)
3534 self
._reload
_connection
()
3535 self
.nova
.server_groups
.delete(affinity_group_id
)
3537 return affinity_group_id
3539 nvExceptions
.NotFound
,
3540 ksExceptions
.ClientException
,
3541 nvExceptions
.ClientException
,
3544 self
._format
_exception
(e
)
3546 def get_monitoring_data(self
):
3548 self
.logger
.debug("Getting servers and ports data from Openstack VIMs.")
3549 self
._reload
_connection
()
3550 all_servers
= self
.nova
.servers
.list(detailed
=True)
3551 all_ports
= self
.neutron
.list_ports()
3552 return all_servers
, all_ports
3554 vimconn
.VimConnException
,
3555 vimconn
.VimConnNotFoundException
,
3556 vimconn
.VimConnConnectionException
,
3558 raise vimconn
.VimConnException(
3559 f
"Exception in monitoring while getting VMs and ports status: {str(e)}"