blob: c8ef020d75a300b583bfa1b5fc7f3701974aae69 [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:
garciadeblasb6153a22017-02-06 15:38:33 +0100367 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
368 raise vimconn.vimconnException(str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200369 except vimconn.vimconnException as e:
370 if return_on_error:
garciadeblas14480452017-01-10 13:08:07 +0100371 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200372 raise
tierno5e91eb82016-10-04 09:39:07 +0000373 image_vim_id = None
garciadeblas14480452017-01-10 13:08:07 +0100374 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
tiernoae4a8d12016-07-08 12:30:39 +0200375 continue
376 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000377 if return_on_error:
378 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
379 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200380 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000381 image_vim_id = None
garciadeblas30833382017-01-09 09:46:31 +0100382 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200383 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200384 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100385 #add new vim_id at datacenters_images
386 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
387 elif image_db[0]["vim_id"]!=image_vim_id:
388 #modify existing vim_id at datacenters_images
389 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
390
tiernof97fd272016-07-11 14:32:37 +0200391 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100392
tierno5e91eb82016-10-04 09:39:07 +0000393def 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 +0100394 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
395 'ram':flavor_dict.get('ram'),
396 'vcpus':flavor_dict.get('vcpus'),
397 }
398 if 'extended' in flavor_dict and flavor_dict['extended']==None:
399 del flavor_dict['extended']
400 if 'extended' in flavor_dict:
401 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
402
403 #look if flavor exist
404 if only_create_at_vim:
405 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000406 if return_on_error == None:
407 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100408 else:
tiernof97fd272016-07-11 14:32:37 +0200409 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
410 if len(flavors)>=1:
411 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100412 else:
413 #create flavor
414 #create one by one the images of aditional disks
415 dev_image_list=[] #list of images
416 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
417 dev_nb=0
418 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200419 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100420 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200421 image_dict={}
422 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
423 image_dict['universal_name']=device.get('image name')
424 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
425 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100426 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200427 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100428 image_metadata_dict = device.get('image metadata', None)
429 image_metadata_str = None
430 if image_metadata_dict != None:
431 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
432 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200433 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
434 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100435 dev_image_list.append(image_id)
436 dev_nb += 1
437 temp_flavor_dict['name'] = flavor_dict['name']
438 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200439 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
440 flavor_mano_id= content
441 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100442 #create flavor at every vim
443 if 'uuid' in flavor_dict:
444 del flavor_dict['uuid']
445 flavor_vim_id=None
446 for vim_id,vim in vims.items():
447 flavor_created="false"
448 #look at database
tiernof97fd272016-07-11 14:32:37 +0200449 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100450 #look at VIM if this flavor exist SKIPPED
451 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
452 #if res_vim < 0:
453 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
454 # continue
455 #elif res_vim==0:
456
457 #Create the flavor in VIM
458 #Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000459 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100460 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
461 #make a copy of original devices
462 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000463
tierno7edb6752016-03-21 17:37:52 +0100464 for device in flavor_dict["extended"].get("devices",[]):
465 dev={}
466 dev.update(device)
467 devices_original.append(dev)
468 if 'image' in device:
469 del device['image']
470 if 'image metadata' in device:
471 del device['image metadata']
472 dev_nb=0
473 for index in range(0,len(devices_original)) :
474 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000475 if "image" not in device and "image name" not in device:
476 if 'size' in device:
477 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100478 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200479 image_dict={}
480 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
481 image_dict['universal_name']=device.get('image name')
482 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
483 image_dict['location']=device.get('image')
garciadeblas14480452017-01-10 13:08:07 +0100484 #image_dict['new_location']=device.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200485 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100486 image_metadata_dict = device.get('image metadata', None)
487 image_metadata_str = None
488 if image_metadata_dict != None:
489 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
490 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200491 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 +0100492 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200493 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 +0000494
495 #save disk information (image must be based on and size
496 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
497
tierno7edb6752016-03-21 17:37:52 +0100498 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
499 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200500 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100501 #check that this vim_id exist in VIM, if not create
502 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200503 try:
504 vim.get_flavor(flavor_vim_id)
505 continue #flavor exist
506 except vimconn.vimconnException:
507 pass
tierno7edb6752016-03-21 17:37:52 +0100508 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200509 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
510 try:
tiernocf157a82017-01-30 14:07:06 +0100511 flavor_vim_id = None
512 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
513 flavor_create="false"
514 except vimconn.vimconnException as e:
515 pass
516 try:
517 if not flavor_vim_id:
518 flavor_vim_id = vim.new_flavor(flavor_dict)
519 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
520 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200521 except vimconn.vimconnException as e:
522 if return_on_error:
523 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200524 raise
tiernoae4a8d12016-07-08 12:30:39 +0200525 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000526 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200527 continue
tierno7edb6752016-03-21 17:37:52 +0100528 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200529 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100530 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000531 extended_devices_yaml = None
532 if len(disk_list) > 0:
533 extended_devices = dict()
534 extended_devices['disks'] = disk_list
535 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
536 mydb.new_row('datacenters_flavors',
537 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
538 'created':flavor_created,'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100539 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
540 #modify existing vim_id at datacenters_flavors
541 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
542
tiernof97fd272016-07-11 14:32:37 +0200543 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100544
545def new_vnf(mydb, tenant_id, vnf_descriptor):
546 global global_config
547
548 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100549 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
tierno7edb6752016-03-21 17:37:52 +0100550 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100551 vims = {}
tierno7edb6752016-03-21 17:37:52 +0100552 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200553 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100554 if "tenant_id" in vnf_descriptor["vnf"]:
555 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200556 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
557 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100558 else:
559 vnf_descriptor['vnf']['tenant_id'] = tenant_id
560 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100561 if global_config["auto_push_VNF_to_VIMs"]:
562 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100563
564 # Step 4. Review the descriptor and add missing fields
565 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +0200566 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +0100567 vnf_name = vnf_descriptor['vnf']['name']
568 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
569 if "physical" in vnf_descriptor['vnf']:
570 del vnf_descriptor['vnf']['physical']
571 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100572
tierno7edb6752016-03-21 17:37:52 +0100573 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200574 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
575 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno7edb6752016-03-21 17:37:52 +0100576
577 #For each VNFC, we add it to the VNFCDict and we create a flavor.
578 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
579 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +0100580 try:
tiernof97fd272016-07-11 14:32:37 +0200581 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100582 for vnfc in vnf_descriptor['vnf']['VNFC']:
583 VNFCitem={}
584 VNFCitem["name"] = vnfc['name']
585 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
586
tiernof97fd272016-07-11 14:32:37 +0200587 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno7edb6752016-03-21 17:37:52 +0100588
589 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200590 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 +0100591 myflavorDict["description"] = VNFCitem["description"]
592 myflavorDict["ram"] = vnfc.get("ram", 0)
593 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
594 myflavorDict["disk"] = vnfc.get("disk", 1)
595 myflavorDict["extended"] = {}
596
597 devices = vnfc.get("devices")
598 if devices != None:
599 myflavorDict["extended"]["devices"] = devices
600
601 # TODO:
602 # 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
603 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
604
605 # Previous code has been commented
606 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
607 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
608 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
609 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
610 #else:
611 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
612 # if result2:
613 # print "Error creating flavor: unknown processor model. Rollback successful."
614 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
615 # else:
616 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
617 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
618
619 if 'numas' in vnfc and len(vnfc['numas'])>0:
620 myflavorDict['extended']['numas'] = vnfc['numas']
621
622 #print myflavorDict
623
624 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200625 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +0100626
tiernof97fd272016-07-11 14:32:37 +0200627 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100628 VNFCitem["flavor_id"] = flavor_id
629 VNFCDict[vnfc['name']] = VNFCitem
630
tiernof97fd272016-07-11 14:32:37 +0200631 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100632 # Step 6.3 New images are created in the VIM
633 #For each VNFC, we must create the appropriate image.
634 #This "for" loop might be integrated with the previous one
635 #In case this integration is made, the VNFCDict might become a VNFClist.
636 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +0200637 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200638 image_dict={}
639 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
640 image_dict['universal_name']=vnfc.get('image name')
641 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
642 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100643 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200644 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100645 image_metadata_dict = vnfc.get('image metadata', None)
646 image_metadata_str = None
647 if image_metadata_dict is not None:
648 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
649 image_dict['metadata']=image_metadata_str
650 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +0200651 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
652 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +0100653 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200654 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100655 if vnfc.get("boot-data"):
656 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
tierno7edb6752016-03-21 17:37:52 +0100657
tiernof97fd272016-07-11 14:32:37 +0200658
659 # Step 7. Storing the VNF descriptor in the repository
660 if "descriptor" not in vnf_descriptor["vnf"]:
661 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +0100662
tiernof97fd272016-07-11 14:32:37 +0200663 # Step 8. Adding the VNF to the NFVO DB
664 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
665 return vnf_id
666 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +0100667 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +0200668 if isinstance(e, db_base_Exception):
669 error_text = "Exception at database"
670 elif isinstance(e, KeyError):
671 error_text = "KeyError exception "
672 e.http_code = HTTP_Internal_Server_Error
673 else:
674 error_text = "Exception at VIM"
675 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
676 #logger.error("start_scenario %s", error_text)
677 raise NfvoException(error_text, e.http_code)
678
garciadeblas9f8456e2016-09-05 05:02:59 +0200679def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
680 global global_config
681
682 # Step 1. Check the VNF descriptor
tiernoafed5f12017-01-26 17:57:43 +0100683 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
garciadeblas9f8456e2016-09-05 05:02:59 +0200684 # Step 2. Check tenant exist
tiernod29b1d32017-01-25 11:02:52 +0100685 vims = {}
garciadeblas9f8456e2016-09-05 05:02:59 +0200686 if tenant_id != "any":
687 check_tenant(mydb, tenant_id)
688 if "tenant_id" in vnf_descriptor["vnf"]:
689 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
690 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
691 HTTP_Unauthorized)
692 else:
693 vnf_descriptor['vnf']['tenant_id'] = tenant_id
694 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernod29b1d32017-01-25 11:02:52 +0100695 if global_config["auto_push_VNF_to_VIMs"]:
696 vims = get_vim(mydb, tenant_id)
garciadeblas9f8456e2016-09-05 05:02:59 +0200697
698 # Step 4. Review the descriptor and add missing fields
699 #print vnf_descriptor
700 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
701 vnf_name = vnf_descriptor['vnf']['name']
702 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
703 if "physical" in vnf_descriptor['vnf']:
704 del vnf_descriptor['vnf']['physical']
705 #print vnf_descriptor
tiernoafed5f12017-01-26 17:57:43 +0100706
garciadeblas9f8456e2016-09-05 05:02:59 +0200707 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
708 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
709 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
710
711 #For each VNFC, we add it to the VNFCDict and we create a flavor.
712 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
713 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
714 try:
715 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
716 for vnfc in vnf_descriptor['vnf']['VNFC']:
717 VNFCitem={}
718 VNFCitem["name"] = vnfc['name']
719 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
720
721 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
722
723 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200724 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 +0200725 myflavorDict["description"] = VNFCitem["description"]
726 myflavorDict["ram"] = vnfc.get("ram", 0)
727 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
728 myflavorDict["disk"] = vnfc.get("disk", 1)
729 myflavorDict["extended"] = {}
730
731 devices = vnfc.get("devices")
732 if devices != None:
733 myflavorDict["extended"]["devices"] = devices
734
735 # TODO:
736 # 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
737 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
738
739 # Previous code has been commented
740 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
741 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
742 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
743 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
744 #else:
745 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
746 # if result2:
747 # print "Error creating flavor: unknown processor model. Rollback successful."
748 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
749 # else:
750 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
751 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
752
753 if 'numas' in vnfc and len(vnfc['numas'])>0:
754 myflavorDict['extended']['numas'] = vnfc['numas']
755
756 #print myflavorDict
757
758 # Step 6.2 New flavors are created in the VIM
759 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
760
761 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
762 VNFCitem["flavor_id"] = flavor_id
763 VNFCDict[vnfc['name']] = VNFCitem
764
765 logger.debug("Creating new images in the VIM for each VNFC")
766 # Step 6.3 New images are created in the VIM
767 #For each VNFC, we must create the appropriate image.
768 #This "for" loop might be integrated with the previous one
769 #In case this integration is made, the VNFCDict might become a VNFClist.
770 for vnfc in vnf_descriptor['vnf']['VNFC']:
771 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200772 image_dict={}
773 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
774 image_dict['universal_name']=vnfc.get('image name')
775 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
776 image_dict['location']=vnfc.get('VNFC image')
garciadeblas14480452017-01-10 13:08:07 +0100777 #image_dict['new_location']=vnfc.get('image location')
garciadeblasb69fa9f2016-09-28 12:04:10 +0200778 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +0200779 image_metadata_dict = vnfc.get('image metadata', None)
780 image_metadata_str = None
781 if image_metadata_dict is not None:
782 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
783 image_dict['metadata']=image_metadata_str
784 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
785 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
786 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
787 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200788 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno36c0b172017-01-12 18:32:28 +0100789 if vnfc.get("boot-data"):
790 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
garciadeblas9f8456e2016-09-05 05:02:59 +0200791
garciadeblas9f8456e2016-09-05 05:02:59 +0200792 # Step 7. Storing the VNF descriptor in the repository
793 if "descriptor" not in vnf_descriptor["vnf"]:
794 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
795
796 # Step 8. Adding the VNF to the NFVO DB
797 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
798 return vnf_id
799 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
800 _, message = rollback(mydb, vims, rollback_list)
801 if isinstance(e, db_base_Exception):
802 error_text = "Exception at database"
803 elif isinstance(e, KeyError):
804 error_text = "KeyError exception "
805 e.http_code = HTTP_Internal_Server_Error
806 else:
807 error_text = "Exception at VIM"
808 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
809 #logger.error("start_scenario %s", error_text)
810 raise NfvoException(error_text, e.http_code)
811
tierno7edb6752016-03-21 17:37:52 +0100812def get_vnf_id(mydb, tenant_id, vnf_id):
813 #check valid tenant_id
tiernof97fd272016-07-11 14:32:37 +0200814 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100815 #obtain data
816 where_or = {}
817 if tenant_id != "any":
818 where_or["tenant_id"] = tenant_id
819 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200820 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 +0100821
tiernof97fd272016-07-11 14:32:37 +0200822 vnf_id=vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100823 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +0200824 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +0100825 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
826 data={'vnf' : filtered_content}
827 #GET VM
tiernof97fd272016-07-11 14:32:37 +0200828 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tierno36c0b172017-01-12 18:32:28 +0100829 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'),
tierno7edb6752016-03-21 17:37:52 +0100830 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200831 if len(content)==0:
832 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno36c0b172017-01-12 18:32:28 +0100833 # change boot_data into boot-data
834 for vm in content:
835 if vm.get("boot_data"):
836 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
837 del vm["boot_data"]
tierno7edb6752016-03-21 17:37:52 +0100838
839 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200840 #TODO: GET all the information from a VNFC and include it in the output.
841
tierno7edb6752016-03-21 17:37:52 +0100842 #GET NET
tiernof97fd272016-07-11 14:32:37 +0200843 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +0100844 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
845 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200846 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200847
848 #GET ip-profile for each net
849 for net in data['vnf']['nets']:
850 ipprofiles = mydb.get_rows(FROM='ip_profiles',
851 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
852 WHERE={'net_id': net["uuid"]} )
853 if len(ipprofiles)==1:
854 net["ip_profile"] = ipprofiles[0]
855 elif len(ipprofiles)>1:
856 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
857
858
859 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
860
861 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +0200862 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 +0100863 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
864 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
865 WHERE={'vnfs.uuid': vnf_id},
866 WHERE_NOT={'interfaces.external_name': None} )
867 #print content
tiernof97fd272016-07-11 14:32:37 +0200868 data['vnf']['external-connections'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200869
tiernof97fd272016-07-11 14:32:37 +0200870 return data
tierno7edb6752016-03-21 17:37:52 +0100871
872
873def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
874 # Check tenant exist
875 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200876 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100877 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +0200878 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100879 else:
880 vims={}
881
882 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
883 where_or = {}
884 if tenant_id != "any":
885 where_or["tenant_id"] = tenant_id
886 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200887 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
888 vnf_id = vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100889
890 # "Getting the list of flavors and tenants of the VNF"
tiernof97fd272016-07-11 14:32:37 +0200891 flavorList = get_flavorlist(mydb, vnf_id)
892 if len(flavorList)==0:
893 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100894
tiernof97fd272016-07-11 14:32:37 +0200895 imageList = get_imagelist(mydb, vnf_id)
896 if len(imageList)==0:
897 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100898
tiernof97fd272016-07-11 14:32:37 +0200899 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
900 if deleted == 0:
901 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100902
903 undeletedItems = []
904 for flavor in flavorList:
905 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +0200906 try:
907 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
908 if len(c) > 0:
909 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
910 continue
911 #flavor not used, must be deleted
912 #delelte at VIM
913 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +0100914 for flavor_vim in c:
915 if flavor_vim["datacenter_id"] not in vims:
916 continue
917 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
918 continue
919 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200920 try:
921 myvim.delete_flavor(flavor_vim["vim_id"])
922 except vimconn.vimconnNotFoundException as e:
923 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
924 except vimconn.vimconnException as e:
925 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
926 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
927 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200928 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
929 mydb.delete_row_by_id('flavors', flavor)
930 except db_base_Exception as e:
931 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +0100932 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +0200933
tierno7edb6752016-03-21 17:37:52 +0100934
935 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +0200936 try:
937 #check if image is used by other vnf
938 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
939 if len(c) > 0:
940 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
941 continue
942 #image not used, must be deleted
943 #delelte at VIM
944 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +0100945 for image_vim in c:
946 if image_vim["datacenter_id"] not in vims:
947 continue
948 if image_vim['created']=='false': #skip this image because not created by openmano
949 continue
950 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200951 try:
952 myvim.delete_image(image_vim["vim_id"])
953 except vimconn.vimconnNotFoundException as e:
954 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
955 except vimconn.vimconnException as e:
956 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
957 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
958 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200959 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
960 mydb.delete_row_by_id('images', image)
961 except db_base_Exception as e:
962 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +0100963 undeletedItems.append("image %s" % image)
964
tiernof97fd272016-07-11 14:32:37 +0200965 return vnf_id + " " + vnf["name"]
966 #if undeletedItems:
967 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +0100968
969def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
970 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
971 if result < 0:
972 return result, vims
973 elif result == 0:
974 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
975 myvim = vims.values()[0]
976 result,servers = myvim.get_hosts_info()
977 if result < 0:
978 return result, servers
979 topology = {'name':myvim['name'] , 'servers': servers}
980 return result, topology
981
982def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +0200983 vims = get_vim(mydb, nfvo_tenant_id)
984 if len(vims) == 0:
985 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
986 elif len(vims)>1:
987 #print "nfvo.datacenter_action() error. Several datacenters found"
988 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +0100989 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +0200990 try:
991 hosts = myvim.get_hosts()
992 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +0100993
tiernof97fd272016-07-11 14:32:37 +0200994 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
995 for host in hosts:
996 server={'name':host['name'], 'vms':[]}
997 for vm in host['instances']:
998 #get internal name and model
999 try:
1000 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1001 WHERE={'vim_vm_id':vm['id']} )
1002 if len(c) == 0:
1003 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1004 continue
1005 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1006
1007 except db_base_Exception as e:
1008 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1009 datacenter['Datacenters'][0]['servers'].append(server)
1010 #return -400, "en construccion"
tierno7edb6752016-03-21 17:37:52 +01001011
tiernof97fd272016-07-11 14:32:37 +02001012 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1013 return datacenter
1014 except vimconn.vimconnException as e:
1015 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001016
1017def new_scenario(mydb, tenant_id, topo):
1018
1019# result, vims = get_vim(mydb, tenant_id)
1020# if result < 0:
1021# return result, vims
1022#1: parse input
1023 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001024 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001025 if "tenant_id" in topo:
1026 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +02001027 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1028 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001029 else:
1030 tenant_id=None
1031
1032#1.1: get VNFs and external_networks (other_nets).
1033 vnfs={}
1034 other_nets={} #external_networks, bridge_networks and data_networkds
1035 nodes = topo['topology']['nodes']
1036 for k in nodes.keys():
1037 if nodes[k]['type'] == 'VNF':
1038 vnfs[k] = nodes[k]
1039 vnfs[k]['ifaces'] = {}
1040 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1041 other_nets[k] = nodes[k]
1042 other_nets[k]['external']=True
1043 elif nodes[k]['type'] == 'network':
1044 other_nets[k] = nodes[k]
1045 other_nets[k]['external']=False
1046
1047
1048#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1049 for name,vnf in vnfs.items():
tiernocea279c2016-07-18 12:36:49 +02001050 where={}
1051 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001052 error_text = ""
1053 error_pos = "'topology':'nodes':'" + name + "'"
1054 if 'vnf_id' in vnf:
1055 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001056 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +01001057 if 'VNF model' in vnf:
1058 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +02001059 where['name'] = vnf['VNF model']
1060 if len(where) == 0:
tiernof97fd272016-07-11 14:32:37 +02001061 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
1062
tiernocea279c2016-07-18 12:36:49 +02001063 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1064 FROM='vnfs',
1065 WHERE=where,
1066 WHERE_OR=where_or,
1067 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001068 if len(vnf_db)==0:
1069 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1070 elif len(vnf_db)>1:
1071 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001072 vnf['uuid']=vnf_db[0]['uuid']
1073 vnf['description']=vnf_db[0]['description']
1074 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001075 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 +01001076 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1077 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001078 for ext_iface in ext_ifaces:
1079 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1080
1081#1.4 get list of connections
1082 conections = topo['topology']['connections']
1083 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001084 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001085 for k in conections.keys():
1086 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1087 ifaces_list = conections[k]['nodes'].items()
1088 elif type(conections[k]['nodes'])==list: #list with dictionary
1089 ifaces_list=[]
1090 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1091 for k2 in conection_pair_list:
1092 ifaces_list += k2
1093
1094 con_type = conections[k].get("type", "link")
1095 if con_type != "link":
1096 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001097 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001098 other_nets[k] = {'external': False}
1099 if conections[k].get("graph"):
1100 other_nets[k]["graph"] = conections[k]["graph"]
1101 ifaces_list.append( (k, None) )
1102
1103
1104 if con_type == "external_network":
1105 other_nets[k]['external'] = True
1106 if conections[k].get("model"):
1107 other_nets[k]["model"] = conections[k]["model"]
1108 else:
1109 other_nets[k]["model"] = k
1110 if con_type == "dataplane_net" or con_type == "bridge_net":
1111 other_nets[k]["model"] = con_type
1112
tiernoefd80c92016-09-16 14:17:46 +02001113 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001114 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)
1115 #print set(ifaces_list)
1116 #check valid VNF and iface names
1117 for iface in ifaces_list:
1118 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001119 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1120 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001121 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001122 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1123 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001124
1125#1.5 unify connections from the pair list to a consolidated list
1126 index=0
1127 while index < len(conections_list):
1128 index2 = index+1
1129 while index2 < len(conections_list):
1130 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1131 conections_list[index] |= conections_list[index2]
1132 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001133 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001134 else:
1135 index2 += 1
1136 conections_list[index] = list(conections_list[index]) # from set to list again
1137 index += 1
1138 #for k in conections_list:
1139 # print k
1140
1141
1142
1143#1.6 Delete non external nets
1144# for k in other_nets.keys():
1145# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1146# for con in conections_list:
1147# delete_indexes=[]
1148# for index in range(0,len(con)):
1149# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1150# for index in delete_indexes:
1151# del con[index]
1152# del other_nets[k]
1153#1.7: Check external_ports are present at database table datacenter_nets
1154 for k,net in other_nets.items():
1155 error_pos = "'topology':'nodes':'" + k + "'"
1156 if net['external']==False:
1157 if 'name' not in net:
1158 net['name']=k
1159 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001160 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001161 if net['model']=='bridge_net':
1162 net['type']='bridge';
1163 elif net['model']=='dataplane_net':
1164 net['type']='data';
1165 else:
tiernof97fd272016-07-11 14:32:37 +02001166 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001167 else: #external
1168#IF we do not want to check that external network exist at datacenter
1169 pass
1170#ELSE
1171# error_text = ""
1172# WHERE_={}
1173# if 'net_id' in net:
1174# error_text += " 'net_id' " + net['net_id']
1175# WHERE_['uuid'] = net['net_id']
1176# if 'model' in net:
1177# error_text += " 'model' " + net['model']
1178# WHERE_['name'] = net['model']
1179# if len(WHERE_) == 0:
1180# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1181# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1182# FROM='datacenter_nets', WHERE=WHERE_ )
1183# if r<0:
1184# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1185# elif r==0:
1186# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1187# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1188# elif r>1:
1189# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1190# return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1191# other_nets[k].update(net_db[0])
1192#ENDIF
1193 net_list={}
1194 net_nb=0 #Number of nets
1195 for con in conections_list:
1196 #check if this is connected to a external net
1197 other_net_index=-1
1198 #print
1199 #print "con", con
1200 for index in range(0,len(con)):
1201 #check if this is connected to a external net
1202 for net_key in other_nets.keys():
1203 if con[index][0]==net_key:
1204 if other_net_index>=0:
1205 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 +02001206 #print "nfvo.new_scenario " + error_text
1207 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001208 else:
1209 other_net_index = index
1210 net_target = net_key
1211 break
1212 #print "other_net_index", other_net_index
1213 try:
1214 if other_net_index>=0:
1215 del con[other_net_index]
1216#IF we do not want to check that external network exist at datacenter
1217 if other_nets[net_target]['external'] :
1218 if "name" not in other_nets[net_target]:
1219 other_nets[net_target]['name'] = other_nets[net_target]['model']
1220 if other_nets[net_target]["type"] == "external_network":
1221 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1222 other_nets[net_target]["type"] = "data"
1223 else:
1224 other_nets[net_target]["type"] = "bridge"
1225#ELSE
1226# if other_nets[net_target]['external'] :
1227# 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
1228# if type_=='data' and other_nets[net_target]['type']=="ptp":
1229# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1230# print "nfvo.new_scenario " + error_text
1231# return -HTTP_Bad_Request, error_text
1232#ENDIF
1233 for iface in con:
1234 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1235 else:
1236 #create a net
1237 net_type_bridge=False
1238 net_type_data=False
1239 net_target = "__-__net"+str(net_nb)
tiernoefd80c92016-09-16 14:17:46 +02001240 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
1241 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno7edb6752016-03-21 17:37:52 +01001242 'external':False}
1243 for iface in con:
1244 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1245 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1246 if iface_type=='mgmt' or iface_type=='bridge':
1247 net_type_bridge = True
1248 else:
1249 net_type_data = True
1250 if net_type_bridge and net_type_data:
1251 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 +02001252 #print "nfvo.new_scenario " + error_text
1253 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001254 elif net_type_bridge:
1255 type_='bridge'
1256 else:
1257 type_='data' if len(con)>2 else 'ptp'
1258 net_list[net_target]['type'] = type_
1259 net_nb+=1
1260 except Exception:
1261 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001262 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001263 #raise e
tiernof97fd272016-07-11 14:32:37 +02001264 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001265
1266#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1267 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001268 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001269 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
1270 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001271 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001272 add_mgmt_net = False
1273 for vnf in vnfs.values():
1274 for iface in vnf['ifaces'].values():
1275 if iface['type']=='mgmt' and 'net_key' not in iface:
1276 #iface not connected
1277 iface['net_key'] = 'mgmt'
1278 add_mgmt_net = True
1279 if add_mgmt_net and 'mgmt' not in net_list:
1280 net_list['mgmt']=mgmt_net[0]
1281 net_list['mgmt']['external']=True
1282 net_list['mgmt']['graph']={'visible':False}
1283
1284 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001285 #print
1286 #print 'net_list', net_list
1287 #print
1288 #print 'vnfs', vnfs
1289 #print
tierno7edb6752016-03-21 17:37:52 +01001290
1291#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001292 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001293 'tenant_id':tenant_id, 'name':topo['name'],
1294 'description':topo.get('description',topo['name']),
1295 'public': topo.get('public', False)
1296 })
tierno7edb6752016-03-21 17:37:52 +01001297
tiernof97fd272016-07-11 14:32:37 +02001298 return c
tierno7edb6752016-03-21 17:37:52 +01001299
tierno392f2852016-05-13 12:28:55 +02001300def new_scenario_v02(mydb, tenant_id, scenario_dict):
1301 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001302 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001303 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001304 if "tenant_id" in scenario:
1305 if scenario["tenant_id"] != tenant_id:
1306 print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001307 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1308 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001309 else:
1310 tenant_id=None
1311
1312#1: Check that VNF are present at database table vnfs and update content into scenario dict
1313 for name,vnf in scenario["vnfs"].iteritems():
tiernocea279c2016-07-18 12:36:49 +02001314 where={}
1315 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001316 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001317 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001318 if 'vnf_id' in vnf:
1319 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001320 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001321 if 'vnf_name' in vnf:
1322 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001323 where['name'] = vnf['vnf_name']
1324 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001325 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tiernocea279c2016-07-18 12:36:49 +02001326 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1327 FROM='vnfs',
1328 WHERE=where,
1329 WHERE_OR=where_or,
1330 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001331 if len(vnf_db)==0:
1332 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1333 elif len(vnf_db)>1:
1334 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001335 vnf['uuid']=vnf_db[0]['uuid']
1336 vnf['description']=vnf_db[0]['description']
1337 vnf['ifaces'] = {}
1338 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001339 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 +01001340 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1341 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001342 for ext_iface in ext_ifaces:
1343 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1344
1345#2: Insert net_key at every vnf interface
1346 for net_name,net in scenario["networks"].iteritems():
1347 net_type_bridge=False
1348 net_type_data=False
1349 for iface_dict in net["interfaces"]:
1350 for vnf,iface in iface_dict.iteritems():
1351 if vnf not in scenario["vnfs"]:
1352 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
tiernof97fd272016-07-11 14:32:37 +02001353 #print "nfvo.new_scenario_v02 " + error_text
1354 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001355 if iface not in scenario["vnfs"][vnf]['ifaces']:
1356 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
tiernof97fd272016-07-11 14:32:37 +02001357 #print "nfvo.new_scenario_v02 " + error_text
1358 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001359 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1360 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1361 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
tiernof97fd272016-07-11 14:32:37 +02001362 #print "nfvo.new_scenario_v02 " + error_text
1363 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001364 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1365 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1366 if iface_type=='mgmt' or iface_type=='bridge':
1367 net_type_bridge = True
1368 else:
1369 net_type_data = True
1370 if net_type_bridge and net_type_data:
1371 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
tiernof97fd272016-07-11 14:32:37 +02001372 #print "nfvo.new_scenario " + error_text
1373 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001374 elif net_type_bridge:
1375 type_='bridge'
1376 else:
1377 type_='data' if len(net["interfaces"])>2 else 'ptp'
1378 net['type'] = type_
1379 net['name'] = net_name
1380 net['external'] = net.get('external', False)
1381
1382#3: insert at database
1383 scenario["nets"] = scenario["networks"]
1384 scenario['tenant_id'] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001385 scenario_id = mydb.new_scenario( scenario)
1386 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01001387
1388def edit_scenario(mydb, tenant_id, scenario_id, data):
1389 data["uuid"] = scenario_id
1390 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001391 c = mydb.edit_scenario( data )
1392 return c
tierno7edb6752016-03-21 17:37:52 +01001393
1394def 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 +02001395 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00001396 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1397 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02001398 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01001399 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00001400
tierno7edb6752016-03-21 17:37:52 +01001401 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001402 try:
1403 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tiernof97fd272016-07-11 14:32:37 +02001404 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00001405 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02001406 scenarioDict['datacenter_id'] = datacenter_id
1407 #print '================scenarioDict======================='
1408 #print json.dumps(scenarioDict, indent=4)
1409 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno7edb6752016-03-21 17:37:52 +01001410
tiernoae4a8d12016-07-08 12:30:39 +02001411 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1412 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01001413
tiernoae4a8d12016-07-08 12:30:39 +02001414 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1415 auxNetDict['scenario'] = {}
1416
1417 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1418 for sce_net in scenarioDict['nets']:
1419 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno7edb6752016-03-21 17:37:52 +01001420
tiernoae4a8d12016-07-08 12:30:39 +02001421 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01001422 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02001423 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01001424 myNetDict = {}
1425 myNetDict["name"] = myNetName
1426 myNetDict["type"] = myNetType
1427 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001428 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01001429 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02001430 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02001431 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02001432 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02001433 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001434 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1435 sce_net['vim_id'] = network_id
1436 auxNetDict['scenario'][sce_net['uuid']] = network_id
1437 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001438 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001439 else:
1440 if sce_net['vim_id'] == None:
1441 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1442 _, message = rollback(mydb, vims, rollbackList)
1443 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02001444 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02001445 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1446 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno7edb6752016-03-21 17:37:52 +01001447
tiernoae4a8d12016-07-08 12:30:39 +02001448 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1449 #For each vnf net, we create it and we add it to instanceNetlist.
1450 for sce_vnf in scenarioDict['vnfs']:
1451 for net in sce_vnf['nets']:
1452 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
1453
1454 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1455 myNetName = myNetName[0:255] #limit length
1456 myNetType = net['type']
1457 myNetDict = {}
1458 myNetDict["name"] = myNetName
1459 myNetDict["type"] = myNetType
1460 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001461 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02001462 #print myNetDict
1463 #TODO:
1464 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02001465 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001466 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1467 net['vim_id'] = network_id
1468 if sce_vnf['uuid'] not in auxNetDict:
1469 auxNetDict[sce_vnf['uuid']] = {}
1470 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1471 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001472 net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001473
1474 #print "auxNetDict:"
1475 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
1476
1477 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1478 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1479 i = 0
1480 for sce_vnf in scenarioDict['vnfs']:
1481 for vm in sce_vnf['vms']:
1482 i += 1
1483 myVMDict = {}
1484 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01001485 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02001486 #myVMDict['description'] = vm['description']
1487 myVMDict['description'] = myVMDict['name'][0:99]
1488 if not startvms:
1489 myVMDict['start'] = "no"
1490 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1491 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
1492
1493 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001494 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
1495 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001496 vm['vim_image_id'] = image_id
1497
1498 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001499 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02001500 if flavor_dict['extended']!=None:
1501 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tiernof97fd272016-07-11 14:32:37 +02001502 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001503 vm['vim_flavor_id'] = flavor_id
1504
1505
1506 myVMDict['imageRef'] = vm['vim_image_id']
1507 myVMDict['flavorRef'] = vm['vim_flavor_id']
1508 myVMDict['networks'] = []
1509 for iface in vm['interfaces']:
1510 netDict = {}
1511 if iface['type']=="data":
1512 netDict['type'] = iface['model']
1513 elif "model" in iface and iface["model"]!=None:
1514 netDict['model']=iface['model']
1515 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1516 #discover type of interface looking at flavor
1517 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1518 for flavor_iface in numa.get('interfaces',[]):
1519 if flavor_iface.get('name') == iface['internal_name']:
1520 if flavor_iface['dedicated'] == 'yes':
1521 netDict['type']="PF" #passthrough
1522 elif flavor_iface['dedicated'] == 'no':
1523 netDict['type']="VF" #siov
1524 elif flavor_iface['dedicated'] == 'yes:sriov':
1525 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1526 netDict["mac_address"] = flavor_iface.get("mac_address")
1527 break;
1528 netDict["use"]=iface['type']
1529 if netDict["use"]=="data" and not netDict.get("type"):
1530 #print "netDict", netDict
1531 #print "iface", iface
1532 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'])
1533 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02001534 raise NfvoException(e_text + "After database migration some information is not available. \
1535 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02001536 else:
tiernof97fd272016-07-11 14:32:37 +02001537 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02001538 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1539 netDict["type"]="virtual"
1540 if "vpci" in iface and iface["vpci"] is not None:
1541 netDict['vpci'] = iface['vpci']
1542 if "mac" in iface and iface["mac"] is not None:
1543 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00001544 if "port-security" in iface and iface["port-security"] is not None:
1545 netDict['port_security'] = iface['port-security']
1546 if "floating-ip" in iface and iface["floating-ip"] is not None:
1547 netDict['floating_ip'] = iface['floating-ip']
tiernoae4a8d12016-07-08 12:30:39 +02001548 netDict['name'] = iface['internal_name']
1549 if iface['net_id'] is None:
1550 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02001551 #print iface
1552 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02001553 if vnf_iface['interface_id']==iface['uuid']:
1554 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1555 break
1556 else:
1557 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1558 #skip bridge ifaces not connected to any net
1559 #if 'net_id' not in netDict or netDict['net_id']==None:
1560 # continue
1561 myVMDict['networks'].append(netDict)
1562 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1563 #print myVMDict['name']
1564 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1565 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1566 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1567 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1568 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1569 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1570 vm['vim_id'] = vm_id
1571 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1572 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1573 for net in myVMDict['networks']:
1574 if "vim_id" in net:
1575 for iface in vm['interfaces']:
1576 if net["name"]==iface["internal_name"]:
1577 iface["vim_id"]=net["vim_id"]
1578 break
1579
1580 logger.debug("start scenario Deployment done")
1581 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1582 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02001583 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1584 return mydb.get_instance_scenario(instance_id)
1585
1586 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001587 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02001588 if isinstance(e, db_base_Exception):
1589 error_text = "Exception at database"
1590 else:
1591 error_text = "Exception at VIM"
1592 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1593 #logger.error("start_scenario %s", error_text)
1594 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001595
tierno36c0b172017-01-12 18:32:28 +01001596def unify_cloud_config(cloud_config_preserve, cloud_config):
1597 ''' join the cloud config information into cloud_config_preserve.
1598 In case of conflict cloud_config_preserve preserves
1599 None is admited
1600 '''
1601 if not cloud_config_preserve and not cloud_config:
1602 return None
1603
1604 new_cloud_config = {"key-pairs":[], "users":[]}
1605 # key-pairs
1606 if cloud_config_preserve:
1607 for key in cloud_config_preserve.get("key-pairs", () ):
1608 if key not in new_cloud_config["key-pairs"]:
1609 new_cloud_config["key-pairs"].append(key)
1610 if cloud_config:
1611 for key in cloud_config.get("key-pairs", () ):
1612 if key not in new_cloud_config["key-pairs"]:
1613 new_cloud_config["key-pairs"].append(key)
1614 if not new_cloud_config["key-pairs"]:
1615 del new_cloud_config["key-pairs"]
1616
1617 # users
1618 if cloud_config:
1619 new_cloud_config["users"] += cloud_config.get("users", () )
1620 if cloud_config_preserve:
1621 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
tiernoa4e1a6e2016-08-31 14:19:40 +02001622 index_to_delete = []
tierno36c0b172017-01-12 18:32:28 +01001623 users = new_cloud_config.get("users", [])
tiernoa4e1a6e2016-08-31 14:19:40 +02001624 for index0 in range(0,len(users)):
1625 if index0 in index_to_delete:
1626 continue
1627 for index1 in range(index0+1,len(users)):
1628 if index1 in index_to_delete:
1629 continue
1630 if users[index0]["name"] == users[index1]["name"]:
1631 index_to_delete.append(index1)
1632 for key in users[index1].get("key-pairs",()):
tierno36c0b172017-01-12 18:32:28 +01001633 if "key-pairs" not in users[index0]:
tiernoa4e1a6e2016-08-31 14:19:40 +02001634 users[index0]["key-pairs"] = [key]
1635 elif key not in users[index0]["key-pairs"]:
1636 users[index0]["key-pairs"].append(key)
1637 index_to_delete.sort(reverse=True)
1638 for index in index_to_delete:
1639 del users[index]
tierno36c0b172017-01-12 18:32:28 +01001640 if not new_cloud_config["users"]:
1641 del new_cloud_config["users"]
1642
1643 #boot-data-drive
1644 if cloud_config and cloud_config.get("boot-data-drive") != None:
1645 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
1646 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
1647 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
1648
1649 # user-data
1650 if cloud_config and cloud_config.get("user-data") != None:
1651 new_cloud_config["user-data"] = cloud_config["user-data"]
1652 if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
1653 new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
1654
1655 # config files
1656 new_cloud_config["config-files"] = []
1657 if cloud_config and cloud_config.get("config-files") != None:
1658 new_cloud_config["config-files"] += cloud_config["config-files"]
1659 if cloud_config_preserve:
1660 for file in cloud_config_preserve.get("config-files", ()):
1661 for index in range(0, len(new_cloud_config["config-files"])):
1662 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
1663 new_cloud_config["config-files"][index] = file
1664 break
1665 else:
1666 new_cloud_config["config-files"].append(file)
1667 if not new_cloud_config["config-files"]:
1668 del new_cloud_config["config-files"]
1669 return new_cloud_config
1670
1671
tiernoa4e1a6e2016-08-31 14:19:40 +02001672
tiernoa2793912016-10-04 08:15:08 +00001673def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02001674 datacenter_id = None
1675 datacenter_name = None
1676 if datacenter_id_name:
1677 if utils.check_valid_uuid(datacenter_id_name):
1678 datacenter_id = datacenter_id_name
1679 else:
1680 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00001681 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02001682 if len(vims) == 0:
1683 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1684 elif len(vims)>1:
1685 #print "nfvo.datacenter_action() error. Several datacenters found"
1686 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1687 return vims.keys()[0], vims.values()[0]
1688
garciadeblas9f8456e2016-09-05 05:02:59 +02001689def new_scenario_v03(mydb, tenant_id, scenario_dict):
1690 scenario = scenario_dict["scenario"]
1691 if tenant_id != "any":
1692 check_tenant(mydb, tenant_id)
1693 if "tenant_id" in scenario:
1694 if scenario["tenant_id"] != tenant_id:
1695 logger("Tenant '%s' not found", tenant_id)
1696 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1697 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
1698 else:
1699 tenant_id=None
1700
1701#1: Check that VNF are present at database table vnfs and update content into scenario dict
1702 for name,vnf in scenario["vnfs"].iteritems():
1703 where={}
1704 where_or={"tenant_id": tenant_id, 'public': "true"}
1705 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001706 error_pos = "'scenario':'vnfs':'" + name + "'"
garciadeblas9f8456e2016-09-05 05:02:59 +02001707 if 'vnf_id' in vnf:
1708 error_text += " 'vnf_id' " + vnf['vnf_id']
1709 where['uuid'] = vnf['vnf_id']
1710 if 'vnf_name' in vnf:
1711 error_text += " 'vnf_name' " + vnf['vnf_name']
1712 where['name'] = vnf['vnf_name']
1713 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001714 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
garciadeblas9f8456e2016-09-05 05:02:59 +02001715 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1716 FROM='vnfs',
1717 WHERE=where,
1718 WHERE_OR=where_or,
1719 WHERE_AND_OR="AND")
1720 if len(vnf_db)==0:
1721 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1722 elif len(vnf_db)>1:
1723 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1724 vnf['uuid']=vnf_db[0]['uuid']
1725 vnf['description']=vnf_db[0]['description']
1726 vnf['ifaces'] = {}
1727 # get external interfaces
1728 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1729 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1730 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
1731 for ext_iface in ext_ifaces:
1732 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1733
1734 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1735
1736#2: Insert net_key and ip_address at every vnf interface
1737 for net_name,net in scenario["networks"].iteritems():
1738 net_type_bridge=False
1739 net_type_data=False
1740 for iface_dict in net["interfaces"]:
1741 logger.debug("Iface_dict %s", iface_dict)
1742 vnf = iface_dict["vnf"]
1743 iface = iface_dict["vnf_interface"]
1744 if vnf not in scenario["vnfs"]:
1745 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
1746 #logger.debug(error_text)
1747 raise NfvoException(error_text, HTTP_Not_Found)
1748 if iface not in scenario["vnfs"][vnf]['ifaces']:
1749 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
1750 #logger.debug(error_text)
1751 raise NfvoException(error_text, HTTP_Bad_Request)
1752 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1753 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1754 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1755 #logger.debug(error_text)
1756 raise NfvoException(error_text, HTTP_Bad_Request)
1757 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1758 scenario["vnfs"][vnf]['ifaces'][ iface ]['ip_address'] = iface_dict.get('ip_address',None)
1759 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1760 if iface_type=='mgmt' or iface_type=='bridge':
1761 net_type_bridge = True
1762 else:
1763 net_type_data = True
1764 if net_type_bridge and net_type_data:
1765 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
1766 #logger.debug(error_text)
1767 raise NfvoException(error_text, HTTP_Bad_Request)
1768 elif net_type_bridge:
1769 type_='bridge'
1770 else:
1771 type_='data' if len(net["interfaces"])>2 else 'ptp'
1772
1773 if ("implementation" in net):
1774 if (type_ == "bridge" and net["implementation"] == "underlay"):
1775 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at 'network':'%s'" % (net_name)
1776 #logger.debug(error_text)
1777 raise NfvoException(error_text, HTTP_Bad_Request)
1778 elif (type_ <> "bridge" and net["implementation"] == "overlay"):
1779 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at 'network':'%s'" % (net_name)
1780 #logger.debug(error_text)
1781 raise NfvoException(error_text, HTTP_Bad_Request)
1782 net.pop("implementation")
1783 if ("type" in net):
1784 if (type_ == "data" and net["type"] == "e-line"):
1785 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type 'e-line' at 'network':'%s'" % (net_name)
1786 #logger.debug(error_text)
1787 raise NfvoException(error_text, HTTP_Bad_Request)
1788 elif (type_ == "ptp" and net["type"] == "e-lan"):
1789 type_ = "data"
1790
1791 net['type'] = type_
1792 net['name'] = net_name
1793 net['external'] = net.get('external', False)
1794
1795#3: insert at database
1796 scenario["nets"] = scenario["networks"]
1797 scenario['tenant_id'] = tenant_id
1798 scenario_id = mydb.new_scenario2(scenario)
1799 return scenario_id
1800
1801def update(d, u):
1802 '''Takes dict d and updates it with the values in dict u.'''
1803 '''It merges all depth levels'''
1804 for k, v in u.iteritems():
1805 if isinstance(v, collections.Mapping):
1806 r = update(d.get(k, {}), v)
1807 d[k] = r
1808 else:
1809 d[k] = u[k]
1810 return d
1811
tierno7edb6752016-03-21 17:37:52 +01001812def create_instance(mydb, tenant_id, instance_dict):
tiernoae4a8d12016-07-08 12:30:39 +02001813 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno4319dad2016-09-05 12:11:11 +02001814 #logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01001815 scenario = instance_dict["scenario"]
tiernobe41e222016-09-02 15:16:13 +02001816
1817 #find main datacenter
1818 myvims = {}
tiernoa2793912016-10-04 08:15:08 +00001819 datacenter2tenant = {}
tierno7edb6752016-03-21 17:37:52 +01001820 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02001821 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1822 myvims[default_datacenter_id] = vim
tiernoa2793912016-10-04 08:15:08 +00001823 datacenter2tenant[default_datacenter_id] = vim['config']['datacenter_tenant_id']
tierno392f2852016-05-13 12:28:55 +02001824 #myvim_tenant = myvim['tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001825# default_datacenter_name = vim['name']
tierno7edb6752016-03-21 17:37:52 +01001826 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001827
1828 #print "Checking that the scenario exists and getting the scenario dictionary"
tiernobe41e222016-09-02 15:16:13 +02001829 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001830
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001831 #logger.debug(">>>>>>> Dictionaries before merging")
1832 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1833 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
garciadeblas9f8456e2016-09-05 05:02:59 +02001834
tiernobe41e222016-09-02 15:16:13 +02001835 scenarioDict['datacenter_id'] = default_datacenter_id
garciadeblas9f8456e2016-09-05 05:02:59 +02001836
tierno7edb6752016-03-21 17:37:52 +01001837 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1838 auxNetDict['scenario'] = {}
1839
tierno4319dad2016-09-05 12:11:11 +02001840 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 +01001841 instance_name = instance_dict["name"]
1842 instance_description = instance_dict.get("description")
1843 try:
1844 #0 check correct parameters
tiernobe41e222016-09-02 15:16:13 +02001845 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001846 found=False
1847 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001848 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01001849 found = True
1850 break
1851 if not found:
tiernobe41e222016-09-02 15:16:13 +02001852 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
1853 if "sites" not in net_instance_desc:
1854 net_instance_desc["sites"] = [ {} ]
1855 site_without_datacenter_field = False
1856 for site in net_instance_desc["sites"]:
1857 if site.get("datacenter"):
1858 if site["datacenter"] not in myvims:
1859 #Add this datacenter to myvims
1860 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
1861 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001862 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001863 site["datacenter"] = d #change name to id
1864 else:
1865 if site_without_datacenter_field:
1866 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
1867 site_without_datacenter_field = True
1868 site["datacenter"] = default_datacenter_id #change name to id
1869
1870 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001871 found=False
1872 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02001873 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01001874 found = True
1875 break
1876 if not found:
tiernobe41e222016-09-02 15:16:13 +02001877 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
1878 if "datacenter" in vnf_instance_desc:
1879 #Add this datacenter to myvims
1880 if vnf_instance_desc["datacenter"] not in myvims:
1881 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
1882 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001883 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
1884 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
garciadeblas30833382017-01-09 09:46:31 +01001885
tiernoa4e1a6e2016-08-31 14:19:40 +02001886 #0.1 parse cloud-config parameters
tierno36c0b172017-01-12 18:32:28 +01001887 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
garciadeblas9f8456e2016-09-05 05:02:59 +02001888
1889 #0.2 merge instance information into scenario
1890 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
1891 #However, this is not possible yet.
1892 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
1893 for scenario_net in scenarioDict['nets']:
1894 if net_name == scenario_net["name"]:
1895 if 'ip-profile' in net_instance_desc:
1896 ipprofile = net_instance_desc['ip-profile']
1897 ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None)
1898 ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4')
1899 ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None)
1900 ipprofile['dns_address'] = ipprofile.pop('dns-address',None)
1901 if 'dhcp' in ipprofile:
1902 ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None)
1903 ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True)
1904 ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None)
1905 del ipprofile['dhcp']
garciadeblasedca7b32016-09-29 14:01:52 +00001906 if 'ip_profile' not in scenario_net:
1907 scenario_net['ip_profile'] = ipprofile
1908 else:
1909 update(scenario_net['ip_profile'],ipprofile)
tiernoe6c58ce2016-09-14 16:02:49 +02001910 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02001911 if 'ip_address' in interface:
1912 for vnf in scenarioDict['vnfs']:
1913 if interface['vnf'] == vnf['name']:
1914 for vnf_interface in vnf['interfaces']:
1915 if interface['vnf_interface'] == vnf_interface['external_name']:
1916 vnf_interface['ip_address']=interface['ip_address']
1917
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001918 #logger.debug(">>>>>>>> Merged dictionary")
tierno4319dad2016-09-05 12:11:11 +02001919 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 +02001920
tierno7edb6752016-03-21 17:37:52 +01001921
1922 #1. Creating new nets (sce_nets) in the VIM"
1923 for sce_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001924 sce_net["vim_id_sites"]={}
tierno7edb6752016-03-21 17:37:52 +01001925 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
tiernobe41e222016-09-02 15:16:13 +02001926 net_name = descriptor_net.get("vim-network-name")
1927 auxNetDict['scenario'][sce_net['uuid']] = {}
1928
1929 sites = descriptor_net.get("sites", [ {} ])
1930 for site in sites:
1931 if site.get("datacenter"):
1932 vim = myvims[ site["datacenter"] ]
1933 datacenter_id = site["datacenter"]
tierno7edb6752016-03-21 17:37:52 +01001934 else:
tiernobe41e222016-09-02 15:16:13 +02001935 vim = myvims[ default_datacenter_id ]
1936 datacenter_id = default_datacenter_id
tiernobe41e222016-09-02 15:16:13 +02001937 net_type = sce_net['type']
1938 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
1939 if sce_net["external"]:
1940 if not net_name:
1941 net_name = sce_net["name"]
1942 if "netmap-use" in site or "netmap-create" in site:
1943 create_network = False
1944 lookfor_network = False
1945 if "netmap-use" in site:
1946 lookfor_network = True
1947 if utils.check_valid_uuid(site["netmap-use"]):
1948 filter_text = "scenario id '%s'" % site["netmap-use"]
1949 lookfor_filter["id"] = site["netmap-use"]
1950 else:
1951 filter_text = "scenario name '%s'" % site["netmap-use"]
1952 lookfor_filter["name"] = site["netmap-use"]
1953 if "netmap-create" in site:
1954 create_network = True
1955 net_vim_name = net_name
1956 if site["netmap-create"]:
1957 net_vim_name = site["netmap-create"]
1958
1959 elif sce_net['vim_id'] != None:
1960 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
1961 create_network = False
1962 lookfor_network = True
1963 lookfor_filter["id"] = sce_net['vim_id']
1964 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
1965 #look for network at datacenter and return error
1966 else:
1967 #There is not a netmap, look at datacenter for a net with this name and create if not found
1968 create_network = True
1969 lookfor_network = True
1970 lookfor_filter["name"] = sce_net["name"]
1971 net_vim_name = sce_net["name"]
1972 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01001973 else:
tiernobe41e222016-09-02 15:16:13 +02001974 if not net_name:
1975 net_name = "%s.%s" %(instance_name, sce_net["name"])
1976 net_name = net_name[:255] #limit length
1977 net_vim_name = net_name
1978 create_network = True
1979 lookfor_network = False
1980
1981 if lookfor_network:
1982 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
1983 if len(vim_nets) > 1:
1984 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1985 elif len(vim_nets) == 0:
1986 if not create_network:
1987 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1988 else:
1989 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
tiernobe41e222016-09-02 15:16:13 +02001990 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
1991 create_network = False
1992 if create_network:
1993 #if network is not external
garciadeblas9f8456e2016-09-05 05:02:59 +02001994 network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
tiernobe41e222016-09-02 15:16:13 +02001995 sce_net["vim_id_sites"][datacenter_id] = network_id
1996 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = network_id
1997 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001998 sce_net["created"] = True
tierno7edb6752016-03-21 17:37:52 +01001999
2000 #2. Creating new nets (vnf internal nets) in the VIM"
2001 #For each vnf net, we create it and we add it to instanceNetlist.
2002 for sce_vnf in scenarioDict['vnfs']:
2003 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02002004 if sce_vnf.get("datacenter"):
2005 vim = myvims[ sce_vnf["datacenter"] ]
2006 datacenter_id = sce_vnf["datacenter"]
2007 else:
2008 vim = myvims[ default_datacenter_id ]
2009 datacenter_id = default_datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002010 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
2011 net_name = descriptor_net.get("name")
2012 if not net_name:
2013 net_name = "%s.%s" %(instance_name, net["name"])
2014 net_name = net_name[:255] #limit length
2015 net_type = net['type']
garciadeblas9f8456e2016-09-05 05:02:59 +02002016 network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
tierno7edb6752016-03-21 17:37:52 +01002017 net['vim_id'] = network_id
2018 if sce_vnf['uuid'] not in auxNetDict:
2019 auxNetDict[sce_vnf['uuid']] = {}
2020 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2021 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02002022 net["created"] = True
2023
tierno7edb6752016-03-21 17:37:52 +01002024
tiernoae4a8d12016-07-08 12:30:39 +02002025 #print "auxNetDict:"
2026 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002027
2028 #3. Creating new vm instances in the VIM
tiernoae4a8d12016-07-08 12:30:39 +02002029 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
tierno7edb6752016-03-21 17:37:52 +01002030 for sce_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02002031 if sce_vnf.get("datacenter"):
2032 vim = myvims[ sce_vnf["datacenter"] ]
2033 datacenter_id = sce_vnf["datacenter"]
2034 else:
2035 vim = myvims[ default_datacenter_id ]
2036 datacenter_id = default_datacenter_id
2037 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002038 i = 0
2039 for vm in sce_vnf['vms']:
2040 i += 1
2041 myVMDict = {}
tiernoae65a482016-11-24 16:20:05 +01002042 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
tierno7edb6752016-03-21 17:37:52 +01002043 myVMDict['description'] = myVMDict['name'][0:99]
2044# if not startvms:
2045# myVMDict['start'] = "no"
2046 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2047 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002048 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00002049 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01002050 vm['vim_image_id'] = image_id
2051
2052 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02002053 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01002054 if flavor_dict['extended']!=None:
2055 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00002056 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2057
2058
2059
2060
2061 #Obtain information for additional disks
2062 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2063 if not extended_flavor_dict:
2064 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2065 return
2066
2067 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2068 myVMDict['disks'] = None
2069 extended_info = extended_flavor_dict[0]['extended']
2070 if extended_info != None:
2071 extended_flavor_dict_yaml = yaml.load(extended_info)
2072 if 'disks' in extended_flavor_dict_yaml:
2073 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2074
2075
2076
2077
tierno7edb6752016-03-21 17:37:52 +01002078 vm['vim_flavor_id'] = flavor_id
2079
2080 myVMDict['imageRef'] = vm['vim_image_id']
2081 myVMDict['flavorRef'] = vm['vim_flavor_id']
2082 myVMDict['networks'] = []
tiernoa2793912016-10-04 08:15:08 +00002083 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno7edb6752016-03-21 17:37:52 +01002084 for iface in vm['interfaces']:
2085 netDict = {}
2086 if iface['type']=="data":
2087 netDict['type'] = iface['model']
2088 elif "model" in iface and iface["model"]!=None:
2089 netDict['model']=iface['model']
2090 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2091 #discover type of interface looking at flavor
2092 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2093 for flavor_iface in numa.get('interfaces',[]):
2094 if flavor_iface.get('name') == iface['internal_name']:
2095 if flavor_iface['dedicated'] == 'yes':
2096 netDict['type']="PF" #passthrough
2097 elif flavor_iface['dedicated'] == 'no':
2098 netDict['type']="VF" #siov
2099 elif flavor_iface['dedicated'] == 'yes:sriov':
2100 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2101 netDict["mac_address"] = flavor_iface.get("mac_address")
2102 break;
2103 netDict["use"]=iface['type']
2104 if netDict["use"]=="data" and not netDict.get("type"):
2105 #print "netDict", netDict
2106 #print "iface", iface
2107 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'])
2108 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02002109 raise NfvoException(e_text + "After database migration some information is not available. \
2110 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002111 else:
tiernoae4a8d12016-07-08 12:30:39 +02002112 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002113 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2114 netDict["type"]="virtual"
2115 if "vpci" in iface and iface["vpci"] is not None:
2116 netDict['vpci'] = iface['vpci']
2117 if "mac" in iface and iface["mac"] is not None:
2118 netDict['mac_address'] = iface['mac']
montesmoreno2a1fc4e2017-01-09 16:46:04 +00002119 if "port-security" in iface and iface["port-security"] is not None:
2120 netDict['port_security'] = iface['port-security']
2121 if "floating-ip" in iface and iface["floating-ip"] is not None:
2122 netDict['floating_ip'] = iface['floating-ip']
tierno7edb6752016-03-21 17:37:52 +01002123 netDict['name'] = iface['internal_name']
2124 if iface['net_id'] is None:
2125 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002126 #print iface
2127 #print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01002128 if vnf_iface['interface_id']==iface['uuid']:
tiernobe41e222016-09-02 15:16:13 +02002129 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002130 break
2131 else:
2132 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2133 #skip bridge ifaces not connected to any net
2134 #if 'net_id' not in netDict or netDict['net_id']==None:
2135 # continue
2136 myVMDict['networks'].append(netDict)
tiernoae4a8d12016-07-08 12:30:39 +02002137 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2138 #print myVMDict['name']
2139 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2140 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2141 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tierno36c0b172017-01-12 18:32:28 +01002142 if vm.get("boot_data"):
2143 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
2144 else:
2145 cloud_config_vm = cloud_config
tiernobe41e222016-09-02 15:16:13 +02002146 vm_id = vim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
tierno36c0b172017-01-12 18:32:28 +01002147 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'], cloud_config = cloud_config_vm,
montesmoreno0c8def02016-12-22 12:16:23 +00002148 disk_list = myVMDict['disks'])
2149
tierno7edb6752016-03-21 17:37:52 +01002150 vm['vim_id'] = vm_id
2151 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2152 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2153 for net in myVMDict['networks']:
2154 if "vim_id" in net:
2155 for iface in vm['interfaces']:
2156 if net["name"]==iface["internal_name"]:
2157 iface["vim_id"]=net["vim_id"]
2158 break
tiernoa2793912016-10-04 08:15:08 +00002159 scenarioDict["datacenter2tenant"] = datacenter2tenant
2160 logger.debug("create_instance Deployment done scenarioDict: %s",
2161 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
tiernof97fd272016-07-11 14:32:37 +02002162 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
2163 return mydb.get_instance_scenario(instance_id)
2164 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02002165 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002166 if isinstance(e, db_base_Exception):
2167 error_text = "database Exception"
2168 elif isinstance(e, vimconn.vimconnException):
2169 error_text = "VIM Exception"
2170 else:
2171 error_text = "Exception"
2172 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2173 #logger.error("create_instance: %s", error_text)
2174 raise NfvoException(error_text, e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002175
tierno7edb6752016-03-21 17:37:52 +01002176def delete_instance(mydb, tenant_id, instance_id):
tiernoae4a8d12016-07-08 12:30:39 +02002177 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002178 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02002179 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002180 tenant_id = instanceDict["tenant_id"]
tiernoae4a8d12016-07-08 12:30:39 +02002181 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01002182
tiernoa2793912016-10-04 08:15:08 +00002183 #1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02002184 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002185
2186 #2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00002187 error_msg = ""
2188 myvims={}
tierno7edb6752016-03-21 17:37:52 +01002189
2190 #2.1 deleting VMs
2191 #vm_fail_list=[]
2192 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002193 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2194 if datacenter_key not in myvims:
2195 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2196 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2197 if len(vims) == 0:
2198 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2199 sce_vnf["datacenter_tenant_id"]))
2200 myvims[datacenter_key] = None
2201 else:
2202 myvims[datacenter_key] = vims.values()[0]
2203 myvim = myvims[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01002204 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002205 if not myvim:
2206 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2207 continue
tiernoae4a8d12016-07-08 12:30:39 +02002208 try:
2209 myvim.delete_vminstance(vm['vim_vm_id'])
2210 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002211 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 +02002212 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2213 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2214 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002215 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2216 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
tiernoae4a8d12016-07-08 12:30:39 +02002217 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
tierno7edb6752016-03-21 17:37:52 +01002218
2219 #2.2 deleting NETS
2220 #net_fail_list=[]
2221 for net in instanceDict['nets']:
tierno66345bc2016-09-26 11:37:55 +02002222 if not net['created']:
tierno7edb6752016-03-21 17:37:52 +01002223 continue #skip not created nets
tiernoa2793912016-10-04 08:15:08 +00002224 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2225 if datacenter_key not in myvims:
2226 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2227 datacenter_tenant_id=net["datacenter_tenant_id"])
2228 if len(vims) == 0:
2229 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2230 myvims[datacenter_key] = None
2231 else:
2232 myvims[datacenter_key] = vims.values()[0]
2233 myvim = myvims[datacenter_key]
2234
tierno7edb6752016-03-21 17:37:52 +01002235 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00002236 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 +01002237 continue
tiernoae4a8d12016-07-08 12:30:39 +02002238 try:
2239 myvim.delete_network(net['vim_net_id'])
2240 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002241 error_msg+="\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
2242 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
2243 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
tiernoae4a8d12016-07-08 12:30:39 +02002244 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002245 error_msg+="\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'], net["datacenter_id"], e.http_code, str(e))
2246 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
2247 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
tierno7edb6752016-03-21 17:37:52 +01002248 if len(error_msg)>0:
tiernof97fd272016-07-11 14:32:37 +02002249 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 +01002250 else:
tiernof97fd272016-07-11 14:32:37 +02002251 return 'instance ' + message + ' deleted'
tierno7edb6752016-03-21 17:37:52 +01002252
2253def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2254 '''Refreshes a scenario instance. It modifies instanceDict'''
2255 '''Returns:
2256 - 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
2257 - error_msg
2258 '''
2259 # Assumption: nfvo_tenant and instance_id were checked before entering into this function
tiernoae4a8d12016-07-08 12:30:39 +02002260 #print "nfvo.refresh_instance begins"
tierno7edb6752016-03-21 17:37:52 +01002261 #print json.dumps(instanceDict, indent=4)
2262
tiernoae4a8d12016-07-08 12:30:39 +02002263 #print "Getting the VIM URL and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002264 myvims={}
2265
tiernoae4a8d12016-07-08 12:30:39 +02002266 # 1. Getting VIM vm and net list
tierno7edb6752016-03-21 17:37:52 +01002267 vms_updated = [] #List of VM instance uuids in openmano that were updated
2268 vms_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002269 vm_list = {}
tierno7edb6752016-03-21 17:37:52 +01002270 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002271 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2272 if datacenter_key not in vm_list:
2273 vm_list[datacenter_key] = []
2274 if datacenter_key not in myvims:
2275 vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2276 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2277 if len(vims) == 0:
2278 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2279 myvims[datacenter_key] = None
2280 else:
2281 myvims[datacenter_key] = vims.values()[0]
tierno7edb6752016-03-21 17:37:52 +01002282 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002283 vm_list[datacenter_key].append(vm['vim_vm_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002284 vms_notupdated.append(vm["uuid"])
2285
2286 nets_updated = [] #List of VM instance uuids in openmano that were updated
tierno7edb6752016-03-21 17:37:52 +01002287 nets_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002288 net_list = {}
tierno7edb6752016-03-21 17:37:52 +01002289 for net in instanceDict['nets']:
tiernoa2793912016-10-04 08:15:08 +00002290 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2291 if datacenter_key not in net_list:
2292 net_list[datacenter_key] = []
2293 if datacenter_key not in myvims:
2294 vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2295 datacenter_tenant_id=net["datacenter_tenant_id"])
2296 if len(vims) == 0:
2297 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2298 myvims[datacenter_key] = None
2299 else:
2300 myvims[datacenter_key] = vims.values()[0]
2301
2302 net_list[datacenter_key].append(net['vim_net_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002303 nets_notupdated.append(net["uuid"])
2304
tiernoa2793912016-10-04 08:15:08 +00002305 # 1. Getting the status of all VMs
2306 vm_dict={}
2307 for datacenter_key in myvims:
2308 if not vm_list.get(datacenter_key):
2309 continue
2310 failed = True
2311 failed_message=""
2312 if not myvims[datacenter_key]:
2313 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2314 else:
2315 try:
2316 vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2317 failed = False
2318 except vimconn.vimconnException as e:
2319 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2320 failed_message = str(e)
2321 if failed:
2322 for vm in vm_list[datacenter_key]:
2323 vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
tiernoae4a8d12016-07-08 12:30:39 +02002324
tiernoa2793912016-10-04 08:15:08 +00002325 # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2326 for sce_vnf in instanceDict['vnfs']:
2327 for vm in sce_vnf['vms']:
2328 vm_id = vm['vim_vm_id']
2329 interfaces = vm_dict[vm_id].pop('interfaces', [])
2330 #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2331 has_mgmt_iface = False
2332 for iface in vm["interfaces"]:
2333 if iface["type"]=="mgmt":
2334 has_mgmt_iface = True
2335 if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2336 vm_dict[vm_id]['status'] = "ACTIVE"
tiernoa3d49e62016-10-05 15:20:26 +00002337 if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2338 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 +00002339 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'):
2340 vm['status'] = vm_dict[vm_id]['status']
2341 vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2342 vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2343 # 2.1. Update in openmano DB the VMs whose status changed
tiernof97fd272016-07-11 14:32:37 +02002344 try:
tiernoa2793912016-10-04 08:15:08 +00002345 updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2346 vms_notupdated.remove(vm["uuid"])
2347 if updates>0:
2348 vms_updated.append(vm["uuid"])
tiernof97fd272016-07-11 14:32:37 +02002349 except db_base_Exception as e:
2350 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tiernoa2793912016-10-04 08:15:08 +00002351 # 2.2. Update in openmano DB the interface VMs
2352 for interface in interfaces:
2353 #translate from vim_net_id to instance_net_id
2354 network_id_list=[]
2355 for net in instanceDict['nets']:
2356 if net["vim_net_id"] == interface["vim_net_id"]:
2357 network_id_list.append(net["uuid"])
2358 if not network_id_list:
2359 continue
2360 del interface["vim_net_id"]
2361 try:
2362 for network_id in network_id_list:
2363 mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2364 except db_base_Exception as e:
2365 logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2366
2367 # 3. Getting the status of all nets
2368 net_dict = {}
2369 for datacenter_key in myvims:
2370 if not net_list.get(datacenter_key):
2371 continue
2372 failed = True
2373 failed_message = ""
2374 if not myvims[datacenter_key]:
2375 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2376 else:
2377 try:
2378 net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2379 failed = False
2380 except vimconn.vimconnException as e:
2381 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2382 failed_message = str(e)
2383 if failed:
2384 for net in net_list[datacenter_key]:
2385 net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2386
2387 # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2388 # TODO: update nets inside a vnf
2389 for net in instanceDict['nets']:
2390 net_id = net['vim_net_id']
tiernoa3d49e62016-10-05 15:20:26 +00002391 if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2392 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 +00002393 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'):
2394 net['status'] = net_dict[net_id]['status']
2395 net['error_msg'] = net_dict[net_id].get('error_msg')
2396 net['vim_info'] = net_dict[net_id].get('vim_info')
2397 # 5.1. Update in openmano DB the nets whose status changed
2398 try:
2399 updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2400 nets_notupdated.remove(net["uuid"])
2401 if updated>0:
2402 nets_updated.append(net["uuid"])
2403 except db_base_Exception as e:
2404 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tierno7edb6752016-03-21 17:37:52 +01002405
2406 # Returns appropriate output
tiernoae4a8d12016-07-08 12:30:39 +02002407 #print "nfvo.refresh_instance finishes"
2408 logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2409 str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01002410 instance_id = instanceDict['uuid']
tierno7edb6752016-03-21 17:37:52 +01002411 if len(vms_notupdated)+len(nets_notupdated)>0:
tiernoae4a8d12016-07-08 12:30:39 +02002412 error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
tierno7edb6752016-03-21 17:37:52 +01002413 return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
2414
tiernoae4a8d12016-07-08 12:30:39 +02002415 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01002416
2417def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02002418 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002419 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002420 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2421
tiernoae4a8d12016-07-08 12:30:39 +02002422 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02002423 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2424 if len(vims) == 0:
2425 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002426 myvim = vims.values()[0]
2427
2428
2429 input_vnfs = action_dict.pop("vnfs", [])
2430 input_vms = action_dict.pop("vms", [])
2431 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2432 vm_result = {}
2433 vm_error = 0
2434 vm_ok = 0
2435 for sce_vnf in instanceDict['vnfs']:
2436 for vm in sce_vnf['vms']:
2437 if not action_over_all:
2438 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2439 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2440 continue
tiernoae4a8d12016-07-08 12:30:39 +02002441 try:
2442 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
tierno7edb6752016-03-21 17:37:52 +01002443 if "console" in action_dict:
tierno20fc2a22016-08-19 17:02:35 +02002444 if not global_config["http_console_proxy"]:
2445 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2446 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2447 protocol=data["protocol"],
2448 ip = data["server"],
2449 port = data["port"],
2450 suffix = data["suffix"]),
2451 "name":vm['name']
2452 }
2453 vm_ok +=1
2454 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
tierno7edb6752016-03-21 17:37:52 +01002455 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2456 "description": "this console is only reachable by local interface",
2457 "name":vm['name']
2458 }
2459 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02002460 else:
tierno7edb6752016-03-21 17:37:52 +01002461 #print "console data", data
tierno20fc2a22016-08-19 17:02:35 +02002462 try:
2463 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2464 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2465 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2466 protocol=data["protocol"],
2467 ip = global_config["http_console_host"],
2468 port = console_thread.port,
2469 suffix = data["suffix"]),
2470 "name":vm['name']
2471 }
2472 vm_ok +=1
2473 except NfvoException as e:
2474 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2475 vm_error+=1
2476
tierno7edb6752016-03-21 17:37:52 +01002477 else:
tiernof97fd272016-07-11 14:32:37 +02002478 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
tierno7edb6752016-03-21 17:37:52 +01002479 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02002480 except vimconn.vimconnException as e:
2481 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2482 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01002483
2484 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02002485 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002486 else:
tierno351863c2016-07-23 01:46:03 +02002487 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002488
2489def create_or_use_console_proxy_thread(console_server, console_port):
2490 #look for a non-used port
2491 console_thread_key = console_server + ":" + str(console_port)
2492 if console_thread_key in global_config["console_thread"]:
2493 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02002494 return global_config["console_thread"][console_thread_key]
tierno7edb6752016-03-21 17:37:52 +01002495
2496 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02002497 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01002498 if port in global_config["console_ports"]:
2499 continue
2500 try:
2501 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2502 clithread.start()
2503 global_config["console_thread"][console_thread_key] = clithread
2504 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02002505 return clithread
tierno7edb6752016-03-21 17:37:52 +01002506 except cli.ConsoleProxyExceptionPortUsed as e:
2507 #port used, try with onoher
2508 continue
2509 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02002510 raise NfvoException(str(e), HTTP_Bad_Request)
2511 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002512
2513def check_tenant(mydb, tenant_id):
2514 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02002515 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2516 if not tenant:
2517 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2518 return
tierno7edb6752016-03-21 17:37:52 +01002519
2520def new_tenant(mydb, tenant_dict):
tiernof97fd272016-07-11 14:32:37 +02002521 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2522 return tenant_id
tierno7edb6752016-03-21 17:37:52 +01002523
2524def delete_tenant(mydb, tenant):
2525 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002526
2527 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2528 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2529 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01002530
2531def new_datacenter(mydb, datacenter_descriptor):
2532 if "config" in datacenter_descriptor:
2533 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02002534 #Check that datacenter-type is correct
2535 datacenter_type = datacenter_descriptor.get("type", "openvim");
2536 module_info = None
2537 try:
2538 module = "vimconn_" + datacenter_type
2539 module_info = imp.find_module(module)
2540 except (IOError, ImportError):
2541 if module_info and module_info[0]:
2542 file.close(module_info[0])
2543 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
2544
tiernof97fd272016-07-11 14:32:37 +02002545 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2546 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002547
2548def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2549 #obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02002550 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno7edb6752016-03-21 17:37:52 +01002551 #edit data
tiernof97fd272016-07-11 14:32:37 +02002552 datacenter_id = datacenter['uuid']
2553 where={'uuid': datacenter['uuid']}
tierno7edb6752016-03-21 17:37:52 +01002554 if "config" in datacenter_descriptor:
2555 if datacenter_descriptor['config']!=None:
2556 try:
2557 new_config_dict = datacenter_descriptor["config"]
2558 #delete null fields
2559 to_delete=[]
2560 for k in new_config_dict:
2561 if new_config_dict[k]==None:
2562 to_delete.append(k)
2563
tiernof97fd272016-07-11 14:32:37 +02002564 config_dict = yaml.load(datacenter["config"])
tierno7edb6752016-03-21 17:37:52 +01002565 config_dict.update(new_config_dict)
2566 #delete null fields
2567 for k in to_delete:
2568 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02002569 except Exception as e:
2570 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002571 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 +02002572 mydb.update_rows('datacenters', datacenter_descriptor, where)
2573 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002574
2575def delete_datacenter(mydb, datacenter):
2576 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002577 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2578 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2579 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01002580
tierno8008c3a2016-10-13 15:34:28 +00002581def 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 +01002582 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002583 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002584 datacenter_name=myvim["name"]
2585
2586 create_vim_tenant=True if vim_tenant_id==None and vim_tenant_name==None else False
2587
2588 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002589 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002590 if vim_tenant_name==None:
2591 vim_tenant_name=tenant_dict['name']
2592
2593 #check that this association does not exist before
2594 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernof97fd272016-07-11 14:32:37 +02002595 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2596 if len(tenants_datacenters)>0:
2597 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002598
2599 vim_tenant_id_exist_atdb=False
2600 if not create_vim_tenant:
2601 where_={"datacenter_id": datacenter_id}
2602 if vim_tenant_id!=None:
2603 where_["vim_tenant_id"] = vim_tenant_id
2604 if vim_tenant_name!=None:
2605 where_["vim_tenant_name"] = vim_tenant_name
2606 #check if vim_tenant_id is already at database
tiernof97fd272016-07-11 14:32:37 +02002607 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2608 if len(datacenter_tenants_dict)>=1:
tierno7edb6752016-03-21 17:37:52 +01002609 datacenter_tenants_dict = datacenter_tenants_dict[0]
2610 vim_tenant_id_exist_atdb=True
2611 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2612 else: #result=0
2613 datacenter_tenants_dict = {}
2614 #insert at table datacenter_tenants
2615 else: #if vim_tenant_id==None:
2616 #create tenant at VIM if not provided
tiernoae4a8d12016-07-08 12:30:39 +02002617 try:
2618 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2619 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002620 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 +01002621 datacenter_tenants_dict = {}
2622 datacenter_tenants_dict["created"]="true"
2623
2624 #fill datacenter_tenants table
2625 if not vim_tenant_id_exist_atdb:
2626 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
2627 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
2628 datacenter_tenants_dict["user"] = vim_username
2629 datacenter_tenants_dict["passwd"] = vim_password
2630 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tierno8008c3a2016-10-13 15:34:28 +00002631 if config:
2632 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
tiernof97fd272016-07-11 14:32:37 +02002633 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002634 datacenter_tenants_dict["uuid"] = id_
2635
2636 #fill tenants_datacenters table
2637 tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"]
tiernof97fd272016-07-11 14:32:37 +02002638 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
2639 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002640
2641def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2642 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002643 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002644
2645 #get nfvo_tenant info
2646 if not tenant_id or tenant_id=="any":
2647 tenant_uuid = None
2648 else:
tiernof97fd272016-07-11 14:32:37 +02002649 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002650 tenant_uuid = tenant_dict['uuid']
2651
2652 #check that this association exist before
2653 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2654 if tenant_uuid:
2655 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02002656 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2657 if len(tenant_datacenter_list)==0 and tenant_uuid:
2658 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002659
2660 #delete this association
tiernof97fd272016-07-11 14:32:37 +02002661 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01002662
2663 #get vim_tenant info and deletes
2664 warning=''
2665 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02002666 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2667 #try to delete vim:tenant
2668 try:
2669 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2670 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01002671 #delete tenant at VIM if created by NFVO
tiernoae4a8d12016-07-08 12:30:39 +02002672 try:
2673 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2674 except vimconn.vimconnException as e:
2675 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2676 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02002677 except db_base_Exception as e:
2678 logger.error("Cannot delete datacenter_tenants " + str(e))
2679 pass #the error will be caused because dependencies, vim_tenant can not be deleted
tierno7edb6752016-03-21 17:37:52 +01002680
tiernof97fd272016-07-11 14:32:37 +02002681 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01002682
2683def datacenter_action(mydb, tenant_id, datacenter, action_dict):
2684 #DEPRECATED
2685 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002686 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002687
2688 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02002689 try:
tiernof97fd272016-07-11 14:32:37 +02002690 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02002691 #print content
2692 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002693 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
2694 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002695 #update nets Change from VIM format to NFVO format
2696 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002697 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01002698 net_nfvo={'datacenter_id': datacenter_id}
2699 net_nfvo['name'] = net['name']
2700 #net_nfvo['description']= net['name']
2701 net_nfvo['vim_net_id'] = net['id']
2702 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2703 net_nfvo['shared'] = net['shared']
2704 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
2705 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002706 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
2707 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
2708 return inserted
tierno7edb6752016-03-21 17:37:52 +01002709 elif 'net-edit' in action_dict:
2710 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02002711 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002712 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01002713 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002714 return result
tierno7edb6752016-03-21 17:37:52 +01002715 elif 'net-delete' in action_dict:
2716 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02002717 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002718 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01002719 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002720 return result
tierno7edb6752016-03-21 17:37:52 +01002721
2722 else:
tiernof97fd272016-07-11 14:32:37 +02002723 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002724
2725def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
2726 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002727 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002728
tierno42fcc3b2016-07-06 17:20:40 +02002729 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002730 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01002731 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02002732 return result
tierno7edb6752016-03-21 17:37:52 +01002733
2734def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
2735 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002736 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002737 filter_dict={}
2738 if action_dict:
2739 action_dict = action_dict["netmap"]
2740 if 'vim_id' in action_dict:
2741 filter_dict["id"] = action_dict['vim_id']
2742 if 'vim_name' in action_dict:
2743 filter_dict["name"] = action_dict['vim_name']
2744 else:
2745 filter_dict["shared"] = True
2746
tiernoae4a8d12016-07-08 12:30:39 +02002747 try:
tiernof97fd272016-07-11 14:32:37 +02002748 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02002749 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002750 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
2751 raise NfvoException(str(e), HTTP_Internal_Server_Error)
2752 if len(vim_nets)>1 and action_dict:
2753 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
2754 elif len(vim_nets)==0: # and action_dict:
2755 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002756 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002757 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01002758 net_nfvo={'datacenter_id': datacenter_id}
2759 if action_dict and "name" in action_dict:
2760 net_nfvo['name'] = action_dict['name']
2761 else:
2762 net_nfvo['name'] = net['name']
2763 #net_nfvo['description']= net['name']
2764 net_nfvo['vim_net_id'] = net['id']
2765 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2766 net_nfvo['shared'] = net['shared']
2767 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02002768 try:
2769 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002770 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02002771 net_nfvo["uuid"] = net_id
2772 except db_base_Exception as e:
2773 if action_dict:
2774 raise
2775 else:
2776 net_nfvo["status"] = "FAIL: " + str(e)
tierno7edb6752016-03-21 17:37:52 +01002777 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002778 return net_list
tierno7edb6752016-03-21 17:37:52 +01002779
2780def vim_action_get(mydb, tenant_id, datacenter, item, name):
2781 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002782 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002783 filter_dict={}
2784 if name:
tierno42fcc3b2016-07-06 17:20:40 +02002785 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01002786 filter_dict["id"] = name
2787 else:
2788 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02002789 try:
2790 if item=="networks":
2791 #filter_dict['tenant_id'] = myvim['tenant_id']
2792 content = myvim.get_network_list(filter_dict=filter_dict)
2793 elif item=="tenants":
2794 content = myvim.get_tenant_list(filter_dict=filter_dict)
tierno4540ea52017-01-18 17:44:32 +01002795 elif item == "images":
2796 content = myvim.get_image_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02002797 else:
tiernof97fd272016-07-11 14:32:37 +02002798 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02002799 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02002800 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02002801 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02002802 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02002803 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 +02002804 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002805 else:
tiernof97fd272016-07-11 14:32:37 +02002806 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02002807 except vimconn.vimconnException as e:
2808 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02002809 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002810
2811def vim_action_delete(mydb, tenant_id, datacenter, item, name):
2812 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02002813 if tenant_id == "any":
2814 tenant_id=None
2815
tiernoa2793912016-10-04 08:15:08 +00002816 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02002817 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02002818 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
2819 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02002820 items = content.values()[0]
2821 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02002822 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002823 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02002824 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002825 else: # it is a dict
2826 item_id = items["id"]
2827 item_name = str(items.get("name"))
tierno7edb6752016-03-21 17:37:52 +01002828
tiernoae4a8d12016-07-08 12:30:39 +02002829 try:
2830 if item=="networks":
2831 content = myvim.delete_network(item_id)
2832 elif item=="tenants":
2833 content = myvim.delete_tenant(item_id)
tierno4540ea52017-01-18 17:44:32 +01002834 elif item == "images":
2835 content = myvim.delete_image(item_id)
tiernoae4a8d12016-07-08 12:30:39 +02002836 else:
tiernof97fd272016-07-11 14:32:37 +02002837 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002838 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002839 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
2840 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002841
tiernof97fd272016-07-11 14:32:37 +02002842 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno7edb6752016-03-21 17:37:52 +01002843
2844def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
2845 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002846 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02002847 if tenant_id == "any":
2848 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00002849 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002850 try:
2851 if item=="networks":
2852 net = descriptor["network"]
2853 net_name = net.pop("name")
2854 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02002855 net_public = net.pop("shared", False)
2856 net_ipprofile = net.pop("ip_profile", None)
2857 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, **net)
tiernoae4a8d12016-07-08 12:30:39 +02002858 elif item=="tenants":
2859 tenant = descriptor["tenant"]
2860 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
2861 else:
tiernof97fd272016-07-11 14:32:37 +02002862 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002863 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002864 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002865
tierno7edb6752016-03-21 17:37:52 +01002866 return vim_action_get(mydb, tenant_id, datacenter, item, content)
2867
tierno66aa0372016-07-06 17:31:12 +02002868