Adds SFC CRUD interface to the VIM connector
[osm/RO.git] / osm_ro / nfvo.py
1 # -*- 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 '''
25 NFVO 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
30 # import imp
31 # import json
32 import yaml
33 import utils
34 import vim_thread
35 from db_base import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\
36 HTTP_Conflict, HTTP_Method_Not_Allowed
37 import console_proxy_thread as cli
38 import vimconn
39 import logging
40 import collections
41 from db_base import db_base_Exception
42
43 import nfvo_db
44 from threading import Lock
45 from time import time
46 from lib_osm_openvim import ovim as ovim_module
47 from lib_osm_openvim.ovim import ovimException
48
49 global global_config
50 global vimconn_imported
51 global logger
52 global default_volume_size
53 default_volume_size = '5' #size in GB
54 global ovim
55 ovim = None
56 global_config = None
57
58 vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
59 vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
60 vim_persistent_info = {}
61 logger = logging.getLogger('openmano.nfvo')
62 task_lock = Lock()
63 global_instance_tasks = {}
64 last_task_id = 0.0
65 db=None
66 db_lock=Lock()
67
68 class NfvoException(Exception):
69 def __init__(self, message, http_code):
70 self.http_code = http_code
71 Exception.__init__(self, message)
72
73
74 def get_task_id():
75 global last_task_id
76 task_id = time()
77 if task_id <= last_task_id:
78 task_id = last_task_id + 0.000001
79 last_task_id = task_id
80 return "TASK.{:.6f}".format(task_id)
81
82
83 def new_task(name, params, depends=None):
84 task_id = get_task_id()
85 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
86 if depends:
87 task["depends"] = depends
88 return task
89
90
91 def is_task_id(id):
92 return True if id[:5] == "TASK." else False
93
94
95 def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
96 name = datacenter_name[:16]
97 if name not in vim_threads["names"]:
98 vim_threads["names"].append(name)
99 return name
100 name = datacenter_name[:16] + "." + tenant_name[:16]
101 if name not in vim_threads["names"]:
102 vim_threads["names"].append(name)
103 return name
104 name = datacenter_id + "-" + tenant_id
105 vim_threads["names"].append(name)
106 return name
107
108
109 def start_service(mydb):
110 global db, global_config
111 db = nfvo_db.nfvo_db()
112 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
113 global ovim
114
115 # Initialize openvim for SDN control
116 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
117 # TODO: review ovim.py to delete not needed configuration
118 ovim_configuration = {
119 'logger_name': 'openmano.ovim',
120 'network_vlan_range_start': 1000,
121 'network_vlan_range_end': 4096,
122 'db_name': global_config["db_ovim_name"],
123 'db_host': global_config["db_ovim_host"],
124 'db_user': global_config["db_ovim_user"],
125 'db_passwd': global_config["db_ovim_passwd"],
126 'bridge_ifaces': {},
127 'mode': 'normal',
128 'network_type': 'bridge',
129 #TODO: log_level_of should not be needed. To be modified in ovim
130 'log_level_of': 'DEBUG'
131 }
132 try:
133 ovim = ovim_module.ovim(ovim_configuration)
134 ovim.start_service()
135
136 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
137 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
138 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
139 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
140 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
141 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
142 vims = mydb.get_rows(FROM=from_, SELECT=select_)
143 for vim in vims:
144 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
145 'datacenter_id': vim.get('datacenter_id')}
146 if vim["config"]:
147 extra.update(yaml.load(vim["config"]))
148 if vim.get('dt_config'):
149 extra.update(yaml.load(vim["dt_config"]))
150 if vim["type"] not in vimconn_imported:
151 module_info=None
152 try:
153 module = "vimconn_" + vim["type"]
154 pkg = __import__("osm_ro." + module)
155 vim_conn = getattr(pkg, module)
156 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
157 # vim_conn = imp.load_module(vim["type"], *module_info)
158 vimconn_imported[vim["type"]] = vim_conn
159 except (IOError, ImportError) as e:
160 # if module_info and module_info[0]:
161 # file.close(module_info[0])
162 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
163 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
164
165 thread_id = vim['datacenter_tenant_id']
166 vim_persistent_info[thread_id] = {}
167 try:
168 #if not tenant:
169 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
170 myvim = vimconn_imported[ vim["type"] ].vimconnector(
171 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
172 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
173 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
174 user=vim['user'], passwd=vim['passwd'],
175 config=extra, persistent_info=vim_persistent_info[thread_id]
176 )
177 except Exception as e:
178 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
179 HTTP_Internal_Server_Error)
180 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
181 vim['vim_tenant_id'])
182 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
183 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
184 new_thread.start()
185 vim_threads["running"][thread_id] = new_thread
186 except db_base_Exception as e:
187 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
188 except ovim_module.ovimException as e:
189 message = str(e)
190 if message[:22] == "DATABASE wrong version":
191 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
192 "at host {dbhost}".format(
193 msg=message[22:-3], dbname=global_config["db_ovim_name"],
194 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
195 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
196 raise NfvoException(message, HTTP_Bad_Request)
197
198
199 def stop_service():
200 global ovim, global_config
201 if ovim:
202 ovim.stop_service()
203 for thread_id,thread in vim_threads["running"].items():
204 thread.insert_task(new_task("exit", None))
205 vim_threads["deleting"][thread_id] = thread
206 vim_threads["running"] = {}
207 if global_config and global_config.get("console_thread"):
208 for thread in global_config["console_thread"]:
209 thread.terminate = True
210
211 def get_version():
212 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
213 global_config["version_date"] ))
214
215
216 def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
217 '''Obtain flavorList
218 return result, content:
219 <0, error_text upon error
220 nb_records, flavor_list on success
221 '''
222 WHERE_dict={}
223 WHERE_dict['vnf_id'] = vnf_id
224 if nfvo_tenant is not None:
225 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
226
227 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
228 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
229 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
230 #print "get_flavor_list result:", result
231 #print "get_flavor_list content:", content
232 flavorList=[]
233 for flavor in flavors:
234 flavorList.append(flavor['flavor_id'])
235 return flavorList
236
237
238 def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
239 '''Obtain imageList
240 return result, content:
241 <0, error_text upon error
242 nb_records, flavor_list on success
243 '''
244 WHERE_dict={}
245 WHERE_dict['vnf_id'] = vnf_id
246 if nfvo_tenant is not None:
247 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
248
249 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
250 images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
251 imageList=[]
252 for image in images:
253 imageList.append(image['image_id'])
254 return imageList
255
256
257 def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
258 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
259 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
260 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
261 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
262 raise exception upon error
263 '''
264 WHERE_dict={}
265 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
266 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
267 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
268 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
269 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
270 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
271 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
272 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'
273 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
274 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
275 'user','passwd', 'dt.config as dt_config')
276 else:
277 from_ = 'datacenters as d'
278 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
279 try:
280 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
281 vim_dict={}
282 for vim in vims:
283 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
284 'datacenter_id': vim.get('datacenter_id')}
285 if vim["config"]:
286 extra.update(yaml.load(vim["config"]))
287 if vim.get('dt_config'):
288 extra.update(yaml.load(vim["dt_config"]))
289 if vim["type"] not in vimconn_imported:
290 module_info=None
291 try:
292 module = "vimconn_" + vim["type"]
293 pkg = __import__("osm_ro." + module)
294 vim_conn = getattr(pkg, module)
295 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
296 # vim_conn = imp.load_module(vim["type"], *module_info)
297 vimconn_imported[vim["type"]] = vim_conn
298 except (IOError, ImportError) as e:
299 # if module_info and module_info[0]:
300 # file.close(module_info[0])
301 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
302 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
303
304 try:
305 if 'datacenter_tenant_id' in vim:
306 thread_id = vim["datacenter_tenant_id"]
307 if thread_id not in vim_persistent_info:
308 vim_persistent_info[thread_id] = {}
309 persistent_info = vim_persistent_info[thread_id]
310 else:
311 persistent_info = {}
312 #if not tenant:
313 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
314 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
315 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
316 tenant_id=vim.get('vim_tenant_id',vim_tenant),
317 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
318 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
319 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
320 config=extra, persistent_info=persistent_info
321 )
322 except Exception as e:
323 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
324 return vim_dict
325 except db_base_Exception as e:
326 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
327
328
329 def rollback(mydb, vims, rollback_list):
330 undeleted_items=[]
331 #delete things by reverse order
332 for i in range(len(rollback_list)-1, -1, -1):
333 item = rollback_list[i]
334 if item["where"]=="vim":
335 if item["vim_id"] not in vims:
336 continue
337 vim=vims[ item["vim_id"] ]
338 try:
339 if item["what"]=="image":
340 vim.delete_image(item["uuid"])
341 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
342 elif item["what"]=="flavor":
343 vim.delete_flavor(item["uuid"])
344 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
345 elif item["what"]=="network":
346 vim.delete_network(item["uuid"])
347 elif item["what"]=="vm":
348 vim.delete_vminstance(item["uuid"])
349 except vimconn.vimconnException as e:
350 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
351 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
352 except db_base_Exception as e:
353 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
354
355 else: # where==mano
356 try:
357 if item["what"]=="image":
358 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
359 elif item["what"]=="flavor":
360 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
361 except db_base_Exception as e:
362 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
363 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
364 if len(undeleted_items)==0:
365 return True," Rollback successful."
366 else:
367 return False," Rollback fails to delete: " + str(undeleted_items)
368
369
370 def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
371 global global_config
372 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
373 vnfc_interfaces={}
374 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
375 name_dict = {}
376 #dataplane interfaces
377 for numa in vnfc.get("numas",() ):
378 for interface in numa.get("interfaces",()):
379 if interface["name"] in name_dict:
380 raise NfvoException(
381 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
382 vnfc["name"], interface["name"]),
383 HTTP_Bad_Request)
384 name_dict[ interface["name"] ] = "underlay"
385 #bridge interfaces
386 for interface in vnfc.get("bridge-ifaces",() ):
387 if interface["name"] in name_dict:
388 raise NfvoException(
389 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
390 vnfc["name"], interface["name"]),
391 HTTP_Bad_Request)
392 name_dict[ interface["name"] ] = "overlay"
393 vnfc_interfaces[ vnfc["name"] ] = name_dict
394 # check bood-data info
395 if "boot-data" in vnfc:
396 # check that user-data is incompatible with users and config-files
397 if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
398 raise NfvoException(
399 "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
400 HTTP_Bad_Request)
401
402 #check if the info in external_connections matches with the one in the vnfcs
403 name_list=[]
404 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
405 if external_connection["name"] in name_list:
406 raise NfvoException(
407 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
408 external_connection["name"]),
409 HTTP_Bad_Request)
410 name_list.append(external_connection["name"])
411 if external_connection["VNFC"] not in vnfc_interfaces:
412 raise NfvoException(
413 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
414 external_connection["name"], external_connection["VNFC"]),
415 HTTP_Bad_Request)
416
417 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
418 raise NfvoException(
419 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
420 external_connection["name"],
421 external_connection["local_iface_name"]),
422 HTTP_Bad_Request )
423
424 #check if the info in internal_connections matches with the one in the vnfcs
425 name_list=[]
426 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
427 if internal_connection["name"] in name_list:
428 raise NfvoException(
429 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
430 internal_connection["name"]),
431 HTTP_Bad_Request)
432 name_list.append(internal_connection["name"])
433 #We should check that internal-connections of type "ptp" have only 2 elements
434
435 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
436 raise NfvoException(
437 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
438 internal_connection["name"],
439 'ptp' if vnf_descriptor_version==1 else 'e-line',
440 'data' if vnf_descriptor_version==1 else "e-lan"),
441 HTTP_Bad_Request)
442 for port in internal_connection["elements"]:
443 vnf = port["VNFC"]
444 iface = port["local_iface_name"]
445 if vnf not in vnfc_interfaces:
446 raise NfvoException(
447 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
448 internal_connection["name"], vnf),
449 HTTP_Bad_Request)
450 if iface not in vnfc_interfaces[ vnf ]:
451 raise NfvoException(
452 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
453 internal_connection["name"], iface),
454 HTTP_Bad_Request)
455 return -HTTP_Bad_Request,
456 if vnf_descriptor_version==1 and "type" not in internal_connection:
457 if vnfc_interfaces[vnf][iface] == "overlay":
458 internal_connection["type"] = "bridge"
459 else:
460 internal_connection["type"] = "data"
461 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
462 if vnfc_interfaces[vnf][iface] == "overlay":
463 internal_connection["implementation"] = "overlay"
464 else:
465 internal_connection["implementation"] = "underlay"
466 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
467 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
468 raise NfvoException(
469 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
470 internal_connection["name"],
471 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
472 'data' if vnf_descriptor_version==1 else 'underlay'),
473 HTTP_Bad_Request)
474 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
475 vnfc_interfaces[vnf][iface] == "underlay":
476 raise NfvoException(
477 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
478 internal_connection["name"], iface,
479 'data' if vnf_descriptor_version==1 else 'underlay',
480 'bridge' if vnf_descriptor_version==1 else 'overlay'),
481 HTTP_Bad_Request)
482
483
484 def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
485 #look if image exist
486 if only_create_at_vim:
487 image_mano_id = image_dict['uuid']
488 if return_on_error == None:
489 return_on_error = True
490 else:
491 if image_dict['location']:
492 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
493 else:
494 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
495 if len(images)>=1:
496 image_mano_id = images[0]['uuid']
497 else:
498 #create image in MANO DB
499 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
500 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
501 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
502 }
503 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
504 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
505 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
506 #create image at every vim
507 for vim_id,vim in vims.iteritems():
508 image_created="false"
509 #look at database
510 image_db = mydb.get_rows(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
511 #look at VIM if this image exist
512 try:
513 if image_dict['location'] is not None:
514 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
515 else:
516 filter_dict = {}
517 filter_dict['name'] = image_dict['universal_name']
518 if image_dict.get('checksum') != None:
519 filter_dict['checksum'] = image_dict['checksum']
520 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
521 vim_images = vim.get_image_list(filter_dict)
522 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
523 if len(vim_images) > 1:
524 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
525 elif len(vim_images) == 0:
526 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
527 else:
528 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
529 image_vim_id = vim_images[0]['id']
530
531 except vimconn.vimconnNotFoundException as e:
532 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
533 try:
534 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
535 if image_dict['location']:
536 image_vim_id = vim.new_image(image_dict)
537 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
538 image_created="true"
539 else:
540 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
541 raise vimconn.vimconnException(str(e))
542 except vimconn.vimconnException as e:
543 if return_on_error:
544 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
545 raise
546 image_vim_id = None
547 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
548 continue
549 except vimconn.vimconnException as e:
550 if return_on_error:
551 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
552 raise
553 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
554 image_vim_id = None
555 continue
556 #if we reach here, the image has been created or existed
557 if len(image_db)==0:
558 #add new vim_id at datacenters_images
559 mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
560 elif image_db[0]["vim_id"]!=image_vim_id:
561 #modify existing vim_id at datacenters_images
562 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
563
564 return image_vim_id if only_create_at_vim else image_mano_id
565
566
567 def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
568 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
569 'ram':flavor_dict.get('ram'),
570 'vcpus':flavor_dict.get('vcpus'),
571 }
572 if 'extended' in flavor_dict and flavor_dict['extended']==None:
573 del flavor_dict['extended']
574 if 'extended' in flavor_dict:
575 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
576
577 #look if flavor exist
578 if only_create_at_vim:
579 flavor_mano_id = flavor_dict['uuid']
580 if return_on_error == None:
581 return_on_error = True
582 else:
583 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
584 if len(flavors)>=1:
585 flavor_mano_id = flavors[0]['uuid']
586 else:
587 #create flavor
588 #create one by one the images of aditional disks
589 dev_image_list=[] #list of images
590 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
591 dev_nb=0
592 for device in flavor_dict['extended'].get('devices',[]):
593 if "image" not in device and "image name" not in device:
594 continue
595 image_dict={}
596 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
597 image_dict['universal_name']=device.get('image name')
598 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
599 image_dict['location']=device.get('image')
600 #image_dict['new_location']=vnfc.get('image location')
601 image_dict['checksum']=device.get('image checksum')
602 image_metadata_dict = device.get('image metadata', None)
603 image_metadata_str = None
604 if image_metadata_dict != None:
605 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
606 image_dict['metadata']=image_metadata_str
607 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
608 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
609 dev_image_list.append(image_id)
610 dev_nb += 1
611 temp_flavor_dict['name'] = flavor_dict['name']
612 temp_flavor_dict['description'] = flavor_dict.get('description',None)
613 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
614 flavor_mano_id= content
615 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
616 #create flavor at every vim
617 if 'uuid' in flavor_dict:
618 del flavor_dict['uuid']
619 flavor_vim_id=None
620 for vim_id,vim in vims.items():
621 flavor_created="false"
622 #look at database
623 flavor_db = mydb.get_rows(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
624 #look at VIM if this flavor exist SKIPPED
625 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
626 #if res_vim < 0:
627 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
628 # continue
629 #elif res_vim==0:
630
631 #Create the flavor in VIM
632 #Translate images at devices from MANO id to VIM id
633 disk_list = []
634 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
635 #make a copy of original devices
636 devices_original=[]
637
638 for device in flavor_dict["extended"].get("devices",[]):
639 dev={}
640 dev.update(device)
641 devices_original.append(dev)
642 if 'image' in device:
643 del device['image']
644 if 'image metadata' in device:
645 del device['image metadata']
646 dev_nb=0
647 for index in range(0,len(devices_original)) :
648 device=devices_original[index]
649 if "image" not in device and "image name" not in device:
650 if 'size' in device:
651 disk_list.append({'size': device.get('size', default_volume_size)})
652 continue
653 image_dict={}
654 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
655 image_dict['universal_name']=device.get('image name')
656 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
657 image_dict['location']=device.get('image')
658 #image_dict['new_location']=device.get('image location')
659 image_dict['checksum']=device.get('image checksum')
660 image_metadata_dict = device.get('image metadata', None)
661 image_metadata_str = None
662 if image_metadata_dict != None:
663 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
664 image_dict['metadata']=image_metadata_str
665 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
666 image_dict["uuid"]=image_mano_id
667 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
668
669 #save disk information (image must be based on and size
670 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
671
672 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
673 dev_nb += 1
674 if len(flavor_db)>0:
675 #check that this vim_id exist in VIM, if not create
676 flavor_vim_id=flavor_db[0]["vim_id"]
677 try:
678 vim.get_flavor(flavor_vim_id)
679 continue #flavor exist
680 except vimconn.vimconnException:
681 pass
682 #create flavor at vim
683 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
684 try:
685 flavor_vim_id = None
686 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
687 flavor_create="false"
688 except vimconn.vimconnException as e:
689 pass
690 try:
691 if not flavor_vim_id:
692 flavor_vim_id = vim.new_flavor(flavor_dict)
693 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
694 flavor_created="true"
695 except vimconn.vimconnException as e:
696 if return_on_error:
697 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
698 raise
699 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
700 flavor_vim_id = None
701 continue
702 #if reach here the flavor has been create or exist
703 if len(flavor_db)==0:
704 #add new vim_id at datacenters_flavors
705 extended_devices_yaml = None
706 if len(disk_list) > 0:
707 extended_devices = dict()
708 extended_devices['disks'] = disk_list
709 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
710 mydb.new_row('datacenters_flavors',
711 {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id,
712 'created':flavor_created,'extended': extended_devices_yaml})
713 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
714 #modify existing vim_id at datacenters_flavors
715 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
716
717 return flavor_vim_id if only_create_at_vim else flavor_mano_id
718
719
720 def new_vnf(mydb, tenant_id, vnf_descriptor):
721 global global_config
722
723 # Step 1. Check the VNF descriptor
724 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
725 # Step 2. Check tenant exist
726 vims = {}
727 if tenant_id != "any":
728 check_tenant(mydb, tenant_id)
729 if "tenant_id" in vnf_descriptor["vnf"]:
730 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
731 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
732 HTTP_Unauthorized)
733 else:
734 vnf_descriptor['vnf']['tenant_id'] = tenant_id
735 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
736 if global_config["auto_push_VNF_to_VIMs"]:
737 vims = get_vim(mydb, tenant_id)
738
739 # Step 4. Review the descriptor and add missing fields
740 #print vnf_descriptor
741 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
742 vnf_name = vnf_descriptor['vnf']['name']
743 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
744 if "physical" in vnf_descriptor['vnf']:
745 del vnf_descriptor['vnf']['physical']
746 #print vnf_descriptor
747
748 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
749 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
750 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
751
752 #For each VNFC, we add it to the VNFCDict and we create a flavor.
753 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
754 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
755 try:
756 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
757 for vnfc in vnf_descriptor['vnf']['VNFC']:
758 VNFCitem={}
759 VNFCitem["name"] = vnfc['name']
760 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
761
762 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
763
764 myflavorDict = {}
765 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
766 myflavorDict["description"] = VNFCitem["description"]
767 myflavorDict["ram"] = vnfc.get("ram", 0)
768 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
769 myflavorDict["disk"] = vnfc.get("disk", 1)
770 myflavorDict["extended"] = {}
771
772 devices = vnfc.get("devices")
773 if devices != None:
774 myflavorDict["extended"]["devices"] = devices
775
776 # TODO:
777 # 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
778 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
779
780 # Previous code has been commented
781 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
782 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
783 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
784 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
785 #else:
786 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
787 # if result2:
788 # print "Error creating flavor: unknown processor model. Rollback successful."
789 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
790 # else:
791 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
792 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
793
794 if 'numas' in vnfc and len(vnfc['numas'])>0:
795 myflavorDict['extended']['numas'] = vnfc['numas']
796
797 #print myflavorDict
798
799 # Step 6.2 New flavors are created in the VIM
800 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
801
802 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
803 VNFCitem["flavor_id"] = flavor_id
804 VNFCDict[vnfc['name']] = VNFCitem
805
806 logger.debug("Creating new images in the VIM for each VNFC")
807 # Step 6.3 New images are created in the VIM
808 #For each VNFC, we must create the appropriate image.
809 #This "for" loop might be integrated with the previous one
810 #In case this integration is made, the VNFCDict might become a VNFClist.
811 for vnfc in vnf_descriptor['vnf']['VNFC']:
812 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
813 image_dict={}
814 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
815 image_dict['universal_name']=vnfc.get('image name')
816 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
817 image_dict['location']=vnfc.get('VNFC image')
818 #image_dict['new_location']=vnfc.get('image location')
819 image_dict['checksum']=vnfc.get('image checksum')
820 image_metadata_dict = vnfc.get('image metadata', None)
821 image_metadata_str = None
822 if image_metadata_dict is not None:
823 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
824 image_dict['metadata']=image_metadata_str
825 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
826 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
827 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
828 VNFCDict[vnfc['name']]["image_id"] = image_id
829 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
830 if vnfc.get("boot-data"):
831 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
832
833
834 # Step 7. Storing the VNF descriptor in the repository
835 if "descriptor" not in vnf_descriptor["vnf"]:
836 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
837
838 # Step 8. Adding the VNF to the NFVO DB
839 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
840 return vnf_id
841 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
842 _, message = rollback(mydb, vims, rollback_list)
843 if isinstance(e, db_base_Exception):
844 error_text = "Exception at database"
845 elif isinstance(e, KeyError):
846 error_text = "KeyError exception "
847 e.http_code = HTTP_Internal_Server_Error
848 else:
849 error_text = "Exception at VIM"
850 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
851 #logger.error("start_scenario %s", error_text)
852 raise NfvoException(error_text, e.http_code)
853
854
855 def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
856 global global_config
857
858 # Step 1. Check the VNF descriptor
859 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
860 # Step 2. Check tenant exist
861 vims = {}
862 if tenant_id != "any":
863 check_tenant(mydb, tenant_id)
864 if "tenant_id" in vnf_descriptor["vnf"]:
865 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
866 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
867 HTTP_Unauthorized)
868 else:
869 vnf_descriptor['vnf']['tenant_id'] = tenant_id
870 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
871 if global_config["auto_push_VNF_to_VIMs"]:
872 vims = get_vim(mydb, tenant_id)
873
874 # Step 4. Review the descriptor and add missing fields
875 #print vnf_descriptor
876 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
877 vnf_name = vnf_descriptor['vnf']['name']
878 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
879 if "physical" in vnf_descriptor['vnf']:
880 del vnf_descriptor['vnf']['physical']
881 #print vnf_descriptor
882
883 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
884 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
885 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
886
887 #For each VNFC, we add it to the VNFCDict and we create a flavor.
888 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
889 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
890 try:
891 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
892 for vnfc in vnf_descriptor['vnf']['VNFC']:
893 VNFCitem={}
894 VNFCitem["name"] = vnfc['name']
895 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
896
897 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
898
899 myflavorDict = {}
900 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
901 myflavorDict["description"] = VNFCitem["description"]
902 myflavorDict["ram"] = vnfc.get("ram", 0)
903 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
904 myflavorDict["disk"] = vnfc.get("disk", 1)
905 myflavorDict["extended"] = {}
906
907 devices = vnfc.get("devices")
908 if devices != None:
909 myflavorDict["extended"]["devices"] = devices
910
911 # TODO:
912 # 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
913 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
914
915 # Previous code has been commented
916 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
917 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
918 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
919 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
920 #else:
921 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
922 # if result2:
923 # print "Error creating flavor: unknown processor model. Rollback successful."
924 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
925 # else:
926 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
927 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
928
929 if 'numas' in vnfc and len(vnfc['numas'])>0:
930 myflavorDict['extended']['numas'] = vnfc['numas']
931
932 #print myflavorDict
933
934 # Step 6.2 New flavors are created in the VIM
935 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
936
937 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
938 VNFCitem["flavor_id"] = flavor_id
939 VNFCDict[vnfc['name']] = VNFCitem
940
941 logger.debug("Creating new images in the VIM for each VNFC")
942 # Step 6.3 New images are created in the VIM
943 #For each VNFC, we must create the appropriate image.
944 #This "for" loop might be integrated with the previous one
945 #In case this integration is made, the VNFCDict might become a VNFClist.
946 for vnfc in vnf_descriptor['vnf']['VNFC']:
947 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
948 image_dict={}
949 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
950 image_dict['universal_name']=vnfc.get('image name')
951 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
952 image_dict['location']=vnfc.get('VNFC image')
953 #image_dict['new_location']=vnfc.get('image location')
954 image_dict['checksum']=vnfc.get('image checksum')
955 image_metadata_dict = vnfc.get('image metadata', None)
956 image_metadata_str = None
957 if image_metadata_dict is not None:
958 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
959 image_dict['metadata']=image_metadata_str
960 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
961 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
962 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
963 VNFCDict[vnfc['name']]["image_id"] = image_id
964 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
965 if vnfc.get("boot-data"):
966 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
967
968 # Step 7. Storing the VNF descriptor in the repository
969 if "descriptor" not in vnf_descriptor["vnf"]:
970 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
971
972 # Step 8. Adding the VNF to the NFVO DB
973 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
974 return vnf_id
975 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
976 _, message = rollback(mydb, vims, rollback_list)
977 if isinstance(e, db_base_Exception):
978 error_text = "Exception at database"
979 elif isinstance(e, KeyError):
980 error_text = "KeyError exception "
981 e.http_code = HTTP_Internal_Server_Error
982 else:
983 error_text = "Exception at VIM"
984 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
985 #logger.error("start_scenario %s", error_text)
986 raise NfvoException(error_text, e.http_code)
987
988
989 def get_vnf_id(mydb, tenant_id, vnf_id):
990 #check valid tenant_id
991 check_tenant(mydb, tenant_id)
992 #obtain data
993 where_or = {}
994 if tenant_id != "any":
995 where_or["tenant_id"] = tenant_id
996 where_or["public"] = True
997 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
998
999 vnf_id=vnf["uuid"]
1000 filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
1001 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
1002 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1003 data={'vnf' : filtered_content}
1004 #GET VM
1005 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
1006 SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description', 'boot_data'),
1007 WHERE={'vnfs.uuid': vnf_id} )
1008 if len(content)==0:
1009 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
1010 # change boot_data into boot-data
1011 for vm in content:
1012 if vm.get("boot_data"):
1013 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1014 del vm["boot_data"]
1015
1016 data['vnf']['VNFC'] = content
1017 #TODO: GET all the information from a VNFC and include it in the output.
1018
1019 #GET NET
1020 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
1021 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1022 WHERE={'vnfs.uuid': vnf_id} )
1023 data['vnf']['nets'] = content
1024
1025 #GET ip-profile for each net
1026 for net in data['vnf']['nets']:
1027 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1028 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1029 WHERE={'net_id': net["uuid"]} )
1030 if len(ipprofiles)==1:
1031 net["ip_profile"] = ipprofiles[0]
1032 elif len(ipprofiles)>1:
1033 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
1034
1035
1036 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1037
1038 #GET External Interfaces
1039 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1040 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1041 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1042 WHERE={'vnfs.uuid': vnf_id},
1043 WHERE_NOT={'interfaces.external_name': None} )
1044 #print content
1045 data['vnf']['external-connections'] = content
1046
1047 return data
1048
1049
1050 def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1051 # Check tenant exist
1052 if tenant_id != "any":
1053 check_tenant(mydb, tenant_id)
1054 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1055 vims = get_vim(mydb, tenant_id)
1056 else:
1057 vims={}
1058
1059 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1060 where_or = {}
1061 if tenant_id != "any":
1062 where_or["tenant_id"] = tenant_id
1063 where_or["public"] = True
1064 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1065 vnf_id = vnf["uuid"]
1066
1067 # "Getting the list of flavors and tenants of the VNF"
1068 flavorList = get_flavorlist(mydb, vnf_id)
1069 if len(flavorList)==0:
1070 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
1071
1072 imageList = get_imagelist(mydb, vnf_id)
1073 if len(imageList)==0:
1074 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
1075
1076 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1077 if deleted == 0:
1078 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
1079
1080 undeletedItems = []
1081 for flavor in flavorList:
1082 #check if flavor is used by other vnf
1083 try:
1084 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1085 if len(c) > 0:
1086 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1087 continue
1088 #flavor not used, must be deleted
1089 #delelte at VIM
1090 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
1091 for flavor_vim in c:
1092 if flavor_vim["datacenter_id"] not in vims:
1093 continue
1094 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
1095 continue
1096 myvim=vims[ flavor_vim["datacenter_id"] ]
1097 try:
1098 myvim.delete_flavor(flavor_vim["vim_id"])
1099 except vimconn.vimconnNotFoundException as e:
1100 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
1101 except vimconn.vimconnException as e:
1102 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1103 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
1104 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
1105 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1106 mydb.delete_row_by_id('flavors', flavor)
1107 except db_base_Exception as e:
1108 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
1109 undeletedItems.append("flavor %s" % flavor)
1110
1111
1112 for image in imageList:
1113 try:
1114 #check if image is used by other vnf
1115 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
1116 if len(c) > 0:
1117 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1118 continue
1119 #image not used, must be deleted
1120 #delelte at VIM
1121 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
1122 for image_vim in c:
1123 if image_vim["datacenter_id"] not in vims:
1124 continue
1125 if image_vim['created']=='false': #skip this image because not created by openmano
1126 continue
1127 myvim=vims[ image_vim["datacenter_id"] ]
1128 try:
1129 myvim.delete_image(image_vim["vim_id"])
1130 except vimconn.vimconnNotFoundException as e:
1131 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1132 except vimconn.vimconnException as e:
1133 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1134 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1135 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
1136 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1137 mydb.delete_row_by_id('images', image)
1138 except db_base_Exception as e:
1139 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
1140 undeletedItems.append("image %s" % image)
1141
1142 return vnf_id + " " + vnf["name"]
1143 #if undeletedItems:
1144 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1145
1146
1147 def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1148 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1149 if result < 0:
1150 return result, vims
1151 elif result == 0:
1152 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1153 myvim = vims.values()[0]
1154 result,servers = myvim.get_hosts_info()
1155 if result < 0:
1156 return result, servers
1157 topology = {'name':myvim['name'] , 'servers': servers}
1158 return result, topology
1159
1160
1161 def get_hosts(mydb, nfvo_tenant_id):
1162 vims = get_vim(mydb, nfvo_tenant_id)
1163 if len(vims) == 0:
1164 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1165 elif len(vims)>1:
1166 #print "nfvo.datacenter_action() error. Several datacenters found"
1167 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1168 myvim = vims.values()[0]
1169 try:
1170 hosts = myvim.get_hosts()
1171 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
1172
1173 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1174 for host in hosts:
1175 server={'name':host['name'], 'vms':[]}
1176 for vm in host['instances']:
1177 #get internal name and model
1178 try:
1179 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1180 WHERE={'vim_vm_id':vm['id']} )
1181 if len(c) == 0:
1182 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1183 continue
1184 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1185
1186 except db_base_Exception as e:
1187 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1188 datacenter['Datacenters'][0]['servers'].append(server)
1189 #return -400, "en construccion"
1190
1191 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1192 return datacenter
1193 except vimconn.vimconnException as e:
1194 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
1195
1196
1197 def new_scenario(mydb, tenant_id, topo):
1198
1199 # result, vims = get_vim(mydb, tenant_id)
1200 # if result < 0:
1201 # return result, vims
1202 #1: parse input
1203 if tenant_id != "any":
1204 check_tenant(mydb, tenant_id)
1205 if "tenant_id" in topo:
1206 if topo["tenant_id"] != tenant_id:
1207 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1208 HTTP_Unauthorized)
1209 else:
1210 tenant_id=None
1211
1212 #1.1: get VNFs and external_networks (other_nets).
1213 vnfs={}
1214 other_nets={} #external_networks, bridge_networks and data_networkds
1215 nodes = topo['topology']['nodes']
1216 for k in nodes.keys():
1217 if nodes[k]['type'] == 'VNF':
1218 vnfs[k] = nodes[k]
1219 vnfs[k]['ifaces'] = {}
1220 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1221 other_nets[k] = nodes[k]
1222 other_nets[k]['external']=True
1223 elif nodes[k]['type'] == 'network':
1224 other_nets[k] = nodes[k]
1225 other_nets[k]['external']=False
1226
1227
1228 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1229 for name,vnf in vnfs.items():
1230 where={}
1231 where_or={"tenant_id": tenant_id, 'public': "true"}
1232 error_text = ""
1233 error_pos = "'topology':'nodes':'" + name + "'"
1234 if 'vnf_id' in vnf:
1235 error_text += " 'vnf_id' " + vnf['vnf_id']
1236 where['uuid'] = vnf['vnf_id']
1237 if 'VNF model' in vnf:
1238 error_text += " 'VNF model' " + vnf['VNF model']
1239 where['name'] = vnf['VNF model']
1240 if len(where) == 0:
1241 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
1242
1243 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1244 FROM='vnfs',
1245 WHERE=where,
1246 WHERE_OR=where_or,
1247 WHERE_AND_OR="AND")
1248 if len(vnf_db)==0:
1249 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1250 elif len(vnf_db)>1:
1251 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1252 vnf['uuid']=vnf_db[0]['uuid']
1253 vnf['description']=vnf_db[0]['description']
1254 #get external interfaces
1255 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1256 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1257 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
1258 for ext_iface in ext_ifaces:
1259 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1260
1261 #1.4 get list of connections
1262 conections = topo['topology']['connections']
1263 conections_list = []
1264 conections_list_name = []
1265 for k in conections.keys():
1266 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1267 ifaces_list = conections[k]['nodes'].items()
1268 elif type(conections[k]['nodes'])==list: #list with dictionary
1269 ifaces_list=[]
1270 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1271 for k2 in conection_pair_list:
1272 ifaces_list += k2
1273
1274 con_type = conections[k].get("type", "link")
1275 if con_type != "link":
1276 if k in other_nets:
1277 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
1278 other_nets[k] = {'external': False}
1279 if conections[k].get("graph"):
1280 other_nets[k]["graph"] = conections[k]["graph"]
1281 ifaces_list.append( (k, None) )
1282
1283
1284 if con_type == "external_network":
1285 other_nets[k]['external'] = True
1286 if conections[k].get("model"):
1287 other_nets[k]["model"] = conections[k]["model"]
1288 else:
1289 other_nets[k]["model"] = k
1290 if con_type == "dataplane_net" or con_type == "bridge_net":
1291 other_nets[k]["model"] = con_type
1292
1293 conections_list_name.append(k)
1294 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)
1295 #print set(ifaces_list)
1296 #check valid VNF and iface names
1297 for iface in ifaces_list:
1298 if iface[0] not in vnfs and iface[0] not in other_nets :
1299 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1300 str(k), iface[0]), HTTP_Not_Found)
1301 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
1302 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1303 str(k), iface[0], iface[1]), HTTP_Not_Found)
1304
1305 #1.5 unify connections from the pair list to a consolidated list
1306 index=0
1307 while index < len(conections_list):
1308 index2 = index+1
1309 while index2 < len(conections_list):
1310 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1311 conections_list[index] |= conections_list[index2]
1312 del conections_list[index2]
1313 del conections_list_name[index2]
1314 else:
1315 index2 += 1
1316 conections_list[index] = list(conections_list[index]) # from set to list again
1317 index += 1
1318 #for k in conections_list:
1319 # print k
1320
1321
1322
1323 #1.6 Delete non external nets
1324 # for k in other_nets.keys():
1325 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1326 # for con in conections_list:
1327 # delete_indexes=[]
1328 # for index in range(0,len(con)):
1329 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1330 # for index in delete_indexes:
1331 # del con[index]
1332 # del other_nets[k]
1333 #1.7: Check external_ports are present at database table datacenter_nets
1334 for k,net in other_nets.items():
1335 error_pos = "'topology':'nodes':'" + k + "'"
1336 if net['external']==False:
1337 if 'name' not in net:
1338 net['name']=k
1339 if 'model' not in net:
1340 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
1341 if net['model']=='bridge_net':
1342 net['type']='bridge';
1343 elif net['model']=='dataplane_net':
1344 net['type']='data';
1345 else:
1346 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
1347 else: #external
1348 #IF we do not want to check that external network exist at datacenter
1349 pass
1350 #ELSE
1351 # error_text = ""
1352 # WHERE_={}
1353 # if 'net_id' in net:
1354 # error_text += " 'net_id' " + net['net_id']
1355 # WHERE_['uuid'] = net['net_id']
1356 # if 'model' in net:
1357 # error_text += " 'model' " + net['model']
1358 # WHERE_['name'] = net['model']
1359 # if len(WHERE_) == 0:
1360 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1361 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1362 # FROM='datacenter_nets', WHERE=WHERE_ )
1363 # if r<0:
1364 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1365 # elif r==0:
1366 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1367 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1368 # elif r>1:
1369 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1370 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1371 # other_nets[k].update(net_db[0])
1372 #ENDIF
1373 net_list={}
1374 net_nb=0 #Number of nets
1375 for con in conections_list:
1376 #check if this is connected to a external net
1377 other_net_index=-1
1378 #print
1379 #print "con", con
1380 for index in range(0,len(con)):
1381 #check if this is connected to a external net
1382 for net_key in other_nets.keys():
1383 if con[index][0]==net_key:
1384 if other_net_index>=0:
1385 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
1386 #print "nfvo.new_scenario " + error_text
1387 raise NfvoException(error_text, HTTP_Bad_Request)
1388 else:
1389 other_net_index = index
1390 net_target = net_key
1391 break
1392 #print "other_net_index", other_net_index
1393 try:
1394 if other_net_index>=0:
1395 del con[other_net_index]
1396 #IF we do not want to check that external network exist at datacenter
1397 if other_nets[net_target]['external'] :
1398 if "name" not in other_nets[net_target]:
1399 other_nets[net_target]['name'] = other_nets[net_target]['model']
1400 if other_nets[net_target]["type"] == "external_network":
1401 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1402 other_nets[net_target]["type"] = "data"
1403 else:
1404 other_nets[net_target]["type"] = "bridge"
1405 #ELSE
1406 # if other_nets[net_target]['external'] :
1407 # 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
1408 # if type_=='data' and other_nets[net_target]['type']=="ptp":
1409 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1410 # print "nfvo.new_scenario " + error_text
1411 # return -HTTP_Bad_Request, error_text
1412 #ENDIF
1413 for iface in con:
1414 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1415 else:
1416 #create a net
1417 net_type_bridge=False
1418 net_type_data=False
1419 net_target = "__-__net"+str(net_nb)
1420 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
1421 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
1422 'external':False}
1423 for iface in con:
1424 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1425 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1426 if iface_type=='mgmt' or iface_type=='bridge':
1427 net_type_bridge = True
1428 else:
1429 net_type_data = True
1430 if net_type_bridge and net_type_data:
1431 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
1432 #print "nfvo.new_scenario " + error_text
1433 raise NfvoException(error_text, HTTP_Bad_Request)
1434 elif net_type_bridge:
1435 type_='bridge'
1436 else:
1437 type_='data' if len(con)>2 else 'ptp'
1438 net_list[net_target]['type'] = type_
1439 net_nb+=1
1440 except Exception:
1441 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
1442 #print "nfvo.new_scenario " + error_text
1443 #raise e
1444 raise NfvoException(error_text, HTTP_Bad_Request)
1445
1446 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1447 #1.8.1 obtain management net
1448 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
1449 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
1450 #1.8.2 check all interfaces from all vnfs
1451 if len(mgmt_net)>0:
1452 add_mgmt_net = False
1453 for vnf in vnfs.values():
1454 for iface in vnf['ifaces'].values():
1455 if iface['type']=='mgmt' and 'net_key' not in iface:
1456 #iface not connected
1457 iface['net_key'] = 'mgmt'
1458 add_mgmt_net = True
1459 if add_mgmt_net and 'mgmt' not in net_list:
1460 net_list['mgmt']=mgmt_net[0]
1461 net_list['mgmt']['external']=True
1462 net_list['mgmt']['graph']={'visible':False}
1463
1464 net_list.update(other_nets)
1465 #print
1466 #print 'net_list', net_list
1467 #print
1468 #print 'vnfs', vnfs
1469 #print
1470
1471 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
1472 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
1473 'tenant_id':tenant_id, 'name':topo['name'],
1474 'description':topo.get('description',topo['name']),
1475 'public': topo.get('public', False)
1476 })
1477
1478 return c
1479
1480
1481 def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
1482 """ This creates a new scenario for version 0.2 and 0.3"""
1483 scenario = scenario_dict["scenario"]
1484 if tenant_id != "any":
1485 check_tenant(mydb, tenant_id)
1486 if "tenant_id" in scenario:
1487 if scenario["tenant_id"] != tenant_id:
1488 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
1489 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1490 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
1491 else:
1492 tenant_id=None
1493
1494 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
1495 for name,vnf in scenario["vnfs"].iteritems():
1496 where={}
1497 where_or={"tenant_id": tenant_id, 'public': "true"}
1498 error_text = ""
1499 error_pos = "'scenario':'vnfs':'" + name + "'"
1500 if 'vnf_id' in vnf:
1501 error_text += " 'vnf_id' " + vnf['vnf_id']
1502 where['uuid'] = vnf['vnf_id']
1503 if 'vnf_name' in vnf:
1504 error_text += " 'vnf_name' " + vnf['vnf_name']
1505 where['name'] = vnf['vnf_name']
1506 if len(where) == 0:
1507 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
1508 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
1509 FROM='vnfs',
1510 WHERE=where,
1511 WHERE_OR=where_or,
1512 WHERE_AND_OR="AND")
1513 if len(vnf_db) == 0:
1514 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1515 elif len(vnf_db) > 1:
1516 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1517 vnf['uuid'] = vnf_db[0]['uuid']
1518 vnf['description'] = vnf_db[0]['description']
1519 vnf['ifaces'] = {}
1520 # get external interfaces
1521 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1522 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1523 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name': None} )
1524 for ext_iface in ext_ifaces:
1525 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
1526 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1527
1528 # 2: Insert net_key and ip_address at every vnf interface
1529 for net_name, net in scenario["networks"].items():
1530 net_type_bridge = False
1531 net_type_data = False
1532 for iface_dict in net["interfaces"]:
1533 if version == "0.2":
1534 temp_dict = iface_dict
1535 ip_address = None
1536 elif version == "0.3":
1537 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
1538 ip_address = iface_dict.get('ip_address', None)
1539 for vnf, iface in temp_dict.items():
1540 if vnf not in scenario["vnfs"]:
1541 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1542 net_name, vnf)
1543 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1544 raise NfvoException(error_text, HTTP_Not_Found)
1545 if iface not in scenario["vnfs"][vnf]['ifaces']:
1546 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1547 .format(net_name, iface)
1548 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1549 raise NfvoException(error_text, HTTP_Bad_Request)
1550 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1551 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1552 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1553 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1554 raise NfvoException(error_text, HTTP_Bad_Request)
1555 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1556 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
1557 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1558 if iface_type == 'mgmt' or iface_type == 'bridge':
1559 net_type_bridge = True
1560 else:
1561 net_type_data = True
1562
1563 if net_type_bridge and net_type_data:
1564 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1565 .format(net_name)
1566 # logger.debug("nfvo.new_scenario " + error_text)
1567 raise NfvoException(error_text, HTTP_Bad_Request)
1568 elif net_type_bridge:
1569 type_ = 'bridge'
1570 else:
1571 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
1572
1573 if net.get("implementation"): # for v0.3
1574 if type_ == "bridge" and net["implementation"] == "underlay":
1575 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1576 "'network':'{}'".format(net_name)
1577 # logger.debug(error_text)
1578 raise NfvoException(error_text, HTTP_Bad_Request)
1579 elif type_ != "bridge" and net["implementation"] == "overlay":
1580 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1581 "'network':'{}'".format(net_name)
1582 # logger.debug(error_text)
1583 raise NfvoException(error_text, HTTP_Bad_Request)
1584 net.pop("implementation")
1585 if "type" in net and version == "0.3": # for v0.3
1586 if type_ == "data" and net["type"] == "e-line":
1587 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
1588 "'e-line' at 'network':'{}'".format(net_name)
1589 # logger.debug(error_text)
1590 raise NfvoException(error_text, HTTP_Bad_Request)
1591 elif type_ == "ptp" and net["type"] == "e-lan":
1592 type_ = "data"
1593
1594 net['type'] = type_
1595 net['name'] = net_name
1596 net['external'] = net.get('external', False)
1597
1598 # 3: insert at database
1599 scenario["nets"] = scenario["networks"]
1600 scenario['tenant_id'] = tenant_id
1601 scenario_id = mydb.new_scenario(scenario)
1602 return scenario_id
1603
1604
1605 def edit_scenario(mydb, tenant_id, scenario_id, data):
1606 data["uuid"] = scenario_id
1607 data["tenant_id"] = tenant_id
1608 c = mydb.edit_scenario( data )
1609 return c
1610
1611
1612 def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
1613 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
1614 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
1615 vims = {datacenter_id: myvim}
1616 myvim_tenant = myvim['tenant_id']
1617 datacenter_name = myvim['name']
1618
1619 rollbackList=[]
1620 try:
1621 #print "Checking that the scenario_id exists and getting the scenario dictionary"
1622 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
1623 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
1624 scenarioDict['datacenter_id'] = datacenter_id
1625 #print '================scenarioDict======================='
1626 #print json.dumps(scenarioDict, indent=4)
1627 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
1628
1629 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
1630 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1631
1632 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1633 auxNetDict['scenario'] = {}
1634
1635 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
1636 for sce_net in scenarioDict['nets']:
1637 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
1638
1639 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
1640 myNetName = myNetName[0:255] #limit length
1641 myNetType = sce_net['type']
1642 myNetDict = {}
1643 myNetDict["name"] = myNetName
1644 myNetDict["type"] = myNetType
1645 myNetDict["tenant_id"] = myvim_tenant
1646 myNetIPProfile = sce_net.get('ip_profile', None)
1647 #TODO:
1648 #We should use the dictionary as input parameter for new_network
1649 #print myNetDict
1650 if not sce_net["external"]:
1651 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
1652 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
1653 sce_net['vim_id'] = network_id
1654 auxNetDict['scenario'][sce_net['uuid']] = network_id
1655 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
1656 sce_net["created"] = True
1657 else:
1658 if sce_net['vim_id'] == None:
1659 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
1660 _, message = rollback(mydb, vims, rollbackList)
1661 logger.error("nfvo.start_scenario: %s", error_text)
1662 raise NfvoException(error_text, HTTP_Bad_Request)
1663 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
1664 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
1665
1666 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
1667 #For each vnf net, we create it and we add it to instanceNetlist.
1668 for sce_vnf in scenarioDict['vnfs']:
1669 for net in sce_vnf['nets']:
1670 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
1671
1672 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
1673 myNetName = myNetName[0:255] #limit length
1674 myNetType = net['type']
1675 myNetDict = {}
1676 myNetDict["name"] = myNetName
1677 myNetDict["type"] = myNetType
1678 myNetDict["tenant_id"] = myvim_tenant
1679 myNetIPProfile = net.get('ip_profile', None)
1680 #print myNetDict
1681 #TODO:
1682 #We should use the dictionary as input parameter for new_network
1683 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
1684 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
1685 net['vim_id'] = network_id
1686 if sce_vnf['uuid'] not in auxNetDict:
1687 auxNetDict[sce_vnf['uuid']] = {}
1688 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
1689 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
1690 net["created"] = True
1691
1692 #print "auxNetDict:"
1693 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
1694
1695 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
1696 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
1697 i = 0
1698 for sce_vnf in scenarioDict['vnfs']:
1699 for vm in sce_vnf['vms']:
1700 i += 1
1701 myVMDict = {}
1702 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
1703 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
1704 #myVMDict['description'] = vm['description']
1705 myVMDict['description'] = myVMDict['name'][0:99]
1706 if not startvms:
1707 myVMDict['start'] = "no"
1708 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
1709 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
1710
1711 #create image at vim in case it not exist
1712 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
1713 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
1714 vm['vim_image_id'] = image_id
1715
1716 #create flavor at vim in case it not exist
1717 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
1718 if flavor_dict['extended']!=None:
1719 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
1720 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
1721 vm['vim_flavor_id'] = flavor_id
1722
1723
1724 myVMDict['imageRef'] = vm['vim_image_id']
1725 myVMDict['flavorRef'] = vm['vim_flavor_id']
1726 myVMDict['networks'] = []
1727 for iface in vm['interfaces']:
1728 netDict = {}
1729 if iface['type']=="data":
1730 netDict['type'] = iface['model']
1731 elif "model" in iface and iface["model"]!=None:
1732 netDict['model']=iface['model']
1733 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
1734 #discover type of interface looking at flavor
1735 for numa in flavor_dict.get('extended',{}).get('numas',[]):
1736 for flavor_iface in numa.get('interfaces',[]):
1737 if flavor_iface.get('name') == iface['internal_name']:
1738 if flavor_iface['dedicated'] == 'yes':
1739 netDict['type']="PF" #passthrough
1740 elif flavor_iface['dedicated'] == 'no':
1741 netDict['type']="VF" #siov
1742 elif flavor_iface['dedicated'] == 'yes:sriov':
1743 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
1744 netDict["mac_address"] = flavor_iface.get("mac_address")
1745 break;
1746 netDict["use"]=iface['type']
1747 if netDict["use"]=="data" and not netDict.get("type"):
1748 #print "netDict", netDict
1749 #print "iface", iface
1750 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'])
1751 if flavor_dict.get('extended')==None:
1752 raise NfvoException(e_text + "After database migration some information is not available. \
1753 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
1754 else:
1755 raise NfvoException(e_text, HTTP_Internal_Server_Error)
1756 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
1757 netDict["type"]="virtual"
1758 if "vpci" in iface and iface["vpci"] is not None:
1759 netDict['vpci'] = iface['vpci']
1760 if "mac" in iface and iface["mac"] is not None:
1761 netDict['mac_address'] = iface['mac']
1762 if "port-security" in iface and iface["port-security"] is not None:
1763 netDict['port_security'] = iface['port-security']
1764 if "floating-ip" in iface and iface["floating-ip"] is not None:
1765 netDict['floating_ip'] = iface['floating-ip']
1766 netDict['name'] = iface['internal_name']
1767 if iface['net_id'] is None:
1768 for vnf_iface in sce_vnf["interfaces"]:
1769 #print iface
1770 #print vnf_iface
1771 if vnf_iface['interface_id']==iface['uuid']:
1772 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
1773 break
1774 else:
1775 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
1776 #skip bridge ifaces not connected to any net
1777 #if 'net_id' not in netDict or netDict['net_id']==None:
1778 # continue
1779 myVMDict['networks'].append(netDict)
1780 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1781 #print myVMDict['name']
1782 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
1783 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
1784 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
1785 vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
1786 myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
1787 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
1788 vm['vim_id'] = vm_id
1789 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
1790 #put interface uuid back to scenario[vnfs][vms[[interfaces]
1791 for net in myVMDict['networks']:
1792 if "vim_id" in net:
1793 for iface in vm['interfaces']:
1794 if net["name"]==iface["internal_name"]:
1795 iface["vim_id"]=net["vim_id"]
1796 break
1797
1798 logger.debug("start scenario Deployment done")
1799 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
1800 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
1801 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
1802 return mydb.get_instance_scenario(instance_id)
1803
1804 except (db_base_Exception, vimconn.vimconnException) as e:
1805 _, message = rollback(mydb, vims, rollbackList)
1806 if isinstance(e, db_base_Exception):
1807 error_text = "Exception at database"
1808 else:
1809 error_text = "Exception at VIM"
1810 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1811 #logger.error("start_scenario %s", error_text)
1812 raise NfvoException(error_text, e.http_code)
1813
1814
1815 def unify_cloud_config(cloud_config_preserve, cloud_config):
1816 ''' join the cloud config information into cloud_config_preserve.
1817 In case of conflict cloud_config_preserve preserves
1818 None is admited
1819 '''
1820 if not cloud_config_preserve and not cloud_config:
1821 return None
1822
1823 new_cloud_config = {"key-pairs":[], "users":[]}
1824 # key-pairs
1825 if cloud_config_preserve:
1826 for key in cloud_config_preserve.get("key-pairs", () ):
1827 if key not in new_cloud_config["key-pairs"]:
1828 new_cloud_config["key-pairs"].append(key)
1829 if cloud_config:
1830 for key in cloud_config.get("key-pairs", () ):
1831 if key not in new_cloud_config["key-pairs"]:
1832 new_cloud_config["key-pairs"].append(key)
1833 if not new_cloud_config["key-pairs"]:
1834 del new_cloud_config["key-pairs"]
1835
1836 # users
1837 if cloud_config:
1838 new_cloud_config["users"] += cloud_config.get("users", () )
1839 if cloud_config_preserve:
1840 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
1841 index_to_delete = []
1842 users = new_cloud_config.get("users", [])
1843 for index0 in range(0,len(users)):
1844 if index0 in index_to_delete:
1845 continue
1846 for index1 in range(index0+1,len(users)):
1847 if index1 in index_to_delete:
1848 continue
1849 if users[index0]["name"] == users[index1]["name"]:
1850 index_to_delete.append(index1)
1851 for key in users[index1].get("key-pairs",()):
1852 if "key-pairs" not in users[index0]:
1853 users[index0]["key-pairs"] = [key]
1854 elif key not in users[index0]["key-pairs"]:
1855 users[index0]["key-pairs"].append(key)
1856 index_to_delete.sort(reverse=True)
1857 for index in index_to_delete:
1858 del users[index]
1859 if not new_cloud_config["users"]:
1860 del new_cloud_config["users"]
1861
1862 #boot-data-drive
1863 if cloud_config and cloud_config.get("boot-data-drive") != None:
1864 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
1865 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
1866 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
1867
1868 # user-data
1869 if cloud_config and cloud_config.get("user-data") != None:
1870 new_cloud_config["user-data"] = cloud_config["user-data"]
1871 if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
1872 new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
1873
1874 # config files
1875 new_cloud_config["config-files"] = []
1876 if cloud_config and cloud_config.get("config-files") != None:
1877 new_cloud_config["config-files"] += cloud_config["config-files"]
1878 if cloud_config_preserve:
1879 for file in cloud_config_preserve.get("config-files", ()):
1880 for index in range(0, len(new_cloud_config["config-files"])):
1881 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
1882 new_cloud_config["config-files"][index] = file
1883 break
1884 else:
1885 new_cloud_config["config-files"].append(file)
1886 if not new_cloud_config["config-files"]:
1887 del new_cloud_config["config-files"]
1888 return new_cloud_config
1889
1890
1891 def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
1892 datacenter_id = None
1893 datacenter_name = None
1894 thread = None
1895 try:
1896 if datacenter_tenant_id:
1897 thread_id = datacenter_tenant_id
1898 thread = vim_threads["running"].get(datacenter_tenant_id)
1899 else:
1900 where_={"td.nfvo_tenant_id": tenant_id}
1901 if datacenter_id_name:
1902 if utils.check_valid_uuid(datacenter_id_name):
1903 datacenter_id = datacenter_id_name
1904 where_["dt.datacenter_id"] = datacenter_id
1905 else:
1906 datacenter_name = datacenter_id_name
1907 where_["d.name"] = datacenter_name
1908 if datacenter_tenant_id:
1909 where_["dt.uuid"] = datacenter_tenant_id
1910 datacenters = mydb.get_rows(
1911 SELECT=("dt.uuid as datacenter_tenant_id",),
1912 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
1913 "join datacenters as d on d.uuid=dt.datacenter_id",
1914 WHERE=where_)
1915 if len(datacenters) > 1:
1916 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1917 elif datacenters:
1918 thread_id = datacenters[0]["datacenter_tenant_id"]
1919 thread = vim_threads["running"].get(thread_id)
1920 if not thread:
1921 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1922 return thread_id, thread
1923 except db_base_Exception as e:
1924 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
1925
1926
1927 def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
1928 datacenter_id = None
1929 datacenter_name = None
1930 if datacenter_id_name:
1931 if utils.check_valid_uuid(datacenter_id_name):
1932 datacenter_id = datacenter_id_name
1933 else:
1934 datacenter_name = datacenter_id_name
1935 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
1936 if len(vims) == 0:
1937 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
1938 elif len(vims)>1:
1939 #print "nfvo.datacenter_action() error. Several datacenters found"
1940 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1941 return vims.keys()[0], vims.values()[0]
1942
1943
1944 def update(d, u):
1945 '''Takes dict d and updates it with the values in dict u.'''
1946 '''It merges all depth levels'''
1947 for k, v in u.iteritems():
1948 if isinstance(v, collections.Mapping):
1949 r = update(d.get(k, {}), v)
1950 d[k] = r
1951 else:
1952 d[k] = u[k]
1953 return d
1954
1955
1956 def create_instance(mydb, tenant_id, instance_dict):
1957 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
1958 # logger.debug("Creating instance...")
1959 scenario = instance_dict["scenario"]
1960
1961 #find main datacenter
1962 myvims = {}
1963 myvim_threads_id = {}
1964 instance_tasks={}
1965 tasks_to_launch={}
1966 datacenter = instance_dict.get("datacenter")
1967 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
1968 myvims[default_datacenter_id] = vim
1969 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
1970 tasks_to_launch[myvim_threads_id[default_datacenter_id]] = []
1971 #myvim_tenant = myvim['tenant_id']
1972 # default_datacenter_name = vim['name']
1973 rollbackList=[]
1974
1975 #print "Checking that the scenario exists and getting the scenario dictionary"
1976 scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
1977
1978 #logger.debug(">>>>>>> Dictionaries before merging")
1979 #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
1980 #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
1981
1982 scenarioDict['datacenter_id'] = default_datacenter_id
1983
1984 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
1985 auxNetDict['scenario'] = {}
1986
1987 logger.debug("Creating instance from scenario-dict:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) #TODO remove
1988 instance_name = instance_dict["name"]
1989 instance_description = instance_dict.get("description")
1990 try:
1991 # 0 check correct parameters
1992 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
1993 found = False
1994 for scenario_net in scenarioDict['nets']:
1995 if net_name == scenario_net["name"]:
1996 found = True
1997 break
1998 if not found:
1999 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name), HTTP_Bad_Request)
2000 if "sites" not in net_instance_desc:
2001 net_instance_desc["sites"] = [ {} ]
2002 site_without_datacenter_field = False
2003 for site in net_instance_desc["sites"]:
2004 if site.get("datacenter"):
2005 if site["datacenter"] not in myvims:
2006 #Add this datacenter to myvims
2007 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
2008 myvims[d] = v
2009 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, site["datacenter"])
2010 tasks_to_launch[myvim_threads_id[d]] = []
2011 site["datacenter"] = d #change name to id
2012 else:
2013 if site_without_datacenter_field:
2014 raise NfvoException("Found more than one entries without datacenter field at instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
2015 site_without_datacenter_field = True
2016 site["datacenter"] = default_datacenter_id #change name to id
2017
2018 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
2019 found=False
2020 for scenario_vnf in scenarioDict['vnfs']:
2021 if vnf_name == scenario_vnf['name']:
2022 found = True
2023 break
2024 if not found:
2025 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
2026 if "datacenter" in vnf_instance_desc:
2027 # Add this datacenter to myvims
2028 if vnf_instance_desc["datacenter"] not in myvims:
2029 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2030 myvims[d] = v
2031 myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
2032 tasks_to_launch[myvim_threads_id[d]] = []
2033 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
2034
2035 #0.1 parse cloud-config parameters
2036 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
2037
2038 #0.2 merge instance information into scenario
2039 #Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2040 #However, this is not possible yet.
2041 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
2042 for scenario_net in scenarioDict['nets']:
2043 if net_name == scenario_net["name"]:
2044 if 'ip-profile' in net_instance_desc:
2045 # translate from input format to database format
2046 ipprofile_in = net_instance_desc['ip-profile']
2047 ipprofile_db = {}
2048 ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address')
2049 ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4')
2050 ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address')
2051 ipprofile_db['dns_address'] = ipprofile_in.get('dns-address')
2052 if isinstance(ipprofile_db['dns_address'], (list, tuple)):
2053 ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address'])
2054 if 'dhcp' in ipprofile_in:
2055 ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address')
2056 ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True)
2057 ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' )
2058 if 'ip_profile' not in scenario_net:
2059 scenario_net['ip_profile'] = ipprofile_db
2060 else:
2061 update(scenario_net['ip_profile'], ipprofile_db)
2062 for interface in net_instance_desc.get('interfaces', () ):
2063 if 'ip_address' in interface:
2064 for vnf in scenarioDict['vnfs']:
2065 if interface['vnf'] == vnf['name']:
2066 for vnf_interface in vnf['interfaces']:
2067 if interface['vnf_interface'] == vnf_interface['external_name']:
2068 vnf_interface['ip_address']=interface['ip_address']
2069
2070 #logger.debug(">>>>>>>> Merged dictionary")
2071 logger.debug("Creating instance scenario-dict MERGED:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2072
2073
2074 # 1. Creating new nets (sce_nets) in the VIM"
2075 for sce_net in scenarioDict['nets']:
2076 sce_net["vim_id_sites"]={}
2077 descriptor_net = instance_dict.get("networks",{}).get(sce_net["name"],{})
2078 net_name = descriptor_net.get("vim-network-name")
2079 auxNetDict['scenario'][sce_net['uuid']] = {}
2080
2081 sites = descriptor_net.get("sites", [ {} ])
2082 for site in sites:
2083 if site.get("datacenter"):
2084 vim = myvims[ site["datacenter"] ]
2085 datacenter_id = site["datacenter"]
2086 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
2087 else:
2088 vim = myvims[ default_datacenter_id ]
2089 datacenter_id = default_datacenter_id
2090 myvim_thread_id = myvim_threads_id[default_datacenter_id]
2091 net_type = sce_net['type']
2092 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
2093 if sce_net["external"]:
2094 if not net_name:
2095 net_name = sce_net["name"]
2096 if "netmap-use" in site or "netmap-create" in site:
2097 create_network = False
2098 lookfor_network = False
2099 if "netmap-use" in site:
2100 lookfor_network = True
2101 if utils.check_valid_uuid(site["netmap-use"]):
2102 filter_text = "scenario id '%s'" % site["netmap-use"]
2103 lookfor_filter["id"] = site["netmap-use"]
2104 else:
2105 filter_text = "scenario name '%s'" % site["netmap-use"]
2106 lookfor_filter["name"] = site["netmap-use"]
2107 if "netmap-create" in site:
2108 create_network = True
2109 net_vim_name = net_name
2110 if site["netmap-create"]:
2111 net_vim_name = site["netmap-create"]
2112
2113 elif sce_net['vim_id'] != None:
2114 #there is a netmap at datacenter_nets database #TODO REVISE!!!!
2115 create_network = False
2116 lookfor_network = True
2117 lookfor_filter["id"] = sce_net['vim_id']
2118 filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
2119 #look for network at datacenter and return error
2120 else:
2121 #There is not a netmap, look at datacenter for a net with this name and create if not found
2122 create_network = True
2123 lookfor_network = True
2124 lookfor_filter["name"] = sce_net["name"]
2125 net_vim_name = sce_net["name"]
2126 filter_text = "scenario name '%s'" % sce_net["name"]
2127 else:
2128 if not net_name:
2129 net_name = "%s.%s" %(instance_name, sce_net["name"])
2130 net_name = net_name[:255] #limit length
2131 net_vim_name = net_name
2132 create_network = True
2133 lookfor_network = False
2134
2135 if lookfor_network:
2136 vim_nets = vim.get_network_list(filter_dict=lookfor_filter)
2137 if len(vim_nets) > 1:
2138 raise NfvoException("More than one candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2139 elif len(vim_nets) == 0:
2140 if not create_network:
2141 raise NfvoException("No candidate VIM network found for " + filter_text, HTTP_Bad_Request )
2142 else:
2143 sce_net["vim_id_sites"][datacenter_id] = vim_nets[0]['id']
2144 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = vim_nets[0]['id']
2145 create_network = False
2146 if create_network:
2147 #if network is not external
2148 task = new_task("new-net", (net_vim_name, net_type, sce_net.get('ip_profile',None)))
2149 task_id = task["id"]
2150 instance_tasks[task_id] = task
2151 tasks_to_launch[myvim_thread_id].append(task)
2152 #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
2153 sce_net["vim_id_sites"][datacenter_id] = task_id
2154 auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = task_id
2155 rollbackList.append({'what':'network', 'where':'vim', 'vim_id':datacenter_id, 'uuid':task_id})
2156 sce_net["created"] = True
2157
2158 # 2. Creating new nets (vnf internal nets) in the VIM"
2159 #For each vnf net, we create it and we add it to instanceNetlist.
2160 for sce_vnf in scenarioDict['vnfs']:
2161 for net in sce_vnf['nets']:
2162 if sce_vnf.get("datacenter"):
2163 vim = myvims[ sce_vnf["datacenter"] ]
2164 datacenter_id = sce_vnf["datacenter"]
2165 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"]]
2166 else:
2167 vim = myvims[ default_datacenter_id ]
2168 datacenter_id = default_datacenter_id
2169 myvim_thread_id = myvim_threads_id[default_datacenter_id]
2170 descriptor_net = instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
2171 net_name = descriptor_net.get("name")
2172 if not net_name:
2173 net_name = "%s.%s" %(instance_name, net["name"])
2174 net_name = net_name[:255] #limit length
2175 net_type = net['type']
2176 task = new_task("new-net", (net_name, net_type, net.get('ip_profile',None)))
2177 task_id = task["id"]
2178 instance_tasks[task_id] = task
2179 tasks_to_launch[myvim_thread_id].append(task)
2180 # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
2181 net['vim_id'] = task_id
2182 if sce_vnf['uuid'] not in auxNetDict:
2183 auxNetDict[sce_vnf['uuid']] = {}
2184 auxNetDict[sce_vnf['uuid']][net['uuid']] = task_id
2185 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':task_id})
2186 net["created"] = True
2187
2188
2189 #print "auxNetDict:"
2190 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2191
2192 # 3. Creating new vm instances in the VIM
2193 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2194 for sce_vnf in scenarioDict['vnfs']:
2195 if sce_vnf.get("datacenter"):
2196 vim = myvims[ sce_vnf["datacenter"] ]
2197 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
2198 datacenter_id = sce_vnf["datacenter"]
2199 else:
2200 vim = myvims[ default_datacenter_id ]
2201 myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
2202 datacenter_id = default_datacenter_id
2203 sce_vnf["datacenter_id"] = datacenter_id
2204 i = 0
2205 for vm in sce_vnf['vms']:
2206 i += 1
2207 myVMDict = {}
2208 myVMDict['name'] = "{}.{}.{}".format(instance_name,sce_vnf['name'],chr(96+i))
2209 myVMDict['description'] = myVMDict['name'][0:99]
2210 # if not startvms:
2211 # myVMDict['start'] = "no"
2212 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2213 #create image at vim in case it not exist
2214 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
2215 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
2216 vm['vim_image_id'] = image_id
2217
2218 #create flavor at vim in case it not exist
2219 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
2220 if flavor_dict['extended']!=None:
2221 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
2222 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2223
2224 #Obtain information for additional disks
2225 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2226 if not extended_flavor_dict:
2227 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2228 return
2229
2230 #extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2231 myVMDict['disks'] = None
2232 extended_info = extended_flavor_dict[0]['extended']
2233 if extended_info != None:
2234 extended_flavor_dict_yaml = yaml.load(extended_info)
2235 if 'disks' in extended_flavor_dict_yaml:
2236 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2237
2238 vm['vim_flavor_id'] = flavor_id
2239 myVMDict['imageRef'] = vm['vim_image_id']
2240 myVMDict['flavorRef'] = vm['vim_flavor_id']
2241 myVMDict['networks'] = []
2242 task_depends = {}
2243 #TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
2244 for iface in vm['interfaces']:
2245 netDict = {}
2246 if iface['type']=="data":
2247 netDict['type'] = iface['model']
2248 elif "model" in iface and iface["model"]!=None:
2249 netDict['model']=iface['model']
2250 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2251 #discover type of interface looking at flavor
2252 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2253 for flavor_iface in numa.get('interfaces',[]):
2254 if flavor_iface.get('name') == iface['internal_name']:
2255 if flavor_iface['dedicated'] == 'yes':
2256 netDict['type']="PF" #passthrough
2257 elif flavor_iface['dedicated'] == 'no':
2258 netDict['type']="VF" #siov
2259 elif flavor_iface['dedicated'] == 'yes:sriov':
2260 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2261 netDict["mac_address"] = flavor_iface.get("mac_address")
2262 break;
2263 netDict["use"]=iface['type']
2264 if netDict["use"]=="data" and not netDict.get("type"):
2265 #print "netDict", netDict
2266 #print "iface", iface
2267 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'])
2268 if flavor_dict.get('extended')==None:
2269 raise NfvoException(e_text + "After database migration some information is not available. \
2270 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
2271 else:
2272 raise NfvoException(e_text, HTTP_Internal_Server_Error)
2273 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2274 netDict["type"]="virtual"
2275 if "vpci" in iface and iface["vpci"] is not None:
2276 netDict['vpci'] = iface['vpci']
2277 if "mac" in iface and iface["mac"] is not None:
2278 netDict['mac_address'] = iface['mac']
2279 if "port-security" in iface and iface["port-security"] is not None:
2280 netDict['port_security'] = iface['port-security']
2281 if "floating-ip" in iface and iface["floating-ip"] is not None:
2282 netDict['floating_ip'] = iface['floating-ip']
2283 netDict['name'] = iface['internal_name']
2284 if iface['net_id'] is None:
2285 for vnf_iface in sce_vnf["interfaces"]:
2286 #print iface
2287 #print vnf_iface
2288 if vnf_iface['interface_id']==iface['uuid']:
2289 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id]
2290 break
2291 else:
2292 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2293 if netDict.get('net_id') and is_task_id(netDict['net_id']):
2294 task_depends[netDict['net_id']] = instance_tasks[netDict['net_id']]
2295 #skip bridge ifaces not connected to any net
2296 #if 'net_id' not in netDict or netDict['net_id']==None:
2297 # continue
2298 myVMDict['networks'].append(netDict)
2299 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2300 #print myVMDict['name']
2301 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2302 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2303 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2304 if vm.get("boot_data"):
2305 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
2306 else:
2307 cloud_config_vm = cloud_config
2308 task = new_task("new-vm", (myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2309 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2310 cloud_config_vm, myVMDict['disks']), depends=task_depends)
2311 instance_tasks[task["id"]] = task
2312 tasks_to_launch[myvim_thread_id].append(task)
2313 vm_id = task["id"]
2314 vm['vim_id'] = vm_id
2315 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2316 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2317 for net in myVMDict['networks']:
2318 if "vim_id" in net:
2319 for iface in vm['interfaces']:
2320 if net["name"]==iface["internal_name"]:
2321 iface["vim_id"]=net["vim_id"]
2322 break
2323 scenarioDict["datacenter2tenant"] = myvim_threads_id
2324 logger.debug("create_instance Deployment done scenarioDict: %s",
2325 yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
2326 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
2327 for myvim_thread_id,task_list in tasks_to_launch.items():
2328 for task in task_list:
2329 vim_threads["running"][myvim_thread_id].insert_task(task)
2330
2331 global_instance_tasks[instance_id] = instance_tasks
2332 # Update database with those ended instance_tasks
2333 # for task in instance_tasks.values():
2334 # if task["status"] == "ok":
2335 # if task["name"] == "new-vm":
2336 # mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
2337 # WHERE={"vim_vm_id": task["id"]})
2338 # elif task["name"] == "new-net":
2339 # mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
2340 # WHERE={"vim_net_id": task["id"]})
2341 return mydb.get_instance_scenario(instance_id)
2342 except (NfvoException, vimconn.vimconnException,db_base_Exception) as e:
2343 message = rollback(mydb, myvims, rollbackList)
2344 if isinstance(e, db_base_Exception):
2345 error_text = "database Exception"
2346 elif isinstance(e, vimconn.vimconnException):
2347 error_text = "VIM Exception"
2348 else:
2349 error_text = "Exception"
2350 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2351 #logger.error("create_instance: %s", error_text)
2352 raise NfvoException(error_text, e.http_code)
2353
2354
2355 def delete_instance(mydb, tenant_id, instance_id):
2356 #print "Checking that the instance_id exists and getting the instance dictionary"
2357 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
2358 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2359 tenant_id = instanceDict["tenant_id"]
2360 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2361
2362 #1. Delete from Database
2363 message = mydb.delete_instance_scenario(instance_id, tenant_id)
2364
2365 #2. delete from VIM
2366 error_msg = ""
2367 myvims = {}
2368 myvim_threads = {}
2369
2370 #2.1 deleting VMs
2371 #vm_fail_list=[]
2372 for sce_vnf in instanceDict['vnfs']:
2373 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2374 if datacenter_key not in myvims:
2375 try:
2376 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2377 except NfvoException as e:
2378 logger.error(str(e))
2379 myvim_thread = None
2380 myvim_threads[datacenter_key] = myvim_thread
2381 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
2382 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2383 if len(vims) == 0:
2384 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
2385 sce_vnf["datacenter_tenant_id"]))
2386 myvims[datacenter_key] = None
2387 else:
2388 myvims[datacenter_key] = vims.values()[0]
2389 myvim = myvims[datacenter_key]
2390 myvim_thread = myvim_threads[datacenter_key]
2391 for vm in sce_vnf['vms']:
2392 if not myvim:
2393 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2394 continue
2395 try:
2396 task=None
2397 if is_task_id(vm['vim_vm_id']):
2398 task_id = vm['vim_vm_id']
2399 old_task = global_instance_tasks[instance_id].get(task_id)
2400 if not old_task:
2401 error_msg += "\n VM was scheduled for create, but task {} is not found".format(task_id)
2402 continue
2403 with task_lock:
2404 if old_task["status"] == "enqueued":
2405 old_task["status"] = "deleted"
2406 elif old_task["status"] == "error":
2407 continue
2408 elif old_task["status"] == "processing":
2409 task = new_task("del-vm", (task_id, vm["interfaces"]), depends={task_id: old_task})
2410 else: #ok
2411 task = new_task("del-vm", (old_task["result"], vm["interfaces"]))
2412 else:
2413 task = new_task("del-vm", (vm['vim_vm_id'], vm["interfaces"]) )
2414 if task:
2415 myvim_thread.insert_task(task)
2416 except vimconn.vimconnNotFoundException as e:
2417 error_msg+="\n VM VIM_id={} not found at datacenter={}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
2418 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
2419 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
2420 except vimconn.vimconnException as e:
2421 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
2422 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
2423 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
2424
2425 #2.2 deleting NETS
2426 #net_fail_list=[]
2427 for net in instanceDict['nets']:
2428 if not net['created']:
2429 continue #skip not created nets
2430 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2431 if datacenter_key not in myvims:
2432 try:
2433 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2434 except NfvoException as e:
2435 logger.error(str(e))
2436 myvim_thread = None
2437 myvim_threads[datacenter_key] = myvim_thread
2438 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
2439 datacenter_tenant_id=net["datacenter_tenant_id"])
2440 if len(vims) == 0:
2441 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2442 myvims[datacenter_key] = None
2443 else:
2444 myvims[datacenter_key] = vims.values()[0]
2445 myvim = myvims[datacenter_key]
2446 myvim_thread = myvim_threads[datacenter_key]
2447
2448 if not myvim:
2449 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
2450 continue
2451 try:
2452 task = None
2453 if is_task_id(net['vim_net_id']):
2454 task_id = net['vim_net_id']
2455 old_task = global_instance_tasks[instance_id].get(task_id)
2456 if not old_task:
2457 error_msg += "\n NET was scheduled for create, but task {} is not found".format(task_id)
2458 continue
2459 with task_lock:
2460 if old_task["status"] == "enqueued":
2461 old_task["status"] = "deleted"
2462 elif old_task["status"] == "error":
2463 continue
2464 elif old_task["status"] == "processing":
2465 task = new_task("del-net", task_id, depends={task_id: old_task})
2466 else: # ok
2467 task = new_task("del-net", old_task["result"])
2468 else:
2469 task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id']))
2470 if task:
2471 myvim_thread.insert_task(task)
2472 except vimconn.vimconnNotFoundException as e:
2473 error_msg += "\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
2474 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
2475 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
2476 except vimconn.vimconnException as e:
2477 error_msg += "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'],
2478 net["datacenter_id"],
2479 e.http_code, str(e))
2480 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
2481 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
2482 if len(error_msg) > 0:
2483 return 'instance ' + message + ' deleted but some elements could not be deleted, or already deleted (error: 404) from VIM: ' + error_msg
2484 else:
2485 return 'instance ' + message + ' deleted'
2486
2487
2488 def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
2489 '''Refreshes a scenario instance. It modifies instanceDict'''
2490 '''Returns:
2491 - 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
2492 - error_msg
2493 '''
2494 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
2495 # #print "nfvo.refresh_instance begins"
2496 # #print json.dumps(instanceDict, indent=4)
2497 #
2498 # #print "Getting the VIM URL and the VIM tenant_id"
2499 # myvims={}
2500 #
2501 # # 1. Getting VIM vm and net list
2502 # vms_updated = [] #List of VM instance uuids in openmano that were updated
2503 # vms_notupdated=[]
2504 # vm_list = {}
2505 # for sce_vnf in instanceDict['vnfs']:
2506 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
2507 # if datacenter_key not in vm_list:
2508 # vm_list[datacenter_key] = []
2509 # if datacenter_key not in myvims:
2510 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
2511 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
2512 # if len(vims) == 0:
2513 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
2514 # myvims[datacenter_key] = None
2515 # else:
2516 # myvims[datacenter_key] = vims.values()[0]
2517 # for vm in sce_vnf['vms']:
2518 # vm_list[datacenter_key].append(vm['vim_vm_id'])
2519 # vms_notupdated.append(vm["uuid"])
2520 #
2521 # nets_updated = [] #List of VM instance uuids in openmano that were updated
2522 # nets_notupdated=[]
2523 # net_list = {}
2524 # for net in instanceDict['nets']:
2525 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
2526 # if datacenter_key not in net_list:
2527 # net_list[datacenter_key] = []
2528 # if datacenter_key not in myvims:
2529 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
2530 # datacenter_tenant_id=net["datacenter_tenant_id"])
2531 # if len(vims) == 0:
2532 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
2533 # myvims[datacenter_key] = None
2534 # else:
2535 # myvims[datacenter_key] = vims.values()[0]
2536 #
2537 # net_list[datacenter_key].append(net['vim_net_id'])
2538 # nets_notupdated.append(net["uuid"])
2539 #
2540 # # 1. Getting the status of all VMs
2541 # vm_dict={}
2542 # for datacenter_key in myvims:
2543 # if not vm_list.get(datacenter_key):
2544 # continue
2545 # failed = True
2546 # failed_message=""
2547 # if not myvims[datacenter_key]:
2548 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2549 # else:
2550 # try:
2551 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
2552 # failed = False
2553 # except vimconn.vimconnException as e:
2554 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2555 # failed_message = str(e)
2556 # if failed:
2557 # for vm in vm_list[datacenter_key]:
2558 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2559 #
2560 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
2561 # for sce_vnf in instanceDict['vnfs']:
2562 # for vm in sce_vnf['vms']:
2563 # vm_id = vm['vim_vm_id']
2564 # interfaces = vm_dict[vm_id].pop('interfaces', [])
2565 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
2566 # has_mgmt_iface = False
2567 # for iface in vm["interfaces"]:
2568 # if iface["type"]=="mgmt":
2569 # has_mgmt_iface = True
2570 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
2571 # vm_dict[vm_id]['status'] = "ACTIVE"
2572 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
2573 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
2574 # 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'):
2575 # vm['status'] = vm_dict[vm_id]['status']
2576 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
2577 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
2578 # # 2.1. Update in openmano DB the VMs whose status changed
2579 # try:
2580 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
2581 # vms_notupdated.remove(vm["uuid"])
2582 # if updates>0:
2583 # vms_updated.append(vm["uuid"])
2584 # except db_base_Exception as e:
2585 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2586 # # 2.2. Update in openmano DB the interface VMs
2587 # for interface in interfaces:
2588 # #translate from vim_net_id to instance_net_id
2589 # network_id_list=[]
2590 # for net in instanceDict['nets']:
2591 # if net["vim_net_id"] == interface["vim_net_id"]:
2592 # network_id_list.append(net["uuid"])
2593 # if not network_id_list:
2594 # continue
2595 # del interface["vim_net_id"]
2596 # try:
2597 # for network_id in network_id_list:
2598 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
2599 # except db_base_Exception as e:
2600 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
2601 #
2602 # # 3. Getting the status of all nets
2603 # net_dict = {}
2604 # for datacenter_key in myvims:
2605 # if not net_list.get(datacenter_key):
2606 # continue
2607 # failed = True
2608 # failed_message = ""
2609 # if not myvims[datacenter_key]:
2610 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
2611 # else:
2612 # try:
2613 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
2614 # failed = False
2615 # except vimconn.vimconnException as e:
2616 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
2617 # failed_message = str(e)
2618 # if failed:
2619 # for net in net_list[datacenter_key]:
2620 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
2621 #
2622 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
2623 # # TODO: update nets inside a vnf
2624 # for net in instanceDict['nets']:
2625 # net_id = net['vim_net_id']
2626 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
2627 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
2628 # 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'):
2629 # net['status'] = net_dict[net_id]['status']
2630 # net['error_msg'] = net_dict[net_id].get('error_msg')
2631 # net['vim_info'] = net_dict[net_id].get('vim_info')
2632 # # 5.1. Update in openmano DB the nets whose status changed
2633 # try:
2634 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
2635 # nets_notupdated.remove(net["uuid"])
2636 # if updated>0:
2637 # nets_updated.append(net["uuid"])
2638 # except db_base_Exception as e:
2639 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
2640 #
2641 # # Returns appropriate output
2642 # #print "nfvo.refresh_instance finishes"
2643 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
2644 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
2645 instance_id = instanceDict['uuid']
2646 # if len(vms_notupdated)+len(nets_notupdated)>0:
2647 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
2648 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
2649
2650 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
2651
2652
2653 def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
2654 #print "Checking that the instance_id exists and getting the instance dictionary"
2655 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
2656 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
2657
2658 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2659 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
2660 if len(vims) == 0:
2661 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
2662 myvim = vims.values()[0]
2663
2664
2665 input_vnfs = action_dict.pop("vnfs", [])
2666 input_vms = action_dict.pop("vms", [])
2667 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
2668 vm_result = {}
2669 vm_error = 0
2670 vm_ok = 0
2671 for sce_vnf in instanceDict['vnfs']:
2672 for vm in sce_vnf['vms']:
2673 if not action_over_all:
2674 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
2675 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
2676 continue
2677 try:
2678 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
2679 if "console" in action_dict:
2680 if not global_config["http_console_proxy"]:
2681 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2682 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2683 protocol=data["protocol"],
2684 ip = data["server"],
2685 port = data["port"],
2686 suffix = data["suffix"]),
2687 "name":vm['name']
2688 }
2689 vm_ok +=1
2690 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
2691 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
2692 "description": "this console is only reachable by local interface",
2693 "name":vm['name']
2694 }
2695 vm_error+=1
2696 else:
2697 #print "console data", data
2698 try:
2699 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
2700 vm_result[ vm['uuid'] ] = {"vim_result": 200,
2701 "description": "{protocol}//{ip}:{port}/{suffix}".format(
2702 protocol=data["protocol"],
2703 ip = global_config["http_console_host"],
2704 port = console_thread.port,
2705 suffix = data["suffix"]),
2706 "name":vm['name']
2707 }
2708 vm_ok +=1
2709 except NfvoException as e:
2710 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2711 vm_error+=1
2712
2713 else:
2714 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
2715 vm_ok +=1
2716 except vimconn.vimconnException as e:
2717 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
2718 vm_error+=1
2719
2720 if vm_ok==0: #all goes wrong
2721 return vm_result
2722 else:
2723 return vm_result
2724
2725
2726 def create_or_use_console_proxy_thread(console_server, console_port):
2727 #look for a non-used port
2728 console_thread_key = console_server + ":" + str(console_port)
2729 if console_thread_key in global_config["console_thread"]:
2730 #global_config["console_thread"][console_thread_key].start_timeout()
2731 return global_config["console_thread"][console_thread_key]
2732
2733 for port in global_config["console_port_iterator"]():
2734 #print "create_or_use_console_proxy_thread() port:", port
2735 if port in global_config["console_ports"]:
2736 continue
2737 try:
2738 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
2739 clithread.start()
2740 global_config["console_thread"][console_thread_key] = clithread
2741 global_config["console_ports"][port] = console_thread_key
2742 return clithread
2743 except cli.ConsoleProxyExceptionPortUsed as e:
2744 #port used, try with onoher
2745 continue
2746 except cli.ConsoleProxyException as e:
2747 raise NfvoException(str(e), HTTP_Bad_Request)
2748 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
2749
2750
2751 def check_tenant(mydb, tenant_id):
2752 '''check that tenant exists at database'''
2753 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
2754 if not tenant:
2755 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
2756 return
2757
2758
2759 def new_tenant(mydb, tenant_dict):
2760 tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, add_uuid=True)
2761 return tenant_id
2762
2763
2764 def delete_tenant(mydb, tenant):
2765 #get nfvo_tenant info
2766
2767 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
2768 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
2769 return tenant_dict['uuid'] + " " + tenant_dict["name"]
2770
2771
2772 def new_datacenter(mydb, datacenter_descriptor):
2773 if "config" in datacenter_descriptor:
2774 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
2775 #Check that datacenter-type is correct
2776 datacenter_type = datacenter_descriptor.get("type", "openvim");
2777 module_info = None
2778 try:
2779 module = "vimconn_" + datacenter_type
2780 pkg = __import__("osm_ro." + module)
2781 vim_conn = getattr(pkg, module)
2782 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
2783 except (IOError, ImportError):
2784 # if module_info and module_info[0]:
2785 # file.close(module_info[0])
2786 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
2787
2788 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True)
2789 return datacenter_id
2790
2791
2792 def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
2793 # obtain data, check that only one exist
2794 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
2795
2796 # edit data
2797 datacenter_id = datacenter['uuid']
2798 where={'uuid': datacenter['uuid']}
2799 remove_port_mapping = False
2800 if "config" in datacenter_descriptor:
2801 if datacenter_descriptor['config'] != None:
2802 try:
2803 new_config_dict = datacenter_descriptor["config"]
2804 #delete null fields
2805 to_delete=[]
2806 for k in new_config_dict:
2807 if new_config_dict[k] == None:
2808 to_delete.append(k)
2809 if k == 'sdn-controller':
2810 remove_port_mapping = True
2811
2812 config_text = datacenter.get("config")
2813 if not config_text:
2814 config_text = '{}'
2815 config_dict = yaml.load(config_text)
2816 config_dict.update(new_config_dict)
2817 #delete null fields
2818 for k in to_delete:
2819 del config_dict[k]
2820 except Exception as e:
2821 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
2822 if config_dict:
2823 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
2824 else:
2825 datacenter_descriptor["config"] = None
2826 if remove_port_mapping:
2827 try:
2828 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
2829 except ovimException as e:
2830 logger.error("Error deleting datacenter-port-mapping " + str(e))
2831
2832 mydb.update_rows('datacenters', datacenter_descriptor, where)
2833 return datacenter_id
2834
2835
2836 def delete_datacenter(mydb, datacenter):
2837 #get nfvo_tenant info
2838 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
2839 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
2840 try:
2841 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
2842 except ovimException as e:
2843 logger.error("Error deleting datacenter-port-mapping " + str(e))
2844 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
2845
2846
2847 def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
2848 #get datacenter info
2849 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, vim_passwd=vim_password)
2850 datacenter_name = myvim["name"]
2851
2852 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
2853
2854 # get nfvo_tenant info
2855 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
2856 if vim_tenant_name==None:
2857 vim_tenant_name=tenant_dict['name']
2858
2859 #check that this association does not exist before
2860 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
2861 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2862 if len(tenants_datacenters)>0:
2863 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
2864
2865 vim_tenant_id_exist_atdb=False
2866 if not create_vim_tenant:
2867 where_={"datacenter_id": datacenter_id}
2868 if vim_tenant_id!=None:
2869 where_["vim_tenant_id"] = vim_tenant_id
2870 if vim_tenant_name!=None:
2871 where_["vim_tenant_name"] = vim_tenant_name
2872 #check if vim_tenant_id is already at database
2873 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
2874 if len(datacenter_tenants_dict)>=1:
2875 datacenter_tenants_dict = datacenter_tenants_dict[0]
2876 vim_tenant_id_exist_atdb=True
2877 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
2878 else: #result=0
2879 datacenter_tenants_dict = {}
2880 #insert at table datacenter_tenants
2881 else: #if vim_tenant_id==None:
2882 #create tenant at VIM if not provided
2883 try:
2884 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
2885 except vimconn.vimconnException as e:
2886 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error)
2887 datacenter_tenants_dict = {}
2888 datacenter_tenants_dict["created"]="true"
2889
2890 #fill datacenter_tenants table
2891 if not vim_tenant_id_exist_atdb:
2892 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
2893 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
2894 datacenter_tenants_dict["user"] = vim_username
2895 datacenter_tenants_dict["passwd"] = vim_password
2896 datacenter_tenants_dict["datacenter_id"] = datacenter_id
2897 if config:
2898 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
2899 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True)
2900 datacenter_tenants_dict["uuid"] = id_
2901
2902 #fill tenants_datacenters table
2903 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
2904 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
2905 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
2906 # create thread
2907 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
2908 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
2909 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
2910 db=db, db_lock=db_lock, ovim=ovim)
2911 new_thread.start()
2912 thread_id = datacenter_tenants_dict["uuid"]
2913 vim_threads["running"][thread_id] = new_thread
2914 return datacenter_id
2915
2916
2917 def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
2918 vim_username=None, vim_password=None, config=None):
2919 #Obtain the data of this datacenter_tenant_id
2920 vim_data = mydb.get_rows(
2921 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
2922 "datacenter_tenants.passwd", "datacenter_tenants.config"),
2923 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
2924 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
2925 "tenants_datacenters.datacenter_id": datacenter_id})
2926
2927 logger.debug(str(vim_data))
2928 if len(vim_data) < 1:
2929 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
2930
2931 v = vim_data[0]
2932 if v['config']:
2933 v['config'] = yaml.load(v['config'])
2934
2935 if vim_tenant_id:
2936 v['vim_tenant_id'] = vim_tenant_id
2937 if vim_tenant_name:
2938 v['vim_tenant_name'] = vim_tenant_name
2939 if vim_username:
2940 v['user'] = vim_username
2941 if vim_password:
2942 v['passwd'] = vim_password
2943 if config:
2944 if not v['config']:
2945 v['config'] = {}
2946 v['config'].update(config)
2947
2948 logger.debug(str(v))
2949 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
2950 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
2951 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
2952
2953 return datacenter_id
2954
2955 def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
2956 #get datacenter info
2957 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter)
2958
2959 #get nfvo_tenant info
2960 if not tenant_id or tenant_id=="any":
2961 tenant_uuid = None
2962 else:
2963 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
2964 tenant_uuid = tenant_dict['uuid']
2965
2966 #check that this association exist before
2967 tenants_datacenter_dict={"datacenter_id":datacenter_id }
2968 if tenant_uuid:
2969 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
2970 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2971 if len(tenant_datacenter_list)==0 and tenant_uuid:
2972 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
2973
2974 #delete this association
2975 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
2976
2977 #get vim_tenant info and deletes
2978 warning=''
2979 for tenant_datacenter_item in tenant_datacenter_list:
2980 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2981 #try to delete vim:tenant
2982 try:
2983 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
2984 if vim_tenant_dict['created']=='true':
2985 #delete tenant at VIM if created by NFVO
2986 try:
2987 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
2988 except vimconn.vimconnException as e:
2989 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
2990 logger.warn(warning)
2991 except db_base_Exception as e:
2992 logger.error("Cannot delete datacenter_tenants " + str(e))
2993 pass # the error will be caused because dependencies, vim_tenant can not be deleted
2994 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
2995 thread = vim_threads["running"][thread_id]
2996 thread.insert_task(new_task("exit", None))
2997 vim_threads["deleting"][thread_id] = thread
2998 return "datacenter {} detached. {}".format(datacenter_id, warning)
2999
3000
3001 def datacenter_action(mydb, tenant_id, datacenter, action_dict):
3002 #DEPRECATED
3003 #get datacenter info
3004 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3005
3006 if 'net-update' in action_dict:
3007 try:
3008 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
3009 #print content
3010 except vimconn.vimconnException as e:
3011 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
3012 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3013 #update nets Change from VIM format to NFVO format
3014 net_list=[]
3015 for net in nets:
3016 net_nfvo={'datacenter_id': datacenter_id}
3017 net_nfvo['name'] = net['name']
3018 #net_nfvo['description']= net['name']
3019 net_nfvo['vim_net_id'] = net['id']
3020 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3021 net_nfvo['shared'] = net['shared']
3022 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3023 net_list.append(net_nfvo)
3024 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
3025 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
3026 return inserted
3027 elif 'net-edit' in action_dict:
3028 net = action_dict['net-edit'].pop('net')
3029 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
3030 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
3031 WHERE={'datacenter_id':datacenter_id, what: net})
3032 return result
3033 elif 'net-delete' in action_dict:
3034 net = action_dict['net-deelte'].get('net')
3035 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
3036 result = mydb.delete_row(FROM='datacenter_nets',
3037 WHERE={'datacenter_id':datacenter_id, what: net})
3038 return result
3039
3040 else:
3041 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
3042
3043
3044 def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
3045 #get datacenter info
3046 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3047
3048 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
3049 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
3050 WHERE={'datacenter_id':datacenter_id, what: netmap})
3051 return result
3052
3053
3054 def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
3055 #get datacenter info
3056 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3057 filter_dict={}
3058 if action_dict:
3059 action_dict = action_dict["netmap"]
3060 if 'vim_id' in action_dict:
3061 filter_dict["id"] = action_dict['vim_id']
3062 if 'vim_name' in action_dict:
3063 filter_dict["name"] = action_dict['vim_name']
3064 else:
3065 filter_dict["shared"] = True
3066
3067 try:
3068 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
3069 except vimconn.vimconnException as e:
3070 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3071 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3072 if len(vim_nets)>1 and action_dict:
3073 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
3074 elif len(vim_nets)==0: # and action_dict:
3075 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
3076 net_list=[]
3077 for net in vim_nets:
3078 net_nfvo={'datacenter_id': datacenter_id}
3079 if action_dict and "name" in action_dict:
3080 net_nfvo['name'] = action_dict['name']
3081 else:
3082 net_nfvo['name'] = net['name']
3083 #net_nfvo['description']= net['name']
3084 net_nfvo['vim_net_id'] = net['id']
3085 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3086 net_nfvo['shared'] = net['shared']
3087 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3088 try:
3089 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
3090 net_nfvo["status"] = "OK"
3091 net_nfvo["uuid"] = net_id
3092 except db_base_Exception as e:
3093 if action_dict:
3094 raise
3095 else:
3096 net_nfvo["status"] = "FAIL: " + str(e)
3097 net_list.append(net_nfvo)
3098 return net_list
3099
3100 def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
3101 # obtain all network data
3102 try:
3103 if utils.check_valid_uuid(network_id):
3104 filter_dict = {"id": network_id}
3105 else:
3106 filter_dict = {"name": network_id}
3107
3108 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3109 network = myvim.get_network_list(filter_dict=filter_dict)
3110 except vimconn.vimconnException as e:
3111 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
3112 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
3113
3114 # ensure the network is defined
3115 if len(network) == 0:
3116 raise NfvoException("Network {} is not present in the system".format(network_id),
3117 HTTP_Bad_Request)
3118
3119 # ensure there is only one network with the provided name
3120 if len(network) > 1:
3121 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request)
3122
3123 # ensure it is a dataplane network
3124 if network[0]['type'] != 'data':
3125 return None
3126
3127 # ensure we use the id
3128 network_id = network[0]['id']
3129
3130 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
3131 # and with instance_scenario_id==NULL
3132 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
3133 search_dict = {'vim_net_id': network_id}
3134
3135 try:
3136 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
3137 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
3138 except db_base_Exception as e:
3139 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
3140 network_id) + str(e), HTTP_Internal_Server_Error)
3141
3142 sdn_net_counter = 0
3143 for net in result:
3144 if net['sdn_net_id'] != None:
3145 sdn_net_counter+=1
3146 sdn_net_id = net['sdn_net_id']
3147
3148 if sdn_net_counter == 0:
3149 return None
3150 elif sdn_net_counter == 1:
3151 return sdn_net_id
3152 else:
3153 raise NfvoException("More than one SDN network is associated to vim network {}".format(
3154 network_id), HTTP_Internal_Server_Error)
3155
3156 def get_sdn_controller_id(mydb, datacenter):
3157 # Obtain sdn controller id
3158 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
3159 if not config:
3160 return None
3161
3162 return yaml.load(config).get('sdn-controller')
3163
3164 def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
3165 try:
3166 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
3167 if not sdn_network_id:
3168 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error)
3169
3170 #Obtain sdn controller id
3171 controller_id = get_sdn_controller_id(mydb, datacenter)
3172 if not controller_id:
3173 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error)
3174
3175 #Obtain sdn controller info
3176 sdn_controller = ovim.show_of_controller(controller_id)
3177
3178 port_data = {
3179 'name': 'external_port',
3180 'net_id': sdn_network_id,
3181 'ofc_id': controller_id,
3182 'switch_dpid': sdn_controller['dpid'],
3183 'switch_port': descriptor['port']
3184 }
3185
3186 if 'vlan' in descriptor:
3187 port_data['vlan'] = descriptor['vlan']
3188 if 'mac' in descriptor:
3189 port_data['mac'] = descriptor['mac']
3190
3191 result = ovim.new_port(port_data)
3192 except ovimException as e:
3193 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
3194 sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error)
3195 except db_base_Exception as e:
3196 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
3197 network_id) + str(e), HTTP_Internal_Server_Error)
3198
3199 return 'Port uuid: '+ result
3200
3201 def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
3202 if port_id:
3203 filter = {'uuid': port_id}
3204 else:
3205 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
3206 if not sdn_network_id:
3207 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
3208 HTTP_Internal_Server_Error)
3209 #in case no port_id is specified only ports marked as 'external_port' will be detached
3210 filter = {'name': 'external_port', 'net_id': sdn_network_id}
3211
3212 try:
3213 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
3214 except ovimException as e:
3215 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
3216 HTTP_Internal_Server_Error)
3217
3218 if len(port_list) == 0:
3219 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
3220 HTTP_Bad_Request)
3221
3222 port_uuid_list = []
3223 for port in port_list:
3224 try:
3225 port_uuid_list.append(port['uuid'])
3226 ovim.delete_port(port['uuid'])
3227 except ovimException as e:
3228 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error)
3229
3230 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
3231
3232 def vim_action_get(mydb, tenant_id, datacenter, item, name):
3233 #get datacenter info
3234 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3235 filter_dict={}
3236 if name:
3237 if utils.check_valid_uuid(name):
3238 filter_dict["id"] = name
3239 else:
3240 filter_dict["name"] = name
3241 try:
3242 if item=="networks":
3243 #filter_dict['tenant_id'] = myvim['tenant_id']
3244 content = myvim.get_network_list(filter_dict=filter_dict)
3245
3246 if len(content) == 0:
3247 raise NfvoException("Network {} is not present in the system. ".format(name),
3248 HTTP_Bad_Request)
3249
3250 #Update the networks with the attached ports
3251 for net in content:
3252 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
3253 if sdn_network_id != None:
3254 try:
3255 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
3256 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
3257 except ovimException as e:
3258 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error)
3259 #Remove field name and if port name is external_port save it as 'type'
3260 for port in port_list:
3261 if port['name'] == 'external_port':
3262 port['type'] = "External"
3263 del port['name']
3264 net['sdn_network_id'] = sdn_network_id
3265 net['sdn_attached_ports'] = port_list
3266
3267 elif item=="tenants":
3268 content = myvim.get_tenant_list(filter_dict=filter_dict)
3269 elif item == "images":
3270
3271 content = myvim.get_image_list(filter_dict=filter_dict)
3272 else:
3273 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
3274 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
3275 if name and len(content)==1:
3276 return {item[:-1]: content[0]}
3277 elif name and len(content)==0:
3278 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
3279 datacenter)
3280 else:
3281 return {item: content}
3282 except vimconn.vimconnException as e:
3283 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
3284 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
3285
3286
3287 def vim_action_delete(mydb, tenant_id, datacenter, item, name):
3288 #get datacenter info
3289 if tenant_id == "any":
3290 tenant_id=None
3291
3292 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3293 #get uuid name
3294 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
3295 logger.debug("vim_action_delete vim response: " + str(content))
3296 items = content.values()[0]
3297 if type(items)==list and len(items)==0:
3298 raise NfvoException("Not found " + item, HTTP_Not_Found)
3299 elif type(items)==list and len(items)>1:
3300 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
3301 else: # it is a dict
3302 item_id = items["id"]
3303 item_name = str(items.get("name"))
3304
3305 try:
3306 if item=="networks":
3307 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
3308 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
3309 if sdn_network_id != None:
3310 #Delete any port attachment to this network
3311 try:
3312 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
3313 except ovimException as e:
3314 raise NfvoException(
3315 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
3316 HTTP_Internal_Server_Error)
3317
3318 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
3319 for port in port_list:
3320 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
3321
3322 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
3323 try:
3324 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
3325 except db_base_Exception as e:
3326 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
3327 str(e), HTTP_Internal_Server_Error)
3328
3329 #Delete the SDN network
3330 try:
3331 ovim.delete_network(sdn_network_id)
3332 except ovimException as e:
3333 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
3334 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
3335 HTTP_Internal_Server_Error)
3336
3337 content = myvim.delete_network(item_id)
3338 elif item=="tenants":
3339 content = myvim.delete_tenant(item_id)
3340 elif item == "images":
3341 content = myvim.delete_image(item_id)
3342 else:
3343 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
3344 except vimconn.vimconnException as e:
3345 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
3346 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
3347
3348 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
3349
3350
3351 def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
3352 #get datacenter info
3353 logger.debug("vim_action_create descriptor %s", str(descriptor))
3354 if tenant_id == "any":
3355 tenant_id=None
3356 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3357 try:
3358 if item=="networks":
3359 net = descriptor["network"]
3360 net_name = net.pop("name")
3361 net_type = net.pop("type", "bridge")
3362 net_public = net.pop("shared", False)
3363 net_ipprofile = net.pop("ip_profile", None)
3364 net_vlan = net.pop("vlan", None)
3365 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
3366
3367 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
3368 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
3369 try:
3370 sdn_network = {}
3371 sdn_network['vlan'] = net_vlan
3372 sdn_network['type'] = net_type
3373 sdn_network['name'] = net_name
3374 ovim_content = ovim.new_network(sdn_network)
3375 except ovimException as e:
3376 self.logger.error("ovimException creating SDN network={} ".format(
3377 sdn_network) + str(e), exc_info=True)
3378 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
3379 HTTP_Internal_Server_Error)
3380
3381 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
3382 # use instance_scenario_id=None to distinguish from real instaces of nets
3383 correspondence = {'instance_scenario_id': None, 'sdn_net_id': ovim_content, 'vim_net_id': content}
3384 #obtain datacenter_tenant_id
3385 correspondence['datacenter_tenant_id'] = mydb.get_rows(SELECT=('uuid',), FROM='datacenter_tenants', WHERE={'datacenter_id': datacenter})[0]['uuid']
3386
3387 try:
3388 mydb.new_row('instance_nets', correspondence, add_uuid=True)
3389 except db_base_Exception as e:
3390 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
3391 str(e), HTTP_Internal_Server_Error)
3392 elif item=="tenants":
3393 tenant = descriptor["tenant"]
3394 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
3395 else:
3396 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
3397 except vimconn.vimconnException as e:
3398 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
3399
3400 return vim_action_get(mydb, tenant_id, datacenter, item, content)
3401
3402 def sdn_controller_create(mydb, tenant_id, sdn_controller):
3403 data = ovim.new_of_controller(sdn_controller)
3404 logger.debug('New SDN controller created with uuid {}'.format(data))
3405 return data
3406
3407 def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
3408 data = ovim.edit_of_controller(controller_id, sdn_controller)
3409 msg = 'SDN controller {} updated'.format(data)
3410 logger.debug(msg)
3411 return msg
3412
3413 def sdn_controller_list(mydb, tenant_id, controller_id=None):
3414 if controller_id == None:
3415 data = ovim.get_of_controllers()
3416 else:
3417 data = ovim.show_of_controller(controller_id)
3418
3419 msg = 'SDN controller list:\n {}'.format(data)
3420 logger.debug(msg)
3421 return data
3422
3423 def sdn_controller_delete(mydb, tenant_id, controller_id):
3424 select_ = ('uuid', 'config')
3425 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
3426 for datacenter in datacenters:
3427 if datacenter['config']:
3428 config = yaml.load(datacenter['config'])
3429 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
3430 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
3431
3432 data = ovim.delete_of_controller(controller_id)
3433 msg = 'SDN controller {} deleted'.format(data)
3434 logger.debug(msg)
3435 return msg
3436
3437 def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
3438 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
3439 if len(controller) < 1:
3440 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
3441
3442 try:
3443 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
3444 except:
3445 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
3446
3447 sdn_controller = ovim.show_of_controller(sdn_controller_id)
3448 switch_dpid = sdn_controller["dpid"]
3449
3450 maps = list()
3451 for compute_node in sdn_port_mapping:
3452 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
3453 element = dict()
3454 element["compute_node"] = compute_node["compute_node"]
3455 for port in compute_node["ports"]:
3456 element["pci"] = port.get("pci")
3457 element["switch_port"] = port.get("switch_port")
3458 element["switch_mac"] = port.get("switch_mac")
3459 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
3460 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
3461 " or 'switch_mac'", HTTP_Bad_Request)
3462 maps.append(dict(element))
3463
3464 return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)
3465
3466 def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
3467 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
3468
3469 result = {
3470 "sdn-controller": None,
3471 "datacenter-id": datacenter_id,
3472 "dpid": None,
3473 "ports_mapping": list()
3474 }
3475
3476 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
3477 if datacenter['config']:
3478 config = yaml.load(datacenter['config'])
3479 if 'sdn-controller' in config:
3480 controller_id = config['sdn-controller']
3481 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
3482 result["sdn-controller"] = controller_id
3483 result["dpid"] = sdn_controller["dpid"]
3484
3485 if result["sdn-controller"] == None:
3486 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request)
3487 if result["dpid"] == None:
3488 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
3489 HTTP_Internal_Server_Error)
3490
3491 if len(maps) == 0:
3492 return result
3493
3494 ports_correspondence_dict = dict()
3495 for link in maps:
3496 if result["sdn-controller"] != link["ofc_id"]:
3497 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
3498 if result["dpid"] != link["switch_dpid"]:
3499 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
3500 element = dict()
3501 element["pci"] = link["pci"]
3502 if link["switch_port"]:
3503 element["switch_port"] = link["switch_port"]
3504 if link["switch_mac"]:
3505 element["switch_mac"] = link["switch_mac"]
3506
3507 if not link["compute_node"] in ports_correspondence_dict:
3508 content = dict()
3509 content["compute_node"] = link["compute_node"]
3510 content["ports"] = list()
3511 ports_correspondence_dict[link["compute_node"]] = content
3512
3513 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
3514
3515 for key in sorted(ports_correspondence_dict):
3516 result["ports_mapping"].append(ports_correspondence_dict[key])
3517
3518 return result
3519
3520 def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
3521 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})