Fix bug 350 - Sensitive information recorded in openmano.log
[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 uuid import uuid4
42 from db_base import db_base_Exception
43
44 import nfvo_db
45 from threading import Lock
46 import time as t
47 from lib_osm_openvim import ovim as ovim_module
48 from lib_osm_openvim.ovim import ovimException
49 from Crypto.PublicKey import RSA
50
51 import osm_im.vnfd as vnfd_catalog
52 import osm_im.nsd as nsd_catalog
53 from pyangbind.lib.serialise import pybindJSONDecoder
54 from itertools import chain
55
56 global global_config
57 global vimconn_imported
58 global logger
59 global default_volume_size
60 default_volume_size = '5' #size in GB
61 global ovim
62 ovim = None
63 global_config = None
64
65 vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
66 vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
67 vim_persistent_info = {}
68 logger = logging.getLogger('openmano.nfvo')
69 task_lock = Lock()
70 last_task_id = 0.0
71 db = None
72 db_lock = Lock()
73
74 class NfvoException(Exception):
75 def __init__(self, message, http_code):
76 self.http_code = http_code
77 Exception.__init__(self, message)
78
79
80 def get_task_id():
81 global last_task_id
82 task_id = t.time()
83 if task_id <= last_task_id:
84 task_id = last_task_id + 0.000001
85 last_task_id = task_id
86 return "ACTION-{:.6f}".format(task_id)
87 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
88
89
90 def new_task(name, params, depends=None):
91 """Deprected!!!"""
92 task_id = get_task_id()
93 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
94 if depends:
95 task["depends"] = depends
96 return task
97
98
99 def is_task_id(id):
100 return True if id[:5] == "TASK-" else False
101
102
103 def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
104 name = datacenter_name[:16]
105 if name not in vim_threads["names"]:
106 vim_threads["names"].append(name)
107 return name
108 name = datacenter_name[:16] + "." + tenant_name[:16]
109 if name not in vim_threads["names"]:
110 vim_threads["names"].append(name)
111 return name
112 name = datacenter_id + "-" + tenant_id
113 vim_threads["names"].append(name)
114 return name
115
116
117 def start_service(mydb):
118 global db, global_config
119 db = nfvo_db.nfvo_db()
120 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
121 global ovim
122
123 # Initialize openvim for SDN control
124 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
125 # TODO: review ovim.py to delete not needed configuration
126 ovim_configuration = {
127 'logger_name': 'openmano.ovim',
128 'network_vlan_range_start': 1000,
129 'network_vlan_range_end': 4096,
130 'db_name': global_config["db_ovim_name"],
131 'db_host': global_config["db_ovim_host"],
132 'db_user': global_config["db_ovim_user"],
133 'db_passwd': global_config["db_ovim_passwd"],
134 'bridge_ifaces': {},
135 'mode': 'normal',
136 'network_type': 'bridge',
137 #TODO: log_level_of should not be needed. To be modified in ovim
138 'log_level_of': 'DEBUG'
139 }
140 try:
141 ovim = ovim_module.ovim(ovim_configuration)
142 ovim.start_service()
143
144 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
145 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
146 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
147 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
148 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
149 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
150 vims = mydb.get_rows(FROM=from_, SELECT=select_)
151 for vim in vims:
152 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
153 'datacenter_id': vim.get('datacenter_id')}
154 if vim["config"]:
155 extra.update(yaml.load(vim["config"]))
156 if vim.get('dt_config'):
157 extra.update(yaml.load(vim["dt_config"]))
158 if vim["type"] not in vimconn_imported:
159 module_info=None
160 try:
161 module = "vimconn_" + vim["type"]
162 pkg = __import__("osm_ro." + module)
163 vim_conn = getattr(pkg, module)
164 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
165 # vim_conn = imp.load_module(vim["type"], *module_info)
166 vimconn_imported[vim["type"]] = vim_conn
167 except (IOError, ImportError) as e:
168 # if module_info and module_info[0]:
169 # file.close(module_info[0])
170 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
171 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
172
173 thread_id = vim['datacenter_tenant_id']
174 vim_persistent_info[thread_id] = {}
175 try:
176 #if not tenant:
177 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
178 myvim = vimconn_imported[ vim["type"] ].vimconnector(
179 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
180 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
181 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
182 user=vim['user'], passwd=vim['passwd'],
183 config=extra, persistent_info=vim_persistent_info[thread_id]
184 )
185 except Exception as e:
186 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
187 HTTP_Internal_Server_Error)
188 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
189 vim['vim_tenant_id'])
190 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
191 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
192 new_thread.start()
193 vim_threads["running"][thread_id] = new_thread
194 except db_base_Exception as e:
195 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
196 except ovim_module.ovimException as e:
197 message = str(e)
198 if message[:22] == "DATABASE wrong version":
199 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
200 "at host {dbhost}".format(
201 msg=message[22:-3], dbname=global_config["db_ovim_name"],
202 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
203 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
204 raise NfvoException(message, HTTP_Bad_Request)
205
206
207 def stop_service():
208 global ovim, global_config
209 if ovim:
210 ovim.stop_service()
211 for thread_id,thread in vim_threads["running"].items():
212 thread.insert_task("exit")
213 vim_threads["deleting"][thread_id] = thread
214 vim_threads["running"] = {}
215 if global_config and global_config.get("console_thread"):
216 for thread in global_config["console_thread"]:
217 thread.terminate = True
218
219 def get_version():
220 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
221 global_config["version_date"] ))
222
223
224 def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
225 '''Obtain flavorList
226 return result, content:
227 <0, error_text upon error
228 nb_records, flavor_list on success
229 '''
230 WHERE_dict={}
231 WHERE_dict['vnf_id'] = vnf_id
232 if nfvo_tenant is not None:
233 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
234
235 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
236 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
237 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
238 #print "get_flavor_list result:", result
239 #print "get_flavor_list content:", content
240 flavorList=[]
241 for flavor in flavors:
242 flavorList.append(flavor['flavor_id'])
243 return flavorList
244
245
246 def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
247 '''Obtain imageList
248 return result, content:
249 <0, error_text upon error
250 nb_records, flavor_list on success
251 '''
252 WHERE_dict={}
253 WHERE_dict['vnf_id'] = vnf_id
254 if nfvo_tenant is not None:
255 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
256
257 #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
258 images = mydb.get_rows(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
259 imageList=[]
260 for image in images:
261 imageList.append(image['image_id'])
262 return imageList
263
264
265 def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
266 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None):
267 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
268 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
269 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
270 raise exception upon error
271 '''
272 WHERE_dict={}
273 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
274 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
275 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
276 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
277 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
278 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
279 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
280 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'
281 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
282 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
283 'user','passwd', 'dt.config as dt_config')
284 else:
285 from_ = 'datacenters as d'
286 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
287 try:
288 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
289 vim_dict={}
290 for vim in vims:
291 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
292 'datacenter_id': vim.get('datacenter_id')}
293 if vim["config"]:
294 extra.update(yaml.load(vim["config"]))
295 if vim.get('dt_config'):
296 extra.update(yaml.load(vim["dt_config"]))
297 if vim["type"] not in vimconn_imported:
298 module_info=None
299 try:
300 module = "vimconn_" + vim["type"]
301 pkg = __import__("osm_ro." + module)
302 vim_conn = getattr(pkg, module)
303 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
304 # vim_conn = imp.load_module(vim["type"], *module_info)
305 vimconn_imported[vim["type"]] = vim_conn
306 except (IOError, ImportError) as e:
307 # if module_info and module_info[0]:
308 # file.close(module_info[0])
309 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
310 vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
311
312 try:
313 if 'datacenter_tenant_id' in vim:
314 thread_id = vim["datacenter_tenant_id"]
315 if thread_id not in vim_persistent_info:
316 vim_persistent_info[thread_id] = {}
317 persistent_info = vim_persistent_info[thread_id]
318 else:
319 persistent_info = {}
320 #if not tenant:
321 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
322 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
323 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
324 tenant_id=vim.get('vim_tenant_id',vim_tenant),
325 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
326 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
327 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
328 config=extra, persistent_info=persistent_info
329 )
330 except Exception as e:
331 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
332 return vim_dict
333 except db_base_Exception as e:
334 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
335
336
337 def rollback(mydb, vims, rollback_list):
338 undeleted_items=[]
339 #delete things by reverse order
340 for i in range(len(rollback_list)-1, -1, -1):
341 item = rollback_list[i]
342 if item["where"]=="vim":
343 if item["vim_id"] not in vims:
344 continue
345 if is_task_id(item["uuid"]):
346 continue
347 vim = vims[item["vim_id"]]
348 try:
349 if item["what"]=="image":
350 vim.delete_image(item["uuid"])
351 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
352 elif item["what"]=="flavor":
353 vim.delete_flavor(item["uuid"])
354 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
355 elif item["what"]=="network":
356 vim.delete_network(item["uuid"])
357 elif item["what"]=="vm":
358 vim.delete_vminstance(item["uuid"])
359 except vimconn.vimconnException as e:
360 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
361 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
362 except db_base_Exception as e:
363 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
364
365 else: # where==mano
366 try:
367 if item["what"]=="image":
368 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
369 elif item["what"]=="flavor":
370 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
371 except db_base_Exception as e:
372 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
373 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
374 if len(undeleted_items)==0:
375 return True," Rollback successful."
376 else:
377 return False," Rollback fails to delete: " + str(undeleted_items)
378
379
380 def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
381 global global_config
382 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
383 vnfc_interfaces={}
384 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
385 name_dict = {}
386 #dataplane interfaces
387 for numa in vnfc.get("numas",() ):
388 for interface in numa.get("interfaces",()):
389 if interface["name"] in name_dict:
390 raise NfvoException(
391 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
392 vnfc["name"], interface["name"]),
393 HTTP_Bad_Request)
394 name_dict[ interface["name"] ] = "underlay"
395 #bridge interfaces
396 for interface in vnfc.get("bridge-ifaces",() ):
397 if interface["name"] in name_dict:
398 raise NfvoException(
399 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
400 vnfc["name"], interface["name"]),
401 HTTP_Bad_Request)
402 name_dict[ interface["name"] ] = "overlay"
403 vnfc_interfaces[ vnfc["name"] ] = name_dict
404 # check bood-data info
405 # if "boot-data" in vnfc:
406 # # check that user-data is incompatible with users and config-files
407 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
408 # raise NfvoException(
409 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
410 # HTTP_Bad_Request)
411
412 #check if the info in external_connections matches with the one in the vnfcs
413 name_list=[]
414 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
415 if external_connection["name"] in name_list:
416 raise NfvoException(
417 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
418 external_connection["name"]),
419 HTTP_Bad_Request)
420 name_list.append(external_connection["name"])
421 if external_connection["VNFC"] not in vnfc_interfaces:
422 raise NfvoException(
423 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
424 external_connection["name"], external_connection["VNFC"]),
425 HTTP_Bad_Request)
426
427 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
428 raise NfvoException(
429 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
430 external_connection["name"],
431 external_connection["local_iface_name"]),
432 HTTP_Bad_Request )
433
434 #check if the info in internal_connections matches with the one in the vnfcs
435 name_list=[]
436 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
437 if internal_connection["name"] in name_list:
438 raise NfvoException(
439 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
440 internal_connection["name"]),
441 HTTP_Bad_Request)
442 name_list.append(internal_connection["name"])
443 #We should check that internal-connections of type "ptp" have only 2 elements
444
445 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
446 raise NfvoException(
447 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
448 internal_connection["name"],
449 'ptp' if vnf_descriptor_version==1 else 'e-line',
450 'data' if vnf_descriptor_version==1 else "e-lan"),
451 HTTP_Bad_Request)
452 for port in internal_connection["elements"]:
453 vnf = port["VNFC"]
454 iface = port["local_iface_name"]
455 if vnf not in vnfc_interfaces:
456 raise NfvoException(
457 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
458 internal_connection["name"], vnf),
459 HTTP_Bad_Request)
460 if iface not in vnfc_interfaces[ vnf ]:
461 raise NfvoException(
462 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
463 internal_connection["name"], iface),
464 HTTP_Bad_Request)
465 return -HTTP_Bad_Request,
466 if vnf_descriptor_version==1 and "type" not in internal_connection:
467 if vnfc_interfaces[vnf][iface] == "overlay":
468 internal_connection["type"] = "bridge"
469 else:
470 internal_connection["type"] = "data"
471 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
472 if vnfc_interfaces[vnf][iface] == "overlay":
473 internal_connection["implementation"] = "overlay"
474 else:
475 internal_connection["implementation"] = "underlay"
476 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
477 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
478 raise NfvoException(
479 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
480 internal_connection["name"],
481 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
482 'data' if vnf_descriptor_version==1 else 'underlay'),
483 HTTP_Bad_Request)
484 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
485 vnfc_interfaces[vnf][iface] == "underlay":
486 raise NfvoException(
487 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
488 internal_connection["name"], iface,
489 'data' if vnf_descriptor_version==1 else 'underlay',
490 'bridge' if vnf_descriptor_version==1 else 'overlay'),
491 HTTP_Bad_Request)
492
493
494 def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None):
495 #look if image exist
496 if only_create_at_vim:
497 image_mano_id = image_dict['uuid']
498 if return_on_error == None:
499 return_on_error = True
500 else:
501 if image_dict['location']:
502 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
503 else:
504 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
505 if len(images)>=1:
506 image_mano_id = images[0]['uuid']
507 else:
508 #create image in MANO DB
509 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
510 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
511 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
512 }
513 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
514 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
515 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
516 #create image at every vim
517 for vim_id,vim in vims.iteritems():
518 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
519 image_created="false"
520 #look at database
521 image_db = mydb.get_rows(FROM="datacenters_images",
522 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
523 #look at VIM if this image exist
524 try:
525 if image_dict['location'] is not None:
526 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
527 else:
528 filter_dict = {}
529 filter_dict['name'] = image_dict['universal_name']
530 if image_dict.get('checksum') != None:
531 filter_dict['checksum'] = image_dict['checksum']
532 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
533 vim_images = vim.get_image_list(filter_dict)
534 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
535 if len(vim_images) > 1:
536 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), HTTP_Conflict)
537 elif len(vim_images) == 0:
538 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
539 else:
540 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
541 image_vim_id = vim_images[0]['id']
542
543 except vimconn.vimconnNotFoundException as e:
544 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
545 try:
546 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
547 if image_dict['location']:
548 image_vim_id = vim.new_image(image_dict)
549 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
550 image_created="true"
551 else:
552 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
553 raise vimconn.vimconnException(str(e))
554 except vimconn.vimconnException as e:
555 if return_on_error:
556 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
557 raise
558 image_vim_id = None
559 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
560 continue
561 except vimconn.vimconnException as e:
562 if return_on_error:
563 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
564 raise
565 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
566 image_vim_id = None
567 continue
568 #if we reach here, the image has been created or existed
569 if len(image_db)==0:
570 #add new vim_id at datacenters_images
571 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
572 'image_id':image_mano_id,
573 'vim_id': image_vim_id,
574 'created':image_created})
575 elif image_db[0]["vim_id"]!=image_vim_id:
576 #modify existing vim_id at datacenters_images
577 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id})
578
579 return image_vim_id if only_create_at_vim else image_mano_id
580
581
582 def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
583 temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
584 'ram':flavor_dict.get('ram'),
585 'vcpus':flavor_dict.get('vcpus'),
586 }
587 if 'extended' in flavor_dict and flavor_dict['extended']==None:
588 del flavor_dict['extended']
589 if 'extended' in flavor_dict:
590 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
591
592 #look if flavor exist
593 if only_create_at_vim:
594 flavor_mano_id = flavor_dict['uuid']
595 if return_on_error == None:
596 return_on_error = True
597 else:
598 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
599 if len(flavors)>=1:
600 flavor_mano_id = flavors[0]['uuid']
601 else:
602 #create flavor
603 #create one by one the images of aditional disks
604 dev_image_list=[] #list of images
605 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
606 dev_nb=0
607 for device in flavor_dict['extended'].get('devices',[]):
608 if "image" not in device and "image name" not in device:
609 continue
610 image_dict={}
611 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
612 image_dict['universal_name']=device.get('image name')
613 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
614 image_dict['location']=device.get('image')
615 #image_dict['new_location']=vnfc.get('image location')
616 image_dict['checksum']=device.get('image checksum')
617 image_metadata_dict = device.get('image metadata', None)
618 image_metadata_str = None
619 if image_metadata_dict != None:
620 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
621 image_dict['metadata']=image_metadata_str
622 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
623 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
624 dev_image_list.append(image_id)
625 dev_nb += 1
626 temp_flavor_dict['name'] = flavor_dict['name']
627 temp_flavor_dict['description'] = flavor_dict.get('description',None)
628 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
629 flavor_mano_id= content
630 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
631 #create flavor at every vim
632 if 'uuid' in flavor_dict:
633 del flavor_dict['uuid']
634 flavor_vim_id=None
635 for vim_id,vim in vims.items():
636 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
637 flavor_created="false"
638 #look at database
639 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
640 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
641 #look at VIM if this flavor exist SKIPPED
642 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
643 #if res_vim < 0:
644 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
645 # continue
646 #elif res_vim==0:
647
648 # Create the flavor in VIM
649 # Translate images at devices from MANO id to VIM id
650 disk_list = []
651 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
652 # make a copy of original devices
653 devices_original=[]
654
655 for device in flavor_dict["extended"].get("devices",[]):
656 dev={}
657 dev.update(device)
658 devices_original.append(dev)
659 if 'image' in device:
660 del device['image']
661 if 'image metadata' in device:
662 del device['image metadata']
663 if 'image checksum' in device:
664 del device['image checksum']
665 dev_nb = 0
666 for index in range(0,len(devices_original)) :
667 device=devices_original[index]
668 if "image" not in device and "image name" not in device:
669 if 'size' in device:
670 disk_list.append({'size': device.get('size', default_volume_size)})
671 continue
672 image_dict={}
673 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
674 image_dict['universal_name']=device.get('image name')
675 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
676 image_dict['location']=device.get('image')
677 # image_dict['new_location']=device.get('image location')
678 image_dict['checksum']=device.get('image checksum')
679 image_metadata_dict = device.get('image metadata', None)
680 image_metadata_str = None
681 if image_metadata_dict != None:
682 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
683 image_dict['metadata']=image_metadata_str
684 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
685 image_dict["uuid"]=image_mano_id
686 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
687
688 #save disk information (image must be based on and size
689 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
690
691 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
692 dev_nb += 1
693 if len(flavor_db)>0:
694 #check that this vim_id exist in VIM, if not create
695 flavor_vim_id=flavor_db[0]["vim_id"]
696 try:
697 vim.get_flavor(flavor_vim_id)
698 continue #flavor exist
699 except vimconn.vimconnException:
700 pass
701 #create flavor at vim
702 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
703 try:
704 flavor_vim_id = None
705 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
706 flavor_create="false"
707 except vimconn.vimconnException as e:
708 pass
709 try:
710 if not flavor_vim_id:
711 flavor_vim_id = vim.new_flavor(flavor_dict)
712 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
713 flavor_created="true"
714 except vimconn.vimconnException as e:
715 if return_on_error:
716 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
717 raise
718 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
719 flavor_vim_id = None
720 continue
721 #if reach here the flavor has been create or exist
722 if len(flavor_db)==0:
723 #add new vim_id at datacenters_flavors
724 extended_devices_yaml = None
725 if len(disk_list) > 0:
726 extended_devices = dict()
727 extended_devices['disks'] = disk_list
728 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
729 mydb.new_row('datacenters_flavors',
730 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
731 'created': flavor_created, 'extended': extended_devices_yaml})
732 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
733 #modify existing vim_id at datacenters_flavors
734 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
735 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
736
737 return flavor_vim_id if only_create_at_vim else flavor_mano_id
738
739
740 def get_str(obj, field, length):
741 """
742 Obtain the str value,
743 :param obj:
744 :param length:
745 :return:
746 """
747 value = obj.get(field)
748 if value is not None:
749 value = str(value)[:length]
750 return value
751
752 def _lookfor_or_create_image(db_image, mydb, descriptor):
753 """
754 fill image content at db_image dictionary. Check if the image with this image and checksum exist
755 :param db_image: dictionary to insert data
756 :param mydb: database connector
757 :param descriptor: yang descriptor
758 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
759 """
760
761 db_image["name"] = get_str(descriptor, "image", 255)
762 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
763 if not db_image["checksum"]: # Ensure that if empty string, None is stored
764 db_image["checksum"] = None
765 if db_image["name"].startswith("/"):
766 db_image["location"] = db_image["name"]
767 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
768 else:
769 db_image["universal_name"] = db_image["name"]
770 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
771 'checksum': db_image['checksum']})
772 if existing_images:
773 return existing_images[0]["uuid"]
774 else:
775 image_uuid = str(uuid4())
776 db_image["uuid"] = image_uuid
777 return None
778
779 def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
780 """
781 Parses an OSM IM vnfd_catalog and insert at DB
782 :param mydb:
783 :param tenant_id:
784 :param vnf_descriptor:
785 :return: The list of cretated vnf ids
786 """
787 try:
788 myvnfd = vnfd_catalog.vnfd()
789 try:
790 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd)
791 except Exception as e:
792 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), HTTP_Bad_Request)
793 db_vnfs = []
794 db_nets = []
795 db_vms = []
796 db_vms_index = 0
797 db_interfaces = []
798 db_images = []
799 db_flavors = []
800 uuid_list = []
801 vnfd_uuid_list = []
802 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
803 vnfd = vnfd_yang.get()
804
805 # table vnf
806 vnf_uuid = str(uuid4())
807 uuid_list.append(vnf_uuid)
808 vnfd_uuid_list.append(vnf_uuid)
809 db_vnf = {
810 "uuid": vnf_uuid,
811 "osm_id": get_str(vnfd, "id", 255),
812 "name": get_str(vnfd, "name", 255),
813 "description": get_str(vnfd, "description", 255),
814 "tenant_id": tenant_id,
815 "vendor": get_str(vnfd, "vendor", 255),
816 "short_name": get_str(vnfd, "short-name", 255),
817 "descriptor": str(vnf_descriptor)[:60000]
818 }
819
820 # table nets (internal-vld)
821 net_id2uuid = {} # for mapping interface with network
822 for vld in vnfd.get("internal-vld").itervalues():
823 net_uuid = str(uuid4())
824 uuid_list.append(net_uuid)
825 db_net = {
826 "name": get_str(vld, "name", 255),
827 "vnf_id": vnf_uuid,
828 "uuid": net_uuid,
829 "description": get_str(vld, "description", 255),
830 "type": "bridge", # TODO adjust depending on connection point type
831 }
832 net_id2uuid[vld.get("id")] = net_uuid
833 db_nets.append(db_net)
834
835 # table vms (vdus)
836 vdu_id2uuid = {}
837 vdu_id2db_table_index = {}
838 for vdu in vnfd.get("vdu").itervalues():
839 vm_uuid = str(uuid4())
840 uuid_list.append(vm_uuid)
841 db_vm = {
842 "uuid": vm_uuid,
843 "osm_id": get_str(vdu, "id", 255),
844 "name": get_str(vdu, "name", 255),
845 "description": get_str(vdu, "description", 255),
846 "vnf_id": vnf_uuid,
847 }
848 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
849 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
850 if vdu.get("count"):
851 db_vm["count"] = int(vdu["count"])
852
853 # table image
854 image_present = False
855 if vdu.get("image"):
856 image_present = True
857 db_image = {}
858 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
859 if not image_uuid:
860 image_uuid = db_image["uuid"]
861 db_images.append(db_image)
862 db_vm["image_id"] = image_uuid
863
864 # volumes
865 devices = []
866 if vdu.get("volumes"):
867 for volume_key in sorted(vdu["volumes"]):
868 volume = vdu["volumes"][volume_key]
869 if not image_present:
870 # Convert the first volume to vnfc.image
871 image_present = True
872 db_image = {}
873 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
874 if not image_uuid:
875 image_uuid = db_image["uuid"]
876 db_images.append(db_image)
877 db_vm["image_id"] = image_uuid
878 else:
879 # Add Openmano devices
880 device = {}
881 device["type"] = str(volume.get("device-type"))
882 if volume.get("size"):
883 device["size"] = int(volume["size"])
884 if volume.get("image"):
885 device["image name"] = str(volume["image"])
886 if volume.get("image-checksum"):
887 device["image checksum"] = str(volume["image-checksum"])
888 devices.append(device)
889
890 # table flavors
891 db_flavor = {
892 "name": get_str(vdu, "name", 250) + "-flv",
893 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
894 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
895 "disk": int(vdu["vm-flavor"].get("storage-gb", 1)),
896 }
897 # EPA TODO revise
898 extended = {}
899 numa = {}
900 if devices:
901 extended["devices"] = devices
902 if vdu.get("guest-epa"): # TODO or dedicated_int:
903 epa_vcpu_set = False
904 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
905 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
906 if numa_node_policy.get("node"):
907 numa_node = numa_node_policy["node"]['0']
908 if numa_node.get("num-cores"):
909 numa["cores"] = numa_node["num-cores"]
910 epa_vcpu_set = True
911 if numa_node.get("paired-threads"):
912 if numa_node["paired-threads"].get("num-paired-threads"):
913 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
914 epa_vcpu_set = True
915 if len(numa_node["paired-threads"].get("paired-thread-ids")):
916 numa["paired-threads-id"] = []
917 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
918 numa["paired-threads-id"].append(
919 (str(pair["thread-a"]), str(pair["thread-b"]))
920 )
921 if numa_node.get("num-threads"):
922 numa["threads"] = int(numa_node["num-threads"])
923 epa_vcpu_set = True
924 if numa_node.get("memory-mb"):
925 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
926 if vdu["guest-epa"].get("mempage-size"):
927 if vdu["guest-epa"]["mempage-size"] != "SMALL":
928 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
929 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
930 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
931 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
932 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
933 numa["cores"] = max(db_flavor["vcpus"], 1)
934 else:
935 numa["threads"] = max(db_flavor["vcpus"], 1)
936 if numa:
937 extended["numas"] = [numa]
938 if extended:
939 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
940 db_flavor["extended"] = extended_text
941 # look if flavor exist
942
943 temp_flavor_dict = {'disk': db_flavor.get('disk', 1),
944 'ram': db_flavor.get('ram'),
945 'vcpus': db_flavor.get('vcpus'),
946 'extended': db_flavor.get('extended')
947 }
948 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
949 if existing_flavors:
950 flavor_uuid = existing_flavors[0]["uuid"]
951 else:
952 flavor_uuid = str(uuid4())
953 uuid_list.append(flavor_uuid)
954 db_flavor["uuid"] = flavor_uuid
955 db_flavors.append(db_flavor)
956 db_vm["flavor_id"] = flavor_uuid
957
958 # cloud-init
959 boot_data = {}
960 if vdu.get("cloud-init"):
961 boot_data["user-data"] = str(vdu["cloud-init"])
962 elif vdu.get("cloud-init-file"):
963 # TODO Where this file content is present???
964 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
965 boot_data["user-data"] = str(vdu["cloud-init-file"])
966
967 if vdu.get("supplemental-boot-data"):
968 if vdu["supplemental-boot-data"].get('boot-data-drive'):
969 boot_data['boot-data-drive'] = True
970 if vdu["supplemental-boot-data"].get('config-file'):
971 om_cfgfile_list = list()
972 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
973 # TODO Where this file content is present???
974 cfg_source = str(custom_config_file["source"])
975 om_cfgfile_list.append({"dest": custom_config_file["dest"],
976 "content": cfg_source})
977 boot_data['config-files'] = om_cfgfile_list
978 if boot_data:
979 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
980
981 db_vms.append(db_vm)
982 db_vms_index += 1
983
984 # table interfaces (internal/external interfaces)
985 cp_name2iface_uuid = {}
986 cp_name2vm_uuid = {}
987 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
988 for iface in vdu.get("interface").itervalues():
989 iface_uuid = str(uuid4())
990 uuid_list.append(iface_uuid)
991 db_interface = {
992 "uuid": iface_uuid,
993 "internal_name": get_str(iface, "name", 255),
994 "vm_id": vm_uuid,
995 }
996 if iface.get("virtual-interface").get("vpci"):
997 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
998
999 if iface.get("virtual-interface").get("bandwidth"):
1000 bps = int(iface.get("virtual-interface").get("bandwidth"))
1001 db_interface["bw"] = bps/1000
1002
1003 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1004 db_interface["type"] = "mgmt"
1005 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
1006 db_interface["type"] = "bridge"
1007 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1008 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1009 db_interface["type"] = "data"
1010 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1011 else:
1012 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1013 "-interface':'type':'{}'. Interface type is not supported".format(
1014 str(vnfd["id"])[:255], str(vdu["id"])[:255],
1015 iface.get("virtual-interface").get("type")),
1016 HTTP_Bad_Request)
1017
1018 if iface.get("external-connection-point-ref"):
1019 try:
1020 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1021 db_interface["external_name"] = get_str(cp, "name", 255)
1022 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1023 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1024 if cp.get("port-security-enabled") == False:
1025 db_interface["port_security"] = 0
1026 elif cp.get("port-security-enabled") == True:
1027 db_interface["port_security"] = 1
1028 except KeyError:
1029 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1030 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1031 " at connection-point".format(
1032 vnf=vnfd["id"], vdu=vdu["id"], iface=iface["name"],
1033 cp=iface.get("vnfd-connection-point-ref")),
1034 HTTP_Bad_Request)
1035 elif iface.get("internal-connection-point-ref"):
1036 try:
1037 for vld in vnfd.get("internal-vld").itervalues():
1038 for cp in vld.get("internal-connection-point").itervalues():
1039 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
1040 db_interface["net_id"] = net_id2uuid[vld.get("id")]
1041 if cp.get("port-security-enabled") == False:
1042 db_interface["port_security"] = 0
1043 elif cp.get("port-security-enabled") == True:
1044 db_interface["port_security"] = 1
1045 break
1046 except KeyError:
1047 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1048 "'interface[{iface}]':'vdu-internal-connection-point-ref':'{cp}' is not"
1049 " referenced by any internal-vld".format(
1050 vnf=vnfd["id"], vdu=vdu["id"], iface=iface["name"],
1051 cp=iface.get("vdu-internal-connection-point-ref")),
1052 HTTP_Bad_Request)
1053 if iface.get("position") is not None:
1054 db_interface["created_at"] = int(iface.get("position")) - 1000
1055 db_interfaces.append(db_interface)
1056
1057 # VNF affinity and antiaffinity
1058 for pg in vnfd.get("placement-groups").itervalues():
1059 pg_name = get_str(pg, "name", 255)
1060 for vdu in pg.get("member-vdus").itervalues():
1061 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1062 if vdu_id not in vdu_id2db_table_index:
1063 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1064 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1065 vnf=vnfd["id"], pg=pg_name, vdu=vdu_id),
1066 HTTP_Bad_Request)
1067 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
1068 # TODO consider the case of isolation and not colocation
1069 # if pg.get("strategy") == "ISOLATION":
1070
1071 # VNF mgmt configuration
1072 mgmt_access = {}
1073 if vnfd["mgmt-interface"].get("vdu-id"):
1074 if vnfd["mgmt-interface"]["vdu-id"] not in vdu_id2uuid:
1075 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1076 "'{vdu}'. Reference to a non-existing vdu".format(
1077 vnf=vnfd["id"], vdu=vnfd["mgmt-interface"]["vdu-id"]),
1078 HTTP_Bad_Request)
1079 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
1080 if vnfd["mgmt-interface"].get("ip-address"):
1081 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1082 if vnfd["mgmt-interface"].get("cp"):
1083 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
1084 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
1085 "Reference to a non-existing connection-point".format(
1086 vnf=vnfd["id"], cp=vnfd["mgmt-interface"]["cp"]),
1087 HTTP_Bad_Request)
1088 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1089 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
1090 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1091 "default-user", 64)
1092
1093 if default_user:
1094 mgmt_access["default_user"] = default_user
1095 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1096 "required", 6)
1097 if required:
1098 mgmt_access["required"] = required
1099
1100 if mgmt_access:
1101 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1102
1103
1104
1105 db_vnfs.append(db_vnf)
1106 db_tables=[
1107 {"vnfs": db_vnfs},
1108 {"nets": db_nets},
1109 {"images": db_images},
1110 {"flavors": db_flavors},
1111 {"vms": db_vms},
1112 {"interfaces": db_interfaces},
1113 ]
1114
1115 logger.debug("create_vnf Deployment done vnfDict: %s",
1116 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1117 mydb.new_rows(db_tables, uuid_list)
1118 return vnfd_uuid_list
1119 except NfvoException:
1120 raise
1121 except Exception as e:
1122 logger.error("Exception {}".format(e))
1123 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1124
1125
1126 def new_vnf(mydb, tenant_id, vnf_descriptor):
1127 global global_config
1128
1129 # Step 1. Check the VNF descriptor
1130 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
1131 # Step 2. Check tenant exist
1132 vims = {}
1133 if tenant_id != "any":
1134 check_tenant(mydb, tenant_id)
1135 if "tenant_id" in vnf_descriptor["vnf"]:
1136 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1137 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1138 HTTP_Unauthorized)
1139 else:
1140 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1141 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1142 if global_config["auto_push_VNF_to_VIMs"]:
1143 vims = get_vim(mydb, tenant_id)
1144
1145 # Step 4. Review the descriptor and add missing fields
1146 #print vnf_descriptor
1147 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1148 vnf_name = vnf_descriptor['vnf']['name']
1149 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1150 if "physical" in vnf_descriptor['vnf']:
1151 del vnf_descriptor['vnf']['physical']
1152 #print vnf_descriptor
1153
1154 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1155 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1156 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1157
1158 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1159 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1160 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1161 try:
1162 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1163 for vnfc in vnf_descriptor['vnf']['VNFC']:
1164 VNFCitem={}
1165 VNFCitem["name"] = vnfc['name']
1166 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
1167 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
1168
1169 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1170
1171 myflavorDict = {}
1172 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1173 myflavorDict["description"] = VNFCitem["description"]
1174 myflavorDict["ram"] = vnfc.get("ram", 0)
1175 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1176 myflavorDict["disk"] = vnfc.get("disk", 1)
1177 myflavorDict["extended"] = {}
1178
1179 devices = vnfc.get("devices")
1180 if devices != None:
1181 myflavorDict["extended"]["devices"] = devices
1182
1183 # TODO:
1184 # 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
1185 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1186
1187 # Previous code has been commented
1188 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1189 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1190 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1191 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1192 #else:
1193 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1194 # if result2:
1195 # print "Error creating flavor: unknown processor model. Rollback successful."
1196 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1197 # else:
1198 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1199 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1200
1201 if 'numas' in vnfc and len(vnfc['numas'])>0:
1202 myflavorDict['extended']['numas'] = vnfc['numas']
1203
1204 #print myflavorDict
1205
1206 # Step 6.2 New flavors are created in the VIM
1207 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1208
1209 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1210 VNFCitem["flavor_id"] = flavor_id
1211 VNFCDict[vnfc['name']] = VNFCitem
1212
1213 logger.debug("Creating new images in the VIM for each VNFC")
1214 # Step 6.3 New images are created in the VIM
1215 #For each VNFC, we must create the appropriate image.
1216 #This "for" loop might be integrated with the previous one
1217 #In case this integration is made, the VNFCDict might become a VNFClist.
1218 for vnfc in vnf_descriptor['vnf']['VNFC']:
1219 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1220 image_dict={}
1221 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1222 image_dict['universal_name']=vnfc.get('image name')
1223 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1224 image_dict['location']=vnfc.get('VNFC image')
1225 #image_dict['new_location']=vnfc.get('image location')
1226 image_dict['checksum']=vnfc.get('image checksum')
1227 image_metadata_dict = vnfc.get('image metadata', None)
1228 image_metadata_str = None
1229 if image_metadata_dict is not None:
1230 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1231 image_dict['metadata']=image_metadata_str
1232 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1233 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1234 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1235 VNFCDict[vnfc['name']]["image_id"] = image_id
1236 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1237 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1238 if vnfc.get("boot-data"):
1239 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1240
1241
1242 # Step 7. Storing the VNF descriptor in the repository
1243 if "descriptor" not in vnf_descriptor["vnf"]:
1244 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1245
1246 # Step 8. Adding the VNF to the NFVO DB
1247 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1248 return vnf_id
1249 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1250 _, message = rollback(mydb, vims, rollback_list)
1251 if isinstance(e, db_base_Exception):
1252 error_text = "Exception at database"
1253 elif isinstance(e, KeyError):
1254 error_text = "KeyError exception "
1255 e.http_code = HTTP_Internal_Server_Error
1256 else:
1257 error_text = "Exception at VIM"
1258 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1259 #logger.error("start_scenario %s", error_text)
1260 raise NfvoException(error_text, e.http_code)
1261
1262
1263 def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1264 global global_config
1265
1266 # Step 1. Check the VNF descriptor
1267 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
1268 # Step 2. Check tenant exist
1269 vims = {}
1270 if tenant_id != "any":
1271 check_tenant(mydb, tenant_id)
1272 if "tenant_id" in vnf_descriptor["vnf"]:
1273 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1274 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1275 HTTP_Unauthorized)
1276 else:
1277 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1278 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1279 if global_config["auto_push_VNF_to_VIMs"]:
1280 vims = get_vim(mydb, tenant_id)
1281
1282 # Step 4. Review the descriptor and add missing fields
1283 #print vnf_descriptor
1284 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1285 vnf_name = vnf_descriptor['vnf']['name']
1286 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1287 if "physical" in vnf_descriptor['vnf']:
1288 del vnf_descriptor['vnf']['physical']
1289 #print vnf_descriptor
1290
1291 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1292 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1293 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1294
1295 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1296 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1297 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1298 try:
1299 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1300 for vnfc in vnf_descriptor['vnf']['VNFC']:
1301 VNFCitem={}
1302 VNFCitem["name"] = vnfc['name']
1303 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
1304
1305 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1306
1307 myflavorDict = {}
1308 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1309 myflavorDict["description"] = VNFCitem["description"]
1310 myflavorDict["ram"] = vnfc.get("ram", 0)
1311 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1312 myflavorDict["disk"] = vnfc.get("disk", 1)
1313 myflavorDict["extended"] = {}
1314
1315 devices = vnfc.get("devices")
1316 if devices != None:
1317 myflavorDict["extended"]["devices"] = devices
1318
1319 # TODO:
1320 # 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
1321 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1322
1323 # Previous code has been commented
1324 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1325 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1326 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1327 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1328 #else:
1329 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1330 # if result2:
1331 # print "Error creating flavor: unknown processor model. Rollback successful."
1332 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1333 # else:
1334 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1335 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1336
1337 if 'numas' in vnfc and len(vnfc['numas'])>0:
1338 myflavorDict['extended']['numas'] = vnfc['numas']
1339
1340 #print myflavorDict
1341
1342 # Step 6.2 New flavors are created in the VIM
1343 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1344
1345 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1346 VNFCitem["flavor_id"] = flavor_id
1347 VNFCDict[vnfc['name']] = VNFCitem
1348
1349 logger.debug("Creating new images in the VIM for each VNFC")
1350 # Step 6.3 New images are created in the VIM
1351 #For each VNFC, we must create the appropriate image.
1352 #This "for" loop might be integrated with the previous one
1353 #In case this integration is made, the VNFCDict might become a VNFClist.
1354 for vnfc in vnf_descriptor['vnf']['VNFC']:
1355 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1356 image_dict={}
1357 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1358 image_dict['universal_name']=vnfc.get('image name')
1359 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1360 image_dict['location']=vnfc.get('VNFC image')
1361 #image_dict['new_location']=vnfc.get('image location')
1362 image_dict['checksum']=vnfc.get('image checksum')
1363 image_metadata_dict = vnfc.get('image metadata', None)
1364 image_metadata_str = None
1365 if image_metadata_dict is not None:
1366 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1367 image_dict['metadata']=image_metadata_str
1368 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1369 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1370 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1371 VNFCDict[vnfc['name']]["image_id"] = image_id
1372 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1373 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1374 if vnfc.get("boot-data"):
1375 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1376
1377 # Step 7. Storing the VNF descriptor in the repository
1378 if "descriptor" not in vnf_descriptor["vnf"]:
1379 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1380
1381 # Step 8. Adding the VNF to the NFVO DB
1382 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1383 return vnf_id
1384 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1385 _, message = rollback(mydb, vims, rollback_list)
1386 if isinstance(e, db_base_Exception):
1387 error_text = "Exception at database"
1388 elif isinstance(e, KeyError):
1389 error_text = "KeyError exception "
1390 e.http_code = HTTP_Internal_Server_Error
1391 else:
1392 error_text = "Exception at VIM"
1393 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1394 #logger.error("start_scenario %s", error_text)
1395 raise NfvoException(error_text, e.http_code)
1396
1397
1398 def get_vnf_id(mydb, tenant_id, vnf_id):
1399 #check valid tenant_id
1400 check_tenant(mydb, tenant_id)
1401 #obtain data
1402 where_or = {}
1403 if tenant_id != "any":
1404 where_or["tenant_id"] = tenant_id
1405 where_or["public"] = True
1406 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1407
1408 vnf_id = vnf["uuid"]
1409 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1410 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
1411 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1412 data={'vnf' : filtered_content}
1413 #GET VM
1414 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
1415 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1416 'boot_data'),
1417 WHERE={'vnfs.uuid': vnf_id} )
1418 if len(content)==0:
1419 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
1420 # change boot_data into boot-data
1421 for vm in content:
1422 if vm.get("boot_data"):
1423 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1424 del vm["boot_data"]
1425
1426 data['vnf']['VNFC'] = content
1427 #TODO: GET all the information from a VNFC and include it in the output.
1428
1429 #GET NET
1430 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
1431 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1432 WHERE={'vnfs.uuid': vnf_id} )
1433 data['vnf']['nets'] = content
1434
1435 #GET ip-profile for each net
1436 for net in data['vnf']['nets']:
1437 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1438 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1439 WHERE={'net_id': net["uuid"]} )
1440 if len(ipprofiles)==1:
1441 net["ip_profile"] = ipprofiles[0]
1442 elif len(ipprofiles)>1:
1443 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), HTTP_Bad_Request)
1444
1445
1446 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1447
1448 #GET External Interfaces
1449 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1450 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1451 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1452 WHERE={'vnfs.uuid': vnf_id},
1453 WHERE_NOT={'interfaces.external_name': None} )
1454 #print content
1455 data['vnf']['external-connections'] = content
1456
1457 return data
1458
1459
1460 def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1461 # Check tenant exist
1462 if tenant_id != "any":
1463 check_tenant(mydb, tenant_id)
1464 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1465 vims = get_vim(mydb, tenant_id)
1466 else:
1467 vims={}
1468
1469 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1470 where_or = {}
1471 if tenant_id != "any":
1472 where_or["tenant_id"] = tenant_id
1473 where_or["public"] = True
1474 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1475 vnf_id = vnf["uuid"]
1476
1477 # "Getting the list of flavors and tenants of the VNF"
1478 flavorList = get_flavorlist(mydb, vnf_id)
1479 if len(flavorList)==0:
1480 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
1481
1482 imageList = get_imagelist(mydb, vnf_id)
1483 if len(imageList)==0:
1484 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
1485
1486 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1487 if deleted == 0:
1488 raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
1489
1490 undeletedItems = []
1491 for flavor in flavorList:
1492 #check if flavor is used by other vnf
1493 try:
1494 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1495 if len(c) > 0:
1496 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1497 continue
1498 #flavor not used, must be deleted
1499 #delelte at VIM
1500 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
1501 for flavor_vim in c:
1502 if flavor_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
1503 continue
1504 if flavor_vim['created']=='false': #skip this flavor because not created by openmano
1505 continue
1506 myvim=vims[ flavor_vim["datacenter_id"] ]
1507 try:
1508 myvim.delete_flavor(flavor_vim["vim_id"])
1509 except vimconn.vimconnNotFoundException as e:
1510 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"], flavor_vim["datacenter_id"] )
1511 except vimconn.vimconnException as e:
1512 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1513 flavor_vim["vim_id"], flavor_vim["datacenter_id"], type(e).__name__, str(e))
1514 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
1515 #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1516 mydb.delete_row_by_id('flavors', flavor)
1517 except db_base_Exception as e:
1518 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
1519 undeletedItems.append("flavor %s" % flavor)
1520
1521
1522 for image in imageList:
1523 try:
1524 #check if image is used by other vnf
1525 c = mydb.get_rows(FROM='vms', WHERE={'image_id':image} )
1526 if len(c) > 0:
1527 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1528 continue
1529 #image not used, must be deleted
1530 #delelte at VIM
1531 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
1532 for image_vim in c:
1533 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
1534 continue
1535 if image_vim['created']=='false': #skip this image because not created by openmano
1536 continue
1537 myvim=vims[ image_vim["datacenter_id"] ]
1538 try:
1539 myvim.delete_image(image_vim["vim_id"])
1540 except vimconn.vimconnNotFoundException as e:
1541 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1542 except vimconn.vimconnException as e:
1543 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1544 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1545 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
1546 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1547 mydb.delete_row_by_id('images', image)
1548 except db_base_Exception as e:
1549 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
1550 undeletedItems.append("image %s" % image)
1551
1552 return vnf_id + " " + vnf["name"]
1553 #if undeletedItems:
1554 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1555
1556
1557 def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1558 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1559 if result < 0:
1560 return result, vims
1561 elif result == 0:
1562 return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
1563 myvim = vims.values()[0]
1564 result,servers = myvim.get_hosts_info()
1565 if result < 0:
1566 return result, servers
1567 topology = {'name':myvim['name'] , 'servers': servers}
1568 return result, topology
1569
1570
1571 def get_hosts(mydb, nfvo_tenant_id):
1572 vims = get_vim(mydb, nfvo_tenant_id)
1573 if len(vims) == 0:
1574 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), HTTP_Not_Found)
1575 elif len(vims)>1:
1576 #print "nfvo.datacenter_action() error. Several datacenters found"
1577 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
1578 myvim = vims.values()[0]
1579 try:
1580 hosts = myvim.get_hosts()
1581 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
1582
1583 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1584 for host in hosts:
1585 server={'name':host['name'], 'vms':[]}
1586 for vm in host['instances']:
1587 #get internal name and model
1588 try:
1589 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1590 WHERE={'vim_vm_id':vm['id']} )
1591 if len(c) == 0:
1592 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1593 continue
1594 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1595
1596 except db_base_Exception as e:
1597 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1598 datacenter['Datacenters'][0]['servers'].append(server)
1599 #return -400, "en construccion"
1600
1601 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1602 return datacenter
1603 except vimconn.vimconnException as e:
1604 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
1605
1606
1607 def new_scenario(mydb, tenant_id, topo):
1608
1609 # result, vims = get_vim(mydb, tenant_id)
1610 # if result < 0:
1611 # return result, vims
1612 #1: parse input
1613 if tenant_id != "any":
1614 check_tenant(mydb, tenant_id)
1615 if "tenant_id" in topo:
1616 if topo["tenant_id"] != tenant_id:
1617 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1618 HTTP_Unauthorized)
1619 else:
1620 tenant_id=None
1621
1622 #1.1: get VNFs and external_networks (other_nets).
1623 vnfs={}
1624 other_nets={} #external_networks, bridge_networks and data_networkds
1625 nodes = topo['topology']['nodes']
1626 for k in nodes.keys():
1627 if nodes[k]['type'] == 'VNF':
1628 vnfs[k] = nodes[k]
1629 vnfs[k]['ifaces'] = {}
1630 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1631 other_nets[k] = nodes[k]
1632 other_nets[k]['external']=True
1633 elif nodes[k]['type'] == 'network':
1634 other_nets[k] = nodes[k]
1635 other_nets[k]['external']=False
1636
1637
1638 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1639 for name,vnf in vnfs.items():
1640 where={}
1641 where_or={"tenant_id": tenant_id, 'public': "true"}
1642 error_text = ""
1643 error_pos = "'topology':'nodes':'" + name + "'"
1644 if 'vnf_id' in vnf:
1645 error_text += " 'vnf_id' " + vnf['vnf_id']
1646 where['uuid'] = vnf['vnf_id']
1647 if 'VNF model' in vnf:
1648 error_text += " 'VNF model' " + vnf['VNF model']
1649 where['name'] = vnf['VNF model']
1650 if len(where) == 0:
1651 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, HTTP_Bad_Request)
1652
1653 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1654 FROM='vnfs',
1655 WHERE=where,
1656 WHERE_OR=where_or,
1657 WHERE_AND_OR="AND")
1658 if len(vnf_db)==0:
1659 raise NfvoException("unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1660 elif len(vnf_db)>1:
1661 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1662 vnf['uuid']=vnf_db[0]['uuid']
1663 vnf['description']=vnf_db[0]['description']
1664 #get external interfaces
1665 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1666 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1667 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
1668 for ext_iface in ext_ifaces:
1669 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1670
1671 #1.4 get list of connections
1672 conections = topo['topology']['connections']
1673 conections_list = []
1674 conections_list_name = []
1675 for k in conections.keys():
1676 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1677 ifaces_list = conections[k]['nodes'].items()
1678 elif type(conections[k]['nodes'])==list: #list with dictionary
1679 ifaces_list=[]
1680 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1681 for k2 in conection_pair_list:
1682 ifaces_list += k2
1683
1684 con_type = conections[k].get("type", "link")
1685 if con_type != "link":
1686 if k in other_nets:
1687 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), HTTP_Bad_Request)
1688 other_nets[k] = {'external': False}
1689 if conections[k].get("graph"):
1690 other_nets[k]["graph"] = conections[k]["graph"]
1691 ifaces_list.append( (k, None) )
1692
1693
1694 if con_type == "external_network":
1695 other_nets[k]['external'] = True
1696 if conections[k].get("model"):
1697 other_nets[k]["model"] = conections[k]["model"]
1698 else:
1699 other_nets[k]["model"] = k
1700 if con_type == "dataplane_net" or con_type == "bridge_net":
1701 other_nets[k]["model"] = con_type
1702
1703 conections_list_name.append(k)
1704 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)
1705 #print set(ifaces_list)
1706 #check valid VNF and iface names
1707 for iface in ifaces_list:
1708 if iface[0] not in vnfs and iface[0] not in other_nets :
1709 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1710 str(k), iface[0]), HTTP_Not_Found)
1711 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
1712 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1713 str(k), iface[0], iface[1]), HTTP_Not_Found)
1714
1715 #1.5 unify connections from the pair list to a consolidated list
1716 index=0
1717 while index < len(conections_list):
1718 index2 = index+1
1719 while index2 < len(conections_list):
1720 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1721 conections_list[index] |= conections_list[index2]
1722 del conections_list[index2]
1723 del conections_list_name[index2]
1724 else:
1725 index2 += 1
1726 conections_list[index] = list(conections_list[index]) # from set to list again
1727 index += 1
1728 #for k in conections_list:
1729 # print k
1730
1731
1732
1733 #1.6 Delete non external nets
1734 # for k in other_nets.keys():
1735 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1736 # for con in conections_list:
1737 # delete_indexes=[]
1738 # for index in range(0,len(con)):
1739 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1740 # for index in delete_indexes:
1741 # del con[index]
1742 # del other_nets[k]
1743 #1.7: Check external_ports are present at database table datacenter_nets
1744 for k,net in other_nets.items():
1745 error_pos = "'topology':'nodes':'" + k + "'"
1746 if net['external']==False:
1747 if 'name' not in net:
1748 net['name']=k
1749 if 'model' not in net:
1750 raise NfvoException("needed a 'model' at " + error_pos, HTTP_Bad_Request)
1751 if net['model']=='bridge_net':
1752 net['type']='bridge';
1753 elif net['model']=='dataplane_net':
1754 net['type']='data';
1755 else:
1756 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, HTTP_Not_Found)
1757 else: #external
1758 #IF we do not want to check that external network exist at datacenter
1759 pass
1760 #ELSE
1761 # error_text = ""
1762 # WHERE_={}
1763 # if 'net_id' in net:
1764 # error_text += " 'net_id' " + net['net_id']
1765 # WHERE_['uuid'] = net['net_id']
1766 # if 'model' in net:
1767 # error_text += " 'model' " + net['model']
1768 # WHERE_['name'] = net['model']
1769 # if len(WHERE_) == 0:
1770 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1771 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1772 # FROM='datacenter_nets', WHERE=WHERE_ )
1773 # if r<0:
1774 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1775 # elif r==0:
1776 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1777 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1778 # elif r>1:
1779 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1780 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1781 # other_nets[k].update(net_db[0])
1782 #ENDIF
1783 net_list={}
1784 net_nb=0 #Number of nets
1785 for con in conections_list:
1786 #check if this is connected to a external net
1787 other_net_index=-1
1788 #print
1789 #print "con", con
1790 for index in range(0,len(con)):
1791 #check if this is connected to a external net
1792 for net_key in other_nets.keys():
1793 if con[index][0]==net_key:
1794 if other_net_index>=0:
1795 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
1796 #print "nfvo.new_scenario " + error_text
1797 raise NfvoException(error_text, HTTP_Bad_Request)
1798 else:
1799 other_net_index = index
1800 net_target = net_key
1801 break
1802 #print "other_net_index", other_net_index
1803 try:
1804 if other_net_index>=0:
1805 del con[other_net_index]
1806 #IF we do not want to check that external network exist at datacenter
1807 if other_nets[net_target]['external'] :
1808 if "name" not in other_nets[net_target]:
1809 other_nets[net_target]['name'] = other_nets[net_target]['model']
1810 if other_nets[net_target]["type"] == "external_network":
1811 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
1812 other_nets[net_target]["type"] = "data"
1813 else:
1814 other_nets[net_target]["type"] = "bridge"
1815 #ELSE
1816 # if other_nets[net_target]['external'] :
1817 # 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
1818 # if type_=='data' and other_nets[net_target]['type']=="ptp":
1819 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
1820 # print "nfvo.new_scenario " + error_text
1821 # return -HTTP_Bad_Request, error_text
1822 #ENDIF
1823 for iface in con:
1824 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1825 else:
1826 #create a net
1827 net_type_bridge=False
1828 net_type_data=False
1829 net_target = "__-__net"+str(net_nb)
1830 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
1831 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
1832 'external':False}
1833 for iface in con:
1834 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
1835 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
1836 if iface_type=='mgmt' or iface_type=='bridge':
1837 net_type_bridge = True
1838 else:
1839 net_type_data = True
1840 if net_type_bridge and net_type_data:
1841 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
1842 #print "nfvo.new_scenario " + error_text
1843 raise NfvoException(error_text, HTTP_Bad_Request)
1844 elif net_type_bridge:
1845 type_='bridge'
1846 else:
1847 type_='data' if len(con)>2 else 'ptp'
1848 net_list[net_target]['type'] = type_
1849 net_nb+=1
1850 except Exception:
1851 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
1852 #print "nfvo.new_scenario " + error_text
1853 #raise e
1854 raise NfvoException(error_text, HTTP_Bad_Request)
1855
1856 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
1857 #1.8.1 obtain management net
1858 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
1859 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
1860 #1.8.2 check all interfaces from all vnfs
1861 if len(mgmt_net)>0:
1862 add_mgmt_net = False
1863 for vnf in vnfs.values():
1864 for iface in vnf['ifaces'].values():
1865 if iface['type']=='mgmt' and 'net_key' not in iface:
1866 #iface not connected
1867 iface['net_key'] = 'mgmt'
1868 add_mgmt_net = True
1869 if add_mgmt_net and 'mgmt' not in net_list:
1870 net_list['mgmt']=mgmt_net[0]
1871 net_list['mgmt']['external']=True
1872 net_list['mgmt']['graph']={'visible':False}
1873
1874 net_list.update(other_nets)
1875 #print
1876 #print 'net_list', net_list
1877 #print
1878 #print 'vnfs', vnfs
1879 #print
1880
1881 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
1882 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
1883 'tenant_id':tenant_id, 'name':topo['name'],
1884 'description':topo.get('description',topo['name']),
1885 'public': topo.get('public', False)
1886 })
1887
1888 return c
1889
1890
1891 def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
1892 """ This creates a new scenario for version 0.2 and 0.3"""
1893 scenario = scenario_dict["scenario"]
1894 if tenant_id != "any":
1895 check_tenant(mydb, tenant_id)
1896 if "tenant_id" in scenario:
1897 if scenario["tenant_id"] != tenant_id:
1898 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
1899 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
1900 scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
1901 else:
1902 tenant_id=None
1903
1904 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
1905 for name,vnf in scenario["vnfs"].iteritems():
1906 where={}
1907 where_or={"tenant_id": tenant_id, 'public': "true"}
1908 error_text = ""
1909 error_pos = "'scenario':'vnfs':'" + name + "'"
1910 if 'vnf_id' in vnf:
1911 error_text += " 'vnf_id' " + vnf['vnf_id']
1912 where['uuid'] = vnf['vnf_id']
1913 if 'vnf_name' in vnf:
1914 error_text += " 'vnf_name' " + vnf['vnf_name']
1915 where['name'] = vnf['vnf_name']
1916 if len(where) == 0:
1917 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
1918 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
1919 FROM='vnfs',
1920 WHERE=where,
1921 WHERE_OR=where_or,
1922 WHERE_AND_OR="AND")
1923 if len(vnf_db) == 0:
1924 raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
1925 elif len(vnf_db) > 1:
1926 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
1927 vnf['uuid'] = vnf_db[0]['uuid']
1928 vnf['description'] = vnf_db[0]['description']
1929 vnf['ifaces'] = {}
1930 # get external interfaces
1931 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
1932 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1933 WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name': None} )
1934 for ext_iface in ext_ifaces:
1935 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
1936 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
1937
1938 # 2: Insert net_key and ip_address at every vnf interface
1939 for net_name, net in scenario["networks"].items():
1940 net_type_bridge = False
1941 net_type_data = False
1942 for iface_dict in net["interfaces"]:
1943 if version == "0.2":
1944 temp_dict = iface_dict
1945 ip_address = None
1946 elif version == "0.3":
1947 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
1948 ip_address = iface_dict.get('ip_address', None)
1949 for vnf, iface in temp_dict.items():
1950 if vnf not in scenario["vnfs"]:
1951 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
1952 net_name, vnf)
1953 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1954 raise NfvoException(error_text, HTTP_Not_Found)
1955 if iface not in scenario["vnfs"][vnf]['ifaces']:
1956 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
1957 .format(net_name, iface)
1958 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1959 raise NfvoException(error_text, HTTP_Bad_Request)
1960 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
1961 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
1962 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
1963 # logger.debug("nfvo.new_scenario_v02 " + error_text)
1964 raise NfvoException(error_text, HTTP_Bad_Request)
1965 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
1966 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
1967 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
1968 if iface_type == 'mgmt' or iface_type == 'bridge':
1969 net_type_bridge = True
1970 else:
1971 net_type_data = True
1972
1973 if net_type_bridge and net_type_data:
1974 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
1975 .format(net_name)
1976 # logger.debug("nfvo.new_scenario " + error_text)
1977 raise NfvoException(error_text, HTTP_Bad_Request)
1978 elif net_type_bridge:
1979 type_ = 'bridge'
1980 else:
1981 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
1982
1983 if net.get("implementation"): # for v0.3
1984 if type_ == "bridge" and net["implementation"] == "underlay":
1985 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
1986 "'network':'{}'".format(net_name)
1987 # logger.debug(error_text)
1988 raise NfvoException(error_text, HTTP_Bad_Request)
1989 elif type_ != "bridge" and net["implementation"] == "overlay":
1990 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
1991 "'network':'{}'".format(net_name)
1992 # logger.debug(error_text)
1993 raise NfvoException(error_text, HTTP_Bad_Request)
1994 net.pop("implementation")
1995 if "type" in net and version == "0.3": # for v0.3
1996 if type_ == "data" and net["type"] == "e-line":
1997 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
1998 "'e-line' at 'network':'{}'".format(net_name)
1999 # logger.debug(error_text)
2000 raise NfvoException(error_text, HTTP_Bad_Request)
2001 elif type_ == "ptp" and net["type"] == "e-lan":
2002 type_ = "data"
2003
2004 net['type'] = type_
2005 net['name'] = net_name
2006 net['external'] = net.get('external', False)
2007
2008 # 3: insert at database
2009 scenario["nets"] = scenario["networks"]
2010 scenario['tenant_id'] = tenant_id
2011 scenario_id = mydb.new_scenario(scenario)
2012 return scenario_id
2013
2014
2015 def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2016 """
2017 Parses an OSM IM nsd_catalog and insert at DB
2018 :param mydb:
2019 :param tenant_id:
2020 :param nsd_descriptor:
2021 :return: The list of cretated NSD ids
2022 """
2023 try:
2024 mynsd = nsd_catalog.nsd()
2025 try:
2026 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd)
2027 except Exception as e:
2028 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), HTTP_Bad_Request)
2029 db_scenarios = []
2030 db_sce_nets = []
2031 db_sce_vnfs = []
2032 db_sce_interfaces = []
2033 db_ip_profiles = []
2034 db_ip_profiles_index = 0
2035 uuid_list = []
2036 nsd_uuid_list = []
2037 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2038 nsd = nsd_yang.get()
2039
2040 # table sceanrios
2041 scenario_uuid = str(uuid4())
2042 uuid_list.append(scenario_uuid)
2043 nsd_uuid_list.append(scenario_uuid)
2044 db_scenario = {
2045 "uuid": scenario_uuid,
2046 "osm_id": get_str(nsd, "id", 255),
2047 "name": get_str(nsd, "name", 255),
2048 "description": get_str(nsd, "description", 255),
2049 "tenant_id": tenant_id,
2050 "vendor": get_str(nsd, "vendor", 255),
2051 "short_name": get_str(nsd, "short-name", 255),
2052 "descriptor": str(nsd_descriptor)[:60000],
2053 }
2054 db_scenarios.append(db_scenario)
2055
2056 # table sce_vnfs (constituent-vnfd)
2057 vnf_index2scevnf_uuid = {}
2058 vnf_index2vnf_uuid = {}
2059 for vnf in nsd.get("constituent-vnfd").itervalues():
2060 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2061 'tenant_id': tenant_id})
2062 if not existing_vnf:
2063 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2064 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2065 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2066 HTTP_Bad_Request)
2067 sce_vnf_uuid = str(uuid4())
2068 uuid_list.append(sce_vnf_uuid)
2069 db_sce_vnf = {
2070 "uuid": sce_vnf_uuid,
2071 "scenario_id": scenario_uuid,
2072 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 5),
2073 "vnf_id": existing_vnf[0]["uuid"],
2074 "member_vnf_index": int(vnf["member-vnf-index"]),
2075 # TODO 'start-by-default': True
2076 }
2077 vnf_index2scevnf_uuid[int(vnf['member-vnf-index'])] = sce_vnf_uuid
2078 vnf_index2vnf_uuid[int(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
2079 db_sce_vnfs.append(db_sce_vnf)
2080
2081 # table ip_profiles (ip-profiles)
2082 ip_profile_name2db_table_index = {}
2083 for ip_profile in nsd.get("ip-profiles").itervalues():
2084 db_ip_profile = {
2085 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2086 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2087 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2088 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2089 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2090 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2091 }
2092 dns_list = []
2093 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2094 dns_list.append(str(dns.get("address")))
2095 db_ip_profile["dns_address"] = ";".join(dns_list)
2096 if ip_profile["ip-profile-params"].get('security-group'):
2097 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2098 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2099 db_ip_profiles_index += 1
2100 db_ip_profiles.append(db_ip_profile)
2101
2102 # table sce_nets (internal-vld)
2103 for vld in nsd.get("vld").itervalues():
2104 sce_net_uuid = str(uuid4())
2105 uuid_list.append(sce_net_uuid)
2106 db_sce_net = {
2107 "uuid": sce_net_uuid,
2108 "name": get_str(vld, "name", 255),
2109 "scenario_id": scenario_uuid,
2110 # "type": #TODO
2111 "multipoint": not vld.get("type") == "ELINE",
2112 # "external": #TODO
2113 "description": get_str(vld, "description", 255),
2114 }
2115 # guess type of network
2116 if vld.get("mgmt-network"):
2117 db_sce_net["type"] = "bridge"
2118 db_sce_net["external"] = True
2119 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2120 db_sce_net["type"] = "data"
2121 else:
2122 db_sce_net["type"] = "bridge"
2123 db_sce_nets.append(db_sce_net)
2124
2125 # ip-profile, link db_ip_profile with db_sce_net
2126 if vld.get("ip-profile-ref"):
2127 ip_profile_name = vld.get("ip-profile-ref")
2128 if ip_profile_name not in ip_profile_name2db_table_index:
2129 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2130 " Reference to a non-existing 'ip_profiles'".format(
2131 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2132 HTTP_Bad_Request)
2133 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
2134
2135 # table sce_interfaces (vld:vnfd-connection-point-ref)
2136 for iface in vld.get("vnfd-connection-point-ref").itervalues():
2137 vnf_index = int(iface['member-vnf-index-ref'])
2138 # check correct parameters
2139 if vnf_index not in vnf_index2vnf_uuid:
2140 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2141 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2142 "'nsd':'constituent-vnfd'".format(
2143 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2144 HTTP_Bad_Request)
2145
2146 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2147 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2148 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2149 'external_name': get_str(iface, "vnfd-connection-point-ref",
2150 255)})
2151 if not existing_ifaces:
2152 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2153 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2154 "connection-point name at VNFD '{}'".format(
2155 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2156 str(iface.get("vnfd-id-ref"))[:255]),
2157 HTTP_Bad_Request)
2158 interface_uuid = existing_ifaces[0]["uuid"]
2159 sce_interface_uuid = str(uuid4())
2160 uuid_list.append(sce_net_uuid)
2161 db_sce_interface = {
2162 "uuid": sce_interface_uuid,
2163 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2164 "sce_net_id": sce_net_uuid,
2165 "interface_id": interface_uuid,
2166 # "ip_address": #TODO
2167 }
2168 db_sce_interfaces.append(db_sce_interface)
2169
2170 db_tables = [
2171 {"scenarios": db_scenarios},
2172 {"sce_nets": db_sce_nets},
2173 {"ip_profiles": db_ip_profiles},
2174 {"sce_vnfs": db_sce_vnfs},
2175 {"sce_interfaces": db_sce_interfaces},
2176 ]
2177
2178 logger.debug("create_vnf Deployment done vnfDict: %s",
2179 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2180 mydb.new_rows(db_tables, uuid_list)
2181 return nsd_uuid_list
2182 except NfvoException:
2183 raise
2184 except Exception as e:
2185 logger.error("Exception {}".format(e))
2186 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2187
2188
2189 def edit_scenario(mydb, tenant_id, scenario_id, data):
2190 data["uuid"] = scenario_id
2191 data["tenant_id"] = tenant_id
2192 c = mydb.edit_scenario( data )
2193 return c
2194
2195
2196 def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
2197 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2198 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2199 vims = {datacenter_id: myvim}
2200 myvim_tenant = myvim['tenant_id']
2201 datacenter_name = myvim['name']
2202
2203 rollbackList=[]
2204 try:
2205 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2206 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
2207 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
2208 scenarioDict['datacenter_id'] = datacenter_id
2209 #print '================scenarioDict======================='
2210 #print json.dumps(scenarioDict, indent=4)
2211 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2212
2213 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2214 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2215
2216 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2217 auxNetDict['scenario'] = {}
2218
2219 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2220 for sce_net in scenarioDict['nets']:
2221 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2222
2223 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
2224 myNetName = myNetName[0:255] #limit length
2225 myNetType = sce_net['type']
2226 myNetDict = {}
2227 myNetDict["name"] = myNetName
2228 myNetDict["type"] = myNetType
2229 myNetDict["tenant_id"] = myvim_tenant
2230 myNetIPProfile = sce_net.get('ip_profile', None)
2231 #TODO:
2232 #We should use the dictionary as input parameter for new_network
2233 #print myNetDict
2234 if not sce_net["external"]:
2235 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
2236 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2237 sce_net['vim_id'] = network_id
2238 auxNetDict['scenario'][sce_net['uuid']] = network_id
2239 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2240 sce_net["created"] = True
2241 else:
2242 if sce_net['vim_id'] == None:
2243 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2244 _, message = rollback(mydb, vims, rollbackList)
2245 logger.error("nfvo.start_scenario: %s", error_text)
2246 raise NfvoException(error_text, HTTP_Bad_Request)
2247 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2248 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
2249
2250 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2251 #For each vnf net, we create it and we add it to instanceNetlist.
2252
2253 for sce_vnf in scenarioDict['vnfs']:
2254 for net in sce_vnf['nets']:
2255 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2256
2257 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2258 myNetName = myNetName[0:255] #limit length
2259 myNetType = net['type']
2260 myNetDict = {}
2261 myNetDict["name"] = myNetName
2262 myNetDict["type"] = myNetType
2263 myNetDict["tenant_id"] = myvim_tenant
2264 myNetIPProfile = net.get('ip_profile', None)
2265 #print myNetDict
2266 #TODO:
2267 #We should use the dictionary as input parameter for new_network
2268 network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
2269 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2270 net['vim_id'] = network_id
2271 if sce_vnf['uuid'] not in auxNetDict:
2272 auxNetDict[sce_vnf['uuid']] = {}
2273 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2274 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2275 net["created"] = True
2276
2277 #print "auxNetDict:"
2278 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2279
2280 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2281 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2282 i = 0
2283 for sce_vnf in scenarioDict['vnfs']:
2284 vnf_availability_zones = []
2285 for vm in sce_vnf['vms']:
2286 vm_av = vm.get('availability_zone')
2287 if vm_av and vm_av not in vnf_availability_zones:
2288 vnf_availability_zones.append(vm_av)
2289
2290 # check if there is enough availability zones available at vim level.
2291 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2292 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2293 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
2294
2295 for vm in sce_vnf['vms']:
2296 i += 1
2297 myVMDict = {}
2298 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2299 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
2300 #myVMDict['description'] = vm['description']
2301 myVMDict['description'] = myVMDict['name'][0:99]
2302 if not startvms:
2303 myVMDict['start'] = "no"
2304 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2305 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2306
2307 #create image at vim in case it not exist
2308 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
2309 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
2310 vm['vim_image_id'] = image_id
2311
2312 #create flavor at vim in case it not exist
2313 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
2314 if flavor_dict['extended']!=None:
2315 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
2316 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
2317 vm['vim_flavor_id'] = flavor_id
2318
2319
2320 myVMDict['imageRef'] = vm['vim_image_id']
2321 myVMDict['flavorRef'] = vm['vim_flavor_id']
2322 myVMDict['networks'] = []
2323 for iface in vm['interfaces']:
2324 netDict = {}
2325 if iface['type']=="data":
2326 netDict['type'] = iface['model']
2327 elif "model" in iface and iface["model"]!=None:
2328 netDict['model']=iface['model']
2329 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2330 #discover type of interface looking at flavor
2331 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2332 for flavor_iface in numa.get('interfaces',[]):
2333 if flavor_iface.get('name') == iface['internal_name']:
2334 if flavor_iface['dedicated'] == 'yes':
2335 netDict['type']="PF" #passthrough
2336 elif flavor_iface['dedicated'] == 'no':
2337 netDict['type']="VF" #siov
2338 elif flavor_iface['dedicated'] == 'yes:sriov':
2339 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2340 netDict["mac_address"] = flavor_iface.get("mac_address")
2341 break;
2342 netDict["use"]=iface['type']
2343 if netDict["use"]=="data" and not netDict.get("type"):
2344 #print "netDict", netDict
2345 #print "iface", iface
2346 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'])
2347 if flavor_dict.get('extended')==None:
2348 raise NfvoException(e_text + "After database migration some information is not available. \
2349 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
2350 else:
2351 raise NfvoException(e_text, HTTP_Internal_Server_Error)
2352 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2353 netDict["type"]="virtual"
2354 if "vpci" in iface and iface["vpci"] is not None:
2355 netDict['vpci'] = iface['vpci']
2356 if "mac" in iface and iface["mac"] is not None:
2357 netDict['mac_address'] = iface['mac']
2358 if "port-security" in iface and iface["port-security"] is not None:
2359 netDict['port_security'] = iface['port-security']
2360 if "floating-ip" in iface and iface["floating-ip"] is not None:
2361 netDict['floating_ip'] = iface['floating-ip']
2362 netDict['name'] = iface['internal_name']
2363 if iface['net_id'] is None:
2364 for vnf_iface in sce_vnf["interfaces"]:
2365 #print iface
2366 #print vnf_iface
2367 if vnf_iface['interface_id']==iface['uuid']:
2368 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2369 break
2370 else:
2371 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2372 #skip bridge ifaces not connected to any net
2373 #if 'net_id' not in netDict or netDict['net_id']==None:
2374 # continue
2375 myVMDict['networks'].append(netDict)
2376 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2377 #print myVMDict['name']
2378 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2379 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2380 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2381
2382 if 'availability_zone' in myVMDict:
2383 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
2384 else:
2385 av_index = None
2386
2387 vm_id = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2388 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2389 availability_zone_index=av_index,
2390 availability_zone_list=vnf_availability_zones)
2391 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2392 vm['vim_id'] = vm_id
2393 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2394 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2395 for net in myVMDict['networks']:
2396 if "vim_id" in net:
2397 for iface in vm['interfaces']:
2398 if net["name"]==iface["internal_name"]:
2399 iface["vim_id"]=net["vim_id"]
2400 break
2401
2402 logger.debug("start scenario Deployment done")
2403 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2404 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2405 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2406 return mydb.get_instance_scenario(instance_id)
2407
2408 except (db_base_Exception, vimconn.vimconnException) as e:
2409 _, message = rollback(mydb, vims, rollbackList)
2410 if isinstance(e, db_base_Exception):
2411 error_text = "Exception at database"
2412 else:
2413 error_text = "Exception at VIM"
2414 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2415 #logger.error("start_scenario %s", error_text)
2416 raise NfvoException(error_text, e.http_code)
2417
2418 def unify_cloud_config(cloud_config_preserve, cloud_config):
2419 """ join the cloud config information into cloud_config_preserve.
2420 In case of conflict cloud_config_preserve preserves
2421 None is allowed
2422 """
2423 if not cloud_config_preserve and not cloud_config:
2424 return None
2425
2426 new_cloud_config = {"key-pairs":[], "users":[]}
2427 # key-pairs
2428 if cloud_config_preserve:
2429 for key in cloud_config_preserve.get("key-pairs", () ):
2430 if key not in new_cloud_config["key-pairs"]:
2431 new_cloud_config["key-pairs"].append(key)
2432 if cloud_config:
2433 for key in cloud_config.get("key-pairs", () ):
2434 if key not in new_cloud_config["key-pairs"]:
2435 new_cloud_config["key-pairs"].append(key)
2436 if not new_cloud_config["key-pairs"]:
2437 del new_cloud_config["key-pairs"]
2438
2439 # users
2440 if cloud_config:
2441 new_cloud_config["users"] += cloud_config.get("users", () )
2442 if cloud_config_preserve:
2443 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
2444 index_to_delete = []
2445 users = new_cloud_config.get("users", [])
2446 for index0 in range(0,len(users)):
2447 if index0 in index_to_delete:
2448 continue
2449 for index1 in range(index0+1,len(users)):
2450 if index1 in index_to_delete:
2451 continue
2452 if users[index0]["name"] == users[index1]["name"]:
2453 index_to_delete.append(index1)
2454 for key in users[index1].get("key-pairs",()):
2455 if "key-pairs" not in users[index0]:
2456 users[index0]["key-pairs"] = [key]
2457 elif key not in users[index0]["key-pairs"]:
2458 users[index0]["key-pairs"].append(key)
2459 index_to_delete.sort(reverse=True)
2460 for index in index_to_delete:
2461 del users[index]
2462 if not new_cloud_config["users"]:
2463 del new_cloud_config["users"]
2464
2465 #boot-data-drive
2466 if cloud_config and cloud_config.get("boot-data-drive") != None:
2467 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2468 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2469 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2470
2471 # user-data
2472 new_cloud_config["user-data"] = []
2473 if cloud_config and cloud_config.get("user-data"):
2474 if isinstance(cloud_config["user-data"], list):
2475 new_cloud_config["user-data"] += cloud_config["user-data"]
2476 else:
2477 new_cloud_config["user-data"].append(cloud_config["user-data"])
2478 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2479 if isinstance(cloud_config_preserve["user-data"], list):
2480 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2481 else:
2482 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2483 if not new_cloud_config["user-data"]:
2484 del new_cloud_config["user-data"]
2485
2486 # config files
2487 new_cloud_config["config-files"] = []
2488 if cloud_config and cloud_config.get("config-files") != None:
2489 new_cloud_config["config-files"] += cloud_config["config-files"]
2490 if cloud_config_preserve:
2491 for file in cloud_config_preserve.get("config-files", ()):
2492 for index in range(0, len(new_cloud_config["config-files"])):
2493 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2494 new_cloud_config["config-files"][index] = file
2495 break
2496 else:
2497 new_cloud_config["config-files"].append(file)
2498 if not new_cloud_config["config-files"]:
2499 del new_cloud_config["config-files"]
2500 return new_cloud_config
2501
2502
2503 def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
2504 datacenter_id = None
2505 datacenter_name = None
2506 thread = None
2507 try:
2508 if datacenter_tenant_id:
2509 thread_id = datacenter_tenant_id
2510 thread = vim_threads["running"].get(datacenter_tenant_id)
2511 else:
2512 where_={"td.nfvo_tenant_id": tenant_id}
2513 if datacenter_id_name:
2514 if utils.check_valid_uuid(datacenter_id_name):
2515 datacenter_id = datacenter_id_name
2516 where_["dt.datacenter_id"] = datacenter_id
2517 else:
2518 datacenter_name = datacenter_id_name
2519 where_["d.name"] = datacenter_name
2520 if datacenter_tenant_id:
2521 where_["dt.uuid"] = datacenter_tenant_id
2522 datacenters = mydb.get_rows(
2523 SELECT=("dt.uuid as datacenter_tenant_id",),
2524 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2525 "join datacenters as d on d.uuid=dt.datacenter_id",
2526 WHERE=where_)
2527 if len(datacenters) > 1:
2528 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2529 elif datacenters:
2530 thread_id = datacenters[0]["datacenter_tenant_id"]
2531 thread = vim_threads["running"].get(thread_id)
2532 if not thread:
2533 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2534 return thread_id, thread
2535 except db_base_Exception as e:
2536 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
2537
2538
2539 def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2540 WHERE_dict={}
2541 if utils.check_valid_uuid(datacenter_id_name):
2542 WHERE_dict['d.uuid'] = datacenter_id_name
2543 else:
2544 WHERE_dict['d.name'] = datacenter_id_name
2545
2546 if tenant_id:
2547 WHERE_dict['nfvo_tenant_id'] = tenant_id
2548 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2549 " dt on td.datacenter_tenant_id=dt.uuid"
2550 else:
2551 from_ = 'datacenters as d'
2552 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid",), WHERE=WHERE_dict )
2553 if len(vimaccounts) == 0:
2554 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2555 elif len(vimaccounts)>1:
2556 #print "nfvo.datacenter_action() error. Several datacenters found"
2557 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2558 return vimaccounts[0]["uuid"]
2559
2560
2561 def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
2562 datacenter_id = None
2563 datacenter_name = None
2564 if datacenter_id_name:
2565 if utils.check_valid_uuid(datacenter_id_name):
2566 datacenter_id = datacenter_id_name
2567 else:
2568 datacenter_name = datacenter_id_name
2569 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
2570 if len(vims) == 0:
2571 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
2572 elif len(vims)>1:
2573 #print "nfvo.datacenter_action() error. Several datacenters found"
2574 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
2575 return vims.keys()[0], vims.values()[0]
2576
2577
2578 def update(d, u):
2579 '''Takes dict d and updates it with the values in dict u.'''
2580 '''It merges all depth levels'''
2581 for k, v in u.iteritems():
2582 if isinstance(v, collections.Mapping):
2583 r = update(d.get(k, {}), v)
2584 d[k] = r
2585 else:
2586 d[k] = u[k]
2587 return d
2588
2589 def create_instance(mydb, tenant_id, instance_dict):
2590 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2591 # logger.debug("Creating instance...")
2592 scenario = instance_dict["scenario"]
2593
2594 # find main datacenter
2595 myvims = {}
2596 myvim_threads_id = {}
2597 datacenter = instance_dict.get("datacenter")
2598 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
2599 myvims[default_datacenter_id] = vim
2600 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
2601 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
2602 # myvim_tenant = myvim['tenant_id']
2603
2604 rollbackList=[]
2605
2606 # print "Checking that the scenario exists and getting the scenario dictionary"
2607 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
2608 datacenter_id=default_datacenter_id)
2609
2610 # logger.debug(">>>>>> Dictionaries before merging")
2611 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2612 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
2613
2614 db_instance_vnfs = []
2615 db_instance_vms = []
2616 db_instance_interfaces = []
2617 db_ip_profiles = []
2618 db_vim_actions = []
2619 uuid_list = []
2620 task_index = 0
2621 instance_name = instance_dict["name"]
2622 instance_uuid = str(uuid4())
2623 uuid_list.append(instance_uuid)
2624 db_instance_scenario = {
2625 "uuid": instance_uuid,
2626 "name": instance_name,
2627 "tenant_id": tenant_id,
2628 "scenario_id": scenarioDict['uuid'],
2629 "datacenter_id": default_datacenter_id,
2630 # filled bellow 'datacenter_tenant_id'
2631 "description": instance_dict.get("description"),
2632 }
2633 if scenarioDict.get("cloud-config"):
2634 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
2635 default_flow_style=True, width=256)
2636 instance_action_id = get_task_id()
2637 db_instance_action = {
2638 "uuid": instance_action_id, # same uuid for the instance and the action on create
2639 "tenant_id": tenant_id,
2640 "instance_id": instance_uuid,
2641 "description": "CREATE",
2642 }
2643
2644 # Auxiliary dictionaries from x to y
2645 vnf_net2instance = {}
2646 sce_net2instance = {}
2647 net2task_id = {'scenario': {}}
2648
2649 # logger.debug("Creating instance from scenario-dict:\n%s",
2650 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2651 try:
2652 # 0 check correct parameters
2653 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
2654 found = False
2655 for scenario_net in scenarioDict['nets']:
2656 if net_name == scenario_net["name"]:
2657 found = True
2658 break
2659 if not found:
2660 raise NfvoException("Invalid scenario network name '{}' at instance:networks".format(net_name),
2661 HTTP_Bad_Request)
2662 if "sites" not in net_instance_desc:
2663 net_instance_desc["sites"] = [ {} ]
2664 site_without_datacenter_field = False
2665 for site in net_instance_desc["sites"]:
2666 if site.get("datacenter"):
2667 site["datacenter"] = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
2668 if site["datacenter"] not in myvims:
2669 # Add this datacenter to myvims
2670 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
2671 myvims[d] = v
2672 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
2673 site["datacenter"] = d # change name to id
2674 else:
2675 if site_without_datacenter_field:
2676 raise NfvoException("Found more than one entries without datacenter field at "
2677 "instance:networks:{}:sites".format(net_name), HTTP_Bad_Request)
2678 site_without_datacenter_field = True
2679 site["datacenter"] = default_datacenter_id # change name to id
2680
2681 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
2682 found = False
2683 for scenario_vnf in scenarioDict['vnfs']:
2684 if vnf_name == scenario_vnf['name']:
2685 found = True
2686 break
2687 if not found:
2688 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_instance_desc), HTTP_Bad_Request)
2689 if "datacenter" in vnf_instance_desc:
2690 # Add this datacenter to myvims
2691 vnf_instance_desc["datacenter"] = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2692 if vnf_instance_desc["datacenter"] not in myvims:
2693 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
2694 myvims[d] = v
2695 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
2696 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
2697
2698 # 0.1 parse cloud-config parameters
2699 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
2700 # We add the RO key to cloud_config
2701 if tenant[0].get('RO_pub_key'):
2702 RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
2703 cloud_config = unify_cloud_config(cloud_config, RO_key)
2704
2705 # 0.2 merge instance information into scenario
2706 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
2707 # However, this is not possible yet.
2708 for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
2709 for scenario_net in scenarioDict['nets']:
2710 if net_name == scenario_net["name"]:
2711 if 'ip-profile' in net_instance_desc:
2712 # translate from input format to database format
2713 ipprofile_in = net_instance_desc['ip-profile']
2714 ipprofile_db = {}
2715 ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address')
2716 ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4')
2717 ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address')
2718 ipprofile_db['dns_address'] = ipprofile_in.get('dns-address')
2719 if isinstance(ipprofile_db['dns_address'], (list, tuple)):
2720 ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address'])
2721 if 'dhcp' in ipprofile_in:
2722 ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address')
2723 ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True)
2724 ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' )
2725 if 'ip_profile' not in scenario_net:
2726 scenario_net['ip_profile'] = ipprofile_db
2727 else:
2728 update(scenario_net['ip_profile'], ipprofile_db)
2729 for interface in net_instance_desc.get('interfaces', () ):
2730 if 'ip_address' in interface:
2731 for vnf in scenarioDict['vnfs']:
2732 if interface['vnf'] == vnf['name']:
2733 for vnf_interface in vnf['interfaces']:
2734 if interface['vnf_interface'] == vnf_interface['external_name']:
2735 vnf_interface['ip_address']=interface['ip_address']
2736
2737 # logger.debug(">>>>>>>> Merged dictionary")
2738 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
2739 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2740
2741 # 1. Creating new nets (sce_nets) in the VIM"
2742 db_instance_nets = []
2743 for sce_net in scenarioDict['nets']:
2744 descriptor_net = instance_dict.get("networks", {}).get(sce_net["name"], {})
2745 net_name = descriptor_net.get("vim-network-name")
2746 sce_net2instance[sce_net['uuid']] = {}
2747 net2task_id['scenario'][sce_net['uuid']] = {}
2748
2749 sites = descriptor_net.get("sites", [ {} ])
2750 for site in sites:
2751 if site.get("datacenter"):
2752 vim = myvims[ site["datacenter"] ]
2753 datacenter_id = site["datacenter"]
2754 myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
2755 else:
2756 vim = myvims[ default_datacenter_id ]
2757 datacenter_id = default_datacenter_id
2758 myvim_thread_id = myvim_threads_id[default_datacenter_id]
2759 net_type = sce_net['type']
2760 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
2761
2762 if not net_name:
2763 if sce_net["external"]:
2764 net_name = sce_net["name"]
2765 else:
2766 net_name = "{}.{}".format(instance_name, sce_net["name"])
2767 net_name = net_name[:255] # limit length
2768
2769 if "netmap-use" in site or "netmap-create" in site:
2770 create_network = False
2771 lookfor_network = False
2772 if "netmap-use" in site:
2773 lookfor_network = True
2774 if utils.check_valid_uuid(site["netmap-use"]):
2775 filter_text = "scenario id '%s'" % site["netmap-use"]
2776 lookfor_filter["id"] = site["netmap-use"]
2777 else:
2778 filter_text = "scenario name '%s'" % site["netmap-use"]
2779 lookfor_filter["name"] = site["netmap-use"]
2780 if "netmap-create" in site:
2781 create_network = True
2782 net_vim_name = net_name
2783 if site["netmap-create"]:
2784 net_vim_name = site["netmap-create"]
2785 elif sce_net["external"]:
2786 if sce_net['vim_id'] != None:
2787 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
2788 create_network = False
2789 lookfor_network = True
2790 lookfor_filter["id"] = sce_net['vim_id']
2791 filter_text = "vim_id '{}' datacenter_netmap name '{}'. Try to reload vims with "\
2792 "datacenter-net-update".format(sce_net['vim_id'], sce_net["name"])
2793 # look for network at datacenter and return error
2794 else:
2795 # There is not a netmap, look at datacenter for a net with this name and create if not found
2796 create_network = True
2797 lookfor_network = True
2798 lookfor_filter["name"] = sce_net["name"]
2799 net_vim_name = sce_net["name"]
2800 filter_text = "scenario name '%s'" % sce_net["name"]
2801 else:
2802 net_vim_name = net_name
2803 create_network = True
2804 lookfor_network = False
2805
2806 if lookfor_network and create_network:
2807 # TODO create two tasks FIND + CREATE with their relationship
2808 task_action = "FIND"
2809 task_params = (lookfor_filter,)
2810 # task_action = "CREATE"
2811 # task_params = (net_vim_name, net_type, sce_net.get('ip_profile', None))
2812 # task
2813 elif lookfor_network:
2814 task_action = "FIND"
2815 task_params = (lookfor_filter,)
2816 elif create_network:
2817 task_action = "CREATE"
2818 task_params = (net_vim_name, net_type, sce_net.get('ip_profile', None))
2819
2820 # fill database content
2821 net_uuid = str(uuid4())
2822 uuid_list.append(net_uuid)
2823 sce_net2instance[sce_net['uuid']][datacenter_id] = net_uuid
2824 db_net = {
2825 "uuid": net_uuid,
2826 'vim_net_id': None,
2827 "instance_scenario_id": instance_uuid,
2828 "sce_net_id": sce_net["uuid"],
2829 "created": create_network,
2830 'datacenter_id': datacenter_id,
2831 'datacenter_tenant_id': myvim_thread_id,
2832 'status': 'BUILD' if create_network else "ACTIVE"
2833 }
2834 db_instance_nets.append(db_net)
2835 db_vim_action = {
2836 "instance_action_id": instance_action_id,
2837 "status": "SCHEDULED",
2838 "task_index": task_index,
2839 "datacenter_vim_id": myvim_thread_id,
2840 "action": task_action,
2841 "item": "instance_nets",
2842 "item_id": net_uuid,
2843 "extra": yaml.safe_dump({"params": task_params}, default_flow_style=True, width=256)
2844 }
2845 net2task_id['scenario'][sce_net['uuid']][datacenter_id] = task_index
2846 task_index += 1
2847 db_vim_actions.append(db_vim_action)
2848
2849 if 'ip_profile' in sce_net:
2850 db_ip_profile={
2851 'instance_net_id': net_uuid,
2852 'ip_version': sce_net['ip_profile']['ip_version'],
2853 'subnet_address': sce_net['ip_profile']['subnet_address'],
2854 'gateway_address': sce_net['ip_profile']['gateway_address'],
2855 'dns_address': sce_net['ip_profile']['dns_address'],
2856 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
2857 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
2858 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
2859 }
2860 db_ip_profiles.append(db_ip_profile)
2861
2862 # 2. Creating new nets (vnf internal nets) in the VIM"
2863 # For each vnf net, we create it and we add it to instanceNetlist.
2864 for sce_vnf in scenarioDict['vnfs']:
2865 for net in sce_vnf['nets']:
2866 if sce_vnf.get("datacenter"):
2867 datacenter_id = sce_vnf["datacenter"]
2868 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
2869 else:
2870 datacenter_id = default_datacenter_id
2871 myvim_thread_id = myvim_threads_id[default_datacenter_id]
2872 descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
2873 net_name = descriptor_net.get("name")
2874 if not net_name:
2875 net_name = "{}.{}".format(instance_name, net["name"])
2876 net_name = net_name[:255] # limit length
2877 net_type = net['type']
2878
2879 if sce_vnf['uuid'] not in vnf_net2instance:
2880 vnf_net2instance[sce_vnf['uuid']] = {}
2881 if sce_vnf['uuid'] not in net2task_id:
2882 net2task_id[sce_vnf['uuid']] = {}
2883 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
2884
2885 # fill database content
2886 net_uuid = str(uuid4())
2887 uuid_list.append(net_uuid)
2888 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
2889 db_net = {
2890 "uuid": net_uuid,
2891 'vim_net_id': None,
2892 "instance_scenario_id": instance_uuid,
2893 "net_id": net["uuid"],
2894 "created": True,
2895 'datacenter_id': datacenter_id,
2896 'datacenter_tenant_id': myvim_thread_id,
2897 }
2898 db_instance_nets.append(db_net)
2899
2900 db_vim_action = {
2901 "instance_action_id": instance_action_id,
2902 "task_index": task_index,
2903 "datacenter_vim_id": myvim_thread_id,
2904 "status": "SCHEDULED",
2905 "action": "CREATE",
2906 "item": "instance_nets",
2907 "item_id": net_uuid,
2908 "extra": yaml.safe_dump({"params": (net_name, net_type, net.get('ip_profile',None))},
2909 default_flow_style=True, width=256)
2910 }
2911 task_index += 1
2912 db_vim_actions.append(db_vim_action)
2913
2914 if 'ip_profile' in net:
2915 db_ip_profile = {
2916 'instance_net_id': net_uuid,
2917 'ip_version': net['ip_profile']['ip_version'],
2918 'subnet_address': net['ip_profile']['subnet_address'],
2919 'gateway_address': net['ip_profile']['gateway_address'],
2920 'dns_address': net['ip_profile']['dns_address'],
2921 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
2922 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
2923 'dhcp_count': net['ip_profile']['dhcp_count'],
2924 }
2925 db_ip_profiles.append(db_ip_profile)
2926
2927 # print "vnf_net2instance:"
2928 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
2929
2930 # 3. Creating new vm instances in the VIM
2931 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2932 sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
2933 for sce_vnf in sce_vnf_list:
2934 vnf_availability_zones = []
2935 for vm in sce_vnf['vms']:
2936 vm_av = vm.get('availability_zone')
2937 if vm_av and vm_av not in vnf_availability_zones:
2938 vnf_availability_zones.append(vm_av)
2939
2940 # check if there is enough availability zones available at vim level.
2941 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2942 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2943 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request)
2944
2945 if sce_vnf.get("datacenter"):
2946 vim = myvims[ sce_vnf["datacenter"] ]
2947 myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
2948 datacenter_id = sce_vnf["datacenter"]
2949 else:
2950 vim = myvims[ default_datacenter_id ]
2951 myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
2952 datacenter_id = default_datacenter_id
2953 sce_vnf["datacenter_id"] = datacenter_id
2954 i = 0
2955
2956 vnf_uuid = str(uuid4())
2957 uuid_list.append(vnf_uuid)
2958 db_instance_vnf = {
2959 'uuid': vnf_uuid,
2960 'instance_scenario_id': instance_uuid,
2961 'vnf_id': sce_vnf['vnf_id'],
2962 'sce_vnf_id': sce_vnf['uuid'],
2963 'datacenter_id': datacenter_id,
2964 'datacenter_tenant_id': myvim_thread_id,
2965 }
2966 db_instance_vnfs.append(db_instance_vnf)
2967
2968 for vm in sce_vnf['vms']:
2969 myVMDict = {}
2970 myVMDict['name'] = "{}.{}.{}".format(instance_name[:64], sce_vnf['name'][:64], vm["name"][:64])
2971 myVMDict['description'] = myVMDict['name'][0:99]
2972 # if not startvms:
2973 # myVMDict['start'] = "no"
2974 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
2975 #create image at vim in case it not exist
2976 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
2977 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
2978 vm['vim_image_id'] = image_id
2979
2980 # create flavor at vim in case it not exist
2981 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
2982 if flavor_dict['extended']!=None:
2983 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
2984 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
2985
2986 # Obtain information for additional disks
2987 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
2988 if not extended_flavor_dict:
2989 raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
2990 return
2991
2992 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
2993 myVMDict['disks'] = None
2994 extended_info = extended_flavor_dict[0]['extended']
2995 if extended_info != None:
2996 extended_flavor_dict_yaml = yaml.load(extended_info)
2997 if 'disks' in extended_flavor_dict_yaml:
2998 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
2999
3000 vm['vim_flavor_id'] = flavor_id
3001 myVMDict['imageRef'] = vm['vim_image_id']
3002 myVMDict['flavorRef'] = vm['vim_flavor_id']
3003 myVMDict['availability_zone'] = vm.get('availability_zone')
3004 myVMDict['networks'] = []
3005 task_depends_on = []
3006 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3007 db_vm_ifaces = []
3008 for iface in vm['interfaces']:
3009 netDict = {}
3010 if iface['type']=="data":
3011 netDict['type'] = iface['model']
3012 elif "model" in iface and iface["model"]!=None:
3013 netDict['model']=iface['model']
3014 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3015 # is obtained from iterface table model
3016 # discover type of interface looking at flavor
3017 for numa in flavor_dict.get('extended',{}).get('numas',[]):
3018 for flavor_iface in numa.get('interfaces',[]):
3019 if flavor_iface.get('name') == iface['internal_name']:
3020 if flavor_iface['dedicated'] == 'yes':
3021 netDict['type']="PF" #passthrough
3022 elif flavor_iface['dedicated'] == 'no':
3023 netDict['type']="VF" #siov
3024 elif flavor_iface['dedicated'] == 'yes:sriov':
3025 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
3026 netDict["mac_address"] = flavor_iface.get("mac_address")
3027 break;
3028 netDict["use"]=iface['type']
3029 if netDict["use"]=="data" and not netDict.get("type"):
3030 #print "netDict", netDict
3031 #print "iface", iface
3032 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'])
3033 if flavor_dict.get('extended')==None:
3034 raise NfvoException(e_text + "After database migration some information is not available. \
3035 Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
3036 else:
3037 raise NfvoException(e_text, HTTP_Internal_Server_Error)
3038 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
3039 netDict["type"]="virtual"
3040 if "vpci" in iface and iface["vpci"] is not None:
3041 netDict['vpci'] = iface['vpci']
3042 if "mac" in iface and iface["mac"] is not None:
3043 netDict['mac_address'] = iface['mac']
3044 if "port-security" in iface and iface["port-security"] is not None:
3045 netDict['port_security'] = iface['port-security']
3046 if "floating-ip" in iface and iface["floating-ip"] is not None:
3047 netDict['floating_ip'] = iface['floating-ip']
3048 netDict['name'] = iface['internal_name']
3049 if iface['net_id'] is None:
3050 for vnf_iface in sce_vnf["interfaces"]:
3051 # print iface
3052 # print vnf_iface
3053 if vnf_iface['interface_id']==iface['uuid']:
3054 netDict['net_id'] = "TASK-{}".format(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
3055 instance_net_id = sce_net2instance[ vnf_iface['sce_net_id'] ][datacenter_id]
3056 task_depends_on.append(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
3057 break
3058 else:
3059 netDict['net_id'] = "TASK-{}".format(net2task_id[ sce_vnf['uuid'] ][ iface['net_id'] ])
3060 instance_net_id = vnf_net2instance[ sce_vnf['uuid'] ][ iface['net_id'] ]
3061 task_depends_on.append(net2task_id[sce_vnf['uuid'] ][ iface['net_id']])
3062 # skip bridge ifaces not connected to any net
3063 if 'net_id' not in netDict or netDict['net_id']==None:
3064 continue
3065 myVMDict['networks'].append(netDict)
3066 db_vm_iface={
3067 # "uuid"
3068 # 'instance_vm_id': instance_vm_uuid,
3069 "instance_net_id": instance_net_id,
3070 'interface_id': iface['uuid'],
3071 # 'vim_interface_id': ,
3072 'type': 'external' if iface['external_name'] is not None else 'internal',
3073 'ip_address': iface.get('ip_address'),
3074 'floating_ip': int(iface.get('floating-ip', False)),
3075 'port_security': int(iface.get('port-security', True))
3076 }
3077 db_vm_ifaces.append(db_vm_iface)
3078 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3079 # print myVMDict['name']
3080 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3081 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3082 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3083 if vm.get("boot_data"):
3084 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
3085 else:
3086 cloud_config_vm = cloud_config
3087 if myVMDict.get('availability_zone'):
3088 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3089 else:
3090 av_index = None
3091 for vm_index in range(0, vm.get('count', 1)):
3092 vm_index_name = ""
3093 if vm.get('count', 1) > 1:
3094 vm_index_name += "." + chr(97 + vm_index)
3095 task_params = (myVMDict['name']+vm_index_name, myVMDict['description'], myVMDict.get('start', None),
3096 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3097 myVMDict['disks'], av_index, vnf_availability_zones)
3098 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3099 for net in myVMDict['networks']:
3100 if "vim_id" in net:
3101 for iface in vm['interfaces']:
3102 if net["name"]==iface["internal_name"]:
3103 iface["vim_id"]=net["vim_id"]
3104 break
3105 vm_uuid = str(uuid4())
3106 uuid_list.append(vm_uuid)
3107 db_vm = {
3108 "uuid": vm_uuid,
3109 'instance_vnf_id': vnf_uuid,
3110 #TODO delete "vim_vm_id": vm_id,
3111 "vm_id": vm["uuid"],
3112 # "status":
3113 }
3114 db_instance_vms.append(db_vm)
3115
3116 iface_index = 0
3117 for db_vm_iface in db_vm_ifaces:
3118 iface_uuid = str(uuid4())
3119 uuid_list.append(iface_uuid)
3120 db_vm_iface_instance = {
3121 "uuid": iface_uuid,
3122 "instance_vm_id": vm_uuid
3123 }
3124 db_vm_iface_instance.update(db_vm_iface)
3125 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3126 ip = db_vm_iface_instance.get("ip_address")
3127 i = ip.rfind(".")
3128 if i > 0:
3129 try:
3130 i += 1
3131 ip = ip[i:] + str(int(ip[:i]) +1)
3132 db_vm_iface_instance["ip_address"] = ip
3133 except:
3134 db_vm_iface_instance["ip_address"] = None
3135 db_instance_interfaces.append(db_vm_iface_instance)
3136 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
3137 iface_index += 1
3138
3139 db_vim_action = {
3140 "instance_action_id": instance_action_id,
3141 "task_index": task_index,
3142 "datacenter_vim_id": myvim_thread_id,
3143 "action": "CREATE",
3144 "status": "SCHEDULED",
3145 "item": "instance_vms",
3146 "item_id": vm_uuid,
3147 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
3148 default_flow_style=True, width=256)
3149 }
3150 task_index += 1
3151 db_vim_actions.append(db_vim_action)
3152
3153 scenarioDict["datacenter2tenant"] = myvim_threads_id
3154
3155 db_instance_action["number_tasks"] = task_index
3156 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3157 db_instance_scenario['datacenter_id'] = default_datacenter_id
3158 db_tables=[
3159 {"instance_scenarios": db_instance_scenario},
3160 {"instance_vnfs": db_instance_vnfs},
3161 {"instance_nets": db_instance_nets},
3162 {"ip_profiles": db_ip_profiles},
3163 {"instance_vms": db_instance_vms},
3164 {"instance_interfaces": db_instance_interfaces},
3165 {"instance_actions": db_instance_action},
3166 {"vim_actions": db_vim_actions}
3167 ]
3168
3169 logger.debug("create_instance done DB tables: %s",
3170 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3171 mydb.new_rows(db_tables, uuid_list)
3172 for myvim_thread_id in myvim_threads_id.values():
3173 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
3174
3175 returned_instance = mydb.get_instance_scenario(instance_uuid)
3176 returned_instance["action_id"] = instance_action_id
3177 return returned_instance
3178 except (NfvoException, vimconn.vimconnException, db_base_Exception) as e:
3179 message = rollback(mydb, myvims, rollbackList)
3180 if isinstance(e, db_base_Exception):
3181 error_text = "database Exception"
3182 elif isinstance(e, vimconn.vimconnException):
3183 error_text = "VIM Exception"
3184 else:
3185 error_text = "Exception"
3186 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
3187 # logger.error("create_instance: %s", error_text)
3188 raise NfvoException(error_text, e.http_code)
3189
3190
3191 def delete_instance(mydb, tenant_id, instance_id):
3192 # print "Checking that the instance_id exists and getting the instance dictionary"
3193 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
3194 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3195 tenant_id = instanceDict["tenant_id"]
3196 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3197
3198 # 1. Delete from Database
3199 message = mydb.delete_instance_scenario(instance_id, tenant_id)
3200
3201 # 2. delete from VIM
3202 error_msg = ""
3203 myvims = {}
3204 myvim_threads = {}
3205 vimthread_affected = {}
3206
3207 task_index = 0
3208 instance_action_id = get_task_id()
3209 db_vim_actions = []
3210 db_instance_action = {
3211 "uuid": instance_action_id, # same uuid for the instance and the action on create
3212 "tenant_id": tenant_id,
3213 "instance_id": instance_id,
3214 "description": "DELETE",
3215 # "number_tasks": 0 # filled bellow
3216 }
3217
3218 # 2.1 deleting VMs
3219 # vm_fail_list=[]
3220 for sce_vnf in instanceDict['vnfs']:
3221 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3222 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
3223 if datacenter_key not in myvims:
3224 try:
3225 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3226 except NfvoException as e:
3227 logger.error(str(e))
3228 myvim_thread = None
3229 myvim_threads[datacenter_key] = myvim_thread
3230 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
3231 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3232 if len(vims) == 0:
3233 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
3234 sce_vnf["datacenter_tenant_id"]))
3235 myvims[datacenter_key] = None
3236 else:
3237 myvims[datacenter_key] = vims.values()[0]
3238 myvim = myvims[datacenter_key]
3239 myvim_thread = myvim_threads[datacenter_key]
3240 for vm in sce_vnf['vms']:
3241 if not myvim:
3242 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
3243 continue
3244 try:
3245 db_vim_action = {
3246 "instance_action_id": instance_action_id,
3247 "task_index": task_index,
3248 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
3249 "action": "DELETE",
3250 "status": "SCHEDULED",
3251 "item": "instance_vms",
3252 "item_id": vm["uuid"],
3253 "extra": yaml.safe_dump({"params": vm["interfaces"]},
3254 default_flow_style=True, width=256)
3255 }
3256 task_index += 1
3257 db_vim_actions.append(db_vim_action)
3258
3259 except vimconn.vimconnNotFoundException as e:
3260 error_msg+="\n VM VIM_id={} not found at datacenter={}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
3261 logger.warn("VM instance '%s'uuid '%s', VIM id '%s', from VNF_id '%s' not found",
3262 vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'])
3263 except vimconn.vimconnException as e:
3264 error_msg+="\n VM VIM_id={} at datacenter={} Error: {} {}".format(vm['vim_vm_id'], sce_vnf["datacenter_id"], e.http_code, str(e))
3265 logger.error("Error %d deleting VM instance '%s'uuid '%s', VIM_id '%s', from VNF_id '%s': %s",
3266 e.http_code, vm['name'], vm['uuid'], vm['vim_vm_id'], sce_vnf['vnf_id'], str(e))
3267
3268 # 2.2 deleting NETS
3269 # net_fail_list=[]
3270 for net in instanceDict['nets']:
3271 # TODO if not net['created']:
3272 # TODO continue #skip not created nets
3273
3274 vimthread_affected[net["datacenter_tenant_id"]] = None
3275 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3276 if datacenter_key not in myvims:
3277 try:
3278 _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3279 except NfvoException as e:
3280 logger.error(str(e))
3281 myvim_thread = None
3282 myvim_threads[datacenter_key] = myvim_thread
3283 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
3284 datacenter_tenant_id=net["datacenter_tenant_id"])
3285 if len(vims) == 0:
3286 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3287 myvims[datacenter_key] = None
3288 else:
3289 myvims[datacenter_key] = vims.values()[0]
3290 myvim = myvims[datacenter_key]
3291 myvim_thread = myvim_threads[datacenter_key]
3292
3293 if not myvim:
3294 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
3295 continue
3296 try:
3297 db_vim_action = {
3298 "instance_action_id": instance_action_id,
3299 "task_index": task_index,
3300 "datacenter_vim_id": net["datacenter_tenant_id"],
3301 "action": "DELETE",
3302 "status": "SCHEDULED",
3303 "item": "instance_nets",
3304 "item_id": net["uuid"],
3305 "extra": yaml.safe_dump({"params": (net['vim_net_id'], net['sdn_net_id'])},
3306 default_flow_style=True, width=256)
3307 }
3308 task_index += 1
3309 db_vim_actions.append(db_vim_action)
3310
3311 except vimconn.vimconnNotFoundException as e:
3312 error_msg += "\n NET VIM_id={} not found at datacenter={}".format(net['vim_net_id'], net["datacenter_id"])
3313 logger.warn("NET '%s', VIM_id '%s', from VNF_net_id '%s' not found",
3314 net['uuid'], net['vim_net_id'], str(net['vnf_net_id']))
3315 except vimconn.vimconnException as e:
3316 error_msg += "\n NET VIM_id={} at datacenter={} Error: {} {}".format(net['vim_net_id'],
3317 net["datacenter_id"],
3318 e.http_code, str(e))
3319 logger.error("Error %d deleting NET '%s', VIM_id '%s', from VNF_net_id '%s': %s",
3320 e.http_code, net['uuid'], net['vim_net_id'], str(net['vnf_net_id']), str(e))
3321
3322 db_instance_action["number_tasks"] = task_index
3323 db_tables = [
3324 {"instance_actions": db_instance_action},
3325 {"vim_actions": db_vim_actions}
3326 ]
3327
3328 logger.debug("delete_instance done DB tables: %s",
3329 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
3330 mydb.new_rows(db_tables, ())
3331 for myvim_thread_id in vimthread_affected.keys():
3332 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
3333
3334 if len(error_msg) > 0:
3335 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
3336 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
3337 else:
3338 return "action_id={} instance {} deleted".format(instance_action_id, message)
3339
3340
3341 def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
3342 '''Refreshes a scenario instance. It modifies instanceDict'''
3343 '''Returns:
3344 - 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
3345 - error_msg
3346 '''
3347 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
3348 # #print "nfvo.refresh_instance begins"
3349 # #print json.dumps(instanceDict, indent=4)
3350 #
3351 # #print "Getting the VIM URL and the VIM tenant_id"
3352 # myvims={}
3353 #
3354 # # 1. Getting VIM vm and net list
3355 # vms_updated = [] #List of VM instance uuids in openmano that were updated
3356 # vms_notupdated=[]
3357 # vm_list = {}
3358 # for sce_vnf in instanceDict['vnfs']:
3359 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
3360 # if datacenter_key not in vm_list:
3361 # vm_list[datacenter_key] = []
3362 # if datacenter_key not in myvims:
3363 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
3364 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
3365 # if len(vims) == 0:
3366 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
3367 # myvims[datacenter_key] = None
3368 # else:
3369 # myvims[datacenter_key] = vims.values()[0]
3370 # for vm in sce_vnf['vms']:
3371 # vm_list[datacenter_key].append(vm['vim_vm_id'])
3372 # vms_notupdated.append(vm["uuid"])
3373 #
3374 # nets_updated = [] #List of VM instance uuids in openmano that were updated
3375 # nets_notupdated=[]
3376 # net_list = {}
3377 # for net in instanceDict['nets']:
3378 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
3379 # if datacenter_key not in net_list:
3380 # net_list[datacenter_key] = []
3381 # if datacenter_key not in myvims:
3382 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
3383 # datacenter_tenant_id=net["datacenter_tenant_id"])
3384 # if len(vims) == 0:
3385 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
3386 # myvims[datacenter_key] = None
3387 # else:
3388 # myvims[datacenter_key] = vims.values()[0]
3389 #
3390 # net_list[datacenter_key].append(net['vim_net_id'])
3391 # nets_notupdated.append(net["uuid"])
3392 #
3393 # # 1. Getting the status of all VMs
3394 # vm_dict={}
3395 # for datacenter_key in myvims:
3396 # if not vm_list.get(datacenter_key):
3397 # continue
3398 # failed = True
3399 # failed_message=""
3400 # if not myvims[datacenter_key]:
3401 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3402 # else:
3403 # try:
3404 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
3405 # failed = False
3406 # except vimconn.vimconnException as e:
3407 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3408 # failed_message = str(e)
3409 # if failed:
3410 # for vm in vm_list[datacenter_key]:
3411 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3412 #
3413 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
3414 # for sce_vnf in instanceDict['vnfs']:
3415 # for vm in sce_vnf['vms']:
3416 # vm_id = vm['vim_vm_id']
3417 # interfaces = vm_dict[vm_id].pop('interfaces', [])
3418 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
3419 # has_mgmt_iface = False
3420 # for iface in vm["interfaces"]:
3421 # if iface["type"]=="mgmt":
3422 # has_mgmt_iface = True
3423 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
3424 # vm_dict[vm_id]['status'] = "ACTIVE"
3425 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
3426 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
3427 # 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'):
3428 # vm['status'] = vm_dict[vm_id]['status']
3429 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
3430 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
3431 # # 2.1. Update in openmano DB the VMs whose status changed
3432 # try:
3433 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
3434 # vms_notupdated.remove(vm["uuid"])
3435 # if updates>0:
3436 # vms_updated.append(vm["uuid"])
3437 # except db_base_Exception as e:
3438 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3439 # # 2.2. Update in openmano DB the interface VMs
3440 # for interface in interfaces:
3441 # #translate from vim_net_id to instance_net_id
3442 # network_id_list=[]
3443 # for net in instanceDict['nets']:
3444 # if net["vim_net_id"] == interface["vim_net_id"]:
3445 # network_id_list.append(net["uuid"])
3446 # if not network_id_list:
3447 # continue
3448 # del interface["vim_net_id"]
3449 # try:
3450 # for network_id in network_id_list:
3451 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
3452 # except db_base_Exception as e:
3453 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
3454 #
3455 # # 3. Getting the status of all nets
3456 # net_dict = {}
3457 # for datacenter_key in myvims:
3458 # if not net_list.get(datacenter_key):
3459 # continue
3460 # failed = True
3461 # failed_message = ""
3462 # if not myvims[datacenter_key]:
3463 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
3464 # else:
3465 # try:
3466 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
3467 # failed = False
3468 # except vimconn.vimconnException as e:
3469 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
3470 # failed_message = str(e)
3471 # if failed:
3472 # for net in net_list[datacenter_key]:
3473 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
3474 #
3475 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
3476 # # TODO: update nets inside a vnf
3477 # for net in instanceDict['nets']:
3478 # net_id = net['vim_net_id']
3479 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
3480 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
3481 # 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'):
3482 # net['status'] = net_dict[net_id]['status']
3483 # net['error_msg'] = net_dict[net_id].get('error_msg')
3484 # net['vim_info'] = net_dict[net_id].get('vim_info')
3485 # # 5.1. Update in openmano DB the nets whose status changed
3486 # try:
3487 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
3488 # nets_notupdated.remove(net["uuid"])
3489 # if updated>0:
3490 # nets_updated.append(net["uuid"])
3491 # except db_base_Exception as e:
3492 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
3493 #
3494 # # Returns appropriate output
3495 # #print "nfvo.refresh_instance finishes"
3496 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
3497 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
3498 instance_id = instanceDict['uuid']
3499 # if len(vms_notupdated)+len(nets_notupdated)>0:
3500 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
3501 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
3502
3503 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
3504
3505 def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
3506 #print "Checking that the instance_id exists and getting the instance dictionary"
3507 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
3508 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3509
3510 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3511 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
3512 if len(vims) == 0:
3513 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found)
3514 myvim = vims.values()[0]
3515
3516 if action_dict.get("create-vdu"):
3517 for vdu in action_dict["create-vdu"]:
3518 vdu_id = vdu.get("vdu-id")
3519 vdu_count = vdu.get("count", 1)
3520 # get from database TODO
3521 # insert tasks TODO
3522 pass
3523
3524 input_vnfs = action_dict.pop("vnfs", [])
3525 input_vms = action_dict.pop("vms", [])
3526 action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
3527 vm_result = {}
3528 vm_error = 0
3529 vm_ok = 0
3530 for sce_vnf in instanceDict['vnfs']:
3531 for vm in sce_vnf['vms']:
3532 if not action_over_all:
3533 if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
3534 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
3535 continue
3536 try:
3537 if "add_public_key" in action_dict:
3538 mgmt_access = {}
3539 if sce_vnf.get('mgmt_access'):
3540 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
3541 ssh_access = mgmt_access['config-access']['ssh-access']
3542 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
3543 try:
3544 if ssh_access['required'] and ssh_access['default-user']:
3545 if 'ip_address' in vm:
3546 mgmt_ip = vm['ip_address'].split(';')
3547 password = mgmt_access['config-access'].get('password')
3548 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
3549 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
3550 action_dict['add_public_key'],
3551 password=password, ro_key=priv_RO_key)
3552 else:
3553 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3554 HTTP_Internal_Server_Error)
3555 except KeyError:
3556 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3557 HTTP_Internal_Server_Error)
3558 else:
3559 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
3560 HTTP_Internal_Server_Error)
3561 else:
3562 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
3563 if "console" in action_dict:
3564 if not global_config["http_console_proxy"]:
3565 vm_result[ vm['uuid'] ] = {"vim_result": 200,
3566 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3567 protocol=data["protocol"],
3568 ip = data["server"],
3569 port = data["port"],
3570 suffix = data["suffix"]),
3571 "name":vm['name']
3572 }
3573 vm_ok +=1
3574 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
3575 vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
3576 "description": "this console is only reachable by local interface",
3577 "name":vm['name']
3578 }
3579 vm_error+=1
3580 else:
3581 #print "console data", data
3582 try:
3583 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
3584 vm_result[ vm['uuid'] ] = {"vim_result": 200,
3585 "description": "{protocol}//{ip}:{port}/{suffix}".format(
3586 protocol=data["protocol"],
3587 ip = global_config["http_console_host"],
3588 port = console_thread.port,
3589 suffix = data["suffix"]),
3590 "name":vm['name']
3591 }
3592 vm_ok +=1
3593 except NfvoException as e:
3594 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
3595 vm_error+=1
3596
3597 else:
3598 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
3599 vm_ok +=1
3600 except vimconn.vimconnException as e:
3601 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
3602 vm_error+=1
3603
3604 if vm_ok==0: #all goes wrong
3605 return vm_result
3606 else:
3607 return vm_result
3608
3609 def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
3610 filter={}
3611 if nfvo_tenant and nfvo_tenant != "any":
3612 filter["tenant_id"] = nfvo_tenant
3613 if instance_id and instance_id != "any":
3614 filter["instance_id"] = instance_id
3615 if action_id:
3616 filter["uuid"] = action_id
3617 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
3618 if not rows and action_id:
3619 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found)
3620 return {"ations": rows}
3621
3622
3623 def create_or_use_console_proxy_thread(console_server, console_port):
3624 #look for a non-used port
3625 console_thread_key = console_server + ":" + str(console_port)
3626 if console_thread_key in global_config["console_thread"]:
3627 #global_config["console_thread"][console_thread_key].start_timeout()
3628 return global_config["console_thread"][console_thread_key]
3629
3630 for port in global_config["console_port_iterator"]():
3631 #print "create_or_use_console_proxy_thread() port:", port
3632 if port in global_config["console_ports"]:
3633 continue
3634 try:
3635 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
3636 clithread.start()
3637 global_config["console_thread"][console_thread_key] = clithread
3638 global_config["console_ports"][port] = console_thread_key
3639 return clithread
3640 except cli.ConsoleProxyExceptionPortUsed as e:
3641 #port used, try with onoher
3642 continue
3643 except cli.ConsoleProxyException as e:
3644 raise NfvoException(str(e), HTTP_Bad_Request)
3645 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict)
3646
3647
3648 def check_tenant(mydb, tenant_id):
3649 '''check that tenant exists at database'''
3650 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
3651 if not tenant:
3652 raise NfvoException("tenant '{}' not found".format(tenant_id), HTTP_Not_Found)
3653 return
3654
3655 def new_tenant(mydb, tenant_dict):
3656
3657 tenant_uuid = str(uuid4())
3658 tenant_dict['uuid'] = tenant_uuid
3659 try:
3660 pub_key, priv_key = create_RO_keypair(tenant_uuid)
3661 tenant_dict['RO_pub_key'] = pub_key
3662 tenant_dict['encrypted_RO_priv_key'] = priv_key
3663 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
3664 except db_base_Exception as e:
3665 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), HTTP_Internal_Server_Error)
3666 return tenant_uuid
3667
3668 def delete_tenant(mydb, tenant):
3669 #get nfvo_tenant info
3670
3671 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
3672 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
3673 return tenant_dict['uuid'] + " " + tenant_dict["name"]
3674
3675
3676 def new_datacenter(mydb, datacenter_descriptor):
3677 if "config" in datacenter_descriptor:
3678 datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
3679 #Check that datacenter-type is correct
3680 datacenter_type = datacenter_descriptor.get("type", "openvim");
3681 module_info = None
3682 try:
3683 module = "vimconn_" + datacenter_type
3684 pkg = __import__("osm_ro." + module)
3685 vim_conn = getattr(pkg, module)
3686 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
3687 except (IOError, ImportError):
3688 # if module_info and module_info[0]:
3689 # file.close(module_info[0])
3690 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request)
3691
3692 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
3693 return datacenter_id
3694
3695
3696 def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
3697 # obtain data, check that only one exist
3698 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
3699
3700 # edit data
3701 datacenter_id = datacenter['uuid']
3702 where={'uuid': datacenter['uuid']}
3703 remove_port_mapping = False
3704 if "config" in datacenter_descriptor:
3705 if datacenter_descriptor['config'] != None:
3706 try:
3707 new_config_dict = datacenter_descriptor["config"]
3708 #delete null fields
3709 to_delete=[]
3710 for k in new_config_dict:
3711 if new_config_dict[k] == None:
3712 to_delete.append(k)
3713 if k == 'sdn-controller':
3714 remove_port_mapping = True
3715
3716 config_text = datacenter.get("config")
3717 if not config_text:
3718 config_text = '{}'
3719 config_dict = yaml.load(config_text)
3720 config_dict.update(new_config_dict)
3721 #delete null fields
3722 for k in to_delete:
3723 del config_dict[k]
3724 except Exception as e:
3725 raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request)
3726 if config_dict:
3727 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
3728 else:
3729 datacenter_descriptor["config"] = None
3730 if remove_port_mapping:
3731 try:
3732 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
3733 except ovimException as e:
3734 logger.error("Error deleting datacenter-port-mapping " + str(e))
3735
3736 mydb.update_rows('datacenters', datacenter_descriptor, where)
3737 return datacenter_id
3738
3739
3740 def delete_datacenter(mydb, datacenter):
3741 #get nfvo_tenant info
3742 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
3743 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
3744 try:
3745 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
3746 except ovimException as e:
3747 logger.error("Error deleting datacenter-port-mapping " + str(e))
3748 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
3749
3750
3751 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):
3752 #get datacenter info
3753 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, vim_passwd=vim_password)
3754 datacenter_name = myvim["name"]
3755
3756 create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False
3757
3758 # get nfvo_tenant info
3759 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
3760 if vim_tenant_name==None:
3761 vim_tenant_name=tenant_dict['name']
3762
3763 #check that this association does not exist before
3764 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
3765 tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
3766 if len(tenants_datacenters)>0:
3767 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
3768
3769 vim_tenant_id_exist_atdb=False
3770 if not create_vim_tenant:
3771 where_={"datacenter_id": datacenter_id}
3772 if vim_tenant_id!=None:
3773 where_["vim_tenant_id"] = vim_tenant_id
3774 if vim_tenant_name!=None:
3775 where_["vim_tenant_name"] = vim_tenant_name
3776 #check if vim_tenant_id is already at database
3777 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
3778 if len(datacenter_tenants_dict)>=1:
3779 datacenter_tenants_dict = datacenter_tenants_dict[0]
3780 vim_tenant_id_exist_atdb=True
3781 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
3782 else: #result=0
3783 datacenter_tenants_dict = {}
3784 #insert at table datacenter_tenants
3785 else: #if vim_tenant_id==None:
3786 #create tenant at VIM if not provided
3787 try:
3788 vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
3789 except vimconn.vimconnException as e:
3790 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), HTTP_Internal_Server_Error)
3791 datacenter_tenants_dict = {}
3792 datacenter_tenants_dict["created"]="true"
3793
3794 #fill datacenter_tenants table
3795 if not vim_tenant_id_exist_atdb:
3796 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant_id
3797 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
3798 datacenter_tenants_dict["user"] = vim_username
3799 datacenter_tenants_dict["passwd"] = vim_password
3800 datacenter_tenants_dict["datacenter_id"] = datacenter_id
3801 if config:
3802 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
3803 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
3804 datacenter_tenants_dict["uuid"] = id_
3805
3806 #fill tenants_datacenters table
3807 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
3808 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
3809 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
3810 # create thread
3811 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data
3812 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
3813 new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id,
3814 db=db, db_lock=db_lock, ovim=ovim)
3815 new_thread.start()
3816 thread_id = datacenter_tenants_dict["uuid"]
3817 vim_threads["running"][thread_id] = new_thread
3818 return datacenter_id
3819
3820
3821 def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None,
3822 vim_username=None, vim_password=None, config=None):
3823 #Obtain the data of this datacenter_tenant_id
3824 vim_data = mydb.get_rows(
3825 SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
3826 "datacenter_tenants.passwd", "datacenter_tenants.config"),
3827 FROM="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
3828 WHERE={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant,
3829 "tenants_datacenters.datacenter_id": datacenter_id})
3830
3831 logger.debug(str(vim_data))
3832 if len(vim_data) < 1:
3833 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id, nfvo_tenant), HTTP_Conflict)
3834
3835 v = vim_data[0]
3836 if v['config']:
3837 v['config'] = yaml.load(v['config'])
3838
3839 if vim_tenant_id:
3840 v['vim_tenant_id'] = vim_tenant_id
3841 if vim_tenant_name:
3842 v['vim_tenant_name'] = vim_tenant_name
3843 if vim_username:
3844 v['user'] = vim_username
3845 if vim_password:
3846 v['passwd'] = vim_password
3847 if config:
3848 if not v['config']:
3849 v['config'] = {}
3850 v['config'].update(config)
3851
3852 logger.debug(str(v))
3853 deassociate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'])
3854 associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=v['vim_tenant_id'], vim_tenant_name=v['vim_tenant_name'],
3855 vim_username=v['user'], vim_password=v['passwd'], config=v['config'])
3856
3857 return datacenter_id
3858
3859 def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
3860 #get datacenter info
3861 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3862
3863 #get nfvo_tenant info
3864 if not tenant_id or tenant_id=="any":
3865 tenant_uuid = None
3866 else:
3867 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
3868 tenant_uuid = tenant_dict['uuid']
3869
3870 #check that this association exist before
3871 tenants_datacenter_dict={"datacenter_id":datacenter_id }
3872 if tenant_uuid:
3873 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
3874 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
3875 if len(tenant_datacenter_list)==0 and tenant_uuid:
3876 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Not_Found)
3877
3878 #delete this association
3879 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
3880
3881 #get vim_tenant info and deletes
3882 warning=''
3883 for tenant_datacenter_item in tenant_datacenter_list:
3884 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
3885 #try to delete vim:tenant
3886 try:
3887 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
3888 if vim_tenant_dict['created']=='true':
3889 #delete tenant at VIM if created by NFVO
3890 try:
3891 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
3892 except vimconn.vimconnException as e:
3893 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
3894 logger.warn(warning)
3895 except db_base_Exception as e:
3896 logger.error("Cannot delete datacenter_tenants " + str(e))
3897 pass # the error will be caused because dependencies, vim_tenant can not be deleted
3898 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
3899 thread = vim_threads["running"][thread_id]
3900 thread.insert_task("exit")
3901 vim_threads["deleting"][thread_id] = thread
3902 return "datacenter {} detached. {}".format(datacenter_id, warning)
3903
3904
3905 def datacenter_action(mydb, tenant_id, datacenter, action_dict):
3906 #DEPRECATED
3907 #get datacenter info
3908 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3909
3910 if 'net-update' in action_dict:
3911 try:
3912 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
3913 #print content
3914 except vimconn.vimconnException as e:
3915 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
3916 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3917 #update nets Change from VIM format to NFVO format
3918 net_list=[]
3919 for net in nets:
3920 net_nfvo={'datacenter_id': datacenter_id}
3921 net_nfvo['name'] = net['name']
3922 #net_nfvo['description']= net['name']
3923 net_nfvo['vim_net_id'] = net['id']
3924 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3925 net_nfvo['shared'] = net['shared']
3926 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3927 net_list.append(net_nfvo)
3928 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
3929 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
3930 return inserted
3931 elif 'net-edit' in action_dict:
3932 net = action_dict['net-edit'].pop('net')
3933 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
3934 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
3935 WHERE={'datacenter_id':datacenter_id, what: net})
3936 return result
3937 elif 'net-delete' in action_dict:
3938 net = action_dict['net-deelte'].get('net')
3939 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
3940 result = mydb.delete_row(FROM='datacenter_nets',
3941 WHERE={'datacenter_id':datacenter_id, what: net})
3942 return result
3943
3944 else:
3945 raise NfvoException("Unknown action " + str(action_dict), HTTP_Bad_Request)
3946
3947
3948 def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
3949 #get datacenter info
3950 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3951
3952 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
3953 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
3954 WHERE={'datacenter_id':datacenter_id, what: netmap})
3955 return result
3956
3957
3958 def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
3959 #get datacenter info
3960 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3961 filter_dict={}
3962 if action_dict:
3963 action_dict = action_dict["netmap"]
3964 if 'vim_id' in action_dict:
3965 filter_dict["id"] = action_dict['vim_id']
3966 if 'vim_name' in action_dict:
3967 filter_dict["name"] = action_dict['vim_name']
3968 else:
3969 filter_dict["shared"] = True
3970
3971 try:
3972 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
3973 except vimconn.vimconnException as e:
3974 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
3975 raise NfvoException(str(e), HTTP_Internal_Server_Error)
3976 if len(vim_nets)>1 and action_dict:
3977 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict)
3978 elif len(vim_nets)==0: # and action_dict:
3979 raise NfvoException("Not found a network at VIM with " + str(filter_dict), HTTP_Not_Found)
3980 net_list=[]
3981 for net in vim_nets:
3982 net_nfvo={'datacenter_id': datacenter_id}
3983 if action_dict and "name" in action_dict:
3984 net_nfvo['name'] = action_dict['name']
3985 else:
3986 net_nfvo['name'] = net['name']
3987 #net_nfvo['description']= net['name']
3988 net_nfvo['vim_net_id'] = net['id']
3989 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
3990 net_nfvo['shared'] = net['shared']
3991 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
3992 try:
3993 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
3994 net_nfvo["status"] = "OK"
3995 net_nfvo["uuid"] = net_id
3996 except db_base_Exception as e:
3997 if action_dict:
3998 raise
3999 else:
4000 net_nfvo["status"] = "FAIL: " + str(e)
4001 net_list.append(net_nfvo)
4002 return net_list
4003
4004 def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
4005 # obtain all network data
4006 try:
4007 if utils.check_valid_uuid(network_id):
4008 filter_dict = {"id": network_id}
4009 else:
4010 filter_dict = {"name": network_id}
4011
4012 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4013 network = myvim.get_network_list(filter_dict=filter_dict)
4014 except vimconn.vimconnException as e:
4015 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e)), e.http_code)
4016
4017 # ensure the network is defined
4018 if len(network) == 0:
4019 raise NfvoException("Network {} is not present in the system".format(network_id),
4020 HTTP_Bad_Request)
4021
4022 # ensure there is only one network with the provided name
4023 if len(network) > 1:
4024 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request)
4025
4026 # ensure it is a dataplane network
4027 if network[0]['type'] != 'data':
4028 return None
4029
4030 # ensure we use the id
4031 network_id = network[0]['id']
4032
4033 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
4034 # and with instance_scenario_id==NULL
4035 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
4036 search_dict = {'vim_net_id': network_id}
4037
4038 try:
4039 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
4040 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
4041 except db_base_Exception as e:
4042 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
4043 network_id) + str(e), HTTP_Internal_Server_Error)
4044
4045 sdn_net_counter = 0
4046 for net in result:
4047 if net['sdn_net_id'] != None:
4048 sdn_net_counter+=1
4049 sdn_net_id = net['sdn_net_id']
4050
4051 if sdn_net_counter == 0:
4052 return None
4053 elif sdn_net_counter == 1:
4054 return sdn_net_id
4055 else:
4056 raise NfvoException("More than one SDN network is associated to vim network {}".format(
4057 network_id), HTTP_Internal_Server_Error)
4058
4059 def get_sdn_controller_id(mydb, datacenter):
4060 # Obtain sdn controller id
4061 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
4062 if not config:
4063 return None
4064
4065 return yaml.load(config).get('sdn-controller')
4066
4067 def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
4068 try:
4069 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4070 if not sdn_network_id:
4071 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error)
4072
4073 #Obtain sdn controller id
4074 controller_id = get_sdn_controller_id(mydb, datacenter)
4075 if not controller_id:
4076 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error)
4077
4078 #Obtain sdn controller info
4079 sdn_controller = ovim.show_of_controller(controller_id)
4080
4081 port_data = {
4082 'name': 'external_port',
4083 'net_id': sdn_network_id,
4084 'ofc_id': controller_id,
4085 'switch_dpid': sdn_controller['dpid'],
4086 'switch_port': descriptor['port']
4087 }
4088
4089 if 'vlan' in descriptor:
4090 port_data['vlan'] = descriptor['vlan']
4091 if 'mac' in descriptor:
4092 port_data['mac'] = descriptor['mac']
4093
4094 result = ovim.new_port(port_data)
4095 except ovimException as e:
4096 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
4097 sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error)
4098 except db_base_Exception as e:
4099 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
4100 network_id) + str(e), HTTP_Internal_Server_Error)
4101
4102 return 'Port uuid: '+ result
4103
4104 def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
4105 if port_id:
4106 filter = {'uuid': port_id}
4107 else:
4108 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
4109 if not sdn_network_id:
4110 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
4111 HTTP_Internal_Server_Error)
4112 #in case no port_id is specified only ports marked as 'external_port' will be detached
4113 filter = {'name': 'external_port', 'net_id': sdn_network_id}
4114
4115 try:
4116 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
4117 except ovimException as e:
4118 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4119 HTTP_Internal_Server_Error)
4120
4121 if len(port_list) == 0:
4122 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
4123 HTTP_Bad_Request)
4124
4125 port_uuid_list = []
4126 for port in port_list:
4127 try:
4128 port_uuid_list.append(port['uuid'])
4129 ovim.delete_port(port['uuid'])
4130 except ovimException as e:
4131 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error)
4132
4133 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
4134
4135 def vim_action_get(mydb, tenant_id, datacenter, item, name):
4136 #get datacenter info
4137 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4138 filter_dict={}
4139 if name:
4140 if utils.check_valid_uuid(name):
4141 filter_dict["id"] = name
4142 else:
4143 filter_dict["name"] = name
4144 try:
4145 if item=="networks":
4146 #filter_dict['tenant_id'] = myvim['tenant_id']
4147 content = myvim.get_network_list(filter_dict=filter_dict)
4148
4149 if len(content) == 0:
4150 raise NfvoException("Network {} is not present in the system. ".format(name),
4151 HTTP_Bad_Request)
4152
4153 #Update the networks with the attached ports
4154 for net in content:
4155 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
4156 if sdn_network_id != None:
4157 try:
4158 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
4159 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
4160 except ovimException as e:
4161 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error)
4162 #Remove field name and if port name is external_port save it as 'type'
4163 for port in port_list:
4164 if port['name'] == 'external_port':
4165 port['type'] = "External"
4166 del port['name']
4167 net['sdn_network_id'] = sdn_network_id
4168 net['sdn_attached_ports'] = port_list
4169
4170 elif item=="tenants":
4171 content = myvim.get_tenant_list(filter_dict=filter_dict)
4172 elif item == "images":
4173
4174 content = myvim.get_image_list(filter_dict=filter_dict)
4175 else:
4176 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
4177 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
4178 if name and len(content)==1:
4179 return {item[:-1]: content[0]}
4180 elif name and len(content)==0:
4181 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
4182 datacenter)
4183 else:
4184 return {item: content}
4185 except vimconn.vimconnException as e:
4186 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
4187 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
4188
4189
4190 def vim_action_delete(mydb, tenant_id, datacenter, item, name):
4191 #get datacenter info
4192 if tenant_id == "any":
4193 tenant_id=None
4194
4195 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4196 #get uuid name
4197 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
4198 logger.debug("vim_action_delete vim response: " + str(content))
4199 items = content.values()[0]
4200 if type(items)==list and len(items)==0:
4201 raise NfvoException("Not found " + item, HTTP_Not_Found)
4202 elif type(items)==list and len(items)>1:
4203 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), HTTP_Not_Found)
4204 else: # it is a dict
4205 item_id = items["id"]
4206 item_name = str(items.get("name"))
4207
4208 try:
4209 if item=="networks":
4210 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
4211 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
4212 if sdn_network_id != None:
4213 #Delete any port attachment to this network
4214 try:
4215 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
4216 except ovimException as e:
4217 raise NfvoException(
4218 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
4219 HTTP_Internal_Server_Error)
4220
4221 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
4222 for port in port_list:
4223 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
4224
4225 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
4226 try:
4227 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
4228 except db_base_Exception as e:
4229 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
4230 str(e), HTTP_Internal_Server_Error)
4231
4232 #Delete the SDN network
4233 try:
4234 ovim.delete_network(sdn_network_id)
4235 except ovimException as e:
4236 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
4237 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
4238 HTTP_Internal_Server_Error)
4239
4240 content = myvim.delete_network(item_id)
4241 elif item=="tenants":
4242 content = myvim.delete_tenant(item_id)
4243 elif item == "images":
4244 content = myvim.delete_image(item_id)
4245 else:
4246 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
4247 except vimconn.vimconnException as e:
4248 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
4249 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
4250
4251 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
4252
4253
4254 def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
4255 #get datacenter info
4256 logger.debug("vim_action_create descriptor %s", str(descriptor))
4257 if tenant_id == "any":
4258 tenant_id=None
4259 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
4260 try:
4261 if item=="networks":
4262 net = descriptor["network"]
4263 net_name = net.pop("name")
4264 net_type = net.pop("type", "bridge")
4265 net_public = net.pop("shared", False)
4266 net_ipprofile = net.pop("ip_profile", None)
4267 net_vlan = net.pop("vlan", None)
4268 content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
4269
4270 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
4271 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
4272 try:
4273 sdn_network = {}
4274 sdn_network['vlan'] = net_vlan
4275 sdn_network['type'] = net_type
4276 sdn_network['name'] = net_name
4277 ovim_content = ovim.new_network(sdn_network)
4278 except ovimException as e:
4279 self.logger.error("ovimException creating SDN network={} ".format(
4280 sdn_network) + str(e), exc_info=True)
4281 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
4282 HTTP_Internal_Server_Error)
4283
4284 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
4285 # use instance_scenario_id=None to distinguish from real instaces of nets
4286 correspondence = {'instance_scenario_id': None, 'sdn_net_id': ovim_content, 'vim_net_id': content}
4287 #obtain datacenter_tenant_id
4288 correspondence['datacenter_tenant_id'] = mydb.get_rows(SELECT=('uuid',), FROM='datacenter_tenants', WHERE={'datacenter_id': datacenter})[0]['uuid']
4289
4290 try:
4291 mydb.new_row('instance_nets', correspondence, add_uuid=True)
4292 except db_base_Exception as e:
4293 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
4294 str(e), HTTP_Internal_Server_Error)
4295 elif item=="tenants":
4296 tenant = descriptor["tenant"]
4297 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
4298 else:
4299 raise NfvoException(item + "?", HTTP_Method_Not_Allowed)
4300 except vimconn.vimconnException as e:
4301 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
4302
4303 return vim_action_get(mydb, tenant_id, datacenter, item, content)
4304
4305 def sdn_controller_create(mydb, tenant_id, sdn_controller):
4306 data = ovim.new_of_controller(sdn_controller)
4307 logger.debug('New SDN controller created with uuid {}'.format(data))
4308 return data
4309
4310 def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
4311 data = ovim.edit_of_controller(controller_id, sdn_controller)
4312 msg = 'SDN controller {} updated'.format(data)
4313 logger.debug(msg)
4314 return msg
4315
4316 def sdn_controller_list(mydb, tenant_id, controller_id=None):
4317 if controller_id == None:
4318 data = ovim.get_of_controllers()
4319 else:
4320 data = ovim.show_of_controller(controller_id)
4321
4322 msg = 'SDN controller list:\n {}'.format(data)
4323 logger.debug(msg)
4324 return data
4325
4326 def sdn_controller_delete(mydb, tenant_id, controller_id):
4327 select_ = ('uuid', 'config')
4328 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
4329 for datacenter in datacenters:
4330 if datacenter['config']:
4331 config = yaml.load(datacenter['config'])
4332 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
4333 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), HTTP_Conflict)
4334
4335 data = ovim.delete_of_controller(controller_id)
4336 msg = 'SDN controller {} deleted'.format(data)
4337 logger.debug(msg)
4338 return msg
4339
4340 def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
4341 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
4342 if len(controller) < 1:
4343 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), HTTP_Not_Found)
4344
4345 try:
4346 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
4347 except:
4348 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), HTTP_Bad_Request)
4349
4350 sdn_controller = ovim.show_of_controller(sdn_controller_id)
4351 switch_dpid = sdn_controller["dpid"]
4352
4353 maps = list()
4354 for compute_node in sdn_port_mapping:
4355 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
4356 element = dict()
4357 element["compute_node"] = compute_node["compute_node"]
4358 for port in compute_node["ports"]:
4359 element["pci"] = port.get("pci")
4360 element["switch_port"] = port.get("switch_port")
4361 element["switch_mac"] = port.get("switch_mac")
4362 if not element["pci"] or not (element["switch_port"] or element["switch_mac"]):
4363 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
4364 " or 'switch_mac'", HTTP_Bad_Request)
4365 maps.append(dict(element))
4366
4367 return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)
4368
4369 def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
4370 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
4371
4372 result = {
4373 "sdn-controller": None,
4374 "datacenter-id": datacenter_id,
4375 "dpid": None,
4376 "ports_mapping": list()
4377 }
4378
4379 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
4380 if datacenter['config']:
4381 config = yaml.load(datacenter['config'])
4382 if 'sdn-controller' in config:
4383 controller_id = config['sdn-controller']
4384 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
4385 result["sdn-controller"] = controller_id
4386 result["dpid"] = sdn_controller["dpid"]
4387
4388 if result["sdn-controller"] == None:
4389 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request)
4390 if result["dpid"] == None:
4391 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
4392 HTTP_Internal_Server_Error)
4393
4394 if len(maps) == 0:
4395 return result
4396
4397 ports_correspondence_dict = dict()
4398 for link in maps:
4399 if result["sdn-controller"] != link["ofc_id"]:
4400 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error)
4401 if result["dpid"] != link["switch_dpid"]:
4402 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error)
4403 element = dict()
4404 element["pci"] = link["pci"]
4405 if link["switch_port"]:
4406 element["switch_port"] = link["switch_port"]
4407 if link["switch_mac"]:
4408 element["switch_mac"] = link["switch_mac"]
4409
4410 if not link["compute_node"] in ports_correspondence_dict:
4411 content = dict()
4412 content["compute_node"] = link["compute_node"]
4413 content["ports"] = list()
4414 ports_correspondence_dict[link["compute_node"]] = content
4415
4416 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
4417
4418 for key in sorted(ports_correspondence_dict):
4419 result["ports_mapping"].append(ports_correspondence_dict[key])
4420
4421 return result
4422
4423 def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
4424 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
4425
4426 def create_RO_keypair(tenant_id):
4427 """
4428 Creates a public / private keys for a RO tenant and returns their values
4429 Params:
4430 tenant_id: ID of the tenant
4431 Return:
4432 public_key: Public key for the RO tenant
4433 private_key: Encrypted private key for RO tenant
4434 """
4435
4436 bits = 2048
4437 key = RSA.generate(bits)
4438 try:
4439 public_key = key.publickey().exportKey('OpenSSH')
4440 if isinstance(public_key, ValueError):
4441 raise NfvoException("Unable to create public key: {}".format(public_key), HTTP_Internal_Server_Error)
4442 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
4443 except (ValueError, NameError) as e:
4444 raise NfvoException("Unable to create private key: {}".format(e), HTTP_Internal_Server_Error)
4445 return public_key, private_key
4446
4447 def decrypt_key (key, tenant_id):
4448 """
4449 Decrypts an encrypted RSA key
4450 Params:
4451 key: Private key to be decrypted
4452 tenant_id: ID of the tenant
4453 Return:
4454 unencrypted_key: Unencrypted private key for RO tenant
4455 """
4456 try:
4457 key = RSA.importKey(key,tenant_id)
4458 unencrypted_key = key.exportKey('PEM')
4459 if isinstance(unencrypted_key, ValueError):
4460 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), HTTP_Internal_Server_Error)
4461 except ValueError as e:
4462 raise NfvoException("Unable to decrypt the private key: {}".format(e), HTTP_Internal_Server_Error)
4463 return unencrypted_key