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)
33 from osm_ro_plugin
import vimconn
43 from pprint
import pformat
44 from novaclient
import client
as nClient
, exceptions
as nvExceptions
45 from keystoneauth1
.identity
import v2
, v3
46 from keystoneauth1
import session
47 import keystoneclient
.exceptions
as ksExceptions
48 import keystoneclient
.v3
.client
as ksClient_v3
49 import keystoneclient
.v2_0
.client
as ksClient_v2
50 from glanceclient
import client
as glClient
51 import glanceclient
.exc
as gl1Exceptions
52 from cinderclient
import client
as cClient
54 # TODO py3 check that this base exception matches python2 httplib.HTTPException
55 from http
.client
import HTTPException
56 from neutronclient
.neutron
import client
as neClient
57 from neutronclient
.common
import exceptions
as neExceptions
58 from requests
.exceptions
import ConnectionError
60 __author__
= "Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research, Igor D.C., Eduardo Sousa"
61 __date__
= "$22-sep-2017 23:59:59$"
63 """contain the openstack virtual machine status to openmano status"""
64 vmStatus2manoFormat
= {
67 "SUSPENDED": "SUSPENDED",
68 "SHUTOFF": "INACTIVE",
73 netStatus2manoFormat
= {
76 "INACTIVE": "INACTIVE",
82 supportedClassificationTypes
= ["legacy_flow_classifier"]
84 # global var to have a timeout creating and deleting volumes
89 class SafeDumper(yaml
.SafeDumper
):
90 def represent_data(self
, data
):
91 # Openstack APIs use custom subclasses of dict and YAML safe dumper
92 # is designed to not handle that (reference issue 142 of pyyaml)
93 if isinstance(data
, dict) and data
.__class
__ != dict:
94 # A simple solution is to convert those items back to dicts
95 data
= dict(data
.items())
97 return super(SafeDumper
, self
).represent_data(data
)
100 class vimconnector(vimconn
.VimConnector
):
115 """using common constructor parameters. In this case
116 'url' is the keystone authorization url,
117 'url_admin' is not use
119 api_version
= config
.get("APIversion")
121 if api_version
and api_version
not in ("v3.3", "v2.0", "2", "3"):
122 raise vimconn
.VimConnException(
123 "Invalid value '{}' for config:APIversion. "
124 "Allowed values are 'v3.3', 'v2.0', '2' or '3'".format(api_version
)
127 vim_type
= config
.get("vim_type")
129 if vim_type
and vim_type
not in ("vio", "VIO"):
130 raise vimconn
.VimConnException(
131 "Invalid value '{}' for config:vim_type."
132 "Allowed values are 'vio' or 'VIO'".format(vim_type
)
135 if config
.get("dataplane_net_vlan_range") is not None:
136 # validate vlan ranges provided by user
137 self
._validate
_vlan
_ranges
(
138 config
.get("dataplane_net_vlan_range"), "dataplane_net_vlan_range"
141 if config
.get("multisegment_vlan_range") is not None:
142 # validate vlan ranges provided by user
143 self
._validate
_vlan
_ranges
(
144 config
.get("multisegment_vlan_range"), "multisegment_vlan_range"
147 vimconn
.VimConnector
.__init
__(
161 if self
.config
.get("insecure") and self
.config
.get("ca_cert"):
162 raise vimconn
.VimConnException(
163 "options insecure and ca_cert are mutually exclusive"
168 if self
.config
.get("insecure"):
171 if self
.config
.get("ca_cert"):
172 self
.verify
= self
.config
.get("ca_cert")
175 raise TypeError("url param can not be NoneType")
177 self
.persistent_info
= persistent_info
178 self
.availability_zone
= persistent_info
.get("availability_zone", None)
179 self
.session
= persistent_info
.get("session", {"reload_client": True})
180 self
.my_tenant_id
= self
.session
.get("my_tenant_id")
181 self
.nova
= self
.session
.get("nova")
182 self
.neutron
= self
.session
.get("neutron")
183 self
.cinder
= self
.session
.get("cinder")
184 self
.glance
= self
.session
.get("glance")
185 # self.glancev1 = self.session.get("glancev1")
186 self
.keystone
= self
.session
.get("keystone")
187 self
.api_version3
= self
.session
.get("api_version3")
188 self
.vim_type
= self
.config
.get("vim_type")
191 self
.vim_type
= self
.vim_type
.upper()
193 if self
.config
.get("use_internal_endpoint"):
194 self
.endpoint_type
= "internalURL"
196 self
.endpoint_type
= None
198 logging
.getLogger("urllib3").setLevel(logging
.WARNING
)
199 logging
.getLogger("keystoneauth").setLevel(logging
.WARNING
)
200 logging
.getLogger("novaclient").setLevel(logging
.WARNING
)
201 self
.logger
= logging
.getLogger("ro.vim.openstack")
203 # allow security_groups to be a list or a single string
204 if isinstance(self
.config
.get("security_groups"), str):
205 self
.config
["security_groups"] = [self
.config
["security_groups"]]
207 self
.security_groups_id
= None
209 # ###### VIO Specific Changes #########
210 if self
.vim_type
== "VIO":
211 self
.logger
= logging
.getLogger("ro.vim.vio")
214 self
.logger
.setLevel(getattr(logging
, log_level
))
216 def __getitem__(self
, index
):
217 """Get individuals parameters.
219 if index
== "project_domain_id":
220 return self
.config
.get("project_domain_id")
221 elif index
== "user_domain_id":
222 return self
.config
.get("user_domain_id")
224 return vimconn
.VimConnector
.__getitem
__(self
, index
)
226 def __setitem__(self
, index
, value
):
227 """Set individuals parameters and it is marked as dirty so to force connection reload.
229 if index
== "project_domain_id":
230 self
.config
["project_domain_id"] = value
231 elif index
== "user_domain_id":
232 self
.config
["user_domain_id"] = value
234 vimconn
.VimConnector
.__setitem
__(self
, index
, value
)
236 self
.session
["reload_client"] = True
238 def serialize(self
, value
):
239 """Serialization of python basic types.
241 In the case value is not serializable a message will be logged and a
242 simple representation of the data that cannot be converted back to
245 if isinstance(value
, str):
250 value
, Dumper
=SafeDumper
, default_flow_style
=True, width
=256
252 except yaml
.representer
.RepresenterError
:
254 "The following entity cannot be serialized in YAML:\n\n%s\n\n",
261 def _reload_connection(self
):
262 """Called before any operation, it check if credentials has changed
263 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
265 # TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
266 if self
.session
["reload_client"]:
267 if self
.config
.get("APIversion"):
268 self
.api_version3
= (
269 self
.config
["APIversion"] == "v3.3"
270 or self
.config
["APIversion"] == "3"
272 else: # get from ending auth_url that end with v3 or with v2.0
273 self
.api_version3
= self
.url
.endswith("/v3") or self
.url
.endswith(
277 self
.session
["api_version3"] = self
.api_version3
279 if self
.api_version3
:
280 if self
.config
.get("project_domain_id") or self
.config
.get(
281 "project_domain_name"
283 project_domain_id_default
= None
285 project_domain_id_default
= "default"
287 if self
.config
.get("user_domain_id") or self
.config
.get(
290 user_domain_id_default
= None
292 user_domain_id_default
= "default"
296 password
=self
.passwd
,
297 project_name
=self
.tenant_name
,
298 project_id
=self
.tenant_id
,
299 project_domain_id
=self
.config
.get(
300 "project_domain_id", project_domain_id_default
302 user_domain_id
=self
.config
.get(
303 "user_domain_id", user_domain_id_default
305 project_domain_name
=self
.config
.get("project_domain_name"),
306 user_domain_name
=self
.config
.get("user_domain_name"),
312 password
=self
.passwd
,
313 tenant_name
=self
.tenant_name
,
314 tenant_id
=self
.tenant_id
,
317 sess
= session
.Session(auth
=auth
, verify
=self
.verify
)
318 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
319 # Titanium cloud and StarlingX
320 region_name
= self
.config
.get("region_name")
322 if self
.api_version3
:
323 self
.keystone
= ksClient_v3
.Client(
325 endpoint_type
=self
.endpoint_type
,
326 region_name
=region_name
,
329 self
.keystone
= ksClient_v2
.Client(
330 session
=sess
, endpoint_type
=self
.endpoint_type
333 self
.session
["keystone"] = self
.keystone
334 # In order to enable microversion functionality an explicit microversion must be specified in "config".
335 # This implementation approach is due to the warning message in
336 # https://developer.openstack.org/api-guide/compute/microversions.html
337 # where it is stated that microversion backwards compatibility is not guaranteed and clients should
338 # always require an specific microversion.
339 # To be able to use "device role tagging" functionality define "microversion: 2.32" in datacenter config
340 version
= self
.config
.get("microversion")
345 # addedd region_name to keystone, nova, neutron and cinder to support distributed cloud for Wind River
346 # Titanium cloud and StarlingX
347 self
.nova
= self
.session
["nova"] = nClient
.Client(
350 endpoint_type
=self
.endpoint_type
,
351 region_name
=region_name
,
353 self
.neutron
= self
.session
["neutron"] = neClient
.Client(
356 endpoint_type
=self
.endpoint_type
,
357 region_name
=region_name
,
359 self
.cinder
= self
.session
["cinder"] = cClient
.Client(
362 endpoint_type
=self
.endpoint_type
,
363 region_name
=region_name
,
367 self
.my_tenant_id
= self
.session
["my_tenant_id"] = sess
.get_project_id()
369 self
.logger
.error("Cannot get project_id from session", exc_info
=True)
371 if self
.endpoint_type
== "internalURL":
372 glance_service_id
= self
.keystone
.services
.list(name
="glance")[0].id
373 glance_endpoint
= self
.keystone
.endpoints
.list(
374 glance_service_id
, interface
="internal"
377 glance_endpoint
= None
379 self
.glance
= self
.session
["glance"] = glClient
.Client(
380 2, session
=sess
, endpoint
=glance_endpoint
382 # using version 1 of glance client in new_image()
383 # self.glancev1 = self.session["glancev1"] = glClient.Client("1", session=sess,
384 # endpoint=glance_endpoint)
385 self
.session
["reload_client"] = False
386 self
.persistent_info
["session"] = self
.session
387 # add availablity zone info inside self.persistent_info
388 self
._set
_availablity
_zones
()
389 self
.persistent_info
["availability_zone"] = self
.availability_zone
390 # force to get again security_groups_ids next time they are needed
391 self
.security_groups_id
= None
393 def __net_os2mano(self
, net_list_dict
):
394 """Transform the net openstack format to mano format
395 net_list_dict can be a list of dict or a single dict"""
396 if type(net_list_dict
) is dict:
397 net_list_
= (net_list_dict
,)
398 elif type(net_list_dict
) is list:
399 net_list_
= net_list_dict
401 raise TypeError("param net_list_dict must be a list or a dictionary")
402 for net
in net_list_
:
403 if net
.get("provider:network_type") == "vlan":
406 net
["type"] = "bridge"
408 def __classification_os2mano(self
, class_list_dict
):
409 """Transform the openstack format (Flow Classifier) to mano format
410 (Classification) class_list_dict can be a list of dict or a single dict
412 if isinstance(class_list_dict
, dict):
413 class_list_
= [class_list_dict
]
414 elif isinstance(class_list_dict
, list):
415 class_list_
= class_list_dict
417 raise TypeError("param class_list_dict must be a list or a dictionary")
418 for classification
in class_list_
:
419 id = classification
.pop("id")
420 name
= classification
.pop("name")
421 description
= classification
.pop("description")
422 project_id
= classification
.pop("project_id")
423 tenant_id
= classification
.pop("tenant_id")
424 original_classification
= copy
.deepcopy(classification
)
425 classification
.clear()
426 classification
["ctype"] = "legacy_flow_classifier"
427 classification
["definition"] = original_classification
428 classification
["id"] = id
429 classification
["name"] = name
430 classification
["description"] = description
431 classification
["project_id"] = project_id
432 classification
["tenant_id"] = tenant_id
434 def __sfi_os2mano(self
, sfi_list_dict
):
435 """Transform the openstack format (Port Pair) to mano format (SFI)
436 sfi_list_dict can be a list of dict or a single dict
438 if isinstance(sfi_list_dict
, dict):
439 sfi_list_
= [sfi_list_dict
]
440 elif isinstance(sfi_list_dict
, list):
441 sfi_list_
= sfi_list_dict
443 raise TypeError("param sfi_list_dict must be a list or a dictionary")
445 for sfi
in sfi_list_
:
446 sfi
["ingress_ports"] = []
447 sfi
["egress_ports"] = []
449 if sfi
.get("ingress"):
450 sfi
["ingress_ports"].append(sfi
["ingress"])
452 if sfi
.get("egress"):
453 sfi
["egress_ports"].append(sfi
["egress"])
457 params
= sfi
.get("service_function_parameters")
461 correlation
= params
.get("correlation")
466 sfi
["sfc_encap"] = sfc_encap
467 del sfi
["service_function_parameters"]
469 def __sf_os2mano(self
, sf_list_dict
):
470 """Transform the openstack format (Port Pair Group) to mano format (SF)
471 sf_list_dict can be a list of dict or a single dict
473 if isinstance(sf_list_dict
, dict):
474 sf_list_
= [sf_list_dict
]
475 elif isinstance(sf_list_dict
, list):
476 sf_list_
= sf_list_dict
478 raise TypeError("param sf_list_dict must be a list or a dictionary")
481 del sf
["port_pair_group_parameters"]
482 sf
["sfis"] = sf
["port_pairs"]
485 def __sfp_os2mano(self
, sfp_list_dict
):
486 """Transform the openstack format (Port Chain) to mano format (SFP)
487 sfp_list_dict can be a list of dict or a single dict
489 if isinstance(sfp_list_dict
, dict):
490 sfp_list_
= [sfp_list_dict
]
491 elif isinstance(sfp_list_dict
, list):
492 sfp_list_
= sfp_list_dict
494 raise TypeError("param sfp_list_dict must be a list or a dictionary")
496 for sfp
in sfp_list_
:
497 params
= sfp
.pop("chain_parameters")
501 correlation
= params
.get("correlation")
506 sfp
["sfc_encap"] = sfc_encap
507 sfp
["spi"] = sfp
.pop("chain_id")
508 sfp
["classifications"] = sfp
.pop("flow_classifiers")
509 sfp
["service_functions"] = sfp
.pop("port_pair_groups")
511 # placeholder for now; read TODO note below
512 def _validate_classification(self
, type, definition
):
513 # only legacy_flow_classifier Type is supported at this point
515 # TODO(igordcard): this method should be an abstract method of an
516 # abstract Classification class to be implemented by the specific
517 # Types. Also, abstract vimconnector should call the validation
518 # method before the implemented VIM connectors are called.
520 def _format_exception(self
, exception
):
521 """Transform a keystone, nova, neutron exception into a vimconn exception discovering the cause"""
522 message_error
= str(exception
)
528 neExceptions
.NetworkNotFoundClient
,
529 nvExceptions
.NotFound
,
530 ksExceptions
.NotFound
,
531 gl1Exceptions
.HTTPNotFound
,
534 raise vimconn
.VimConnNotFoundException(
535 type(exception
).__name
__ + ": " + message_error
541 gl1Exceptions
.HTTPException
,
542 gl1Exceptions
.CommunicationError
,
544 ksExceptions
.ConnectionError
,
545 neExceptions
.ConnectionFailed
,
548 if type(exception
).__name
__ == "SSLError":
549 tip
= " (maybe option 'insecure' must be added to the VIM)"
551 raise vimconn
.VimConnConnectionException(
552 "Invalid URL or credentials{}: {}".format(tip
, message_error
)
558 nvExceptions
.BadRequest
,
559 ksExceptions
.BadRequest
,
562 raise vimconn
.VimConnException(
563 type(exception
).__name
__ + ": " + message_error
568 nvExceptions
.ClientException
,
569 ksExceptions
.ClientException
,
570 neExceptions
.NeutronException
,
573 raise vimconn
.VimConnUnexpectedResponse(
574 type(exception
).__name
__ + ": " + message_error
576 elif isinstance(exception
, nvExceptions
.Conflict
):
577 raise vimconn
.VimConnConflictException(
578 type(exception
).__name
__ + ": " + message_error
580 elif isinstance(exception
, vimconn
.VimConnException
):
583 self
.logger
.error("General Exception " + message_error
, exc_info
=True)
585 raise vimconn
.VimConnConnectionException(
586 type(exception
).__name
__ + ": " + message_error
589 def _get_ids_from_name(self
):
591 Obtain ids from name of tenant and security_groups. Store at self .security_groups_id"
594 # get tenant_id if only tenant_name is supplied
595 self
._reload
_connection
()
597 if not self
.my_tenant_id
:
598 raise vimconn
.VimConnConnectionException(
599 "Error getting tenant information from name={} id={}".format(
600 self
.tenant_name
, self
.tenant_id
604 if self
.config
.get("security_groups") and not self
.security_groups_id
:
605 # convert from name to id
606 neutron_sg_list
= self
.neutron
.list_security_groups(
607 tenant_id
=self
.my_tenant_id
610 self
.security_groups_id
= []
611 for sg
in self
.config
.get("security_groups"):
612 for neutron_sg
in neutron_sg_list
:
613 if sg
in (neutron_sg
["id"], neutron_sg
["name"]):
614 self
.security_groups_id
.append(neutron_sg
["id"])
617 self
.security_groups_id
= None
619 raise vimconn
.VimConnConnectionException(
620 "Not found security group {} for this tenant".format(sg
)
623 def check_vim_connectivity(self
):
624 # just get network list to check connectivity and credentials
625 self
.get_network_list(filter_dict
={})
627 def get_tenant_list(self
, filter_dict
={}):
628 """Obtain tenants of VIM
629 filter_dict can contain the following keys:
630 name: filter by tenant name
631 id: filter by tenant uuid/id
633 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
635 self
.logger
.debug("Getting tenants from VIM filter: '%s'", str(filter_dict
))
638 self
._reload
_connection
()
640 if self
.api_version3
:
641 project_class_list
= self
.keystone
.projects
.list(
642 name
=filter_dict
.get("name")
645 project_class_list
= self
.keystone
.tenants
.findall(**filter_dict
)
649 for project
in project_class_list
:
650 if filter_dict
.get("id") and filter_dict
["id"] != project
.id:
653 project_list
.append(project
.to_dict())
657 ksExceptions
.ConnectionError
,
658 ksExceptions
.ClientException
,
661 self
._format
_exception
(e
)
663 def new_tenant(self
, tenant_name
, tenant_description
):
664 """Adds a new tenant to openstack VIM. Returns the tenant identifier"""
665 self
.logger
.debug("Adding a new tenant name: %s", tenant_name
)
668 self
._reload
_connection
()
670 if self
.api_version3
:
671 project
= self
.keystone
.projects
.create(
673 self
.config
.get("project_domain_id", "default"),
674 description
=tenant_description
,
678 project
= self
.keystone
.tenants
.create(tenant_name
, tenant_description
)
682 ksExceptions
.ConnectionError
,
683 ksExceptions
.ClientException
,
684 ksExceptions
.BadRequest
,
687 self
._format
_exception
(e
)
689 def delete_tenant(self
, tenant_id
):
690 """Delete a tenant from openstack VIM. Returns the old tenant identifier"""
691 self
.logger
.debug("Deleting tenant %s from VIM", tenant_id
)
694 self
._reload
_connection
()
696 if self
.api_version3
:
697 self
.keystone
.projects
.delete(tenant_id
)
699 self
.keystone
.tenants
.delete(tenant_id
)
703 ksExceptions
.ConnectionError
,
704 ksExceptions
.ClientException
,
705 ksExceptions
.NotFound
,
708 self
._format
_exception
(e
)
716 provider_network_profile
=None,
718 """Adds a tenant network to VIM
720 'net_name': name of the network
722 'bridge': overlay isolated network
723 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
724 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
725 'ip_profile': is a dict containing the IP parameters of the network
726 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
727 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
728 'gateway_address': (Optional) ip_schema, that is X.X.X.X
729 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
730 'dhcp_enabled': True or False
731 'dhcp_start_address': ip_schema, first IP to grant
732 'dhcp_count': number of IPs to grant.
733 'shared': if this network can be seen/use by other tenants/organization
734 'provider_network_profile': (optional) contains {segmentation-id: vlan, network-type: vlan|vxlan,
735 physical-network: physnet-label}
736 Returns a tuple with the network identifier and created_items, or raises an exception on error
737 created_items can be None or a dictionary where this method can include key-values that will be passed to
738 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
739 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
743 "Adding a new network to VIM name '%s', type '%s'", net_name
, net_type
745 # self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
750 if provider_network_profile
:
751 vlan
= provider_network_profile
.get("segmentation-id")
755 self
._reload
_connection
()
756 network_dict
= {"name": net_name
, "admin_state_up": True}
758 if net_type
in ("data", "ptp"):
759 provider_physical_network
= None
761 if provider_network_profile
and provider_network_profile
.get(
764 provider_physical_network
= provider_network_profile
.get(
768 # provider-network must be one of the dataplane_physcial_netowrk if this is a list. If it is string
769 # or not declared, just ignore the checking
772 self
.config
.get("dataplane_physical_net"), (tuple, list)
774 and provider_physical_network
775 not in self
.config
["dataplane_physical_net"]
777 raise vimconn
.VimConnConflictException(
778 "Invalid parameter 'provider-network:physical-network' "
779 "for network creation. '{}' is not one of the declared "
780 "list at VIM_config:dataplane_physical_net".format(
781 provider_physical_network
785 # use the default dataplane_physical_net
786 if not provider_physical_network
:
787 provider_physical_network
= self
.config
.get(
788 "dataplane_physical_net"
791 # if it is non empty list, use the first value. If it is a string use the value directly
793 isinstance(provider_physical_network
, (tuple, list))
794 and provider_physical_network
796 provider_physical_network
= provider_physical_network
[0]
798 if not provider_physical_network
:
799 raise vimconn
.VimConnConflictException(
800 "missing information needed for underlay networks. Provide "
801 "'dataplane_physical_net' configuration at VIM or use the NS "
802 "instantiation parameter 'provider-network.physical-network'"
806 if not self
.config
.get("multisegment_support"):
808 "provider:physical_network"
809 ] = provider_physical_network
812 provider_network_profile
813 and "network-type" in provider_network_profile
816 "provider:network_type"
817 ] = provider_network_profile
["network-type"]
819 network_dict
["provider:network_type"] = self
.config
.get(
820 "dataplane_network_type", "vlan"
824 network_dict
["provider:segmentation_id"] = vlan
829 "provider:physical_network": "",
830 "provider:network_type": "vxlan",
832 segment_list
.append(segment1_dict
)
834 "provider:physical_network": provider_physical_network
,
835 "provider:network_type": "vlan",
839 segment2_dict
["provider:segmentation_id"] = vlan
840 elif self
.config
.get("multisegment_vlan_range"):
841 vlanID
= self
._generate
_multisegment
_vlanID
()
842 segment2_dict
["provider:segmentation_id"] = vlanID
845 # raise vimconn.VimConnConflictException(
846 # "You must provide "multisegment_vlan_range" at config dict before creating a multisegment
848 segment_list
.append(segment2_dict
)
849 network_dict
["segments"] = segment_list
851 # VIO Specific Changes. It needs a concrete VLAN
852 if self
.vim_type
== "VIO" and vlan
is None:
853 if self
.config
.get("dataplane_net_vlan_range") is None:
854 raise vimconn
.VimConnConflictException(
855 "You must provide 'dataplane_net_vlan_range' in format "
856 "[start_ID - end_ID] at VIM_config for creating underlay "
860 network_dict
["provider:segmentation_id"] = self
._generate
_vlanID
()
862 network_dict
["shared"] = shared
864 if self
.config
.get("disable_network_port_security"):
865 network_dict
["port_security_enabled"] = False
867 new_net
= self
.neutron
.create_network({"network": network_dict
})
869 # create subnetwork, even if there is no profile
874 if not ip_profile
.get("subnet_address"):
875 # Fake subnet is required
876 subnet_rand
= random
.randint(0, 255)
877 ip_profile
["subnet_address"] = "192.168.{}.0/24".format(subnet_rand
)
879 if "ip_version" not in ip_profile
:
880 ip_profile
["ip_version"] = "IPv4"
883 "name": net_name
+ "-subnet",
884 "network_id": new_net
["network"]["id"],
885 "ip_version": 4 if ip_profile
["ip_version"] == "IPv4" else 6,
886 "cidr": ip_profile
["subnet_address"],
889 # Gateway should be set to None if not needed. Otherwise openstack assigns one by default
890 if ip_profile
.get("gateway_address"):
891 subnet
["gateway_ip"] = ip_profile
["gateway_address"]
893 subnet
["gateway_ip"] = None
895 if ip_profile
.get("dns_address"):
896 subnet
["dns_nameservers"] = ip_profile
["dns_address"].split(";")
898 if "dhcp_enabled" in ip_profile
:
899 subnet
["enable_dhcp"] = (
901 if ip_profile
["dhcp_enabled"] == "false"
902 or ip_profile
["dhcp_enabled"] is False
906 if ip_profile
.get("dhcp_start_address"):
907 subnet
["allocation_pools"] = []
908 subnet
["allocation_pools"].append(dict())
909 subnet
["allocation_pools"][0]["start"] = ip_profile
[
913 if ip_profile
.get("dhcp_count"):
914 # parts = ip_profile["dhcp_start_address"].split(".")
915 # ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
916 ip_int
= int(netaddr
.IPAddress(ip_profile
["dhcp_start_address"]))
917 ip_int
+= ip_profile
["dhcp_count"] - 1
918 ip_str
= str(netaddr
.IPAddress(ip_int
))
919 subnet
["allocation_pools"][0]["end"] = ip_str
921 # self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
922 self
.neutron
.create_subnet({"subnet": subnet
})
924 if net_type
== "data" and self
.config
.get("multisegment_support"):
925 if self
.config
.get("l2gw_support"):
926 l2gw_list
= self
.neutron
.list_l2_gateways().get("l2_gateways", ())
927 for l2gw
in l2gw_list
:
929 "l2_gateway_id": l2gw
["id"],
930 "network_id": new_net
["network"]["id"],
931 "segmentation_id": str(vlanID
),
933 new_l2gw_conn
= self
.neutron
.create_l2_gateway_connection(
934 {"l2_gateway_connection": l2gw_conn
}
938 + str(new_l2gw_conn
["l2_gateway_connection"]["id"])
941 return new_net
["network"]["id"], created_items
942 except Exception as e
:
943 # delete l2gw connections (if any) before deleting the network
944 for k
, v
in created_items
.items():
945 if not v
: # skip already deleted
949 k_item
, _
, k_id
= k
.partition(":")
951 if k_item
== "l2gwconn":
952 self
.neutron
.delete_l2_gateway_connection(k_id
)
953 except Exception as e2
:
955 "Error deleting l2 gateway connection: {}: {}".format(
956 type(e2
).__name
__, e2
961 self
.neutron
.delete_network(new_net
["network"]["id"])
963 self
._format
_exception
(e
)
965 def get_network_list(self
, filter_dict
={}):
966 """Obtain tenant networks of VIM
972 admin_state_up: boolean
974 Returns the network list of dictionaries
976 self
.logger
.debug("Getting network from VIM filter: '%s'", str(filter_dict
))
979 self
._reload
_connection
()
980 filter_dict_os
= filter_dict
.copy()
982 if self
.api_version3
and "tenant_id" in filter_dict_os
:
984 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
986 net_dict
= self
.neutron
.list_networks(**filter_dict_os
)
987 net_list
= net_dict
["networks"]
988 self
.__net
_os
2mano
(net_list
)
992 neExceptions
.ConnectionFailed
,
993 ksExceptions
.ClientException
,
994 neExceptions
.NeutronException
,
997 self
._format
_exception
(e
)
999 def get_network(self
, net_id
):
1000 """Obtain details of network from VIM
1001 Returns the network information from a network id"""
1002 self
.logger
.debug(" Getting tenant network %s from VIM", net_id
)
1003 filter_dict
= {"id": net_id
}
1004 net_list
= self
.get_network_list(filter_dict
)
1006 if len(net_list
) == 0:
1007 raise vimconn
.VimConnNotFoundException(
1008 "Network '{}' not found".format(net_id
)
1010 elif len(net_list
) > 1:
1011 raise vimconn
.VimConnConflictException(
1012 "Found more than one network with this criteria"
1017 for subnet_id
in net
.get("subnets", ()):
1019 subnet
= self
.neutron
.show_subnet(subnet_id
)
1020 except Exception as e
:
1022 "osconnector.get_network(): Error getting subnet %s %s"
1025 subnet
= {"id": subnet_id
, "fault": str(e
)}
1027 subnets
.append(subnet
)
1029 net
["subnets"] = subnets
1030 net
["encapsulation"] = net
.get("provider:network_type")
1031 net
["encapsulation_type"] = net
.get("provider:network_type")
1032 net
["segmentation_id"] = net
.get("provider:segmentation_id")
1033 net
["encapsulation_id"] = net
.get("provider:segmentation_id")
1037 def delete_network(self
, net_id
, created_items
=None):
1039 Removes a tenant network from VIM and its associated elements
1040 :param net_id: VIM identifier of the network, provided by method new_network
1041 :param created_items: dictionary with extra items to be deleted. provided by method new_network
1042 Returns the network identifier or raises an exception upon error or when network is not found
1044 self
.logger
.debug("Deleting network '%s' from VIM", net_id
)
1046 if created_items
is None:
1050 self
._reload
_connection
()
1051 # delete l2gw connections (if any) before deleting the network
1052 for k
, v
in created_items
.items():
1053 if not v
: # skip already deleted
1057 k_item
, _
, k_id
= k
.partition(":")
1058 if k_item
== "l2gwconn":
1059 self
.neutron
.delete_l2_gateway_connection(k_id
)
1060 except Exception as e
:
1062 "Error deleting l2 gateway connection: {}: {}".format(
1067 # delete VM ports attached to this networks before the network
1068 ports
= self
.neutron
.list_ports(network_id
=net_id
)
1069 for p
in ports
["ports"]:
1071 self
.neutron
.delete_port(p
["id"])
1072 except Exception as e
:
1073 self
.logger
.error("Error deleting port %s: %s", p
["id"], str(e
))
1075 self
.neutron
.delete_network(net_id
)
1079 neExceptions
.ConnectionFailed
,
1080 neExceptions
.NetworkNotFoundClient
,
1081 neExceptions
.NeutronException
,
1082 ksExceptions
.ClientException
,
1083 neExceptions
.NeutronException
,
1086 self
._format
_exception
(e
)
1088 def refresh_nets_status(self
, net_list
):
1089 """Get the status of the networks
1090 Params: the list of network identifiers
1091 Returns a dictionary with:
1092 net_id: #VIM id of this network
1093 status: #Mandatory. Text with one of:
1094 # DELETED (not found at vim)
1095 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1096 # OTHER (Vim reported other status not understood)
1097 # ERROR (VIM indicates an ERROR status)
1098 # ACTIVE, INACTIVE, DOWN (admin down),
1099 # BUILD (on building process)
1101 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1102 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1106 for net_id
in net_list
:
1110 net_vim
= self
.get_network(net_id
)
1112 if net_vim
["status"] in netStatus2manoFormat
:
1113 net
["status"] = netStatus2manoFormat
[net_vim
["status"]]
1115 net
["status"] = "OTHER"
1116 net
["error_msg"] = "VIM status reported " + net_vim
["status"]
1118 if net
["status"] == "ACTIVE" and not net_vim
["admin_state_up"]:
1119 net
["status"] = "DOWN"
1121 net
["vim_info"] = self
.serialize(net_vim
)
1123 if net_vim
.get("fault"): # TODO
1124 net
["error_msg"] = str(net_vim
["fault"])
1125 except vimconn
.VimConnNotFoundException
as e
:
1126 self
.logger
.error("Exception getting net status: %s", str(e
))
1127 net
["status"] = "DELETED"
1128 net
["error_msg"] = str(e
)
1129 except vimconn
.VimConnException
as e
:
1130 self
.logger
.error("Exception getting net status: %s", str(e
))
1131 net
["status"] = "VIM_ERROR"
1132 net
["error_msg"] = str(e
)
1133 net_dict
[net_id
] = net
1136 def get_flavor(self
, flavor_id
):
1137 """Obtain flavor details from the VIM. Returns the flavor dict details"""
1138 self
.logger
.debug("Getting flavor '%s'", flavor_id
)
1141 self
._reload
_connection
()
1142 flavor
= self
.nova
.flavors
.find(id=flavor_id
)
1143 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
1145 return flavor
.to_dict()
1147 nvExceptions
.NotFound
,
1148 nvExceptions
.ClientException
,
1149 ksExceptions
.ClientException
,
1152 self
._format
_exception
(e
)
1154 def get_flavor_id_from_data(self
, flavor_dict
):
1155 """Obtain flavor id that match the flavor description
1156 Returns the flavor_id or raises a vimconnNotFoundException
1157 flavor_dict: contains the required ram, vcpus, disk
1158 If 'use_existing_flavors' is set to True at config, the closer flavor that provides same or more ram, vcpus
1159 and disk is returned. Otherwise a flavor with exactly same ram, vcpus and disk is returned or a
1160 vimconnNotFoundException is raised
1162 exact_match
= False if self
.config
.get("use_existing_flavors") else True
1165 self
._reload
_connection
()
1166 flavor_candidate_id
= None
1167 flavor_candidate_data
= (10000, 10000, 10000)
1170 flavor_dict
["vcpus"],
1171 flavor_dict
["disk"],
1174 extended
= flavor_dict
.get("extended", {})
1177 raise vimconn
.VimConnNotFoundException(
1178 "Flavor with EPA still not implemented"
1180 # if len(numas) > 1:
1181 # raise vimconn.VimConnNotFoundException("Cannot find any flavor with more than one numa")
1183 # numas = extended.get("numas")
1184 for flavor
in self
.nova
.flavors
.list():
1185 epa
= flavor
.get_keys()
1191 flavor_data
= (flavor
.ram
, flavor
.vcpus
, flavor
.disk
)
1192 if flavor_data
== flavor_target
:
1196 and flavor_target
< flavor_data
< flavor_candidate_data
1198 flavor_candidate_id
= flavor
.id
1199 flavor_candidate_data
= flavor_data
1201 if not exact_match
and flavor_candidate_id
:
1202 return flavor_candidate_id
1204 raise vimconn
.VimConnNotFoundException(
1205 "Cannot find any flavor matching '{}'".format(flavor_dict
)
1208 nvExceptions
.NotFound
,
1209 nvExceptions
.ClientException
,
1210 ksExceptions
.ClientException
,
1213 self
._format
_exception
(e
)
1215 def process_resource_quota(self
, quota
, prefix
, extra_specs
):
1221 if "limit" in quota
:
1222 extra_specs
["quota:" + prefix
+ "_limit"] = quota
["limit"]
1224 if "reserve" in quota
:
1225 extra_specs
["quota:" + prefix
+ "_reservation"] = quota
["reserve"]
1227 if "shares" in quota
:
1228 extra_specs
["quota:" + prefix
+ "_shares_level"] = "custom"
1229 extra_specs
["quota:" + prefix
+ "_shares_share"] = quota
["shares"]
1231 def new_flavor(self
, flavor_data
, change_name_if_used
=True):
1232 """Adds a tenant flavor to openstack VIM
1233 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name
1235 Returns the flavor identifier
1237 self
.logger
.debug("Adding flavor '%s'", str(flavor_data
))
1243 name
= flavor_data
["name"]
1244 while retry
< max_retries
:
1247 self
._reload
_connection
()
1249 if change_name_if_used
:
1252 fl
= self
.nova
.flavors
.list()
1255 fl_names
.append(f
.name
)
1257 while name
in fl_names
:
1259 name
= flavor_data
["name"] + "-" + str(name_suffix
)
1261 ram
= flavor_data
.get("ram", 64)
1262 vcpus
= flavor_data
.get("vcpus", 1)
1265 extended
= flavor_data
.get("extended")
1267 numas
= extended
.get("numas")
1270 numa_nodes
= len(numas
)
1273 return -1, "Can not add flavor with more than one numa"
1275 extra_specs
["hw:numa_nodes"] = str(numa_nodes
)
1276 extra_specs
["hw:mem_page_size"] = "large"
1277 extra_specs
["hw:cpu_policy"] = "dedicated"
1278 extra_specs
["hw:numa_mempolicy"] = "strict"
1280 if self
.vim_type
== "VIO":
1282 "vmware:extra_config"
1283 ] = '{"numa.nodeAffinity":"0"}'
1284 extra_specs
["vmware:latency_sensitivity_level"] = "high"
1287 # overwrite ram and vcpus
1288 # check if key "memory" is present in numa else use ram value at flavor
1289 if "memory" in numa
:
1290 ram
= numa
["memory"] * 1024
1291 # See for reference: https://specs.openstack.org/openstack/nova-specs/specs/mitaka/
1292 # implemented/virt-driver-cpu-thread-pinning.html
1293 extra_specs
["hw:cpu_sockets"] = 1
1295 if "paired-threads" in numa
:
1296 vcpus
= numa
["paired-threads"] * 2
1297 # cpu_thread_policy "require" implies that the compute node must have an
1299 extra_specs
["hw:cpu_thread_policy"] = "require"
1300 extra_specs
["hw:cpu_policy"] = "dedicated"
1301 elif "cores" in numa
:
1302 vcpus
= numa
["cores"]
1303 # cpu_thread_policy "prefer" implies that the host must not have an SMT
1304 # architecture, or a non-SMT architecture will be emulated
1305 extra_specs
["hw:cpu_thread_policy"] = "isolate"
1306 extra_specs
["hw:cpu_policy"] = "dedicated"
1307 elif "threads" in numa
:
1308 vcpus
= numa
["threads"]
1309 # cpu_thread_policy "prefer" implies that the host may or may not have an SMT
1311 extra_specs
["hw:cpu_thread_policy"] = "prefer"
1312 extra_specs
["hw:cpu_policy"] = "dedicated"
1313 # for interface in numa.get("interfaces",() ):
1314 # if interface["dedicated"]=="yes":
1315 # raise vimconn.VimConnException("Passthrough interfaces are not supported
1316 # for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
1317 # #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"'
1318 # when a way to connect it is available
1319 elif extended
.get("cpu-quota"):
1320 self
.process_resource_quota(
1321 extended
.get("cpu-quota"), "cpu", extra_specs
1324 if extended
.get("mem-quota"):
1325 self
.process_resource_quota(
1326 extended
.get("mem-quota"), "memory", extra_specs
1329 if extended
.get("vif-quota"):
1330 self
.process_resource_quota(
1331 extended
.get("vif-quota"), "vif", extra_specs
1334 if extended
.get("disk-io-quota"):
1335 self
.process_resource_quota(
1336 extended
.get("disk-io-quota"), "disk_io", extra_specs
1340 new_flavor
= self
.nova
.flavors
.create(
1344 flavor_data
.get("disk", 0),
1345 is_public
=flavor_data
.get("is_public", True),
1349 new_flavor
.set_keys(extra_specs
)
1351 return new_flavor
.id
1352 except nvExceptions
.Conflict
as e
:
1353 if change_name_if_used
and retry
< max_retries
:
1356 self
._format
_exception
(e
)
1357 # except nvExceptions.BadRequest as e:
1359 ksExceptions
.ClientException
,
1360 nvExceptions
.ClientException
,
1364 self
._format
_exception
(e
)
1366 def delete_flavor(self
, flavor_id
):
1367 """Deletes a tenant flavor from openstack VIM. Returns the old flavor_id"""
1369 self
._reload
_connection
()
1370 self
.nova
.flavors
.delete(flavor_id
)
1373 # except nvExceptions.BadRequest as e:
1375 nvExceptions
.NotFound
,
1376 ksExceptions
.ClientException
,
1377 nvExceptions
.ClientException
,
1380 self
._format
_exception
(e
)
1382 def new_image(self
, image_dict
):
1384 Adds a tenant image to VIM. imge_dict is a dictionary with:
1386 disk_format: qcow2, vhd, vmdk, raw (by default), ...
1387 location: path or URI
1388 public: "yes" or "no"
1389 metadata: metadata of the image
1390 Returns the image_id
1395 while retry
< max_retries
:
1398 self
._reload
_connection
()
1400 # determine format http://docs.openstack.org/developer/glance/formats.html
1401 if "disk_format" in image_dict
:
1402 disk_format
= image_dict
["disk_format"]
1403 else: # autodiscover based on extension
1404 if image_dict
["location"].endswith(".qcow2"):
1405 disk_format
= "qcow2"
1406 elif image_dict
["location"].endswith(".vhd"):
1408 elif image_dict
["location"].endswith(".vmdk"):
1409 disk_format
= "vmdk"
1410 elif image_dict
["location"].endswith(".vdi"):
1412 elif image_dict
["location"].endswith(".iso"):
1414 elif image_dict
["location"].endswith(".aki"):
1416 elif image_dict
["location"].endswith(".ari"):
1418 elif image_dict
["location"].endswith(".ami"):
1424 "new_image: '%s' loading from '%s'",
1426 image_dict
["location"],
1428 if self
.vim_type
== "VIO":
1429 container_format
= "bare"
1430 if "container_format" in image_dict
:
1431 container_format
= image_dict
["container_format"]
1433 new_image
= self
.glance
.images
.create(
1434 name
=image_dict
["name"],
1435 container_format
=container_format
,
1436 disk_format
=disk_format
,
1439 new_image
= self
.glance
.images
.create(name
=image_dict
["name"])
1441 if image_dict
["location"].startswith("http"):
1442 # TODO there is not a method to direct download. It must be downloaded locally with requests
1443 raise vimconn
.VimConnNotImplemented("Cannot create image from URL")
1445 with
open(image_dict
["location"]) as fimage
:
1446 self
.glance
.images
.upload(new_image
.id, fimage
)
1447 # new_image = self.glancev1.images.create(name=image_dict["name"], is_public=
1448 # image_dict.get("public","yes")=="yes",
1449 # container_format="bare", data=fimage, disk_format=disk_format)
1451 metadata_to_load
= image_dict
.get("metadata")
1453 # TODO location is a reserved word for current openstack versions. fixed for VIO please check
1455 if self
.vim_type
== "VIO":
1456 metadata_to_load
["upload_location"] = image_dict
["location"]
1458 metadata_to_load
["location"] = image_dict
["location"]
1460 self
.glance
.images
.update(new_image
.id, **metadata_to_load
)
1464 nvExceptions
.Conflict
,
1465 ksExceptions
.ClientException
,
1466 nvExceptions
.ClientException
,
1468 self
._format
_exception
(e
)
1471 gl1Exceptions
.HTTPException
,
1472 gl1Exceptions
.CommunicationError
,
1475 if retry
== max_retries
:
1478 self
._format
_exception
(e
)
1479 except IOError as e
: # can not open the file
1480 raise vimconn
.VimConnConnectionException(
1481 "{}: {} for {}".format(type(e
).__name
__, e
, image_dict
["location"]),
1482 http_code
=vimconn
.HTTP_Bad_Request
,
1485 def delete_image(self
, image_id
):
1486 """Deletes a tenant image from openstack VIM. Returns the old id"""
1488 self
._reload
_connection
()
1489 self
.glance
.images
.delete(image_id
)
1493 nvExceptions
.NotFound
,
1494 ksExceptions
.ClientException
,
1495 nvExceptions
.ClientException
,
1496 gl1Exceptions
.CommunicationError
,
1497 gl1Exceptions
.HTTPNotFound
,
1499 ) as e
: # TODO remove
1500 self
._format
_exception
(e
)
1502 def get_image_id_from_path(self
, path
):
1503 """Get the image id from image path in the VIM database. Returns the image_id"""
1505 self
._reload
_connection
()
1506 images
= self
.glance
.images
.list()
1508 for image
in images
:
1509 if image
.metadata
.get("location") == path
:
1512 raise vimconn
.VimConnNotFoundException(
1513 "image with location '{}' not found".format(path
)
1516 ksExceptions
.ClientException
,
1517 nvExceptions
.ClientException
,
1518 gl1Exceptions
.CommunicationError
,
1521 self
._format
_exception
(e
)
1523 def get_image_list(self
, filter_dict
={}):
1524 """Obtain tenant images from VIM
1528 checksum: image checksum
1529 Returns the image list of dictionaries:
1530 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1533 self
.logger
.debug("Getting image list from VIM filter: '%s'", str(filter_dict
))
1536 self
._reload
_connection
()
1537 # filter_dict_os = filter_dict.copy()
1538 # First we filter by the available filter fields: name, id. The others are removed.
1539 image_list
= self
.glance
.images
.list()
1542 for image
in image_list
:
1544 if filter_dict
.get("name") and image
["name"] != filter_dict
["name"]:
1547 if filter_dict
.get("id") and image
["id"] != filter_dict
["id"]:
1551 filter_dict
.get("checksum")
1552 and image
["checksum"] != filter_dict
["checksum"]
1556 filtered_list
.append(image
.copy())
1557 except gl1Exceptions
.HTTPNotFound
:
1560 return filtered_list
1562 ksExceptions
.ClientException
,
1563 nvExceptions
.ClientException
,
1564 gl1Exceptions
.CommunicationError
,
1567 self
._format
_exception
(e
)
1569 def __wait_for_vm(self
, vm_id
, status
):
1570 """wait until vm is in the desired status and return True.
1571 If the VM gets in ERROR status, return false.
1572 If the timeout is reached generate an exception"""
1574 while elapsed_time
< server_timeout
:
1575 vm_status
= self
.nova
.servers
.get(vm_id
).status
1577 if vm_status
== status
:
1580 if vm_status
== "ERROR":
1586 # if we exceeded the timeout rollback
1587 if elapsed_time
>= server_timeout
:
1588 raise vimconn
.VimConnException(
1589 "Timeout waiting for instance " + vm_id
+ " to get " + status
,
1590 http_code
=vimconn
.HTTP_Request_Timeout
,
1593 def _get_openstack_availablity_zones(self
):
1595 Get from openstack availability zones available
1599 openstack_availability_zone
= self
.nova
.availability_zones
.list()
1600 openstack_availability_zone
= [
1602 for zone
in openstack_availability_zone
1603 if zone
.zoneName
!= "internal"
1606 return openstack_availability_zone
1610 def _set_availablity_zones(self
):
1612 Set vim availablity zone
1615 if "availability_zone" in self
.config
:
1616 vim_availability_zones
= self
.config
.get("availability_zone")
1618 if isinstance(vim_availability_zones
, str):
1619 self
.availability_zone
= [vim_availability_zones
]
1620 elif isinstance(vim_availability_zones
, list):
1621 self
.availability_zone
= vim_availability_zones
1623 self
.availability_zone
= self
._get
_openstack
_availablity
_zones
()
1625 def _get_vm_availability_zone(
1626 self
, availability_zone_index
, availability_zone_list
1629 Return thge availability zone to be used by the created VM.
1630 :return: The VIM availability zone to be used or None
1632 if availability_zone_index
is None:
1633 if not self
.config
.get("availability_zone"):
1635 elif isinstance(self
.config
.get("availability_zone"), str):
1636 return self
.config
["availability_zone"]
1638 # TODO consider using a different parameter at config for default AV and AV list match
1639 return self
.config
["availability_zone"][0]
1641 vim_availability_zones
= self
.availability_zone
1642 # check if VIM offer enough availability zones describe in the VNFD
1643 if vim_availability_zones
and len(availability_zone_list
) <= len(
1644 vim_availability_zones
1646 # check if all the names of NFV AV match VIM AV names
1647 match_by_index
= False
1648 for av
in availability_zone_list
:
1649 if av
not in vim_availability_zones
:
1650 match_by_index
= True
1654 return vim_availability_zones
[availability_zone_index
]
1656 return availability_zone_list
[availability_zone_index
]
1658 raise vimconn
.VimConnConflictException(
1659 "No enough availability zones at VIM for this deployment"
1672 availability_zone_index
=None,
1673 availability_zone_list
=None,
1675 """Adds a VM instance to VIM
1677 start: indicates if VM must start or boot in pause mode. Ignored
1678 image_id,flavor_id: iamge and flavor uuid
1679 net_list: list of interfaces, each one is a dictionary with:
1681 net_id: network uuid to connect
1682 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
1683 model: interface model, ignored #TODO
1684 mac_address: used for SR-IOV ifaces #TODO for other types
1685 use: 'data', 'bridge', 'mgmt'
1686 type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared'
1687 vim_id: filled/added by this function
1688 floating_ip: True/False (or it can be None)
1689 port_security: True/False
1690 'cloud_config': (optional) dictionary with:
1691 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1692 'users': (optional) list of users to be inserted, each item is a dict with:
1693 'name': (mandatory) user name,
1694 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1695 'user-data': (optional) string is a text script to be passed directly to cloud-init
1696 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1697 'dest': (mandatory) string with the destination absolute path
1698 'encoding': (optional, by default text). Can be one of:
1699 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1700 'content' (mandatory): string with the content of the file
1701 'permissions': (optional) string with file permissions, typically octal notation '0644'
1702 'owner': (optional) file owner, string with the format 'owner:group'
1703 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1704 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1705 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1706 'size': (mandatory) string with the size of the disk in GB
1707 'vim_id' (optional) should use this existing volume id
1708 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1709 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1710 availability_zone_index is None
1711 #TODO ip, security groups
1712 Returns a tuple with the instance identifier and created_items or raises an exception on error
1713 created_items can be None or a dictionary where this method can include key-values that will be passed to
1714 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1715 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1719 "new_vminstance input: image='%s' flavor='%s' nics='%s'",
1730 external_network
= []
1731 # ^list of external networks to be connected to instance, later on used to create floating_ip
1732 no_secured_ports
= [] # List of port-is with port-security disabled
1733 self
._reload
_connection
()
1734 # metadata_vpci = {} # For a specific neutron plugin
1735 block_device_mapping
= None
1737 for net
in net_list
:
1738 if not net
.get("net_id"): # skip non connected iface
1742 "network_id": net
["net_id"],
1743 "name": net
.get("name"),
1744 "admin_state_up": True,
1748 self
.config
.get("security_groups")
1749 and net
.get("port_security") is not False
1750 and not self
.config
.get("no_port_security_extension")
1752 if not self
.security_groups_id
:
1753 self
._get
_ids
_from
_name
()
1755 port_dict
["security_groups"] = self
.security_groups_id
1757 if net
["type"] == "virtual":
1760 # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
1761 elif net
["type"] == "VF" or net
["type"] == "SR-IOV": # for VF
1763 # if "VF" not in metadata_vpci:
1764 # metadata_vpci["VF"]=[]
1765 # metadata_vpci["VF"].append([ net["vpci"], "" ])
1766 port_dict
["binding:vnic_type"] = "direct"
1768 # VIO specific Changes
1769 if self
.vim_type
== "VIO":
1770 # Need to create port with port_security_enabled = False and no-security-groups
1771 port_dict
["port_security_enabled"] = False
1772 port_dict
["provider_security_groups"] = []
1773 port_dict
["security_groups"] = []
1774 else: # For PT PCI-PASSTHROUGH
1776 # if "PF" not in metadata_vpci:
1777 # metadata_vpci["PF"]=[]
1778 # metadata_vpci["PF"].append([ net["vpci"], "" ])
1779 port_dict
["binding:vnic_type"] = "direct-physical"
1781 if not port_dict
["name"]:
1782 port_dict
["name"] = name
1784 if net
.get("mac_address"):
1785 port_dict
["mac_address"] = net
["mac_address"]
1787 if net
.get("ip_address"):
1788 port_dict
["fixed_ips"] = [{"ip_address": net
["ip_address"]}]
1789 # TODO add "subnet_id": <subnet_id>
1791 new_port
= self
.neutron
.create_port({"port": port_dict
})
1792 created_items
["port:" + str(new_port
["port"]["id"])] = True
1793 net
["mac_adress"] = new_port
["port"]["mac_address"]
1794 net
["vim_id"] = new_port
["port"]["id"]
1795 # if try to use a network without subnetwork, it will return a emtpy list
1796 fixed_ips
= new_port
["port"].get("fixed_ips")
1799 net
["ip"] = fixed_ips
[0].get("ip_address")
1803 port
= {"port-id": new_port
["port"]["id"]}
1804 if float(self
.nova
.api_version
.get_string()) >= 2.32:
1805 port
["tag"] = new_port
["port"]["name"]
1807 net_list_vim
.append(port
)
1809 if net
.get("floating_ip", False):
1810 net
["exit_on_floating_ip_error"] = True
1811 external_network
.append(net
)
1812 elif net
["use"] == "mgmt" and self
.config
.get("use_floating_ip"):
1813 net
["exit_on_floating_ip_error"] = False
1814 external_network
.append(net
)
1815 net
["floating_ip"] = self
.config
.get("use_floating_ip")
1817 # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic
1819 # As a workaround we wait until the VM is active and then disable the port-security
1820 if net
.get("port_security") is False and not self
.config
.get(
1821 "no_port_security_extension"
1823 no_secured_ports
.append(
1825 new_port
["port"]["id"],
1826 net
.get("port_security_disable_strategy"),
1831 # metadata = {"pci_assignement": json.dumps(metadata_vpci)}
1832 # if len(metadata["pci_assignement"]) >255:
1833 # #limit the metadata size
1834 # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
1835 # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
1839 "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'",
1848 config_drive
, userdata
= self
._create
_user
_data
(cloud_config
)
1850 # Create additional volumes in case these are present in disk_list
1851 base_disk_index
= ord("b")
1853 block_device_mapping
= {}
1854 for disk
in disk_list
:
1855 if disk
.get("vim_id"):
1856 block_device_mapping
["_vd" + chr(base_disk_index
)] = disk
[
1860 if "image_id" in disk
:
1861 volume
= self
.cinder
.volumes
.create(
1863 name
=name
+ "_vd" + chr(base_disk_index
),
1864 imageRef
=disk
["image_id"],
1867 volume
= self
.cinder
.volumes
.create(
1869 name
=name
+ "_vd" + chr(base_disk_index
),
1872 created_items
["volume:" + str(volume
.id)] = True
1873 block_device_mapping
["_vd" + chr(base_disk_index
)] = volume
.id
1875 base_disk_index
+= 1
1877 # Wait until created volumes are with status available
1879 while elapsed_time
< volume_timeout
:
1880 for created_item
in created_items
:
1881 v
, _
, volume_id
= created_item
.partition(":")
1883 if self
.cinder
.volumes
.get(volume_id
).status
!= "available":
1885 else: # all ready: break from while
1891 # If we exceeded the timeout rollback
1892 if elapsed_time
>= volume_timeout
:
1893 raise vimconn
.VimConnException(
1894 "Timeout creating volumes for instance " + name
,
1895 http_code
=vimconn
.HTTP_Request_Timeout
,
1898 # get availability Zone
1899 vm_av_zone
= self
._get
_vm
_availability
_zone
(
1900 availability_zone_index
, availability_zone_list
1904 "nova.servers.create({}, {}, {}, nics={}, security_groups={}, "
1905 "availability_zone={}, key_name={}, userdata={}, config_drive={}, "
1906 "block_device_mapping={})".format(
1911 self
.config
.get("security_groups"),
1913 self
.config
.get("keypair"),
1916 block_device_mapping
,
1919 server
= self
.nova
.servers
.create(
1924 security_groups
=self
.config
.get("security_groups"),
1925 # TODO remove security_groups in future versions. Already at neutron port
1926 availability_zone
=vm_av_zone
,
1927 key_name
=self
.config
.get("keypair"),
1929 config_drive
=config_drive
,
1930 block_device_mapping
=block_device_mapping
,
1931 ) # , description=description)
1933 vm_start_time
= time
.time()
1934 # Previously mentioned workaround to wait until the VM is active and then disable the port-security
1935 if no_secured_ports
:
1936 self
.__wait
_for
_vm
(server
.id, "ACTIVE")
1938 for port
in no_secured_ports
:
1940 "port": {"port_security_enabled": False, "security_groups": None}
1943 if port
[1] == "allow-address-pairs":
1945 "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}
1949 self
.neutron
.update_port(port
[0], port_update
)
1951 raise vimconn
.VimConnException(
1952 "It was not possible to disable port security for port {}".format(
1957 # print "DONE :-)", server
1960 for floating_network
in external_network
:
1963 floating_ip_retries
= 3
1964 # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry
1967 floating_ips
= self
.neutron
.list_floatingips().get(
1970 random
.shuffle(floating_ips
) # randomize
1971 for fip
in floating_ips
:
1974 or fip
.get("tenant_id") != server
.tenant_id
1978 if isinstance(floating_network
["floating_ip"], str):
1980 fip
.get("floating_network_id")
1981 != floating_network
["floating_ip"]
1985 free_floating_ip
= fip
["id"]
1989 isinstance(floating_network
["floating_ip"], str)
1990 and floating_network
["floating_ip"].lower() != "true"
1992 pool_id
= floating_network
["floating_ip"]
1994 # Find the external network
1995 external_nets
= list()
1997 for net
in self
.neutron
.list_networks()["networks"]:
1998 if net
["router:external"]:
1999 external_nets
.append(net
)
2001 if len(external_nets
) == 0:
2002 raise vimconn
.VimConnException(
2003 "Cannot create floating_ip automatically since "
2004 "no external network is present",
2005 http_code
=vimconn
.HTTP_Conflict
,
2008 if len(external_nets
) > 1:
2009 raise vimconn
.VimConnException(
2010 "Cannot create floating_ip automatically since "
2011 "multiple external networks are present",
2012 http_code
=vimconn
.HTTP_Conflict
,
2015 pool_id
= external_nets
[0].get("id")
2019 "floating_network_id": pool_id
,
2020 "tenant_id": server
.tenant_id
,
2025 # self.logger.debug("Creating floating IP")
2026 new_floating_ip
= self
.neutron
.create_floatingip(param
)
2027 free_floating_ip
= new_floating_ip
["floatingip"]["id"]
2029 "floating_ip:" + str(free_floating_ip
)
2031 except Exception as e
:
2032 raise vimconn
.VimConnException(
2034 + ": Cannot create new floating_ip "
2036 http_code
=vimconn
.HTTP_Conflict
,
2040 # for race condition ensure not already assigned
2041 fip
= self
.neutron
.show_floatingip(free_floating_ip
)
2043 if fip
["floatingip"]["port_id"]:
2046 # the vim_id key contains the neutron.port_id
2047 self
.neutron
.update_floatingip(
2049 {"floatingip": {"port_id": floating_network
["vim_id"]}},
2051 # for race condition ensure not re-assigned to other VM after 5 seconds
2053 fip
= self
.neutron
.show_floatingip(free_floating_ip
)
2056 fip
["floatingip"]["port_id"]
2057 != floating_network
["vim_id"]
2060 "floating_ip {} re-assigned to other port".format(
2067 "Assigned floating_ip {} to VM {}".format(
2068 free_floating_ip
, server
.id
2072 except Exception as e
:
2073 # openstack need some time after VM creation to assign an IP. So retry if fails
2074 vm_status
= self
.nova
.servers
.get(server
.id).status
2076 if vm_status
not in ("ACTIVE", "ERROR"):
2077 if time
.time() - vm_start_time
< server_timeout
:
2080 elif floating_ip_retries
> 0:
2081 floating_ip_retries
-= 1
2084 raise vimconn
.VimConnException(
2085 "Cannot create floating_ip: {} {}".format(
2088 http_code
=vimconn
.HTTP_Conflict
,
2091 except Exception as e
:
2092 if not floating_network
["exit_on_floating_ip_error"]:
2093 self
.logger
.error("Cannot create floating_ip. %s", str(e
))
2098 return server
.id, created_items
2099 # except nvExceptions.NotFound as e:
2100 # error_value=-vimconn.HTTP_Not_Found
2101 # error_text= "vm instance %s not found" % vm_id
2102 # except TypeError as e:
2103 # raise vimconn.VimConnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
2105 except Exception as e
:
2108 server_id
= server
.id
2111 self
.delete_vminstance(server_id
, created_items
)
2112 except Exception as e2
:
2113 self
.logger
.error("new_vminstance rollback fail {}".format(e2
))
2115 self
._format
_exception
(e
)
2117 def get_vminstance(self
, vm_id
):
2118 """Returns the VM instance information from VIM"""
2119 # self.logger.debug("Getting VM from VIM")
2121 self
._reload
_connection
()
2122 server
= self
.nova
.servers
.find(id=vm_id
)
2123 # TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
2125 return server
.to_dict()
2127 ksExceptions
.ClientException
,
2128 nvExceptions
.ClientException
,
2129 nvExceptions
.NotFound
,
2132 self
._format
_exception
(e
)
2134 def get_vminstance_console(self
, vm_id
, console_type
="vnc"):
2136 Get a console for the virtual machine
2138 vm_id: uuid of the VM
2139 console_type, can be:
2140 "novnc" (by default), "xvpvnc" for VNC types,
2141 "rdp-html5" for RDP types, "spice-html5" for SPICE types
2142 Returns dict with the console parameters:
2143 protocol: ssh, ftp, http, https, ...
2144 server: usually ip address
2145 port: the http, ssh, ... port
2146 suffix: extra text, e.g. the http path and query string
2148 self
.logger
.debug("Getting VM CONSOLE from VIM")
2151 self
._reload
_connection
()
2152 server
= self
.nova
.servers
.find(id=vm_id
)
2154 if console_type
is None or console_type
== "novnc":
2155 console_dict
= server
.get_vnc_console("novnc")
2156 elif console_type
== "xvpvnc":
2157 console_dict
= server
.get_vnc_console(console_type
)
2158 elif console_type
== "rdp-html5":
2159 console_dict
= server
.get_rdp_console(console_type
)
2160 elif console_type
== "spice-html5":
2161 console_dict
= server
.get_spice_console(console_type
)
2163 raise vimconn
.VimConnException(
2164 "console type '{}' not allowed".format(console_type
),
2165 http_code
=vimconn
.HTTP_Bad_Request
,
2168 console_dict1
= console_dict
.get("console")
2171 console_url
= console_dict1
.get("url")
2175 protocol_index
= console_url
.find("//")
2177 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
2180 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
2185 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
2187 -vimconn
.HTTP_Internal_Server_Error
,
2188 "Unexpected response from VIM",
2192 "protocol": console_url
[0:protocol_index
],
2193 "server": console_url
[protocol_index
+ 2 : port_index
],
2194 "port": console_url
[port_index
:suffix_index
],
2195 "suffix": console_url
[suffix_index
+ 1 :],
2200 raise vimconn
.VimConnUnexpectedResponse("Unexpected response from VIM")
2202 nvExceptions
.NotFound
,
2203 ksExceptions
.ClientException
,
2204 nvExceptions
.ClientException
,
2205 nvExceptions
.BadRequest
,
2208 self
._format
_exception
(e
)
2210 def delete_vminstance(self
, vm_id
, created_items
=None):
2211 """Removes a VM instance from VIM. Returns the old identifier"""
2212 # print "osconnector: Getting VM from VIM"
2213 if created_items
is None:
2217 self
._reload
_connection
()
2218 # delete VM ports attached to this networks before the virtual machine
2219 for k
, v
in created_items
.items():
2220 if not v
: # skip already deleted
2224 k_item
, _
, k_id
= k
.partition(":")
2225 if k_item
== "port":
2226 self
.neutron
.delete_port(k_id
)
2227 except Exception as e
:
2229 "Error deleting port: {}: {}".format(type(e
).__name
__, e
)
2232 # #commented because detaching the volumes makes the servers.delete not work properly ?!?
2233 # #dettach volumes attached
2234 # server = self.nova.servers.get(vm_id)
2235 # volumes_attached_dict = server._info["os-extended-volumes:volumes_attached"] #volume["id"]
2236 # #for volume in volumes_attached_dict:
2237 # # self.cinder.volumes.detach(volume["id"])
2240 self
.nova
.servers
.delete(vm_id
)
2242 # delete volumes. Although having detached, they should have in active status before deleting
2243 # we ensure in this loop
2247 while keep_waiting
and elapsed_time
< volume_timeout
:
2248 keep_waiting
= False
2250 for k
, v
in created_items
.items():
2251 if not v
: # skip already deleted
2255 k_item
, _
, k_id
= k
.partition(":")
2256 if k_item
== "volume":
2257 if self
.cinder
.volumes
.get(k_id
).status
!= "available":
2260 self
.cinder
.volumes
.delete(k_id
)
2261 created_items
[k
] = None
2262 elif k_item
== "floating_ip": # floating ip
2263 self
.neutron
.delete_floatingip(k_id
)
2264 created_items
[k
] = None
2266 except Exception as e
:
2267 self
.logger
.error("Error deleting {}: {}".format(k
, e
))
2275 nvExceptions
.NotFound
,
2276 ksExceptions
.ClientException
,
2277 nvExceptions
.ClientException
,
2280 self
._format
_exception
(e
)
2282 def refresh_vms_status(self
, vm_list
):
2283 """Get the status of the virtual machines and their interfaces/ports
2284 Params: the list of VM identifiers
2285 Returns a dictionary with:
2286 vm_id: #VIM id of this Virtual Machine
2287 status: #Mandatory. Text with one of:
2288 # DELETED (not found at vim)
2289 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
2290 # OTHER (Vim reported other status not understood)
2291 # ERROR (VIM indicates an ERROR status)
2292 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
2293 # CREATING (on building process), ERROR
2294 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2296 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
2297 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2299 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2300 mac_address: #Text format XX:XX:XX:XX:XX:XX
2301 vim_net_id: #network id where this interface is connected
2302 vim_interface_id: #interface/port VIM id
2303 ip_address: #null, or text with IPv4, IPv6 address
2304 compute_node: #identification of compute node where PF,VF interface is allocated
2305 pci: #PCI address of the NIC that hosts the PF,VF
2306 vlan: #physical VLAN used for VF
2310 "refresh_vms status: Getting tenant VM instance information from VIM"
2313 for vm_id
in vm_list
:
2317 vm_vim
= self
.get_vminstance(vm_id
)
2319 if vm_vim
["status"] in vmStatus2manoFormat
:
2320 vm
["status"] = vmStatus2manoFormat
[vm_vim
["status"]]
2322 vm
["status"] = "OTHER"
2323 vm
["error_msg"] = "VIM status reported " + vm_vim
["status"]
2325 vm_vim
.pop("OS-EXT-SRV-ATTR:user_data", None)
2326 vm_vim
.pop("user_data", None)
2327 vm
["vim_info"] = self
.serialize(vm_vim
)
2329 vm
["interfaces"] = []
2330 if vm_vim
.get("fault"):
2331 vm
["error_msg"] = str(vm_vim
["fault"])
2335 self
._reload
_connection
()
2336 port_dict
= self
.neutron
.list_ports(device_id
=vm_id
)
2338 for port
in port_dict
["ports"]:
2340 interface
["vim_info"] = self
.serialize(port
)
2341 interface
["mac_address"] = port
.get("mac_address")
2342 interface
["vim_net_id"] = port
["network_id"]
2343 interface
["vim_interface_id"] = port
["id"]
2344 # check if OS-EXT-SRV-ATTR:host is there,
2345 # in case of non-admin credentials, it will be missing
2347 if vm_vim
.get("OS-EXT-SRV-ATTR:host"):
2348 interface
["compute_node"] = vm_vim
["OS-EXT-SRV-ATTR:host"]
2350 interface
["pci"] = None
2352 # check if binding:profile is there,
2353 # in case of non-admin credentials, it will be missing
2354 if port
.get("binding:profile"):
2355 if port
["binding:profile"].get("pci_slot"):
2356 # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting
2358 # TODO: This is just a workaround valid for niantinc. Find a better way to do so
2359 # CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2) assuming there are 2 ports per nic
2360 pci
= port
["binding:profile"]["pci_slot"]
2361 # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
2362 interface
["pci"] = pci
2364 interface
["vlan"] = None
2366 if port
.get("binding:vif_details"):
2367 interface
["vlan"] = port
["binding:vif_details"].get("vlan")
2369 # Get vlan from network in case not present in port for those old openstacks and cases where
2370 # it is needed vlan at PT
2371 if not interface
["vlan"]:
2372 # if network is of type vlan and port is of type direct (sr-iov) then set vlan id
2373 network
= self
.neutron
.show_network(port
["network_id"])
2376 network
["network"].get("provider:network_type")
2379 # and port.get("binding:vnic_type") in ("direct", "direct-physical"):
2380 interface
["vlan"] = network
["network"].get(
2381 "provider:segmentation_id"
2385 # look for floating ip address
2387 floating_ip_dict
= self
.neutron
.list_floatingips(
2391 if floating_ip_dict
.get("floatingips"):
2393 floating_ip_dict
["floatingips"][0].get(
2394 "floating_ip_address"
2400 for subnet
in port
["fixed_ips"]:
2401 ips
.append(subnet
["ip_address"])
2403 interface
["ip_address"] = ";".join(ips
)
2404 vm
["interfaces"].append(interface
)
2405 except Exception as e
:
2407 "Error getting vm interface information {}: {}".format(
2412 except vimconn
.VimConnNotFoundException
as e
:
2413 self
.logger
.error("Exception getting vm status: %s", str(e
))
2414 vm
["status"] = "DELETED"
2415 vm
["error_msg"] = str(e
)
2416 except vimconn
.VimConnException
as e
:
2417 self
.logger
.error("Exception getting vm status: %s", str(e
))
2418 vm
["status"] = "VIM_ERROR"
2419 vm
["error_msg"] = str(e
)
2425 def action_vminstance(self
, vm_id
, action_dict
, created_items
={}):
2426 """Send and action over a VM instance from VIM
2427 Returns None or the console dict if the action was successfully sent to the VIM"""
2428 self
.logger
.debug("Action over VM '%s': %s", vm_id
, str(action_dict
))
2431 self
._reload
_connection
()
2432 server
= self
.nova
.servers
.find(id=vm_id
)
2434 if "start" in action_dict
:
2435 if action_dict
["start"] == "rebuild":
2438 if server
.status
== "PAUSED":
2440 elif server
.status
== "SUSPENDED":
2442 elif server
.status
== "SHUTOFF":
2444 elif "pause" in action_dict
:
2446 elif "resume" in action_dict
:
2448 elif "shutoff" in action_dict
or "shutdown" in action_dict
:
2450 elif "forceOff" in action_dict
:
2451 server
.stop() # TODO
2452 elif "terminate" in action_dict
:
2454 elif "createImage" in action_dict
:
2455 server
.create_image()
2456 # "path":path_schema,
2457 # "description":description_schema,
2458 # "name":name_schema,
2459 # "metadata":metadata_schema,
2460 # "imageRef": id_schema,
2461 # "disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
2462 elif "rebuild" in action_dict
:
2463 server
.rebuild(server
.image
["id"])
2464 elif "reboot" in action_dict
:
2465 server
.reboot() # reboot_type="SOFT"
2466 elif "console" in action_dict
:
2467 console_type
= action_dict
["console"]
2469 if console_type
is None or console_type
== "novnc":
2470 console_dict
= server
.get_vnc_console("novnc")
2471 elif console_type
== "xvpvnc":
2472 console_dict
= server
.get_vnc_console(console_type
)
2473 elif console_type
== "rdp-html5":
2474 console_dict
= server
.get_rdp_console(console_type
)
2475 elif console_type
== "spice-html5":
2476 console_dict
= server
.get_spice_console(console_type
)
2478 raise vimconn
.VimConnException(
2479 "console type '{}' not allowed".format(console_type
),
2480 http_code
=vimconn
.HTTP_Bad_Request
,
2484 console_url
= console_dict
["console"]["url"]
2486 protocol_index
= console_url
.find("//")
2488 console_url
[protocol_index
+ 2 :].find("/") + protocol_index
+ 2
2491 console_url
[protocol_index
+ 2 : suffix_index
].find(":")
2496 if protocol_index
< 0 or port_index
< 0 or suffix_index
< 0:
2497 raise vimconn
.VimConnException(
2498 "Unexpected response from VIM " + str(console_dict
)
2502 "protocol": console_url
[0:protocol_index
],
2503 "server": console_url
[protocol_index
+ 2 : port_index
],
2504 "port": int(console_url
[port_index
+ 1 : suffix_index
]),
2505 "suffix": console_url
[suffix_index
+ 1 :],
2508 return console_dict2
2510 raise vimconn
.VimConnException(
2511 "Unexpected response from VIM " + str(console_dict
)
2516 ksExceptions
.ClientException
,
2517 nvExceptions
.ClientException
,
2518 nvExceptions
.NotFound
,
2521 self
._format
_exception
(e
)
2522 # TODO insert exception vimconn.HTTP_Unauthorized
2524 # ###### VIO Specific Changes #########
2525 def _generate_vlanID(self
):
2527 Method to get unused vlanID
2535 networks
= self
.get_network_list()
2537 for net
in networks
:
2538 if net
.get("provider:segmentation_id"):
2539 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
2541 used_vlanIDs
= set(usedVlanIDs
)
2543 # find unused VLAN ID
2544 for vlanID_range
in self
.config
.get("dataplane_net_vlan_range"):
2546 start_vlanid
, end_vlanid
= map(
2547 int, vlanID_range
.replace(" ", "").split("-")
2550 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
2551 if vlanID
not in used_vlanIDs
:
2553 except Exception as exp
:
2554 raise vimconn
.VimConnException(
2555 "Exception {} occurred while generating VLAN ID.".format(exp
)
2558 raise vimconn
.VimConnConflictException(
2559 "Unable to create the SRIOV VLAN network. All given Vlan IDs {} are in use.".format(
2560 self
.config
.get("dataplane_net_vlan_range")
2564 def _generate_multisegment_vlanID(self
):
2566 Method to get unused vlanID
2574 networks
= self
.get_network_list()
2575 for net
in networks
:
2576 if net
.get("provider:network_type") == "vlan" and net
.get(
2577 "provider:segmentation_id"
2579 usedVlanIDs
.append(net
.get("provider:segmentation_id"))
2580 elif net
.get("segments"):
2581 for segment
in net
.get("segments"):
2582 if segment
.get("provider:network_type") == "vlan" and segment
.get(
2583 "provider:segmentation_id"
2585 usedVlanIDs
.append(segment
.get("provider:segmentation_id"))
2587 used_vlanIDs
= set(usedVlanIDs
)
2589 # find unused VLAN ID
2590 for vlanID_range
in self
.config
.get("multisegment_vlan_range"):
2592 start_vlanid
, end_vlanid
= map(
2593 int, vlanID_range
.replace(" ", "").split("-")
2596 for vlanID
in range(start_vlanid
, end_vlanid
+ 1):
2597 if vlanID
not in used_vlanIDs
:
2599 except Exception as exp
:
2600 raise vimconn
.VimConnException(
2601 "Exception {} occurred while generating VLAN ID.".format(exp
)
2604 raise vimconn
.VimConnConflictException(
2605 "Unable to create the VLAN segment. All VLAN IDs {} are in use.".format(
2606 self
.config
.get("multisegment_vlan_range")
2610 def _validate_vlan_ranges(self
, input_vlan_range
, text_vlan_range
):
2612 Method to validate user given vlanID ranges
2616 for vlanID_range
in input_vlan_range
:
2617 vlan_range
= vlanID_range
.replace(" ", "")
2619 vlanID_pattern
= r
"(\d)*-(\d)*$"
2620 match_obj
= re
.match(vlanID_pattern
, vlan_range
)
2622 raise vimconn
.VimConnConflictException(
2623 "Invalid VLAN range for {}: {}.You must provide "
2624 "'{}' in format [start_ID - end_ID].".format(
2625 text_vlan_range
, vlanID_range
, text_vlan_range
2629 start_vlanid
, end_vlanid
= map(int, vlan_range
.split("-"))
2630 if start_vlanid
<= 0:
2631 raise vimconn
.VimConnConflictException(
2632 "Invalid VLAN range for {}: {}. Start ID can not be zero. For VLAN "
2633 "networks valid IDs are 1 to 4094 ".format(
2634 text_vlan_range
, vlanID_range
2638 if end_vlanid
> 4094:
2639 raise vimconn
.VimConnConflictException(
2640 "Invalid VLAN range for {}: {}. End VLAN ID can not be "
2641 "greater than 4094. For VLAN networks valid IDs are 1 to 4094 ".format(
2642 text_vlan_range
, vlanID_range
2646 if start_vlanid
> end_vlanid
:
2647 raise vimconn
.VimConnConflictException(
2648 "Invalid VLAN range for {}: {}. You must provide '{}'"
2649 " in format start_ID - end_ID and start_ID < end_ID ".format(
2650 text_vlan_range
, vlanID_range
, text_vlan_range
2654 # NOT USED FUNCTIONS
2656 def new_external_port(self
, port_data
):
2657 """Adds a external port to VIM
2658 Returns the port identifier"""
2659 # TODO openstack if needed
2661 -vimconn
.HTTP_Internal_Server_Error
,
2662 "osconnector.new_external_port() not implemented",
2665 def connect_port_network(self
, port_id
, network_id
, admin
=False):
2666 """Connects a external port to a network
2667 Returns status code of the VIM response"""
2668 # TODO openstack if needed
2670 -vimconn
.HTTP_Internal_Server_Error
,
2671 "osconnector.connect_port_network() not implemented",
2674 def new_user(self
, user_name
, user_passwd
, tenant_id
=None):
2675 """Adds a new user to openstack VIM
2676 Returns the user identifier"""
2677 self
.logger
.debug("osconnector: Adding a new user to VIM")
2680 self
._reload
_connection
()
2681 user
= self
.keystone
.users
.create(
2682 user_name
, password
=user_passwd
, default_project
=tenant_id
2684 # self.keystone.tenants.add_user(self.k_creds["username"], #role)
2687 except ksExceptions
.ConnectionError
as e
:
2688 error_value
= -vimconn
.HTTP_Bad_Request
2692 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2694 except ksExceptions
.ClientException
as e
: # TODO remove
2695 error_value
= -vimconn
.HTTP_Bad_Request
2699 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2702 # TODO insert exception vimconn.HTTP_Unauthorized
2703 # if reaching here is because an exception
2704 self
.logger
.debug("new_user " + error_text
)
2706 return error_value
, error_text
2708 def delete_user(self
, user_id
):
2709 """Delete a user from openstack VIM
2710 Returns the user identifier"""
2712 print("osconnector: Deleting a user from VIM")
2715 self
._reload
_connection
()
2716 self
.keystone
.users
.delete(user_id
)
2719 except ksExceptions
.ConnectionError
as e
:
2720 error_value
= -vimconn
.HTTP_Bad_Request
2724 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2726 except ksExceptions
.NotFound
as e
:
2727 error_value
= -vimconn
.HTTP_Not_Found
2731 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2733 except ksExceptions
.ClientException
as e
: # TODO remove
2734 error_value
= -vimconn
.HTTP_Bad_Request
2738 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2741 # TODO insert exception vimconn.HTTP_Unauthorized
2742 # if reaching here is because an exception
2743 self
.logger
.debug("delete_tenant " + error_text
)
2745 return error_value
, error_text
2747 def get_hosts_info(self
):
2748 """Get the information of deployed hosts
2749 Returns the hosts content"""
2751 print("osconnector: Getting Host info from VIM")
2755 self
._reload
_connection
()
2756 hypervisors
= self
.nova
.hypervisors
.list()
2758 for hype
in hypervisors
:
2759 h_list
.append(hype
.to_dict())
2761 return 1, {"hosts": h_list
}
2762 except nvExceptions
.NotFound
as e
:
2763 error_value
= -vimconn
.HTTP_Not_Found
2764 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
2765 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
2766 error_value
= -vimconn
.HTTP_Bad_Request
2770 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2773 # TODO insert exception vimconn.HTTP_Unauthorized
2774 # if reaching here is because an exception
2775 self
.logger
.debug("get_hosts_info " + error_text
)
2777 return error_value
, error_text
2779 def get_hosts(self
, vim_tenant
):
2780 """Get the hosts and deployed instances
2781 Returns the hosts content"""
2782 r
, hype_dict
= self
.get_hosts_info()
2787 hypervisors
= hype_dict
["hosts"]
2790 servers
= self
.nova
.servers
.list()
2791 for hype
in hypervisors
:
2792 for server
in servers
:
2794 server
.to_dict()["OS-EXT-SRV-ATTR:hypervisor_hostname"]
2795 == hype
["hypervisor_hostname"]
2798 hype
["vm"].append(server
.id)
2800 hype
["vm"] = [server
.id]
2803 except nvExceptions
.NotFound
as e
:
2804 error_value
= -vimconn
.HTTP_Not_Found
2805 error_text
= str(e
) if len(e
.args
) == 0 else str(e
.args
[0])
2806 except (ksExceptions
.ClientException
, nvExceptions
.ClientException
) as e
:
2807 error_value
= -vimconn
.HTTP_Bad_Request
2811 + (str(e
) if len(e
.args
) == 0 else str(e
.args
[0]))
2814 # TODO insert exception vimconn.HTTP_Unauthorized
2815 # if reaching here is because an exception
2816 self
.logger
.debug("get_hosts " + error_text
)
2818 return error_value
, error_text
2820 def new_classification(self
, name
, ctype
, definition
):
2822 "Adding a new (Traffic) Classification to VIM, named %s", name
2827 self
._reload
_connection
()
2829 if ctype
not in supportedClassificationTypes
:
2830 raise vimconn
.VimConnNotSupportedException(
2831 "OpenStack VIM connector does not support provided "
2832 "Classification Type {}, supported ones are: {}".format(
2833 ctype
, supportedClassificationTypes
2837 if not self
._validate
_classification
(ctype
, definition
):
2838 raise vimconn
.VimConnException(
2839 "Incorrect Classification definition for the type specified."
2842 classification_dict
= definition
2843 classification_dict
["name"] = name
2844 new_class
= self
.neutron
.create_sfc_flow_classifier(
2845 {"flow_classifier": classification_dict
}
2848 return new_class
["flow_classifier"]["id"]
2850 neExceptions
.ConnectionFailed
,
2851 ksExceptions
.ClientException
,
2852 neExceptions
.NeutronException
,
2855 self
.logger
.error("Creation of Classification failed.")
2856 self
._format
_exception
(e
)
2858 def get_classification(self
, class_id
):
2859 self
.logger
.debug(" Getting Classification %s from VIM", class_id
)
2860 filter_dict
= {"id": class_id
}
2861 class_list
= self
.get_classification_list(filter_dict
)
2863 if len(class_list
) == 0:
2864 raise vimconn
.VimConnNotFoundException(
2865 "Classification '{}' not found".format(class_id
)
2867 elif len(class_list
) > 1:
2868 raise vimconn
.VimConnConflictException(
2869 "Found more than one Classification with this criteria"
2872 classification
= class_list
[0]
2874 return classification
2876 def get_classification_list(self
, filter_dict
={}):
2878 "Getting Classifications from VIM filter: '%s'", str(filter_dict
)
2882 filter_dict_os
= filter_dict
.copy()
2883 self
._reload
_connection
()
2885 if self
.api_version3
and "tenant_id" in filter_dict_os
:
2886 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
2888 classification_dict
= self
.neutron
.list_sfc_flow_classifiers(
2891 classification_list
= classification_dict
["flow_classifiers"]
2892 self
.__classification
_os
2mano
(classification_list
)
2894 return classification_list
2896 neExceptions
.ConnectionFailed
,
2897 ksExceptions
.ClientException
,
2898 neExceptions
.NeutronException
,
2901 self
._format
_exception
(e
)
2903 def delete_classification(self
, class_id
):
2904 self
.logger
.debug("Deleting Classification '%s' from VIM", class_id
)
2907 self
._reload
_connection
()
2908 self
.neutron
.delete_sfc_flow_classifier(class_id
)
2912 neExceptions
.ConnectionFailed
,
2913 neExceptions
.NeutronException
,
2914 ksExceptions
.ClientException
,
2915 neExceptions
.NeutronException
,
2918 self
._format
_exception
(e
)
2920 def new_sfi(self
, name
, ingress_ports
, egress_ports
, sfc_encap
=True):
2922 "Adding a new Service Function Instance to VIM, named '%s'", name
2927 self
._reload
_connection
()
2933 if len(ingress_ports
) != 1:
2934 raise vimconn
.VimConnNotSupportedException(
2935 "OpenStack VIM connector can only have 1 ingress port per SFI"
2938 if len(egress_ports
) != 1:
2939 raise vimconn
.VimConnNotSupportedException(
2940 "OpenStack VIM connector can only have 1 egress port per SFI"
2945 "ingress": ingress_ports
[0],
2946 "egress": egress_ports
[0],
2947 "service_function_parameters": {"correlation": correlation
},
2949 new_sfi
= self
.neutron
.create_sfc_port_pair({"port_pair": sfi_dict
})
2951 return new_sfi
["port_pair"]["id"]
2953 neExceptions
.ConnectionFailed
,
2954 ksExceptions
.ClientException
,
2955 neExceptions
.NeutronException
,
2960 self
.neutron
.delete_sfc_port_pair(new_sfi
["port_pair"]["id"])
2963 "Creation of Service Function Instance failed, with "
2964 "subsequent deletion failure as well."
2967 self
._format
_exception
(e
)
2969 def get_sfi(self
, sfi_id
):
2970 self
.logger
.debug("Getting Service Function Instance %s from VIM", sfi_id
)
2971 filter_dict
= {"id": sfi_id
}
2972 sfi_list
= self
.get_sfi_list(filter_dict
)
2974 if len(sfi_list
) == 0:
2975 raise vimconn
.VimConnNotFoundException(
2976 "Service Function Instance '{}' not found".format(sfi_id
)
2978 elif len(sfi_list
) > 1:
2979 raise vimconn
.VimConnConflictException(
2980 "Found more than one Service Function Instance with this criteria"
2987 def get_sfi_list(self
, filter_dict
={}):
2989 "Getting Service Function Instances from VIM filter: '%s'", str(filter_dict
)
2993 self
._reload
_connection
()
2994 filter_dict_os
= filter_dict
.copy()
2996 if self
.api_version3
and "tenant_id" in filter_dict_os
:
2997 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
2999 sfi_dict
= self
.neutron
.list_sfc_port_pairs(**filter_dict_os
)
3000 sfi_list
= sfi_dict
["port_pairs"]
3001 self
.__sfi
_os
2mano
(sfi_list
)
3005 neExceptions
.ConnectionFailed
,
3006 ksExceptions
.ClientException
,
3007 neExceptions
.NeutronException
,
3010 self
._format
_exception
(e
)
3012 def delete_sfi(self
, sfi_id
):
3013 self
.logger
.debug("Deleting Service Function Instance '%s' from VIM", sfi_id
)
3016 self
._reload
_connection
()
3017 self
.neutron
.delete_sfc_port_pair(sfi_id
)
3021 neExceptions
.ConnectionFailed
,
3022 neExceptions
.NeutronException
,
3023 ksExceptions
.ClientException
,
3024 neExceptions
.NeutronException
,
3027 self
._format
_exception
(e
)
3029 def new_sf(self
, name
, sfis
, sfc_encap
=True):
3030 self
.logger
.debug("Adding a new Service Function to VIM, named '%s'", name
)
3034 self
._reload
_connection
()
3035 # correlation = None
3037 # correlation = "nsh"
3039 for instance
in sfis
:
3040 sfi
= self
.get_sfi(instance
)
3042 if sfi
.get("sfc_encap") != sfc_encap
:
3043 raise vimconn
.VimConnNotSupportedException(
3044 "OpenStack VIM connector requires all SFIs of the "
3045 "same SF to share the same SFC Encapsulation"
3048 sf_dict
= {"name": name
, "port_pairs": sfis
}
3049 new_sf
= self
.neutron
.create_sfc_port_pair_group(
3050 {"port_pair_group": sf_dict
}
3053 return new_sf
["port_pair_group"]["id"]
3055 neExceptions
.ConnectionFailed
,
3056 ksExceptions
.ClientException
,
3057 neExceptions
.NeutronException
,
3062 self
.neutron
.delete_sfc_port_pair_group(
3063 new_sf
["port_pair_group"]["id"]
3067 "Creation of Service Function failed, with "
3068 "subsequent deletion failure as well."
3071 self
._format
_exception
(e
)
3073 def get_sf(self
, sf_id
):
3074 self
.logger
.debug("Getting Service Function %s from VIM", sf_id
)
3075 filter_dict
= {"id": sf_id
}
3076 sf_list
= self
.get_sf_list(filter_dict
)
3078 if len(sf_list
) == 0:
3079 raise vimconn
.VimConnNotFoundException(
3080 "Service Function '{}' not found".format(sf_id
)
3082 elif len(sf_list
) > 1:
3083 raise vimconn
.VimConnConflictException(
3084 "Found more than one Service Function with this criteria"
3091 def get_sf_list(self
, filter_dict
={}):
3093 "Getting Service Function from VIM filter: '%s'", str(filter_dict
)
3097 self
._reload
_connection
()
3098 filter_dict_os
= filter_dict
.copy()
3100 if self
.api_version3
and "tenant_id" in filter_dict_os
:
3101 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
3103 sf_dict
= self
.neutron
.list_sfc_port_pair_groups(**filter_dict_os
)
3104 sf_list
= sf_dict
["port_pair_groups"]
3105 self
.__sf
_os
2mano
(sf_list
)
3109 neExceptions
.ConnectionFailed
,
3110 ksExceptions
.ClientException
,
3111 neExceptions
.NeutronException
,
3114 self
._format
_exception
(e
)
3116 def delete_sf(self
, sf_id
):
3117 self
.logger
.debug("Deleting Service Function '%s' from VIM", sf_id
)
3120 self
._reload
_connection
()
3121 self
.neutron
.delete_sfc_port_pair_group(sf_id
)
3125 neExceptions
.ConnectionFailed
,
3126 neExceptions
.NeutronException
,
3127 ksExceptions
.ClientException
,
3128 neExceptions
.NeutronException
,
3131 self
._format
_exception
(e
)
3133 def new_sfp(self
, name
, classifications
, sfs
, sfc_encap
=True, spi
=None):
3134 self
.logger
.debug("Adding a new Service Function Path to VIM, named '%s'", name
)
3138 self
._reload
_connection
()
3139 # In networking-sfc the MPLS encapsulation is legacy
3140 # should be used when no full SFC Encapsulation is intended
3141 correlation
= "mpls"
3148 "flow_classifiers": classifications
,
3149 "port_pair_groups": sfs
,
3150 "chain_parameters": {"correlation": correlation
},
3154 sfp_dict
["chain_id"] = spi
3156 new_sfp
= self
.neutron
.create_sfc_port_chain({"port_chain": sfp_dict
})
3158 return new_sfp
["port_chain"]["id"]
3160 neExceptions
.ConnectionFailed
,
3161 ksExceptions
.ClientException
,
3162 neExceptions
.NeutronException
,
3167 self
.neutron
.delete_sfc_port_chain(new_sfp
["port_chain"]["id"])
3170 "Creation of Service Function Path failed, with "
3171 "subsequent deletion failure as well."
3174 self
._format
_exception
(e
)
3176 def get_sfp(self
, sfp_id
):
3177 self
.logger
.debug(" Getting Service Function Path %s from VIM", sfp_id
)
3179 filter_dict
= {"id": sfp_id
}
3180 sfp_list
= self
.get_sfp_list(filter_dict
)
3182 if len(sfp_list
) == 0:
3183 raise vimconn
.VimConnNotFoundException(
3184 "Service Function Path '{}' not found".format(sfp_id
)
3186 elif len(sfp_list
) > 1:
3187 raise vimconn
.VimConnConflictException(
3188 "Found more than one Service Function Path with this criteria"
3195 def get_sfp_list(self
, filter_dict
={}):
3197 "Getting Service Function Paths from VIM filter: '%s'", str(filter_dict
)
3201 self
._reload
_connection
()
3202 filter_dict_os
= filter_dict
.copy()
3204 if self
.api_version3
and "tenant_id" in filter_dict_os
:
3205 filter_dict_os
["project_id"] = filter_dict_os
.pop("tenant_id")
3207 sfp_dict
= self
.neutron
.list_sfc_port_chains(**filter_dict_os
)
3208 sfp_list
= sfp_dict
["port_chains"]
3209 self
.__sfp
_os
2mano
(sfp_list
)
3213 neExceptions
.ConnectionFailed
,
3214 ksExceptions
.ClientException
,
3215 neExceptions
.NeutronException
,
3218 self
._format
_exception
(e
)
3220 def delete_sfp(self
, sfp_id
):
3221 self
.logger
.debug("Deleting Service Function Path '%s' from VIM", sfp_id
)
3224 self
._reload
_connection
()
3225 self
.neutron
.delete_sfc_port_chain(sfp_id
)
3229 neExceptions
.ConnectionFailed
,
3230 neExceptions
.NeutronException
,
3231 ksExceptions
.ClientException
,
3232 neExceptions
.NeutronException
,
3235 self
._format
_exception
(e
)
3237 def refresh_sfps_status(self
, sfp_list
):
3238 """Get the status of the service function path
3239 Params: the list of sfp identifiers
3240 Returns a dictionary with:
3241 vm_id: #VIM id of this service function path
3242 status: #Mandatory. Text with one of:
3243 # DELETED (not found at vim)
3244 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3245 # OTHER (Vim reported other status not understood)
3246 # ERROR (VIM indicates an ERROR status)
3248 # CREATING (on building process)
3249 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3250 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)F
3254 "refresh_sfps status: Getting tenant SFP information from VIM"
3257 for sfp_id
in sfp_list
:
3261 sfp_vim
= self
.get_sfp(sfp_id
)
3264 sfp
["status"] = vmStatus2manoFormat
["ACTIVE"]
3266 sfp
["status"] = "OTHER"
3267 sfp
["error_msg"] = "VIM status reported " + sfp
["status"]
3269 sfp
["vim_info"] = self
.serialize(sfp_vim
)
3271 if sfp_vim
.get("fault"):
3272 sfp
["error_msg"] = str(sfp_vim
["fault"])
3273 except vimconn
.VimConnNotFoundException
as e
:
3274 self
.logger
.error("Exception getting sfp status: %s", str(e
))
3275 sfp
["status"] = "DELETED"
3276 sfp
["error_msg"] = str(e
)
3277 except vimconn
.VimConnException
as e
:
3278 self
.logger
.error("Exception getting sfp status: %s", str(e
))
3279 sfp
["status"] = "VIM_ERROR"
3280 sfp
["error_msg"] = str(e
)
3282 sfp_dict
[sfp_id
] = sfp
3286 def refresh_sfis_status(self
, sfi_list
):
3287 """Get the status of the service function instances
3288 Params: the list of sfi identifiers
3289 Returns a dictionary with:
3290 vm_id: #VIM id of this service function instance
3291 status: #Mandatory. Text with one of:
3292 # DELETED (not found at vim)
3293 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3294 # OTHER (Vim reported other status not understood)
3295 # ERROR (VIM indicates an ERROR status)
3297 # CREATING (on building process)
3298 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3299 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3303 "refresh_sfis status: Getting tenant sfi information from VIM"
3306 for sfi_id
in sfi_list
:
3310 sfi_vim
= self
.get_sfi(sfi_id
)
3313 sfi
["status"] = vmStatus2manoFormat
["ACTIVE"]
3315 sfi
["status"] = "OTHER"
3316 sfi
["error_msg"] = "VIM status reported " + sfi
["status"]
3318 sfi
["vim_info"] = self
.serialize(sfi_vim
)
3320 if sfi_vim
.get("fault"):
3321 sfi
["error_msg"] = str(sfi_vim
["fault"])
3322 except vimconn
.VimConnNotFoundException
as e
:
3323 self
.logger
.error("Exception getting sfi status: %s", str(e
))
3324 sfi
["status"] = "DELETED"
3325 sfi
["error_msg"] = str(e
)
3326 except vimconn
.VimConnException
as e
:
3327 self
.logger
.error("Exception getting sfi status: %s", str(e
))
3328 sfi
["status"] = "VIM_ERROR"
3329 sfi
["error_msg"] = str(e
)
3331 sfi_dict
[sfi_id
] = sfi
3335 def refresh_sfs_status(self
, sf_list
):
3336 """Get the status of the service functions
3337 Params: the list of sf identifiers
3338 Returns a dictionary with:
3339 vm_id: #VIM id of this service function
3340 status: #Mandatory. Text with one of:
3341 # DELETED (not found at vim)
3342 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3343 # OTHER (Vim reported other status not understood)
3344 # ERROR (VIM indicates an ERROR status)
3346 # CREATING (on building process)
3347 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3348 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3351 self
.logger
.debug("refresh_sfs status: Getting tenant sf information from VIM")
3353 for sf_id
in sf_list
:
3357 sf_vim
= self
.get_sf(sf_id
)
3360 sf
["status"] = vmStatus2manoFormat
["ACTIVE"]
3362 sf
["status"] = "OTHER"
3363 sf
["error_msg"] = "VIM status reported " + sf_vim
["status"]
3365 sf
["vim_info"] = self
.serialize(sf_vim
)
3367 if sf_vim
.get("fault"):
3368 sf
["error_msg"] = str(sf_vim
["fault"])
3369 except vimconn
.VimConnNotFoundException
as e
:
3370 self
.logger
.error("Exception getting sf status: %s", str(e
))
3371 sf
["status"] = "DELETED"
3372 sf
["error_msg"] = str(e
)
3373 except vimconn
.VimConnException
as e
:
3374 self
.logger
.error("Exception getting sf status: %s", str(e
))
3375 sf
["status"] = "VIM_ERROR"
3376 sf
["error_msg"] = str(e
)
3382 def refresh_classifications_status(self
, classification_list
):
3383 """Get the status of the classifications
3384 Params: the list of classification identifiers
3385 Returns a dictionary with:
3386 vm_id: #VIM id of this classifier
3387 status: #Mandatory. Text with one of:
3388 # DELETED (not found at vim)
3389 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
3390 # OTHER (Vim reported other status not understood)
3391 # ERROR (VIM indicates an ERROR status)
3393 # CREATING (on building process)
3394 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
3395 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
3397 classification_dict
= {}
3399 "refresh_classifications status: Getting tenant classification information from VIM"
3402 for classification_id
in classification_list
:
3406 classification_vim
= self
.get_classification(classification_id
)
3408 if classification_vim
:
3409 classification
["status"] = vmStatus2manoFormat
["ACTIVE"]
3411 classification
["status"] = "OTHER"
3412 classification
["error_msg"] = (
3413 "VIM status reported " + classification
["status"]
3416 classification
["vim_info"] = self
.serialize(classification_vim
)
3418 if classification_vim
.get("fault"):
3419 classification
["error_msg"] = str(classification_vim
["fault"])
3420 except vimconn
.VimConnNotFoundException
as e
:
3421 self
.logger
.error("Exception getting classification status: %s", str(e
))
3422 classification
["status"] = "DELETED"
3423 classification
["error_msg"] = str(e
)
3424 except vimconn
.VimConnException
as e
:
3425 self
.logger
.error("Exception getting classification status: %s", str(e
))
3426 classification
["status"] = "VIM_ERROR"
3427 classification
["error_msg"] = str(e
)
3429 classification_dict
[classification_id
] = classification
3431 return classification_dict