blob: ba670c74dfd148b3d9003fc9a5b09098da103aec [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24'''
25NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
26'''
27__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28__date__ ="$16-sep-2014 22:05:01$"
29
30import imp
31#import json
32import yaml
tierno42fcc3b2016-07-06 17:20:40 +020033import utils
tiernof97fd272016-07-11 14:32:37 +020034from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\
tierno7edb6752016-03-21 17:37:52 +010035 HTTP_Conflict, HTTP_Method_Not_Allowed
36import console_proxy_thread as cli
tiernoae4a8d12016-07-08 12:30:39 +020037import vimconn
38import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020039import collections
tiernof97fd272016-07-11 14:32:37 +020040from db_base import db_base_Exception
tierno7edb6752016-03-21 17:37:52 +010041
42global global_config
43global vimconn_imported
tierno73ad9e42016-09-12 18:11:11 +020044global logger
montesmoreno0c8def02016-12-22 12:16:23 +000045global default_volume_size
46default_volume_size = '5' #size in GB
tierno7edb6752016-03-21 17:37:52 +010047
tiernoae4a8d12016-07-08 12:30:39 +020048
tierno7edb6752016-03-21 17:37:52 +010049vimconn_imported={} #dictionary with VIM type as key, loaded module as value
tierno73ad9e42016-09-12 18:11:11 +020050logger = logging.getLogger('openmano.nfvo')
tierno7edb6752016-03-21 17:37:52 +010051
52class NfvoException(Exception):
tiernoae4a8d12016-07-08 12:30:39 +020053 def __init__(self, message, http_code):
54 self.http_code = http_code
55 Exception.__init__(self, message)
tierno7edb6752016-03-21 17:37:52 +010056
57
58def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
59 '''Obtain flavorList
60 return result, content:
61 <0, error_text upon error
62 nb_records, flavor_list on success
63 '''
64 WHERE_dict={}
65 WHERE_dict['vnf_id'] = vnf_id
66 if nfvo_tenant is not None:
67 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
68
69 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
70 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +020071 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
72 #print "get_flavor_list result:", result
73 #print "get_flavor_list content:", content
tierno7edb6752016-03-21 17:37:52 +010074 flavorList=[]
tiernof97fd272016-07-11 14:32:37 +020075 for flavor in flavors:
tierno7edb6752016-03-21 17:37:52 +010076 flavorList.append(flavor['flavor_id'])
tiernof97fd272016-07-11 14:32:37 +020077 return flavorList
tierno7edb6752016-03-21 17:37:52 +010078
79def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
80 '''Obtain imageList
81 return result, content:
82 <0, error_text upon error
83 nb_records, flavor_list on success
84 '''
85 WHERE_dict={}
86 WHERE_dict['vnf_id'] = vnf_id
87 if nfvo_tenant is not None:
88 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
89
90 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
tiernof97fd272016-07-11 14:32:37 +020091 images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
tierno7edb6752016-03-21 17:37:52 +010092 imageList=[]
tiernof97fd272016-07-11 14:32:37 +020093 for image in images:
tierno7edb6752016-03-21 17:37:52 +010094 imageList.append(image['image_id'])
tiernof97fd272016-07-11 14:32:37 +020095 return imageList
tierno7edb6752016-03-21 17:37:52 +010096
tiernoa2793912016-10-04 08:15:08 +000097def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
98 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
tierno7edb6752016-03-21 17:37:52 +010099 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
tiernobe41e222016-09-02 15:16:13 +0200100 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
tierno7edb6752016-03-21 17:37:52 +0100101 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
tiernobe41e222016-09-02 15:16:13 +0200102 raise exception upon error
tierno7edb6752016-03-21 17:37:52 +0100103 '''
104 WHERE_dict={}
105 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
106 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
tiernoa2793912016-10-04 08:15:08 +0000107 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
tierno7edb6752016-03-21 17:37:52 +0100108 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
109 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
tiernoa2793912016-10-04 08:15:08 +0000110 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
111 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
tierno7edb6752016-03-21 17:37:52 +0100112 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
tierno8008c3a2016-10-13 15:34:28 +0000113 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
tierno7edb6752016-03-21 17:37:52 +0100114 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
tierno8008c3a2016-10-13 15:34:28 +0000115 'user','passwd', 'dt.config as dt_config')
tierno7edb6752016-03-21 17:37:52 +0100116 else:
117 from_ = 'datacenters as d'
118 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
tiernof97fd272016-07-11 14:32:37 +0200119 try:
120 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
121 vim_dict={}
122 for vim in vims:
123 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id')}
tierno8008c3a2016-10-13 15:34:28 +0000124 if vim["config"]:
tiernof97fd272016-07-11 14:32:37 +0200125 extra.update(yaml.load(vim["config"]))
tierno8008c3a2016-10-13 15:34:28 +0000126 if vim.get('dt_config'):
127 extra.update(yaml.load(vim["dt_config"]))
tiernof97fd272016-07-11 14:32:37 +0200128 if vim["type"] not in vimconn_imported:
129 module_info=None
130 try:
131 module = "vimconn_" + vim["type"]
132 module_info = imp.find_module(module)
133 vim_conn = imp.load_module(vim["type"], *module_info)
134 vimconn_imported[vim["type"]] = vim_conn
135 except (IOError, ImportError) as e:
136 if module_info and module_info[0]:
137 file.close(module_info[0])
138 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
139 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
140
tierno7edb6752016-03-21 17:37:52 +0100141 try:
tiernof97fd272016-07-11 14:32:37 +0200142 #if not tenant:
143 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
144 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
145 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
tierno3ae39742016-09-07 12:17:51 +0200146 tenant_id=vim.get('vim_tenant_id',vim_tenant), tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
tiernof97fd272016-07-11 14:32:37 +0200147 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
tierno3ae39742016-09-07 12:17:51 +0200148 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
tiernof97fd272016-07-11 14:32:37 +0200149 config=extra
150 )
151 except Exception as e:
152 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
153 return vim_dict
154 except db_base_Exception as e:
155 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
156
tierno7edb6752016-03-21 17:37:52 +0100157def rollback(mydb, vims, rollback_list):
158 undeleted_items=[]
159 #delete things by reverse order
160 for i in range(len(rollback_list)-1, -1, -1):
161 item = rollback_list[i]
162 if item["where"]=="vim":
163 if item["vim_id"] not in vims:
164 continue
165 vim=vims[ item["vim_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200166 try:
167 if item["what"]=="image":
168 vim.delete_image(item["uuid"])
tiernof97fd272016-07-11 14:32:37 +0200169 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200170 elif item["what"]=="flavor":
171 vim.delete_flavor(item["uuid"])
garciadeblas9f8456e2016-09-05 05:02:59 +0200172 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
tiernoae4a8d12016-07-08 12:30:39 +0200173 elif item["what"]=="network":
174 vim.delete_network(item["uuid"])
175 elif item["what"]=="vm":
176 vim.delete_vminstance(item["uuid"])
177 except vimconn.vimconnException as e:
178 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
179 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
tiernof97fd272016-07-11 14:32:37 +0200180 except db_base_Exception as e:
181 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200182
tierno7edb6752016-03-21 17:37:52 +0100183 else: # where==mano
tiernof97fd272016-07-11 14:32:37 +0200184 try:
185 if item["what"]=="image":
186 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
187 elif item["what"]=="flavor":
188 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
189 except db_base_Exception as e:
190 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
191 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
tierno7edb6752016-03-21 17:37:52 +0100192 if len(undeleted_items)==0:
193 return True," Rollback successful."
194 else:
195 return False," Rollback fails to delete: " + str(undeleted_items)
196
tiernoafed5f12017-01-26 17:57:43 +0100197def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
tierno7edb6752016-03-21 17:37:52 +0100198 global global_config
199 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
200 vnfc_interfaces={}
201 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
tiernoafed5f12017-01-26 17:57:43 +0100202 name_dict = {}
tierno7edb6752016-03-21 17:37:52 +0100203 #dataplane interfaces
204 for numa in vnfc.get("numas",() ):
205 for interface in numa.get("interfaces",()):
tiernoafed5f12017-01-26 17:57:43 +0100206 if interface["name"] in name_dict:
207 raise NfvoException(
208 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
209 vnfc["name"], interface["name"]),
210 HTTP_Bad_Request)
211 name_dict[ interface["name"] ] = "underlay"
tierno7edb6752016-03-21 17:37:52 +0100212 #bridge interfaces
213 for interface in vnfc.get("bridge-ifaces",() ):
tiernoafed5f12017-01-26 17:57:43 +0100214 if interface["name"] in name_dict:
215 raise NfvoException(
216 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
217 vnfc["name"], interface["name"]),
218 HTTP_Bad_Request)
219 name_dict[ interface["name"] ] = "overlay"
220 vnfc_interfaces[ vnfc["name"] ] = name_dict
tierno36c0b172017-01-12 18:32:28 +0100221 # check bood-data info
222 if "boot-data" in vnfc:
223 # check that user-data is incompatible with users and config-files
224 if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
225 raise NfvoException(
226 "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
227 HTTP_Bad_Request)
228
tierno7edb6752016-03-21 17:37:52 +0100229 #check if the info in external_connections matches with the one in the vnfcs
230 name_list=[]
231 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
232 if external_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100233 raise NfvoException(
234 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
235 external_connection["name"]),
236 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100237 name_list.append(external_connection["name"])
238 if external_connection["VNFC"] not in vnfc_interfaces:
tiernoafed5f12017-01-26 17:57:43 +0100239 raise NfvoException(
240 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
241 external_connection["name"], external_connection["VNFC"]),
242 HTTP_Bad_Request)
tiernof97fd272016-07-11 14:32:37 +0200243
tierno7edb6752016-03-21 17:37:52 +0100244 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernoafed5f12017-01-26 17:57:43 +0100245 raise NfvoException(
246 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
247 external_connection["name"],
248 external_connection["local_iface_name"]),
249 HTTP_Bad_Request )
tierno7edb6752016-03-21 17:37:52 +0100250
251 #check if the info in internal_connections matches with the one in the vnfcs
252 name_list=[]
253 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
254 if internal_connection["name"] in name_list:
tiernoafed5f12017-01-26 17:57:43 +0100255 raise NfvoException(
256 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
257 internal_connection["name"]),
258 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100259 name_list.append(internal_connection["name"])
260 #We should check that internal-connections of type "ptp" have only 2 elements
tiernoafed5f12017-01-26 17:57:43 +0100261
262 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
263 raise NfvoException(
264 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
265 internal_connection["name"],
266 'ptp' if vnf_descriptor_version==1 else 'e-line',
267 'data' if vnf_descriptor_version==1 else "e-lan"),
268 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100269 for port in internal_connection["elements"]:
tiernoafed5f12017-01-26 17:57:43 +0100270 vnf = port["VNFC"]
271 iface = port["local_iface_name"]
272 if vnf not in vnfc_interfaces:
273 raise NfvoException(
274 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
275 internal_connection["name"], vnf),
276 HTTP_Bad_Request)
277 if iface not in vnfc_interfaces[ vnf ]:
278 raise NfvoException(
279 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
280 internal_connection["name"], iface),
281 HTTP_Bad_Request)
282 return -HTTP_Bad_Request,
283 if vnf_descriptor_version==1 and "type" not in internal_connection:
284 if vnfc_interfaces[vnf][iface] == "overlay":
285 internal_connection["type"] = "bridge"
286 else:
287 internal_connection["type"] = "data"
288 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
289 if vnfc_interfaces[vnf][iface] == "overlay":
290 internal_connection["implementation"] = "overlay"
291 else:
292 internal_connection["implementation"] = "underlay"
293 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
294 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
295 raise NfvoException(
296 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
297 internal_connection["name"],
298 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
299 'data' if vnf_descriptor_version==1 else 'underlay'),
300 HTTP_Bad_Request)
301 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
302 vnfc_interfaces[vnf][iface] == "underlay":
303 raise NfvoException(
304 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
305 internal_connection["name"], iface,
306 'data' if vnf_descriptor_version==1 else 'underlay',
307 'bridge' if vnf_descriptor_version==1 else 'overlay'),
308 HTTP_Bad_Request)
309
tierno7edb6752016-03-21 17:37:52 +0100310
tierno5e91eb82016-10-04 09:39:07 +0000311def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
tierno7edb6752016-03-21 17:37:52 +0100312 #look if image exist
313 if only_create_at_vim:
314 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000315 if return_on_error == None:
316 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100317 else:
garciadeblas14480452017-01-10 13:08:07 +0100318 if image_dict['location']:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200319 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
320 else:
321 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200322 if len(images)>=1:
323 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100324 else:
garciadeblas14480452017-01-10 13:08:07 +0100325 #create image in MANO DB
tierno7edb6752016-03-21 17:37:52 +0100326 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200327 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
328 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100329 }
garciadeblas14480452017-01-10 13:08:07 +0100330 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
tiernof97fd272016-07-11 14:32:37 +0200331 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
332 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100333 #create image at every vim
334 for vim_id,vim in vims.iteritems():
335 image_created="false"
336 #look at database
tiernof97fd272016-07-11 14:32:37 +0200337 image_db = mydb.get_rows(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100338 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200339 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200340 if image_dict['location'] is not None:
341 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
342 else:
garciadeblas30833382017-01-09 09:46:31 +0100343 filter_dict = {}
344 filter_dict['name'] = image_dict['universal_name']
345 if image_dict.get('checksum') != None:
346 filter_dict['checksum'] = image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000347 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200348 vim_images = vim.get_image_list(filter_dict)
garciadeblas14480452017-01-10 13:08:07 +0100349 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200350 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100351 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000352 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100353 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200354 else:
garciadeblas14480452017-01-10 13:08:07 +0100355 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
356 image_vim_id = vim_images[0]['id']
garciadeblasb69fa9f2016-09-28 12:04:10 +0200357
tiernoae4a8d12016-07-08 12:30:39 +0200358 except vimconn.vimconnNotFoundException as e:
garciadeblas14480452017-01-10 13:08:07 +0100359 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
tiernoae4a8d12016-07-08 12:30:39 +0200360 try:
garciadeblas14480452017-01-10 13:08:07 +0100361 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
362 if image_dict['location']:
363 image_vim_id = vim.new_image(image_dict)
364 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
365 image_created="true"
366 else:
367 raise vimconn.vimconnException("Cannot create image without location")
tiernoae4a8d12016-07-08 12:30:39 +0200368 except vimconn.vimconnException as e:
369 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100370 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200371 raise
tierno5e91eb82016-10-04 09:39:07 +0000372 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100373 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200374 continue
375 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000376 if return_on_error:
377 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
378 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200379 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000380 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100381 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200382 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200383 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100384 #add new vim_id at datacenters_images
385 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
386 elif image_db[0]["vim_id"]!=image_vim_id:
387 #modify existing vim_id at datacenters_images
388 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
389
tiernof97fd272016-07-11 14:32:37 +0200390 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100391
tierno5e91eb82016-10-04 09:39:07 +0000392def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
tierno7edb6752016-03-21 17:37:52 +0100393 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
394 'ram':flavor_dict.get('ram'),
395 'vcpus':flavor_dict.get('vcpus'),
396 }
397 if 'extended' in flavor_dict and flavor_dict['extended']==None:
398 del flavor_dict['extended']
399 if 'extended' in flavor_dict:
400 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
401
402 #look if flavor exist
403 if only_create_at_vim:
404 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000405 if return_on_error == None:
406 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100407 else:
tiernof97fd272016-07-11 14:32:37 +0200408 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
409 if len(flavors)>=1:
410 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100411 else:
412 #create flavor
413 #create one by one the images of aditional disks
414 dev_image_list=[] #list of images
415 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
416 dev_nb=0
417 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200418 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100419 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200420 image_dict={}
421 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
422 image_dict['universal_name']=device.get('image name')
423 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
424 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100425 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200426 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100427 image_metadata_dict = device.get('image metadata', None)
428 image_metadata_str = None
429 if image_metadata_dict != None:
430 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
431 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200432 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
433 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100434 dev_image_list.append(image_id)
435 dev_nb += 1
436 temp_flavor_dict['name'] = flavor_dict['name']
437 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200438 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
439 flavor_mano_id= content
440 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100441 #create flavor at every vim
442 if 'uuid' in flavor_dict:
443 del flavor_dict['uuid']
444 flavor_vim_id=None
445 for vim_id,vim in vims.items():
446 flavor_created="false"
447 #look at database
tiernof97fd272016-07-11 14:32:37 +0200448 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100449 #look at VIM if this flavor exist SKIPPED
450 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
451 #if res_vim < 0:
452 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
453 # continue
454 #elif res_vim==0:
455
456 #Create the flavor in VIM
457 #Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000458 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100459 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
460 #make a copy of original devices
461 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000462
tierno7edb6752016-03-21 17:37:52 +0100463 for device in flavor_dict["extended"].get("devices",[]):
464 dev={}
465 dev.update(device)
466 devices_original.append(dev)
467 if 'image' in device:
468 del device['image']
469 if 'image metadata' in device:
470 del device['image metadata']
471 dev_nb=0
472 for index in range(0,len(devices_original)) :
473 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000474 if "image" not in device and "image name" not in device:
475 if 'size' in device:
476 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100477 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200478 image_dict={}
479 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
480 image_dict['universal_name']=device.get('image name')
481 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
482 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100483 #image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200484 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100485 image_metadata_dict = device.get('image metadata', None)
486 image_metadata_str = None
487 if image_metadata_dict != None:
488 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
489 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200490 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
tierno7edb6752016-03-21 17:37:52 +0100491 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200492 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
montesmoreno0c8def02016-12-22 12:16:23 +0000493
494 #save disk information (image must be based on and size
495 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
496
tierno7edb6752016-03-21 17:37:52 +0100497 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
498 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200499 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100500 #check that this vim_id exist in VIM, if not create
501 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200502 try:
503 vim.get_flavor(flavor_vim_id)
504 continue #flavor exist
505 except vimconn.vimconnException:
506 pass
tierno7edb6752016-03-21 17:37:52 +0100507 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200508 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
509 try:
tiernocf157a82017-01-30 14:07:06 +0100510 flavor_vim_id = None
511 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
512 flavor_create="false"
513 except vimconn.vimconnException as e:
514 pass
515 try:
516 if not flavor_vim_id:
517 flavor_vim_id = vim.new_flavor(flavor_dict)
518 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
519 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200520 except vimconn.vimconnException as e:
521 if return_on_error:
522 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200523 raise
tiernoae4a8d12016-07-08 12:30:39 +0200524 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000525 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200526 continue
tierno7edb6752016-03-21 17:37:52 +0100527 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200528 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100529 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000530 extended_devices_yaml = None
531 if len(disk_list) > 0:
532 extended_devices = dict()
533 extended_devices['disks'] = disk_list
534 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
535 mydb.new_row('datacenters_flavors',
536 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
537 'created':flavor_created,'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100538 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
539 #modify existing vim_id at datacenters_flavors
540 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
541
tiernof97fd272016-07-11 14:32:37 +0200542 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100543
544def new_vnf(mydb, tenant_id, vnf_descriptor):
545 global global_config
546
547 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100548 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +0100549 # Step 2. Check tenant exist
550 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200551 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100552 if "tenant_id" in vnf_descriptor["vnf"]:
553 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200554 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
555 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100556 else:
557 vnf_descriptor['vnf']['tenant_id'] = tenant_id
558 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +0200559 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100560 else:
561 vims={}
562
563 # Step 4. Review the descriptor and add missing fields
564 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +0200565 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +0100566 vnf_name = vnf_descriptor['vnf']['name']
567 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
568 if "physical" in vnf_descriptor['vnf']:
569 del vnf_descriptor['vnf']['physical']
570 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100571
tierno7edb6752016-03-21 17:37:52 +0100572 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200573 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
574 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno7edb6752016-03-21 17:37:52 +0100575
576 #For each VNFC, we add it to the VNFCDict and we create a flavor.
577 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
578 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +0100579 try:
tiernof97fd272016-07-11 14:32:37 +0200580 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100581 for vnfc in vnf_descriptor['vnf']['VNFC']:
582 VNFCitem={}
583 VNFCitem["name"] = vnfc['name']
584 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
585
tiernof97fd272016-07-11 14:32:37 +0200586 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno7edb6752016-03-21 17:37:52 +0100587
588 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200589 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
tierno7edb6752016-03-21 17:37:52 +0100590 myflavorDict["description"] = VNFCitem["description"]
591 myflavorDict["ram"] = vnfc.get("ram", 0)
592 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
593 myflavorDict["disk"] = vnfc.get("disk", 1)
594 myflavorDict["extended"] = {}
595
596 devices = vnfc.get("devices")
597 if devices != None:
598 myflavorDict["extended"]["devices"] = devices
599
600 # TODO:
601 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
602 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
603
604 # Previous code has been commented
605 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
606 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
607 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
608 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
609 #else:
610 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
611 # if result2:
612 # print "Error creating flavor: unknown processor model. Rollback successful."
613 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
614 # else:
615 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
616 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
617
618 if 'numas' in vnfc and len(vnfc['numas'])>0:
619 myflavorDict['extended']['numas'] = vnfc['numas']
620
621 #print myflavorDict
622
623 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200624 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +0100625
tiernof97fd272016-07-11 14:32:37 +0200626 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100627 VNFCitem["flavor_id"] = flavor_id
628 VNFCDict[vnfc['name']] = VNFCitem
629
tiernof97fd272016-07-11 14:32:37 +0200630 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100631 # Step 6.3 New images are created in the VIM
632 #For each VNFC, we must create the appropriate image.
633 #This "for" loop might be integrated with the previous one
634 #In case this integration is made, the VNFCDict might become a VNFClist.
635 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +0200636 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200637 image_dict={}
638 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
639 image_dict['universal_name']=vnfc.get('image name')
640 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
641 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100642 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200643 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100644 image_metadata_dict = vnfc.get('image metadata', None)
645 image_metadata_str = None
646 if image_metadata_dict is not None:
647 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
648 image_dict['metadata']=image_metadata_str
649 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +0200650 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
651 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +0100652 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200653 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100654 if vnfc.get("boot-data"):
655 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +0100656
tiernof97fd272016-07-11 14:32:37 +0200657
658 # Step 7. Storing the VNF descriptor in the repository
659 if "descriptor" not in vnf_descriptor["vnf"]:
660 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +0100661
tiernof97fd272016-07-11 14:32:37 +0200662 # Step 8. Adding the VNF to the NFVO DB
663 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
664 return vnf_id
665 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +0100666 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +0200667 if isinstance(e, db_base_Exception):
668 error_text = "Exception at database"
669 elif isinstance(e, KeyError):
670 error_text = "KeyError exception "
671 e.http_code = HTTP_Internal_Server_Error
672 else:
673 error_text = "Exception at VIM"
674 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
675 #logger.error("start_scenario %s", error_text)
676 raise NfvoException(error_text, e.http_code)
677
garciadeblas9f8456e2016-09-05 05:02:59 +0200678def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
679 global global_config
680
681 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100682 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +0200683 # Step 2. Check tenant exist
684 if tenant_id != "any":
685 check_tenant(mydb, tenant_id)
686 if "tenant_id" in vnf_descriptor["vnf"]:
687 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
688 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
689 HTTP_Unauthorized)
690 else:
691 vnf_descriptor['vnf']['tenant_id'] = tenant_id
692 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
693 vims = get_vim(mydb, tenant_id)
694 else:
695 vims={}
696
697 # Step 4. Review the descriptor and add missing fields
698 #print vnf_descriptor
699 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
700 vnf_name = vnf_descriptor['vnf']['name']
701 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
702 if "physical" in vnf_descriptor['vnf']:
703 del vnf_descriptor['vnf']['physical']
704 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100705
garciadeblas9f8456e2016-09-05 05:02:59 +0200706 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
707 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
708 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
709
710 #For each VNFC, we add it to the VNFCDict and we create a flavor.
711 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
712 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
713 try:
714 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
715 for vnfc in vnf_descriptor['vnf']['VNFC']:
716 VNFCitem={}
717 VNFCitem["name"] = vnfc['name']
718 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
719
720 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
721
722 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200723 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
garciadeblas9f8456e2016-09-05 05:02:59 +0200724 myflavorDict["description"] = VNFCitem["description"]
725 myflavorDict["ram"] = vnfc.get("ram", 0)
726 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
727 myflavorDict["disk"] = vnfc.get("disk", 1)
728 myflavorDict["extended"] = {}
729
730 devices = vnfc.get("devices")
731 if devices != None:
732 myflavorDict["extended"]["devices"] = devices
733
734 # TODO:
735 # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
736 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
737
738 # Previous code has been commented
739 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
740 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
741 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
742 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
743 #else:
744 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
745 # if result2:
746 # print "Error creating flavor: unknown processor model. Rollback successful."
747 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
748 # else:
749 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
750 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
751
752 if 'numas' in vnfc and len(vnfc['numas'])>0:
753 myflavorDict['extended']['numas'] = vnfc['numas']
754
755 #print myflavorDict
756
757 # Step 6.2 New flavors are created in the VIM
758 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
759
760 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
761 VNFCitem["flavor_id"] = flavor_id
762 VNFCDict[vnfc['name']] = VNFCitem
763
764 logger.debug("Creating new images in the VIM for each VNFC")
765 # Step 6.3 New images are created in the VIM
766 #For each VNFC, we must create the appropriate image.
767 #This "for" loop might be integrated with the previous one
768 #In case this integration is made, the VNFCDict might become a VNFClist.
769 for vnfc in vnf_descriptor['vnf']['VNFC']:
770 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200771 image_dict={}
772 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
773 image_dict['universal_name']=vnfc.get('image name')
774 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
775 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100776 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200777 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +0200778 image_metadata_dict = vnfc.get('image metadata', None)
779 image_metadata_str = None
780 if image_metadata_dict is not None:
781 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
782 image_dict['metadata']=image_metadata_str
783 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
784 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
785 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
786 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200787 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100788 if vnfc.get("boot-data"):
789 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +0200790
garciadeblas9f8456e2016-09-05 05:02:59 +0200791 # Step 7. Storing the VNF descriptor in the repository
792 if "descriptor" not in vnf_descriptor["vnf"]:
793 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
794
795 # Step 8. Adding the VNF to the NFVO DB
796 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
797 return vnf_id
798 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
799 _, message = rollback(mydb, vims, rollback_list)
800 if isinstance(e, db_base_Exception):
801 error_text = "Exception at database"
802 elif isinstance(e, KeyError):
803 error_text = "KeyError exception "
804 e.http_code = HTTP_Internal_Server_Error
805 else:
806 error_text = "Exception at VIM"
807 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
808 #logger.error("start_scenario %s", error_text)
809 raise NfvoException(error_text, e.http_code)
810
tierno7edb6752016-03-21 17:37:52 +0100811def get_vnf_id(mydb, tenant_id, vnf_id):
812 #check valid tenant_id
tiernof97fd272016-07-11 14:32:37 +0200813 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100814 #obtain data
815 where_or = {}
816 if tenant_id != "any":
817 where_or["tenant_id"] = tenant_id
818 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200819 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
tierno7edb6752016-03-21 17:37:52 +0100820
tiernof97fd272016-07-11 14:32:37 +0200821 vnf_id=vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100822 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +0200823 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +0100824 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
825 data={'vnf' : filtered_content}
826 #GET VM
tiernof97fd272016-07-11 14:32:37 +0200827 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tierno36c0b172017-01-12 18:32:28 +0100828 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +0100829 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200830 if len(content)==0:
831 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +0100832 # change boot_data into boot-data
833 for vm in content:
834 if vm.get("boot_data"):
835 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
836 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +0100837
838 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200839 #TODO: GET all the information from a VNFC and include it in the output.
840
tierno7edb6752016-03-21 17:37:52 +0100841 #GET NET
tiernof97fd272016-07-11 14:32:37 +0200842 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +0100843 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
844 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200845 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200846
847 #GET ip-profile for each net
848 for net in data['vnf']['nets']:
849 ipprofiles = mydb.get_rows(FROM='ip_profiles',
850 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
851 WHERE={'net_id': net["uuid"]} )
852 if len(ipprofiles)==1:
853 net["ip_profile"] = ipprofiles[0]
854 elif len(ipprofiles)>1:
855 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
856
857
858 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
859
860 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +0200861 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
tierno7edb6752016-03-21 17:37:52 +0100862 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
863 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
864 WHERE={'vnfs.uuid': vnf_id},
865 WHERE_NOT={'interfaces.external_name': None} )
866 #print content
tiernof97fd272016-07-11 14:32:37 +0200867 data['vnf']['external-connections'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200868
tiernof97fd272016-07-11 14:32:37 +0200869 return data
tierno7edb6752016-03-21 17:37:52 +0100870
871
872def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
873 # Check tenant exist
874 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200875 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100876 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +0200877 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100878 else:
879 vims={}
880
881 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
882 where_or = {}
883 if tenant_id != "any":
884 where_or["tenant_id"] = tenant_id
885 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200886 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
887 vnf_id = vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100888
889 # "Getting the list of flavors and tenants of the VNF"
tiernof97fd272016-07-11 14:32:37 +0200890 flavorList = get_flavorlist(mydb, vnf_id)
891 if len(flavorList)==0:
892 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100893
tiernof97fd272016-07-11 14:32:37 +0200894 imageList = get_imagelist(mydb, vnf_id)
895 if len(imageList)==0:
896 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100897
tiernof97fd272016-07-11 14:32:37 +0200898 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
899 if deleted == 0:
900 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100901
902 undeletedItems = []
903 for flavor in flavorList:
904 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +0200905 try:
906 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
907 if len(c) > 0:
908 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
909 continue
910 #flavor not used, must be deleted
911 #delelte at VIM
912 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +0100913 for flavor_vim in c:
914 if flavor_vim["datacenter_id"] not in vims:
915 continue
916 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
917 continue
918 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200919 try:
920 myvim.delete_flavor(flavor_vim["vim_id"])
921 except vimconn.vimconnNotFoundException as e:
922 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
923 except vimconn.vimconnException as e:
924 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
925 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
926 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200927 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
928 mydb.delete_row_by_id('flavors', flavor)
929 except db_base_Exception as e:
930 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +0100931 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +0200932
tierno7edb6752016-03-21 17:37:52 +0100933
934 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +0200935 try:
936 #check if image is used by other vnf
937 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
938 if len(c) > 0:
939 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
940 continue
941 #image not used, must be deleted
942 #delelte at VIM
943 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +0100944 for image_vim in c:
945 if image_vim["datacenter_id"] not in vims:
946 continue
947 if image_vim['created']=='false': #skip this image because not created by openmano
948 continue
949 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200950 try:
951 myvim.delete_image(image_vim["vim_id"])
952 except vimconn.vimconnNotFoundException as e:
953 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
954 except vimconn.vimconnException as e:
955 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
956 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
957 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200958 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
959 mydb.delete_row_by_id('images', image)
960 except db_base_Exception as e:
961 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +0100962 undeletedItems.append("image %s" % image)
963
tiernof97fd272016-07-11 14:32:37 +0200964 return vnf_id + " " + vnf["name"]
965 #if undeletedItems:
966 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +0100967
968def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
969 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
970 if result < 0:
971 return result, vims
972 elif result == 0:
973 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
974 myvim = vims.values()[0]
975 result,servers = myvim.get_hosts_info()
976 if result < 0:
977 return result, servers
978 topology = {'name':myvim['name'] , 'servers': servers}
979 return result, topology
980
981def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +0200982 vims = get_vim(mydb, nfvo_tenant_id)
983 if len(vims) == 0:
984 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
985 elif len(vims)>1:
986 #print "nfvo.datacenter_action() error. Several datacenters found"
987 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +0100988 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +0200989 try:
990 hosts = myvim.get_hosts()
991 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +0100992
tiernof97fd272016-07-11 14:32:37 +0200993 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
994 for host in hosts:
995 server={'name':host['name'], 'vms':[]}
996 for vm in host['instances']:
997 #get internal name and model
998 try:
999 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1000 WHERE={'vim_vm_id':vm['id']} )
1001 if len(c) == 0:
1002 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1003 continue
1004 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1005
1006 except db_base_Exception as e:
1007 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1008 datacenter['Datacenters'][0]['servers'].append(server)
1009 #return -400, "en construccion"
tierno7edb6752016-03-21 17:37:52 +01001010
tiernof97fd272016-07-11 14:32:37 +02001011 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1012 return datacenter
1013 except vimconn.vimconnException as e:
1014 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001015
1016def new_scenario(mydb, tenant_id, topo):
1017
1018# result, vims = get_vim(mydb, tenant_id)
1019# if result < 0:
1020# return result, vims
1021#1: parse input
1022 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001023 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001024 if "tenant_id" in topo:
1025 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001026 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1027 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001028 else:
1029 tenant_id=None
1030
1031#1.1: get VNFs and external_networks (other_nets).
1032 vnfs={}
1033 other_nets={} #external_networks, bridge_networks and data_networkds
1034 nodes = topo['topology']['nodes']
1035 for k in nodes.keys():
1036 if nodes[k]['type'] == 'VNF':
1037 vnfs[k] = nodes[k]
1038 vnfs[k]['ifaces'] = {}
1039 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1040 other_nets[k] = nodes[k]
1041 other_nets[k]['external']=True
1042 elif nodes[k]['type'] == 'network':
1043 other_nets[k] = nodes[k]
1044 other_nets[k]['external']=False
1045
1046
1047#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1048 for name,vnf in vnfs.items():
tiernocea279c2016-07-18 12:36:49 +02001049 where={}
1050 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001051 error_text = ""
1052 error_pos = "'topology':'nodes':'" + name + "'"
1053 if 'vnf_id' in vnf:
1054 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001055 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001056 if 'VNF model' in vnf:
1057 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001058 where['name'] = vnf['VNF model']
1059 if len(where) == 0:
tiernof97fd272016-07-11 14:32:37 +02001060 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
1061
tiernocea279c2016-07-18 12:36:49 +02001062 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1063 FROM='vnfs',
1064 WHERE=where,
1065 WHERE_OR=where_or,
1066 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001067 if len(vnf_db)==0:
1068 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1069 elif len(vnf_db)>1:
1070 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001071 vnf['uuid']=vnf_db[0]['uuid']
1072 vnf['description']=vnf_db[0]['description']
1073 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001074 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
tierno7edb6752016-03-21 17:37:52 +01001075 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1076 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001077 for ext_iface in ext_ifaces:
1078 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1079
1080#1.4 get list of connections
1081 conections = topo['topology']['connections']
1082 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001083 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001084 for k in conections.keys():
1085 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1086 ifaces_list = conections[k]['nodes'].items()
1087 elif type(conections[k]['nodes'])==list: #list with dictionary
1088 ifaces_list=[]
1089 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1090 for k2 in conection_pair_list:
1091 ifaces_list += k2
1092
1093 con_type = conections[k].get("type", "link")
1094 if con_type != "link":
1095 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001096 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001097 other_nets[k] = {'external': False}
1098 if conections[k].get("graph"):
1099 other_nets[k]["graph"] = conections[k]["graph"]
1100 ifaces_list.append( (k, None) )
1101
1102
1103 if con_type == "external_network":
1104 other_nets[k]['external'] = True
1105 if conections[k].get("model"):
1106 other_nets[k]["model"] = conections[k]["model"]
1107 else:
1108 other_nets[k]["model"] = k
1109 if con_type == "dataplane_net" or con_type == "bridge_net":
1110 other_nets[k]["model"] = con_type
1111
tiernoefd80c92016-09-16 14:17:46 +02001112 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001113 conections_list.append(set(ifaces_list)) #from list to set to operate as a set (this conversion removes elements that are repeated in a list)
1114 #print set(ifaces_list)
1115 #check valid VNF and iface names
1116 for iface in ifaces_list:
1117 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001118 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1119 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001120 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001121 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1122 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001123
1124#1.5 unify connections from the pair list to a consolidated list
1125 index=0
1126 while index < len(conections_list):
1127 index2 = index+1
1128 while index2 < len(conections_list):
1129 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1130 conections_list[index] |= conections_list[index2]
1131 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001132 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001133 else:
1134 index2 += 1
1135 conections_list[index] = list(conections_list[index]) # from set to list again
1136 index += 1
1137 #for k in conections_list:
1138 # print k
1139
1140
1141
1142#1.6 Delete non external nets
1143# for k in other_nets.keys():
1144# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1145# for con in conections_list:
1146# delete_indexes=[]
1147# for index in range(0,len(con)):
1148# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1149# for index in delete_indexes:
1150# del con[index]
1151# del other_nets[k]
1152#1.7: Check external_ports are present at database table datacenter_nets
1153 for k,net in other_nets.items():
1154 error_pos = "'topology':'nodes':'" + k + "'"
1155 if net['external']==False:
1156 if 'name' not in net:
1157 net['name']=k
1158 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001159 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001160 if net['model']=='bridge_net':
1161 net['type']='bridge';
1162 elif net['model']=='dataplane_net':
1163 net['type']='data';
1164 else:
tiernof97fd272016-07-11 14:32:37 +02001165 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001166 else: #external
1167#IF we do not want to check that external network exist at datacenter
1168 pass
1169#ELSE
1170# error_text = ""
1171# WHERE_={}
1172# if 'net_id' in net:
1173# error_text += " 'net_id' " + net['net_id']
1174# WHERE_['uuid'] = net['net_id']
1175# if 'model' in net:
1176# error_text += " 'model' " + net['model']
1177# WHERE_['name'] = net['model']
1178# if len(WHERE_) == 0:
1179# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1180# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1181# FROM='datacenter_nets', WHERE=WHERE_ )
1182# if r<0:
1183# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1184# elif r==0:
1185# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1186# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1187# elif r>1:
1188# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1189# return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1190# other_nets[k].update(net_db[0])
1191#ENDIF
1192 net_list={}
1193 net_nb=0 #Number of nets
1194 for con in conections_list:
1195 #check if this is connected to a external net
1196 other_net_index=-1
1197 #print
1198 #print "con", con
1199 for index in range(0,len(con)):
1200 #check if this is connected to a external net
1201 for net_key in other_nets.keys():
1202 if con[index][0]==net_key:
1203 if other_net_index>=0:
1204 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
tiernof97fd272016-07-11 14:32:37 +02001205 #print "nfvo.new_scenario " + error_text
1206 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001207 else:
1208 other_net_index = index
1209 net_target = net_key
1210 break
1211 #print "other_net_index", other_net_index
1212 try:
1213 if other_net_index>=0:
1214 del con[other_net_index]
1215#IF we do not want to check that external network exist at datacenter
1216 if other_nets[net_target]['external'] :
1217 if "name" not in other_nets[net_target]:
1218 other_nets[net_target]['name'] = other_nets[net_target]['model']
1219 if other_nets[net_target]["type"] == "external_network":
1220 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1221 other_nets[net_target]["type"] = "data"
1222 else:
1223 other_nets[net_target]["type"] = "bridge"
1224#ELSE
1225# if other_nets[net_target]['external'] :
1226# type_='data' if len(con)>1 else 'ptp' #an external net is connected to a external port, so it is ptp if only one connection is done to this net
1227# if type_=='data' and other_nets[net_target]['type']=="ptp":
1228# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1229# print "nfvo.new_scenario " + error_text
1230# return -HTTP_Bad_Request, error_text
1231#ENDIF
1232 for iface in con:
1233 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1234 else:
1235 #create a net
1236 net_type_bridge=False
1237 net_type_data=False
1238 net_target = "__-__net"+str(net_nb)
tiernoefd80c92016-09-16 14:17:46 +02001239 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
1240 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno7edb6752016-03-21 17:37:52 +01001241 'external':False}
1242 for iface in con:
1243 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1244 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1245 if iface_type=='mgmt' or iface_type=='bridge':
1246 net_type_bridge = True
1247 else:
1248 net_type_data = True
1249 if net_type_bridge and net_type_data:
1250 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001251 #print "nfvo.new_scenario " + error_text
1252 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001253 elif net_type_bridge:
1254 type_='bridge'
1255 else:
1256 type_='data' if len(con)>2 else 'ptp'
1257 net_list[net_target]['type'] = type_
1258 net_nb+=1
1259 except Exception:
1260 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001261 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001262 #raise e
tiernof97fd272016-07-11 14:32:37 +02001263 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001264
1265#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1266 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001267 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001268 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
1269 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001270 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001271 add_mgmt_net = False
1272 for vnf in vnfs.values():
1273 for iface in vnf['ifaces'].values():
1274 if iface['type']=='mgmt' and 'net_key' not in iface:
1275 #iface not connected
1276 iface['net_key'] = 'mgmt'
1277 add_mgmt_net = True
1278 if add_mgmt_net and 'mgmt' not in net_list:
1279 net_list['mgmt']=mgmt_net[0]
1280 net_list['mgmt']['external']=True
1281 net_list['mgmt']['graph']={'visible':False}
1282
1283 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001284 #print
1285 #print 'net_list', net_list
1286 #print
1287 #print 'vnfs', vnfs
1288 #print
tierno7edb6752016-03-21 17:37:52 +01001289
1290#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001291 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001292 'tenant_id':tenant_id, 'name':topo['name'],
1293 'description':topo.get('description',topo['name']),
1294 'public': topo.get('public', False)
1295 })
tierno7edb6752016-03-21 17:37:52 +01001296
tiernof97fd272016-07-11 14:32:37 +02001297 return c
tierno7edb6752016-03-21 17:37:52 +01001298
tierno392f2852016-05-13 12:28:55 +02001299def new_scenario_v02(mydb, tenant_id, scenario_dict):
1300 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001301 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001302 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001303 if "tenant_id" in scenario:
1304 if scenario["tenant_id"] != tenant_id:
1305 print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001306 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1307 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001308 else:
1309 tenant_id=None
1310
1311#1: Check that VNF are present at database table vnfs and update content into scenario dict
1312 for name,vnf in scenario["vnfs"].iteritems():
tiernocea279c2016-07-18 12:36:49 +02001313 where={}
1314 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001315 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001316 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001317 if 'vnf_id' in vnf:
1318 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001319 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001320 if 'vnf_name' in vnf:
1321 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001322 where['name'] = vnf['vnf_name']
1323 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001324 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tiernocea279c2016-07-18 12:36:49 +02001325 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1326 FROM='vnfs',
1327 WHERE=where,
1328 WHERE_OR=where_or,
1329 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001330 if len(vnf_db)==0:
1331 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1332 elif len(vnf_db)>1:
1333 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001334 vnf['uuid']=vnf_db[0]['uuid']
1335 vnf['description']=vnf_db[0]['description']
1336 vnf['ifaces'] = {}
1337 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001338 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
tierno7edb6752016-03-21 17:37:52 +01001339 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1340 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001341 for ext_iface in ext_ifaces:
1342 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1343
1344#2: Insert net_key at every vnf interface
1345 for net_name,net in scenario["networks"].iteritems():
1346 net_type_bridge=False
1347 net_type_data=False
1348 for iface_dict in net["interfaces"]:
1349 for vnf,iface in iface_dict.iteritems():
1350 if vnf not in scenario["vnfs"]:
1351 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
tiernof97fd272016-07-11 14:32:37 +02001352 #print "nfvo.new_scenario_v02 " + error_text
1353 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001354 if iface not in scenario["vnfs"][vnf]['ifaces']:
1355 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
tiernof97fd272016-07-11 14:32:37 +02001356 #print "nfvo.new_scenario_v02 " + error_text
1357 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001358 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1359 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1360 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
tiernof97fd272016-07-11 14:32:37 +02001361 #print "nfvo.new_scenario_v02 " + error_text
1362 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001363 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1364 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1365 if iface_type=='mgmt' or iface_type=='bridge':
1366 net_type_bridge = True
1367 else:
1368 net_type_data = True
1369 if net_type_bridge and net_type_data:
1370 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
tiernof97fd272016-07-11 14:32:37 +02001371 #print "nfvo.new_scenario " + error_text
1372 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001373 elif net_type_bridge:
1374 type_='bridge'
1375 else:
1376 type_='data' if len(net["interfaces"])>2 else 'ptp'
1377 net['type'] = type_
1378 net['name'] = net_name
1379 net['external'] = net.get('external', False)
1380
1381#3: insert at database
1382 scenario["nets"] = scenario["networks"]
1383 scenario['tenant_id'] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001384 scenario_id = mydb.new_scenario( scenario)
1385 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01001386
1387def edit_scenario(mydb, tenant_id, scenario_id, data):
1388 data["uuid"] = scenario_id
1389 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001390 c = mydb.edit_scenario( data )
1391 return c
tierno7edb6752016-03-21 17:37:52 +01001392
1393def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
tiernoae4a8d12016-07-08 12:30:39 +02001394 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00001395 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1396 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02001397 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01001398 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00001399
tierno7edb6752016-03-21 17:37:52 +01001400 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001401 try:
1402 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tiernof97fd272016-07-11 14:32:37 +02001403 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00001404 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02001405 scenarioDict['datacenter_id'] = datacenter_id
1406 #print '================scenarioDict======================='
1407 #print json.dumps(scenarioDict, indent=4)
1408 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno7edb6752016-03-21 17:37:52 +01001409
tiernoae4a8d12016-07-08 12:30:39 +02001410 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1411 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01001412
tiernoae4a8d12016-07-08 12:30:39 +02001413 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1414 auxNetDict['scenario'] = {}
1415
1416 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1417 for sce_net in scenarioDict['nets']:
1418 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno7edb6752016-03-21 17:37:52 +01001419
tiernoae4a8d12016-07-08 12:30:39 +02001420 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01001421 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02001422 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01001423 myNetDict = {}
1424 myNetDict["name"] = myNetName
1425 myNetDict["type"] = myNetType
1426 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001427 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01001428 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02001429 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02001430 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02001431 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02001432 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001433 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1434 sce_net['vim_id'] = network_id
1435 auxNetDict['scenario'][sce_net['uuid']] = network_id
1436 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001437 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001438 else:
1439 if sce_net['vim_id'] == None:
1440 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1441 _, message = rollback(mydb, vims, rollbackList)
1442 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02001443 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02001444 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1445 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno7edb6752016-03-21 17:37:52 +01001446
tiernoae4a8d12016-07-08 12:30:39 +02001447 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1448 #For each vnf net, we create it and we add it to instanceNetlist.
1449 for sce_vnf in scenarioDict['vnfs']:
1450 for net in sce_vnf['nets']:
1451 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
1452
1453 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1454 myNetName = myNetName[0:255] #limit length
1455 myNetType = net['type']
1456 myNetDict = {}
1457 myNetDict["name"] = myNetName
1458 myNetDict["type"] = myNetType
1459 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001460 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02001461 #print myNetDict
1462 #TODO:
1463 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02001464 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001465 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1466 net['vim_id'] = network_id
1467 if sce_vnf['uuid'] not in auxNetDict:
1468 auxNetDict[sce_vnf['uuid']] = {}
1469 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1470 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001471 net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001472
1473 #print "auxNetDict:"
1474 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
1475
1476 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1477 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1478 i = 0
1479 for sce_vnf in scenarioDict['vnfs']:
1480 for vm in sce_vnf['vms']:
1481 i += 1
1482 myVMDict = {}
1483 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01001484 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02001485 #myVMDict['description'] = vm['description']
1486 myVMDict['description'] = myVMDict['name'][0:99]
1487 if not startvms:
1488 myVMDict['start'] = "no"
1489 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1490 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
1491
1492 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001493 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
1494 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001495 vm['vim_image_id'] = image_id
1496
1497 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001498 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02001499 if flavor_dict['extended']!=None:
1500 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tiernof97fd272016-07-11 14:32:37 +02001501 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001502 vm['vim_flavor_id'] = flavor_id
1503
1504
1505 myVMDict['imageRef'] = vm['vim_image_id']
1506 myVMDict['flavorRef'] = vm['vim_flavor_id']
1507 myVMDict['networks'] = []
1508 for iface in vm['interfaces']:
1509 netDict = {}
1510 if iface['type']=="data":
1511 netDict['type'] = iface['model']
1512 elif "model" in iface and iface["model"]!=None:
1513 netDict['model']=iface['model']
1514 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1515 #discover type of interface looking at flavor
1516 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1517 for flavor_iface in numa.get('interfaces',[]):
1518 if flavor_iface.get('name') == iface['internal_name']:
1519 if flavor_iface['dedicated'] == 'yes':
1520 netDict['type']="PF" #passthrough
1521 elif flavor_iface['dedicated'] == 'no':
1522 netDict['type']="VF" #siov
1523 elif flavor_iface['dedicated'] == 'yes:sriov':
1524 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1525 netDict["mac_address"] = flavor_iface.get("mac_address")
1526 break;
1527 netDict["use"]=iface['type']
1528 if netDict["use"]=="data" and not netDict.get("type"):
1529 #print "netDict", netDict
1530 #print "iface", iface
1531 e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
1532 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02001533 raise NfvoException(e_text + "After database migration some information is not available. \
1534 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02001535 else:
tiernof97fd272016-07-11 14:32:37 +02001536 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02001537 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1538 netDict["type"]="virtual"
1539 if "vpci" in iface and iface["vpci"] is not None:
1540 netDict['vpci'] = iface['vpci']
1541 if "mac" in iface and iface["mac"] is not None:
1542 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001543 if "port-security" in iface and iface["port-security"] is not None:
1544 netDict['port_security'] = iface['port-security']
1545 if "floating-ip" in iface and iface["floating-ip"] is not None:
1546 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02001547 netDict['name'] = iface['internal_name']
1548 if iface['net_id'] is None:
1549 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02001550 #print iface
1551 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02001552 if vnf_iface['interface_id']==iface['uuid']:
1553 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1554 break
1555 else:
1556 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1557 #skip bridge ifaces not connected to any net
1558 #if 'net_id' not in netDict or netDict['net_id']==None:
1559 # continue
1560 myVMDict['networks'].append(netDict)
1561 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1562 #print myVMDict['name']
1563 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1564 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1565 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1566 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1567 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1568 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1569 vm['vim_id'] = vm_id
1570 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1571 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1572 for net in myVMDict['networks']:
1573 if "vim_id" in net:
1574 for iface in vm['interfaces']:
1575 if net["name"]==iface["internal_name"]:
1576 iface["vim_id"]=net["vim_id"]
1577 break
1578
1579 logger.debug("start scenario Deployment done")
1580 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1581 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02001582 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1583 return mydb.get_instance_scenario(instance_id)
1584
1585 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001586 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02001587 if isinstance(e, db_base_Exception):
1588 error_text = "Exception at database"
1589 else:
1590 error_text = "Exception at VIM"
1591 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1592 #logger.error("start_scenario %s", error_text)
1593 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001594
tierno36c0b172017-01-12 18:32:28 +01001595def unify_cloud_config(cloud_config_preserve, cloud_config):
1596 ''' join the cloud config information into cloud_config_preserve.
1597 In case of conflict cloud_config_preserve preserves
1598 None is admited
1599 '''
1600 if not cloud_config_preserve and not cloud_config:
1601 return None
1602
1603 new_cloud_config = {"key-pairs":[], "users":[]}
1604 # key-pairs
1605 if cloud_config_preserve:
1606 for key in cloud_config_preserve.get("key-pairs", () ):
1607 if key not in new_cloud_config["key-pairs"]:
1608 new_cloud_config["key-pairs"].append(key)
1609 if cloud_config:
1610 for key in cloud_config.get("key-pairs", () ):
1611 if key not in new_cloud_config["key-pairs"]:
1612 new_cloud_config["key-pairs"].append(key)
1613 if not new_cloud_config["key-pairs"]:
1614 del new_cloud_config["key-pairs"]
1615
1616 # users
1617 if cloud_config:
1618 new_cloud_config["users"] += cloud_config.get("users", () )
1619 if cloud_config_preserve:
1620 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02001621 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01001622 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02001623 for index0 in range(0,len(users)):
1624 if index0 in index_to_delete:
1625 continue
1626 for index1 in range(index0+1,len(users)):
1627 if index1 in index_to_delete:
1628 continue
1629 if users[index0]["name"] == users[index1]["name"]:
1630 index_to_delete.append(index1)
1631 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01001632 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02001633 users[index0]["key-pairs"] = [key]
1634 elif key not in users[index0]["key-pairs"]:
1635 users[index0]["key-pairs"].append(key)
1636 index_to_delete.sort(reverse=True)
1637 for index in index_to_delete:
1638 del users[index]
tierno36c0b172017-01-12 18:32:28 +01001639 if not new_cloud_config["users"]:
1640 del new_cloud_config["users"]
1641
1642 #boot-data-drive
1643 if cloud_config and cloud_config.get("boot-data-drive") != None:
1644 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
1645 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
1646 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
1647
1648 # user-data
1649 if cloud_config and cloud_config.get("user-data") != None:
1650 new_cloud_config["user-data"] = cloud_config["user-data"]
1651 if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
1652 new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
1653
1654 # config files
1655 new_cloud_config["config-files"] = []
1656 if cloud_config and cloud_config.get("config-files") != None:
1657 new_cloud_config["config-files"] += cloud_config["config-files"]
1658 if cloud_config_preserve:
1659 for file in cloud_config_preserve.get("config-files", ()):
1660 for index in range(0, len(new_cloud_config["config-files"])):
1661 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
1662 new_cloud_config["config-files"][index] = file
1663 break
1664 else:
1665 new_cloud_config["config-files"].append(file)
1666 if not new_cloud_config["config-files"]:
1667 del new_cloud_config["config-files"]
1668 return new_cloud_config
1669
1670
tiernoa4e1a6e2016-08-31 14:19:40 +02001671
tiernoa2793912016-10-04 08:15:08 +00001672def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02001673 datacenter_id = None
1674 datacenter_name = None
1675 if datacenter_id_name:
1676 if utils.check_valid_uuid(datacenter_id_name):
1677 datacenter_id = datacenter_id_name
1678 else:
1679 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00001680 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02001681 if len(vims) == 0:
1682 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1683 elif len(vims)>1:
1684 #print "nfvo.datacenter_action() error. Several datacenters found"
1685 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1686 return vims.keys()[0], vims.values()[0]
1687
garciadeblas9f8456e2016-09-05 05:02:59 +02001688def new_scenario_v03(mydb, tenant_id, scenario_dict):
1689 scenario = scenario_dict["scenario"]
1690 if tenant_id != "any":
1691 check_tenant(mydb, tenant_id)
1692 if "tenant_id" in scenario:
1693 if scenario["tenant_id"] != tenant_id:
1694 logger("Tenant '%s' not found", tenant_id)
1695 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1696 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
1697 else:
1698 tenant_id=None
1699
1700#1: Check that VNF are present at database table vnfs and update content into scenario dict
1701 for name,vnf in scenario["vnfs"].iteritems():
1702 where={}
1703 where_or={"tenant_id": tenant_id, 'public': "true"}
1704 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001705 error_pos = "'scenario':'vnfs':'" + name + "'"
garciadeblas9f8456e2016-09-05 05:02:59 +02001706 if 'vnf_id' in vnf:
1707 error_text += " 'vnf_id' " + vnf['vnf_id']
1708 where['uuid'] = vnf['vnf_id']
1709 if 'vnf_name' in vnf:
1710 error_text += " 'vnf_name' " + vnf['vnf_name']
1711 where['name'] = vnf['vnf_name']
1712 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001713 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
garciadeblas9f8456e2016-09-05 05:02:59 +02001714 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1715 FROM='vnfs',
1716 WHERE=where,
1717 WHERE_OR=where_or,
1718 WHERE_AND_OR="AND")
1719 if len(vnf_db)==0:
1720 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1721 elif len(vnf_db)>1:
1722 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1723 vnf['uuid']=vnf_db[0]['uuid']
1724 vnf['description']=vnf_db[0]['description']
1725 vnf['ifaces'] = {}
1726 # get external interfaces
1727 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1728 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1729 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
1730 for ext_iface in ext_ifaces:
1731 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1732
1733 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1734
1735#2: Insert net_key and ip_address at every vnf interface
1736 for net_name,net in scenario["networks"].iteritems():
1737 net_type_bridge=False
1738 net_type_data=False
1739 for iface_dict in net["interfaces"]:
1740 logger.debug("Iface_dict %s", iface_dict)
1741 vnf = iface_dict["vnf"]
1742 iface = iface_dict["vnf_interface"]
1743 if vnf not in scenario["vnfs"]:
1744 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
1745 #logger.debug(error_text)
1746 raise NfvoException(error_text, HTTP_Not_Found)
1747 if iface not in scenario["vnfs"][vnf]['ifaces']:
1748 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
1749 #logger.debug(error_text)
1750 raise NfvoException(error_text, HTTP_Bad_Request)
1751 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1752 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1753 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1754 #logger.debug(error_text)
1755 raise NfvoException(error_text, HTTP_Bad_Request)
1756 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1757 scenario["vnfs"][vnf]['ifaces'][ iface ]['ip_address'] = iface_dict.get('ip_address',None)
1758 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1759 if iface_type=='mgmt' or iface_type=='bridge':
1760 net_type_bridge = True
1761 else:
1762 net_type_data = True
1763 if net_type_bridge and net_type_data:
1764 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
1765 #logger.debug(error_text)
1766 raise NfvoException(error_text, HTTP_Bad_Request)
1767 elif net_type_bridge:
1768 type_='bridge'
1769 else:
1770 type_='data' if len(net["interfaces"])>2 else 'ptp'
1771
1772 if ("implementation" in net):
1773 if (type_ == "bridge" and net["implementation"] == "underlay"):
1774 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at 'network':'%s'" % (net_name)
1775 #logger.debug(error_text)
1776 raise NfvoException(error_text, HTTP_Bad_Request)
1777 elif (type_ <> "bridge" and net["implementation"] == "overlay"):
1778 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at 'network':'%s'" % (net_name)
1779 #logger.debug(error_text)
1780 raise NfvoException(error_text, HTTP_Bad_Request)
1781 net.pop("implementation")
1782 if ("type" in net):
1783 if (type_ == "data" and net["type"] == "e-line"):
1784 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type 'e-line' at 'network':'%s'" % (net_name)
1785 #logger.debug(error_text)
1786 raise NfvoException(error_text, HTTP_Bad_Request)
1787 elif (type_ == "ptp" and net["type"] == "e-lan"):
1788 type_ = "data"
1789
1790 net['type'] = type_
1791 net['name'] = net_name
1792 net['external'] = net.get('external', False)
1793
1794#3: insert at database
1795 scenario["nets"] = scenario["networks"]
1796 scenario['tenant_id'] = tenant_id
1797 scenario_id = mydb.new_scenario2(scenario)
1798 return scenario_id
1799
1800def update(d, u):
1801 '''Takes dict d and updates it with the values in dict u.'''
1802 '''It merges all depth levels'''
1803 for k, v in u.iteritems():
1804 if isinstance(v, collections.Mapping):
1805 r = update(d.get(k, {}), v)
1806 d[k] = r
1807 else:
1808 d[k] = u[k]
1809 return d
1810
tierno7edb6752016-03-21 17:37:52 +01001811def create_instance(mydb, tenant_id, instance_dict):
tiernoae4a8d12016-07-08 12:30:39 +02001812 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno4319dad2016-09-05 12:11:11 +02001813 #logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01001814 scenario = instance_dict["scenario"]
tiernobe41e222016-09-02 15:16:13 +02001815
1816 #find main datacenter
1817 myvims = {}
tiernoa2793912016-10-04 08:15:08 +00001818 datacenter2tenant = {}
tierno7edb6752016-03-21 17:37:52 +01001819 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02001820 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1821 myvims[default_datacenter_id] = vim
tiernoa2793912016-10-04 08:15:08 +00001822 datacenter2tenant[default_datacenter_id] = vim['config']['datacenter_tenant_id']
tierno392f2852016-05-13 12:28:55 +02001823 #myvim_tenant = myvim['tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001824# default_datacenter_name = vim['name']
tierno7edb6752016-03-21 17:37:52 +01001825 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001826
1827 #print "Checking that the scenario exists and getting the scenario dictionary"
tiernobe41e222016-09-02 15:16:13 +02001828 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001829
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001830 #logger.debug(">>>>>>> Dictionaries before merging")
1831 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1832 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
garciadeblas9f8456e2016-09-05 05:02:59 +02001833
tiernobe41e222016-09-02 15:16:13 +02001834 scenarioDict['datacenter_id'] = default_datacenter_id
garciadeblas9f8456e2016-09-05 05:02:59 +02001835
tierno7edb6752016-03-21 17:37:52 +01001836 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1837 auxNetDict['scenario'] = {}
1838
tierno4319dad2016-09-05 12:11:11 +02001839 logger.debug("Creating instance from scenario-dict:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001840 instance_name = instance_dict["name"]
1841 instance_description = instance_dict.get("description")
1842 try:
1843 #0 check correct parameters
tiernobe41e222016-09-02 15:16:13 +02001844 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001845 found=False
1846 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001847 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01001848 found = True
1849 break
1850 if not found:
tiernobe41e222016-09-02 15:16:13 +02001851 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
1852 if "sites" not in net_instance_desc:
1853 net_instance_desc["sites"] = [ {} ]
1854 site_without_datacenter_field = False
1855 for site in net_instance_desc["sites"]:
1856 if site.get("datacenter"):
1857 if site["datacenter"] not in myvims:
1858 #Add this datacenter to myvims
1859 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
1860 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001861 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001862 site["datacenter"] = d #change name to id
1863 else:
1864 if site_without_datacenter_field:
1865 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
1866 site_without_datacenter_field = True
1867 site["datacenter"] = default_datacenter_id #change name to id
1868
1869 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001870 found=False
1871 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02001872 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01001873 found = True
1874 break
1875 if not found:
tiernobe41e222016-09-02 15:16:13 +02001876 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
1877 if "datacenter" in vnf_instance_desc:
1878 #Add this datacenter to myvims
1879 if vnf_instance_desc["datacenter"] not in myvims:
1880 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
1881 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001882 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
1883 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01001884
tiernoa4e1a6e2016-08-31 14:19:40 +02001885 #0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01001886 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02001887
1888 #0.2 merge instance information into scenario
1889 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
1890 #However, this is not possible yet.
1891 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
1892 for scenario_net in scenarioDict['nets']:
1893 if net_name == scenario_net["name"]:
1894 if 'ip-profile' in net_instance_desc:
1895 ipprofile = net_instance_desc['ip-profile']
1896 ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None)
1897 ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4')
1898 ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None)
1899 ipprofile['dns_address'] = ipprofile.pop('dns-address',None)
1900 if 'dhcp' in ipprofile:
1901 ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None)
1902 ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True)
1903 ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None)
1904 del ipprofile['dhcp']
garciadeblasedca7b32016-09-29 14:01:52 +00001905 if 'ip_profile' not in scenario_net:
1906 scenario_net['ip_profile'] = ipprofile
1907 else:
1908 update(scenario_net['ip_profile'],ipprofile)
tiernoe6c58ce2016-09-14 16:02:49 +02001909 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02001910 if 'ip_address' in interface:
1911 for vnf in scenarioDict['vnfs']:
1912 if interface['vnf'] == vnf['name']:
1913 for vnf_interface in vnf['interfaces']:
1914 if interface['vnf_interface'] == vnf_interface['external_name']:
1915 vnf_interface['ip_address']=interface['ip_address']
1916
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001917 #logger.debug(">>>>>>>> Merged dictionary")
tierno4319dad2016-09-05 12:11:11 +02001918 logger.debug("Creating instance scenario-dict MERGED:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
garciadeblas9f8456e2016-09-05 05:02:59 +02001919
tierno7edb6752016-03-21 17:37:52 +01001920
1921 #1. Creating new nets (sce_nets) in the VIM"
1922 for sce_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001923 sce_net["vim_id_sites"]={}
tierno7edb6752016-03-21 17:37:52 +01001924 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
tiernobe41e222016-09-02 15:16:13 +02001925 net_name = descriptor_net.get("vim-network-name")
1926 auxNetDict['scenario'][sce_net['uuid']] = {}
1927
1928 sites = descriptor_net.get("sites", [ {} ])
1929 for site in sites:
1930 if site.get("datacenter"):
1931 vim = myvims[ site["datacenter"] ]
1932 datacenter_id = site["datacenter"]
tierno7edb6752016-03-21 17:37:52 +01001933 else:
tiernobe41e222016-09-02 15:16:13 +02001934 vim = myvims[ default_datacenter_id ]
1935 datacenter_id = default_datacenter_id
tiernobe41e222016-09-02 15:16:13 +02001936 net_type = sce_net['type']
1937 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
1938 if sce_net["external"]:
1939 if not net_name:
1940 net_name = sce_net["name"]
1941 if "netmap-use" in site or "netmap-create" in site:
1942 create_network = False
1943 lookfor_network = False
1944 if "netmap-use" in site:
1945 lookfor_network = True
1946 if utils.check_valid_uuid(site["netmap-use"]):
1947 filter_text = "scenario id '%s'" % site["netmap-use"]
1948 lookfor_filter["id"] = site["netmap-use"]
1949 else:
1950 filter_text = "scenario name '%s'" % site["netmap-use"]
1951 lookfor_filter["name"] = site["netmap-use"]
1952 if "netmap-create" in site:
1953 create_network = True
1954 net_vim_name = net_name
1955 if site["netmap-create"]:
1956 net_vim_name = site["netmap-create"]
1957
1958 elif sce_net['vim_id'] != None:
1959 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
1960 create_network = False
1961 lookfor_network = True
1962 lookfor_filter["id"] = sce_net['vim_id']
1963 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
1964 #look for network at datacenter and return error
1965 else:
1966 #There is not a netmap, look at datacenter for a net with this name and create if not found
1967 create_network = True
1968 lookfor_network = True
1969 lookfor_filter["name"] = sce_net["name"]
1970 net_vim_name = sce_net["name"]
1971 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01001972 else:
tiernobe41e222016-09-02 15:16:13 +02001973 if not net_name:
1974 net_name = "%s.%s" %(instance_name, sce_net["name"])
1975 net_name = net_name[:255] #limit length
1976 net_vim_name = net_name
1977 create_network = True
1978 lookfor_network = False
1979
1980 if lookfor_network:
1981 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
1982 if len(vim_nets) > 1:
1983 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1984 elif len(vim_nets) == 0:
1985 if not create_network:
1986 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1987 else:
1988 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
tiernobe41e222016-09-02 15:16:13 +02001989 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
1990 create_network = False
1991 if create_network:
1992 #if network is not external
garciadeblas9f8456e2016-09-05 05:02:59 +02001993 network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
tiernobe41e222016-09-02 15:16:13 +02001994 sce_net["vim_id_sites"][datacenter_id] = network_id
1995 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = network_id
1996 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001997 sce_net["created"] = True
tierno7edb6752016-03-21 17:37:52 +01001998
1999 #2. Creating new nets (vnf internal nets) in the VIM"
2000 #For each vnf net, we create it and we add it to instanceNetlist.
2001 for sce_vnf in scenarioDict['vnfs']:
2002 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02002003 if sce_vnf.get("datacenter"):
2004 vim = myvims[ sce_vnf["datacenter"] ]
2005 datacenter_id = sce_vnf["datacenter"]
2006 else:
2007 vim = myvims[ default_datacenter_id ]
2008 datacenter_id = default_datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002009 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
2010 net_name = descriptor_net.get("name")
2011 if not net_name:
2012 net_name = "%s.%s" %(instance_name, net["name"])
2013 net_name = net_name[:255] #limit length
2014 net_type = net['type']
garciadeblas9f8456e2016-09-05 05:02:59 +02002015 network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
tierno7edb6752016-03-21 17:37:52 +01002016 net['vim_id'] = network_id
2017 if sce_vnf['uuid'] not in auxNetDict:
2018 auxNetDict[sce_vnf['uuid']] = {}
2019 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2020 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002021 net["created"] = True
2022
tierno7edb6752016-03-21 17:37:52 +01002023
tiernoae4a8d12016-07-08 12:30:39 +02002024 #print "auxNetDict:"
2025 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002026
2027 #3. Creating new vm instances in the VIM
tiernoae4a8d12016-07-08 12:30:39 +02002028 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
tierno7edb6752016-03-21 17:37:52 +01002029 for sce_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002030 if sce_vnf.get("datacenter"):
2031 vim = myvims[ sce_vnf["datacenter"] ]
2032 datacenter_id = sce_vnf["datacenter"]
2033 else:
2034 vim = myvims[ default_datacenter_id ]
2035 datacenter_id = default_datacenter_id
2036 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002037 i = 0
2038 for vm in sce_vnf['vms']:
2039 i += 1
2040 myVMDict = {}
tiernoae65a482016-11-24 16:20:05 +01002041 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
tierno7edb6752016-03-21 17:37:52 +01002042 myVMDict['description'] = myVMDict['name'][0:99]
2043# if not startvms:
2044# myVMDict['start'] = "no"
2045 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2046 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002047 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00002048 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01002049 vm['vim_image_id'] = image_id
2050
2051 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002052 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01002053 if flavor_dict['extended']!=None:
2054 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00002055 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2056
2057
2058
2059
2060 #Obtain information for additional disks
2061 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2062 if not extended_flavor_dict:
2063 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2064 return
2065
2066 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2067 myVMDict['disks'] = None
2068 extended_info = extended_flavor_dict[0]['extended']
2069 if extended_info != None:
2070 extended_flavor_dict_yaml = yaml.load(extended_info)
2071 if 'disks' in extended_flavor_dict_yaml:
2072 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2073
2074
2075
2076
tierno7edb6752016-03-21 17:37:52 +01002077 vm['vim_flavor_id'] = flavor_id
2078
2079 myVMDict['imageRef'] = vm['vim_image_id']
2080 myVMDict['flavorRef'] = vm['vim_flavor_id']
2081 myVMDict['networks'] = []
tiernoa2793912016-10-04 08:15:08 +00002082 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno7edb6752016-03-21 17:37:52 +01002083 for iface in vm['interfaces']:
2084 netDict = {}
2085 if iface['type']=="data":
2086 netDict['type'] = iface['model']
2087 elif "model" in iface and iface["model"]!=None:
2088 netDict['model']=iface['model']
2089 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2090 #discover type of interface looking at flavor
2091 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2092 for flavor_iface in numa.get('interfaces',[]):
2093 if flavor_iface.get('name') == iface['internal_name']:
2094 if flavor_iface['dedicated'] == 'yes':
2095 netDict['type']="PF" #passthrough
2096 elif flavor_iface['dedicated'] == 'no':
2097 netDict['type']="VF" #siov
2098 elif flavor_iface['dedicated'] == 'yes:sriov':
2099 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2100 netDict["mac_address"] = flavor_iface.get("mac_address")
2101 break;
2102 netDict["use"]=iface['type']
2103 if netDict["use"]=="data" and not netDict.get("type"):
2104 #print "netDict", netDict
2105 #print "iface", iface
2106 e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
2107 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02002108 raise NfvoException(e_text + "After database migration some information is not available. \
2109 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002110 else:
tiernoae4a8d12016-07-08 12:30:39 +02002111 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002112 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2113 netDict["type"]="virtual"
2114 if "vpci" in iface and iface["vpci"] is not None:
2115 netDict['vpci'] = iface['vpci']
2116 if "mac" in iface and iface["mac"] is not None:
2117 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002118 if "port-security" in iface and iface["port-security"] is not None:
2119 netDict['port_security'] = iface['port-security']
2120 if "floating-ip" in iface and iface["floating-ip"] is not None:
2121 netDict['floating_ip'] = iface['floating-ip']
tierno7edb6752016-03-21 17:37:52 +01002122 netDict['name'] = iface['internal_name']
2123 if iface['net_id'] is None:
2124 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002125 #print iface
2126 #print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01002127 if vnf_iface['interface_id']==iface['uuid']:
tiernobe41e222016-09-02 15:16:13 +02002128 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002129 break
2130 else:
2131 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2132 #skip bridge ifaces not connected to any net
2133 #if 'net_id' not in netDict or netDict['net_id']==None:
2134 # continue
2135 myVMDict['networks'].append(netDict)
tiernoae4a8d12016-07-08 12:30:39 +02002136 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2137 #print myVMDict['name']
2138 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2139 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2140 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tierno36c0b172017-01-12 18:32:28 +01002141 if vm.get("boot_data"):
2142 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
2143 else:
2144 cloud_config_vm = cloud_config
tiernobe41e222016-09-02 15:16:13 +02002145 vm_id = vim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
tierno36c0b172017-01-12 18:32:28 +01002146 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'], cloud_config = cloud_config_vm,
montesmoreno0c8def02016-12-22 12:16:23 +00002147 disk_list = myVMDict['disks'])
2148
tierno7edb6752016-03-21 17:37:52 +01002149 vm['vim_id'] = vm_id
2150 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2151 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2152 for net in myVMDict['networks']:
2153 if "vim_id" in net:
2154 for iface in vm['interfaces']:
2155 if net["name"]==iface["internal_name"]:
2156 iface["vim_id"]=net["vim_id"]
2157 break
tiernoa2793912016-10-04 08:15:08 +00002158 scenarioDict["datacenter2tenant"] = datacenter2tenant
2159 logger.debug("create_instance Deployment done scenarioDict: %s",
2160 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
tiernof97fd272016-07-11 14:32:37 +02002161 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
2162 return mydb.get_instance_scenario(instance_id)
2163 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02002164 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002165 if isinstance(e, db_base_Exception):
2166 error_text = "database Exception"
2167 elif isinstance(e, vimconn.vimconnException):
2168 error_text = "VIM Exception"
2169 else:
2170 error_text = "Exception"
2171 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2172 #logger.error("create_instance: %s", error_text)
2173 raise NfvoException(error_text, e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002174
tierno7edb6752016-03-21 17:37:52 +01002175def delete_instance(mydb, tenant_id, instance_id):
tiernoae4a8d12016-07-08 12:30:39 +02002176 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002177 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02002178 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002179 tenant_id = instanceDict["tenant_id"]
tiernoae4a8d12016-07-08 12:30:39 +02002180 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01002181
tiernoa2793912016-10-04 08:15:08 +00002182 #1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02002183 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002184
2185 #2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00002186 error_msg = ""
2187 myvims={}
tierno7edb6752016-03-21 17:37:52 +01002188
2189 #2.1 deleting VMs
2190 #vm_fail_list=[]
2191 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002192 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2193 if datacenter_key not in myvims:
2194 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2195 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2196 if len(vims) == 0:
2197 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2198 sce_vnf["datacenter_tenant_id"]))
2199 myvims[datacenter_key] = None
2200 else:
2201 myvims[datacenter_key] = vims.values()[0]
2202 myvim = myvims[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01002203 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002204 if not myvim:
2205 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2206 continue
tiernoae4a8d12016-07-08 12:30:39 +02002207 try:
2208 myvim.delete_vminstance(vm['vim_vm_id'])
2209 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002210 error_msg+="\n VM VIM_id={} not found at datacenter={}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
tiernoae4a8d12016-07-08 12:30:39 +02002211 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2212 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2213 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002214 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2215 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
tiernoae4a8d12016-07-08 12:30:39 +02002216 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
tierno7edb6752016-03-21 17:37:52 +01002217
2218 #2.2 deleting NETS
2219 #net_fail_list=[]
2220 for net in instanceDict['nets']:
tierno66345bc2016-09-26 11:37:55 +02002221 if not net['created']:
tierno7edb6752016-03-21 17:37:52 +01002222 continue #skip not created nets
tiernoa2793912016-10-04 08:15:08 +00002223 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2224 if datacenter_key not in myvims:
2225 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2226 datacenter_tenant_id=net["datacenter_tenant_id"])
2227 if len(vims) == 0:
2228 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2229 myvims[datacenter_key] = None
2230 else:
2231 myvims[datacenter_key] = vims.values()[0]
2232 myvim = myvims[datacenter_key]
2233
tierno7edb6752016-03-21 17:37:52 +01002234 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00002235 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
tierno7edb6752016-03-21 17:37:52 +01002236 continue
tiernoae4a8d12016-07-08 12:30:39 +02002237 try:
2238 myvim.delete_network(net['vim_net_id'])
2239 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002240 error_msg+="\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
2241 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
2242 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
tiernoae4a8d12016-07-08 12:30:39 +02002243 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002244 error_msg+="\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'], net["datacenter_id"], e.http_code, str(e))
2245 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
2246 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
tierno7edb6752016-03-21 17:37:52 +01002247 if len(error_msg)>0:
tiernof97fd272016-07-11 14:32:37 +02002248 return 'instance ' + message + ' deleted but some elements could not be deleted, or already deleted (error: 404) from VIM: ' + error_msg
tierno7edb6752016-03-21 17:37:52 +01002249 else:
tiernof97fd272016-07-11 14:32:37 +02002250 return 'instance ' + message + ' deleted'
tierno7edb6752016-03-21 17:37:52 +01002251
2252def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2253 '''Refreshes a scenario instance. It modifies instanceDict'''
2254 '''Returns:
2255 - result: <0 if there is any unexpected error, n>=0 if no errors where n is the number of vms and nets that couldn't be updated in the database
2256 - error_msg
2257 '''
2258 # Assumption: nfvo_tenant and instance_id were checked before entering into this function
tiernoae4a8d12016-07-08 12:30:39 +02002259 #print "nfvo.refresh_instance begins"
tierno7edb6752016-03-21 17:37:52 +01002260 #print json.dumps(instanceDict, indent=4)
2261
tiernoae4a8d12016-07-08 12:30:39 +02002262 #print "Getting the VIM URL and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002263 myvims={}
2264
tiernoae4a8d12016-07-08 12:30:39 +02002265 # 1. Getting VIM vm and net list
tierno7edb6752016-03-21 17:37:52 +01002266 vms_updated = [] #List of VM instance uuids in openmano that were updated
2267 vms_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002268 vm_list = {}
tierno7edb6752016-03-21 17:37:52 +01002269 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002270 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2271 if datacenter_key not in vm_list:
2272 vm_list[datacenter_key] = []
2273 if datacenter_key not in myvims:
2274 vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2275 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2276 if len(vims) == 0:
2277 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2278 myvims[datacenter_key] = None
2279 else:
2280 myvims[datacenter_key] = vims.values()[0]
tierno7edb6752016-03-21 17:37:52 +01002281 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002282 vm_list[datacenter_key].append(vm['vim_vm_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002283 vms_notupdated.append(vm["uuid"])
2284
2285 nets_updated = [] #List of VM instance uuids in openmano that were updated
tierno7edb6752016-03-21 17:37:52 +01002286 nets_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002287 net_list = {}
tierno7edb6752016-03-21 17:37:52 +01002288 for net in instanceDict['nets']:
tiernoa2793912016-10-04 08:15:08 +00002289 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2290 if datacenter_key not in net_list:
2291 net_list[datacenter_key] = []
2292 if datacenter_key not in myvims:
2293 vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2294 datacenter_tenant_id=net["datacenter_tenant_id"])
2295 if len(vims) == 0:
2296 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2297 myvims[datacenter_key] = None
2298 else:
2299 myvims[datacenter_key] = vims.values()[0]
2300
2301 net_list[datacenter_key].append(net['vim_net_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002302 nets_notupdated.append(net["uuid"])
2303
tiernoa2793912016-10-04 08:15:08 +00002304 # 1. Getting the status of all VMs
2305 vm_dict={}
2306 for datacenter_key in myvims:
2307 if not vm_list.get(datacenter_key):
2308 continue
2309 failed = True
2310 failed_message=""
2311 if not myvims[datacenter_key]:
2312 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2313 else:
2314 try:
2315 vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2316 failed = False
2317 except vimconn.vimconnException as e:
2318 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2319 failed_message = str(e)
2320 if failed:
2321 for vm in vm_list[datacenter_key]:
2322 vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
tiernoae4a8d12016-07-08 12:30:39 +02002323
tiernoa2793912016-10-04 08:15:08 +00002324 # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2325 for sce_vnf in instanceDict['vnfs']:
2326 for vm in sce_vnf['vms']:
2327 vm_id = vm['vim_vm_id']
2328 interfaces = vm_dict[vm_id].pop('interfaces', [])
2329 #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2330 has_mgmt_iface = False
2331 for iface in vm["interfaces"]:
2332 if iface["type"]=="mgmt":
2333 has_mgmt_iface = True
2334 if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2335 vm_dict[vm_id]['status'] = "ACTIVE"
tiernoa3d49e62016-10-05 15:20:26 +00002336 if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2337 vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
tiernoa2793912016-10-04 08:15:08 +00002338 if vm['status'] != vm_dict[vm_id]['status'] or vm.get('error_msg')!=vm_dict[vm_id].get('error_msg') or vm.get('vim_info')!=vm_dict[vm_id].get('vim_info'):
2339 vm['status'] = vm_dict[vm_id]['status']
2340 vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2341 vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2342 # 2.1. Update in openmano DB the VMs whose status changed
tiernof97fd272016-07-11 14:32:37 +02002343 try:
tiernoa2793912016-10-04 08:15:08 +00002344 updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2345 vms_notupdated.remove(vm["uuid"])
2346 if updates>0:
2347 vms_updated.append(vm["uuid"])
tiernof97fd272016-07-11 14:32:37 +02002348 except db_base_Exception as e:
2349 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tiernoa2793912016-10-04 08:15:08 +00002350 # 2.2. Update in openmano DB the interface VMs
2351 for interface in interfaces:
2352 #translate from vim_net_id to instance_net_id
2353 network_id_list=[]
2354 for net in instanceDict['nets']:
2355 if net["vim_net_id"] == interface["vim_net_id"]:
2356 network_id_list.append(net["uuid"])
2357 if not network_id_list:
2358 continue
2359 del interface["vim_net_id"]
2360 try:
2361 for network_id in network_id_list:
2362 mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2363 except db_base_Exception as e:
2364 logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2365
2366 # 3. Getting the status of all nets
2367 net_dict = {}
2368 for datacenter_key in myvims:
2369 if not net_list.get(datacenter_key):
2370 continue
2371 failed = True
2372 failed_message = ""
2373 if not myvims[datacenter_key]:
2374 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2375 else:
2376 try:
2377 net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2378 failed = False
2379 except vimconn.vimconnException as e:
2380 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2381 failed_message = str(e)
2382 if failed:
2383 for net in net_list[datacenter_key]:
2384 net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2385
2386 # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2387 # TODO: update nets inside a vnf
2388 for net in instanceDict['nets']:
2389 net_id = net['vim_net_id']
tiernoa3d49e62016-10-05 15:20:26 +00002390 if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2391 net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
tiernoa2793912016-10-04 08:15:08 +00002392 if net['status'] != net_dict[net_id]['status'] or net.get('error_msg')!=net_dict[net_id].get('error_msg') or net.get('vim_info')!=net_dict[net_id].get('vim_info'):
2393 net['status'] = net_dict[net_id]['status']
2394 net['error_msg'] = net_dict[net_id].get('error_msg')
2395 net['vim_info'] = net_dict[net_id].get('vim_info')
2396 # 5.1. Update in openmano DB the nets whose status changed
2397 try:
2398 updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2399 nets_notupdated.remove(net["uuid"])
2400 if updated>0:
2401 nets_updated.append(net["uuid"])
2402 except db_base_Exception as e:
2403 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tierno7edb6752016-03-21 17:37:52 +01002404
2405 # Returns appropriate output
tiernoae4a8d12016-07-08 12:30:39 +02002406 #print "nfvo.refresh_instance finishes"
2407 logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2408 str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01002409 instance_id = instanceDict['uuid']
tierno7edb6752016-03-21 17:37:52 +01002410 if len(vms_notupdated)+len(nets_notupdated)>0:
tiernoae4a8d12016-07-08 12:30:39 +02002411 error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
tierno7edb6752016-03-21 17:37:52 +01002412 return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
2413
tiernoae4a8d12016-07-08 12:30:39 +02002414 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01002415
2416def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02002417 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002418 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002419 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2420
tiernoae4a8d12016-07-08 12:30:39 +02002421 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02002422 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2423 if len(vims) == 0:
2424 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002425 myvim = vims.values()[0]
2426
2427
2428 input_vnfs = action_dict.pop("vnfs", [])
2429 input_vms = action_dict.pop("vms", [])
2430 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2431 vm_result = {}
2432 vm_error = 0
2433 vm_ok = 0
2434 for sce_vnf in instanceDict['vnfs']:
2435 for vm in sce_vnf['vms']:
2436 if not action_over_all:
2437 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2438 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2439 continue
tiernoae4a8d12016-07-08 12:30:39 +02002440 try:
2441 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
tierno7edb6752016-03-21 17:37:52 +01002442 if "console" in action_dict:
tierno20fc2a22016-08-19 17:02:35 +02002443 if not global_config["http_console_proxy"]:
2444 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2445 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2446 protocol=data["protocol"],
2447 ip = data["server"],
2448 port = data["port"],
2449 suffix = data["suffix"]),
2450 "name":vm['name']
2451 }
2452 vm_ok +=1
2453 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
tierno7edb6752016-03-21 17:37:52 +01002454 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2455 "description": "this console is only reachable by local interface",
2456 "name":vm['name']
2457 }
2458 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02002459 else:
tierno7edb6752016-03-21 17:37:52 +01002460 #print "console data", data
tierno20fc2a22016-08-19 17:02:35 +02002461 try:
2462 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2463 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2464 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2465 protocol=data["protocol"],
2466 ip = global_config["http_console_host"],
2467 port = console_thread.port,
2468 suffix = data["suffix"]),
2469 "name":vm['name']
2470 }
2471 vm_ok +=1
2472 except NfvoException as e:
2473 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2474 vm_error+=1
2475
tierno7edb6752016-03-21 17:37:52 +01002476 else:
tiernof97fd272016-07-11 14:32:37 +02002477 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
tierno7edb6752016-03-21 17:37:52 +01002478 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02002479 except vimconn.vimconnException as e:
2480 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2481 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01002482
2483 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02002484 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002485 else:
tierno351863c2016-07-23 01:46:03 +02002486 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002487
2488def create_or_use_console_proxy_thread(console_server, console_port):
2489 #look for a non-used port
2490 console_thread_key = console_server + ":" + str(console_port)
2491 if console_thread_key in global_config["console_thread"]:
2492 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02002493 return global_config["console_thread"][console_thread_key]
tierno7edb6752016-03-21 17:37:52 +01002494
2495 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02002496 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01002497 if port in global_config["console_ports"]:
2498 continue
2499 try:
2500 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2501 clithread.start()
2502 global_config["console_thread"][console_thread_key] = clithread
2503 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02002504 return clithread
tierno7edb6752016-03-21 17:37:52 +01002505 except cli.ConsoleProxyExceptionPortUsed as e:
2506 #port used, try with onoher
2507 continue
2508 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02002509 raise NfvoException(str(e), HTTP_Bad_Request)
2510 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002511
2512def check_tenant(mydb, tenant_id):
2513 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02002514 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2515 if not tenant:
2516 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2517 return
tierno7edb6752016-03-21 17:37:52 +01002518
2519def new_tenant(mydb, tenant_dict):
tiernof97fd272016-07-11 14:32:37 +02002520 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2521 return tenant_id
tierno7edb6752016-03-21 17:37:52 +01002522
2523def delete_tenant(mydb, tenant):
2524 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002525
2526 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2527 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2528 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01002529
2530def new_datacenter(mydb, datacenter_descriptor):
2531 if "config" in datacenter_descriptor:
2532 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02002533 #Check that datacenter-type is correct
2534 datacenter_type = datacenter_descriptor.get("type", "openvim");
2535 module_info = None
2536 try:
2537 module = "vimconn_" + datacenter_type
2538 module_info = imp.find_module(module)
2539 except (IOError, ImportError):
2540 if module_info and module_info[0]:
2541 file.close(module_info[0])
2542 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
2543
tiernof97fd272016-07-11 14:32:37 +02002544 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2545 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002546
2547def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2548 #obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02002549 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno7edb6752016-03-21 17:37:52 +01002550 #edit data
tiernof97fd272016-07-11 14:32:37 +02002551 datacenter_id = datacenter['uuid']
2552 where={'uuid': datacenter['uuid']}
tierno7edb6752016-03-21 17:37:52 +01002553 if "config" in datacenter_descriptor:
2554 if datacenter_descriptor['config']!=None:
2555 try:
2556 new_config_dict = datacenter_descriptor["config"]
2557 #delete null fields
2558 to_delete=[]
2559 for k in new_config_dict:
2560 if new_config_dict[k]==None:
2561 to_delete.append(k)
2562
tiernof97fd272016-07-11 14:32:37 +02002563 config_dict = yaml.load(datacenter["config"])
tierno7edb6752016-03-21 17:37:52 +01002564 config_dict.update(new_config_dict)
2565 #delete null fields
2566 for k in to_delete:
2567 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02002568 except Exception as e:
2569 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002570 datacenter_descriptor["config"]= yaml.safe_dump(config_dict,default_flow_style=True,width=256) if len(config_dict)>0 else None
tiernof97fd272016-07-11 14:32:37 +02002571 mydb.update_rows('datacenters', datacenter_descriptor, where)
2572 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002573
2574def delete_datacenter(mydb, datacenter):
2575 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002576 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2577 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2578 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01002579
tierno8008c3a2016-10-13 15:34:28 +00002580def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
tierno7edb6752016-03-21 17:37:52 +01002581 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002582 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002583 datacenter_name=myvim["name"]
2584
2585 create_vim_tenant=True if vim_tenant_id==None and vim_tenant_name==None else False
2586
2587 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002588 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002589 if vim_tenant_name==None:
2590 vim_tenant_name=tenant_dict['name']
2591
2592 #check that this association does not exist before
2593 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernof97fd272016-07-11 14:32:37 +02002594 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2595 if len(tenants_datacenters)>0:
2596 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002597
2598 vim_tenant_id_exist_atdb=False
2599 if not create_vim_tenant:
2600 where_={"datacenter_id": datacenter_id}
2601 if vim_tenant_id!=None:
2602 where_["vim_tenant_id"] = vim_tenant_id
2603 if vim_tenant_name!=None:
2604 where_["vim_tenant_name"] = vim_tenant_name
2605 #check if vim_tenant_id is already at database
tiernof97fd272016-07-11 14:32:37 +02002606 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2607 if len(datacenter_tenants_dict)>=1:
tierno7edb6752016-03-21 17:37:52 +01002608 datacenter_tenants_dict = datacenter_tenants_dict[0]
2609 vim_tenant_id_exist_atdb=True
2610 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2611 else: #result=0
2612 datacenter_tenants_dict = {}
2613 #insert at table datacenter_tenants
2614 else: #if vim_tenant_id==None:
2615 #create tenant at VIM if not provided
tiernoae4a8d12016-07-08 12:30:39 +02002616 try:
2617 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2618 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002619 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002620 datacenter_tenants_dict = {}
2621 datacenter_tenants_dict["created"]="true"
2622
2623 #fill datacenter_tenants table
2624 if not vim_tenant_id_exist_atdb:
2625 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
2626 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
2627 datacenter_tenants_dict["user"] = vim_username
2628 datacenter_tenants_dict["passwd"] = vim_password
2629 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tierno8008c3a2016-10-13 15:34:28 +00002630 if config:
2631 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
tiernof97fd272016-07-11 14:32:37 +02002632 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002633 datacenter_tenants_dict["uuid"] = id_
2634
2635 #fill tenants_datacenters table
2636 tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"]
tiernof97fd272016-07-11 14:32:37 +02002637 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
2638 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002639
2640def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2641 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002642 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002643
2644 #get nfvo_tenant info
2645 if not tenant_id or tenant_id=="any":
2646 tenant_uuid = None
2647 else:
tiernof97fd272016-07-11 14:32:37 +02002648 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002649 tenant_uuid = tenant_dict['uuid']
2650
2651 #check that this association exist before
2652 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2653 if tenant_uuid:
2654 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02002655 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2656 if len(tenant_datacenter_list)==0 and tenant_uuid:
2657 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002658
2659 #delete this association
tiernof97fd272016-07-11 14:32:37 +02002660 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01002661
2662 #get vim_tenant info and deletes
2663 warning=''
2664 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02002665 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2666 #try to delete vim:tenant
2667 try:
2668 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2669 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01002670 #delete tenant at VIM if created by NFVO
tiernoae4a8d12016-07-08 12:30:39 +02002671 try:
2672 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2673 except vimconn.vimconnException as e:
2674 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2675 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02002676 except db_base_Exception as e:
2677 logger.error("Cannot delete datacenter_tenants " + str(e))
2678 pass #the error will be caused because dependencies, vim_tenant can not be deleted
tierno7edb6752016-03-21 17:37:52 +01002679
tiernof97fd272016-07-11 14:32:37 +02002680 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01002681
2682def datacenter_action(mydb, tenant_id, datacenter, action_dict):
2683 #DEPRECATED
2684 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002685 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002686
2687 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02002688 try:
tiernof97fd272016-07-11 14:32:37 +02002689 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02002690 #print content
2691 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002692 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
2693 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002694 #update nets Change from VIM format to NFVO format
2695 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002696 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01002697 net_nfvo={'datacenter_id': datacenter_id}
2698 net_nfvo['name'] = net['name']
2699 #net_nfvo['description']= net['name']
2700 net_nfvo['vim_net_id'] = net['id']
2701 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2702 net_nfvo['shared'] = net['shared']
2703 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
2704 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002705 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
2706 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
2707 return inserted
tierno7edb6752016-03-21 17:37:52 +01002708 elif 'net-edit' in action_dict:
2709 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02002710 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002711 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01002712 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002713 return result
tierno7edb6752016-03-21 17:37:52 +01002714 elif 'net-delete' in action_dict:
2715 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02002716 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002717 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01002718 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002719 return result
tierno7edb6752016-03-21 17:37:52 +01002720
2721 else:
tiernof97fd272016-07-11 14:32:37 +02002722 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002723
2724def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
2725 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002726 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002727
tierno42fcc3b2016-07-06 17:20:40 +02002728 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002729 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01002730 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02002731 return result
tierno7edb6752016-03-21 17:37:52 +01002732
2733def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
2734 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002735 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002736 filter_dict={}
2737 if action_dict:
2738 action_dict = action_dict["netmap"]
2739 if 'vim_id' in action_dict:
2740 filter_dict["id"] = action_dict['vim_id']
2741 if 'vim_name' in action_dict:
2742 filter_dict["name"] = action_dict['vim_name']
2743 else:
2744 filter_dict["shared"] = True
2745
tiernoae4a8d12016-07-08 12:30:39 +02002746 try:
tiernof97fd272016-07-11 14:32:37 +02002747 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02002748 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002749 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
2750 raise NfvoException(str(e), HTTP_Internal_Server_Error)
2751 if len(vim_nets)>1 and action_dict:
2752 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
2753 elif len(vim_nets)==0: # and action_dict:
2754 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002755 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002756 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01002757 net_nfvo={'datacenter_id': datacenter_id}
2758 if action_dict and "name" in action_dict:
2759 net_nfvo['name'] = action_dict['name']
2760 else:
2761 net_nfvo['name'] = net['name']
2762 #net_nfvo['description']= net['name']
2763 net_nfvo['vim_net_id'] = net['id']
2764 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2765 net_nfvo['shared'] = net['shared']
2766 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02002767 try:
2768 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002769 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02002770 net_nfvo["uuid"] = net_id
2771 except db_base_Exception as e:
2772 if action_dict:
2773 raise
2774 else:
2775 net_nfvo["status"] = "FAIL: " + str(e)
tierno7edb6752016-03-21 17:37:52 +01002776 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002777 return net_list
tierno7edb6752016-03-21 17:37:52 +01002778
2779def vim_action_get(mydb, tenant_id, datacenter, item, name):
2780 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002781 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002782 filter_dict={}
2783 if name:
tierno42fcc3b2016-07-06 17:20:40 +02002784 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01002785 filter_dict["id"] = name
2786 else:
2787 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02002788 try:
2789 if item=="networks":
2790 #filter_dict['tenant_id'] = myvim['tenant_id']
2791 content = myvim.get_network_list(filter_dict=filter_dict)
2792 elif item=="tenants":
2793 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01002794 elif item == "images":
2795 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02002796 else:
tiernof97fd272016-07-11 14:32:37 +02002797 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02002798 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02002799 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02002800 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02002801 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02002802 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
tiernobe41e222016-09-02 15:16:13 +02002803 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002804 else:
tiernof97fd272016-07-11 14:32:37 +02002805 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02002806 except vimconn.vimconnException as e:
2807 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02002808 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002809
2810def vim_action_delete(mydb, tenant_id, datacenter, item, name):
2811 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02002812 if tenant_id == "any":
2813 tenant_id=None
2814
tiernoa2793912016-10-04 08:15:08 +00002815 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02002816 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02002817 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
2818 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02002819 items = content.values()[0]
2820 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02002821 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002822 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02002823 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002824 else: # it is a dict
2825 item_id = items["id"]
2826 item_name = str(items.get("name"))
tierno7edb6752016-03-21 17:37:52 +01002827
tiernoae4a8d12016-07-08 12:30:39 +02002828 try:
2829 if item=="networks":
2830 content = myvim.delete_network(item_id)
2831 elif item=="tenants":
2832 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01002833 elif item == "images":
2834 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02002835 else:
tiernof97fd272016-07-11 14:32:37 +02002836 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002837 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002838 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
2839 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002840
tiernof97fd272016-07-11 14:32:37 +02002841 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno7edb6752016-03-21 17:37:52 +01002842
2843def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
2844 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002845 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02002846 if tenant_id == "any":
2847 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00002848 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002849 try:
2850 if item=="networks":
2851 net = descriptor["network"]
2852 net_name = net.pop("name")
2853 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02002854 net_public = net.pop("shared", False)
2855 net_ipprofile = net.pop("ip_profile", None)
2856 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, **net)
tiernoae4a8d12016-07-08 12:30:39 +02002857 elif item=="tenants":
2858 tenant = descriptor["tenant"]
2859 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
2860 else:
tiernof97fd272016-07-11 14:32:37 +02002861 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002862 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002863 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002864
tierno7edb6752016-03-21 17:37:52 +01002865 return vim_action_get(mydb, tenant_id, datacenter, item, content)
2866
tierno66aa0372016-07-06 17:31:12 +02002867