blob: 279eb073491718ed89d390698a48ce486510e130 [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
197def check_vnf_descriptor(vnf_descriptor):
198 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"]:
202 name_list = []
203 #dataplane interfaces
204 for numa in vnfc.get("numas",() ):
205 for interface in numa.get("interfaces",()):
206 if interface["name"] in name_list:
tiernof97fd272016-07-11 14:32:37 +0200207 raise NfvoException("Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC"\
208 .format(vnfc["name"], interface["name"]),
209 HTTP_Bad_Request)
210 name_list.append( interface["name"] )
tierno7edb6752016-03-21 17:37:52 +0100211 #bridge interfaces
212 for interface in vnfc.get("bridge-ifaces",() ):
213 if interface["name"] in name_list:
tiernof97fd272016-07-11 14:32:37 +0200214 raise NfvoException("Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC"\
215 .format(vnfc["name"], interface["name"]),
216 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100217 name_list.append( interface["name"] )
218 vnfc_interfaces[ vnfc["name"] ] = name_list
219
220 #check if the info in external_connections matches with the one in the vnfcs
221 name_list=[]
222 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
223 if external_connection["name"] in name_list:
tiernof97fd272016-07-11 14:32:37 +0200224 raise NfvoException("Error at vnf:external-connections:name, value '{}' already used as an external-connection"\
225 .format(external_connection["name"]),
226 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100227 name_list.append(external_connection["name"])
228 if external_connection["VNFC"] not in vnfc_interfaces:
tiernof97fd272016-07-11 14:32:37 +0200229 raise NfvoException("Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC"\
230 .format(external_connection["name"], external_connection["VNFC"]),
231 HTTP_Bad_Request)
232
tierno7edb6752016-03-21 17:37:52 +0100233 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
tiernof97fd272016-07-11 14:32:37 +0200234 raise NfvoException("Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC"\
235 .format(external_connection["name"], external_connection["local_iface_name"]),
236 HTTP_Bad_Request )
tierno7edb6752016-03-21 17:37:52 +0100237
238 #check if the info in internal_connections matches with the one in the vnfcs
239 name_list=[]
240 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
241 if internal_connection["name"] in name_list:
tiernof97fd272016-07-11 14:32:37 +0200242 raise NfvoException("Error at vnf:internal-connections:name, value '%s' already used as an internal-connection"\
243 .format(internal_connection["name"]),
244 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100245 name_list.append(internal_connection["name"])
246 #We should check that internal-connections of type "ptp" have only 2 elements
247 if len(internal_connection["elements"])>2 and internal_connection["type"] == "ptp":
tiernof97fd272016-07-11 14:32:37 +0200248 raise NfvoException("Error at vnf:internal-connections[name:'{}']:elements, size must be 2 for a type:'ptp'"\
249 .format(internal_connection["name"]),
250 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100251 for port in internal_connection["elements"]:
252 if port["VNFC"] not in vnfc_interfaces:
tiernof97fd272016-07-11 14:32:37 +0200253 raise NfvoException("Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC"\
254 .format(internal_connection["name"], port["VNFC"]),
255 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100256 if port["local_iface_name"] not in vnfc_interfaces[ port["VNFC"] ]:
tiernof97fd272016-07-11 14:32:37 +0200257 raise NfvoException("Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC"\
258 .format(internal_connection["name"], port["local_iface_name"]),
259 HTTP_Bad_Request)
260 return -HTTP_Bad_Request,
tierno7edb6752016-03-21 17:37:52 +0100261
tierno5e91eb82016-10-04 09:39:07 +0000262def 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 +0100263 #look if image exist
264 if only_create_at_vim:
265 image_mano_id = image_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000266 if return_on_error == None:
267 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100268 else:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200269 if image_dict['location'] is not None:
270 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
271 else:
272 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
tiernof97fd272016-07-11 14:32:37 +0200273 if len(images)>=1:
274 image_mano_id = images[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100275 else:
276 #create image
277 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
garciadeblasb69fa9f2016-09-28 12:04:10 +0200278 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
279 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
tierno7edb6752016-03-21 17:37:52 +0100280 }
tiernof97fd272016-07-11 14:32:37 +0200281 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
282 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100283 #create image at every vim
284 for vim_id,vim in vims.iteritems():
285 image_created="false"
286 #look at database
tiernof97fd272016-07-11 14:32:37 +0200287 image_db = mydb.get_rows(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100288 #look at VIM if this image exist
tiernoae4a8d12016-07-08 12:30:39 +0200289 try:
garciadeblasb69fa9f2016-09-28 12:04:10 +0200290 if image_dict['location'] is not None:
291 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
292 else:
293 filter_dict={}
294 filter_dict['name']=image_dict['universal_name']
295 filter_dict['checksum']=image_dict['checksum']
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000296 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200297 vim_images = vim.get_image_list(filter_dict)
298 if len(vim_images) > 1:
garciadeblas3fa2c052017-01-05 12:00:08 +0100299 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
garciadeblasbb6a1ed2016-09-30 14:02:09 +0000300 elif len(vim_images) == 0:
garciadeblas3fa2c052017-01-05 12:00:08 +0100301 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
garciadeblasb69fa9f2016-09-28 12:04:10 +0200302 else:
303 image_vim_id = vim_images[0].id
304
tiernoae4a8d12016-07-08 12:30:39 +0200305 except vimconn.vimconnNotFoundException as e:
tierno7edb6752016-03-21 17:37:52 +0100306 #Create the image in VIM
tiernoae4a8d12016-07-08 12:30:39 +0200307 try:
308 image_vim_id = vim.new_image(image_dict)
tierno7edb6752016-03-21 17:37:52 +0100309 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
310 image_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200311 except vimconn.vimconnException as e:
312 if return_on_error:
313 logger.error("Error creating image at VIM: %s", str(e))
tiernof97fd272016-07-11 14:32:37 +0200314 raise
tierno5e91eb82016-10-04 09:39:07 +0000315 image_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200316 logger.warn("Error creating image at VIM: %s", str(e))
317 continue
318 except vimconn.vimconnException as e:
tierno5e91eb82016-10-04 09:39:07 +0000319 if return_on_error:
320 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
321 raise
garciadeblasb69fa9f2016-09-28 12:04:10 +0200322 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
tierno5e91eb82016-10-04 09:39:07 +0000323 image_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200324 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200325 #if we reach here, the image has been created or existed
tiernof97fd272016-07-11 14:32:37 +0200326 if len(image_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100327 #add new vim_id at datacenters_images
328 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
329 elif image_db[0]["vim_id"]!=image_vim_id:
330 #modify existing vim_id at datacenters_images
331 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
332
tiernof97fd272016-07-11 14:32:37 +0200333 return image_vim_id if only_create_at_vim else image_mano_id
tierno7edb6752016-03-21 17:37:52 +0100334
tierno5e91eb82016-10-04 09:39:07 +0000335def 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 +0100336 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
337 'ram':flavor_dict.get('ram'),
338 'vcpus':flavor_dict.get('vcpus'),
339 }
340 if 'extended' in flavor_dict and flavor_dict['extended']==None:
341 del flavor_dict['extended']
342 if 'extended' in flavor_dict:
343 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
344
345 #look if flavor exist
346 if only_create_at_vim:
347 flavor_mano_id = flavor_dict['uuid']
tierno5e91eb82016-10-04 09:39:07 +0000348 if return_on_error == None:
349 return_on_error = True
tierno7edb6752016-03-21 17:37:52 +0100350 else:
tiernof97fd272016-07-11 14:32:37 +0200351 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
352 if len(flavors)>=1:
353 flavor_mano_id = flavors[0]['uuid']
tierno7edb6752016-03-21 17:37:52 +0100354 else:
355 #create flavor
356 #create one by one the images of aditional disks
357 dev_image_list=[] #list of images
358 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
359 dev_nb=0
360 for device in flavor_dict['extended'].get('devices',[]):
garciadeblas41f18be2016-10-04 09:09:58 +0200361 if "image" not in device and "image name" not in device:
tierno7edb6752016-03-21 17:37:52 +0100362 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200363 image_dict={}
364 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
365 image_dict['universal_name']=device.get('image name')
366 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
367 image_dict['location']=device.get('image')
368 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100369 image_metadata_dict = device.get('image metadata', None)
370 image_metadata_str = None
371 if image_metadata_dict != None:
372 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
373 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200374 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
375 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
tierno7edb6752016-03-21 17:37:52 +0100376 dev_image_list.append(image_id)
377 dev_nb += 1
378 temp_flavor_dict['name'] = flavor_dict['name']
379 temp_flavor_dict['description'] = flavor_dict.get('description',None)
tiernof97fd272016-07-11 14:32:37 +0200380 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
381 flavor_mano_id= content
382 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100383 #create flavor at every vim
384 if 'uuid' in flavor_dict:
385 del flavor_dict['uuid']
386 flavor_vim_id=None
387 for vim_id,vim in vims.items():
388 flavor_created="false"
389 #look at database
tiernof97fd272016-07-11 14:32:37 +0200390 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
tierno7edb6752016-03-21 17:37:52 +0100391 #look at VIM if this flavor exist SKIPPED
392 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
393 #if res_vim < 0:
394 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
395 # continue
396 #elif res_vim==0:
397
398 #Create the flavor in VIM
399 #Translate images at devices from MANO id to VIM id
montesmoreno0c8def02016-12-22 12:16:23 +0000400 disk_list = []
tierno7edb6752016-03-21 17:37:52 +0100401 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
402 #make a copy of original devices
403 devices_original=[]
montesmoreno0c8def02016-12-22 12:16:23 +0000404
tierno7edb6752016-03-21 17:37:52 +0100405 for device in flavor_dict["extended"].get("devices",[]):
406 dev={}
407 dev.update(device)
408 devices_original.append(dev)
409 if 'image' in device:
410 del device['image']
411 if 'image metadata' in device:
412 del device['image metadata']
413 dev_nb=0
414 for index in range(0,len(devices_original)) :
415 device=devices_original[index]
montesmoreno0c8def02016-12-22 12:16:23 +0000416 if "image" not in device and "image name" not in device:
417 if 'size' in device:
418 disk_list.append({'size': device.get('size', default_volume_size)})
tierno7edb6752016-03-21 17:37:52 +0100419 continue
garciadeblasb69fa9f2016-09-28 12:04:10 +0200420 image_dict={}
421 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
422 image_dict['universal_name']=device.get('image name')
423 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
424 image_dict['location']=device.get('image')
425 image_dict['checksum']=device.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100426 image_metadata_dict = device.get('image metadata', None)
427 image_metadata_str = None
428 if image_metadata_dict != None:
429 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
430 image_dict['metadata']=image_metadata_str
tiernof97fd272016-07-11 14:32:37 +0200431 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 +0100432 image_dict["uuid"]=image_mano_id
tiernof97fd272016-07-11 14:32:37 +0200433 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 +0000434
435 #save disk information (image must be based on and size
436 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
437
tierno7edb6752016-03-21 17:37:52 +0100438 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
439 dev_nb += 1
tiernof97fd272016-07-11 14:32:37 +0200440 if len(flavor_db)>0:
tierno7edb6752016-03-21 17:37:52 +0100441 #check that this vim_id exist in VIM, if not create
442 flavor_vim_id=flavor_db[0]["vim_id"]
tiernoae4a8d12016-07-08 12:30:39 +0200443 try:
444 vim.get_flavor(flavor_vim_id)
445 continue #flavor exist
446 except vimconn.vimconnException:
447 pass
tierno7edb6752016-03-21 17:37:52 +0100448 #create flavor at vim
tiernoae4a8d12016-07-08 12:30:39 +0200449 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
450 try:
451 flavor_vim_id = vim.new_flavor(flavor_dict)
tierno7edb6752016-03-21 17:37:52 +0100452 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
453 flavor_created="true"
tiernoae4a8d12016-07-08 12:30:39 +0200454 except vimconn.vimconnException as e:
455 if return_on_error:
456 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tiernof97fd272016-07-11 14:32:37 +0200457 raise
tiernoae4a8d12016-07-08 12:30:39 +0200458 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
tierno5e91eb82016-10-04 09:39:07 +0000459 flavor_vim_id = None
tiernoae4a8d12016-07-08 12:30:39 +0200460 continue
tierno7edb6752016-03-21 17:37:52 +0100461 #if reach here the flavor has been create or exist
tiernof97fd272016-07-11 14:32:37 +0200462 if len(flavor_db)==0:
tierno7edb6752016-03-21 17:37:52 +0100463 #add new vim_id at datacenters_flavors
montesmoreno0c8def02016-12-22 12:16:23 +0000464 extended_devices_yaml = None
465 if len(disk_list) > 0:
466 extended_devices = dict()
467 extended_devices['disks'] = disk_list
468 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
469 mydb.new_row('datacenters_flavors',
470 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
471 'created':flavor_created,'extended': extended_devices_yaml})
tierno7edb6752016-03-21 17:37:52 +0100472 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
473 #modify existing vim_id at datacenters_flavors
474 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
475
tiernof97fd272016-07-11 14:32:37 +0200476 return flavor_vim_id if only_create_at_vim else flavor_mano_id
tierno7edb6752016-03-21 17:37:52 +0100477
478def new_vnf(mydb, tenant_id, vnf_descriptor):
479 global global_config
480
481 # Step 1. Check the VNF descriptor
tiernof97fd272016-07-11 14:32:37 +0200482 check_vnf_descriptor(vnf_descriptor)
tierno7edb6752016-03-21 17:37:52 +0100483 # Step 2. Check tenant exist
484 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200485 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100486 if "tenant_id" in vnf_descriptor["vnf"]:
487 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200488 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
489 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100490 else:
491 vnf_descriptor['vnf']['tenant_id'] = tenant_id
492 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +0200493 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100494 else:
495 vims={}
496
497 # Step 4. Review the descriptor and add missing fields
498 #print vnf_descriptor
tiernof97fd272016-07-11 14:32:37 +0200499 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
tierno7edb6752016-03-21 17:37:52 +0100500 vnf_name = vnf_descriptor['vnf']['name']
501 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
502 if "physical" in vnf_descriptor['vnf']:
503 del vnf_descriptor['vnf']['physical']
504 #print vnf_descriptor
505 # Step 5. Check internal connections
506 # TODO: to be moved to step 1????
507 internal_connections=vnf_descriptor['vnf'].get('internal_connections',[])
508 for ic in internal_connections:
509 if len(ic['elements'])>2 and ic['type']=='ptp':
tiernof97fd272016-07-11 14:32:37 +0200510 raise NfvoException("Mismatch 'type':'ptp' with {} elements at 'vnf':'internal-conections'['name':'{}']. Change 'type' to 'data'".format(len(ic), ic['name']),
511 HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100512 elif len(ic['elements'])==2 and ic['type']=='data':
tiernof97fd272016-07-11 14:32:37 +0200513 raise NfvoException("Mismatch 'type':'data' with 2 elements at 'vnf':'internal-conections'['name':'{}']. Change 'type' to 'ptp'".format(ic['name']),
514 HTTP_Bad_Request)
515
tierno7edb6752016-03-21 17:37:52 +0100516 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200517 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
518 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
tierno7edb6752016-03-21 17:37:52 +0100519
520 #For each VNFC, we add it to the VNFCDict and we create a flavor.
521 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
522 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
tierno7edb6752016-03-21 17:37:52 +0100523 try:
tiernof97fd272016-07-11 14:32:37 +0200524 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100525 for vnfc in vnf_descriptor['vnf']['VNFC']:
526 VNFCitem={}
527 VNFCitem["name"] = vnfc['name']
528 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
529
tiernof97fd272016-07-11 14:32:37 +0200530 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
tierno7edb6752016-03-21 17:37:52 +0100531
532 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200533 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 +0100534 myflavorDict["description"] = VNFCitem["description"]
535 myflavorDict["ram"] = vnfc.get("ram", 0)
536 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
537 myflavorDict["disk"] = vnfc.get("disk", 1)
538 myflavorDict["extended"] = {}
539
540 devices = vnfc.get("devices")
541 if devices != None:
542 myflavorDict["extended"]["devices"] = devices
543
544 # TODO:
545 # 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
546 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
547
548 # Previous code has been commented
549 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
550 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
551 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
552 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
553 #else:
554 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
555 # if result2:
556 # print "Error creating flavor: unknown processor model. Rollback successful."
557 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
558 # else:
559 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
560 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
561
562 if 'numas' in vnfc and len(vnfc['numas'])>0:
563 myflavorDict['extended']['numas'] = vnfc['numas']
564
565 #print myflavorDict
566
567 # Step 6.2 New flavors are created in the VIM
tiernof97fd272016-07-11 14:32:37 +0200568 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
tierno7edb6752016-03-21 17:37:52 +0100569
tiernof97fd272016-07-11 14:32:37 +0200570 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100571 VNFCitem["flavor_id"] = flavor_id
572 VNFCDict[vnfc['name']] = VNFCitem
573
tiernof97fd272016-07-11 14:32:37 +0200574 logger.debug("Creating new images in the VIM for each VNFC")
tierno7edb6752016-03-21 17:37:52 +0100575 # Step 6.3 New images are created in the VIM
576 #For each VNFC, we must create the appropriate image.
577 #This "for" loop might be integrated with the previous one
578 #In case this integration is made, the VNFCDict might become a VNFClist.
579 for vnfc in vnf_descriptor['vnf']['VNFC']:
tiernof97fd272016-07-11 14:32:37 +0200580 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200581 image_dict={}
582 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
583 image_dict['universal_name']=vnfc.get('image name')
584 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
585 image_dict['location']=vnfc.get('VNFC image')
586 image_dict['checksum']=vnfc.get('image checksum')
tierno7edb6752016-03-21 17:37:52 +0100587 image_metadata_dict = vnfc.get('image metadata', None)
588 image_metadata_str = None
589 if image_metadata_dict is not None:
590 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
591 image_dict['metadata']=image_metadata_str
592 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
tiernof97fd272016-07-11 14:32:37 +0200593 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
594 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
tierno7edb6752016-03-21 17:37:52 +0100595 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200596 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
tierno7edb6752016-03-21 17:37:52 +0100597
tiernof97fd272016-07-11 14:32:37 +0200598
599 # Step 7. Storing the VNF descriptor in the repository
600 if "descriptor" not in vnf_descriptor["vnf"]:
601 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +0100602
tiernof97fd272016-07-11 14:32:37 +0200603 # Step 8. Adding the VNF to the NFVO DB
604 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
605 return vnf_id
606 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
tierno7edb6752016-03-21 17:37:52 +0100607 _, message = rollback(mydb, vims, rollback_list)
tiernof97fd272016-07-11 14:32:37 +0200608 if isinstance(e, db_base_Exception):
609 error_text = "Exception at database"
610 elif isinstance(e, KeyError):
611 error_text = "KeyError exception "
612 e.http_code = HTTP_Internal_Server_Error
613 else:
614 error_text = "Exception at VIM"
615 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
616 #logger.error("start_scenario %s", error_text)
617 raise NfvoException(error_text, e.http_code)
618
garciadeblas9f8456e2016-09-05 05:02:59 +0200619def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
620 global global_config
621
622 # Step 1. Check the VNF descriptor
623 check_vnf_descriptor(vnf_descriptor)
624 # Step 2. Check tenant exist
625 if tenant_id != "any":
626 check_tenant(mydb, tenant_id)
627 if "tenant_id" in vnf_descriptor["vnf"]:
628 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
629 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
630 HTTP_Unauthorized)
631 else:
632 vnf_descriptor['vnf']['tenant_id'] = tenant_id
633 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
634 vims = get_vim(mydb, tenant_id)
635 else:
636 vims={}
637
638 # Step 4. Review the descriptor and add missing fields
639 #print vnf_descriptor
640 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
641 vnf_name = vnf_descriptor['vnf']['name']
642 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
643 if "physical" in vnf_descriptor['vnf']:
644 del vnf_descriptor['vnf']['physical']
645 #print vnf_descriptor
646 # Step 5. Check internal connections
647 # TODO: to be moved to step 1????
648 internal_connections=vnf_descriptor['vnf'].get('internal_connections',[])
649 for ic in internal_connections:
650 if len(ic['elements'])>2 and ic['type']=='e-line':
651 raise NfvoException("Mismatch 'type':'e-line' with {} elements at 'vnf':'internal-conections'['name':'{}']. Change 'type' to 'e-lan'".format(len(ic), ic['name']),
652 HTTP_Bad_Request)
653
654 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
655 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
656 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
657
658 #For each VNFC, we add it to the VNFCDict and we create a flavor.
659 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
660 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
661 try:
662 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
663 for vnfc in vnf_descriptor['vnf']['VNFC']:
664 VNFCitem={}
665 VNFCitem["name"] = vnfc['name']
666 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
667
668 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
669
670 myflavorDict = {}
garciadeblasb69fa9f2016-09-28 12:04:10 +0200671 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 +0200672 myflavorDict["description"] = VNFCitem["description"]
673 myflavorDict["ram"] = vnfc.get("ram", 0)
674 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
675 myflavorDict["disk"] = vnfc.get("disk", 1)
676 myflavorDict["extended"] = {}
677
678 devices = vnfc.get("devices")
679 if devices != None:
680 myflavorDict["extended"]["devices"] = devices
681
682 # TODO:
683 # 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
684 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
685
686 # Previous code has been commented
687 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
688 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
689 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
690 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
691 #else:
692 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
693 # if result2:
694 # print "Error creating flavor: unknown processor model. Rollback successful."
695 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
696 # else:
697 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
698 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
699
700 if 'numas' in vnfc and len(vnfc['numas'])>0:
701 myflavorDict['extended']['numas'] = vnfc['numas']
702
703 #print myflavorDict
704
705 # Step 6.2 New flavors are created in the VIM
706 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
707
708 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
709 VNFCitem["flavor_id"] = flavor_id
710 VNFCDict[vnfc['name']] = VNFCitem
711
712 logger.debug("Creating new images in the VIM for each VNFC")
713 # Step 6.3 New images are created in the VIM
714 #For each VNFC, we must create the appropriate image.
715 #This "for" loop might be integrated with the previous one
716 #In case this integration is made, the VNFCDict might become a VNFClist.
717 for vnfc in vnf_descriptor['vnf']['VNFC']:
718 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
garciadeblasb69fa9f2016-09-28 12:04:10 +0200719 image_dict={}
720 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
721 image_dict['universal_name']=vnfc.get('image name')
722 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
723 image_dict['location']=vnfc.get('VNFC image')
724 image_dict['checksum']=vnfc.get('image checksum')
garciadeblas9f8456e2016-09-05 05:02:59 +0200725 image_metadata_dict = vnfc.get('image metadata', None)
726 image_metadata_str = None
727 if image_metadata_dict is not None:
728 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
729 image_dict['metadata']=image_metadata_str
730 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
731 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
732 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
733 VNFCDict[vnfc['name']]["image_id"] = image_id
garciadeblasb69fa9f2016-09-28 12:04:10 +0200734 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
garciadeblas9f8456e2016-09-05 05:02:59 +0200735
736
737 # Step 7. Storing the VNF descriptor in the repository
738 if "descriptor" not in vnf_descriptor["vnf"]:
739 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
740
741 # Step 8. Adding the VNF to the NFVO DB
742 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
743 return vnf_id
744 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
745 _, message = rollback(mydb, vims, rollback_list)
746 if isinstance(e, db_base_Exception):
747 error_text = "Exception at database"
748 elif isinstance(e, KeyError):
749 error_text = "KeyError exception "
750 e.http_code = HTTP_Internal_Server_Error
751 else:
752 error_text = "Exception at VIM"
753 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
754 #logger.error("start_scenario %s", error_text)
755 raise NfvoException(error_text, e.http_code)
756
tierno7edb6752016-03-21 17:37:52 +0100757def get_vnf_id(mydb, tenant_id, vnf_id):
758 #check valid tenant_id
tiernof97fd272016-07-11 14:32:37 +0200759 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100760 #obtain data
761 where_or = {}
762 if tenant_id != "any":
763 where_or["tenant_id"] = tenant_id
764 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200765 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 +0100766
tiernof97fd272016-07-11 14:32:37 +0200767 vnf_id=vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100768 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
tiernof97fd272016-07-11 14:32:37 +0200769 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
tierno7edb6752016-03-21 17:37:52 +0100770 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
771 data={'vnf' : filtered_content}
772 #GET VM
tiernof97fd272016-07-11 14:32:37 +0200773 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
tierno7edb6752016-03-21 17:37:52 +0100774 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description'),
775 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200776 if len(content)==0:
777 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100778
779 data['vnf']['VNFC'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200780 #TODO: GET all the information from a VNFC and include it in the output.
781
tierno7edb6752016-03-21 17:37:52 +0100782 #GET NET
tiernof97fd272016-07-11 14:32:37 +0200783 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
tierno7edb6752016-03-21 17:37:52 +0100784 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
785 WHERE={'vnfs.uuid': vnf_id} )
tiernof97fd272016-07-11 14:32:37 +0200786 data['vnf']['nets'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200787
788 #GET ip-profile for each net
789 for net in data['vnf']['nets']:
790 ipprofiles = mydb.get_rows(FROM='ip_profiles',
791 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
792 WHERE={'net_id': net["uuid"]} )
793 if len(ipprofiles)==1:
794 net["ip_profile"] = ipprofiles[0]
795 elif len(ipprofiles)>1:
796 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
797
798
799 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
800
801 #GET External Interfaces
tiernof97fd272016-07-11 14:32:37 +0200802 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 +0100803 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
804 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
805 WHERE={'vnfs.uuid': vnf_id},
806 WHERE_NOT={'interfaces.external_name': None} )
807 #print content
tiernof97fd272016-07-11 14:32:37 +0200808 data['vnf']['external-connections'] = content
garciadeblas9f8456e2016-09-05 05:02:59 +0200809
tiernof97fd272016-07-11 14:32:37 +0200810 return data
tierno7edb6752016-03-21 17:37:52 +0100811
812
813def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
814 # Check tenant exist
815 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200816 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100817 # Get the URL of the VIM from the nfvo_tenant and the datacenter
tiernof97fd272016-07-11 14:32:37 +0200818 vims = get_vim(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100819 else:
820 vims={}
821
822 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
823 where_or = {}
824 if tenant_id != "any":
825 where_or["tenant_id"] = tenant_id
826 where_or["public"] = True
tiernof97fd272016-07-11 14:32:37 +0200827 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
828 vnf_id = vnf["uuid"]
tierno7edb6752016-03-21 17:37:52 +0100829
830 # "Getting the list of flavors and tenants of the VNF"
tiernof97fd272016-07-11 14:32:37 +0200831 flavorList = get_flavorlist(mydb, vnf_id)
832 if len(flavorList)==0:
833 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100834
tiernof97fd272016-07-11 14:32:37 +0200835 imageList = get_imagelist(mydb, vnf_id)
836 if len(imageList)==0:
837 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
tierno7edb6752016-03-21 17:37:52 +0100838
tiernof97fd272016-07-11 14:32:37 +0200839 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
840 if deleted == 0:
841 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +0100842
843 undeletedItems = []
844 for flavor in flavorList:
845 #check if flavor is used by other vnf
tiernof97fd272016-07-11 14:32:37 +0200846 try:
847 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
848 if len(c) > 0:
849 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
850 continue
851 #flavor not used, must be deleted
852 #delelte at VIM
853 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
tierno7edb6752016-03-21 17:37:52 +0100854 for flavor_vim in c:
855 if flavor_vim["datacenter_id"] not in vims:
856 continue
857 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
858 continue
859 myvim=vims[ flavor_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200860 try:
861 myvim.delete_flavor(flavor_vim["vim_id"])
862 except vimconn.vimconnNotFoundException as e:
863 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
864 except vimconn.vimconnException as e:
865 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
866 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
867 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200868 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
869 mydb.delete_row_by_id('flavors', flavor)
870 except db_base_Exception as e:
871 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
tierno7edb6752016-03-21 17:37:52 +0100872 undeletedItems.append("flavor %s" % flavor)
tiernof97fd272016-07-11 14:32:37 +0200873
tierno7edb6752016-03-21 17:37:52 +0100874
875 for image in imageList:
tiernof97fd272016-07-11 14:32:37 +0200876 try:
877 #check if image is used by other vnf
878 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
879 if len(c) > 0:
880 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
881 continue
882 #image not used, must be deleted
883 #delelte at VIM
884 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
tierno7edb6752016-03-21 17:37:52 +0100885 for image_vim in c:
886 if image_vim["datacenter_id"] not in vims:
887 continue
888 if image_vim['created']=='false': #skip this image because not created by openmano
889 continue
890 myvim=vims[ image_vim["datacenter_id"] ]
tiernoae4a8d12016-07-08 12:30:39 +0200891 try:
892 myvim.delete_image(image_vim["vim_id"])
893 except vimconn.vimconnNotFoundException as e:
894 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
895 except vimconn.vimconnException as e:
896 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
897 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
898 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
tiernof97fd272016-07-11 14:32:37 +0200899 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
900 mydb.delete_row_by_id('images', image)
901 except db_base_Exception as e:
902 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
tierno7edb6752016-03-21 17:37:52 +0100903 undeletedItems.append("image %s" % image)
904
tiernof97fd272016-07-11 14:32:37 +0200905 return vnf_id + " " + vnf["name"]
906 #if undeletedItems:
907 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
tierno7edb6752016-03-21 17:37:52 +0100908
909def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
910 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
911 if result < 0:
912 return result, vims
913 elif result == 0:
914 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
915 myvim = vims.values()[0]
916 result,servers = myvim.get_hosts_info()
917 if result < 0:
918 return result, servers
919 topology = {'name':myvim['name'] , 'servers': servers}
920 return result, topology
921
922def get_hosts(mydb, nfvo_tenant_id):
tiernof97fd272016-07-11 14:32:37 +0200923 vims = get_vim(mydb, nfvo_tenant_id)
924 if len(vims) == 0:
925 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
926 elif len(vims)>1:
927 #print "nfvo.datacenter_action() error. Several datacenters found"
928 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +0100929 myvim = vims.values()[0]
tiernof97fd272016-07-11 14:32:37 +0200930 try:
931 hosts = myvim.get_hosts()
932 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
tierno7edb6752016-03-21 17:37:52 +0100933
tiernof97fd272016-07-11 14:32:37 +0200934 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
935 for host in hosts:
936 server={'name':host['name'], 'vms':[]}
937 for vm in host['instances']:
938 #get internal name and model
939 try:
940 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
941 WHERE={'vim_vm_id':vm['id']} )
942 if len(c) == 0:
943 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
944 continue
945 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
946
947 except db_base_Exception as e:
948 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
949 datacenter['Datacenters'][0]['servers'].append(server)
950 #return -400, "en construccion"
tierno7edb6752016-03-21 17:37:52 +0100951
tiernof97fd272016-07-11 14:32:37 +0200952 #print 'datacenters '+ json.dumps(datacenter, indent=4)
953 return datacenter
954 except vimconn.vimconnException as e:
955 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +0100956
957def new_scenario(mydb, tenant_id, topo):
958
959# result, vims = get_vim(mydb, tenant_id)
960# if result < 0:
961# return result, vims
962#1: parse input
963 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +0200964 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +0100965 if "tenant_id" in topo:
966 if topo["tenant_id"] != tenant_id:
tiernof97fd272016-07-11 14:32:37 +0200967 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
968 HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +0100969 else:
970 tenant_id=None
971
972#1.1: get VNFs and external_networks (other_nets).
973 vnfs={}
974 other_nets={} #external_networks, bridge_networks and data_networkds
975 nodes = topo['topology']['nodes']
976 for k in nodes.keys():
977 if nodes[k]['type'] == 'VNF':
978 vnfs[k] = nodes[k]
979 vnfs[k]['ifaces'] = {}
980 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
981 other_nets[k] = nodes[k]
982 other_nets[k]['external']=True
983 elif nodes[k]['type'] == 'network':
984 other_nets[k] = nodes[k]
985 other_nets[k]['external']=False
986
987
988#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
989 for name,vnf in vnfs.items():
tiernocea279c2016-07-18 12:36:49 +0200990 where={}
991 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +0100992 error_text = ""
993 error_pos = "'topology':'nodes':'" + name + "'"
994 if 'vnf_id' in vnf:
995 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +0200996 where['uuid'] = vnf['vnf_id']
tierno7edb6752016-03-21 17:37:52 +0100997 if 'VNF model' in vnf:
998 error_text += " 'VNF model' " + vnf['VNF model']
tiernocea279c2016-07-18 12:36:49 +0200999 where['name'] = vnf['VNF model']
1000 if len(where) == 0:
tiernof97fd272016-07-11 14:32:37 +02001001 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
1002
tiernocea279c2016-07-18 12:36:49 +02001003 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1004 FROM='vnfs',
1005 WHERE=where,
1006 WHERE_OR=where_or,
1007 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001008 if len(vnf_db)==0:
1009 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1010 elif len(vnf_db)>1:
1011 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001012 vnf['uuid']=vnf_db[0]['uuid']
1013 vnf['description']=vnf_db[0]['description']
1014 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001015 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 +01001016 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1017 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001018 for ext_iface in ext_ifaces:
1019 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1020
1021#1.4 get list of connections
1022 conections = topo['topology']['connections']
1023 conections_list = []
tiernoefd80c92016-09-16 14:17:46 +02001024 conections_list_name = []
tierno7edb6752016-03-21 17:37:52 +01001025 for k in conections.keys():
1026 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1027 ifaces_list = conections[k]['nodes'].items()
1028 elif type(conections[k]['nodes'])==list: #list with dictionary
1029 ifaces_list=[]
1030 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1031 for k2 in conection_pair_list:
1032 ifaces_list += k2
1033
1034 con_type = conections[k].get("type", "link")
1035 if con_type != "link":
1036 if k in other_nets:
tiernof97fd272016-07-11 14:32:37 +02001037 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001038 other_nets[k] = {'external': False}
1039 if conections[k].get("graph"):
1040 other_nets[k]["graph"] = conections[k]["graph"]
1041 ifaces_list.append( (k, None) )
1042
1043
1044 if con_type == "external_network":
1045 other_nets[k]['external'] = True
1046 if conections[k].get("model"):
1047 other_nets[k]["model"] = conections[k]["model"]
1048 else:
1049 other_nets[k]["model"] = k
1050 if con_type == "dataplane_net" or con_type == "bridge_net":
1051 other_nets[k]["model"] = con_type
1052
tiernoefd80c92016-09-16 14:17:46 +02001053 conections_list_name.append(k)
tierno7edb6752016-03-21 17:37:52 +01001054 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)
1055 #print set(ifaces_list)
1056 #check valid VNF and iface names
1057 for iface in ifaces_list:
1058 if iface[0] not in vnfs and iface[0] not in other_nets :
tiernof97fd272016-07-11 14:32:37 +02001059 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1060 str(k), iface[0]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001061 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
tiernof97fd272016-07-11 14:32:37 +02001062 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1063 str(k), iface[0], iface[1]), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001064
1065#1.5 unify connections from the pair list to a consolidated list
1066 index=0
1067 while index < len(conections_list):
1068 index2 = index+1
1069 while index2 < len(conections_list):
1070 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1071 conections_list[index] |= conections_list[index2]
1072 del conections_list[index2]
tiernoefd80c92016-09-16 14:17:46 +02001073 del conections_list_name[index2]
tierno7edb6752016-03-21 17:37:52 +01001074 else:
1075 index2 += 1
1076 conections_list[index] = list(conections_list[index]) # from set to list again
1077 index += 1
1078 #for k in conections_list:
1079 # print k
1080
1081
1082
1083#1.6 Delete non external nets
1084# for k in other_nets.keys():
1085# if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1086# for con in conections_list:
1087# delete_indexes=[]
1088# for index in range(0,len(con)):
1089# if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1090# for index in delete_indexes:
1091# del con[index]
1092# del other_nets[k]
1093#1.7: Check external_ports are present at database table datacenter_nets
1094 for k,net in other_nets.items():
1095 error_pos = "'topology':'nodes':'" + k + "'"
1096 if net['external']==False:
1097 if 'name' not in net:
1098 net['name']=k
1099 if 'model' not in net:
tiernof97fd272016-07-11 14:32:37 +02001100 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001101 if net['model']=='bridge_net':
1102 net['type']='bridge';
1103 elif net['model']=='dataplane_net':
1104 net['type']='data';
1105 else:
tiernof97fd272016-07-11 14:32:37 +02001106 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001107 else: #external
1108#IF we do not want to check that external network exist at datacenter
1109 pass
1110#ELSE
1111# error_text = ""
1112# WHERE_={}
1113# if 'net_id' in net:
1114# error_text += " 'net_id' " + net['net_id']
1115# WHERE_['uuid'] = net['net_id']
1116# if 'model' in net:
1117# error_text += " 'model' " + net['model']
1118# WHERE_['name'] = net['model']
1119# if len(WHERE_) == 0:
1120# return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1121# r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1122# FROM='datacenter_nets', WHERE=WHERE_ )
1123# if r<0:
1124# print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1125# elif r==0:
1126# print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1127# return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1128# elif r>1:
1129# print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1130# return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1131# other_nets[k].update(net_db[0])
1132#ENDIF
1133 net_list={}
1134 net_nb=0 #Number of nets
1135 for con in conections_list:
1136 #check if this is connected to a external net
1137 other_net_index=-1
1138 #print
1139 #print "con", con
1140 for index in range(0,len(con)):
1141 #check if this is connected to a external net
1142 for net_key in other_nets.keys():
1143 if con[index][0]==net_key:
1144 if other_net_index>=0:
1145 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 +02001146 #print "nfvo.new_scenario " + error_text
1147 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001148 else:
1149 other_net_index = index
1150 net_target = net_key
1151 break
1152 #print "other_net_index", other_net_index
1153 try:
1154 if other_net_index>=0:
1155 del con[other_net_index]
1156#IF we do not want to check that external network exist at datacenter
1157 if other_nets[net_target]['external'] :
1158 if "name" not in other_nets[net_target]:
1159 other_nets[net_target]['name'] = other_nets[net_target]['model']
1160 if other_nets[net_target]["type"] == "external_network":
1161 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1162 other_nets[net_target]["type"] = "data"
1163 else:
1164 other_nets[net_target]["type"] = "bridge"
1165#ELSE
1166# if other_nets[net_target]['external'] :
1167# 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
1168# if type_=='data' and other_nets[net_target]['type']=="ptp":
1169# error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1170# print "nfvo.new_scenario " + error_text
1171# return -HTTP_Bad_Request, error_text
1172#ENDIF
1173 for iface in con:
1174 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1175 else:
1176 #create a net
1177 net_type_bridge=False
1178 net_type_data=False
1179 net_target = "__-__net"+str(net_nb)
tiernoefd80c92016-09-16 14:17:46 +02001180 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
1181 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
tierno7edb6752016-03-21 17:37:52 +01001182 'external':False}
1183 for iface in con:
1184 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1185 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1186 if iface_type=='mgmt' or iface_type=='bridge':
1187 net_type_bridge = True
1188 else:
1189 net_type_data = True
1190 if net_type_bridge and net_type_data:
1191 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 +02001192 #print "nfvo.new_scenario " + error_text
1193 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001194 elif net_type_bridge:
1195 type_='bridge'
1196 else:
1197 type_='data' if len(con)>2 else 'ptp'
1198 net_list[net_target]['type'] = type_
1199 net_nb+=1
1200 except Exception:
1201 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
tiernof97fd272016-07-11 14:32:37 +02001202 #print "nfvo.new_scenario " + error_text
tierno7edb6752016-03-21 17:37:52 +01001203 #raise e
tiernof97fd272016-07-11 14:32:37 +02001204 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001205
1206#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1207 #1.8.1 obtain management net
tiernof97fd272016-07-11 14:32:37 +02001208 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
tierno7edb6752016-03-21 17:37:52 +01001209 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
1210 #1.8.2 check all interfaces from all vnfs
tiernof97fd272016-07-11 14:32:37 +02001211 if len(mgmt_net)>0:
tierno7edb6752016-03-21 17:37:52 +01001212 add_mgmt_net = False
1213 for vnf in vnfs.values():
1214 for iface in vnf['ifaces'].values():
1215 if iface['type']=='mgmt' and 'net_key' not in iface:
1216 #iface not connected
1217 iface['net_key'] = 'mgmt'
1218 add_mgmt_net = True
1219 if add_mgmt_net and 'mgmt' not in net_list:
1220 net_list['mgmt']=mgmt_net[0]
1221 net_list['mgmt']['external']=True
1222 net_list['mgmt']['graph']={'visible':False}
1223
1224 net_list.update(other_nets)
tiernof97fd272016-07-11 14:32:37 +02001225 #print
1226 #print 'net_list', net_list
1227 #print
1228 #print 'vnfs', vnfs
1229 #print
tierno7edb6752016-03-21 17:37:52 +01001230
1231#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
tiernof97fd272016-07-11 14:32:37 +02001232 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
tierno392f2852016-05-13 12:28:55 +02001233 'tenant_id':tenant_id, 'name':topo['name'],
1234 'description':topo.get('description',topo['name']),
1235 'public': topo.get('public', False)
1236 })
tierno7edb6752016-03-21 17:37:52 +01001237
tiernof97fd272016-07-11 14:32:37 +02001238 return c
tierno7edb6752016-03-21 17:37:52 +01001239
tierno392f2852016-05-13 12:28:55 +02001240def new_scenario_v02(mydb, tenant_id, scenario_dict):
1241 scenario = scenario_dict["scenario"]
tierno7edb6752016-03-21 17:37:52 +01001242 if tenant_id != "any":
tiernof97fd272016-07-11 14:32:37 +02001243 check_tenant(mydb, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01001244 if "tenant_id" in scenario:
1245 if scenario["tenant_id"] != tenant_id:
1246 print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
tiernof97fd272016-07-11 14:32:37 +02001247 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1248 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
tierno7edb6752016-03-21 17:37:52 +01001249 else:
1250 tenant_id=None
1251
1252#1: Check that VNF are present at database table vnfs and update content into scenario dict
1253 for name,vnf in scenario["vnfs"].iteritems():
tiernocea279c2016-07-18 12:36:49 +02001254 where={}
1255 where_or={"tenant_id": tenant_id, 'public': "true"}
tierno7edb6752016-03-21 17:37:52 +01001256 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001257 error_pos = "'scenario':'vnfs':'" + name + "'"
tierno7edb6752016-03-21 17:37:52 +01001258 if 'vnf_id' in vnf:
1259 error_text += " 'vnf_id' " + vnf['vnf_id']
tiernocea279c2016-07-18 12:36:49 +02001260 where['uuid'] = vnf['vnf_id']
tierno392f2852016-05-13 12:28:55 +02001261 if 'vnf_name' in vnf:
1262 error_text += " 'vnf_name' " + vnf['vnf_name']
tiernocea279c2016-07-18 12:36:49 +02001263 where['name'] = vnf['vnf_name']
1264 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001265 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
tiernocea279c2016-07-18 12:36:49 +02001266 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1267 FROM='vnfs',
1268 WHERE=where,
1269 WHERE_OR=where_or,
1270 WHERE_AND_OR="AND")
tiernof97fd272016-07-11 14:32:37 +02001271 if len(vnf_db)==0:
1272 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1273 elif len(vnf_db)>1:
1274 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001275 vnf['uuid']=vnf_db[0]['uuid']
1276 vnf['description']=vnf_db[0]['description']
1277 vnf['ifaces'] = {}
1278 #get external interfaces
tiernof97fd272016-07-11 14:32:37 +02001279 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 +01001280 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1281 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
tierno7edb6752016-03-21 17:37:52 +01001282 for ext_iface in ext_ifaces:
1283 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1284
1285#2: Insert net_key at every vnf interface
1286 for net_name,net in scenario["networks"].iteritems():
1287 net_type_bridge=False
1288 net_type_data=False
1289 for iface_dict in net["interfaces"]:
1290 for vnf,iface in iface_dict.iteritems():
1291 if vnf not in scenario["vnfs"]:
1292 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
tiernof97fd272016-07-11 14:32:37 +02001293 #print "nfvo.new_scenario_v02 " + error_text
1294 raise NfvoException(error_text, HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01001295 if iface not in scenario["vnfs"][vnf]['ifaces']:
1296 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
tiernof97fd272016-07-11 14:32:37 +02001297 #print "nfvo.new_scenario_v02 " + error_text
1298 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001299 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1300 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1301 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
tiernof97fd272016-07-11 14:32:37 +02001302 #print "nfvo.new_scenario_v02 " + error_text
1303 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001304 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1305 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1306 if iface_type=='mgmt' or iface_type=='bridge':
1307 net_type_bridge = True
1308 else:
1309 net_type_data = True
1310 if net_type_bridge and net_type_data:
1311 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
tiernof97fd272016-07-11 14:32:37 +02001312 #print "nfvo.new_scenario " + error_text
1313 raise NfvoException(error_text, HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001314 elif net_type_bridge:
1315 type_='bridge'
1316 else:
1317 type_='data' if len(net["interfaces"])>2 else 'ptp'
1318 net['type'] = type_
1319 net['name'] = net_name
1320 net['external'] = net.get('external', False)
1321
1322#3: insert at database
1323 scenario["nets"] = scenario["networks"]
1324 scenario['tenant_id'] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001325 scenario_id = mydb.new_scenario( scenario)
1326 return scenario_id
tierno7edb6752016-03-21 17:37:52 +01001327
1328def edit_scenario(mydb, tenant_id, scenario_id, data):
1329 data["uuid"] = scenario_id
1330 data["tenant_id"] = tenant_id
tiernof97fd272016-07-11 14:32:37 +02001331 c = mydb.edit_scenario( data )
1332 return c
tierno7edb6752016-03-21 17:37:52 +01001333
1334def 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 +02001335 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00001336 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1337 vims = {datacenter_id: myvim}
tierno392f2852016-05-13 12:28:55 +02001338 myvim_tenant = myvim['tenant_id']
tierno7edb6752016-03-21 17:37:52 +01001339 datacenter_name = myvim['name']
tiernoa2793912016-10-04 08:15:08 +00001340
tierno7edb6752016-03-21 17:37:52 +01001341 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001342 try:
1343 #print "Checking that the scenario_id exists and getting the scenario dictionary"
tiernof97fd272016-07-11 14:32:37 +02001344 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
tiernoa2793912016-10-04 08:15:08 +00001345 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
tiernoae4a8d12016-07-08 12:30:39 +02001346 scenarioDict['datacenter_id'] = datacenter_id
1347 #print '================scenarioDict======================='
1348 #print json.dumps(scenarioDict, indent=4)
1349 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
tierno7edb6752016-03-21 17:37:52 +01001350
tiernoae4a8d12016-07-08 12:30:39 +02001351 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1352 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01001353
tiernoae4a8d12016-07-08 12:30:39 +02001354 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1355 auxNetDict['scenario'] = {}
1356
1357 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1358 for sce_net in scenarioDict['nets']:
1359 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
tierno7edb6752016-03-21 17:37:52 +01001360
tiernoae4a8d12016-07-08 12:30:39 +02001361 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
tierno7edb6752016-03-21 17:37:52 +01001362 myNetName = myNetName[0:255] #limit length
tiernoae4a8d12016-07-08 12:30:39 +02001363 myNetType = sce_net['type']
tierno7edb6752016-03-21 17:37:52 +01001364 myNetDict = {}
1365 myNetDict["name"] = myNetName
1366 myNetDict["type"] = myNetType
1367 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001368 myNetIPProfile = sce_net.get('ip_profile', None)
tierno7edb6752016-03-21 17:37:52 +01001369 #TODO:
tiernoae4a8d12016-07-08 12:30:39 +02001370 #We should use the dictionary as input parameter for new_network
tiernof97fd272016-07-11 14:32:37 +02001371 #print myNetDict
tiernoae4a8d12016-07-08 12:30:39 +02001372 if not sce_net["external"]:
garciadeblas9f8456e2016-09-05 05:02:59 +02001373 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001374 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1375 sce_net['vim_id'] = network_id
1376 auxNetDict['scenario'][sce_net['uuid']] = network_id
1377 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001378 sce_net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001379 else:
1380 if sce_net['vim_id'] == None:
1381 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1382 _, message = rollback(mydb, vims, rollbackList)
1383 logger.error("nfvo.start_scenario: %s", error_text)
tiernof97fd272016-07-11 14:32:37 +02001384 raise NfvoException(error_text, HTTP_Bad_Request)
tiernoae4a8d12016-07-08 12:30:39 +02001385 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1386 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
tierno7edb6752016-03-21 17:37:52 +01001387
tiernoae4a8d12016-07-08 12:30:39 +02001388 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1389 #For each vnf net, we create it and we add it to instanceNetlist.
1390 for sce_vnf in scenarioDict['vnfs']:
1391 for net in sce_vnf['nets']:
1392 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
1393
1394 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1395 myNetName = myNetName[0:255] #limit length
1396 myNetType = net['type']
1397 myNetDict = {}
1398 myNetDict["name"] = myNetName
1399 myNetDict["type"] = myNetType
1400 myNetDict["tenant_id"] = myvim_tenant
garciadeblas9f8456e2016-09-05 05:02:59 +02001401 myNetIPProfile = net.get('ip_profile', None)
tiernoae4a8d12016-07-08 12:30:39 +02001402 #print myNetDict
1403 #TODO:
1404 #We should use the dictionary as input parameter for new_network
garciadeblas9f8456e2016-09-05 05:02:59 +02001405 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
tiernoae4a8d12016-07-08 12:30:39 +02001406 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1407 net['vim_id'] = network_id
1408 if sce_vnf['uuid'] not in auxNetDict:
1409 auxNetDict[sce_vnf['uuid']] = {}
1410 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1411 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001412 net["created"] = True
tiernoae4a8d12016-07-08 12:30:39 +02001413
1414 #print "auxNetDict:"
1415 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
1416
1417 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1418 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1419 i = 0
1420 for sce_vnf in scenarioDict['vnfs']:
1421 for vm in sce_vnf['vms']:
1422 i += 1
1423 myVMDict = {}
1424 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
tiernoae65a482016-11-24 16:20:05 +01001425 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
tiernoae4a8d12016-07-08 12:30:39 +02001426 #myVMDict['description'] = vm['description']
1427 myVMDict['description'] = myVMDict['name'][0:99]
1428 if not startvms:
1429 myVMDict['start'] = "no"
1430 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1431 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
1432
1433 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001434 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
1435 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001436 vm['vim_image_id'] = image_id
1437
1438 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001439 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tiernoae4a8d12016-07-08 12:30:39 +02001440 if flavor_dict['extended']!=None:
1441 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
tiernof97fd272016-07-11 14:32:37 +02001442 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
tiernoae4a8d12016-07-08 12:30:39 +02001443 vm['vim_flavor_id'] = flavor_id
1444
1445
1446 myVMDict['imageRef'] = vm['vim_image_id']
1447 myVMDict['flavorRef'] = vm['vim_flavor_id']
1448 myVMDict['networks'] = []
1449 for iface in vm['interfaces']:
1450 netDict = {}
1451 if iface['type']=="data":
1452 netDict['type'] = iface['model']
1453 elif "model" in iface and iface["model"]!=None:
1454 netDict['model']=iface['model']
1455 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1456 #discover type of interface looking at flavor
1457 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1458 for flavor_iface in numa.get('interfaces',[]):
1459 if flavor_iface.get('name') == iface['internal_name']:
1460 if flavor_iface['dedicated'] == 'yes':
1461 netDict['type']="PF" #passthrough
1462 elif flavor_iface['dedicated'] == 'no':
1463 netDict['type']="VF" #siov
1464 elif flavor_iface['dedicated'] == 'yes:sriov':
1465 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1466 netDict["mac_address"] = flavor_iface.get("mac_address")
1467 break;
1468 netDict["use"]=iface['type']
1469 if netDict["use"]=="data" and not netDict.get("type"):
1470 #print "netDict", netDict
1471 #print "iface", iface
1472 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'])
1473 if flavor_dict.get('extended')==None:
tiernof97fd272016-07-11 14:32:37 +02001474 raise NfvoException(e_text + "After database migration some information is not available. \
1475 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tiernoae4a8d12016-07-08 12:30:39 +02001476 else:
tiernof97fd272016-07-11 14:32:37 +02001477 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tiernoae4a8d12016-07-08 12:30:39 +02001478 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1479 netDict["type"]="virtual"
1480 if "vpci" in iface and iface["vpci"] is not None:
1481 netDict['vpci'] = iface['vpci']
1482 if "mac" in iface and iface["mac"] is not None:
1483 netDict['mac_address'] = iface['mac']
1484 netDict['name'] = iface['internal_name']
1485 if iface['net_id'] is None:
1486 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02001487 #print iface
1488 #print vnf_iface
tiernoae4a8d12016-07-08 12:30:39 +02001489 if vnf_iface['interface_id']==iface['uuid']:
1490 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1491 break
1492 else:
1493 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1494 #skip bridge ifaces not connected to any net
1495 #if 'net_id' not in netDict or netDict['net_id']==None:
1496 # continue
1497 myVMDict['networks'].append(netDict)
1498 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1499 #print myVMDict['name']
1500 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1501 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1502 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1503 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1504 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1505 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1506 vm['vim_id'] = vm_id
1507 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1508 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1509 for net in myVMDict['networks']:
1510 if "vim_id" in net:
1511 for iface in vm['interfaces']:
1512 if net["name"]==iface["internal_name"]:
1513 iface["vim_id"]=net["vim_id"]
1514 break
1515
1516 logger.debug("start scenario Deployment done")
1517 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1518 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
tiernof97fd272016-07-11 14:32:37 +02001519 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1520 return mydb.get_instance_scenario(instance_id)
1521
1522 except (db_base_Exception, vimconn.vimconnException) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001523 _, message = rollback(mydb, vims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02001524 if isinstance(e, db_base_Exception):
1525 error_text = "Exception at database"
1526 else:
1527 error_text = "Exception at VIM"
1528 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1529 #logger.error("start_scenario %s", error_text)
1530 raise NfvoException(error_text, e.http_code)
tierno7edb6752016-03-21 17:37:52 +01001531
tiernoa4e1a6e2016-08-31 14:19:40 +02001532def unify_cloud_config(cloud_config):
1533 index_to_delete = []
1534 users = cloud_config.get("users", [])
1535 for index0 in range(0,len(users)):
1536 if index0 in index_to_delete:
1537 continue
1538 for index1 in range(index0+1,len(users)):
1539 if index1 in index_to_delete:
1540 continue
1541 if users[index0]["name"] == users[index1]["name"]:
1542 index_to_delete.append(index1)
1543 for key in users[index1].get("key-pairs",()):
1544 if "key-pairs" not in users[index0]:
1545 users[index0]["key-pairs"] = [key]
1546 elif key not in users[index0]["key-pairs"]:
1547 users[index0]["key-pairs"].append(key)
1548 index_to_delete.sort(reverse=True)
1549 for index in index_to_delete:
1550 del users[index]
1551
tiernoa2793912016-10-04 08:15:08 +00001552def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
tiernobe41e222016-09-02 15:16:13 +02001553 datacenter_id = None
1554 datacenter_name = None
1555 if datacenter_id_name:
1556 if utils.check_valid_uuid(datacenter_id_name):
1557 datacenter_id = datacenter_id_name
1558 else:
1559 datacenter_name = datacenter_id_name
tiernoa2793912016-10-04 08:15:08 +00001560 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
tiernobe41e222016-09-02 15:16:13 +02001561 if len(vims) == 0:
1562 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1563 elif len(vims)>1:
1564 #print "nfvo.datacenter_action() error. Several datacenters found"
1565 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1566 return vims.keys()[0], vims.values()[0]
1567
garciadeblas9f8456e2016-09-05 05:02:59 +02001568def new_scenario_v03(mydb, tenant_id, scenario_dict):
1569 scenario = scenario_dict["scenario"]
1570 if tenant_id != "any":
1571 check_tenant(mydb, tenant_id)
1572 if "tenant_id" in scenario:
1573 if scenario["tenant_id"] != tenant_id:
1574 logger("Tenant '%s' not found", tenant_id)
1575 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1576 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
1577 else:
1578 tenant_id=None
1579
1580#1: Check that VNF are present at database table vnfs and update content into scenario dict
1581 for name,vnf in scenario["vnfs"].iteritems():
1582 where={}
1583 where_or={"tenant_id": tenant_id, 'public': "true"}
1584 error_text = ""
garciadeblas71781ea2016-09-19 14:41:59 +02001585 error_pos = "'scenario':'vnfs':'" + name + "'"
garciadeblas9f8456e2016-09-05 05:02:59 +02001586 if 'vnf_id' in vnf:
1587 error_text += " 'vnf_id' " + vnf['vnf_id']
1588 where['uuid'] = vnf['vnf_id']
1589 if 'vnf_name' in vnf:
1590 error_text += " 'vnf_name' " + vnf['vnf_name']
1591 where['name'] = vnf['vnf_name']
1592 if len(where) == 0:
garciadeblas71781ea2016-09-19 14:41:59 +02001593 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
garciadeblas9f8456e2016-09-05 05:02:59 +02001594 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1595 FROM='vnfs',
1596 WHERE=where,
1597 WHERE_OR=where_or,
1598 WHERE_AND_OR="AND")
1599 if len(vnf_db)==0:
1600 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1601 elif len(vnf_db)>1:
1602 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1603 vnf['uuid']=vnf_db[0]['uuid']
1604 vnf['description']=vnf_db[0]['description']
1605 vnf['ifaces'] = {}
1606 # get external interfaces
1607 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1608 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1609 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
1610 for ext_iface in ext_ifaces:
1611 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1612
1613 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1614
1615#2: Insert net_key and ip_address at every vnf interface
1616 for net_name,net in scenario["networks"].iteritems():
1617 net_type_bridge=False
1618 net_type_data=False
1619 for iface_dict in net["interfaces"]:
1620 logger.debug("Iface_dict %s", iface_dict)
1621 vnf = iface_dict["vnf"]
1622 iface = iface_dict["vnf_interface"]
1623 if vnf not in scenario["vnfs"]:
1624 error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
1625 #logger.debug(error_text)
1626 raise NfvoException(error_text, HTTP_Not_Found)
1627 if iface not in scenario["vnfs"][vnf]['ifaces']:
1628 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
1629 #logger.debug(error_text)
1630 raise NfvoException(error_text, HTTP_Bad_Request)
1631 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1632 error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
1633 % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1634 #logger.debug(error_text)
1635 raise NfvoException(error_text, HTTP_Bad_Request)
1636 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1637 scenario["vnfs"][vnf]['ifaces'][ iface ]['ip_address'] = iface_dict.get('ip_address',None)
1638 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1639 if iface_type=='mgmt' or iface_type=='bridge':
1640 net_type_bridge = True
1641 else:
1642 net_type_data = True
1643 if net_type_bridge and net_type_data:
1644 error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
1645 #logger.debug(error_text)
1646 raise NfvoException(error_text, HTTP_Bad_Request)
1647 elif net_type_bridge:
1648 type_='bridge'
1649 else:
1650 type_='data' if len(net["interfaces"])>2 else 'ptp'
1651
1652 if ("implementation" in net):
1653 if (type_ == "bridge" and net["implementation"] == "underlay"):
1654 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at 'network':'%s'" % (net_name)
1655 #logger.debug(error_text)
1656 raise NfvoException(error_text, HTTP_Bad_Request)
1657 elif (type_ <> "bridge" and net["implementation"] == "overlay"):
1658 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at 'network':'%s'" % (net_name)
1659 #logger.debug(error_text)
1660 raise NfvoException(error_text, HTTP_Bad_Request)
1661 net.pop("implementation")
1662 if ("type" in net):
1663 if (type_ == "data" and net["type"] == "e-line"):
1664 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type 'e-line' at 'network':'%s'" % (net_name)
1665 #logger.debug(error_text)
1666 raise NfvoException(error_text, HTTP_Bad_Request)
1667 elif (type_ == "ptp" and net["type"] == "e-lan"):
1668 type_ = "data"
1669
1670 net['type'] = type_
1671 net['name'] = net_name
1672 net['external'] = net.get('external', False)
1673
1674#3: insert at database
1675 scenario["nets"] = scenario["networks"]
1676 scenario['tenant_id'] = tenant_id
1677 scenario_id = mydb.new_scenario2(scenario)
1678 return scenario_id
1679
1680def update(d, u):
1681 '''Takes dict d and updates it with the values in dict u.'''
1682 '''It merges all depth levels'''
1683 for k, v in u.iteritems():
1684 if isinstance(v, collections.Mapping):
1685 r = update(d.get(k, {}), v)
1686 d[k] = r
1687 else:
1688 d[k] = u[k]
1689 return d
1690
tierno7edb6752016-03-21 17:37:52 +01001691def create_instance(mydb, tenant_id, instance_dict):
tiernoae4a8d12016-07-08 12:30:39 +02001692 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno4319dad2016-09-05 12:11:11 +02001693 #logger.debug("Creating instance...")
tierno7edb6752016-03-21 17:37:52 +01001694 scenario = instance_dict["scenario"]
tiernobe41e222016-09-02 15:16:13 +02001695
1696 #find main datacenter
1697 myvims = {}
tiernoa2793912016-10-04 08:15:08 +00001698 datacenter2tenant = {}
tierno7edb6752016-03-21 17:37:52 +01001699 datacenter = instance_dict.get("datacenter")
tiernobe41e222016-09-02 15:16:13 +02001700 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1701 myvims[default_datacenter_id] = vim
tiernoa2793912016-10-04 08:15:08 +00001702 datacenter2tenant[default_datacenter_id] = vim['config']['datacenter_tenant_id']
tierno392f2852016-05-13 12:28:55 +02001703 #myvim_tenant = myvim['tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001704# default_datacenter_name = vim['name']
tierno7edb6752016-03-21 17:37:52 +01001705 rollbackList=[]
tiernoae4a8d12016-07-08 12:30:39 +02001706
1707 #print "Checking that the scenario exists and getting the scenario dictionary"
tiernobe41e222016-09-02 15:16:13 +02001708 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
garciadeblas9f8456e2016-09-05 05:02:59 +02001709
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001710 #logger.debug(">>>>>>> Dictionaries before merging")
1711 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1712 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
garciadeblas9f8456e2016-09-05 05:02:59 +02001713
tiernobe41e222016-09-02 15:16:13 +02001714 scenarioDict['datacenter_id'] = default_datacenter_id
garciadeblas9f8456e2016-09-05 05:02:59 +02001715
tierno7edb6752016-03-21 17:37:52 +01001716 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1717 auxNetDict['scenario'] = {}
1718
tierno4319dad2016-09-05 12:11:11 +02001719 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 +01001720 instance_name = instance_dict["name"]
1721 instance_description = instance_dict.get("description")
1722 try:
1723 #0 check correct parameters
tiernobe41e222016-09-02 15:16:13 +02001724 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001725 found=False
1726 for scenario_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001727 if net_name == scenario_net["name"]:
tierno7edb6752016-03-21 17:37:52 +01001728 found = True
1729 break
1730 if not found:
tiernobe41e222016-09-02 15:16:13 +02001731 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
1732 if "sites" not in net_instance_desc:
1733 net_instance_desc["sites"] = [ {} ]
1734 site_without_datacenter_field = False
1735 for site in net_instance_desc["sites"]:
1736 if site.get("datacenter"):
1737 if site["datacenter"] not in myvims:
1738 #Add this datacenter to myvims
1739 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
1740 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001741 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
tiernobe41e222016-09-02 15:16:13 +02001742 site["datacenter"] = d #change name to id
1743 else:
1744 if site_without_datacenter_field:
1745 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
1746 site_without_datacenter_field = True
1747 site["datacenter"] = default_datacenter_id #change name to id
1748
1749 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
tierno7edb6752016-03-21 17:37:52 +01001750 found=False
1751 for scenario_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02001752 if vnf_name == scenario_vnf['name']:
tierno7edb6752016-03-21 17:37:52 +01001753 found = True
1754 break
1755 if not found:
tiernobe41e222016-09-02 15:16:13 +02001756 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
1757 if "datacenter" in vnf_instance_desc:
1758 #Add this datacenter to myvims
1759 if vnf_instance_desc["datacenter"] not in myvims:
1760 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
1761 myvims[d] = v
tiernoa2793912016-10-04 08:15:08 +00001762 datacenter2tenant[d] = v['config']['datacenter_tenant_id']
1763 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
tiernoa4e1a6e2016-08-31 14:19:40 +02001764 #0.1 parse cloud-config parameters
1765 cloud_config = scenarioDict.get("cloud-config", {})
1766 if instance_dict.get("cloud-config"):
1767 cloud_config.update( instance_dict["cloud-config"])
1768 if not cloud_config:
1769 cloud_config = None
1770 else:
1771 scenarioDict["cloud-config"] = cloud_config
1772 unify_cloud_config(cloud_config)
garciadeblas9f8456e2016-09-05 05:02:59 +02001773
1774 #0.2 merge instance information into scenario
1775 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
1776 #However, this is not possible yet.
1777 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
1778 for scenario_net in scenarioDict['nets']:
1779 if net_name == scenario_net["name"]:
1780 if 'ip-profile' in net_instance_desc:
1781 ipprofile = net_instance_desc['ip-profile']
1782 ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None)
1783 ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4')
1784 ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None)
1785 ipprofile['dns_address'] = ipprofile.pop('dns-address',None)
1786 if 'dhcp' in ipprofile:
1787 ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None)
1788 ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True)
1789 ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None)
1790 del ipprofile['dhcp']
garciadeblasedca7b32016-09-29 14:01:52 +00001791 if 'ip_profile' not in scenario_net:
1792 scenario_net['ip_profile'] = ipprofile
1793 else:
1794 update(scenario_net['ip_profile'],ipprofile)
tiernoe6c58ce2016-09-14 16:02:49 +02001795 for interface in net_instance_desc.get('interfaces', () ):
garciadeblas9f8456e2016-09-05 05:02:59 +02001796 if 'ip_address' in interface:
1797 for vnf in scenarioDict['vnfs']:
1798 if interface['vnf'] == vnf['name']:
1799 for vnf_interface in vnf['interfaces']:
1800 if interface['vnf_interface'] == vnf_interface['external_name']:
1801 vnf_interface['ip_address']=interface['ip_address']
1802
garciadeblasbb6a1ed2016-09-30 14:02:09 +00001803 #logger.debug(">>>>>>>> Merged dictionary")
tierno4319dad2016-09-05 12:11:11 +02001804 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 +02001805
tierno7edb6752016-03-21 17:37:52 +01001806
1807 #1. Creating new nets (sce_nets) in the VIM"
1808 for sce_net in scenarioDict['nets']:
tiernobe41e222016-09-02 15:16:13 +02001809 sce_net["vim_id_sites"]={}
tierno7edb6752016-03-21 17:37:52 +01001810 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
tiernobe41e222016-09-02 15:16:13 +02001811 net_name = descriptor_net.get("vim-network-name")
1812 auxNetDict['scenario'][sce_net['uuid']] = {}
1813
1814 sites = descriptor_net.get("sites", [ {} ])
1815 for site in sites:
1816 if site.get("datacenter"):
1817 vim = myvims[ site["datacenter"] ]
1818 datacenter_id = site["datacenter"]
tierno7edb6752016-03-21 17:37:52 +01001819 else:
tiernobe41e222016-09-02 15:16:13 +02001820 vim = myvims[ default_datacenter_id ]
1821 datacenter_id = default_datacenter_id
tiernobe41e222016-09-02 15:16:13 +02001822 net_type = sce_net['type']
1823 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
1824 if sce_net["external"]:
1825 if not net_name:
1826 net_name = sce_net["name"]
1827 if "netmap-use" in site or "netmap-create" in site:
1828 create_network = False
1829 lookfor_network = False
1830 if "netmap-use" in site:
1831 lookfor_network = True
1832 if utils.check_valid_uuid(site["netmap-use"]):
1833 filter_text = "scenario id '%s'" % site["netmap-use"]
1834 lookfor_filter["id"] = site["netmap-use"]
1835 else:
1836 filter_text = "scenario name '%s'" % site["netmap-use"]
1837 lookfor_filter["name"] = site["netmap-use"]
1838 if "netmap-create" in site:
1839 create_network = True
1840 net_vim_name = net_name
1841 if site["netmap-create"]:
1842 net_vim_name = site["netmap-create"]
1843
1844 elif sce_net['vim_id'] != None:
1845 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
1846 create_network = False
1847 lookfor_network = True
1848 lookfor_filter["id"] = sce_net['vim_id']
1849 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
1850 #look for network at datacenter and return error
1851 else:
1852 #There is not a netmap, look at datacenter for a net with this name and create if not found
1853 create_network = True
1854 lookfor_network = True
1855 lookfor_filter["name"] = sce_net["name"]
1856 net_vim_name = sce_net["name"]
1857 filter_text = "scenario name '%s'" % sce_net["name"]
tierno7edb6752016-03-21 17:37:52 +01001858 else:
tiernobe41e222016-09-02 15:16:13 +02001859 if not net_name:
1860 net_name = "%s.%s" %(instance_name, sce_net["name"])
1861 net_name = net_name[:255] #limit length
1862 net_vim_name = net_name
1863 create_network = True
1864 lookfor_network = False
1865
1866 if lookfor_network:
1867 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
1868 if len(vim_nets) > 1:
1869 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1870 elif len(vim_nets) == 0:
1871 if not create_network:
1872 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
1873 else:
1874 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
tiernobe41e222016-09-02 15:16:13 +02001875 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
1876 create_network = False
1877 if create_network:
1878 #if network is not external
garciadeblas9f8456e2016-09-05 05:02:59 +02001879 network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
tiernobe41e222016-09-02 15:16:13 +02001880 sce_net["vim_id_sites"][datacenter_id] = network_id
1881 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = network_id
1882 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001883 sce_net["created"] = True
tierno7edb6752016-03-21 17:37:52 +01001884
1885 #2. Creating new nets (vnf internal nets) in the VIM"
1886 #For each vnf net, we create it and we add it to instanceNetlist.
1887 for sce_vnf in scenarioDict['vnfs']:
1888 for net in sce_vnf['nets']:
tiernobe41e222016-09-02 15:16:13 +02001889 if sce_vnf.get("datacenter"):
1890 vim = myvims[ sce_vnf["datacenter"] ]
1891 datacenter_id = sce_vnf["datacenter"]
1892 else:
1893 vim = myvims[ default_datacenter_id ]
1894 datacenter_id = default_datacenter_id
tierno7edb6752016-03-21 17:37:52 +01001895 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
1896 net_name = descriptor_net.get("name")
1897 if not net_name:
1898 net_name = "%s.%s" %(instance_name, net["name"])
1899 net_name = net_name[:255] #limit length
1900 net_type = net['type']
garciadeblas9f8456e2016-09-05 05:02:59 +02001901 network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
tierno7edb6752016-03-21 17:37:52 +01001902 net['vim_id'] = network_id
1903 if sce_vnf['uuid'] not in auxNetDict:
1904 auxNetDict[sce_vnf['uuid']] = {}
1905 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1906 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
tierno66345bc2016-09-26 11:37:55 +02001907 net["created"] = True
1908
tierno7edb6752016-03-21 17:37:52 +01001909
tiernoae4a8d12016-07-08 12:30:39 +02001910 #print "auxNetDict:"
1911 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01001912
1913 #3. Creating new vm instances in the VIM
tiernoae4a8d12016-07-08 12:30:39 +02001914 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
tierno7edb6752016-03-21 17:37:52 +01001915 for sce_vnf in scenarioDict['vnfs']:
tiernobe41e222016-09-02 15:16:13 +02001916 if sce_vnf.get("datacenter"):
1917 vim = myvims[ sce_vnf["datacenter"] ]
1918 datacenter_id = sce_vnf["datacenter"]
1919 else:
1920 vim = myvims[ default_datacenter_id ]
1921 datacenter_id = default_datacenter_id
1922 sce_vnf["datacenter_id"] = datacenter_id
tierno7edb6752016-03-21 17:37:52 +01001923 i = 0
1924 for vm in sce_vnf['vms']:
1925 i += 1
1926 myVMDict = {}
tiernoae65a482016-11-24 16:20:05 +01001927 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
tierno7edb6752016-03-21 17:37:52 +01001928 myVMDict['description'] = myVMDict['name'][0:99]
1929# if not startvms:
1930# myVMDict['start'] = "no"
1931 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1932 #create image at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001933 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
tierno5e91eb82016-10-04 09:39:07 +00001934 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
tierno7edb6752016-03-21 17:37:52 +01001935 vm['vim_image_id'] = image_id
1936
1937 #create flavor at vim in case it not exist
tiernof97fd272016-07-11 14:32:37 +02001938 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
tierno7edb6752016-03-21 17:37:52 +01001939 if flavor_dict['extended']!=None:
1940 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
montesmoreno0c8def02016-12-22 12:16:23 +00001941 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
1942
1943
1944
1945
1946 #Obtain information for additional disks
1947 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
1948 if not extended_flavor_dict:
1949 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
1950 return
1951
1952 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
1953 myVMDict['disks'] = None
1954 extended_info = extended_flavor_dict[0]['extended']
1955 if extended_info != None:
1956 extended_flavor_dict_yaml = yaml.load(extended_info)
1957 if 'disks' in extended_flavor_dict_yaml:
1958 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
1959
1960
1961
1962
tierno7edb6752016-03-21 17:37:52 +01001963 vm['vim_flavor_id'] = flavor_id
1964
1965 myVMDict['imageRef'] = vm['vim_image_id']
1966 myVMDict['flavorRef'] = vm['vim_flavor_id']
1967 myVMDict['networks'] = []
tiernoa2793912016-10-04 08:15:08 +00001968 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
tierno7edb6752016-03-21 17:37:52 +01001969 for iface in vm['interfaces']:
1970 netDict = {}
1971 if iface['type']=="data":
1972 netDict['type'] = iface['model']
1973 elif "model" in iface and iface["model"]!=None:
1974 netDict['model']=iface['model']
1975 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1976 #discover type of interface looking at flavor
1977 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1978 for flavor_iface in numa.get('interfaces',[]):
1979 if flavor_iface.get('name') == iface['internal_name']:
1980 if flavor_iface['dedicated'] == 'yes':
1981 netDict['type']="PF" #passthrough
1982 elif flavor_iface['dedicated'] == 'no':
1983 netDict['type']="VF" #siov
1984 elif flavor_iface['dedicated'] == 'yes:sriov':
1985 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1986 netDict["mac_address"] = flavor_iface.get("mac_address")
1987 break;
1988 netDict["use"]=iface['type']
1989 if netDict["use"]=="data" and not netDict.get("type"):
1990 #print "netDict", netDict
1991 #print "iface", iface
1992 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'])
1993 if flavor_dict.get('extended')==None:
tiernoae4a8d12016-07-08 12:30:39 +02001994 raise NfvoException(e_text + "After database migration some information is not available. \
1995 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01001996 else:
tiernoae4a8d12016-07-08 12:30:39 +02001997 raise NfvoException(e_text, HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01001998 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1999 netDict["type"]="virtual"
2000 if "vpci" in iface and iface["vpci"] is not None:
2001 netDict['vpci'] = iface['vpci']
2002 if "mac" in iface and iface["mac"] is not None:
2003 netDict['mac_address'] = iface['mac']
2004 netDict['name'] = iface['internal_name']
2005 if iface['net_id'] is None:
2006 for vnf_iface in sce_vnf["interfaces"]:
tiernof97fd272016-07-11 14:32:37 +02002007 #print iface
2008 #print vnf_iface
tierno7edb6752016-03-21 17:37:52 +01002009 if vnf_iface['interface_id']==iface['uuid']:
tiernobe41e222016-09-02 15:16:13 +02002010 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
tierno7edb6752016-03-21 17:37:52 +01002011 break
2012 else:
2013 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2014 #skip bridge ifaces not connected to any net
2015 #if 'net_id' not in netDict or netDict['net_id']==None:
2016 # continue
2017 myVMDict['networks'].append(netDict)
tiernoae4a8d12016-07-08 12:30:39 +02002018 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2019 #print myVMDict['name']
2020 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2021 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2022 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
tiernobe41e222016-09-02 15:16:13 +02002023 vm_id = vim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
montesmoreno0c8def02016-12-22 12:16:23 +00002024 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'], cloud_config = cloud_config,
2025 disk_list = myVMDict['disks'])
2026
tierno7edb6752016-03-21 17:37:52 +01002027 vm['vim_id'] = vm_id
2028 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2029 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2030 for net in myVMDict['networks']:
2031 if "vim_id" in net:
2032 for iface in vm['interfaces']:
2033 if net["name"]==iface["internal_name"]:
2034 iface["vim_id"]=net["vim_id"]
2035 break
tiernoa2793912016-10-04 08:15:08 +00002036 scenarioDict["datacenter2tenant"] = datacenter2tenant
2037 logger.debug("create_instance Deployment done scenarioDict: %s",
2038 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
tiernof97fd272016-07-11 14:32:37 +02002039 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
2040 return mydb.get_instance_scenario(instance_id)
2041 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
tiernobe41e222016-09-02 15:16:13 +02002042 message = rollback(mydb, myvims, rollbackList)
tiernof97fd272016-07-11 14:32:37 +02002043 if isinstance(e, db_base_Exception):
2044 error_text = "database Exception"
2045 elif isinstance(e, vimconn.vimconnException):
2046 error_text = "VIM Exception"
2047 else:
2048 error_text = "Exception"
2049 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2050 #logger.error("create_instance: %s", error_text)
2051 raise NfvoException(error_text, e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002052
tierno7edb6752016-03-21 17:37:52 +01002053def delete_instance(mydb, tenant_id, instance_id):
tiernoae4a8d12016-07-08 12:30:39 +02002054 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002055 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +02002056 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
tierno7edb6752016-03-21 17:37:52 +01002057 tenant_id = instanceDict["tenant_id"]
tiernoae4a8d12016-07-08 12:30:39 +02002058 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tierno7edb6752016-03-21 17:37:52 +01002059
tiernoa2793912016-10-04 08:15:08 +00002060 #1. Delete from Database
tiernof97fd272016-07-11 14:32:37 +02002061 message = mydb.delete_instance_scenario(instance_id, tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002062
2063 #2. delete from VIM
tiernoa2793912016-10-04 08:15:08 +00002064 error_msg = ""
2065 myvims={}
tierno7edb6752016-03-21 17:37:52 +01002066
2067 #2.1 deleting VMs
2068 #vm_fail_list=[]
2069 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002070 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2071 if datacenter_key not in myvims:
2072 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2073 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2074 if len(vims) == 0:
2075 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2076 sce_vnf["datacenter_tenant_id"]))
2077 myvims[datacenter_key] = None
2078 else:
2079 myvims[datacenter_key] = vims.values()[0]
2080 myvim = myvims[datacenter_key]
tierno7edb6752016-03-21 17:37:52 +01002081 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002082 if not myvim:
2083 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2084 continue
tiernoae4a8d12016-07-08 12:30:39 +02002085 try:
2086 myvim.delete_vminstance(vm['vim_vm_id'])
2087 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002088 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 +02002089 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2090 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2091 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002092 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2093 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
tiernoae4a8d12016-07-08 12:30:39 +02002094 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
tierno7edb6752016-03-21 17:37:52 +01002095
2096 #2.2 deleting NETS
2097 #net_fail_list=[]
2098 for net in instanceDict['nets']:
tierno66345bc2016-09-26 11:37:55 +02002099 if not net['created']:
tierno7edb6752016-03-21 17:37:52 +01002100 continue #skip not created nets
tiernoa2793912016-10-04 08:15:08 +00002101 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2102 if datacenter_key not in myvims:
2103 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2104 datacenter_tenant_id=net["datacenter_tenant_id"])
2105 if len(vims) == 0:
2106 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2107 myvims[datacenter_key] = None
2108 else:
2109 myvims[datacenter_key] = vims.values()[0]
2110 myvim = myvims[datacenter_key]
2111
tierno7edb6752016-03-21 17:37:52 +01002112 if not myvim:
tiernoa2793912016-10-04 08:15:08 +00002113 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 +01002114 continue
tiernoae4a8d12016-07-08 12:30:39 +02002115 try:
2116 myvim.delete_network(net['vim_net_id'])
2117 except vimconn.vimconnNotFoundException as e:
tiernoa2793912016-10-04 08:15:08 +00002118 error_msg+="\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
2119 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
2120 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
tiernoae4a8d12016-07-08 12:30:39 +02002121 except vimconn.vimconnException as e:
tiernoa2793912016-10-04 08:15:08 +00002122 error_msg+="\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'], net["datacenter_id"], e.http_code, str(e))
2123 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
2124 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
tierno7edb6752016-03-21 17:37:52 +01002125 if len(error_msg)>0:
tiernof97fd272016-07-11 14:32:37 +02002126 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 +01002127 else:
tiernof97fd272016-07-11 14:32:37 +02002128 return 'instance ' + message + ' deleted'
tierno7edb6752016-03-21 17:37:52 +01002129
2130def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2131 '''Refreshes a scenario instance. It modifies instanceDict'''
2132 '''Returns:
2133 - 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
2134 - error_msg
2135 '''
2136 # Assumption: nfvo_tenant and instance_id were checked before entering into this function
tiernoae4a8d12016-07-08 12:30:39 +02002137 #print "nfvo.refresh_instance begins"
tierno7edb6752016-03-21 17:37:52 +01002138 #print json.dumps(instanceDict, indent=4)
2139
tiernoae4a8d12016-07-08 12:30:39 +02002140 #print "Getting the VIM URL and the VIM tenant_id"
tiernoa2793912016-10-04 08:15:08 +00002141 myvims={}
2142
tiernoae4a8d12016-07-08 12:30:39 +02002143 # 1. Getting VIM vm and net list
tierno7edb6752016-03-21 17:37:52 +01002144 vms_updated = [] #List of VM instance uuids in openmano that were updated
2145 vms_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002146 vm_list = {}
tierno7edb6752016-03-21 17:37:52 +01002147 for sce_vnf in instanceDict['vnfs']:
tiernoa2793912016-10-04 08:15:08 +00002148 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2149 if datacenter_key not in vm_list:
2150 vm_list[datacenter_key] = []
2151 if datacenter_key not in myvims:
2152 vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2153 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2154 if len(vims) == 0:
2155 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2156 myvims[datacenter_key] = None
2157 else:
2158 myvims[datacenter_key] = vims.values()[0]
tierno7edb6752016-03-21 17:37:52 +01002159 for vm in sce_vnf['vms']:
tiernoa2793912016-10-04 08:15:08 +00002160 vm_list[datacenter_key].append(vm['vim_vm_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002161 vms_notupdated.append(vm["uuid"])
2162
2163 nets_updated = [] #List of VM instance uuids in openmano that were updated
tierno7edb6752016-03-21 17:37:52 +01002164 nets_notupdated=[]
tiernoa2793912016-10-04 08:15:08 +00002165 net_list = {}
tierno7edb6752016-03-21 17:37:52 +01002166 for net in instanceDict['nets']:
tiernoa2793912016-10-04 08:15:08 +00002167 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2168 if datacenter_key not in net_list:
2169 net_list[datacenter_key] = []
2170 if datacenter_key not in myvims:
2171 vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2172 datacenter_tenant_id=net["datacenter_tenant_id"])
2173 if len(vims) == 0:
2174 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2175 myvims[datacenter_key] = None
2176 else:
2177 myvims[datacenter_key] = vims.values()[0]
2178
2179 net_list[datacenter_key].append(net['vim_net_id'])
tiernoae4a8d12016-07-08 12:30:39 +02002180 nets_notupdated.append(net["uuid"])
2181
tiernoa2793912016-10-04 08:15:08 +00002182 # 1. Getting the status of all VMs
2183 vm_dict={}
2184 for datacenter_key in myvims:
2185 if not vm_list.get(datacenter_key):
2186 continue
2187 failed = True
2188 failed_message=""
2189 if not myvims[datacenter_key]:
2190 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2191 else:
2192 try:
2193 vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2194 failed = False
2195 except vimconn.vimconnException as e:
2196 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2197 failed_message = str(e)
2198 if failed:
2199 for vm in vm_list[datacenter_key]:
2200 vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
tiernoae4a8d12016-07-08 12:30:39 +02002201
tiernoa2793912016-10-04 08:15:08 +00002202 # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2203 for sce_vnf in instanceDict['vnfs']:
2204 for vm in sce_vnf['vms']:
2205 vm_id = vm['vim_vm_id']
2206 interfaces = vm_dict[vm_id].pop('interfaces', [])
2207 #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2208 has_mgmt_iface = False
2209 for iface in vm["interfaces"]:
2210 if iface["type"]=="mgmt":
2211 has_mgmt_iface = True
2212 if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2213 vm_dict[vm_id]['status'] = "ACTIVE"
tiernoa3d49e62016-10-05 15:20:26 +00002214 if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2215 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 +00002216 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'):
2217 vm['status'] = vm_dict[vm_id]['status']
2218 vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2219 vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2220 # 2.1. Update in openmano DB the VMs whose status changed
tiernof97fd272016-07-11 14:32:37 +02002221 try:
tiernoa2793912016-10-04 08:15:08 +00002222 updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2223 vms_notupdated.remove(vm["uuid"])
2224 if updates>0:
2225 vms_updated.append(vm["uuid"])
tiernof97fd272016-07-11 14:32:37 +02002226 except db_base_Exception as e:
2227 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tiernoa2793912016-10-04 08:15:08 +00002228 # 2.2. Update in openmano DB the interface VMs
2229 for interface in interfaces:
2230 #translate from vim_net_id to instance_net_id
2231 network_id_list=[]
2232 for net in instanceDict['nets']:
2233 if net["vim_net_id"] == interface["vim_net_id"]:
2234 network_id_list.append(net["uuid"])
2235 if not network_id_list:
2236 continue
2237 del interface["vim_net_id"]
2238 try:
2239 for network_id in network_id_list:
2240 mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2241 except db_base_Exception as e:
2242 logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2243
2244 # 3. Getting the status of all nets
2245 net_dict = {}
2246 for datacenter_key in myvims:
2247 if not net_list.get(datacenter_key):
2248 continue
2249 failed = True
2250 failed_message = ""
2251 if not myvims[datacenter_key]:
2252 failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2253 else:
2254 try:
2255 net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2256 failed = False
2257 except vimconn.vimconnException as e:
2258 logger.error("VIM exception %s %s", type(e).__name__, str(e))
2259 failed_message = str(e)
2260 if failed:
2261 for net in net_list[datacenter_key]:
2262 net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2263
2264 # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2265 # TODO: update nets inside a vnf
2266 for net in instanceDict['nets']:
2267 net_id = net['vim_net_id']
tiernoa3d49e62016-10-05 15:20:26 +00002268 if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2269 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 +00002270 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'):
2271 net['status'] = net_dict[net_id]['status']
2272 net['error_msg'] = net_dict[net_id].get('error_msg')
2273 net['vim_info'] = net_dict[net_id].get('vim_info')
2274 # 5.1. Update in openmano DB the nets whose status changed
2275 try:
2276 updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2277 nets_notupdated.remove(net["uuid"])
2278 if updated>0:
2279 nets_updated.append(net["uuid"])
2280 except db_base_Exception as e:
2281 logger.error("nfvo.refresh_instance error database update: %s", str(e))
tierno7edb6752016-03-21 17:37:52 +01002282
2283 # Returns appropriate output
tiernoae4a8d12016-07-08 12:30:39 +02002284 #print "nfvo.refresh_instance finishes"
2285 logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2286 str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
tierno7edb6752016-03-21 17:37:52 +01002287 instance_id = instanceDict['uuid']
tierno7edb6752016-03-21 17:37:52 +01002288 if len(vms_notupdated)+len(nets_notupdated)>0:
tiernoae4a8d12016-07-08 12:30:39 +02002289 error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
tierno7edb6752016-03-21 17:37:52 +01002290 return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
2291
tiernoae4a8d12016-07-08 12:30:39 +02002292 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
tierno7edb6752016-03-21 17:37:52 +01002293
2294def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
tiernoae4a8d12016-07-08 12:30:39 +02002295 #print "Checking that the instance_id exists and getting the instance dictionary"
tiernof97fd272016-07-11 14:32:37 +02002296 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002297 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2298
tiernoae4a8d12016-07-08 12:30:39 +02002299 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
tiernof97fd272016-07-11 14:32:37 +02002300 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2301 if len(vims) == 0:
2302 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002303 myvim = vims.values()[0]
2304
2305
2306 input_vnfs = action_dict.pop("vnfs", [])
2307 input_vms = action_dict.pop("vms", [])
2308 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2309 vm_result = {}
2310 vm_error = 0
2311 vm_ok = 0
2312 for sce_vnf in instanceDict['vnfs']:
2313 for vm in sce_vnf['vms']:
2314 if not action_over_all:
2315 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2316 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2317 continue
tiernoae4a8d12016-07-08 12:30:39 +02002318 try:
2319 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
tierno7edb6752016-03-21 17:37:52 +01002320 if "console" in action_dict:
tierno20fc2a22016-08-19 17:02:35 +02002321 if not global_config["http_console_proxy"]:
2322 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2323 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2324 protocol=data["protocol"],
2325 ip = data["server"],
2326 port = data["port"],
2327 suffix = data["suffix"]),
2328 "name":vm['name']
2329 }
2330 vm_ok +=1
2331 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
tierno7edb6752016-03-21 17:37:52 +01002332 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2333 "description": "this console is only reachable by local interface",
2334 "name":vm['name']
2335 }
2336 vm_error+=1
tierno20fc2a22016-08-19 17:02:35 +02002337 else:
tierno7edb6752016-03-21 17:37:52 +01002338 #print "console data", data
tierno20fc2a22016-08-19 17:02:35 +02002339 try:
2340 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2341 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2342 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2343 protocol=data["protocol"],
2344 ip = global_config["http_console_host"],
2345 port = console_thread.port,
2346 suffix = data["suffix"]),
2347 "name":vm['name']
2348 }
2349 vm_ok +=1
2350 except NfvoException as e:
2351 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2352 vm_error+=1
2353
tierno7edb6752016-03-21 17:37:52 +01002354 else:
tiernof97fd272016-07-11 14:32:37 +02002355 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
tierno7edb6752016-03-21 17:37:52 +01002356 vm_ok +=1
tiernoae4a8d12016-07-08 12:30:39 +02002357 except vimconn.vimconnException as e:
2358 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2359 vm_error+=1
tierno7edb6752016-03-21 17:37:52 +01002360
2361 if vm_ok==0: #all goes wrong
tierno351863c2016-07-23 01:46:03 +02002362 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002363 else:
tierno351863c2016-07-23 01:46:03 +02002364 return vm_result
tierno7edb6752016-03-21 17:37:52 +01002365
2366def create_or_use_console_proxy_thread(console_server, console_port):
2367 #look for a non-used port
2368 console_thread_key = console_server + ":" + str(console_port)
2369 if console_thread_key in global_config["console_thread"]:
2370 #global_config["console_thread"][console_thread_key].start_timeout()
tiernof97fd272016-07-11 14:32:37 +02002371 return global_config["console_thread"][console_thread_key]
tierno7edb6752016-03-21 17:37:52 +01002372
2373 for port in global_config["console_port_iterator"]():
tierno20fc2a22016-08-19 17:02:35 +02002374 #print "create_or_use_console_proxy_thread() port:", port
tierno7edb6752016-03-21 17:37:52 +01002375 if port in global_config["console_ports"]:
2376 continue
2377 try:
2378 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2379 clithread.start()
2380 global_config["console_thread"][console_thread_key] = clithread
2381 global_config["console_ports"][port] = console_thread_key
tiernof97fd272016-07-11 14:32:37 +02002382 return clithread
tierno7edb6752016-03-21 17:37:52 +01002383 except cli.ConsoleProxyExceptionPortUsed as e:
2384 #port used, try with onoher
2385 continue
2386 except cli.ConsoleProxyException as e:
tiernof97fd272016-07-11 14:32:37 +02002387 raise NfvoException(str(e), HTTP_Bad_Request)
2388 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002389
2390def check_tenant(mydb, tenant_id):
2391 '''check that tenant exists at database'''
tiernof97fd272016-07-11 14:32:37 +02002392 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2393 if not tenant:
2394 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2395 return
tierno7edb6752016-03-21 17:37:52 +01002396
2397def new_tenant(mydb, tenant_dict):
tiernof97fd272016-07-11 14:32:37 +02002398 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2399 return tenant_id
tierno7edb6752016-03-21 17:37:52 +01002400
2401def delete_tenant(mydb, tenant):
2402 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002403
2404 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2405 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2406 return tenant_dict['uuid'] + " " + tenant_dict["name"]
tierno7edb6752016-03-21 17:37:52 +01002407
2408def new_datacenter(mydb, datacenter_descriptor):
2409 if "config" in datacenter_descriptor:
2410 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
tierno3ae39742016-09-07 12:17:51 +02002411 #Check that datacenter-type is correct
2412 datacenter_type = datacenter_descriptor.get("type", "openvim");
2413 module_info = None
2414 try:
2415 module = "vimconn_" + datacenter_type
2416 module_info = imp.find_module(module)
2417 except (IOError, ImportError):
2418 if module_info and module_info[0]:
2419 file.close(module_info[0])
2420 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
2421
tiernof97fd272016-07-11 14:32:37 +02002422 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2423 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002424
2425def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2426 #obtain data, check that only one exist
tiernof97fd272016-07-11 14:32:37 +02002427 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
tierno7edb6752016-03-21 17:37:52 +01002428 #edit data
tiernof97fd272016-07-11 14:32:37 +02002429 datacenter_id = datacenter['uuid']
2430 where={'uuid': datacenter['uuid']}
tierno7edb6752016-03-21 17:37:52 +01002431 if "config" in datacenter_descriptor:
2432 if datacenter_descriptor['config']!=None:
2433 try:
2434 new_config_dict = datacenter_descriptor["config"]
2435 #delete null fields
2436 to_delete=[]
2437 for k in new_config_dict:
2438 if new_config_dict[k]==None:
2439 to_delete.append(k)
2440
tiernof97fd272016-07-11 14:32:37 +02002441 config_dict = yaml.load(datacenter["config"])
tierno7edb6752016-03-21 17:37:52 +01002442 config_dict.update(new_config_dict)
2443 #delete null fields
2444 for k in to_delete:
2445 del config_dict[k]
tiernof97fd272016-07-11 14:32:37 +02002446 except Exception as e:
2447 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002448 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 +02002449 mydb.update_rows('datacenters', datacenter_descriptor, where)
2450 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002451
2452def delete_datacenter(mydb, datacenter):
2453 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002454 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2455 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2456 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
tierno7edb6752016-03-21 17:37:52 +01002457
tierno8008c3a2016-10-13 15:34:28 +00002458def 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 +01002459 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002460 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002461 datacenter_name=myvim["name"]
2462
2463 create_vim_tenant=True if vim_tenant_id==None and vim_tenant_name==None else False
2464
2465 #get nfvo_tenant info
tiernof97fd272016-07-11 14:32:37 +02002466 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
tierno7edb6752016-03-21 17:37:52 +01002467 if vim_tenant_name==None:
2468 vim_tenant_name=tenant_dict['name']
2469
2470 #check that this association does not exist before
2471 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
tiernof97fd272016-07-11 14:32:37 +02002472 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2473 if len(tenants_datacenters)>0:
2474 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
tierno7edb6752016-03-21 17:37:52 +01002475
2476 vim_tenant_id_exist_atdb=False
2477 if not create_vim_tenant:
2478 where_={"datacenter_id": datacenter_id}
2479 if vim_tenant_id!=None:
2480 where_["vim_tenant_id"] = vim_tenant_id
2481 if vim_tenant_name!=None:
2482 where_["vim_tenant_name"] = vim_tenant_name
2483 #check if vim_tenant_id is already at database
tiernof97fd272016-07-11 14:32:37 +02002484 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2485 if len(datacenter_tenants_dict)>=1:
tierno7edb6752016-03-21 17:37:52 +01002486 datacenter_tenants_dict = datacenter_tenants_dict[0]
2487 vim_tenant_id_exist_atdb=True
2488 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2489 else: #result=0
2490 datacenter_tenants_dict = {}
2491 #insert at table datacenter_tenants
2492 else: #if vim_tenant_id==None:
2493 #create tenant at VIM if not provided
tiernoae4a8d12016-07-08 12:30:39 +02002494 try:
2495 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2496 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002497 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 +01002498 datacenter_tenants_dict = {}
2499 datacenter_tenants_dict["created"]="true"
2500
2501 #fill datacenter_tenants table
2502 if not vim_tenant_id_exist_atdb:
2503 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
2504 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
2505 datacenter_tenants_dict["user"] = vim_username
2506 datacenter_tenants_dict["passwd"] = vim_password
2507 datacenter_tenants_dict["datacenter_id"] = datacenter_id
tierno8008c3a2016-10-13 15:34:28 +00002508 if config:
2509 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
tiernof97fd272016-07-11 14:32:37 +02002510 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002511 datacenter_tenants_dict["uuid"] = id_
2512
2513 #fill tenants_datacenters table
2514 tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"]
tiernof97fd272016-07-11 14:32:37 +02002515 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
2516 return datacenter_id
tierno7edb6752016-03-21 17:37:52 +01002517
2518def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2519 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002520 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002521
2522 #get nfvo_tenant info
2523 if not tenant_id or tenant_id=="any":
2524 tenant_uuid = None
2525 else:
tiernof97fd272016-07-11 14:32:37 +02002526 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
tierno7edb6752016-03-21 17:37:52 +01002527 tenant_uuid = tenant_dict['uuid']
2528
2529 #check that this association exist before
2530 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2531 if tenant_uuid:
2532 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
tiernof97fd272016-07-11 14:32:37 +02002533 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2534 if len(tenant_datacenter_list)==0 and tenant_uuid:
2535 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002536
2537 #delete this association
tiernof97fd272016-07-11 14:32:37 +02002538 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
tierno7edb6752016-03-21 17:37:52 +01002539
2540 #get vim_tenant info and deletes
2541 warning=''
2542 for tenant_datacenter_item in tenant_datacenter_list:
tiernof97fd272016-07-11 14:32:37 +02002543 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2544 #try to delete vim:tenant
2545 try:
2546 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2547 if vim_tenant_dict['created']=='true':
tierno7edb6752016-03-21 17:37:52 +01002548 #delete tenant at VIM if created by NFVO
tiernoae4a8d12016-07-08 12:30:39 +02002549 try:
2550 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2551 except vimconn.vimconnException as e:
2552 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2553 logger.warn(warning)
tiernof97fd272016-07-11 14:32:37 +02002554 except db_base_Exception as e:
2555 logger.error("Cannot delete datacenter_tenants " + str(e))
2556 pass #the error will be caused because dependencies, vim_tenant can not be deleted
tierno7edb6752016-03-21 17:37:52 +01002557
tiernof97fd272016-07-11 14:32:37 +02002558 return "datacenter {} detached. {}".format(datacenter_id, warning)
tierno7edb6752016-03-21 17:37:52 +01002559
2560def datacenter_action(mydb, tenant_id, datacenter, action_dict):
2561 #DEPRECATED
2562 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002563 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002564
2565 if 'net-update' in action_dict:
tiernoae4a8d12016-07-08 12:30:39 +02002566 try:
tiernof97fd272016-07-11 14:32:37 +02002567 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
tiernoae4a8d12016-07-08 12:30:39 +02002568 #print content
2569 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002570 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
2571 raise NfvoException(str(e), HTTP_Internal_Server_Error)
tierno7edb6752016-03-21 17:37:52 +01002572 #update nets Change from VIM format to NFVO format
2573 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002574 for net in nets:
tierno7edb6752016-03-21 17:37:52 +01002575 net_nfvo={'datacenter_id': datacenter_id}
2576 net_nfvo['name'] = net['name']
2577 #net_nfvo['description']= net['name']
2578 net_nfvo['vim_net_id'] = net['id']
2579 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2580 net_nfvo['shared'] = net['shared']
2581 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
2582 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002583 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
2584 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
2585 return inserted
tierno7edb6752016-03-21 17:37:52 +01002586 elif 'net-edit' in action_dict:
2587 net = action_dict['net-edit'].pop('net')
tierno42fcc3b2016-07-06 17:20:40 +02002588 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002589 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
tierno7edb6752016-03-21 17:37:52 +01002590 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002591 return result
tierno7edb6752016-03-21 17:37:52 +01002592 elif 'net-delete' in action_dict:
2593 net = action_dict['net-deelte'].get('net')
tierno42fcc3b2016-07-06 17:20:40 +02002594 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002595 result = mydb.delete_row(FROM='datacenter_nets',
tierno7edb6752016-03-21 17:37:52 +01002596 WHERE={'datacenter_id':datacenter_id, what: net})
tiernof97fd272016-07-11 14:32:37 +02002597 return result
tierno7edb6752016-03-21 17:37:52 +01002598
2599 else:
tiernof97fd272016-07-11 14:32:37 +02002600 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01002601
2602def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
2603 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002604 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002605
tierno42fcc3b2016-07-06 17:20:40 +02002606 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
tiernof97fd272016-07-11 14:32:37 +02002607 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
tierno7edb6752016-03-21 17:37:52 +01002608 WHERE={'datacenter_id':datacenter_id, what: netmap})
tiernof97fd272016-07-11 14:32:37 +02002609 return result
tierno7edb6752016-03-21 17:37:52 +01002610
2611def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
2612 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002613 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002614 filter_dict={}
2615 if action_dict:
2616 action_dict = action_dict["netmap"]
2617 if 'vim_id' in action_dict:
2618 filter_dict["id"] = action_dict['vim_id']
2619 if 'vim_name' in action_dict:
2620 filter_dict["name"] = action_dict['vim_name']
2621 else:
2622 filter_dict["shared"] = True
2623
tiernoae4a8d12016-07-08 12:30:39 +02002624 try:
tiernof97fd272016-07-11 14:32:37 +02002625 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
tiernoae4a8d12016-07-08 12:30:39 +02002626 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002627 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
2628 raise NfvoException(str(e), HTTP_Internal_Server_Error)
2629 if len(vim_nets)>1 and action_dict:
2630 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
2631 elif len(vim_nets)==0: # and action_dict:
2632 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
tierno7edb6752016-03-21 17:37:52 +01002633 net_list=[]
tiernof97fd272016-07-11 14:32:37 +02002634 for net in vim_nets:
tierno7edb6752016-03-21 17:37:52 +01002635 net_nfvo={'datacenter_id': datacenter_id}
2636 if action_dict and "name" in action_dict:
2637 net_nfvo['name'] = action_dict['name']
2638 else:
2639 net_nfvo['name'] = net['name']
2640 #net_nfvo['description']= net['name']
2641 net_nfvo['vim_net_id'] = net['id']
2642 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
2643 net_nfvo['shared'] = net['shared']
2644 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
tiernof97fd272016-07-11 14:32:37 +02002645 try:
2646 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
tierno7edb6752016-03-21 17:37:52 +01002647 net_nfvo["status"] = "OK"
tiernof97fd272016-07-11 14:32:37 +02002648 net_nfvo["uuid"] = net_id
2649 except db_base_Exception as e:
2650 if action_dict:
2651 raise
2652 else:
2653 net_nfvo["status"] = "FAIL: " + str(e)
tierno7edb6752016-03-21 17:37:52 +01002654 net_list.append(net_nfvo)
tiernof97fd272016-07-11 14:32:37 +02002655 return net_list
tierno7edb6752016-03-21 17:37:52 +01002656
2657def vim_action_get(mydb, tenant_id, datacenter, item, name):
2658 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002659 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno7edb6752016-03-21 17:37:52 +01002660 filter_dict={}
2661 if name:
tierno42fcc3b2016-07-06 17:20:40 +02002662 if utils.check_valid_uuid(name):
tierno7edb6752016-03-21 17:37:52 +01002663 filter_dict["id"] = name
2664 else:
2665 filter_dict["name"] = name
tiernoae4a8d12016-07-08 12:30:39 +02002666 try:
2667 if item=="networks":
2668 #filter_dict['tenant_id'] = myvim['tenant_id']
2669 content = myvim.get_network_list(filter_dict=filter_dict)
2670 elif item=="tenants":
2671 content = myvim.get_tenant_list(filter_dict=filter_dict)
2672 else:
tiernof97fd272016-07-11 14:32:37 +02002673 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernobe41e222016-09-02 15:16:13 +02002674 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
tiernoae4a8d12016-07-08 12:30:39 +02002675 if name and len(content)==1:
tiernof97fd272016-07-11 14:32:37 +02002676 return {item[:-1]: content[0]}
tiernoae4a8d12016-07-08 12:30:39 +02002677 elif name and len(content)==0:
tiernof97fd272016-07-11 14:32:37 +02002678 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 +02002679 datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002680 else:
tiernof97fd272016-07-11 14:32:37 +02002681 return {item: content}
tiernoae4a8d12016-07-08 12:30:39 +02002682 except vimconn.vimconnException as e:
2683 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
tiernof97fd272016-07-11 14:32:37 +02002684 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
tierno7edb6752016-03-21 17:37:52 +01002685
2686def vim_action_delete(mydb, tenant_id, datacenter, item, name):
2687 #get datacenter info
tierno392f2852016-05-13 12:28:55 +02002688 if tenant_id == "any":
2689 tenant_id=None
2690
tiernoa2793912016-10-04 08:15:08 +00002691 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tierno392f2852016-05-13 12:28:55 +02002692 #get uuid name
tiernof97fd272016-07-11 14:32:37 +02002693 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
2694 logger.debug("vim_action_delete vim response: " + str(content))
tierno392f2852016-05-13 12:28:55 +02002695 items = content.values()[0]
2696 if type(items)==list and len(items)==0:
tiernof97fd272016-07-11 14:32:37 +02002697 raise NfvoException("Not found " + item, HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002698 elif type(items)==list and len(items)>1:
tiernof97fd272016-07-11 14:32:37 +02002699 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
tierno392f2852016-05-13 12:28:55 +02002700 else: # it is a dict
2701 item_id = items["id"]
2702 item_name = str(items.get("name"))
tierno7edb6752016-03-21 17:37:52 +01002703
tiernoae4a8d12016-07-08 12:30:39 +02002704 try:
2705 if item=="networks":
2706 content = myvim.delete_network(item_id)
2707 elif item=="tenants":
2708 content = myvim.delete_tenant(item_id)
2709 else:
tiernof97fd272016-07-11 14:32:37 +02002710 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002711 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002712 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
2713 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002714
tiernof97fd272016-07-11 14:32:37 +02002715 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
tierno7edb6752016-03-21 17:37:52 +01002716
2717def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
2718 #get datacenter info
tiernoa2793912016-10-04 08:15:08 +00002719 logger.debug("vim_action_create descriptor %s", str(descriptor))
tierno392f2852016-05-13 12:28:55 +02002720 if tenant_id == "any":
2721 tenant_id=None
tiernoa2793912016-10-04 08:15:08 +00002722 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
tiernoae4a8d12016-07-08 12:30:39 +02002723 try:
2724 if item=="networks":
2725 net = descriptor["network"]
2726 net_name = net.pop("name")
2727 net_type = net.pop("type", "bridge")
garciadeblas9f8456e2016-09-05 05:02:59 +02002728 net_public = net.pop("shared", False)
2729 net_ipprofile = net.pop("ip_profile", None)
2730 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, **net)
tiernoae4a8d12016-07-08 12:30:39 +02002731 elif item=="tenants":
2732 tenant = descriptor["tenant"]
2733 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
2734 else:
tiernof97fd272016-07-11 14:32:37 +02002735 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
tiernoae4a8d12016-07-08 12:30:39 +02002736 except vimconn.vimconnException as e:
tiernof97fd272016-07-11 14:32:37 +02002737 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
tiernoae4a8d12016-07-08 12:30:39 +02002738
tierno7edb6752016-03-21 17:37:52 +01002739 return vim_action_get(mydb, tenant_id, datacenter, item, content)
2740
tierno66aa0372016-07-06 17:31:12 +02002741