Merge branch WIM: Connector using IETF L2VPN Service model
[osm/RO.git] / osm_ro / nfvo.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefonica Investigacion 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 from utils import deprecated
35 import vim_thread
36 import console_proxy_thread as cli
37 import vimconn
38 import logging
39 import collections
40 import math
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 copy import deepcopy
55
56
57 # WIM
58 import wim.wimconn as wimconn
59 import wim.wim_thread as wim_thread
60 from .http_tools import errors as httperrors
61 from .wim.engine import WimEngine
62 from .wim.persistence import WimPersistence
63 from copy import deepcopy
64 from pprint import pformat
65 #
66
67 global global_config
68 global vimconn_imported
69 # WIM
70 global wim_engine
71 wim_engine = None
72 global wimconn_imported
73 #
74 global logger
75 global default_volume_size
76 default_volume_size = '5' #size in GB
77 global ovim
78 ovim = None
79 global_config = None
80
81 vimconn_imported = {} # dictionary with VIM type as key, loaded module as value
82 vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
83 vim_persistent_info = {}
84 # WIM
85 wimconn_imported = {} # dictionary with WIM type as key, loaded module as value
86 wim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
87 wim_persistent_info = {}
88 #
89
90 logger = logging.getLogger('openmano.nfvo')
91 task_lock = Lock()
92 last_task_id = 0.0
93 db = None
94 db_lock = Lock()
95
96
97 class NfvoException(httperrors.HttpMappedError):
98 """Common Class for NFVO errors"""
99
100
101 def get_task_id():
102 global last_task_id
103 task_id = t.time()
104 if task_id <= last_task_id:
105 task_id = last_task_id + 0.000001
106 last_task_id = task_id
107 return "ACTION-{:.6f}".format(task_id)
108 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
109
110
111 def new_task(name, params, depends=None):
112 """Deprected!!!"""
113 task_id = get_task_id()
114 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
115 if depends:
116 task["depends"] = depends
117 return task
118
119
120 def is_task_id(id):
121 return True if id[:5] == "TASK-" else False
122
123
124 def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id):
125 name = datacenter_name[:16]
126 if name not in vim_threads["names"]:
127 vim_threads["names"].append(name)
128 return name
129 name = datacenter_name[:16] + "." + tenant_name[:16]
130 if name not in vim_threads["names"]:
131 vim_threads["names"].append(name)
132 return name
133 name = datacenter_id + "-" + tenant_id
134 vim_threads["names"].append(name)
135 return name
136
137 # -- Move
138 def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id):
139 name = wim_name[:16]
140 if name not in wim_threads["names"]:
141 wim_threads["names"].append(name)
142 return name
143 name = wim_name[:16] + "." + tenant_name[:16]
144 if name not in wim_threads["names"]:
145 wim_threads["names"].append(name)
146 return name
147 name = wim_id + "-" + tenant_id
148 wim_threads["names"].append(name)
149 return name
150
151
152 def start_service(mydb, persistence=None, wim=None):
153 global db, global_config
154 db = nfvo_db.nfvo_db(lock=db_lock)
155 mydb.lock = db_lock
156 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
157 global ovim
158
159 persistence = persistence or WimPersistence(db)
160
161 # Initialize openvim for SDN control
162 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
163 # TODO: review ovim.py to delete not needed configuration
164 ovim_configuration = {
165 'logger_name': 'openmano.ovim',
166 'network_vlan_range_start': 1000,
167 'network_vlan_range_end': 4096,
168 'db_name': global_config["db_ovim_name"],
169 'db_host': global_config["db_ovim_host"],
170 'db_user': global_config["db_ovim_user"],
171 'db_passwd': global_config["db_ovim_passwd"],
172 'bridge_ifaces': {},
173 'mode': 'normal',
174 'network_type': 'bridge',
175 #TODO: log_level_of should not be needed. To be modified in ovim
176 'log_level_of': 'DEBUG'
177 }
178 try:
179 # starts ovim library
180 ovim = ovim_module.ovim(ovim_configuration)
181
182 global wim_engine
183 wim_engine = wim or WimEngine(persistence)
184 wim_engine.ovim = ovim
185
186 ovim.start_service()
187
188 #delete old unneeded vim_wim_actions
189 clean_db(mydb)
190
191 # starts vim_threads
192 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
193 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
194 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
195 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
196 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
197 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
198 vims = mydb.get_rows(FROM=from_, SELECT=select_)
199 for vim in vims:
200 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
201 'datacenter_id': vim.get('datacenter_id')}
202 if vim["config"]:
203 extra.update(yaml.load(vim["config"]))
204 if vim.get('dt_config'):
205 extra.update(yaml.load(vim["dt_config"]))
206 if vim["type"] not in vimconn_imported:
207 module_info=None
208 try:
209 module = "vimconn_" + vim["type"]
210 pkg = __import__("osm_ro." + module)
211 vim_conn = getattr(pkg, module)
212 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
213 # vim_conn = imp.load_module(vim["type"], *module_info)
214 vimconn_imported[vim["type"]] = vim_conn
215 except (IOError, ImportError) as e:
216 # if module_info and module_info[0]:
217 # file.close(module_info[0])
218 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
219 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
220
221 thread_id = vim['datacenter_tenant_id']
222 vim_persistent_info[thread_id] = {}
223 try:
224 #if not tenant:
225 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
226 myvim = vimconn_imported[ vim["type"] ].vimconnector(
227 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
228 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
229 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
230 user=vim['user'], passwd=vim['passwd'],
231 config=extra, persistent_info=vim_persistent_info[thread_id]
232 )
233 except vimconn.vimconnException as e:
234 myvim = e
235 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
236 vim['datacenter_id'], e))
237 except Exception as e:
238 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
239 httperrors.Internal_Server_Error)
240 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'],
241 vim['vim_tenant_id'])
242 new_thread = vim_thread.vim_thread(task_lock, thread_name, vim['datacenter_name'],
243 vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
244 new_thread.start()
245 vim_threads["running"][thread_id] = new_thread
246
247 wim_engine.start_threads()
248 except db_base_Exception as e:
249 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
250 except ovim_module.ovimException as e:
251 message = str(e)
252 if message[:22] == "DATABASE wrong version":
253 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
254 "at host {dbhost}".format(
255 msg=message[22:-3], dbname=global_config["db_ovim_name"],
256 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
257 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
258 raise NfvoException(message, httperrors.Bad_Request)
259
260
261 def stop_service():
262 global ovim, global_config
263 if ovim:
264 ovim.stop_service()
265 for thread_id, thread in vim_threads["running"].items():
266 thread.insert_task("exit")
267 vim_threads["deleting"][thread_id] = thread
268 vim_threads["running"] = {}
269
270 if wim_engine:
271 wim_engine.stop_threads()
272
273 if global_config and global_config.get("console_thread"):
274 for thread in global_config["console_thread"]:
275 thread.terminate = True
276
277 def get_version():
278 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
279 global_config["version_date"] ))
280
281 def clean_db(mydb):
282 """
283 Clean unused or old entries at database to avoid unlimited growing
284 :param mydb: database connector
285 :return: None
286 """
287 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
288 now = t.time()-3600*24*7
289 instance_action_id = None
290 nb_deleted = 0
291 while True:
292 actions_to_delete = mydb.get_rows(
293 SELECT=("item", "item_id", "instance_action_id"),
294 FROM="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
295 "left join instance_scenarios as i on ia.instance_id=i.uuid",
296 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
297 "va.status": ("DONE", "SUPERSEDED")},
298 LIMIT=100
299 )
300 for to_delete in actions_to_delete:
301 mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete)
302 if instance_action_id != to_delete["instance_action_id"]:
303 instance_action_id = to_delete["instance_action_id"]
304 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
305 nb_deleted += len(actions_to_delete)
306 if len(actions_to_delete) < 100:
307 break
308 # clean locks
309 mydb.update_rows("vim_wim_actions", UPDATE={"worker": None}, WHERE={"worker<>": None})
310
311 if nb_deleted:
312 logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted))
313
314
315 def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
316 '''Obtain flavorList
317 return result, content:
318 <0, error_text upon error
319 nb_records, flavor_list on success
320 '''
321 WHERE_dict={}
322 WHERE_dict['vnf_id'] = vnf_id
323 if nfvo_tenant is not None:
324 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
325
326 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
327 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
328 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
329 #print "get_flavor_list result:", result
330 #print "get_flavor_list content:", content
331 flavorList=[]
332 for flavor in flavors:
333 flavorList.append(flavor['flavor_id'])
334 return flavorList
335
336
337 def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
338 """
339 Get used images of all vms belonging to this VNFD
340 :param mydb: database conector
341 :param vnf_id: vnfd uuid
342 :param nfvo_tenant: tenant, not used
343 :return: The list of image uuid used
344 """
345 image_list = []
346 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
347 for vm in vms:
348 if vm["image_id"] and vm["image_id"] not in image_list:
349 image_list.append(vm["image_id"])
350 if vm["image_list"]:
351 vm_image_list = yaml.load(vm["image_list"])
352 for image_dict in vm_image_list:
353 if image_dict["image_id"] not in image_list:
354 image_list.append(image_dict["image_id"])
355 return image_list
356
357
358 def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
359 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False):
360 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
361 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
362 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
363 raise exception upon error
364 '''
365 WHERE_dict={}
366 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
367 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
368 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
369 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
370 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
371 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
372 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
373 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'
374 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
375 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
376 'user','passwd', 'dt.config as dt_config')
377 else:
378 from_ = 'datacenters as d'
379 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
380 try:
381 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
382 vim_dict={}
383 for vim in vims:
384 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
385 'datacenter_id': vim.get('datacenter_id'),
386 '_vim_type_internal': vim.get('type')}
387 if vim["config"]:
388 extra.update(yaml.load(vim["config"]))
389 if vim.get('dt_config'):
390 extra.update(yaml.load(vim["dt_config"]))
391 if vim["type"] not in vimconn_imported:
392 module_info=None
393 try:
394 module = "vimconn_" + vim["type"]
395 pkg = __import__("osm_ro." + module)
396 vim_conn = getattr(pkg, module)
397 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
398 # vim_conn = imp.load_module(vim["type"], *module_info)
399 vimconn_imported[vim["type"]] = vim_conn
400 except (IOError, ImportError) as e:
401 # if module_info and module_info[0]:
402 # file.close(module_info[0])
403 if ignore_errors:
404 logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
405 vim["type"], module, type(e).__name__, str(e)))
406 continue
407 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
408 vim["type"], module, type(e).__name__, str(e)), httperrors.Bad_Request)
409
410 try:
411 if 'datacenter_tenant_id' in vim:
412 thread_id = vim["datacenter_tenant_id"]
413 if thread_id not in vim_persistent_info:
414 vim_persistent_info[thread_id] = {}
415 persistent_info = vim_persistent_info[thread_id]
416 else:
417 persistent_info = {}
418 #if not tenant:
419 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
420 vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
421 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
422 tenant_id=vim.get('vim_tenant_id',vim_tenant),
423 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
424 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
425 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
426 config=extra, persistent_info=persistent_info
427 )
428 except Exception as e:
429 if ignore_errors:
430 logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)))
431 continue
432 http_code = httperrors.Internal_Server_Error
433 if isinstance(e, vimconn.vimconnException):
434 http_code = e.http_code
435 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
436 return vim_dict
437 except db_base_Exception as e:
438 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
439
440
441 def rollback(mydb, vims, rollback_list):
442 undeleted_items=[]
443 #delete things by reverse order
444 for i in range(len(rollback_list)-1, -1, -1):
445 item = rollback_list[i]
446 if item["where"]=="vim":
447 if item["vim_id"] not in vims:
448 continue
449 if is_task_id(item["uuid"]):
450 continue
451 vim = vims[item["vim_id"]]
452 try:
453 if item["what"]=="image":
454 vim.delete_image(item["uuid"])
455 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
456 elif item["what"]=="flavor":
457 vim.delete_flavor(item["uuid"])
458 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
459 elif item["what"]=="network":
460 vim.delete_network(item["uuid"])
461 elif item["what"]=="vm":
462 vim.delete_vminstance(item["uuid"])
463 except vimconn.vimconnException as e:
464 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
465 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
466 except db_base_Exception as e:
467 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
468
469 else: # where==mano
470 try:
471 if item["what"]=="image":
472 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
473 elif item["what"]=="flavor":
474 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
475 except db_base_Exception as e:
476 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
477 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
478 if len(undeleted_items)==0:
479 return True," Rollback successful."
480 else:
481 return False," Rollback fails to delete: " + str(undeleted_items)
482
483
484 def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
485 global global_config
486 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
487 vnfc_interfaces={}
488 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
489 name_dict = {}
490 #dataplane interfaces
491 for numa in vnfc.get("numas",() ):
492 for interface in numa.get("interfaces",()):
493 if interface["name"] in name_dict:
494 raise NfvoException(
495 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
496 vnfc["name"], interface["name"]),
497 httperrors.Bad_Request)
498 name_dict[ interface["name"] ] = "underlay"
499 #bridge interfaces
500 for interface in vnfc.get("bridge-ifaces",() ):
501 if interface["name"] in name_dict:
502 raise NfvoException(
503 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
504 vnfc["name"], interface["name"]),
505 httperrors.Bad_Request)
506 name_dict[ interface["name"] ] = "overlay"
507 vnfc_interfaces[ vnfc["name"] ] = name_dict
508 # check bood-data info
509 # if "boot-data" in vnfc:
510 # # check that user-data is incompatible with users and config-files
511 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
512 # raise NfvoException(
513 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
514 # httperrors.Bad_Request)
515
516 #check if the info in external_connections matches with the one in the vnfcs
517 name_list=[]
518 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
519 if external_connection["name"] in name_list:
520 raise NfvoException(
521 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
522 external_connection["name"]),
523 httperrors.Bad_Request)
524 name_list.append(external_connection["name"])
525 if external_connection["VNFC"] not in vnfc_interfaces:
526 raise NfvoException(
527 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
528 external_connection["name"], external_connection["VNFC"]),
529 httperrors.Bad_Request)
530
531 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
532 raise NfvoException(
533 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
534 external_connection["name"],
535 external_connection["local_iface_name"]),
536 httperrors.Bad_Request )
537
538 #check if the info in internal_connections matches with the one in the vnfcs
539 name_list=[]
540 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
541 if internal_connection["name"] in name_list:
542 raise NfvoException(
543 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
544 internal_connection["name"]),
545 httperrors.Bad_Request)
546 name_list.append(internal_connection["name"])
547 #We should check that internal-connections of type "ptp" have only 2 elements
548
549 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
550 raise NfvoException(
551 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
552 internal_connection["name"],
553 'ptp' if vnf_descriptor_version==1 else 'e-line',
554 'data' if vnf_descriptor_version==1 else "e-lan"),
555 httperrors.Bad_Request)
556 for port in internal_connection["elements"]:
557 vnf = port["VNFC"]
558 iface = port["local_iface_name"]
559 if vnf not in vnfc_interfaces:
560 raise NfvoException(
561 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
562 internal_connection["name"], vnf),
563 httperrors.Bad_Request)
564 if iface not in vnfc_interfaces[ vnf ]:
565 raise NfvoException(
566 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
567 internal_connection["name"], iface),
568 httperrors.Bad_Request)
569 return -httperrors.Bad_Request,
570 if vnf_descriptor_version==1 and "type" not in internal_connection:
571 if vnfc_interfaces[vnf][iface] == "overlay":
572 internal_connection["type"] = "bridge"
573 else:
574 internal_connection["type"] = "data"
575 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
576 if vnfc_interfaces[vnf][iface] == "overlay":
577 internal_connection["implementation"] = "overlay"
578 else:
579 internal_connection["implementation"] = "underlay"
580 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
581 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
582 raise NfvoException(
583 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
584 internal_connection["name"],
585 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
586 'data' if vnf_descriptor_version==1 else 'underlay'),
587 httperrors.Bad_Request)
588 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
589 vnfc_interfaces[vnf][iface] == "underlay":
590 raise NfvoException(
591 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
592 internal_connection["name"], iface,
593 'data' if vnf_descriptor_version==1 else 'underlay',
594 'bridge' if vnf_descriptor_version==1 else 'overlay'),
595 httperrors.Bad_Request)
596
597
598 def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None):
599 #look if image exist
600 if only_create_at_vim:
601 image_mano_id = image_dict['uuid']
602 if return_on_error == None:
603 return_on_error = True
604 else:
605 if image_dict['location']:
606 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
607 else:
608 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
609 if len(images)>=1:
610 image_mano_id = images[0]['uuid']
611 else:
612 #create image in MANO DB
613 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
614 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
615 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
616 }
617 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
618 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
619 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
620 #create image at every vim
621 for vim_id,vim in vims.iteritems():
622 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
623 image_created="false"
624 #look at database
625 image_db = mydb.get_rows(FROM="datacenters_images",
626 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
627 #look at VIM if this image exist
628 try:
629 if image_dict['location'] is not None:
630 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
631 else:
632 filter_dict = {}
633 filter_dict['name'] = image_dict['universal_name']
634 if image_dict.get('checksum') != None:
635 filter_dict['checksum'] = image_dict['checksum']
636 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
637 vim_images = vim.get_image_list(filter_dict)
638 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
639 if len(vim_images) > 1:
640 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), httperrors.Conflict)
641 elif len(vim_images) == 0:
642 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
643 else:
644 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
645 image_vim_id = vim_images[0]['id']
646
647 except vimconn.vimconnNotFoundException as e:
648 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
649 try:
650 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
651 if image_dict['location']:
652 image_vim_id = vim.new_image(image_dict)
653 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
654 image_created="true"
655 else:
656 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
657 raise vimconn.vimconnException(str(e))
658 except vimconn.vimconnException as e:
659 if return_on_error:
660 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
661 raise
662 image_vim_id = None
663 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
664 continue
665 except vimconn.vimconnException as e:
666 if return_on_error:
667 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
668 raise
669 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
670 image_vim_id = None
671 continue
672 #if we reach here, the image has been created or existed
673 if len(image_db)==0:
674 #add new vim_id at datacenters_images
675 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
676 'image_id':image_mano_id,
677 'vim_id': image_vim_id,
678 'created':image_created})
679 elif image_db[0]["vim_id"]!=image_vim_id:
680 #modify existing vim_id at datacenters_images
681 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id})
682
683 return image_vim_id if only_create_at_vim else image_mano_id
684
685
686 def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
687 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
688 'ram':flavor_dict.get('ram'),
689 'vcpus':flavor_dict.get('vcpus'),
690 }
691 if 'extended' in flavor_dict and flavor_dict['extended']==None:
692 del flavor_dict['extended']
693 if 'extended' in flavor_dict:
694 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
695
696 #look if flavor exist
697 if only_create_at_vim:
698 flavor_mano_id = flavor_dict['uuid']
699 if return_on_error == None:
700 return_on_error = True
701 else:
702 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
703 if len(flavors)>=1:
704 flavor_mano_id = flavors[0]['uuid']
705 else:
706 #create flavor
707 #create one by one the images of aditional disks
708 dev_image_list=[] #list of images
709 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
710 dev_nb=0
711 for device in flavor_dict['extended'].get('devices',[]):
712 if "image" not in device and "image name" not in device:
713 continue
714 image_dict={}
715 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
716 image_dict['universal_name']=device.get('image name')
717 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
718 image_dict['location']=device.get('image')
719 #image_dict['new_location']=vnfc.get('image location')
720 image_dict['checksum']=device.get('image checksum')
721 image_metadata_dict = device.get('image metadata', None)
722 image_metadata_str = None
723 if image_metadata_dict != None:
724 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
725 image_dict['metadata']=image_metadata_str
726 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
727 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
728 dev_image_list.append(image_id)
729 dev_nb += 1
730 temp_flavor_dict['name'] = flavor_dict['name']
731 temp_flavor_dict['description'] = flavor_dict.get('description',None)
732 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
733 flavor_mano_id= content
734 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
735 #create flavor at every vim
736 if 'uuid' in flavor_dict:
737 del flavor_dict['uuid']
738 flavor_vim_id=None
739 for vim_id,vim in vims.items():
740 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
741 flavor_created="false"
742 #look at database
743 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
744 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
745 #look at VIM if this flavor exist SKIPPED
746 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
747 #if res_vim < 0:
748 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
749 # continue
750 #elif res_vim==0:
751
752 # Create the flavor in VIM
753 # Translate images at devices from MANO id to VIM id
754 disk_list = []
755 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
756 # make a copy of original devices
757 devices_original=[]
758
759 for device in flavor_dict["extended"].get("devices",[]):
760 dev={}
761 dev.update(device)
762 devices_original.append(dev)
763 if 'image' in device:
764 del device['image']
765 if 'image metadata' in device:
766 del device['image metadata']
767 if 'image checksum' in device:
768 del device['image checksum']
769 dev_nb = 0
770 for index in range(0,len(devices_original)) :
771 device=devices_original[index]
772 if "image" not in device and "image name" not in device:
773 # if 'size' in device:
774 disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')})
775 continue
776 image_dict={}
777 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
778 image_dict['universal_name']=device.get('image name')
779 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
780 image_dict['location']=device.get('image')
781 # image_dict['new_location']=device.get('image location')
782 image_dict['checksum']=device.get('image checksum')
783 image_metadata_dict = device.get('image metadata', None)
784 image_metadata_str = None
785 if image_metadata_dict != None:
786 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
787 image_dict['metadata']=image_metadata_str
788 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
789 image_dict["uuid"]=image_mano_id
790 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
791
792 #save disk information (image must be based on and size
793 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
794
795 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
796 dev_nb += 1
797 if len(flavor_db)>0:
798 #check that this vim_id exist in VIM, if not create
799 flavor_vim_id=flavor_db[0]["vim_id"]
800 try:
801 vim.get_flavor(flavor_vim_id)
802 continue #flavor exist
803 except vimconn.vimconnException:
804 pass
805 #create flavor at vim
806 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
807 try:
808 flavor_vim_id = None
809 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
810 flavor_create="false"
811 except vimconn.vimconnException as e:
812 pass
813 try:
814 if not flavor_vim_id:
815 flavor_vim_id = vim.new_flavor(flavor_dict)
816 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
817 flavor_created="true"
818 except vimconn.vimconnException as e:
819 if return_on_error:
820 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
821 raise
822 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
823 flavor_vim_id = None
824 continue
825 #if reach here the flavor has been create or exist
826 if len(flavor_db)==0:
827 #add new vim_id at datacenters_flavors
828 extended_devices_yaml = None
829 if len(disk_list) > 0:
830 extended_devices = dict()
831 extended_devices['disks'] = disk_list
832 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
833 mydb.new_row('datacenters_flavors',
834 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
835 'created': flavor_created, 'extended': extended_devices_yaml})
836 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
837 #modify existing vim_id at datacenters_flavors
838 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
839 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
840
841 return flavor_vim_id if only_create_at_vim else flavor_mano_id
842
843
844 def get_str(obj, field, length):
845 """
846 Obtain the str value,
847 :param obj:
848 :param length:
849 :return:
850 """
851 value = obj.get(field)
852 if value is not None:
853 value = str(value)[:length]
854 return value
855
856 def _lookfor_or_create_image(db_image, mydb, descriptor):
857 """
858 fill image content at db_image dictionary. Check if the image with this image and checksum exist
859 :param db_image: dictionary to insert data
860 :param mydb: database connector
861 :param descriptor: yang descriptor
862 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
863 """
864
865 db_image["name"] = get_str(descriptor, "image", 255)
866 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
867 if not db_image["checksum"]: # Ensure that if empty string, None is stored
868 db_image["checksum"] = None
869 if db_image["name"].startswith("/"):
870 db_image["location"] = db_image["name"]
871 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
872 else:
873 db_image["universal_name"] = db_image["name"]
874 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
875 'checksum': db_image['checksum']})
876 if existing_images:
877 return existing_images[0]["uuid"]
878 else:
879 image_uuid = str(uuid4())
880 db_image["uuid"] = image_uuid
881 return None
882
883 def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
884 """
885 Parses an OSM IM vnfd_catalog and insert at DB
886 :param mydb:
887 :param tenant_id:
888 :param vnf_descriptor:
889 :return: The list of cretated vnf ids
890 """
891 try:
892 myvnfd = vnfd_catalog.vnfd()
893 try:
894 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True,
895 skip_unknown=True)
896 except Exception as e:
897 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request)
898 db_vnfs = []
899 db_nets = []
900 db_vms = []
901 db_vms_index = 0
902 db_interfaces = []
903 db_images = []
904 db_flavors = []
905 db_ip_profiles_index = 0
906 db_ip_profiles = []
907 uuid_list = []
908 vnfd_uuid_list = []
909 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
910 if not vnfd_catalog_descriptor:
911 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
912 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
913 if not vnfd_descriptor_list:
914 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
915 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.itervalues():
916 vnfd = vnfd_yang.get()
917
918 # table vnf
919 vnf_uuid = str(uuid4())
920 uuid_list.append(vnf_uuid)
921 vnfd_uuid_list.append(vnf_uuid)
922 vnfd_id = get_str(vnfd, "id", 255)
923 db_vnf = {
924 "uuid": vnf_uuid,
925 "osm_id": vnfd_id,
926 "name": get_str(vnfd, "name", 255),
927 "description": get_str(vnfd, "description", 255),
928 "tenant_id": tenant_id,
929 "vendor": get_str(vnfd, "vendor", 255),
930 "short_name": get_str(vnfd, "short-name", 255),
931 "descriptor": str(vnf_descriptor)[:60000]
932 }
933
934 for vnfd_descriptor in vnfd_descriptor_list:
935 if vnfd_descriptor["id"] == str(vnfd["id"]):
936 break
937
938 # table ip_profiles (ip-profiles)
939 ip_profile_name2db_table_index = {}
940 for ip_profile in vnfd.get("ip-profiles").itervalues():
941 db_ip_profile = {
942 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
943 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
944 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
945 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
946 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
947 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
948 }
949 dns_list = []
950 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
951 dns_list.append(str(dns.get("address")))
952 db_ip_profile["dns_address"] = ";".join(dns_list)
953 if ip_profile["ip-profile-params"].get('security-group'):
954 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
955 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
956 db_ip_profiles_index += 1
957 db_ip_profiles.append(db_ip_profile)
958
959 # table nets (internal-vld)
960 net_id2uuid = {} # for mapping interface with network
961 for vld in vnfd.get("internal-vld").itervalues():
962 net_uuid = str(uuid4())
963 uuid_list.append(net_uuid)
964 db_net = {
965 "name": get_str(vld, "name", 255),
966 "vnf_id": vnf_uuid,
967 "uuid": net_uuid,
968 "description": get_str(vld, "description", 255),
969 "osm_id": get_str(vld, "id", 255),
970 "type": "bridge", # TODO adjust depending on connection point type
971 }
972 net_id2uuid[vld.get("id")] = net_uuid
973 db_nets.append(db_net)
974 # ip-profile, link db_ip_profile with db_sce_net
975 if vld.get("ip-profile-ref"):
976 ip_profile_name = vld.get("ip-profile-ref")
977 if ip_profile_name not in ip_profile_name2db_table_index:
978 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
979 "'{}'. Reference to a non-existing 'ip_profiles'".format(
980 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
981 httperrors.Bad_Request)
982 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
983 else: #check no ip-address has been defined
984 for icp in vld.get("internal-connection-point").itervalues():
985 if icp.get("ip-address"):
986 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
987 "contains an ip-address but no ip-profile has been defined at VLD".format(
988 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
989 httperrors.Bad_Request)
990
991 # connection points vaiable declaration
992 cp_name2iface_uuid = {}
993 cp_name2vm_uuid = {}
994 cp_name2db_interface = {}
995 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
996
997 # table vms (vdus)
998 vdu_id2uuid = {}
999 vdu_id2db_table_index = {}
1000 for vdu in vnfd.get("vdu").itervalues():
1001
1002 for vdu_descriptor in vnfd_descriptor["vdu"]:
1003 if vdu_descriptor["id"] == str(vdu["id"]):
1004 break
1005 vm_uuid = str(uuid4())
1006 uuid_list.append(vm_uuid)
1007 vdu_id = get_str(vdu, "id", 255)
1008 db_vm = {
1009 "uuid": vm_uuid,
1010 "osm_id": vdu_id,
1011 "name": get_str(vdu, "name", 255),
1012 "description": get_str(vdu, "description", 255),
1013 "pdu_type": get_str(vdu, "pdu-type", 255),
1014 "vnf_id": vnf_uuid,
1015 }
1016 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
1017 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
1018 if vdu.get("count"):
1019 db_vm["count"] = int(vdu["count"])
1020
1021 # table image
1022 image_present = False
1023 if vdu.get("image"):
1024 image_present = True
1025 db_image = {}
1026 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
1027 if not image_uuid:
1028 image_uuid = db_image["uuid"]
1029 db_images.append(db_image)
1030 db_vm["image_id"] = image_uuid
1031 if vdu.get("alternative-images"):
1032 vm_alternative_images = []
1033 for alt_image in vdu.get("alternative-images").itervalues():
1034 db_image = {}
1035 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
1036 if not image_uuid:
1037 image_uuid = db_image["uuid"]
1038 db_images.append(db_image)
1039 vm_alternative_images.append({
1040 "image_id": image_uuid,
1041 "vim_type": str(alt_image["vim-type"]),
1042 # "universal_name": str(alt_image["image"]),
1043 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1044 })
1045
1046 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
1047
1048 # volumes
1049 devices = []
1050 if vdu.get("volumes"):
1051 for volume_key in vdu["volumes"]:
1052 volume = vdu["volumes"][volume_key]
1053 if not image_present:
1054 # Convert the first volume to vnfc.image
1055 image_present = True
1056 db_image = {}
1057 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1058 if not image_uuid:
1059 image_uuid = db_image["uuid"]
1060 db_images.append(db_image)
1061 db_vm["image_id"] = image_uuid
1062 else:
1063 # Add Openmano devices
1064 device = {"name": str(volume.get("name"))}
1065 device["type"] = str(volume.get("device-type"))
1066 if volume.get("size"):
1067 device["size"] = int(volume["size"])
1068 if volume.get("image"):
1069 device["image name"] = str(volume["image"])
1070 if volume.get("image-checksum"):
1071 device["image checksum"] = str(volume["image-checksum"])
1072
1073 devices.append(device)
1074
1075 if not db_vm.get("image_id"):
1076 if not db_vm["pdu_type"]:
1077 raise NfvoException("Not defined image for VDU")
1078 # create a fake image
1079
1080 # cloud-init
1081 boot_data = {}
1082 if vdu.get("cloud-init"):
1083 boot_data["user-data"] = str(vdu["cloud-init"])
1084 elif vdu.get("cloud-init-file"):
1085 # TODO Where this file content is present???
1086 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1087 boot_data["user-data"] = str(vdu["cloud-init-file"])
1088
1089 if vdu.get("supplemental-boot-data"):
1090 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1091 boot_data['boot-data-drive'] = True
1092 if vdu["supplemental-boot-data"].get('config-file'):
1093 om_cfgfile_list = list()
1094 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].itervalues():
1095 # TODO Where this file content is present???
1096 cfg_source = str(custom_config_file["source"])
1097 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1098 "content": cfg_source})
1099 boot_data['config-files'] = om_cfgfile_list
1100 if boot_data:
1101 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1102
1103 db_vms.append(db_vm)
1104 db_vms_index += 1
1105
1106 # table interfaces (internal/external interfaces)
1107 flavor_epa_interfaces = []
1108 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1109 for iface in vdu.get("interface").itervalues():
1110 flavor_epa_interface = {}
1111 iface_uuid = str(uuid4())
1112 uuid_list.append(iface_uuid)
1113 db_interface = {
1114 "uuid": iface_uuid,
1115 "internal_name": get_str(iface, "name", 255),
1116 "vm_id": vm_uuid,
1117 }
1118 flavor_epa_interface["name"] = db_interface["internal_name"]
1119 if iface.get("virtual-interface").get("vpci"):
1120 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1121 flavor_epa_interface["vpci"] = db_interface["vpci"]
1122
1123 if iface.get("virtual-interface").get("bandwidth"):
1124 bps = int(iface.get("virtual-interface").get("bandwidth"))
1125 db_interface["bw"] = int(math.ceil(bps/1000000.0))
1126 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1127
1128 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1129 db_interface["type"] = "mgmt"
1130 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1131 db_interface["type"] = "bridge"
1132 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1133 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1134 db_interface["type"] = "data"
1135 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1136 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1137 else "yes"
1138 flavor_epa_interfaces.append(flavor_epa_interface)
1139 else:
1140 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1141 "-interface':'type':'{}'. Interface type is not supported".format(
1142 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
1143 httperrors.Bad_Request)
1144
1145 if iface.get("mgmt-interface"):
1146 db_interface["type"] = "mgmt"
1147
1148 if iface.get("external-connection-point-ref"):
1149 try:
1150 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1151 db_interface["external_name"] = get_str(cp, "name", 255)
1152 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1153 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1154 cp_name2db_interface[db_interface["external_name"]] = db_interface
1155 for cp_descriptor in vnfd_descriptor["connection-point"]:
1156 if cp_descriptor["name"] == db_interface["external_name"]:
1157 break
1158 else:
1159 raise KeyError()
1160
1161 if vdu_id in vdu_id2cp_name:
1162 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1163 else:
1164 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1165
1166 # port security
1167 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1168 db_interface["port_security"] = 0
1169 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1170 db_interface["port_security"] = 1
1171 except KeyError:
1172 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1173 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1174 " at connection-point".format(
1175 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1176 cp=iface.get("vnfd-connection-point-ref")),
1177 httperrors.Bad_Request)
1178 elif iface.get("internal-connection-point-ref"):
1179 try:
1180 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1181 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1182 break
1183 else:
1184 raise KeyError("does not exist at vdu:internal-connection-point")
1185 icp = None
1186 icp_vld = None
1187 for vld in vnfd.get("internal-vld").itervalues():
1188 for cp in vld.get("internal-connection-point").itervalues():
1189 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
1190 if icp:
1191 raise KeyError("is referenced by more than one 'internal-vld'")
1192 icp = cp
1193 icp_vld = vld
1194 if not icp:
1195 raise KeyError("is not referenced by any 'internal-vld'")
1196
1197 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1198 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1199 db_interface["port_security"] = 0
1200 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1201 db_interface["port_security"] = 1
1202 if icp.get("ip-address"):
1203 if not icp_vld.get("ip-profile-ref"):
1204 raise NfvoException
1205 db_interface["ip_address"] = str(icp.get("ip-address"))
1206 except KeyError as e:
1207 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1208 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1209 " {msg}".format(
1210 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1211 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
1212 httperrors.Bad_Request)
1213 if iface.get("position"):
1214 db_interface["created_at"] = int(iface.get("position")) * 50
1215 if iface.get("mac-address"):
1216 db_interface["mac"] = str(iface.get("mac-address"))
1217 db_interfaces.append(db_interface)
1218
1219 # table flavors
1220 db_flavor = {
1221 "name": get_str(vdu, "name", 250) + "-flv",
1222 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1223 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
1224 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
1225 }
1226 # TODO revise the case of several numa-node-policy node
1227 extended = {}
1228 numa = {}
1229 if devices:
1230 extended["devices"] = devices
1231 if flavor_epa_interfaces:
1232 numa["interfaces"] = flavor_epa_interfaces
1233 if vdu.get("guest-epa"): # TODO or dedicated_int:
1234 epa_vcpu_set = False
1235 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1236 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1237 if numa_node_policy.get("node"):
1238 numa_node = numa_node_policy["node"].values()[0]
1239 if numa_node.get("num-cores"):
1240 numa["cores"] = numa_node["num-cores"]
1241 epa_vcpu_set = True
1242 if numa_node.get("paired-threads"):
1243 if numa_node["paired-threads"].get("num-paired-threads"):
1244 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
1245 epa_vcpu_set = True
1246 if len(numa_node["paired-threads"].get("paired-thread-ids")):
1247 numa["paired-threads-id"] = []
1248 for pair in numa_node["paired-threads"]["paired-thread-ids"].itervalues():
1249 numa["paired-threads-id"].append(
1250 (str(pair["thread-a"]), str(pair["thread-b"]))
1251 )
1252 if numa_node.get("num-threads"):
1253 numa["threads"] = int(numa_node["num-threads"])
1254 epa_vcpu_set = True
1255 if numa_node.get("memory-mb"):
1256 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1257 if vdu["guest-epa"].get("mempage-size"):
1258 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1259 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1260 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1261 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1262 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1263 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1264 numa["cores"] = max(db_flavor["vcpus"], 1)
1265 else:
1266 numa["threads"] = max(db_flavor["vcpus"], 1)
1267 if numa:
1268 extended["numas"] = [numa]
1269 if extended:
1270 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1271 db_flavor["extended"] = extended_text
1272 # look if flavor exist
1273 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
1274 'ram': db_flavor.get('ram'),
1275 'vcpus': db_flavor.get('vcpus'),
1276 'extended': db_flavor.get('extended')
1277 }
1278 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1279 if existing_flavors:
1280 flavor_uuid = existing_flavors[0]["uuid"]
1281 else:
1282 flavor_uuid = str(uuid4())
1283 uuid_list.append(flavor_uuid)
1284 db_flavor["uuid"] = flavor_uuid
1285 db_flavors.append(db_flavor)
1286 db_vm["flavor_id"] = flavor_uuid
1287
1288 # VNF affinity and antiaffinity
1289 for pg in vnfd.get("placement-groups").itervalues():
1290 pg_name = get_str(pg, "name", 255)
1291 for vdu in pg.get("member-vdus").itervalues():
1292 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1293 if vdu_id not in vdu_id2db_table_index:
1294 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1295 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1296 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
1297 httperrors.Bad_Request)
1298 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
1299 # TODO consider the case of isolation and not colocation
1300 # if pg.get("strategy") == "ISOLATION":
1301
1302 # VNF mgmt configuration
1303 mgmt_access = {}
1304 if vnfd["mgmt-interface"].get("vdu-id"):
1305 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1306 if mgmt_vdu_id not in vdu_id2uuid:
1307 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1308 "'{vdu}'. Reference to a non-existing vdu".format(
1309 vnf=vnfd_id, vdu=mgmt_vdu_id),
1310 httperrors.Bad_Request)
1311 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
1312 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1313 if vdu_id2cp_name.get(mgmt_vdu_id):
1314 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1315 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
1316
1317 if vnfd["mgmt-interface"].get("ip-address"):
1318 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1319 if vnfd["mgmt-interface"].get("cp"):
1320 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
1321 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1322 "Reference to a non-existing connection-point".format(
1323 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
1324 httperrors.Bad_Request)
1325 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1326 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
1327 # mark this interface as of type mgmt
1328 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1329 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
1330
1331 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1332 "default-user", 64)
1333
1334 if default_user:
1335 mgmt_access["default_user"] = default_user
1336 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1337 "required", 6)
1338 if required:
1339 mgmt_access["required"] = required
1340
1341 if mgmt_access:
1342 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1343
1344 db_vnfs.append(db_vnf)
1345 db_tables=[
1346 {"vnfs": db_vnfs},
1347 {"nets": db_nets},
1348 {"images": db_images},
1349 {"flavors": db_flavors},
1350 {"ip_profiles": db_ip_profiles},
1351 {"vms": db_vms},
1352 {"interfaces": db_interfaces},
1353 ]
1354
1355 logger.debug("create_vnf Deployment done vnfDict: %s",
1356 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1357 mydb.new_rows(db_tables, uuid_list)
1358 return vnfd_uuid_list
1359 except NfvoException:
1360 raise
1361 except Exception as e:
1362 logger.error("Exception {}".format(e))
1363 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1364
1365
1366 @deprecated("Use new_vnfd_v3")
1367 def new_vnf(mydb, tenant_id, vnf_descriptor):
1368 global global_config
1369
1370 # Step 1. Check the VNF descriptor
1371 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
1372 # Step 2. Check tenant exist
1373 vims = {}
1374 if tenant_id != "any":
1375 check_tenant(mydb, tenant_id)
1376 if "tenant_id" in vnf_descriptor["vnf"]:
1377 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1378 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1379 httperrors.Unauthorized)
1380 else:
1381 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1382 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1383 if global_config["auto_push_VNF_to_VIMs"]:
1384 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1385
1386 # Step 4. Review the descriptor and add missing fields
1387 #print vnf_descriptor
1388 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1389 vnf_name = vnf_descriptor['vnf']['name']
1390 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1391 if "physical" in vnf_descriptor['vnf']:
1392 del vnf_descriptor['vnf']['physical']
1393 #print vnf_descriptor
1394
1395 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1396 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1397 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1398
1399 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1400 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1401 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1402 try:
1403 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1404 for vnfc in vnf_descriptor['vnf']['VNFC']:
1405 VNFCitem={}
1406 VNFCitem["name"] = vnfc['name']
1407 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
1408 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
1409
1410 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1411
1412 myflavorDict = {}
1413 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1414 myflavorDict["description"] = VNFCitem["description"]
1415 myflavorDict["ram"] = vnfc.get("ram", 0)
1416 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1417 myflavorDict["disk"] = vnfc.get("disk", 0)
1418 myflavorDict["extended"] = {}
1419
1420 devices = vnfc.get("devices")
1421 if devices != None:
1422 myflavorDict["extended"]["devices"] = devices
1423
1424 # TODO:
1425 # 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
1426 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1427
1428 # Previous code has been commented
1429 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1430 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1431 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1432 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1433 #else:
1434 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1435 # if result2:
1436 # print "Error creating flavor: unknown processor model. Rollback successful."
1437 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1438 # else:
1439 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1440 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1441
1442 if 'numas' in vnfc and len(vnfc['numas'])>0:
1443 myflavorDict['extended']['numas'] = vnfc['numas']
1444
1445 #print myflavorDict
1446
1447 # Step 6.2 New flavors are created in the VIM
1448 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1449
1450 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1451 VNFCitem["flavor_id"] = flavor_id
1452 VNFCDict[vnfc['name']] = VNFCitem
1453
1454 logger.debug("Creating new images in the VIM for each VNFC")
1455 # Step 6.3 New images are created in the VIM
1456 #For each VNFC, we must create the appropriate image.
1457 #This "for" loop might be integrated with the previous one
1458 #In case this integration is made, the VNFCDict might become a VNFClist.
1459 for vnfc in vnf_descriptor['vnf']['VNFC']:
1460 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1461 image_dict={}
1462 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1463 image_dict['universal_name']=vnfc.get('image name')
1464 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1465 image_dict['location']=vnfc.get('VNFC image')
1466 #image_dict['new_location']=vnfc.get('image location')
1467 image_dict['checksum']=vnfc.get('image checksum')
1468 image_metadata_dict = vnfc.get('image metadata', None)
1469 image_metadata_str = None
1470 if image_metadata_dict is not None:
1471 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1472 image_dict['metadata']=image_metadata_str
1473 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1474 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1475 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1476 VNFCDict[vnfc['name']]["image_id"] = image_id
1477 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1478 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1479 if vnfc.get("boot-data"):
1480 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1481
1482
1483 # Step 7. Storing the VNF descriptor in the repository
1484 if "descriptor" not in vnf_descriptor["vnf"]:
1485 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1486
1487 # Step 8. Adding the VNF to the NFVO DB
1488 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1489 return vnf_id
1490 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1491 _, message = rollback(mydb, vims, rollback_list)
1492 if isinstance(e, db_base_Exception):
1493 error_text = "Exception at database"
1494 elif isinstance(e, KeyError):
1495 error_text = "KeyError exception "
1496 e.http_code = httperrors.Internal_Server_Error
1497 else:
1498 error_text = "Exception at VIM"
1499 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1500 #logger.error("start_scenario %s", error_text)
1501 raise NfvoException(error_text, e.http_code)
1502
1503
1504 @deprecated("Use new_vnfd_v3")
1505 def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1506 global global_config
1507
1508 # Step 1. Check the VNF descriptor
1509 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
1510 # Step 2. Check tenant exist
1511 vims = {}
1512 if tenant_id != "any":
1513 check_tenant(mydb, tenant_id)
1514 if "tenant_id" in vnf_descriptor["vnf"]:
1515 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1516 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1517 httperrors.Unauthorized)
1518 else:
1519 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1520 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1521 if global_config["auto_push_VNF_to_VIMs"]:
1522 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1523
1524 # Step 4. Review the descriptor and add missing fields
1525 #print vnf_descriptor
1526 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1527 vnf_name = vnf_descriptor['vnf']['name']
1528 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1529 if "physical" in vnf_descriptor['vnf']:
1530 del vnf_descriptor['vnf']['physical']
1531 #print vnf_descriptor
1532
1533 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1534 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1535 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1536
1537 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1538 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1539 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1540 try:
1541 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1542 for vnfc in vnf_descriptor['vnf']['VNFC']:
1543 VNFCitem={}
1544 VNFCitem["name"] = vnfc['name']
1545 VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
1546
1547 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1548
1549 myflavorDict = {}
1550 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1551 myflavorDict["description"] = VNFCitem["description"]
1552 myflavorDict["ram"] = vnfc.get("ram", 0)
1553 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1554 myflavorDict["disk"] = vnfc.get("disk", 0)
1555 myflavorDict["extended"] = {}
1556
1557 devices = vnfc.get("devices")
1558 if devices != None:
1559 myflavorDict["extended"]["devices"] = devices
1560
1561 # TODO:
1562 # 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
1563 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1564
1565 # Previous code has been commented
1566 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1567 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1568 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1569 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1570 #else:
1571 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1572 # if result2:
1573 # print "Error creating flavor: unknown processor model. Rollback successful."
1574 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1575 # else:
1576 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1577 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1578
1579 if 'numas' in vnfc and len(vnfc['numas'])>0:
1580 myflavorDict['extended']['numas'] = vnfc['numas']
1581
1582 #print myflavorDict
1583
1584 # Step 6.2 New flavors are created in the VIM
1585 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1586
1587 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1588 VNFCitem["flavor_id"] = flavor_id
1589 VNFCDict[vnfc['name']] = VNFCitem
1590
1591 logger.debug("Creating new images in the VIM for each VNFC")
1592 # Step 6.3 New images are created in the VIM
1593 #For each VNFC, we must create the appropriate image.
1594 #This "for" loop might be integrated with the previous one
1595 #In case this integration is made, the VNFCDict might become a VNFClist.
1596 for vnfc in vnf_descriptor['vnf']['VNFC']:
1597 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1598 image_dict={}
1599 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1600 image_dict['universal_name']=vnfc.get('image name')
1601 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1602 image_dict['location']=vnfc.get('VNFC image')
1603 #image_dict['new_location']=vnfc.get('image location')
1604 image_dict['checksum']=vnfc.get('image checksum')
1605 image_metadata_dict = vnfc.get('image metadata', None)
1606 image_metadata_str = None
1607 if image_metadata_dict is not None:
1608 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1609 image_dict['metadata']=image_metadata_str
1610 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1611 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1612 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1613 VNFCDict[vnfc['name']]["image_id"] = image_id
1614 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1615 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1616 if vnfc.get("boot-data"):
1617 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1618
1619 # Step 7. Storing the VNF descriptor in the repository
1620 if "descriptor" not in vnf_descriptor["vnf"]:
1621 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1622
1623 # Step 8. Adding the VNF to the NFVO DB
1624 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1625 return vnf_id
1626 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1627 _, message = rollback(mydb, vims, rollback_list)
1628 if isinstance(e, db_base_Exception):
1629 error_text = "Exception at database"
1630 elif isinstance(e, KeyError):
1631 error_text = "KeyError exception "
1632 e.http_code = httperrors.Internal_Server_Error
1633 else:
1634 error_text = "Exception at VIM"
1635 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1636 #logger.error("start_scenario %s", error_text)
1637 raise NfvoException(error_text, e.http_code)
1638
1639
1640 def get_vnf_id(mydb, tenant_id, vnf_id):
1641 #check valid tenant_id
1642 check_tenant(mydb, tenant_id)
1643 #obtain data
1644 where_or = {}
1645 if tenant_id != "any":
1646 where_or["tenant_id"] = tenant_id
1647 where_or["public"] = True
1648 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1649
1650 vnf_id = vnf["uuid"]
1651 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1652 filtered_content = dict( (k,v) for k,v in vnf.iteritems() if k in filter_keys )
1653 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1654 data={'vnf' : filtered_content}
1655 #GET VM
1656 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
1657 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1658 'boot_data'),
1659 WHERE={'vnfs.uuid': vnf_id} )
1660 if len(content) != 0:
1661 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1662 # change boot_data into boot-data
1663 for vm in content:
1664 if vm.get("boot_data"):
1665 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1666 del vm["boot_data"]
1667
1668 data['vnf']['VNFC'] = content
1669 #TODO: GET all the information from a VNFC and include it in the output.
1670
1671 #GET NET
1672 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
1673 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1674 WHERE={'vnfs.uuid': vnf_id} )
1675 data['vnf']['nets'] = content
1676
1677 #GET ip-profile for each net
1678 for net in data['vnf']['nets']:
1679 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1680 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1681 WHERE={'net_id': net["uuid"]} )
1682 if len(ipprofiles)==1:
1683 net["ip_profile"] = ipprofiles[0]
1684 elif len(ipprofiles)>1:
1685 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), httperrors.Bad_Request)
1686
1687
1688 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1689
1690 #GET External Interfaces
1691 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1692 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1693 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1694 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
1695 #print content
1696 data['vnf']['external-connections'] = content
1697
1698 return data
1699
1700
1701 def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1702 # Check tenant exist
1703 if tenant_id != "any":
1704 check_tenant(mydb, tenant_id)
1705 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1706 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1707 else:
1708 vims={}
1709
1710 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1711 where_or = {}
1712 if tenant_id != "any":
1713 where_or["tenant_id"] = tenant_id
1714 where_or["public"] = True
1715 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1716 vnf_id = vnf["uuid"]
1717
1718 # "Getting the list of flavors and tenants of the VNF"
1719 flavorList = get_flavorlist(mydb, vnf_id)
1720 if len(flavorList)==0:
1721 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
1722
1723 imageList = get_imagelist(mydb, vnf_id)
1724 if len(imageList)==0:
1725 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
1726
1727 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1728 if deleted == 0:
1729 raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1730
1731 undeletedItems = []
1732 for flavor in flavorList:
1733 #check if flavor is used by other vnf
1734 try:
1735 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1736 if len(c) > 0:
1737 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1738 continue
1739 #flavor not used, must be deleted
1740 #delelte at VIM
1741 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
1742 for flavor_vim in c:
1743 if not flavor_vim['created']: # skip this flavor because not created by openmano
1744 continue
1745 # look for vim
1746 myvim = None
1747 for vim in vims.values():
1748 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1749 myvim = vim
1750 break
1751 if not myvim:
1752 continue
1753 try:
1754 myvim.delete_flavor(flavor_vim["vim_id"])
1755 except vimconn.vimconnNotFoundException:
1756 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1757 flavor_vim["datacenter_vim_id"] )
1758 except vimconn.vimconnException as e:
1759 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1760 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1761 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1762 flavor_vim["datacenter_vim_id"]))
1763 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1764 mydb.delete_row_by_id('flavors', flavor)
1765 except db_base_Exception as e:
1766 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
1767 undeletedItems.append("flavor {}".format(flavor))
1768
1769
1770 for image in imageList:
1771 try:
1772 #check if image is used by other vnf
1773 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
1774 if len(c) > 0:
1775 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1776 continue
1777 #image not used, must be deleted
1778 #delelte at VIM
1779 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
1780 for image_vim in c:
1781 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
1782 continue
1783 if image_vim['created']=='false': #skip this image because not created by openmano
1784 continue
1785 myvim=vims[ image_vim["datacenter_id"] ]
1786 try:
1787 myvim.delete_image(image_vim["vim_id"])
1788 except vimconn.vimconnNotFoundException as e:
1789 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1790 except vimconn.vimconnException as e:
1791 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1792 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1793 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
1794 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1795 mydb.delete_row_by_id('images', image)
1796 except db_base_Exception as e:
1797 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
1798 undeletedItems.append("image %s" % image)
1799
1800 return vnf_id + " " + vnf["name"]
1801 #if undeletedItems:
1802 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1803
1804
1805 @deprecated("Not used")
1806 def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1807 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1808 if result < 0:
1809 return result, vims
1810 elif result == 0:
1811 return -httperrors.Not_Found, "datacenter '%s' not found" % datacenter_name
1812 myvim = vims.values()[0]
1813 result,servers = myvim.get_hosts_info()
1814 if result < 0:
1815 return result, servers
1816 topology = {'name':myvim['name'] , 'servers': servers}
1817 return result, topology
1818
1819
1820 def get_hosts(mydb, nfvo_tenant_id):
1821 vims = get_vim(mydb, nfvo_tenant_id)
1822 if len(vims) == 0:
1823 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found)
1824 elif len(vims)>1:
1825 #print "nfvo.datacenter_action() error. Several datacenters found"
1826 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
1827 myvim = vims.values()[0]
1828 try:
1829 hosts = myvim.get_hosts()
1830 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
1831
1832 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1833 for host in hosts:
1834 server={'name':host['name'], 'vms':[]}
1835 for vm in host['instances']:
1836 #get internal name and model
1837 try:
1838 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1839 WHERE={'vim_vm_id':vm['id']} )
1840 if len(c) == 0:
1841 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1842 continue
1843 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1844
1845 except db_base_Exception as e:
1846 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1847 datacenter['Datacenters'][0]['servers'].append(server)
1848 #return -400, "en construccion"
1849
1850 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1851 return datacenter
1852 except vimconn.vimconnException as e:
1853 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
1854
1855
1856 @deprecated("Use new_nsd_v3")
1857 def new_scenario(mydb, tenant_id, topo):
1858
1859 # result, vims = get_vim(mydb, tenant_id)
1860 # if result < 0:
1861 # return result, vims
1862 #1: parse input
1863 if tenant_id != "any":
1864 check_tenant(mydb, tenant_id)
1865 if "tenant_id" in topo:
1866 if topo["tenant_id"] != tenant_id:
1867 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1868 httperrors.Unauthorized)
1869 else:
1870 tenant_id=None
1871
1872 #1.1: get VNFs and external_networks (other_nets).
1873 vnfs={}
1874 other_nets={} #external_networks, bridge_networks and data_networkds
1875 nodes = topo['topology']['nodes']
1876 for k in nodes.keys():
1877 if nodes[k]['type'] == 'VNF':
1878 vnfs[k] = nodes[k]
1879 vnfs[k]['ifaces'] = {}
1880 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1881 other_nets[k] = nodes[k]
1882 other_nets[k]['external']=True
1883 elif nodes[k]['type'] == 'network':
1884 other_nets[k] = nodes[k]
1885 other_nets[k]['external']=False
1886
1887
1888 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1889 for name,vnf in vnfs.items():
1890 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
1891 error_text = ""
1892 error_pos = "'topology':'nodes':'" + name + "'"
1893 if 'vnf_id' in vnf:
1894 error_text += " 'vnf_id' " + vnf['vnf_id']
1895 where['uuid'] = vnf['vnf_id']
1896 if 'VNF model' in vnf:
1897 error_text += " 'VNF model' " + vnf['VNF model']
1898 where['name'] = vnf['VNF model']
1899 if len(where) == 1:
1900 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request)
1901
1902 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1903 FROM='vnfs',
1904 WHERE=where)
1905 if len(vnf_db)==0:
1906 raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
1907 elif len(vnf_db)>1:
1908 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
1909 vnf['uuid']=vnf_db[0]['uuid']
1910 vnf['description']=vnf_db[0]['description']
1911 #get external interfaces
1912 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1913 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1914 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
1915 for ext_iface in ext_ifaces:
1916 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1917
1918 #1.4 get list of connections
1919 conections = topo['topology']['connections']
1920 conections_list = []
1921 conections_list_name = []
1922 for k in conections.keys():
1923 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1924 ifaces_list = conections[k]['nodes'].items()
1925 elif type(conections[k]['nodes'])==list: #list with dictionary
1926 ifaces_list=[]
1927 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1928 for k2 in conection_pair_list:
1929 ifaces_list += k2
1930
1931 con_type = conections[k].get("type", "link")
1932 if con_type != "link":
1933 if k in other_nets:
1934 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request)
1935 other_nets[k] = {'external': False}
1936 if conections[k].get("graph"):
1937 other_nets[k]["graph"] = conections[k]["graph"]
1938 ifaces_list.append( (k, None) )
1939
1940
1941 if con_type == "external_network":
1942 other_nets[k]['external'] = True
1943 if conections[k].get("model"):
1944 other_nets[k]["model"] = conections[k]["model"]
1945 else:
1946 other_nets[k]["model"] = k
1947 if con_type == "dataplane_net" or con_type == "bridge_net":
1948 other_nets[k]["model"] = con_type
1949
1950 conections_list_name.append(k)
1951 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)
1952 #print set(ifaces_list)
1953 #check valid VNF and iface names
1954 for iface in ifaces_list:
1955 if iface[0] not in vnfs and iface[0] not in other_nets :
1956 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1957 str(k), iface[0]), httperrors.Not_Found)
1958 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
1959 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1960 str(k), iface[0], iface[1]), httperrors.Not_Found)
1961
1962 #1.5 unify connections from the pair list to a consolidated list
1963 index=0
1964 while index < len(conections_list):
1965 index2 = index+1
1966 while index2 < len(conections_list):
1967 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
1968 conections_list[index] |= conections_list[index2]
1969 del conections_list[index2]
1970 del conections_list_name[index2]
1971 else:
1972 index2 += 1
1973 conections_list[index] = list(conections_list[index]) # from set to list again
1974 index += 1
1975 #for k in conections_list:
1976 # print k
1977
1978
1979
1980 #1.6 Delete non external nets
1981 # for k in other_nets.keys():
1982 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1983 # for con in conections_list:
1984 # delete_indexes=[]
1985 # for index in range(0,len(con)):
1986 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1987 # for index in delete_indexes:
1988 # del con[index]
1989 # del other_nets[k]
1990 #1.7: Check external_ports are present at database table datacenter_nets
1991 for k,net in other_nets.items():
1992 error_pos = "'topology':'nodes':'" + k + "'"
1993 if net['external']==False:
1994 if 'name' not in net:
1995 net['name']=k
1996 if 'model' not in net:
1997 raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request)
1998 if net['model']=='bridge_net':
1999 net['type']='bridge';
2000 elif net['model']=='dataplane_net':
2001 net['type']='data';
2002 else:
2003 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found)
2004 else: #external
2005 #IF we do not want to check that external network exist at datacenter
2006 pass
2007 #ELSE
2008 # error_text = ""
2009 # WHERE_={}
2010 # if 'net_id' in net:
2011 # error_text += " 'net_id' " + net['net_id']
2012 # WHERE_['uuid'] = net['net_id']
2013 # if 'model' in net:
2014 # error_text += " 'model' " + net['model']
2015 # WHERE_['name'] = net['model']
2016 # if len(WHERE_) == 0:
2017 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2018 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2019 # FROM='datacenter_nets', WHERE=WHERE_ )
2020 # if r<0:
2021 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2022 # elif r==0:
2023 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2024 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2025 # elif r>1:
2026 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2027 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2028 # other_nets[k].update(net_db[0])
2029 #ENDIF
2030 net_list={}
2031 net_nb=0 #Number of nets
2032 for con in conections_list:
2033 #check if this is connected to a external net
2034 other_net_index=-1
2035 #print
2036 #print "con", con
2037 for index in range(0,len(con)):
2038 #check if this is connected to a external net
2039 for net_key in other_nets.keys():
2040 if con[index][0]==net_key:
2041 if other_net_index>=0:
2042 error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key)
2043 #print "nfvo.new_scenario " + error_text
2044 raise NfvoException(error_text, httperrors.Bad_Request)
2045 else:
2046 other_net_index = index
2047 net_target = net_key
2048 break
2049 #print "other_net_index", other_net_index
2050 try:
2051 if other_net_index>=0:
2052 del con[other_net_index]
2053 #IF we do not want to check that external network exist at datacenter
2054 if other_nets[net_target]['external'] :
2055 if "name" not in other_nets[net_target]:
2056 other_nets[net_target]['name'] = other_nets[net_target]['model']
2057 if other_nets[net_target]["type"] == "external_network":
2058 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2059 other_nets[net_target]["type"] = "data"
2060 else:
2061 other_nets[net_target]["type"] = "bridge"
2062 #ELSE
2063 # if other_nets[net_target]['external'] :
2064 # 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
2065 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2066 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2067 # print "nfvo.new_scenario " + error_text
2068 # return -httperrors.Bad_Request, error_text
2069 #ENDIF
2070 for iface in con:
2071 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2072 else:
2073 #create a net
2074 net_type_bridge=False
2075 net_type_data=False
2076 net_target = "__-__net"+str(net_nb)
2077 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
2078 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
2079 'external':False}
2080 for iface in con:
2081 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2082 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2083 if iface_type=='mgmt' or iface_type=='bridge':
2084 net_type_bridge = True
2085 else:
2086 net_type_data = True
2087 if net_type_bridge and net_type_data:
2088 error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
2089 #print "nfvo.new_scenario " + error_text
2090 raise NfvoException(error_text, httperrors.Bad_Request)
2091 elif net_type_bridge:
2092 type_='bridge'
2093 else:
2094 type_='data' if len(con)>2 else 'ptp'
2095 net_list[net_target]['type'] = type_
2096 net_nb+=1
2097 except Exception:
2098 error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
2099 #print "nfvo.new_scenario " + error_text
2100 #raise e
2101 raise NfvoException(error_text, httperrors.Bad_Request)
2102
2103 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2104 #1.8.1 obtain management net
2105 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
2106 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
2107 #1.8.2 check all interfaces from all vnfs
2108 if len(mgmt_net)>0:
2109 add_mgmt_net = False
2110 for vnf in vnfs.values():
2111 for iface in vnf['ifaces'].values():
2112 if iface['type']=='mgmt' and 'net_key' not in iface:
2113 #iface not connected
2114 iface['net_key'] = 'mgmt'
2115 add_mgmt_net = True
2116 if add_mgmt_net and 'mgmt' not in net_list:
2117 net_list['mgmt']=mgmt_net[0]
2118 net_list['mgmt']['external']=True
2119 net_list['mgmt']['graph']={'visible':False}
2120
2121 net_list.update(other_nets)
2122 #print
2123 #print 'net_list', net_list
2124 #print
2125 #print 'vnfs', vnfs
2126 #print
2127
2128 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2129 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
2130 'tenant_id':tenant_id, 'name':topo['name'],
2131 'description':topo.get('description',topo['name']),
2132 'public': topo.get('public', False)
2133 })
2134
2135 return c
2136
2137
2138 @deprecated("Use new_nsd_v3")
2139 def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2140 """ This creates a new scenario for version 0.2 and 0.3"""
2141 scenario = scenario_dict["scenario"]
2142 if tenant_id != "any":
2143 check_tenant(mydb, tenant_id)
2144 if "tenant_id" in scenario:
2145 if scenario["tenant_id"] != tenant_id:
2146 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2147 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2148 scenario["tenant_id"], tenant_id), httperrors.Unauthorized)
2149 else:
2150 tenant_id=None
2151
2152 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2153 for name,vnf in scenario["vnfs"].iteritems():
2154 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
2155 error_text = ""
2156 error_pos = "'scenario':'vnfs':'" + name + "'"
2157 if 'vnf_id' in vnf:
2158 error_text += " 'vnf_id' " + vnf['vnf_id']
2159 where['uuid'] = vnf['vnf_id']
2160 if 'vnf_name' in vnf:
2161 error_text += " 'vnf_name' " + vnf['vnf_name']
2162 where['name'] = vnf['vnf_name']
2163 if len(where) == 1:
2164 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request)
2165 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
2166 FROM='vnfs',
2167 WHERE=where)
2168 if len(vnf_db) == 0:
2169 raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
2170 elif len(vnf_db) > 1:
2171 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
2172 vnf['uuid'] = vnf_db[0]['uuid']
2173 vnf['description'] = vnf_db[0]['description']
2174 vnf['ifaces'] = {}
2175 # get external interfaces
2176 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2177 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2178 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
2179 for ext_iface in ext_ifaces:
2180 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2181 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2182
2183 # 2: Insert net_key and ip_address at every vnf interface
2184 for net_name, net in scenario["networks"].items():
2185 net_type_bridge = False
2186 net_type_data = False
2187 for iface_dict in net["interfaces"]:
2188 if version == "0.2":
2189 temp_dict = iface_dict
2190 ip_address = None
2191 elif version == "0.3":
2192 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2193 ip_address = iface_dict.get('ip_address', None)
2194 for vnf, iface in temp_dict.items():
2195 if vnf not in scenario["vnfs"]:
2196 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2197 net_name, vnf)
2198 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2199 raise NfvoException(error_text, httperrors.Not_Found)
2200 if iface not in scenario["vnfs"][vnf]['ifaces']:
2201 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2202 .format(net_name, iface)
2203 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2204 raise NfvoException(error_text, httperrors.Bad_Request)
2205 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
2206 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2207 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2208 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2209 raise NfvoException(error_text, httperrors.Bad_Request)
2210 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
2211 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
2212 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
2213 if iface_type == 'mgmt' or iface_type == 'bridge':
2214 net_type_bridge = True
2215 else:
2216 net_type_data = True
2217
2218 if net_type_bridge and net_type_data:
2219 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2220 .format(net_name)
2221 # logger.debug("nfvo.new_scenario " + error_text)
2222 raise NfvoException(error_text, httperrors.Bad_Request)
2223 elif net_type_bridge:
2224 type_ = 'bridge'
2225 else:
2226 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2227
2228 if net.get("implementation"): # for v0.3
2229 if type_ == "bridge" and net["implementation"] == "underlay":
2230 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2231 "'network':'{}'".format(net_name)
2232 # logger.debug(error_text)
2233 raise NfvoException(error_text, httperrors.Bad_Request)
2234 elif type_ != "bridge" and net["implementation"] == "overlay":
2235 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2236 "'network':'{}'".format(net_name)
2237 # logger.debug(error_text)
2238 raise NfvoException(error_text, httperrors.Bad_Request)
2239 net.pop("implementation")
2240 if "type" in net and version == "0.3": # for v0.3
2241 if type_ == "data" and net["type"] == "e-line":
2242 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2243 "'e-line' at 'network':'{}'".format(net_name)
2244 # logger.debug(error_text)
2245 raise NfvoException(error_text, httperrors.Bad_Request)
2246 elif type_ == "ptp" and net["type"] == "e-lan":
2247 type_ = "data"
2248
2249 net['type'] = type_
2250 net['name'] = net_name
2251 net['external'] = net.get('external', False)
2252
2253 # 3: insert at database
2254 scenario["nets"] = scenario["networks"]
2255 scenario['tenant_id'] = tenant_id
2256 scenario_id = mydb.new_scenario(scenario)
2257 return scenario_id
2258
2259
2260 def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2261 """
2262 Parses an OSM IM nsd_catalog and insert at DB
2263 :param mydb:
2264 :param tenant_id:
2265 :param nsd_descriptor:
2266 :return: The list of created NSD ids
2267 """
2268 try:
2269 mynsd = nsd_catalog.nsd()
2270 try:
2271 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True)
2272 except Exception as e:
2273 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request)
2274 db_scenarios = []
2275 db_sce_nets = []
2276 db_sce_vnfs = []
2277 db_sce_interfaces = []
2278 db_sce_vnffgs = []
2279 db_sce_rsps = []
2280 db_sce_rsp_hops = []
2281 db_sce_classifiers = []
2282 db_sce_classifier_matches = []
2283 db_ip_profiles = []
2284 db_ip_profiles_index = 0
2285 uuid_list = []
2286 nsd_uuid_list = []
2287 for nsd_yang in mynsd.nsd_catalog.nsd.itervalues():
2288 nsd = nsd_yang.get()
2289
2290 # table scenarios
2291 scenario_uuid = str(uuid4())
2292 uuid_list.append(scenario_uuid)
2293 nsd_uuid_list.append(scenario_uuid)
2294 db_scenario = {
2295 "uuid": scenario_uuid,
2296 "osm_id": get_str(nsd, "id", 255),
2297 "name": get_str(nsd, "name", 255),
2298 "description": get_str(nsd, "description", 255),
2299 "tenant_id": tenant_id,
2300 "vendor": get_str(nsd, "vendor", 255),
2301 "short_name": get_str(nsd, "short-name", 255),
2302 "descriptor": str(nsd_descriptor)[:60000],
2303 }
2304 db_scenarios.append(db_scenario)
2305
2306 # table sce_vnfs (constituent-vnfd)
2307 vnf_index2scevnf_uuid = {}
2308 vnf_index2vnf_uuid = {}
2309 for vnf in nsd.get("constituent-vnfd").itervalues():
2310 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2311 'tenant_id': tenant_id})
2312 if not existing_vnf:
2313 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2314 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2315 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2316 httperrors.Bad_Request)
2317 sce_vnf_uuid = str(uuid4())
2318 uuid_list.append(sce_vnf_uuid)
2319 db_sce_vnf = {
2320 "uuid": sce_vnf_uuid,
2321 "scenario_id": scenario_uuid,
2322 # "name": get_str(vnf, "member-vnf-index", 255),
2323 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
2324 "vnf_id": existing_vnf[0]["uuid"],
2325 "member_vnf_index": str(vnf["member-vnf-index"]),
2326 # TODO 'start-by-default': True
2327 }
2328 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2329 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
2330 db_sce_vnfs.append(db_sce_vnf)
2331
2332 # table ip_profiles (ip-profiles)
2333 ip_profile_name2db_table_index = {}
2334 for ip_profile in nsd.get("ip-profiles").itervalues():
2335 db_ip_profile = {
2336 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2337 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2338 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2339 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2340 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2341 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2342 }
2343 dns_list = []
2344 for dns in ip_profile["ip-profile-params"]["dns-server"].itervalues():
2345 dns_list.append(str(dns.get("address")))
2346 db_ip_profile["dns_address"] = ";".join(dns_list)
2347 if ip_profile["ip-profile-params"].get('security-group'):
2348 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2349 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2350 db_ip_profiles_index += 1
2351 db_ip_profiles.append(db_ip_profile)
2352
2353 # table sce_nets (internal-vld)
2354 for vld in nsd.get("vld").itervalues():
2355 sce_net_uuid = str(uuid4())
2356 uuid_list.append(sce_net_uuid)
2357 db_sce_net = {
2358 "uuid": sce_net_uuid,
2359 "name": get_str(vld, "name", 255),
2360 "scenario_id": scenario_uuid,
2361 # "type": #TODO
2362 "multipoint": not vld.get("type") == "ELINE",
2363 "osm_id": get_str(vld, "id", 255),
2364 # "external": #TODO
2365 "description": get_str(vld, "description", 255),
2366 }
2367 # guess type of network
2368 if vld.get("mgmt-network"):
2369 db_sce_net["type"] = "bridge"
2370 db_sce_net["external"] = True
2371 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2372 db_sce_net["type"] = "data"
2373 else:
2374 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2375 db_sce_net["type"] = None
2376 db_sce_nets.append(db_sce_net)
2377
2378 # ip-profile, link db_ip_profile with db_sce_net
2379 if vld.get("ip-profile-ref"):
2380 ip_profile_name = vld.get("ip-profile-ref")
2381 if ip_profile_name not in ip_profile_name2db_table_index:
2382 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2383 " Reference to a non-existing 'ip_profiles'".format(
2384 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2385 httperrors.Bad_Request)
2386 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
2387 elif vld.get("vim-network-name"):
2388 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
2389
2390 # table sce_interfaces (vld:vnfd-connection-point-ref)
2391 for iface in vld.get("vnfd-connection-point-ref").itervalues():
2392 vnf_index = str(iface['member-vnf-index-ref'])
2393 # check correct parameters
2394 if vnf_index not in vnf_index2vnf_uuid:
2395 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2396 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2397 "'nsd':'constituent-vnfd'".format(
2398 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2399 httperrors.Bad_Request)
2400
2401 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
2402 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2403 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2404 'external_name': get_str(iface, "vnfd-connection-point-ref",
2405 255)})
2406 if not existing_ifaces:
2407 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2408 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2409 "connection-point name at VNFD '{}'".format(
2410 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2411 str(iface.get("vnfd-id-ref"))[:255]),
2412 httperrors.Bad_Request)
2413 interface_uuid = existing_ifaces[0]["uuid"]
2414 if existing_ifaces[0]["iface_type"] == "data":
2415 db_sce_net["type"] = "data"
2416 sce_interface_uuid = str(uuid4())
2417 uuid_list.append(sce_net_uuid)
2418 iface_ip_address = None
2419 if iface.get("ip-address"):
2420 iface_ip_address = str(iface.get("ip-address"))
2421 db_sce_interface = {
2422 "uuid": sce_interface_uuid,
2423 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2424 "sce_net_id": sce_net_uuid,
2425 "interface_id": interface_uuid,
2426 "ip_address": iface_ip_address,
2427 }
2428 db_sce_interfaces.append(db_sce_interface)
2429 if not db_sce_net["type"]:
2430 db_sce_net["type"] = "bridge"
2431
2432 # table sce_vnffgs (vnffgd)
2433 for vnffg in nsd.get("vnffgd").itervalues():
2434 sce_vnffg_uuid = str(uuid4())
2435 uuid_list.append(sce_vnffg_uuid)
2436 db_sce_vnffg = {
2437 "uuid": sce_vnffg_uuid,
2438 "name": get_str(vnffg, "name", 255),
2439 "scenario_id": scenario_uuid,
2440 "vendor": get_str(vnffg, "vendor", 255),
2441 "description": get_str(vld, "description", 255),
2442 }
2443 db_sce_vnffgs.append(db_sce_vnffg)
2444
2445 # deal with rsps
2446 for rsp in vnffg.get("rsp").itervalues():
2447 sce_rsp_uuid = str(uuid4())
2448 uuid_list.append(sce_rsp_uuid)
2449 db_sce_rsp = {
2450 "uuid": sce_rsp_uuid,
2451 "name": get_str(rsp, "name", 255),
2452 "sce_vnffg_id": sce_vnffg_uuid,
2453 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2454 }
2455 db_sce_rsps.append(db_sce_rsp)
2456 for iface in rsp.get("vnfd-connection-point-ref").itervalues():
2457 vnf_index = str(iface['member-vnf-index-ref'])
2458 if_order = int(iface['order'])
2459 # check correct parameters
2460 if vnf_index not in vnf_index2vnf_uuid:
2461 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2462 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2463 "'nsd':'constituent-vnfd'".format(
2464 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
2465 httperrors.Bad_Request)
2466
2467 ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2468 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2469 WHERE={
2470 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2471 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
2472 255)})
2473 if not ingress_existing_ifaces:
2474 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2475 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2476 "connection-point name at VNFD '{}'".format(
2477 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
2478 str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
2479
2480 egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2481 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2482 WHERE={
2483 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2484 'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
2485 255)})
2486 if not egress_existing_ifaces:
2487 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2488 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2489 "connection-point name at VNFD '{}'".format(
2490 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
2491 str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
2492
2493 ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
2494 egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
2495 sce_rsp_hop_uuid = str(uuid4())
2496 uuid_list.append(sce_rsp_hop_uuid)
2497 db_sce_rsp_hop = {
2498 "uuid": sce_rsp_hop_uuid,
2499 "if_order": if_order,
2500 "ingress_interface_id": ingress_interface_uuid,
2501 "egress_interface_id": egress_interface_uuid,
2502 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2503 "sce_rsp_id": sce_rsp_uuid,
2504 }
2505 db_sce_rsp_hops.append(db_sce_rsp_hop)
2506
2507 # deal with classifiers
2508 for classifier in vnffg.get("classifier").itervalues():
2509 sce_classifier_uuid = str(uuid4())
2510 uuid_list.append(sce_classifier_uuid)
2511
2512 # source VNF
2513 vnf_index = str(classifier['member-vnf-index-ref'])
2514 if vnf_index not in vnf_index2vnf_uuid:
2515 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2516 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2517 "'nsd':'constituent-vnfd'".format(
2518 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
2519 httperrors.Bad_Request)
2520 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2521 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2522 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2523 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2524 255)})
2525 if not existing_ifaces:
2526 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2527 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2528 "connection-point name at VNFD '{}'".format(
2529 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2530 str(iface.get("vnfd-id-ref"))[:255]),
2531 httperrors.Bad_Request)
2532 interface_uuid = existing_ifaces[0]["uuid"]
2533
2534 db_sce_classifier = {
2535 "uuid": sce_classifier_uuid,
2536 "name": get_str(classifier, "name", 255),
2537 "sce_vnffg_id": sce_vnffg_uuid,
2538 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2539 "interface_id": interface_uuid,
2540 }
2541 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2542 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2543 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2544 db_sce_classifiers.append(db_sce_classifier)
2545
2546 for match in classifier.get("match-attributes").itervalues():
2547 sce_classifier_match_uuid = str(uuid4())
2548 uuid_list.append(sce_classifier_match_uuid)
2549 db_sce_classifier_match = {
2550 "uuid": sce_classifier_match_uuid,
2551 "ip_proto": get_str(match, "ip-proto", 2),
2552 "source_ip": get_str(match, "source-ip-address", 16),
2553 "destination_ip": get_str(match, "destination-ip-address", 16),
2554 "source_port": get_str(match, "source-port", 5),
2555 "destination_port": get_str(match, "destination-port", 5),
2556 "sce_classifier_id": sce_classifier_uuid,
2557 }
2558 db_sce_classifier_matches.append(db_sce_classifier_match)
2559 # TODO: vnf/cp keys
2560
2561 # remove unneeded id's in sce_rsps
2562 for rsp in db_sce_rsps:
2563 rsp.pop('id')
2564
2565 db_tables = [
2566 {"scenarios": db_scenarios},
2567 {"sce_nets": db_sce_nets},
2568 {"ip_profiles": db_ip_profiles},
2569 {"sce_vnfs": db_sce_vnfs},
2570 {"sce_interfaces": db_sce_interfaces},
2571 {"sce_vnffgs": db_sce_vnffgs},
2572 {"sce_rsps": db_sce_rsps},
2573 {"sce_rsp_hops": db_sce_rsp_hops},
2574 {"sce_classifiers": db_sce_classifiers},
2575 {"sce_classifier_matches": db_sce_classifier_matches},
2576 ]
2577
2578 logger.debug("new_nsd_v3 done: %s",
2579 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2580 mydb.new_rows(db_tables, uuid_list)
2581 return nsd_uuid_list
2582 except NfvoException:
2583 raise
2584 except Exception as e:
2585 logger.error("Exception {}".format(e))
2586 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2587
2588
2589 def edit_scenario(mydb, tenant_id, scenario_id, data):
2590 data["uuid"] = scenario_id
2591 data["tenant_id"] = tenant_id
2592 c = mydb.edit_scenario( data )
2593 return c
2594
2595
2596 @deprecated("Use create_instance")
2597 def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
2598 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2599 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2600 vims = {datacenter_id: myvim}
2601 myvim_tenant = myvim['tenant_id']
2602 datacenter_name = myvim['name']
2603
2604 rollbackList=[]
2605 try:
2606 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2607 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
2608 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
2609 scenarioDict['datacenter_id'] = datacenter_id
2610 #print '================scenarioDict======================='
2611 #print json.dumps(scenarioDict, indent=4)
2612 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2613
2614 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2615 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2616
2617 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2618 auxNetDict['scenario'] = {}
2619
2620 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2621 for sce_net in scenarioDict['nets']:
2622 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2623
2624 myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
2625 myNetName = myNetName[0:255] #limit length
2626 myNetType = sce_net['type']
2627 myNetDict = {}
2628 myNetDict["name"] = myNetName
2629 myNetDict["type"] = myNetType
2630 myNetDict["tenant_id"] = myvim_tenant
2631 myNetIPProfile = sce_net.get('ip_profile', None)
2632 #TODO:
2633 #We should use the dictionary as input parameter for new_network
2634 #print myNetDict
2635 if not sce_net["external"]:
2636 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
2637 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2638 sce_net['vim_id'] = network_id
2639 auxNetDict['scenario'][sce_net['uuid']] = network_id
2640 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2641 sce_net["created"] = True
2642 else:
2643 if sce_net['vim_id'] == None:
2644 error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
2645 _, message = rollback(mydb, vims, rollbackList)
2646 logger.error("nfvo.start_scenario: %s", error_text)
2647 raise NfvoException(error_text, httperrors.Bad_Request)
2648 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2649 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
2650
2651 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2652 #For each vnf net, we create it and we add it to instanceNetlist.
2653
2654 for sce_vnf in scenarioDict['vnfs']:
2655 for net in sce_vnf['nets']:
2656 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2657
2658 myNetName = "%s.%s" % (instance_scenario_name,net['name'])
2659 myNetName = myNetName[0:255] #limit length
2660 myNetType = net['type']
2661 myNetDict = {}
2662 myNetDict["name"] = myNetName
2663 myNetDict["type"] = myNetType
2664 myNetDict["tenant_id"] = myvim_tenant
2665 myNetIPProfile = net.get('ip_profile', None)
2666 #print myNetDict
2667 #TODO:
2668 #We should use the dictionary as input parameter for new_network
2669 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile)
2670 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2671 net['vim_id'] = network_id
2672 if sce_vnf['uuid'] not in auxNetDict:
2673 auxNetDict[sce_vnf['uuid']] = {}
2674 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2675 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2676 net["created"] = True
2677
2678 #print "auxNetDict:"
2679 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2680
2681 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2682 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2683 i = 0
2684 for sce_vnf in scenarioDict['vnfs']:
2685 vnf_availability_zones = []
2686 for vm in sce_vnf['vms']:
2687 vm_av = vm.get('availability_zone')
2688 if vm_av and vm_av not in vnf_availability_zones:
2689 vnf_availability_zones.append(vm_av)
2690
2691 # check if there is enough availability zones available at vim level.
2692 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2693 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2694 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
2695
2696 for vm in sce_vnf['vms']:
2697 i += 1
2698 myVMDict = {}
2699 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2700 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
2701 #myVMDict['description'] = vm['description']
2702 myVMDict['description'] = myVMDict['name'][0:99]
2703 if not startvms:
2704 myVMDict['start'] = "no"
2705 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2706 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2707
2708 #create image at vim in case it not exist
2709 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
2710 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
2711 vm['vim_image_id'] = image_id
2712
2713 #create flavor at vim in case it not exist
2714 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
2715 if flavor_dict['extended']!=None:
2716 flavor_dict['extended']= yaml.load(flavor_dict['extended'])
2717 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
2718 vm['vim_flavor_id'] = flavor_id
2719
2720
2721 myVMDict['imageRef'] = vm['vim_image_id']
2722 myVMDict['flavorRef'] = vm['vim_flavor_id']
2723 myVMDict['networks'] = []
2724 for iface in vm['interfaces']:
2725 netDict = {}
2726 if iface['type']=="data":
2727 netDict['type'] = iface['model']
2728 elif "model" in iface and iface["model"]!=None:
2729 netDict['model']=iface['model']
2730 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2731 #discover type of interface looking at flavor
2732 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2733 for flavor_iface in numa.get('interfaces',[]):
2734 if flavor_iface.get('name') == iface['internal_name']:
2735 if flavor_iface['dedicated'] == 'yes':
2736 netDict['type']="PF" #passthrough
2737 elif flavor_iface['dedicated'] == 'no':
2738 netDict['type']="VF" #siov
2739 elif flavor_iface['dedicated'] == 'yes:sriov':
2740 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2741 netDict["mac_address"] = flavor_iface.get("mac_address")
2742 break;
2743 netDict["use"]=iface['type']
2744 if netDict["use"]=="data" and not netDict.get("type"):
2745 #print "netDict", netDict
2746 #print "iface", iface
2747 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'])
2748 if flavor_dict.get('extended')==None:
2749 raise NfvoException(e_text + "After database migration some information is not available. \
2750 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
2751 else:
2752 raise NfvoException(e_text, httperrors.Internal_Server_Error)
2753 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2754 netDict["type"]="virtual"
2755 if "vpci" in iface and iface["vpci"] is not None:
2756 netDict['vpci'] = iface['vpci']
2757 if "mac" in iface and iface["mac"] is not None:
2758 netDict['mac_address'] = iface['mac']
2759 if "port-security" in iface and iface["port-security"] is not None:
2760 netDict['port_security'] = iface['port-security']
2761 if "floating-ip" in iface and iface["floating-ip"] is not None:
2762 netDict['floating_ip'] = iface['floating-ip']
2763 netDict['name'] = iface['internal_name']
2764 if iface['net_id'] is None:
2765 for vnf_iface in sce_vnf["interfaces"]:
2766 #print iface
2767 #print vnf_iface
2768 if vnf_iface['interface_id']==iface['uuid']:
2769 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2770 break
2771 else:
2772 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2773 #skip bridge ifaces not connected to any net
2774 #if 'net_id' not in netDict or netDict['net_id']==None:
2775 # continue
2776 myVMDict['networks'].append(netDict)
2777 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2778 #print myVMDict['name']
2779 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2780 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2781 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2782
2783 if 'availability_zone' in myVMDict:
2784 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
2785 else:
2786 av_index = None
2787
2788 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2789 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2790 availability_zone_index=av_index,
2791 availability_zone_list=vnf_availability_zones)
2792 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2793 vm['vim_id'] = vm_id
2794 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2795 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2796 for net in myVMDict['networks']:
2797 if "vim_id" in net:
2798 for iface in vm['interfaces']:
2799 if net["name"]==iface["internal_name"]:
2800 iface["vim_id"]=net["vim_id"]
2801 break
2802
2803 logger.debug("start scenario Deployment done")
2804 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2805 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2806 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2807 return mydb.get_instance_scenario(instance_id)
2808
2809 except (db_base_Exception, vimconn.vimconnException) as e:
2810 _, message = rollback(mydb, vims, rollbackList)
2811 if isinstance(e, db_base_Exception):
2812 error_text = "Exception at database"
2813 else:
2814 error_text = "Exception at VIM"
2815 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2816 #logger.error("start_scenario %s", error_text)
2817 raise NfvoException(error_text, e.http_code)
2818
2819 def unify_cloud_config(cloud_config_preserve, cloud_config):
2820 """ join the cloud config information into cloud_config_preserve.
2821 In case of conflict cloud_config_preserve preserves
2822 None is allowed
2823 """
2824 if not cloud_config_preserve and not cloud_config:
2825 return None
2826
2827 new_cloud_config = {"key-pairs":[], "users":[]}
2828 # key-pairs
2829 if cloud_config_preserve:
2830 for key in cloud_config_preserve.get("key-pairs", () ):
2831 if key not in new_cloud_config["key-pairs"]:
2832 new_cloud_config["key-pairs"].append(key)
2833 if cloud_config:
2834 for key in cloud_config.get("key-pairs", () ):
2835 if key not in new_cloud_config["key-pairs"]:
2836 new_cloud_config["key-pairs"].append(key)
2837 if not new_cloud_config["key-pairs"]:
2838 del new_cloud_config["key-pairs"]
2839
2840 # users
2841 if cloud_config:
2842 new_cloud_config["users"] += cloud_config.get("users", () )
2843 if cloud_config_preserve:
2844 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
2845 index_to_delete = []
2846 users = new_cloud_config.get("users", [])
2847 for index0 in range(0,len(users)):
2848 if index0 in index_to_delete:
2849 continue
2850 for index1 in range(index0+1,len(users)):
2851 if index1 in index_to_delete:
2852 continue
2853 if users[index0]["name"] == users[index1]["name"]:
2854 index_to_delete.append(index1)
2855 for key in users[index1].get("key-pairs",()):
2856 if "key-pairs" not in users[index0]:
2857 users[index0]["key-pairs"] = [key]
2858 elif key not in users[index0]["key-pairs"]:
2859 users[index0]["key-pairs"].append(key)
2860 index_to_delete.sort(reverse=True)
2861 for index in index_to_delete:
2862 del users[index]
2863 if not new_cloud_config["users"]:
2864 del new_cloud_config["users"]
2865
2866 #boot-data-drive
2867 if cloud_config and cloud_config.get("boot-data-drive") != None:
2868 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2869 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2870 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2871
2872 # user-data
2873 new_cloud_config["user-data"] = []
2874 if cloud_config and cloud_config.get("user-data"):
2875 if isinstance(cloud_config["user-data"], list):
2876 new_cloud_config["user-data"] += cloud_config["user-data"]
2877 else:
2878 new_cloud_config["user-data"].append(cloud_config["user-data"])
2879 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2880 if isinstance(cloud_config_preserve["user-data"], list):
2881 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2882 else:
2883 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2884 if not new_cloud_config["user-data"]:
2885 del new_cloud_config["user-data"]
2886
2887 # config files
2888 new_cloud_config["config-files"] = []
2889 if cloud_config and cloud_config.get("config-files") != None:
2890 new_cloud_config["config-files"] += cloud_config["config-files"]
2891 if cloud_config_preserve:
2892 for file in cloud_config_preserve.get("config-files", ()):
2893 for index in range(0, len(new_cloud_config["config-files"])):
2894 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2895 new_cloud_config["config-files"][index] = file
2896 break
2897 else:
2898 new_cloud_config["config-files"].append(file)
2899 if not new_cloud_config["config-files"]:
2900 del new_cloud_config["config-files"]
2901 return new_cloud_config
2902
2903
2904 def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
2905 datacenter_id = None
2906 datacenter_name = None
2907 thread = None
2908 try:
2909 if datacenter_tenant_id:
2910 thread_id = datacenter_tenant_id
2911 thread = vim_threads["running"].get(datacenter_tenant_id)
2912 else:
2913 where_={"td.nfvo_tenant_id": tenant_id}
2914 if datacenter_id_name:
2915 if utils.check_valid_uuid(datacenter_id_name):
2916 datacenter_id = datacenter_id_name
2917 where_["dt.datacenter_id"] = datacenter_id
2918 else:
2919 datacenter_name = datacenter_id_name
2920 where_["d.name"] = datacenter_name
2921 if datacenter_tenant_id:
2922 where_["dt.uuid"] = datacenter_tenant_id
2923 datacenters = mydb.get_rows(
2924 SELECT=("dt.uuid as datacenter_tenant_id",),
2925 FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2926 "join datacenters as d on d.uuid=dt.datacenter_id",
2927 WHERE=where_)
2928 if len(datacenters) > 1:
2929 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
2930 elif datacenters:
2931 thread_id = datacenters[0]["datacenter_tenant_id"]
2932 thread = vim_threads["running"].get(thread_id)
2933 if not thread:
2934 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
2935 return thread_id, thread
2936 except db_base_Exception as e:
2937 raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
2938
2939
2940 def get_datacenter_uuid(mydb, tenant_id, datacenter_id_name):
2941 WHERE_dict={}
2942 if utils.check_valid_uuid(datacenter_id_name):
2943 WHERE_dict['d.uuid'] = datacenter_id_name
2944 else:
2945 WHERE_dict['d.name'] = datacenter_id_name
2946
2947 if tenant_id:
2948 WHERE_dict['nfvo_tenant_id'] = tenant_id
2949 from_= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2950 " dt on td.datacenter_tenant_id=dt.uuid"
2951 else:
2952 from_ = 'datacenters as d'
2953 vimaccounts = mydb.get_rows(FROM=from_, SELECT=("d.uuid as uuid, d.name as name",), WHERE=WHERE_dict )
2954 if len(vimaccounts) == 0:
2955 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
2956 elif len(vimaccounts)>1:
2957 #print "nfvo.datacenter_action() error. Several datacenters found"
2958 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
2959 return vimaccounts[0]["uuid"], vimaccounts[0]["name"]
2960
2961
2962 def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
2963 datacenter_id = None
2964 datacenter_name = None
2965 if datacenter_id_name:
2966 if utils.check_valid_uuid(datacenter_id_name):
2967 datacenter_id = datacenter_id_name
2968 else:
2969 datacenter_name = datacenter_id_name
2970 vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
2971 if len(vims) == 0:
2972 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found)
2973 elif len(vims)>1:
2974 #print "nfvo.datacenter_action() error. Several datacenters found"
2975 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
2976 return vims.keys()[0], vims.values()[0]
2977
2978
2979 def update(d, u):
2980 """Takes dict d and updates it with the values in dict u.
2981 It merges all depth levels"""
2982 for k, v in u.iteritems():
2983 if isinstance(v, collections.Mapping):
2984 r = update(d.get(k, {}), v)
2985 d[k] = r
2986 else:
2987 d[k] = u[k]
2988 return d
2989
2990
2991 def create_instance(mydb, tenant_id, instance_dict):
2992 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2993 # logger.debug("Creating instance...")
2994 scenario = instance_dict["scenario"]
2995
2996 # find main datacenter
2997 myvims = {}
2998 myvim_threads_id = {}
2999 datacenter = instance_dict.get("datacenter")
3000 default_wim_account = instance_dict.get("wim_account")
3001 default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
3002 myvims[default_datacenter_id] = vim
3003 myvim_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
3004 tenant = mydb.get_rows_by_id('nfvo_tenants', tenant_id)
3005 # myvim_tenant = myvim['tenant_id']
3006 rollbackList = []
3007
3008 # print "Checking that the scenario exists and getting the scenario dictionary"
3009 if isinstance(scenario, str):
3010 scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_vim_id=myvim_threads_id[default_datacenter_id],
3011 datacenter_id=default_datacenter_id)
3012 else:
3013 scenarioDict = scenario
3014 scenarioDict["uuid"] = None
3015
3016 # logger.debug(">>>>>> Dictionaries before merging")
3017 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3018 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
3019
3020 db_instance_vnfs = []
3021 db_instance_vms = []
3022 db_instance_interfaces = []
3023 db_instance_sfis = []
3024 db_instance_sfs = []
3025 db_instance_classifications = []
3026 db_instance_sfps = []
3027 db_ip_profiles = []
3028 db_vim_actions = []
3029 uuid_list = []
3030 task_index = 0
3031 instance_name = instance_dict["name"]
3032 instance_uuid = str(uuid4())
3033 uuid_list.append(instance_uuid)
3034 db_instance_scenario = {
3035 "uuid": instance_uuid,
3036 "name": instance_name,
3037 "tenant_id": tenant_id,
3038 "scenario_id": scenarioDict['uuid'],
3039 "datacenter_id": default_datacenter_id,
3040 # filled bellow 'datacenter_tenant_id'
3041 "description": instance_dict.get("description"),
3042 }
3043 if scenarioDict.get("cloud-config"):
3044 db_instance_scenario["cloud_config"] = yaml.safe_dump(scenarioDict["cloud-config"],
3045 default_flow_style=True, width=256)
3046 instance_action_id = get_task_id()
3047 db_instance_action = {
3048 "uuid": instance_action_id, # same uuid for the instance and the action on create
3049 "tenant_id": tenant_id,
3050 "instance_id": instance_uuid,
3051 "description": "CREATE",
3052 }
3053
3054 # Auxiliary dictionaries from x to y
3055 sce_net2instance = {}
3056 net2task_id = {'scenario': {}}
3057 # Mapping between local networks and WIMs
3058 wim_usage = {}
3059
3060 def ip_profile_IM2RO(ip_profile_im):
3061 # translate from input format to database format
3062 ip_profile_ro = {}
3063 if 'subnet-address' in ip_profile_im:
3064 ip_profile_ro['subnet_address'] = ip_profile_im['subnet-address']
3065 if 'ip-version' in ip_profile_im:
3066 ip_profile_ro['ip_version'] = ip_profile_im['ip-version']
3067 if 'gateway-address' in ip_profile_im:
3068 ip_profile_ro['gateway_address'] = ip_profile_im['gateway-address']
3069 if 'dns-address' in ip_profile_im:
3070 ip_profile_ro['dns_address'] = ip_profile_im['dns-address']
3071 if isinstance(ip_profile_ro['dns_address'], (list, tuple)):
3072 ip_profile_ro['dns_address'] = ";".join(ip_profile_ro['dns_address'])
3073 if 'dhcp' in ip_profile_im:
3074 ip_profile_ro['dhcp_start_address'] = ip_profile_im['dhcp'].get('start-address')
3075 ip_profile_ro['dhcp_enabled'] = ip_profile_im['dhcp'].get('enabled', True)
3076 ip_profile_ro['dhcp_count'] = ip_profile_im['dhcp'].get('count')
3077 return ip_profile_ro
3078
3079 # logger.debug("Creating instance from scenario-dict:\n%s",
3080 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3081 try:
3082 # 0 check correct parameters
3083 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
3084 for scenario_net in scenarioDict['nets']:
3085 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
3086 break
3087 else:
3088 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name),
3089 httperrors.Bad_Request)
3090 if "sites" not in net_instance_desc:
3091 net_instance_desc["sites"] = [ {} ]
3092 site_without_datacenter_field = False
3093 for site in net_instance_desc["sites"]:
3094 if site.get("datacenter"):
3095 site["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, site["datacenter"])
3096 if site["datacenter"] not in myvims:
3097 # Add this datacenter to myvims
3098 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
3099 myvims[d] = v
3100 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, site["datacenter"])
3101 site["datacenter"] = d # change name to id
3102 else:
3103 if site_without_datacenter_field:
3104 raise NfvoException("Found more than one entries without datacenter field at "
3105 "instance:networks:{}:sites".format(net_name), httperrors.Bad_Request)
3106 site_without_datacenter_field = True
3107 site["datacenter"] = default_datacenter_id # change name to id
3108
3109 for vnf_name, vnf_instance_desc in instance_dict.get("vnfs",{}).iteritems():
3110 for scenario_vnf in scenarioDict['vnfs']:
3111 if vnf_name == scenario_vnf['member_vnf_index'] or vnf_name == scenario_vnf['uuid'] or vnf_name == scenario_vnf['name']:
3112 break
3113 else:
3114 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name), httperrors.Bad_Request)
3115 if "datacenter" in vnf_instance_desc:
3116 # Add this datacenter to myvims
3117 vnf_instance_desc["datacenter"], _ = get_datacenter_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3118 if vnf_instance_desc["datacenter"] not in myvims:
3119 d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
3120 myvims[d] = v
3121 myvim_threads_id[d], _ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
3122 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
3123
3124 for net_id, net_instance_desc in vnf_instance_desc.get("networks", {}).iteritems():
3125 for scenario_net in scenario_vnf['nets']:
3126 if net_id == scenario_net['osm_id'] or net_id == scenario_net['uuid'] or net_id == scenario_net["name"]:
3127 break
3128 else:
3129 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id), httperrors.Bad_Request)
3130 if net_instance_desc.get("vim-network-name"):
3131 scenario_net["vim-network-name"] = net_instance_desc["vim-network-name"]
3132 if net_instance_desc.get("vim-network-id"):
3133 scenario_net["vim-network-id"] = net_instance_desc["vim-network-id"]
3134 if net_instance_desc.get("name"):
3135 scenario_net["name"] = net_instance_desc["name"]
3136 if 'ip-profile' in net_instance_desc:
3137 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3138 if 'ip_profile' not in scenario_net:
3139 scenario_net['ip_profile'] = ipprofile_db
3140 else:
3141 update(scenario_net['ip_profile'], ipprofile_db)
3142
3143 for vdu_id, vdu_instance_desc in vnf_instance_desc.get("vdus", {}).iteritems():
3144 for scenario_vm in scenario_vnf['vms']:
3145 if vdu_id == scenario_vm['osm_id'] or vdu_id == scenario_vm["name"]:
3146 break
3147 else:
3148 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
3149 scenario_vm["instance_parameters"] = vdu_instance_desc
3150 for iface_id, iface_instance_desc in vdu_instance_desc.get("interfaces", {}).iteritems():
3151 for scenario_interface in scenario_vm['interfaces']:
3152 if iface_id == scenario_interface['internal_name'] or iface_id == scenario_interface["external_name"]:
3153 scenario_interface.update(iface_instance_desc)
3154 break
3155 else:
3156 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id), httperrors.Bad_Request)
3157
3158 # 0.1 parse cloud-config parameters
3159 cloud_config = unify_cloud_config(instance_dict.get("cloud-config"), scenarioDict.get("cloud-config"))
3160
3161 # 0.2 merge instance information into scenario
3162 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3163 # However, this is not possible yet.
3164 for net_name, net_instance_desc in instance_dict.get("networks", {}).iteritems():
3165 for scenario_net in scenarioDict['nets']:
3166 if net_name == scenario_net.get("name") or net_name == scenario_net.get("osm_id") or net_name == scenario_net.get("uuid"):
3167 if "wim_account" in net_instance_desc and net_instance_desc["wim_account"] is not None:
3168 scenario_net["wim_account"] = net_instance_desc["wim_account"]
3169 if 'ip-profile' in net_instance_desc:
3170 ipprofile_db = ip_profile_IM2RO(net_instance_desc['ip-profile'])
3171 if 'ip_profile' not in scenario_net:
3172 scenario_net['ip_profile'] = ipprofile_db
3173 else:
3174 update(scenario_net['ip_profile'], ipprofile_db)
3175 for interface in net_instance_desc.get('interfaces', ()):
3176 if 'ip_address' in interface:
3177 for vnf in scenarioDict['vnfs']:
3178 if interface['vnf'] == vnf['name']:
3179 for vnf_interface in vnf['interfaces']:
3180 if interface['vnf_interface'] == vnf_interface['external_name']:
3181 vnf_interface['ip_address'] = interface['ip_address']
3182
3183 # logger.debug(">>>>>>>> Merged dictionary")
3184 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3185 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3186
3187 # 1. Creating new nets (sce_nets) in the VIM"
3188 number_mgmt_networks = 0
3189 db_instance_nets = []
3190 for sce_net in scenarioDict['nets']:
3191 sce_net_uuid = sce_net.get('uuid', sce_net["name"])
3192 # get involved datacenters where this network need to be created
3193 involved_datacenters = []
3194 for sce_vnf in scenarioDict.get("vnfs", ()):
3195 vnf_datacenter = sce_vnf.get("datacenter", default_datacenter_id)
3196 if vnf_datacenter in involved_datacenters:
3197 continue
3198 if sce_vnf.get("interfaces"):
3199 for sce_vnf_ifaces in sce_vnf["interfaces"]:
3200 if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
3201 involved_datacenters.append(vnf_datacenter)
3202 break
3203 if not involved_datacenters:
3204 involved_datacenters.append(default_datacenter_id)
3205 target_wim_account = sce_net.get("wim_account", default_wim_account)
3206
3207 # --> WIM
3208 # TODO: use this information during network creation
3209 wim_account_id = wim_account_name = None
3210 if len(involved_datacenters) > 1 and 'uuid' in sce_net:
3211 if target_wim_account is None or target_wim_account is True: # automatic selection of WIM
3212 # OBS: sce_net without uuid are used internally to VNFs
3213 # and the assumption is that VNFs will not be split among
3214 # different datacenters
3215 wim_account = wim_engine.find_suitable_wim_account(
3216 involved_datacenters, tenant_id)
3217 wim_account_id = wim_account['uuid']
3218 wim_account_name = wim_account['name']
3219 wim_usage[sce_net['uuid']] = wim_account_id
3220 elif isinstance(target_wim_account, str): # manual selection of WIM
3221 wim_account.persist.get_wim_account_by(target_wim_account, tenant_id)
3222 wim_account_id = wim_account['uuid']
3223 wim_account_name = wim_account['name']
3224 wim_usage[sce_net['uuid']] = wim_account_id
3225 else: # not WIM usage
3226 wim_usage[sce_net['uuid']] = False
3227 # <-- WIM
3228
3229 descriptor_net = {}
3230 if instance_dict.get("networks"):
3231 if sce_net.get("uuid") in instance_dict["networks"]:
3232 descriptor_net = instance_dict["networks"][sce_net["uuid"]]
3233 descriptor_net_name = sce_net["uuid"]
3234 elif sce_net.get("osm_id") in instance_dict["networks"]:
3235 descriptor_net = instance_dict["networks"][sce_net["osm_id"]]
3236 descriptor_net_name = sce_net["osm_id"]
3237 elif sce_net["name"] in instance_dict["networks"]:
3238 descriptor_net = instance_dict["networks"][sce_net["name"]]
3239 descriptor_net_name = sce_net["name"]
3240 net_name = descriptor_net.get("vim-network-name")
3241 # add datacenters from instantiation parameters
3242 if descriptor_net.get("sites"):
3243 for site in descriptor_net["sites"]:
3244 if site.get("datacenter") and site["datacenter"] not in involved_datacenters:
3245 involved_datacenters.append(site["datacenter"])
3246 sce_net2instance[sce_net_uuid] = {}
3247 net2task_id['scenario'][sce_net_uuid] = {}
3248
3249 use_network = None
3250 related_network = None
3251 if descriptor_net.get("use-network"):
3252 target_instance_nets = mydb.get_rows(
3253 SELECT="related",
3254 FROM="instance_nets",
3255 WHERE={"instance_scenario_id": descriptor_net["use-network"]["instance_scenario_id"],
3256 "osm_id": descriptor_net["use-network"]["osm_id"]},
3257 )
3258 if not target_instance_nets:
3259 raise NfvoException(
3260 "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name),
3261 httperrors.Bad_Request)
3262 else:
3263 use_network = target_instance_nets[0]["related"]
3264
3265 if sce_net["external"]:
3266 number_mgmt_networks += 1
3267
3268 for datacenter_id in involved_datacenters:
3269 netmap_use = None
3270 netmap_create = None
3271 if descriptor_net.get("sites"):
3272 for site in descriptor_net["sites"]:
3273 if site.get("datacenter") == datacenter_id:
3274 netmap_use = site.get("netmap-use")
3275 netmap_create = site.get("netmap-create")
3276 break
3277
3278 vim = myvims[datacenter_id]
3279 myvim_thread_id = myvim_threads_id[datacenter_id]
3280
3281 net_type = sce_net['type']
3282 net_vim_name = None
3283 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3284
3285 if not net_name:
3286 if sce_net["external"]:
3287 net_name = sce_net["name"]
3288 else:
3289 net_name = "{}-{}".format(instance_name, sce_net["name"])
3290 net_name = net_name[:255] # limit length
3291
3292 if netmap_use or netmap_create:
3293 create_network = False
3294 lookfor_network = False
3295 if netmap_use:
3296 lookfor_network = True
3297 if utils.check_valid_uuid(netmap_use):
3298 lookfor_filter["id"] = netmap_use
3299 else:
3300 lookfor_filter["name"] = netmap_use
3301 if netmap_create:
3302 create_network = True
3303 net_vim_name = net_name
3304 if isinstance(netmap_create, str):
3305 net_vim_name = netmap_create
3306 elif sce_net.get("vim_network_name"):
3307 create_network = False
3308 lookfor_network = True
3309 lookfor_filter["name"] = sce_net.get("vim_network_name")
3310 elif sce_net["external"]:
3311 if sce_net.get('vim_id'):
3312 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3313 create_network = False
3314 lookfor_network = True
3315 lookfor_filter["id"] = sce_net['vim_id']
3316 elif vim["config"].get("management_network_id") or vim["config"].get("management_network_name"):
3317 if number_mgmt_networks > 1:
3318 raise NfvoException("Found several VLD of type mgmt. "
3319 "You must concrete what vim-network must be use for each one",
3320 httperrors.Bad_Request)
3321 create_network = False
3322 lookfor_network = True
3323 if vim["config"].get("management_network_id"):
3324 lookfor_filter["id"] = vim["config"]["management_network_id"]
3325 else:
3326 lookfor_filter["name"] = vim["config"]["management_network_name"]
3327 else:
3328 # There is not a netmap, look at datacenter for a net with this name and create if not found
3329 create_network = True
3330 lookfor_network = True
3331 lookfor_filter["name"] = sce_net["name"]
3332 net_vim_name = sce_net["name"]
3333 else:
3334 net_vim_name = net_name
3335 create_network = True
3336 lookfor_network = False
3337
3338 task_extra = {}
3339 if create_network:
3340 task_action = "CREATE"
3341 task_extra["params"] = (net_vim_name, net_type, sce_net.get('ip_profile', None), wim_account_name)
3342 if lookfor_network:
3343 task_extra["find"] = (lookfor_filter,)
3344 elif lookfor_network:
3345 task_action = "FIND"
3346 task_extra["params"] = (lookfor_filter,)
3347
3348 # fill database content
3349 net_uuid = str(uuid4())
3350 uuid_list.append(net_uuid)
3351 sce_net2instance[sce_net_uuid][datacenter_id] = net_uuid
3352 if not related_network: # all db_instance_nets will have same related
3353 related_network = use_network or net_uuid
3354 db_net = {
3355 "uuid": net_uuid,
3356 "osm_id": sce_net.get("osm_id") or sce_net["name"],
3357 "related": related_network,
3358 'vim_net_id': None,
3359 "vim_name": net_vim_name,
3360 "instance_scenario_id": instance_uuid,
3361 "sce_net_id": sce_net.get("uuid"),
3362 "created": create_network,
3363 'datacenter_id': datacenter_id,
3364 'datacenter_tenant_id': myvim_thread_id,
3365 'status': 'BUILD' # if create_network else "ACTIVE"
3366 }
3367 db_instance_nets.append(db_net)
3368 db_vim_action = {
3369 "instance_action_id": instance_action_id,
3370 "status": "SCHEDULED",
3371 "task_index": task_index,
3372 "datacenter_vim_id": myvim_thread_id,
3373 "action": task_action,
3374 "item": "instance_nets",
3375 "item_id": net_uuid,
3376 "related": related_network,
3377 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
3378 }
3379 net2task_id['scenario'][sce_net_uuid][datacenter_id] = task_index
3380 task_index += 1
3381 db_vim_actions.append(db_vim_action)
3382
3383 if 'ip_profile' in sce_net:
3384 db_ip_profile={
3385 'instance_net_id': net_uuid,
3386 'ip_version': sce_net['ip_profile']['ip_version'],
3387 'subnet_address': sce_net['ip_profile']['subnet_address'],
3388 'gateway_address': sce_net['ip_profile']['gateway_address'],
3389 'dns_address': sce_net['ip_profile']['dns_address'],
3390 'dhcp_enabled': sce_net['ip_profile']['dhcp_enabled'],
3391 'dhcp_start_address': sce_net['ip_profile']['dhcp_start_address'],
3392 'dhcp_count': sce_net['ip_profile']['dhcp_count'],
3393 }
3394 db_ip_profiles.append(db_ip_profile)
3395
3396 # Create VNFs
3397 vnf_params = {
3398 "default_datacenter_id": default_datacenter_id,
3399 "myvim_threads_id": myvim_threads_id,
3400 "instance_uuid": instance_uuid,
3401 "instance_name": instance_name,
3402 "instance_action_id": instance_action_id,
3403 "myvims": myvims,
3404 "cloud_config": cloud_config,
3405 "RO_pub_key": tenant[0].get('RO_pub_key'),
3406 "instance_parameters": instance_dict,
3407 }
3408 vnf_params_out = {
3409 "task_index": task_index,
3410 "uuid_list": uuid_list,
3411 "db_instance_nets": db_instance_nets,
3412 "db_vim_actions": db_vim_actions,
3413 "db_ip_profiles": db_ip_profiles,
3414 "db_instance_vnfs": db_instance_vnfs,
3415 "db_instance_vms": db_instance_vms,
3416 "db_instance_interfaces": db_instance_interfaces,
3417 "net2task_id": net2task_id,
3418 "sce_net2instance": sce_net2instance,
3419 }
3420 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3421 for sce_vnf in scenarioDict.get('vnfs', ()): # sce_vnf_list:
3422 instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
3423 task_index = vnf_params_out["task_index"]
3424 uuid_list = vnf_params_out["uuid_list"]
3425
3426 # Create VNFFGs
3427 # task_depends_on = []
3428 for vnffg in scenarioDict.get('vnffgs', ()):
3429 for rsp in vnffg['rsps']:
3430 sfs_created = []
3431 for cp in rsp['connection_points']:
3432 count = mydb.get_rows(
3433 SELECT='vms.count',
3434 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3435 "on interfaces.uuid=h.ingress_interface_id",
3436 WHERE={'h.uuid': cp['uuid']})[0]['count']
3437 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == cp['sce_vnf_id']), None)
3438 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3439 dependencies = []
3440 for instance_vm in instance_vms:
3441 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3442 if action:
3443 dependencies.append(action['task_index'])
3444 # TODO: throw exception if count != len(instance_vms)
3445 # TODO: and action shouldn't ever be None
3446 sfis_created = []
3447 for i in range(count):
3448 # create sfis
3449 sfi_uuid = str(uuid4())
3450 extra_params = {
3451 "ingress_interface_id": cp["ingress_interface_id"],
3452 "egress_interface_id": cp["egress_interface_id"]
3453 }
3454 uuid_list.append(sfi_uuid)
3455 db_sfi = {
3456 "uuid": sfi_uuid,
3457 "related": sfi_uuid,
3458 "instance_scenario_id": instance_uuid,
3459 'sce_rsp_hop_id': cp['uuid'],
3460 'datacenter_id': datacenter_id,
3461 'datacenter_tenant_id': myvim_thread_id,
3462 "vim_sfi_id": None, # vim thread will populate
3463 }
3464 db_instance_sfis.append(db_sfi)
3465 db_vim_action = {
3466 "instance_action_id": instance_action_id,
3467 "task_index": task_index,
3468 "datacenter_vim_id": myvim_thread_id,
3469 "action": "CREATE",
3470 "status": "SCHEDULED",
3471 "item": "instance_sfis",
3472 "item_id": sfi_uuid,
3473 "related": sfi_uuid,
3474 "extra": yaml.safe_dump({"params": extra_params, "depends_on": [dependencies[i]]},
3475 default_flow_style=True, width=256)
3476 }
3477 sfis_created.append(task_index)
3478 task_index += 1
3479 db_vim_actions.append(db_vim_action)
3480 # create sfs
3481 sf_uuid = str(uuid4())
3482 uuid_list.append(sf_uuid)
3483 db_sf = {
3484 "uuid": sf_uuid,
3485 "related": sf_uuid,
3486 "instance_scenario_id": instance_uuid,
3487 'sce_rsp_hop_id': cp['uuid'],
3488 'datacenter_id': datacenter_id,
3489 'datacenter_tenant_id': myvim_thread_id,
3490 "vim_sf_id": None, # vim thread will populate
3491 }
3492 db_instance_sfs.append(db_sf)
3493 db_vim_action = {
3494 "instance_action_id": instance_action_id,
3495 "task_index": task_index,
3496 "datacenter_vim_id": myvim_thread_id,
3497 "action": "CREATE",
3498 "status": "SCHEDULED",
3499 "item": "instance_sfs",
3500 "item_id": sf_uuid,
3501 "related": sf_uuid,
3502 "extra": yaml.safe_dump({"params": "", "depends_on": sfis_created},
3503 default_flow_style=True, width=256)
3504 }
3505 sfs_created.append(task_index)
3506 task_index += 1
3507 db_vim_actions.append(db_vim_action)
3508 classifier = rsp['classifier']
3509
3510 # TODO the following ~13 lines can be reused for the sfi case
3511 count = mydb.get_rows(
3512 SELECT=('vms.count'),
3513 FROM="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3514 WHERE={'c.uuid': classifier['uuid']})[0]['count']
3515 instance_vnf = next((item for item in db_instance_vnfs if item['sce_vnf_id'] == classifier['sce_vnf_id']), None)
3516 instance_vms = [item for item in db_instance_vms if item['instance_vnf_id'] == instance_vnf['uuid']]
3517 dependencies = []
3518 for instance_vm in instance_vms:
3519 action = next((item for item in db_vim_actions if item['item_id'] == instance_vm['uuid']), None)
3520 if action:
3521 dependencies.append(action['task_index'])
3522 # TODO: throw exception if count != len(instance_vms)
3523 # TODO: and action shouldn't ever be None
3524 classifications_created = []
3525 for i in range(count):
3526 for match in classifier['matches']:
3527 # create classifications
3528 classification_uuid = str(uuid4())
3529 uuid_list.append(classification_uuid)
3530 db_classification = {
3531 "uuid": classification_uuid,
3532 "related": classification_uuid,
3533 "instance_scenario_id": instance_uuid,
3534 'sce_classifier_match_id': match['uuid'],
3535 'datacenter_id': datacenter_id,
3536 'datacenter_tenant_id': myvim_thread_id,
3537 "vim_classification_id": None, # vim thread will populate
3538 }
3539 db_instance_classifications.append(db_classification)
3540 classification_params = {
3541 "ip_proto": match["ip_proto"],
3542 "source_ip": match["source_ip"],
3543 "destination_ip": match["destination_ip"],
3544 "source_port": match["source_port"],
3545 "destination_port": match["destination_port"]
3546 }
3547 db_vim_action = {
3548 "instance_action_id": instance_action_id,
3549 "task_index": task_index,
3550 "datacenter_vim_id": myvim_thread_id,
3551 "action": "CREATE",
3552 "status": "SCHEDULED",
3553 "item": "instance_classifications",
3554 "item_id": classification_uuid,
3555 "related": classification_uuid,
3556 "extra": yaml.safe_dump({"params": classification_params, "depends_on": [dependencies[i]]},
3557 default_flow_style=True, width=256)
3558 }
3559 classifications_created.append(task_index)
3560 task_index += 1
3561 db_vim_actions.append(db_vim_action)
3562
3563 # create sfps
3564 sfp_uuid = str(uuid4())
3565 uuid_list.append(sfp_uuid)
3566 db_sfp = {
3567 "uuid": sfp_uuid,
3568 "related": sfp_uuid,
3569 "instance_scenario_id": instance_uuid,
3570 'sce_rsp_id': rsp['uuid'],
3571 'datacenter_id': datacenter_id,
3572 'datacenter_tenant_id': myvim_thread_id,
3573 "vim_sfp_id": None, # vim thread will populate
3574 }
3575 db_instance_sfps.append(db_sfp)
3576 db_vim_action = {
3577 "instance_action_id": instance_action_id,
3578 "task_index": task_index,
3579 "datacenter_vim_id": myvim_thread_id,
3580 "action": "CREATE",
3581 "status": "SCHEDULED",
3582 "item": "instance_sfps",
3583 "item_id": sfp_uuid,
3584 "related": sfp_uuid,
3585 "extra": yaml.safe_dump({"params": "", "depends_on": sfs_created + classifications_created},
3586 default_flow_style=True, width=256)
3587 }
3588 task_index += 1
3589 db_vim_actions.append(db_vim_action)
3590 db_instance_action["number_tasks"] = task_index
3591
3592 # --> WIM
3593 logger.debug('wim_usage:\n%s\n\n', pformat(wim_usage))
3594 wan_links = wim_engine.derive_wan_links(wim_usage, db_instance_nets, tenant_id)
3595 wim_actions = wim_engine.create_actions(wan_links)
3596 wim_actions, db_instance_action = (
3597 wim_engine.incorporate_actions(wim_actions, db_instance_action))
3598 # <-- WIM
3599
3600 scenarioDict["datacenter2tenant"] = myvim_threads_id
3601
3602 db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
3603 db_instance_scenario['datacenter_id'] = default_datacenter_id
3604 db_tables=[
3605 {"instance_scenarios": db_instance_scenario},
3606 {"instance_vnfs": db_instance_vnfs},
3607 {"instance_nets": db_instance_nets},
3608 {"ip_profiles": db_ip_profiles},
3609 {"instance_vms": db_instance_vms},
3610 {"instance_interfaces": db_instance_interfaces},
3611 {"instance_actions": db_instance_action},
3612 {"instance_sfis": db_instance_sfis},
3613 {"instance_sfs": db_instance_sfs},
3614 {"instance_classifications": db_instance_classifications},
3615 {"instance_sfps": db_instance_sfps},
3616 {"instance_wim_nets": wan_links},
3617 {"vim_wim_actions": db_vim_actions + wim_actions}
3618 ]
3619
3620 logger.debug("create_instance done DB tables: %s",
3621 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
3622 mydb.new_rows(db_tables, uuid_list)
3623 for myvim_thread_id in myvim_threads_id.values():
3624 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
3625
3626 wim_engine.dispatch(wim_actions)
3627
3628 returned_instance = mydb.get_instance_scenario(instance_uuid)
3629 returned_instance["action_id"] = instance_action_id
3630 return returned_instance
3631 except (NfvoException, vimconn.vimconnException, wimconn.WimConnectorError, db_base_Exception) as e:
3632 message = rollback(mydb, myvims, rollbackList)
3633 if isinstance(e, db_base_Exception):
3634 error_text = "database Exception"
3635 elif isinstance(e, vimconn.vimconnException):
3636 error_text = "VIM Exception"
3637 elif isinstance(e, wimconn.WimConnectorError):
3638 error_text = "WIM Exception"
3639 else:
3640 error_text = "Exception"
3641 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
3642 # logger.error("create_instance: %s", error_text)
3643 logger.exception(e)
3644 raise NfvoException(error_text, e.http_code)
3645
3646
3647 def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
3648 default_datacenter_id = params["default_datacenter_id"]
3649 myvim_threads_id = params["myvim_threads_id"]
3650 instance_uuid = params["instance_uuid"]
3651 instance_name = params["instance_name"]
3652 instance_action_id = params["instance_action_id"]
3653 myvims = params["myvims"]
3654 cloud_config = params["cloud_config"]
3655 RO_pub_key = params["RO_pub_key"]
3656
3657 task_index = params_out["task_index"]
3658 uuid_list = params_out["uuid_list"]
3659 db_instance_nets = params_out["db_instance_nets"]
3660 db_vim_actions = params_out["db_vim_actions"]
3661 db_ip_profiles = params_out["db_ip_profiles"]
3662 db_instance_vnfs = params_out["db_instance_vnfs"]
3663 db_instance_vms = params_out["db_instance_vms"]
3664 db_instance_interfaces = params_out["db_instance_interfaces"]
3665 net2task_id = params_out["net2task_id"]
3666 sce_net2instance = params_out["sce_net2instance"]
3667
3668 vnf_net2instance = {}
3669
3670 # 2. Creating new nets (vnf internal nets) in the VIM"
3671 # For each vnf net, we create it and we add it to instanceNetlist.
3672 if sce_vnf.get("datacenter"):
3673 datacenter_id = sce_vnf["datacenter"]
3674 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3675 else:
3676 datacenter_id = default_datacenter_id
3677 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3678 for net in sce_vnf['nets']:
3679 # TODO revis
3680 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3681 # net_name = descriptor_net.get("name")
3682 net_name = None
3683 if not net_name:
3684 net_name = "{}-{}".format(instance_name, net["name"])
3685 net_name = net_name[:255] # limit length
3686 net_type = net['type']
3687
3688 if sce_vnf['uuid'] not in vnf_net2instance:
3689 vnf_net2instance[sce_vnf['uuid']] = {}
3690 if sce_vnf['uuid'] not in net2task_id:
3691 net2task_id[sce_vnf['uuid']] = {}
3692 net2task_id[sce_vnf['uuid']][net['uuid']] = task_index
3693
3694 # fill database content
3695 net_uuid = str(uuid4())
3696 uuid_list.append(net_uuid)
3697 vnf_net2instance[sce_vnf['uuid']][net['uuid']] = net_uuid
3698 db_net = {
3699 "uuid": net_uuid,
3700 "related": net_uuid,
3701 'vim_net_id': None,
3702 "vim_name": net_name,
3703 "instance_scenario_id": instance_uuid,
3704 "net_id": net["uuid"],
3705 "created": True,
3706 'datacenter_id': datacenter_id,
3707 'datacenter_tenant_id': myvim_thread_id,
3708 }
3709 db_instance_nets.append(db_net)
3710
3711 lookfor_filter = {}
3712 if net.get("vim-network-name"):
3713 lookfor_filter["name"] = net["vim-network-name"]
3714 if net.get("vim-network-id"):
3715 lookfor_filter["id"] = net["vim-network-id"]
3716 if lookfor_filter:
3717 task_action = "FIND"
3718 task_extra = {"params": (lookfor_filter,)}
3719 else:
3720 task_action = "CREATE"
3721 task_extra = {"params": (net_name, net_type, net.get('ip_profile', None))}
3722
3723 db_vim_action = {
3724 "instance_action_id": instance_action_id,
3725 "task_index": task_index,
3726 "datacenter_vim_id": myvim_thread_id,
3727 "status": "SCHEDULED",
3728 "action": task_action,
3729 "item": "instance_nets",
3730 "item_id": net_uuid,
3731 "related": net_uuid,
3732 "extra": yaml.safe_dump(task_extra, default_flow_style=True, width=256)
3733 }
3734 task_index += 1
3735 db_vim_actions.append(db_vim_action)
3736
3737 if 'ip_profile' in net:
3738 db_ip_profile = {
3739 'instance_net_id': net_uuid,
3740 'ip_version': net['ip_profile']['ip_version'],
3741 'subnet_address': net['ip_profile']['subnet_address'],
3742 'gateway_address': net['ip_profile']['gateway_address'],
3743 'dns_address': net['ip_profile']['dns_address'],
3744 'dhcp_enabled': net['ip_profile']['dhcp_enabled'],
3745 'dhcp_start_address': net['ip_profile']['dhcp_start_address'],
3746 'dhcp_count': net['ip_profile']['dhcp_count'],
3747 }
3748 db_ip_profiles.append(db_ip_profile)
3749
3750 # print "vnf_net2instance:"
3751 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3752
3753 # 3. Creating new vm instances in the VIM
3754 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3755 ssh_access = None
3756 if sce_vnf.get('mgmt_access'):
3757 ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
3758 vnf_availability_zones = []
3759 for vm in sce_vnf.get('vms'):
3760 vm_av = vm.get('availability_zone')
3761 if vm_av and vm_av not in vnf_availability_zones:
3762 vnf_availability_zones.append(vm_av)
3763
3764 # check if there is enough availability zones available at vim level.
3765 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
3766 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
3767 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
3768
3769 if sce_vnf.get("datacenter"):
3770 vim = myvims[sce_vnf["datacenter"]]
3771 myvim_thread_id = myvim_threads_id[sce_vnf["datacenter"]]
3772 datacenter_id = sce_vnf["datacenter"]
3773 else:
3774 vim = myvims[default_datacenter_id]
3775 myvim_thread_id = myvim_threads_id[default_datacenter_id]
3776 datacenter_id = default_datacenter_id
3777 sce_vnf["datacenter_id"] = datacenter_id
3778 i = 0
3779
3780 vnf_uuid = str(uuid4())
3781 uuid_list.append(vnf_uuid)
3782 db_instance_vnf = {
3783 'uuid': vnf_uuid,
3784 'instance_scenario_id': instance_uuid,
3785 'vnf_id': sce_vnf['vnf_id'],
3786 'sce_vnf_id': sce_vnf['uuid'],
3787 'datacenter_id': datacenter_id,
3788 'datacenter_tenant_id': myvim_thread_id,
3789 }
3790 db_instance_vnfs.append(db_instance_vnf)
3791
3792 for vm in sce_vnf['vms']:
3793 # skip PDUs
3794 if vm.get("pdu_type"):
3795 continue
3796
3797 myVMDict = {}
3798 sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
3799 myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
3800 myVMDict['description'] = myVMDict['name'][0:99]
3801 # if not startvms:
3802 # myVMDict['start'] = "no"
3803 if vm.get("instance_parameters") and vm["instance_parameters"].get("name"):
3804 myVMDict['name'] = vm["instance_parameters"].get("name")
3805 myVMDict['name'] = myVMDict['name'][0:255] # limit name length
3806 # create image at vim in case it not exist
3807 image_uuid = vm['image_id']
3808 if vm.get("image_list"):
3809 for alternative_image in vm["image_list"]:
3810 if alternative_image["vim_type"] == vim["config"]["_vim_type_internal"]:
3811 image_uuid = alternative_image['image_id']
3812 break
3813 image_dict = mydb.get_table_by_uuid_name("images", image_uuid)
3814 image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
3815 vm['vim_image_id'] = image_id
3816
3817 # create flavor at vim in case it not exist
3818 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
3819 if flavor_dict['extended'] != None:
3820 flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
3821 flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
3822
3823 # Obtain information for additional disks
3824 extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',),
3825 WHERE={'vim_id': flavor_id})
3826 if not extended_flavor_dict:
3827 raise NfvoException("flavor '{}' not found".format(flavor_id), httperrors.Not_Found)
3828
3829 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3830 myVMDict['disks'] = None
3831 extended_info = extended_flavor_dict[0]['extended']
3832 if extended_info != None:
3833 extended_flavor_dict_yaml = yaml.load(extended_info)
3834 if 'disks' in extended_flavor_dict_yaml:
3835 myVMDict['disks'] = extended_flavor_dict_yaml['disks']
3836 if vm.get("instance_parameters") and vm["instance_parameters"].get("devices"):
3837 for disk in myVMDict['disks']:
3838 if disk.get("name") in vm["instance_parameters"]["devices"]:
3839 disk.update(vm["instance_parameters"]["devices"][disk.get("name")])
3840
3841 vm['vim_flavor_id'] = flavor_id
3842 myVMDict['imageRef'] = vm['vim_image_id']
3843 myVMDict['flavorRef'] = vm['vim_flavor_id']
3844 myVMDict['availability_zone'] = vm.get('availability_zone')
3845 myVMDict['networks'] = []
3846 task_depends_on = []
3847 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3848 is_management_vm = False
3849 db_vm_ifaces = []
3850 for iface in vm['interfaces']:
3851 netDict = {}
3852 if iface['type'] == "data":
3853 netDict['type'] = iface['model']
3854 elif "model" in iface and iface["model"] != None:
3855 netDict['model'] = iface['model']
3856 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3857 # is obtained from iterface table model
3858 # discover type of interface looking at flavor
3859 for numa in flavor_dict.get('extended', {}).get('numas', []):
3860 for flavor_iface in numa.get('interfaces', []):
3861 if flavor_iface.get('name') == iface['internal_name']:
3862 if flavor_iface['dedicated'] == 'yes':
3863 netDict['type'] = "PF" # passthrough
3864 elif flavor_iface['dedicated'] == 'no':
3865 netDict['type'] = "VF" # siov
3866 elif flavor_iface['dedicated'] == 'yes:sriov':
3867 netDict['type'] = "VFnotShared" # sriov but only one sriov on the PF
3868 netDict["mac_address"] = flavor_iface.get("mac_address")
3869 break
3870 netDict["use"] = iface['type']
3871 if netDict["use"] == "data" and not netDict.get("type"):
3872 # print "netDict", netDict
3873 # print "iface", iface
3874 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3875 sce_vnf['name'], vm['name'], iface['internal_name'])
3876 if flavor_dict.get('extended') == None:
3877 raise NfvoException(e_text + "After database migration some information is not available. \
3878 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
3879 else:
3880 raise NfvoException(e_text, httperrors.Internal_Server_Error)
3881 if netDict["use"] == "mgmt":
3882 is_management_vm = True
3883 netDict["type"] = "virtual"
3884 if netDict["use"] == "bridge":
3885 netDict["type"] = "virtual"
3886 if iface.get("vpci"):
3887 netDict['vpci'] = iface['vpci']
3888 if iface.get("mac"):
3889 netDict['mac_address'] = iface['mac']
3890 if iface.get("mac_address"):
3891 netDict['mac_address'] = iface['mac_address']
3892 if iface.get("ip_address"):
3893 netDict['ip_address'] = iface['ip_address']
3894 if iface.get("port-security") is not None:
3895 netDict['port_security'] = iface['port-security']
3896 if iface.get("floating-ip") is not None:
3897 netDict['floating_ip'] = iface['floating-ip']
3898 netDict['name'] = iface['internal_name']
3899 if iface['net_id'] is None:
3900 for vnf_iface in sce_vnf["interfaces"]:
3901 # print iface
3902 # print vnf_iface
3903 if vnf_iface['interface_id'] == iface['uuid']:
3904 netDict['net_id'] = "TASK-{}".format(
3905 net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3906 instance_net_id = sce_net2instance[vnf_iface['sce_net_id']][datacenter_id]
3907 task_depends_on.append(net2task_id['scenario'][vnf_iface['sce_net_id']][datacenter_id])
3908 break
3909 else:
3910 netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']])
3911 instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']]
3912 task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']])
3913 # skip bridge ifaces not connected to any net
3914 if 'net_id' not in netDict or netDict['net_id'] == None:
3915 continue
3916 myVMDict['networks'].append(netDict)
3917 db_vm_iface = {
3918 # "uuid"
3919 # 'instance_vm_id': instance_vm_uuid,
3920 "instance_net_id": instance_net_id,
3921 'interface_id': iface['uuid'],
3922 # 'vim_interface_id': ,
3923 'type': 'external' if iface['external_name'] is not None else 'internal',
3924 'ip_address': iface.get('ip_address'),
3925 'mac_address': iface.get('mac'),
3926 'floating_ip': int(iface.get('floating-ip', False)),
3927 'port_security': int(iface.get('port-security', True))
3928 }
3929 db_vm_ifaces.append(db_vm_iface)
3930 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3931 # print myVMDict['name']
3932 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3933 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3934 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3935
3936 # We add the RO key to cloud_config if vnf will need ssh access
3937 cloud_config_vm = cloud_config
3938 if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
3939 cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
3940 cloud_config_vm)
3941
3942 if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"):
3943 cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
3944 cloud_config_vm)
3945 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3946 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3947 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
3948 if vm.get("boot_data"):
3949 cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
3950
3951 if myVMDict.get('availability_zone'):
3952 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
3953 else:
3954 av_index = None
3955 for vm_index in range(0, vm.get('count', 1)):
3956 vm_name = myVMDict['name'] + "-" + str(vm_index+1)
3957 task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None),
3958 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm,
3959 myVMDict['disks'], av_index, vnf_availability_zones)
3960 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3961 for net in myVMDict['networks']:
3962 if "vim_id" in net:
3963 for iface in vm['interfaces']:
3964 if net["name"] == iface["internal_name"]:
3965 iface["vim_id"] = net["vim_id"]
3966 break
3967 vm_uuid = str(uuid4())
3968 uuid_list.append(vm_uuid)
3969 db_vm = {
3970 "uuid": vm_uuid,
3971 "related": vm_uuid,
3972 'instance_vnf_id': vnf_uuid,
3973 # TODO delete "vim_vm_id": vm_id,
3974 "vm_id": vm["uuid"],
3975 "vim_name": vm_name,
3976 # "status":
3977 }
3978 db_instance_vms.append(db_vm)
3979
3980 iface_index = 0
3981 for db_vm_iface in db_vm_ifaces:
3982 iface_uuid = str(uuid4())
3983 uuid_list.append(iface_uuid)
3984 db_vm_iface_instance = {
3985 "uuid": iface_uuid,
3986 "instance_vm_id": vm_uuid
3987 }
3988 db_vm_iface_instance.update(db_vm_iface)
3989 if db_vm_iface_instance.get("ip_address"): # increment ip_address
3990 ip = db_vm_iface_instance.get("ip_address")
3991 i = ip.rfind(".")
3992 if i > 0:
3993 try:
3994 i += 1
3995 ip = ip[i:] + str(int(ip[:i]) + 1)
3996 db_vm_iface_instance["ip_address"] = ip
3997 except:
3998 db_vm_iface_instance["ip_address"] = None
3999 db_instance_interfaces.append(db_vm_iface_instance)
4000 myVMDict['networks'][iface_index]["uuid"] = iface_uuid
4001 iface_index += 1
4002
4003 db_vim_action = {
4004 "instance_action_id": instance_action_id,
4005 "task_index": task_index,
4006 "datacenter_vim_id": myvim_thread_id,
4007 "action": "CREATE",
4008 "status": "SCHEDULED",
4009 "item": "instance_vms",
4010 "item_id": vm_uuid,
4011 "related": vm_uuid,
4012 "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
4013 default_flow_style=True, width=256)
4014 }
4015 task_index += 1
4016 db_vim_actions.append(db_vim_action)
4017 params_out["task_index"] = task_index
4018 params_out["uuid_list"] = uuid_list
4019
4020
4021 def delete_instance(mydb, tenant_id, instance_id):
4022 # print "Checking that the instance_id exists and getting the instance dictionary"
4023 instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
4024 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4025 tenant_id = instanceDict["tenant_id"]
4026
4027 # --> WIM
4028 # We need to retrieve the WIM Actions now, before the instance_scenario is
4029 # deleted. The reason for that is that: ON CASCADE rules will delete the
4030 # instance_wim_nets record in the database
4031 wim_actions = wim_engine.delete_actions(instance_scenario_id=instance_id)
4032 # <-- WIM
4033
4034 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4035 # 1. Delete from Database
4036 message = mydb.delete_instance_scenario(instance_id, tenant_id)
4037
4038 # 2. delete from VIM
4039 error_msg = ""
4040 myvims = {}
4041 myvim_threads = {}
4042 vimthread_affected = {}
4043 net2vm_dependencies = {}
4044
4045 task_index = 0
4046 instance_action_id = get_task_id()
4047 db_vim_actions = []
4048 db_instance_action = {
4049 "uuid": instance_action_id, # same uuid for the instance and the action on create
4050 "tenant_id": tenant_id,
4051 "instance_id": instance_id,
4052 "description": "DELETE",
4053 # "number_tasks": 0 # filled bellow
4054 }
4055
4056 # 2.1 deleting VNFFGs
4057 for sfp in instanceDict.get('sfps', ()):
4058 vimthread_affected[sfp["datacenter_tenant_id"]] = None
4059 datacenter_key = (sfp["datacenter_id"], sfp["datacenter_tenant_id"])
4060 if datacenter_key not in myvims:
4061 try:
4062 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfp["datacenter_id"], sfp["datacenter_tenant_id"])
4063 except NfvoException as e:
4064 logger.error(str(e))
4065 myvim_thread = None
4066 myvim_threads[datacenter_key] = myvim_thread
4067 vims = get_vim(mydb, tenant_id, datacenter_id=sfp["datacenter_id"],
4068 datacenter_tenant_id=sfp["datacenter_tenant_id"])
4069 if len(vims) == 0:
4070 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp["datacenter_id"], sfp["datacenter_tenant_id"]))
4071 myvims[datacenter_key] = None
4072 else:
4073 myvims[datacenter_key] = vims.values()[0]
4074 myvim = myvims[datacenter_key]
4075 myvim_thread = myvim_threads[datacenter_key]
4076
4077 if not myvim:
4078 error_msg += "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp['vim_sfp_id'], sfp["datacenter_id"])
4079 continue
4080 extra = {"params": (sfp['vim_sfp_id'])}
4081 db_vim_action = {
4082 "instance_action_id": instance_action_id,
4083 "task_index": task_index,
4084 "datacenter_vim_id": sfp["datacenter_tenant_id"],
4085 "action": "DELETE",
4086 "status": "SCHEDULED",
4087 "item": "instance_sfps",
4088 "item_id": sfp["uuid"],
4089 "related": sfp["related"],
4090 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4091 }
4092 task_index += 1
4093 db_vim_actions.append(db_vim_action)
4094
4095 for classification in instanceDict['classifications']:
4096 vimthread_affected[classification["datacenter_tenant_id"]] = None
4097 datacenter_key = (classification["datacenter_id"], classification["datacenter_tenant_id"])
4098 if datacenter_key not in myvims:
4099 try:
4100 _, myvim_thread = get_vim_thread(mydb, tenant_id, classification["datacenter_id"], classification["datacenter_tenant_id"])
4101 except NfvoException as e:
4102 logger.error(str(e))
4103 myvim_thread = None
4104 myvim_threads[datacenter_key] = myvim_thread
4105 vims = get_vim(mydb, tenant_id, datacenter_id=classification["datacenter_id"],
4106 datacenter_tenant_id=classification["datacenter_tenant_id"])
4107 if len(vims) == 0:
4108 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification["datacenter_id"],
4109 classification["datacenter_tenant_id"]))
4110 myvims[datacenter_key] = None
4111 else:
4112 myvims[datacenter_key] = vims.values()[0]
4113 myvim = myvims[datacenter_key]
4114 myvim_thread = myvim_threads[datacenter_key]
4115
4116 if not myvim:
4117 error_msg += "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification['vim_classification_id'],
4118 classification["datacenter_id"])
4119 continue
4120 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4121 extra = {"params": (classification['vim_classification_id']), "depends_on": depends_on}
4122 db_vim_action = {
4123 "instance_action_id": instance_action_id,
4124 "task_index": task_index,
4125 "datacenter_vim_id": classification["datacenter_tenant_id"],
4126 "action": "DELETE",
4127 "status": "SCHEDULED",
4128 "item": "instance_classifications",
4129 "item_id": classification["uuid"],
4130 "related": classification["related"],
4131 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4132 }
4133 task_index += 1
4134 db_vim_actions.append(db_vim_action)
4135
4136 for sf in instanceDict.get('sfs', ()):
4137 vimthread_affected[sf["datacenter_tenant_id"]] = None
4138 datacenter_key = (sf["datacenter_id"], sf["datacenter_tenant_id"])
4139 if datacenter_key not in myvims:
4140 try:
4141 _, myvim_thread = get_vim_thread(mydb, tenant_id, sf["datacenter_id"], sf["datacenter_tenant_id"])
4142 except NfvoException as e:
4143 logger.error(str(e))
4144 myvim_thread = None
4145 myvim_threads[datacenter_key] = myvim_thread
4146 vims = get_vim(mydb, tenant_id, datacenter_id=sf["datacenter_id"],
4147 datacenter_tenant_id=sf["datacenter_tenant_id"])
4148 if len(vims) == 0:
4149 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf["datacenter_id"], sf["datacenter_tenant_id"]))
4150 myvims[datacenter_key] = None
4151 else:
4152 myvims[datacenter_key] = vims.values()[0]
4153 myvim = myvims[datacenter_key]
4154 myvim_thread = myvim_threads[datacenter_key]
4155
4156 if not myvim:
4157 error_msg += "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf['vim_sf_id'], sf["datacenter_id"])
4158 continue
4159 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfps"]
4160 extra = {"params": (sf['vim_sf_id']), "depends_on": depends_on}
4161 db_vim_action = {
4162 "instance_action_id": instance_action_id,
4163 "task_index": task_index,
4164 "datacenter_vim_id": sf["datacenter_tenant_id"],
4165 "action": "DELETE",
4166 "status": "SCHEDULED",
4167 "item": "instance_sfs",
4168 "item_id": sf["uuid"],
4169 "related": sf["related"],
4170 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4171 }
4172 task_index += 1
4173 db_vim_actions.append(db_vim_action)
4174
4175 for sfi in instanceDict.get('sfis', ()):
4176 vimthread_affected[sfi["datacenter_tenant_id"]] = None
4177 datacenter_key = (sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4178 if datacenter_key not in myvims:
4179 try:
4180 _, myvim_thread = get_vim_thread(mydb, tenant_id, sfi["datacenter_id"], sfi["datacenter_tenant_id"])
4181 except NfvoException as e:
4182 logger.error(str(e))
4183 myvim_thread = None
4184 myvim_threads[datacenter_key] = myvim_thread
4185 vims = get_vim(mydb, tenant_id, datacenter_id=sfi["datacenter_id"],
4186 datacenter_tenant_id=sfi["datacenter_tenant_id"])
4187 if len(vims) == 0:
4188 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi["datacenter_id"], sfi["datacenter_tenant_id"]))
4189 myvims[datacenter_key] = None
4190 else:
4191 myvims[datacenter_key] = vims.values()[0]
4192 myvim = myvims[datacenter_key]
4193 myvim_thread = myvim_threads[datacenter_key]
4194
4195 if not myvim:
4196 error_msg += "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi['vim_sfi_id'], sfi["datacenter_id"])
4197 continue
4198 depends_on = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfs"]
4199 extra = {"params": (sfi['vim_sfi_id']), "depends_on": depends_on}
4200 db_vim_action = {
4201 "instance_action_id": instance_action_id,
4202 "task_index": task_index,
4203 "datacenter_vim_id": sfi["datacenter_tenant_id"],
4204 "action": "DELETE",
4205 "status": "SCHEDULED",
4206 "item": "instance_sfis",
4207 "item_id": sfi["uuid"],
4208 "related": sfi["related"],
4209 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4210 }
4211 task_index += 1
4212 db_vim_actions.append(db_vim_action)
4213
4214 # 2.2 deleting VMs
4215 # vm_fail_list=[]
4216 for sce_vnf in instanceDict.get('vnfs', ()):
4217 datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4218 vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
4219 if datacenter_key not in myvims:
4220 try:
4221 _, myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4222 except NfvoException as e:
4223 logger.error(str(e))
4224 myvim_thread = None
4225 myvim_threads[datacenter_key] = myvim_thread
4226 vims = get_vim(mydb, tenant_id, datacenter_id=sce_vnf["datacenter_id"],
4227 datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4228 if len(vims) == 0:
4229 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"],
4230 sce_vnf["datacenter_tenant_id"]))
4231 myvims[datacenter_key] = None
4232 else:
4233 myvims[datacenter_key] = vims.values()[0]
4234 myvim = myvims[datacenter_key]
4235 myvim_thread = myvim_threads[datacenter_key]
4236
4237 for vm in sce_vnf['vms']:
4238 if not myvim:
4239 error_msg += "\n VM id={} cannot be deleted because datacenter={} not found".format(vm['vim_vm_id'], sce_vnf["datacenter_id"])
4240 continue
4241 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4242 db_vim_action = {
4243 "instance_action_id": instance_action_id,
4244 "task_index": task_index,
4245 "datacenter_vim_id": sce_vnf["datacenter_tenant_id"],
4246 "action": "DELETE",
4247 "status": "SCHEDULED",
4248 "item": "instance_vms",
4249 "item_id": vm["uuid"],
4250 "related": vm["related"],
4251 "extra": yaml.safe_dump({"params": vm["interfaces"], "depends_on": sfi_dependencies},
4252 default_flow_style=True, width=256)
4253 }
4254 db_vim_actions.append(db_vim_action)
4255 for interface in vm["interfaces"]:
4256 if not interface.get("instance_net_id"):
4257 continue
4258 if interface["instance_net_id"] not in net2vm_dependencies:
4259 net2vm_dependencies[interface["instance_net_id"]] = []
4260 net2vm_dependencies[interface["instance_net_id"]].append(task_index)
4261 task_index += 1
4262
4263 # 2.3 deleting NETS
4264 # net_fail_list=[]
4265 for net in instanceDict['nets']:
4266 vimthread_affected[net["datacenter_tenant_id"]] = None
4267 datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4268 if datacenter_key not in myvims:
4269 try:
4270 _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
4271 except NfvoException as e:
4272 logger.error(str(e))
4273 myvim_thread = None
4274 myvim_threads[datacenter_key] = myvim_thread
4275 vims = get_vim(mydb, tenant_id, datacenter_id=net["datacenter_id"],
4276 datacenter_tenant_id=net["datacenter_tenant_id"])
4277 if len(vims) == 0:
4278 logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4279 myvims[datacenter_key] = None
4280 else:
4281 myvims[datacenter_key] = vims.values()[0]
4282 myvim = myvims[datacenter_key]
4283 myvim_thread = myvim_threads[datacenter_key]
4284
4285 if not myvim:
4286 error_msg += "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net['vim_net_id'], net["datacenter_id"])
4287 continue
4288 extra = {"params": (net['vim_net_id'], net['sdn_net_id'])}
4289 if net2vm_dependencies.get(net["uuid"]):
4290 extra["depends_on"] = net2vm_dependencies[net["uuid"]]
4291 sfi_dependencies = [action["task_index"] for action in db_vim_actions if action["item"] == "instance_sfis"]
4292 if len(sfi_dependencies) > 0:
4293 if "depends_on" in extra:
4294 extra["depends_on"] += sfi_dependencies
4295 else:
4296 extra["depends_on"] = sfi_dependencies
4297 db_vim_action = {
4298 "instance_action_id": instance_action_id,
4299 "task_index": task_index,
4300 "datacenter_vim_id": net["datacenter_tenant_id"],
4301 "action": "DELETE",
4302 "status": "SCHEDULED",
4303 "item": "instance_nets",
4304 "item_id": net["uuid"],
4305 "related": net["related"],
4306 "extra": yaml.safe_dump(extra, default_flow_style=True, width=256)
4307 }
4308 task_index += 1
4309 db_vim_actions.append(db_vim_action)
4310
4311 db_instance_action["number_tasks"] = task_index
4312
4313 # --> WIM
4314 wim_actions, db_instance_action = (
4315 wim_engine.incorporate_actions(wim_actions, db_instance_action))
4316 # <-- WIM
4317
4318 db_tables = [
4319 {"instance_actions": db_instance_action},
4320 {"vim_wim_actions": db_vim_actions + wim_actions}
4321 ]
4322
4323 logger.debug("delete_instance done DB tables: %s",
4324 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4325 mydb.new_rows(db_tables, ())
4326 for myvim_thread_id in vimthread_affected.keys():
4327 vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
4328
4329 wim_engine.dispatch(wim_actions)
4330
4331 if len(error_msg) > 0:
4332 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4333 '(error: 404) from VIM: {}'.format(instance_action_id, message, error_msg)
4334 else:
4335 return "action_id={} instance {} deleted".format(instance_action_id, message)
4336
4337 def get_instance_id(mydb, tenant_id, instance_id):
4338 global ovim
4339 #check valid tenant_id
4340 check_tenant(mydb, tenant_id)
4341 #obtain data
4342
4343 instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
4344 for net in instance_dict["nets"]:
4345 if net.get("sdn_net_id"):
4346 net_sdn = ovim.show_network(net["sdn_net_id"])
4347 net["sdn_info"] = {
4348 "admin_state_up": net_sdn.get("admin_state_up"),
4349 "flows": net_sdn.get("flows"),
4350 "last_error": net_sdn.get("last_error"),
4351 "ports": net_sdn.get("ports"),
4352 "type": net_sdn.get("type"),
4353 "status": net_sdn.get("status"),
4354 "vlan": net_sdn.get("vlan"),
4355 }
4356 return instance_dict
4357
4358 @deprecated("Instance is automatically refreshed by vim_threads")
4359 def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
4360 '''Refreshes a scenario instance. It modifies instanceDict'''
4361 '''Returns:
4362 - 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
4363 - error_msg
4364 '''
4365 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4366 # #print "nfvo.refresh_instance begins"
4367 # #print json.dumps(instanceDict, indent=4)
4368 #
4369 # #print "Getting the VIM URL and the VIM tenant_id"
4370 # myvims={}
4371 #
4372 # # 1. Getting VIM vm and net list
4373 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4374 # vms_notupdated=[]
4375 # vm_list = {}
4376 # for sce_vnf in instanceDict['vnfs']:
4377 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4378 # if datacenter_key not in vm_list:
4379 # vm_list[datacenter_key] = []
4380 # if datacenter_key not in myvims:
4381 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4382 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4383 # if len(vims) == 0:
4384 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4385 # myvims[datacenter_key] = None
4386 # else:
4387 # myvims[datacenter_key] = vims.values()[0]
4388 # for vm in sce_vnf['vms']:
4389 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4390 # vms_notupdated.append(vm["uuid"])
4391 #
4392 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4393 # nets_notupdated=[]
4394 # net_list = {}
4395 # for net in instanceDict['nets']:
4396 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4397 # if datacenter_key not in net_list:
4398 # net_list[datacenter_key] = []
4399 # if datacenter_key not in myvims:
4400 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4401 # datacenter_tenant_id=net["datacenter_tenant_id"])
4402 # if len(vims) == 0:
4403 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4404 # myvims[datacenter_key] = None
4405 # else:
4406 # myvims[datacenter_key] = vims.values()[0]
4407 #
4408 # net_list[datacenter_key].append(net['vim_net_id'])
4409 # nets_notupdated.append(net["uuid"])
4410 #
4411 # # 1. Getting the status of all VMs
4412 # vm_dict={}
4413 # for datacenter_key in myvims:
4414 # if not vm_list.get(datacenter_key):
4415 # continue
4416 # failed = True
4417 # failed_message=""
4418 # if not myvims[datacenter_key]:
4419 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4420 # else:
4421 # try:
4422 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4423 # failed = False
4424 # except vimconn.vimconnException as e:
4425 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4426 # failed_message = str(e)
4427 # if failed:
4428 # for vm in vm_list[datacenter_key]:
4429 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4430 #
4431 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4432 # for sce_vnf in instanceDict['vnfs']:
4433 # for vm in sce_vnf['vms']:
4434 # vm_id = vm['vim_vm_id']
4435 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4436 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4437 # has_mgmt_iface = False
4438 # for iface in vm["interfaces"]:
4439 # if iface["type"]=="mgmt":
4440 # has_mgmt_iface = True
4441 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4442 # vm_dict[vm_id]['status'] = "ACTIVE"
4443 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4444 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4445 # 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'):
4446 # vm['status'] = vm_dict[vm_id]['status']
4447 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4448 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4449 # # 2.1. Update in openmano DB the VMs whose status changed
4450 # try:
4451 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4452 # vms_notupdated.remove(vm["uuid"])
4453 # if updates>0:
4454 # vms_updated.append(vm["uuid"])
4455 # except db_base_Exception as e:
4456 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4457 # # 2.2. Update in openmano DB the interface VMs
4458 # for interface in interfaces:
4459 # #translate from vim_net_id to instance_net_id
4460 # network_id_list=[]
4461 # for net in instanceDict['nets']:
4462 # if net["vim_net_id"] == interface["vim_net_id"]:
4463 # network_id_list.append(net["uuid"])
4464 # if not network_id_list:
4465 # continue
4466 # del interface["vim_net_id"]
4467 # try:
4468 # for network_id in network_id_list:
4469 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4470 # except db_base_Exception as e:
4471 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4472 #
4473 # # 3. Getting the status of all nets
4474 # net_dict = {}
4475 # for datacenter_key in myvims:
4476 # if not net_list.get(datacenter_key):
4477 # continue
4478 # failed = True
4479 # failed_message = ""
4480 # if not myvims[datacenter_key]:
4481 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4482 # else:
4483 # try:
4484 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4485 # failed = False
4486 # except vimconn.vimconnException as e:
4487 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4488 # failed_message = str(e)
4489 # if failed:
4490 # for net in net_list[datacenter_key]:
4491 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4492 #
4493 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4494 # # TODO: update nets inside a vnf
4495 # for net in instanceDict['nets']:
4496 # net_id = net['vim_net_id']
4497 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4498 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4499 # 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'):
4500 # net['status'] = net_dict[net_id]['status']
4501 # net['error_msg'] = net_dict[net_id].get('error_msg')
4502 # net['vim_info'] = net_dict[net_id].get('vim_info')
4503 # # 5.1. Update in openmano DB the nets whose status changed
4504 # try:
4505 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4506 # nets_notupdated.remove(net["uuid"])
4507 # if updated>0:
4508 # nets_updated.append(net["uuid"])
4509 # except db_base_Exception as e:
4510 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4511 #
4512 # # Returns appropriate output
4513 # #print "nfvo.refresh_instance finishes"
4514 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4515 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4516 instance_id = instanceDict['uuid']
4517 # if len(vms_notupdated)+len(nets_notupdated)>0:
4518 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4519 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4520
4521 return 0, 'Scenario instance ' + instance_id + ' refreshed.'
4522
4523 def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
4524 #print "Checking that the instance_id exists and getting the instance dictionary"
4525 instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
4526 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4527
4528 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4529 vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
4530 if len(vims) == 0:
4531 raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), httperrors.Not_Found)
4532 myvim = vims.values()[0]
4533 vm_result = {}
4534 vm_error = 0
4535 vm_ok = 0
4536
4537 myvim_threads_id = {}
4538 if action_dict.get("vdu-scaling"):
4539 db_instance_vms = []
4540 db_vim_actions = []
4541 db_instance_interfaces = []
4542 instance_action_id = get_task_id()
4543 db_instance_action = {
4544 "uuid": instance_action_id, # same uuid for the instance and the action on create
4545 "tenant_id": nfvo_tenant,
4546 "instance_id": instance_id,
4547 "description": "SCALE",
4548 }
4549 vm_result["instance_action_id"] = instance_action_id
4550 vm_result["created"] = []
4551 vm_result["deleted"] = []
4552 task_index = 0
4553 for vdu in action_dict["vdu-scaling"]:
4554 vdu_id = vdu.get("vdu-id")
4555 osm_vdu_id = vdu.get("osm_vdu_id")
4556 member_vnf_index = vdu.get("member-vnf-index")
4557 vdu_count = vdu.get("count", 1)
4558 if vdu_id:
4559 target_vms = mydb.get_rows(
4560 FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4561 WHERE={"vms.uuid": vdu_id},
4562 ORDER_BY="vms.created_at"
4563 )
4564 if not target_vms:
4565 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found)
4566 else:
4567 if not osm_vdu_id and not member_vnf_index:
4568 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4569 target_vms = mydb.get_rows(
4570 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4571 FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4572 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4573 " join vms on ivms.vm_id=vms.uuid",
4574 WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
4575 "ivnfs.instance_scenario_id": instance_id},
4576 ORDER_BY="ivms.created_at"
4577 )
4578 if not target_vms:
4579 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), httperrors.Not_Found)
4580 vdu_id = target_vms[-1]["uuid"]
4581 target_vm = target_vms[-1]
4582 datacenter = target_vm["datacenter_id"]
4583 myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
4584
4585 if vdu["type"] == "delete":
4586 for index in range(0, vdu_count):
4587 target_vm = target_vms[-1-index]
4588 vdu_id = target_vm["uuid"]
4589 # look for nm
4590 vm_interfaces = None
4591 for sce_vnf in instanceDict['vnfs']:
4592 for vm in sce_vnf['vms']:
4593 if vm["uuid"] == vdu_id:
4594 vm_interfaces = vm["interfaces"]
4595 break
4596
4597 db_vim_action = {
4598 "instance_action_id": instance_action_id,
4599 "task_index": task_index,
4600 "datacenter_vim_id": target_vm["datacenter_tenant_id"],
4601 "action": "DELETE",
4602 "status": "SCHEDULED",
4603 "item": "instance_vms",
4604 "item_id": vdu_id,
4605 "related": vm["related"],
4606 "extra": yaml.safe_dump({"params": vm_interfaces},
4607 default_flow_style=True, width=256)
4608 }
4609 task_index += 1
4610 db_vim_actions.append(db_vim_action)
4611 vm_result["deleted"].append(vdu_id)
4612 # delete from database
4613 db_instance_vms.append({"TO-DELETE": vdu_id})
4614
4615 else: # vdu["type"] == "create":
4616 iface2iface = {}
4617 where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
4618
4619 vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
4620 if not vim_action_to_clone:
4621 raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
4622 vim_action_to_clone = vim_action_to_clone[0]
4623 extra = yaml.safe_load(vim_action_to_clone["extra"])
4624
4625 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4626 # TODO do the same for flavor and image when available
4627 task_depends_on = []
4628 task_params = extra["params"]
4629 task_params_networks = deepcopy(task_params[5])
4630 for iface in task_params[5]:
4631 if iface["net_id"].startswith("TASK-"):
4632 if "." not in iface["net_id"]:
4633 task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
4634 iface["net_id"][5:]))
4635 iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
4636 iface["net_id"][5:])
4637 else:
4638 task_depends_on.append(iface["net_id"][5:])
4639 if "mac_address" in iface:
4640 del iface["mac_address"]
4641
4642 vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
4643 for index in range(0, vdu_count):
4644 vm_uuid = str(uuid4())
4645 vm_name = target_vm.get('vim_name')
4646 try:
4647 suffix = vm_name.rfind("-")
4648 vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
4649 except Exception:
4650 pass
4651 db_instance_vm = {
4652 "uuid": vm_uuid,
4653 'instance_vnf_id': target_vm['instance_vnf_id'],
4654 'vm_id': target_vm['vm_id'],
4655 'vim_name': vm_name
4656 }
4657 db_instance_vms.append(db_instance_vm)
4658
4659 for vm_iface in vm_ifaces_to_clone:
4660 iface_uuid = str(uuid4())
4661 iface2iface[vm_iface["uuid"]] = iface_uuid
4662 db_vm_iface = {
4663 "uuid": iface_uuid,
4664 'instance_vm_id': vm_uuid,
4665 "instance_net_id": vm_iface["instance_net_id"],
4666 'interface_id': vm_iface['interface_id'],
4667 'type': vm_iface['type'],
4668 'floating_ip': vm_iface['floating_ip'],
4669 'port_security': vm_iface['port_security']
4670 }
4671 db_instance_interfaces.append(db_vm_iface)
4672 task_params_copy = deepcopy(task_params)
4673 for iface in task_params_copy[5]:
4674 iface["uuid"] = iface2iface[iface["uuid"]]
4675 # increment ip_address
4676 if "ip_address" in iface:
4677 ip = iface.get("ip_address")
4678 i = ip.rfind(".")
4679 if i > 0:
4680 try:
4681 i += 1
4682 ip = ip[i:] + str(int(ip[:i]) + 1)
4683 iface["ip_address"] = ip
4684 except:
4685 iface["ip_address"] = None
4686 if vm_name:
4687 task_params_copy[0] = vm_name
4688 db_vim_action = {
4689 "instance_action_id": instance_action_id,
4690 "task_index": task_index,
4691 "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
4692 "action": "CREATE",
4693 "status": "SCHEDULED",
4694 "item": "instance_vms",
4695 "item_id": vm_uuid,
4696 "related": target_vm["related"],
4697 # ALF
4698 # ALF
4699 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4700 # ALF
4701 # ALF
4702 "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
4703 }
4704 task_index += 1
4705 db_vim_actions.append(db_vim_action)
4706 vm_result["created"].append(vm_uuid)
4707
4708 db_instance_action["number_tasks"] = task_index
4709 db_tables = [
4710 {"instance_vms": db_instance_vms},
4711 {"instance_interfaces": db_instance_interfaces},
4712 {"instance_actions": db_instance_action},
4713 # TODO revise sfps
4714 # {"instance_sfis": db_instance_sfis},
4715 # {"instance_sfs": db_instance_sfs},
4716 # {"instance_classifications": db_instance_classifications},
4717 # {"instance_sfps": db_instance_sfps},
4718 {"vim_wim_actions": db_vim_actions}
4719 ]
4720 logger.debug("create_vdu done DB tables: %s",
4721 yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
4722 mydb.new_rows(db_tables, [])
4723 for myvim_thread in myvim_threads_id.values():
4724 vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
4725
4726 return vm_result
4727
4728 input_vnfs = action_dict.pop("vnfs", [])
4729 input_vms = action_dict.pop("vms", [])
4730 action_over_all = True if not input_vnfs and not input_vms else False
4731 for sce_vnf in instanceDict['vnfs']:
4732 for vm in sce_vnf['vms']:
4733 if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
4734 sce_vnf['member_vnf_index'] not in input_vnfs and \
4735 vm['uuid'] not in input_vms and vm['name'] not in input_vms:
4736 continue
4737 try:
4738 if "add_public_key" in action_dict:
4739 mgmt_access = {}
4740 if sce_vnf.get('mgmt_access'):
4741 mgmt_access = yaml.load(sce_vnf['mgmt_access'])
4742 ssh_access = mgmt_access['config-access']['ssh-access']
4743 tenant = mydb.get_rows_by_id('nfvo_tenants', nfvo_tenant)
4744 try:
4745 if ssh_access['required'] and ssh_access['default-user']:
4746 if 'ip_address' in vm:
4747 mgmt_ip = vm['ip_address'].split(';')
4748 password = mgmt_access['config-access'].get('password')
4749 priv_RO_key = decrypt_key(tenant[0]['encrypted_RO_priv_key'], tenant[0]['uuid'])
4750 myvim.inject_user_key(mgmt_ip[0], ssh_access['default-user'],
4751 action_dict['add_public_key'],
4752 password=password, ro_key=priv_RO_key)
4753 else:
4754 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4755 httperrors.Internal_Server_Error)
4756 except KeyError:
4757 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4758 httperrors.Internal_Server_Error)
4759 else:
4760 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm['uuid']),
4761 httperrors.Internal_Server_Error)
4762 else:
4763 data = myvim.action_vminstance(vm['vim_vm_id'], action_dict)
4764 if "console" in action_dict:
4765 if not global_config["http_console_proxy"]:
4766 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4767 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4768 protocol=data["protocol"],
4769 ip = data["server"],
4770 port = data["port"],
4771 suffix = data["suffix"]),
4772 "name":vm['name']
4773 }
4774 vm_ok +=1
4775 elif data["server"]=="127.0.0.1" or data["server"]=="localhost":
4776 vm_result[ vm['uuid'] ] = {"vim_result": -httperrors.Unauthorized,
4777 "description": "this console is only reachable by local interface",
4778 "name":vm['name']
4779 }
4780 vm_error+=1
4781 else:
4782 #print "console data", data
4783 try:
4784 console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
4785 vm_result[ vm['uuid'] ] = {"vim_result": 200,
4786 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4787 protocol=data["protocol"],
4788 ip = global_config["http_console_host"],
4789 port = console_thread.port,
4790 suffix = data["suffix"]),
4791 "name":vm['name']
4792 }
4793 vm_ok +=1
4794 except NfvoException as e:
4795 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4796 vm_error+=1
4797
4798 else:
4799 vm_result[ vm['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm['name']}
4800 vm_ok +=1
4801 except vimconn.vimconnException as e:
4802 vm_result[ vm['uuid'] ] = {"vim_result": e.http_code, "name":vm['name'], "description": str(e)}
4803 vm_error+=1
4804
4805 if vm_ok==0: #all goes wrong
4806 return vm_result
4807 else:
4808 return vm_result
4809
4810 def instance_action_get(mydb, nfvo_tenant, instance_id, action_id):
4811 filter = {}
4812 if nfvo_tenant and nfvo_tenant != "any":
4813 filter["tenant_id"] = nfvo_tenant
4814 if instance_id and instance_id != "any":
4815 filter["instance_id"] = instance_id
4816 if action_id:
4817 filter["uuid"] = action_id
4818 rows = mydb.get_rows(FROM="instance_actions", WHERE=filter)
4819 if action_id:
4820 if not rows:
4821 raise NfvoException("Not found any action with this criteria", httperrors.Not_Found)
4822 vim_wim_actions = mydb.get_rows(FROM="vim_wim_actions", WHERE={"instance_action_id": action_id})
4823 rows[0]["vim_wim_actions"] = vim_wim_actions
4824 # for backward compatibility set vim_actions = vim_wim_actions
4825 rows[0]["vim_actions"] = vim_wim_actions
4826 return {"actions": rows}
4827
4828
4829 def create_or_use_console_proxy_thread(console_server, console_port):
4830 #look for a non-used port
4831 console_thread_key = console_server + ":" + str(console_port)
4832 if console_thread_key in global_config["console_thread"]:
4833 #global_config["console_thread"][console_thread_key].start_timeout()
4834 return global_config["console_thread"][console_thread_key]
4835
4836 for port in global_config["console_port_iterator"]():
4837 #print "create_or_use_console_proxy_thread() port:", port
4838 if port in global_config["console_ports"]:
4839 continue
4840 try:
4841 clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
4842 clithread.start()
4843 global_config["console_thread"][console_thread_key] = clithread
4844 global_config["console_ports"][port] = console_thread_key
4845 return clithread
4846 except cli.ConsoleProxyExceptionPortUsed as e:
4847 #port used, try with onoher
4848 continue
4849 except cli.ConsoleProxyException as e:
4850 raise NfvoException(str(e), httperrors.Bad_Request)
4851 raise NfvoException("Not found any free 'http_console_ports'", httperrors.Conflict)
4852
4853
4854 def check_tenant(mydb, tenant_id):
4855 '''check that tenant exists at database'''
4856 tenant = mydb.get_rows(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
4857 if not tenant:
4858 raise NfvoException("tenant '{}' not found".format(tenant_id), httperrors.Not_Found)
4859 return
4860
4861 def new_tenant(mydb, tenant_dict):
4862
4863 tenant_uuid = str(uuid4())
4864 tenant_dict['uuid'] = tenant_uuid
4865 try:
4866 pub_key, priv_key = create_RO_keypair(tenant_uuid)
4867 tenant_dict['RO_pub_key'] = pub_key
4868 tenant_dict['encrypted_RO_priv_key'] = priv_key
4869 mydb.new_row("nfvo_tenants", tenant_dict, confidential_data=True)
4870 except db_base_Exception as e:
4871 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict['name']) + str(e), e.http_code)
4872 return tenant_uuid
4873
4874 def delete_tenant(mydb, tenant):
4875 #get nfvo_tenant info
4876
4877 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
4878 mydb.delete_row_by_id("nfvo_tenants", tenant_dict['uuid'])
4879 return tenant_dict['uuid'] + " " + tenant_dict["name"]
4880
4881
4882 def new_datacenter(mydb, datacenter_descriptor):
4883 sdn_port_mapping = None
4884 if "config" in datacenter_descriptor:
4885 sdn_port_mapping = datacenter_descriptor["config"].pop("sdn-port-mapping", None)
4886 datacenter_descriptor["config"] = yaml.safe_dump(datacenter_descriptor["config"], default_flow_style=True,
4887 width=256)
4888 # Check that datacenter-type is correct
4889 datacenter_type = datacenter_descriptor.get("type", "openvim");
4890 # module_info = None
4891 try:
4892 module = "vimconn_" + datacenter_type
4893 pkg = __import__("osm_ro." + module)
4894 # vim_conn = getattr(pkg, module)
4895 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
4896 except (IOError, ImportError):
4897 # if module_info and module_info[0]:
4898 # file.close(module_info[0])
4899 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type,
4900 module),
4901 httperrors.Bad_Request)
4902
4903 datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True, confidential_data=True)
4904 if sdn_port_mapping:
4905 try:
4906 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, sdn_port_mapping)
4907 except Exception as e:
4908 mydb.delete_row_by_id("datacenters", datacenter_id) # Rollback
4909 raise e
4910 return datacenter_id
4911
4912
4913 def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
4914 # obtain data, check that only one exist
4915 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
4916
4917 # edit data
4918 datacenter_id = datacenter['uuid']
4919 where = {'uuid': datacenter['uuid']}
4920 remove_port_mapping = False
4921 new_sdn_port_mapping = None
4922 if "config" in datacenter_descriptor:
4923 if datacenter_descriptor['config'] != None:
4924 try:
4925 new_config_dict = datacenter_descriptor["config"]
4926 if "sdn-port-mapping" in new_config_dict:
4927 remove_port_mapping = True
4928 new_sdn_port_mapping = new_config_dict.pop("sdn-port-mapping")
4929 # delete null fields
4930 to_delete = []
4931 for k in new_config_dict:
4932 if new_config_dict[k] is None:
4933 to_delete.append(k)
4934 if k == 'sdn-controller':
4935 remove_port_mapping = True
4936
4937 config_text = datacenter.get("config")
4938 if not config_text:
4939 config_text = '{}'
4940 config_dict = yaml.load(config_text)
4941 config_dict.update(new_config_dict)
4942 # delete null fields
4943 for k in to_delete:
4944 del config_dict[k]
4945 except Exception as e:
4946 raise NfvoException("Bad format at datacenter:config " + str(e), httperrors.Bad_Request)
4947 if config_dict:
4948 datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256)
4949 else:
4950 datacenter_descriptor["config"] = None
4951 if remove_port_mapping:
4952 try:
4953 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id)
4954 except ovimException as e:
4955 raise NfvoException("Error deleting datacenter-port-mapping " + str(e), httperrors.Conflict)
4956
4957 mydb.update_rows('datacenters', datacenter_descriptor, where)
4958 if new_sdn_port_mapping:
4959 try:
4960 datacenter_sdn_port_mapping_set(mydb, None, datacenter_id, new_sdn_port_mapping)
4961 except ovimException as e:
4962 # Rollback
4963 mydb.update_rows('datacenters', datacenter, where)
4964 raise NfvoException("Error adding datacenter-port-mapping " + str(e), httperrors.Conflict)
4965 return datacenter_id
4966
4967
4968 def delete_datacenter(mydb, datacenter):
4969 #get nfvo_tenant info
4970 datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
4971 mydb.delete_row_by_id("datacenters", datacenter_dict['uuid'])
4972 try:
4973 datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid'])
4974 except ovimException as e:
4975 raise NfvoException("Error deleting datacenter-port-mapping " + str(e))
4976 return datacenter_dict['uuid'] + " " + datacenter_dict['name']
4977
4978
4979 def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, vim_tenant=None, vim_tenant_name=None,
4980 vim_username=None, vim_password=None, config=None):
4981 # get datacenter info
4982 try:
4983 if not datacenter_id:
4984 if not vim_id:
4985 raise NfvoException("You must provide 'vim_id", http_code=httperrors.Bad_Request)
4986 datacenter_id = vim_id
4987 datacenter_id, datacenter_name = get_datacenter_uuid(mydb, None, datacenter_id)
4988
4989 create_vim_tenant = True if not vim_tenant and not vim_tenant_name else False
4990
4991 # get nfvo_tenant info
4992 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
4993 if vim_tenant_name==None:
4994 vim_tenant_name=tenant_dict['name']
4995
4996 tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
4997 # #check that this association does not exist before
4998 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4999 # if len(tenants_datacenters)>0:
5000 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
5001
5002 vim_tenant_id_exist_atdb=False
5003 if not create_vim_tenant:
5004 where_={"datacenter_id": datacenter_id}
5005 if vim_tenant!=None:
5006 where_["vim_tenant_id"] = vim_tenant
5007 if vim_tenant_name!=None:
5008 where_["vim_tenant_name"] = vim_tenant_name
5009 #check if vim_tenant_id is already at database
5010 datacenter_tenants_dict = mydb.get_rows(FROM='datacenter_tenants', WHERE=where_)
5011 if len(datacenter_tenants_dict)>=1:
5012 datacenter_tenants_dict = datacenter_tenants_dict[0]
5013 vim_tenant_id_exist_atdb=True
5014 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5015 else: #result=0
5016 datacenter_tenants_dict = {}
5017 #insert at table datacenter_tenants
5018 else: #if vim_tenant==None:
5019 #create tenant at VIM if not provided
5020 try:
5021 _, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username,
5022 vim_passwd=vim_password)
5023 datacenter_name = myvim["name"]
5024 vim_tenant = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
5025 except vimconn.vimconnException as e:
5026 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id, str(e)), httperrors.Internal_Server_Error)
5027 datacenter_tenants_dict = {}
5028 datacenter_tenants_dict["created"]="true"
5029
5030 #fill datacenter_tenants table
5031 if not vim_tenant_id_exist_atdb:
5032 datacenter_tenants_dict["vim_tenant_id"] = vim_tenant
5033 datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
5034 datacenter_tenants_dict["user"] = vim_username
5035 datacenter_tenants_dict["passwd"] = vim_password
5036 datacenter_tenants_dict["datacenter_id"] = datacenter_id
5037 if name:
5038 datacenter_tenants_dict["name"] = name
5039 else:
5040 datacenter_tenants_dict["name"] = datacenter_name
5041 if config:
5042 datacenter_tenants_dict["config"] = yaml.safe_dump(config, default_flow_style=True, width=256)
5043 id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, add_uuid=True, confidential_data=True)
5044 datacenter_tenants_dict["uuid"] = id_
5045
5046 #fill tenants_datacenters table
5047 datacenter_tenant_id = datacenter_tenants_dict["uuid"]
5048 tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id
5049 mydb.new_row('tenants_datacenters', tenants_datacenter_dict)
5050
5051 # create thread
5052 thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
5053 new_thread = vim_thread.vim_thread(task_lock, thread_name, datacenter_name, datacenter_tenant_id,
5054 db=db, db_lock=db_lock, ovim=ovim)
5055 new_thread.start()
5056 thread_id = datacenter_tenants_dict["uuid"]
5057 vim_threads["running"][thread_id] = new_thread
5058 return thread_id
5059 except vimconn.vimconnException as e:
5060 raise NfvoException(str(e), httperrors.Bad_Request)
5061
5062
5063 def edit_vim_account(mydb, nfvo_tenant, datacenter_tenant_id, datacenter_id=None, name=None, vim_tenant=None,
5064 vim_tenant_name=None, vim_username=None, vim_password=None, config=None):
5065
5066 # get vim_account; check is valid for this tenant
5067 from_ = "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5068 where_ = {"td.nfvo_tenant_id": nfvo_tenant}
5069 if datacenter_tenant_id:
5070 where_["dt.uuid"] = datacenter_tenant_id
5071 if datacenter_id:
5072 where_["dt.datacenter_id"] = datacenter_id
5073 vim_accounts = mydb.get_rows(SELECT="dt.uuid as uuid, config", FROM=from_, WHERE=where_)
5074 if not vim_accounts:
5075 raise NfvoException("vim_account not found for this tenant", http_code=httperrors.Not_Found)
5076 elif len(vim_accounts) > 1:
5077 raise NfvoException("found more than one vim_account for this tenant", http_code=httperrors.Conflict)
5078 datacenter_tenant_id = vim_accounts[0]["uuid"]
5079 original_config = vim_accounts[0]["config"]
5080
5081 update_ = {}
5082 if config:
5083 original_config_dict = yaml.load(original_config)
5084 original_config_dict.update(config)
5085 update["config"] = yaml.safe_dump(original_config_dict, default_flow_style=True, width=256)
5086 if name:
5087 update_['name'] = name
5088 if vim_tenant:
5089 update_['vim_tenant_id'] = vim_tenant
5090 if vim_tenant_name:
5091 update_['vim_tenant_name'] = vim_tenant_name
5092 if vim_username:
5093 update_['user'] = vim_username
5094 if vim_password:
5095 update_['passwd'] = vim_password
5096 if update_:
5097 mydb.update_rows("datacenter_tenants", UPDATE=update_, WHERE={"uuid": datacenter_tenant_id})
5098
5099 vim_threads["running"][datacenter_tenant_id].insert_task("reload")
5100 return datacenter_tenant_id
5101
5102 def delete_vim_account(mydb, tenant_id, vim_account_id, datacenter=None):
5103 #get nfvo_tenant info
5104 if not tenant_id or tenant_id=="any":
5105 tenant_uuid = None
5106 else:
5107 tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
5108 tenant_uuid = tenant_dict['uuid']
5109
5110 #check that this association exist before
5111 tenants_datacenter_dict = {}
5112 if datacenter:
5113 datacenter_id, _ = get_datacenter_uuid(mydb, tenant_uuid, datacenter)
5114 tenants_datacenter_dict["datacenter_id"] = datacenter_id
5115 elif vim_account_id:
5116 tenants_datacenter_dict["datacenter_tenant_id"] = vim_account_id
5117 if tenant_uuid:
5118 tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
5119 tenant_datacenter_list = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5120 if len(tenant_datacenter_list)==0 and tenant_uuid:
5121 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Not_Found)
5122
5123 #delete this association
5124 mydb.delete_row(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5125
5126 #get vim_tenant info and deletes
5127 warning=''
5128 for tenant_datacenter_item in tenant_datacenter_list:
5129 vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5130 #try to delete vim:tenant
5131 try:
5132 mydb.delete_row_by_id('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
5133 if vim_tenant_dict['created']=='true':
5134 #delete tenant at VIM if created by NFVO
5135 try:
5136 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5137 myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
5138 except vimconn.vimconnException as e:
5139 warning = "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict['vim_tenant_id'], str(e))
5140 logger.warn(warning)
5141 except db_base_Exception as e:
5142 logger.error("Cannot delete datacenter_tenants " + str(e))
5143 pass # the error will be caused because dependencies, vim_tenant can not be deleted
5144 thread_id = tenant_datacenter_item["datacenter_tenant_id"]
5145 thread = vim_threads["running"].get(thread_id)
5146 if thread:
5147 thread.insert_task("exit")
5148 vim_threads["deleting"][thread_id] = thread
5149 return "datacenter {} detached. {}".format(datacenter_id, warning)
5150
5151
5152 def datacenter_action(mydb, tenant_id, datacenter, action_dict):
5153 #DEPRECATED
5154 #get datacenter info
5155 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5156
5157 if 'net-update' in action_dict:
5158 try:
5159 nets = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
5160 #print content
5161 except vimconn.vimconnException as e:
5162 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5163 raise NfvoException(str(e), httperrors.Internal_Server_Error)
5164 #update nets Change from VIM format to NFVO format
5165 net_list=[]
5166 for net in nets:
5167 net_nfvo={'datacenter_id': datacenter_id}
5168 net_nfvo['name'] = net['name']
5169 #net_nfvo['description']= net['name']
5170 net_nfvo['vim_net_id'] = net['id']
5171 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5172 net_nfvo['shared'] = net['shared']
5173 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
5174 net_list.append(net_nfvo)
5175 inserted, deleted = mydb.update_datacenter_nets(datacenter_id, net_list)
5176 logger.info("Inserted %d nets, deleted %d old nets", inserted, deleted)
5177 return inserted
5178 elif 'net-edit' in action_dict:
5179 net = action_dict['net-edit'].pop('net')
5180 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
5181 result = mydb.update_rows('datacenter_nets', action_dict['net-edit'],
5182 WHERE={'datacenter_id':datacenter_id, what: net})
5183 return result
5184 elif 'net-delete' in action_dict:
5185 net = action_dict['net-deelte'].get('net')
5186 what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name'
5187 result = mydb.delete_row(FROM='datacenter_nets',
5188 WHERE={'datacenter_id':datacenter_id, what: net})
5189 return result
5190
5191 else:
5192 raise NfvoException("Unknown action " + str(action_dict), httperrors.Bad_Request)
5193
5194
5195 def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
5196 #get datacenter info
5197 datacenter_id, _ = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5198
5199 what = 'uuid' if utils.check_valid_uuid(netmap) else 'name'
5200 result = mydb.update_rows('datacenter_nets', action_dict['netmap'],
5201 WHERE={'datacenter_id':datacenter_id, what: netmap})
5202 return result
5203
5204
5205 def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None):
5206 #get datacenter info
5207 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5208 filter_dict={}
5209 if action_dict:
5210 action_dict = action_dict["netmap"]
5211 if 'vim_id' in action_dict:
5212 filter_dict["id"] = action_dict['vim_id']
5213 if 'vim_name' in action_dict:
5214 filter_dict["name"] = action_dict['vim_name']
5215 else:
5216 filter_dict["shared"] = True
5217
5218 try:
5219 vim_nets = myvim.get_network_list(filter_dict=filter_dict)
5220 except vimconn.vimconnException as e:
5221 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5222 raise NfvoException(str(e), httperrors.Internal_Server_Error)
5223 if len(vim_nets)>1 and action_dict:
5224 raise NfvoException("more than two networks found, specify with vim_id", httperrors.Conflict)
5225 elif len(vim_nets)==0: # and action_dict:
5226 raise NfvoException("Not found a network at VIM with " + str(filter_dict), httperrors.Not_Found)
5227 net_list=[]
5228 for net in vim_nets:
5229 net_nfvo={'datacenter_id': datacenter_id}
5230 if action_dict and "name" in action_dict:
5231 net_nfvo['name'] = action_dict['name']
5232 else:
5233 net_nfvo['name'] = net['name']
5234 #net_nfvo['description']= net['name']
5235 net_nfvo['vim_net_id'] = net['id']
5236 net_nfvo['type'] = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5237 net_nfvo['shared'] = net['shared']
5238 net_nfvo['multipoint'] = False if net['type']=='ptp' else True
5239 try:
5240 net_id = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
5241 net_nfvo["status"] = "OK"
5242 net_nfvo["uuid"] = net_id
5243 except db_base_Exception as e:
5244 if action_dict:
5245 raise
5246 else:
5247 net_nfvo["status"] = "FAIL: " + str(e)
5248 net_list.append(net_nfvo)
5249 return net_list
5250
5251 def get_sdn_net_id(mydb, tenant_id, datacenter, network_id):
5252 # obtain all network data
5253 try:
5254 if utils.check_valid_uuid(network_id):
5255 filter_dict = {"id": network_id}
5256 else:
5257 filter_dict = {"name": network_id}
5258
5259 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5260 network = myvim.get_network_list(filter_dict=filter_dict)
5261 except vimconn.vimconnException as e:
5262 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e)), e.http_code)
5263
5264 # ensure the network is defined
5265 if len(network) == 0:
5266 raise NfvoException("Network {} is not present in the system".format(network_id),
5267 httperrors.Bad_Request)
5268
5269 # ensure there is only one network with the provided name
5270 if len(network) > 1:
5271 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), httperrors.Bad_Request)
5272
5273 # ensure it is a dataplane network
5274 if network[0]['type'] != 'data':
5275 return None
5276
5277 # ensure we use the id
5278 network_id = network[0]['id']
5279
5280 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5281 # and with instance_scenario_id==NULL
5282 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5283 search_dict = {'vim_net_id': network_id}
5284
5285 try:
5286 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5287 result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)
5288 except db_base_Exception as e:
5289 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5290 network_id) + str(e), e.http_code)
5291
5292 sdn_net_counter = 0
5293 for net in result:
5294 if net['sdn_net_id'] != None:
5295 sdn_net_counter+=1
5296 sdn_net_id = net['sdn_net_id']
5297
5298 if sdn_net_counter == 0:
5299 return None
5300 elif sdn_net_counter == 1:
5301 return sdn_net_id
5302 else:
5303 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5304 network_id), httperrors.Internal_Server_Error)
5305
5306 def get_sdn_controller_id(mydb, datacenter):
5307 # Obtain sdn controller id
5308 config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}')
5309 if not config:
5310 return None
5311
5312 return yaml.load(config).get('sdn-controller')
5313
5314 def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor):
5315 try:
5316 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5317 if not sdn_network_id:
5318 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), httperrors.Internal_Server_Error)
5319
5320 #Obtain sdn controller id
5321 controller_id = get_sdn_controller_id(mydb, datacenter)
5322 if not controller_id:
5323 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), httperrors.Internal_Server_Error)
5324
5325 #Obtain sdn controller info
5326 sdn_controller = ovim.show_of_controller(controller_id)
5327
5328 port_data = {
5329 'name': 'external_port',
5330 'net_id': sdn_network_id,
5331 'ofc_id': controller_id,
5332 'switch_dpid': sdn_controller['dpid'],
5333 'switch_port': descriptor['port']
5334 }
5335
5336 if 'vlan' in descriptor:
5337 port_data['vlan'] = descriptor['vlan']
5338 if 'mac' in descriptor:
5339 port_data['mac'] = descriptor['mac']
5340
5341 result = ovim.new_port(port_data)
5342 except ovimException as e:
5343 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5344 sdn_network_id, network_id) + str(e), httperrors.Internal_Server_Error)
5345 except db_base_Exception as e:
5346 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5347 network_id) + str(e), e.http_code)
5348
5349 return 'Port uuid: '+ result
5350
5351 def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None):
5352 if port_id:
5353 filter = {'uuid': port_id}
5354 else:
5355 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id)
5356 if not sdn_network_id:
5357 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id),
5358 httperrors.Internal_Server_Error)
5359 #in case no port_id is specified only ports marked as 'external_port' will be detached
5360 filter = {'name': 'external_port', 'net_id': sdn_network_id}
5361
5362 try:
5363 port_list = ovim.get_ports(columns={'uuid'}, filter=filter)
5364 except ovimException as e:
5365 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
5366 httperrors.Internal_Server_Error)
5367
5368 if len(port_list) == 0:
5369 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id),
5370 httperrors.Bad_Request)
5371
5372 port_uuid_list = []
5373 for port in port_list:
5374 try:
5375 port_uuid_list.append(port['uuid'])
5376 ovim.delete_port(port['uuid'])
5377 except ovimException as e:
5378 raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), httperrors.Internal_Server_Error)
5379
5380 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list))
5381
5382 def vim_action_get(mydb, tenant_id, datacenter, item, name):
5383 #get datacenter info
5384 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5385 filter_dict={}
5386 if name:
5387 if utils.check_valid_uuid(name):
5388 filter_dict["id"] = name
5389 else:
5390 filter_dict["name"] = name
5391 try:
5392 if item=="networks":
5393 #filter_dict['tenant_id'] = myvim['tenant_id']
5394 content = myvim.get_network_list(filter_dict=filter_dict)
5395
5396 if len(content) == 0:
5397 raise NfvoException("Network {} is not present in the system. ".format(name),
5398 httperrors.Bad_Request)
5399
5400 #Update the networks with the attached ports
5401 for net in content:
5402 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id'])
5403 if sdn_network_id != None:
5404 try:
5405 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5406 port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id})
5407 except ovimException as e:
5408 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), httperrors.Internal_Server_Error)
5409 #Remove field name and if port name is external_port save it as 'type'
5410 for port in port_list:
5411 if port['name'] == 'external_port':
5412 port['type'] = "External"
5413 del port['name']
5414 net['sdn_network_id'] = sdn_network_id
5415 net['sdn_attached_ports'] = port_list
5416
5417 elif item=="tenants":
5418 content = myvim.get_tenant_list(filter_dict=filter_dict)
5419 elif item == "images":
5420
5421 content = myvim.get_image_list(filter_dict=filter_dict)
5422 else:
5423 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
5424 logger.debug("vim_action response %s", content) #update nets Change from VIM format to NFVO format
5425 if name and len(content)==1:
5426 return {item[:-1]: content[0]}
5427 elif name and len(content)==0:
5428 raise NfvoException("No {} found with ".format(item[:-1]) + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())),
5429 datacenter)
5430 else:
5431 return {item: content}
5432 except vimconn.vimconnException as e:
5433 print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e))
5434 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code)
5435
5436
5437 def vim_action_delete(mydb, tenant_id, datacenter, item, name):
5438 #get datacenter info
5439 if tenant_id == "any":
5440 tenant_id=None
5441
5442 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5443 #get uuid name
5444 content = vim_action_get(mydb, tenant_id, datacenter, item, name)
5445 logger.debug("vim_action_delete vim response: " + str(content))
5446 items = content.values()[0]
5447 if type(items)==list and len(items)==0:
5448 raise NfvoException("Not found " + item, httperrors.Not_Found)
5449 elif type(items)==list and len(items)>1:
5450 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item), httperrors.Not_Found)
5451 else: # it is a dict
5452 item_id = items["id"]
5453 item_name = str(items.get("name"))
5454
5455 try:
5456 if item=="networks":
5457 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5458 sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id)
5459 if sdn_network_id != None:
5460 #Delete any port attachment to this network
5461 try:
5462 port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id})
5463 except ovimException as e:
5464 raise NfvoException(
5465 "ovimException obtaining external ports for net {}. ".format(network_id) + str(e),
5466 httperrors.Internal_Server_Error)
5467
5468 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5469 for port in port_list:
5470 vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid'])
5471
5472 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5473 try:
5474 mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id})
5475 except db_base_Exception as e:
5476 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) +
5477 str(e), e.http_code)
5478
5479 #Delete the SDN network
5480 try:
5481 ovim.delete_network(sdn_network_id)
5482 except ovimException as e:
5483 logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True)
5484 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e),
5485 httperrors.Internal_Server_Error)
5486
5487 content = myvim.delete_network(item_id)
5488 elif item=="tenants":
5489 content = myvim.delete_tenant(item_id)
5490 elif item == "images":
5491 content = myvim.delete_image(item_id)
5492 else:
5493 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
5494 except vimconn.vimconnException as e:
5495 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5496 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item, name, str(e)), e.http_code)
5497
5498 return "{} {} {} deleted".format(item[:-1], item_id,item_name)
5499
5500
5501 def vim_action_create(mydb, tenant_id, datacenter, item, descriptor):
5502 #get datacenter info
5503 logger.debug("vim_action_create descriptor %s", str(descriptor))
5504 if tenant_id == "any":
5505 tenant_id=None
5506 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
5507 try:
5508 if item=="networks":
5509 net = descriptor["network"]
5510 net_name = net.pop("name")
5511 net_type = net.pop("type", "bridge")
5512 net_public = net.pop("shared", False)
5513 net_ipprofile = net.pop("ip_profile", None)
5514 net_vlan = net.pop("vlan", None)
5515 content, _ = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net)
5516
5517 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5518 if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'):
5519 #obtain datacenter_tenant_id
5520 datacenter_tenant_id = mydb.get_rows(SELECT=('uuid',),
5521 FROM='datacenter_tenants',
5522 WHERE={'datacenter_id': datacenter})[0]['uuid']
5523 try:
5524 sdn_network = {}
5525 sdn_network['vlan'] = net_vlan
5526 sdn_network['type'] = net_type
5527 sdn_network['name'] = net_name
5528 sdn_network['region'] = datacenter_tenant_id
5529 ovim_content = ovim.new_network(sdn_network)
5530 except ovimException as e:
5531 logger.error("ovimException creating SDN network={} ".format(
5532 sdn_network) + str(e), exc_info=True)
5533 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e),
5534 httperrors.Internal_Server_Error)
5535
5536 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5537 # use instance_scenario_id=None to distinguish from real instaces of nets
5538 correspondence = {'instance_scenario_id': None,
5539 'sdn_net_id': ovim_content,
5540 'vim_net_id': content,
5541 'datacenter_tenant_id': datacenter_tenant_id
5542 }
5543 try:
5544 mydb.new_row('instance_nets', correspondence, add_uuid=True)
5545 except db_base_Exception as e:
5546 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5547 correspondence, e), e.http_code)
5548 elif item=="tenants":
5549 tenant = descriptor["tenant"]
5550 content = myvim.new_tenant(tenant["name"], tenant.get("description"))
5551 else:
5552 raise NfvoException(item + "?", httperrors.Method_Not_Allowed)
5553 except vimconn.vimconnException as e:
5554 raise NfvoException("Not possible to create {} at VIM: {}".format(item, str(e)), e.http_code)
5555
5556 return vim_action_get(mydb, tenant_id, datacenter, item, content)
5557
5558 def sdn_controller_create(mydb, tenant_id, sdn_controller):
5559 data = ovim.new_of_controller(sdn_controller)
5560 logger.debug('New SDN controller created with uuid {}'.format(data))
5561 return data
5562
5563 def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller):
5564 data = ovim.edit_of_controller(controller_id, sdn_controller)
5565 msg = 'SDN controller {} updated'.format(data)
5566 logger.debug(msg)
5567 return msg
5568
5569 def sdn_controller_list(mydb, tenant_id, controller_id=None):
5570 if controller_id == None:
5571 data = ovim.get_of_controllers()
5572 else:
5573 data = ovim.show_of_controller(controller_id)
5574
5575 msg = 'SDN controller list:\n {}'.format(data)
5576 logger.debug(msg)
5577 return data
5578
5579 def sdn_controller_delete(mydb, tenant_id, controller_id):
5580 select_ = ('uuid', 'config')
5581 datacenters = mydb.get_rows(FROM='datacenters', SELECT=select_)
5582 for datacenter in datacenters:
5583 if datacenter['config']:
5584 config = yaml.load(datacenter['config'])
5585 if 'sdn-controller' in config and config['sdn-controller'] == controller_id:
5586 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id, datacenter['uuid']), httperrors.Conflict)
5587
5588 data = ovim.delete_of_controller(controller_id)
5589 msg = 'SDN controller {} deleted'.format(data)
5590 logger.debug(msg)
5591 return msg
5592
5593 def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_mapping):
5594 controller = mydb.get_rows(FROM="datacenters", SELECT=("config",), WHERE={"uuid":datacenter_id})
5595 if len(controller) < 1:
5596 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id), httperrors.Not_Found)
5597
5598 try:
5599 sdn_controller_id = yaml.load(controller[0]["config"])["sdn-controller"]
5600 except:
5601 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id), httperrors.Bad_Request)
5602
5603 sdn_controller = ovim.show_of_controller(sdn_controller_id)
5604 switch_dpid = sdn_controller["dpid"]
5605
5606 maps = list()
5607 for compute_node in sdn_port_mapping:
5608 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5609 element = dict()
5610 element["compute_node"] = compute_node["compute_node"]
5611 for port in compute_node["ports"]:
5612 pci = port.get("pci")
5613 element["switch_port"] = port.get("switch_port")
5614 element["switch_mac"] = port.get("switch_mac")
5615 if not element["switch_port"] and not element["switch_mac"]:
5616 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors.Bad_Request)
5617 for pci_expanded in utils.expand_brackets(pci):
5618 element["pci"] = pci_expanded
5619 maps.append(dict(element))
5620
5621 return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)
5622
5623 def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id):
5624 maps = ovim.get_of_port_mappings(db_filter={"region": datacenter_id})
5625
5626 result = {
5627 "sdn-controller": None,
5628 "datacenter-id": datacenter_id,
5629 "dpid": None,
5630 "ports_mapping": list()
5631 }
5632
5633 datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id)
5634 if datacenter['config']:
5635 config = yaml.load(datacenter['config'])
5636 if 'sdn-controller' in config:
5637 controller_id = config['sdn-controller']
5638 sdn_controller = sdn_controller_list(mydb, tenant_id, controller_id)
5639 result["sdn-controller"] = controller_id
5640 result["dpid"] = sdn_controller["dpid"]
5641
5642 if result["sdn-controller"] == None:
5643 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), httperrors.Bad_Request)
5644 if result["dpid"] == None:
5645 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]),
5646 httperrors.Internal_Server_Error)
5647
5648 if len(maps) == 0:
5649 return result
5650
5651 ports_correspondence_dict = dict()
5652 for link in maps:
5653 if result["sdn-controller"] != link["ofc_id"]:
5654 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors.Internal_Server_Error)
5655 if result["dpid"] != link["switch_dpid"]:
5656 raise NfvoException("The dpid specified for different port mappings differ", httperrors.Internal_Server_Error)
5657 element = dict()
5658 element["pci"] = link["pci"]
5659 if link["switch_port"]:
5660 element["switch_port"] = link["switch_port"]
5661 if link["switch_mac"]:
5662 element["switch_mac"] = link["switch_mac"]
5663
5664 if not link["compute_node"] in ports_correspondence_dict:
5665 content = dict()
5666 content["compute_node"] = link["compute_node"]
5667 content["ports"] = list()
5668 ports_correspondence_dict[link["compute_node"]] = content
5669
5670 ports_correspondence_dict[link["compute_node"]]["ports"].append(element)
5671
5672 for key in sorted(ports_correspondence_dict):
5673 result["ports_mapping"].append(ports_correspondence_dict[key])
5674
5675 return result
5676
5677 def datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id):
5678 return ovim.clear_of_port_mapping(db_filter={"region":datacenter_id})
5679
5680 def create_RO_keypair(tenant_id):
5681 """
5682 Creates a public / private keys for a RO tenant and returns their values
5683 Params:
5684 tenant_id: ID of the tenant
5685 Return:
5686 public_key: Public key for the RO tenant
5687 private_key: Encrypted private key for RO tenant
5688 """
5689
5690 bits = 2048
5691 key = RSA.generate(bits)
5692 try:
5693 public_key = key.publickey().exportKey('OpenSSH')
5694 if isinstance(public_key, ValueError):
5695 raise NfvoException("Unable to create public key: {}".format(public_key), httperrors.Internal_Server_Error)
5696 private_key = key.exportKey(passphrase=tenant_id, pkcs=8)
5697 except (ValueError, NameError) as e:
5698 raise NfvoException("Unable to create private key: {}".format(e), httperrors.Internal_Server_Error)
5699 return public_key, private_key
5700
5701 def decrypt_key (key, tenant_id):
5702 """
5703 Decrypts an encrypted RSA key
5704 Params:
5705 key: Private key to be decrypted
5706 tenant_id: ID of the tenant
5707 Return:
5708 unencrypted_key: Unencrypted private key for RO tenant
5709 """
5710 try:
5711 key = RSA.importKey(key,tenant_id)
5712 unencrypted_key = key.exportKey('PEM')
5713 if isinstance(unencrypted_key, ValueError):
5714 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key), httperrors.Internal_Server_Error)
5715 except ValueError as e:
5716 raise NfvoException("Unable to decrypt the private key: {}".format(e), httperrors.Internal_Server_Error)
5717 return unencrypted_key