96e0a7a0483de69a4d91fc72275aecfbfb264b83
[osm/RO.git] / RO / 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 from random import choice as random_choice
34 from osm_ro import utils
35 from osm_ro.utils import deprecated
36 from osm_ro.vim_thread import vim_thread
37 import osm_ro.console_proxy_thread as cli
38 from osm_ro import vimconn
39 import logging
40 import collections
41 import math
42 from uuid import uuid4
43 from osm_ro.db_base import db_base_Exception
44
45 from osm_ro import nfvo_db
46 from threading import Lock
47 import time as t
48 # TODO py3 BEGIN
49 from osm_ro.sdn import Sdn, SdnException as ovimException
50 # from lib_osm_openvim.ovim import ovimException
51 # from unittest.mock import MagicMock
52 # class ovimException(Exception):
53 # pass
54 # TODO py3 END
55
56 from Crypto.PublicKey import RSA
57
58 import osm_im.vnfd as vnfd_catalog
59 import osm_im.nsd as nsd_catalog
60 from pyangbind.lib.serialise import pybindJSONDecoder
61 from copy import deepcopy
62 from pkg_resources import iter_entry_points
63
64
65 # WIM
66 from .wim import sdnconn
67 from .wim.wimconn_dummy import DummyConnector
68 from .wim.failing_connector import FailingConnector
69 from .http_tools import errors as httperrors
70 from .wim.engine import WimEngine
71 from .wim.persistence import WimPersistence
72 from copy import deepcopy
73 from pprint import pformat
74 #
75
76 global global_config
77 # WIM
78 global wim_engine
79 wim_engine = None
80 global sdnconn_imported
81 #
82 global logger
83 global default_volume_size
84 default_volume_size = '5' #size in GB
85 global ovim
86 ovim = None
87 global_config = None
88
89 plugins = {} # dictionary with VIM type as key, loaded module as value
90 vim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
91 vim_persistent_info = {}
92 # WIM
93 sdnconn_imported = {} # dictionary with WIM type as key, loaded module as value
94 wim_threads = {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
95 wim_persistent_info = {}
96 #
97
98 logger = logging.getLogger('openmano.nfvo')
99 task_lock = Lock()
100 last_task_id = 0.0
101 db = None
102 db_lock = Lock()
103
104 worker_id = None
105
106 class NfvoException(httperrors.HttpMappedError):
107 """Common Class for NFVO errors"""
108
109 def _load_plugin(name, type="vim"):
110 # type can be vim or sdn
111 global plugins
112 try:
113 for v in iter_entry_points('osm_ro{}.plugins'.format(type), name):
114 plugins[name] = v.load()
115 except Exception as e:
116 logger.critical("Cannot load osm_{}: {}".format(name, e))
117 if name:
118 plugins[name] = FailingConnector("Cannot load osm_{}: {}".format(name, e))
119 if name and name not in plugins:
120 error_text = "Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been" \
121 " registered".format(t=type, n=name)
122 logger.critical(error_text)
123 plugins[name] = FailingConnector(error_text)
124 # raise NfvoException("Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been registered".
125 # format(t=type, n=name), httperrors.Bad_Request)
126
127 def get_task_id():
128 global last_task_id
129 task_id = t.time()
130 if task_id <= last_task_id:
131 task_id = last_task_id + 0.000001
132 last_task_id = task_id
133 return "ACTION-{:.6f}".format(task_id)
134 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
135
136
137 def new_task(name, params, depends=None):
138 """Deprected!!!"""
139 task_id = get_task_id()
140 task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
141 if depends:
142 task["depends"] = depends
143 return task
144
145
146 def is_task_id(id):
147 return True if id[:5] == "TASK-" else False
148
149 def get_process_id():
150 """
151 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
152 will provide a random one
153 :return: Obtained ID
154 """
155 # Try getting docker id. If fails, get pid
156 try:
157 with open("/proc/self/cgroup", "r") as f:
158 text_id_ = f.readline()
159 _, _, text_id = text_id_.rpartition("/")
160 text_id = text_id.replace("\n", "")[:12]
161 if text_id:
162 return text_id
163 except Exception:
164 pass
165 # Return a random id
166 return "".join(random_choice("0123456789abcdef") for _ in range(12))
167
168 def get_non_used_vim_name(datacenter_name, datacenter_id):
169 return "{}:{}:{}".format(
170 worker_id[:12], datacenter_id.replace("-", "")[:32], datacenter_name[:16]
171 )
172
173 # -- Move
174 def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id):
175 name = wim_name[:16]
176 if name not in wim_threads["names"]:
177 wim_threads["names"].append(name)
178 return name
179 name = wim_name[:16] + "." + tenant_name[:16]
180 if name not in wim_threads["names"]:
181 wim_threads["names"].append(name)
182 return name
183 name = wim_id + "-" + tenant_id
184 wim_threads["names"].append(name)
185 return name
186
187
188 def start_service(mydb, persistence=None, wim=None):
189 global db, global_config, plugins, ovim, worker_id
190 db = nfvo_db.nfvo_db(lock=db_lock)
191 mydb.lock = db_lock
192 db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
193
194 persistence = persistence or WimPersistence(db)
195
196 try:
197 worker_id = get_process_id()
198 if "rosdn_dummy" not in plugins:
199 plugins["rosdn_dummy"] = DummyConnector
200 # starts ovim library
201 ovim = Sdn(db, plugins)
202
203 global wim_engine
204 wim_engine = wim or WimEngine(persistence, plugins)
205 wim_engine.ovim = ovim
206
207 ovim.start_service()
208
209 #delete old unneeded vim_wim_actions
210 clean_db(mydb)
211
212 # starts vim_threads
213 from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
214 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
215 select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
216 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
217 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
218 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
219 vims = mydb.get_rows(FROM=from_, SELECT=select_)
220 for vim in vims:
221 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
222 'datacenter_id': vim.get('datacenter_id')}
223 if vim["config"]:
224 extra.update(yaml.load(vim["config"], Loader=yaml.Loader))
225 if vim.get('dt_config'):
226 extra.update(yaml.load(vim["dt_config"], Loader=yaml.Loader))
227 plugin_name = "rovim_" + vim["type"]
228 if plugin_name not in plugins:
229 _load_plugin(plugin_name, type="vim")
230
231 thread_id = vim['datacenter_tenant_id']
232 vim_persistent_info[thread_id] = {}
233 try:
234 #if not tenant:
235 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
236 myvim = plugins[plugin_name].vimconnector(
237 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
238 tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
239 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
240 user=vim['user'], passwd=vim['passwd'],
241 config=extra, persistent_info=vim_persistent_info[thread_id]
242 )
243 except vimconn.vimconnException as e:
244 myvim = e
245 logger.error("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
246 vim['datacenter_id'], e))
247 except Exception as e:
248 logger.critical("Cannot launch thread for VIM {} '{}': {}".format(vim['datacenter_name'],
249 vim['datacenter_id'], e))
250 # raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
251 # httperrors.Internal_Server_Error)
252 thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['datacenter_id'])
253 new_thread = vim_thread(task_lock, plugins, thread_name, None,
254 vim['datacenter_tenant_id'], db=db)
255 new_thread.start()
256 vim_threads["running"][thread_id] = new_thread
257 wims = mydb.get_rows(FROM="wim_accounts join wims on wim_accounts.wim_id=wims.uuid",
258 WHERE={"sdn": "true"},
259 SELECT=("wim_accounts.uuid as uuid", "type", "wim_accounts.name as name"))
260 for wim in wims:
261 plugin_name = "rosdn_" + wim["type"]
262 if plugin_name not in plugins:
263 _load_plugin(plugin_name, type="sdn")
264
265 thread_id = wim['uuid']
266 thread_name = get_non_used_vim_name(wim['name'], wim['uuid'])
267 new_thread = vim_thread(task_lock, plugins, thread_name, wim['uuid'], None, db=db)
268 new_thread.start()
269 vim_threads["running"][thread_id] = new_thread
270 wim_engine.start_threads()
271 except db_base_Exception as e:
272 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
273 except ovimException as e:
274 message = str(e)
275 if message[:22] == "DATABASE wrong version":
276 message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
277 "at host {dbhost}".format(
278 msg=message[22:-3], dbname=global_config["db_ovim_name"],
279 dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"],
280 ver=message[-3:-1], dbhost=global_config["db_ovim_host"])
281 raise NfvoException(message, httperrors.Bad_Request)
282
283
284 def stop_service():
285 global ovim, global_config
286 if ovim:
287 ovim.stop_service()
288 for thread_id, thread in vim_threads["running"].items():
289 thread.insert_task("exit")
290 vim_threads["deleting"][thread_id] = thread
291 vim_threads["running"] = {}
292
293 if wim_engine:
294 wim_engine.stop_threads()
295
296 if global_config and global_config.get("console_thread"):
297 for thread in global_config["console_thread"]:
298 thread.terminate = True
299
300 def get_version():
301 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"],
302 global_config["version_date"] ))
303
304 def clean_db(mydb):
305 """
306 Clean unused or old entries at database to avoid unlimited growing
307 :param mydb: database connector
308 :return: None
309 """
310 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
311 now = t.time()-3600*24*7
312 instance_action_id = None
313 nb_deleted = 0
314 while True:
315 actions_to_delete = mydb.get_rows(
316 SELECT=("item", "item_id", "instance_action_id"),
317 FROM="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
318 "left join instance_scenarios as i on ia.instance_id=i.uuid",
319 WHERE={"va.action": "DELETE", "va.modified_at<": now, "i.uuid": None,
320 "va.status": ("DONE", "SUPERSEDED")},
321 LIMIT=100
322 )
323 for to_delete in actions_to_delete:
324 mydb.delete_row(FROM="vim_wim_actions", WHERE=to_delete)
325 if instance_action_id != to_delete["instance_action_id"]:
326 instance_action_id = to_delete["instance_action_id"]
327 mydb.delete_row(FROM="instance_actions", WHERE={"uuid": instance_action_id})
328 nb_deleted += len(actions_to_delete)
329 if len(actions_to_delete) < 100:
330 break
331 # clean locks
332 mydb.update_rows("vim_wim_actions", UPDATE={"worker": None}, WHERE={"worker<>": None})
333
334 if nb_deleted:
335 logger.debug("Removed {} unused vim_wim_actions".format(nb_deleted))
336
337
338 def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
339 '''Obtain flavorList
340 return result, content:
341 <0, error_text upon error
342 nb_records, flavor_list on success
343 '''
344 WHERE_dict={}
345 WHERE_dict['vnf_id'] = vnf_id
346 if nfvo_tenant is not None:
347 WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
348
349 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
350 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
351 flavors = mydb.get_rows(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
352 #print "get_flavor_list result:", result
353 #print "get_flavor_list content:", content
354 flavorList=[]
355 for flavor in flavors:
356 flavorList.append(flavor['flavor_id'])
357 return flavorList
358
359
360 def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
361 """
362 Get used images of all vms belonging to this VNFD
363 :param mydb: database conector
364 :param vnf_id: vnfd uuid
365 :param nfvo_tenant: tenant, not used
366 :return: The list of image uuid used
367 """
368 image_list = []
369 vms = mydb.get_rows(SELECT=('image_id','image_list'), FROM='vms', WHERE={'vnf_id': vnf_id})
370 for vm in vms:
371 if vm["image_id"] and vm["image_id"] not in image_list:
372 image_list.append(vm["image_id"])
373 if vm["image_list"]:
374 vm_image_list = yaml.load(vm["image_list"], Loader=yaml.Loader)
375 for image_dict in vm_image_list:
376 if image_dict["image_id"] not in image_list:
377 image_list.append(image_dict["image_id"])
378 return image_list
379
380
381 def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None,
382 vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False):
383 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
384 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
385 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
386 raise exception upon error
387 '''
388 global plugins
389 WHERE_dict={}
390 if nfvo_tenant is not None: WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
391 if datacenter_id is not None: WHERE_dict['d.uuid'] = datacenter_id
392 if datacenter_tenant_id is not None: WHERE_dict['datacenter_tenant_id'] = datacenter_tenant_id
393 if datacenter_name is not None: WHERE_dict['d.name'] = datacenter_name
394 if vim_tenant is not None: WHERE_dict['dt.vim_tenant_id'] = vim_tenant
395 if vim_tenant_name is not None: WHERE_dict['vim_tenant_name'] = vim_tenant_name
396 if nfvo_tenant or vim_tenant or vim_tenant_name or datacenter_tenant_id:
397 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'
398 select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
399 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
400 'user','passwd', 'dt.config as dt_config')
401 else:
402 from_ = 'datacenters as d'
403 select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
404 try:
405 vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
406 vim_dict={}
407 for vim in vims:
408 extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
409 'datacenter_id': vim.get('datacenter_id'),
410 '_vim_type_internal': vim.get('type')}
411 if vim["config"]:
412 extra.update(yaml.load(vim["config"], Loader=yaml.Loader))
413 if vim.get('dt_config'):
414 extra.update(yaml.load(vim["dt_config"], Loader=yaml.Loader))
415 plugin_name = "rovim_" + vim["type"]
416 if plugin_name not in plugins:
417 try:
418 _load_plugin(plugin_name, type="vim")
419 except NfvoException as e:
420 if ignore_errors:
421 logger.error("{}".format(e))
422 continue
423 else:
424 raise
425 try:
426 if 'datacenter_tenant_id' in vim:
427 thread_id = vim["datacenter_tenant_id"]
428 if thread_id not in vim_persistent_info:
429 vim_persistent_info[thread_id] = {}
430 persistent_info = vim_persistent_info[thread_id]
431 else:
432 persistent_info = {}
433 #if not tenant:
434 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
435 vim_dict[vim['datacenter_id']] = plugins[plugin_name].vimconnector(
436 uuid=vim['datacenter_id'], name=vim['datacenter_name'],
437 tenant_id=vim.get('vim_tenant_id',vim_tenant),
438 tenant_name=vim.get('vim_tenant_name',vim_tenant_name),
439 url=vim['vim_url'], url_admin=vim['vim_url_admin'],
440 user=vim.get('user',vim_user), passwd=vim.get('passwd',vim_passwd),
441 config=extra, persistent_info=persistent_info
442 )
443 except Exception as e:
444 if ignore_errors:
445 logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)))
446 continue
447 http_code = httperrors.Internal_Server_Error
448 if isinstance(e, vimconn.vimconnException):
449 http_code = e.http_code
450 raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), http_code)
451 return vim_dict
452 except db_base_Exception as e:
453 raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
454
455
456 def rollback(mydb, vims, rollback_list):
457 undeleted_items=[]
458 #delete things by reverse order
459 for i in range(len(rollback_list)-1, -1, -1):
460 item = rollback_list[i]
461 if item["where"]=="vim":
462 if item["vim_id"] not in vims:
463 continue
464 if is_task_id(item["uuid"]):
465 continue
466 vim = vims[item["vim_id"]]
467 try:
468 if item["what"]=="image":
469 vim.delete_image(item["uuid"])
470 mydb.delete_row(FROM="datacenters_images", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
471 elif item["what"]=="flavor":
472 vim.delete_flavor(item["uuid"])
473 mydb.delete_row(FROM="datacenters_flavors", WHERE={"datacenter_vim_id": vim["id"], "vim_id":item["uuid"]})
474 elif item["what"]=="network":
475 vim.delete_network(item["uuid"])
476 elif item["what"]=="vm":
477 vim.delete_vminstance(item["uuid"])
478 except vimconn.vimconnException as e:
479 logger.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item['what'], item["uuid"], str(e))
480 undeleted_items.append("{} {} from VIM {}".format(item['what'], item["uuid"], vim["name"]))
481 except db_base_Exception as e:
482 logger.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item['what'], item["uuid"], str(e))
483
484 else: # where==mano
485 try:
486 if item["what"]=="image":
487 mydb.delete_row(FROM="images", WHERE={"uuid": item["uuid"]})
488 elif item["what"]=="flavor":
489 mydb.delete_row(FROM="flavors", WHERE={"uuid": item["uuid"]})
490 except db_base_Exception as e:
491 logger.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item['what'], item["uuid"], str(e))
492 undeleted_items.append("{} '{}'".format(item['what'], item["uuid"]))
493 if len(undeleted_items)==0:
494 return True," Rollback successful."
495 else:
496 return False," Rollback fails to delete: " + str(undeleted_items)
497
498
499 def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1):
500 global global_config
501 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
502 vnfc_interfaces={}
503 for vnfc in vnf_descriptor["vnf"]["VNFC"]:
504 name_dict = {}
505 #dataplane interfaces
506 for numa in vnfc.get("numas",() ):
507 for interface in numa.get("interfaces",()):
508 if interface["name"] in name_dict:
509 raise NfvoException(
510 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
511 vnfc["name"], interface["name"]),
512 httperrors.Bad_Request)
513 name_dict[ interface["name"] ] = "underlay"
514 #bridge interfaces
515 for interface in vnfc.get("bridge-ifaces",() ):
516 if interface["name"] in name_dict:
517 raise NfvoException(
518 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
519 vnfc["name"], interface["name"]),
520 httperrors.Bad_Request)
521 name_dict[ interface["name"] ] = "overlay"
522 vnfc_interfaces[ vnfc["name"] ] = name_dict
523 # check bood-data info
524 # if "boot-data" in vnfc:
525 # # check that user-data is incompatible with users and config-files
526 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
527 # raise NfvoException(
528 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
529 # httperrors.Bad_Request)
530
531 #check if the info in external_connections matches with the one in the vnfcs
532 name_list=[]
533 for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
534 if external_connection["name"] in name_list:
535 raise NfvoException(
536 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
537 external_connection["name"]),
538 httperrors.Bad_Request)
539 name_list.append(external_connection["name"])
540 if external_connection["VNFC"] not in vnfc_interfaces:
541 raise NfvoException(
542 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
543 external_connection["name"], external_connection["VNFC"]),
544 httperrors.Bad_Request)
545
546 if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
547 raise NfvoException(
548 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
549 external_connection["name"],
550 external_connection["local_iface_name"]),
551 httperrors.Bad_Request )
552
553 #check if the info in internal_connections matches with the one in the vnfcs
554 name_list=[]
555 for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
556 if internal_connection["name"] in name_list:
557 raise NfvoException(
558 "Error at vnf:internal-connections:name, value '{}' already used as an internal-connection".format(
559 internal_connection["name"]),
560 httperrors.Bad_Request)
561 name_list.append(internal_connection["name"])
562 #We should check that internal-connections of type "ptp" have only 2 elements
563
564 if len(internal_connection["elements"])>2 and (internal_connection.get("type") == "ptp" or internal_connection.get("type") == "e-line"):
565 raise NfvoException(
566 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
567 internal_connection["name"],
568 'ptp' if vnf_descriptor_version==1 else 'e-line',
569 'data' if vnf_descriptor_version==1 else "e-lan"),
570 httperrors.Bad_Request)
571 for port in internal_connection["elements"]:
572 vnf = port["VNFC"]
573 iface = port["local_iface_name"]
574 if vnf not in vnfc_interfaces:
575 raise NfvoException(
576 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
577 internal_connection["name"], vnf),
578 httperrors.Bad_Request)
579 if iface not in vnfc_interfaces[ vnf ]:
580 raise NfvoException(
581 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
582 internal_connection["name"], iface),
583 httperrors.Bad_Request)
584 return -httperrors.Bad_Request,
585 if vnf_descriptor_version==1 and "type" not in internal_connection:
586 if vnfc_interfaces[vnf][iface] == "overlay":
587 internal_connection["type"] = "bridge"
588 else:
589 internal_connection["type"] = "data"
590 if vnf_descriptor_version==2 and "implementation" not in internal_connection:
591 if vnfc_interfaces[vnf][iface] == "overlay":
592 internal_connection["implementation"] = "overlay"
593 else:
594 internal_connection["implementation"] = "underlay"
595 if (internal_connection.get("type") == "data" or internal_connection.get("type") == "ptp" or \
596 internal_connection.get("implementation") == "underlay") and vnfc_interfaces[vnf][iface] == "overlay":
597 raise NfvoException(
598 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
599 internal_connection["name"],
600 iface, 'bridge' if vnf_descriptor_version==1 else 'overlay',
601 'data' if vnf_descriptor_version==1 else 'underlay'),
602 httperrors.Bad_Request)
603 if (internal_connection.get("type") == "bridge" or internal_connection.get("implementation") == "overlay") and \
604 vnfc_interfaces[vnf][iface] == "underlay":
605 raise NfvoException(
606 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
607 internal_connection["name"], iface,
608 'data' if vnf_descriptor_version==1 else 'underlay',
609 'bridge' if vnf_descriptor_version==1 else 'overlay'),
610 httperrors.Bad_Request)
611
612
613 def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None):
614 #look if image exist
615 if only_create_at_vim:
616 image_mano_id = image_dict['uuid']
617 if return_on_error == None:
618 return_on_error = True
619 else:
620 if image_dict['location']:
621 images = mydb.get_rows(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
622 else:
623 images = mydb.get_rows(FROM="images", WHERE={'universal_name':image_dict['universal_name'], 'checksum':image_dict['checksum']})
624 if len(images)>=1:
625 image_mano_id = images[0]['uuid']
626 else:
627 #create image in MANO DB
628 temp_image_dict={'name':image_dict['name'], 'description':image_dict.get('description',None),
629 'location':image_dict['location'], 'metadata':image_dict.get('metadata',None),
630 'universal_name':image_dict['universal_name'] , 'checksum':image_dict['checksum']
631 }
632 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
633 image_mano_id = mydb.new_row('images', temp_image_dict, add_uuid=True)
634 rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
635 #create image at every vim
636 for vim_id,vim in vims.items():
637 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
638 image_created="false"
639 #look at database
640 image_db = mydb.get_rows(FROM="datacenters_images",
641 WHERE={'datacenter_vim_id': datacenter_vim_id, 'image_id': image_mano_id})
642 #look at VIM if this image exist
643 try:
644 if image_dict['location'] is not None:
645 image_vim_id = vim.get_image_id_from_path(image_dict['location'])
646 else:
647 filter_dict = {}
648 filter_dict['name'] = image_dict['universal_name']
649 if image_dict.get('checksum') != None:
650 filter_dict['checksum'] = image_dict['checksum']
651 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
652 vim_images = vim.get_image_list(filter_dict)
653 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
654 if len(vim_images) > 1:
655 raise vimconn.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict)), httperrors.Conflict)
656 elif len(vim_images) == 0:
657 raise vimconn.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict)))
658 else:
659 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
660 image_vim_id = vim_images[0]['id']
661
662 except vimconn.vimconnNotFoundException as e:
663 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
664 try:
665 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
666 if image_dict['location']:
667 image_vim_id = vim.new_image(image_dict)
668 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
669 image_created="true"
670 else:
671 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
672 raise vimconn.vimconnException(str(e))
673 except vimconn.vimconnException as e:
674 if return_on_error:
675 logger.error("Error creating image at VIM '%s': %s", vim["name"], str(e))
676 raise
677 image_vim_id = None
678 logger.warn("Error creating image at VIM '%s': %s", vim["name"], str(e))
679 continue
680 except vimconn.vimconnException as e:
681 if return_on_error:
682 logger.error("Error contacting VIM to know if the image exists at VIM: %s", str(e))
683 raise
684 logger.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e))
685 image_vim_id = None
686 continue
687 #if we reach here, the image has been created or existed
688 if len(image_db)==0:
689 #add new vim_id at datacenters_images
690 mydb.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id,
691 'image_id':image_mano_id,
692 'vim_id': image_vim_id,
693 'created':image_created})
694 elif image_db[0]["vim_id"]!=image_vim_id:
695 #modify existing vim_id at datacenters_images
696 mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_vim_id':vim_id, 'image_id':image_mano_id})
697
698 return image_vim_id if only_create_at_vim else image_mano_id
699
700
701 def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = None):
702 temp_flavor_dict= {'disk':flavor_dict.get('disk',0),
703 'ram':flavor_dict.get('ram'),
704 'vcpus':flavor_dict.get('vcpus'),
705 }
706 if 'extended' in flavor_dict and flavor_dict['extended']==None:
707 del flavor_dict['extended']
708 if 'extended' in flavor_dict:
709 temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
710
711 #look if flavor exist
712 if only_create_at_vim:
713 flavor_mano_id = flavor_dict['uuid']
714 if return_on_error == None:
715 return_on_error = True
716 else:
717 flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
718 if len(flavors)>=1:
719 flavor_mano_id = flavors[0]['uuid']
720 else:
721 #create flavor
722 #create one by one the images of aditional disks
723 dev_image_list=[] #list of images
724 if 'extended' in flavor_dict and flavor_dict['extended']!=None:
725 dev_nb=0
726 for device in flavor_dict['extended'].get('devices',[]):
727 if "image" not in device and "image name" not in device:
728 continue
729 image_dict={}
730 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
731 image_dict['universal_name']=device.get('image name')
732 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
733 image_dict['location']=device.get('image')
734 #image_dict['new_location']=vnfc.get('image location')
735 image_dict['checksum']=device.get('image checksum')
736 image_metadata_dict = device.get('image metadata', None)
737 image_metadata_str = None
738 if image_metadata_dict != None:
739 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
740 image_dict['metadata']=image_metadata_str
741 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
742 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
743 dev_image_list.append(image_id)
744 dev_nb += 1
745 temp_flavor_dict['name'] = flavor_dict['name']
746 temp_flavor_dict['description'] = flavor_dict.get('description',None)
747 content = mydb.new_row('flavors', temp_flavor_dict, add_uuid=True)
748 flavor_mano_id= content
749 rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
750 #create flavor at every vim
751 if 'uuid' in flavor_dict:
752 del flavor_dict['uuid']
753 flavor_vim_id=None
754 for vim_id,vim in vims.items():
755 datacenter_vim_id = vim["config"]["datacenter_tenant_id"]
756 flavor_created="false"
757 #look at database
758 flavor_db = mydb.get_rows(FROM="datacenters_flavors",
759 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
760 #look at VIM if this flavor exist SKIPPED
761 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
762 #if res_vim < 0:
763 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
764 # continue
765 #elif res_vim==0:
766
767 # Create the flavor in VIM
768 # Translate images at devices from MANO id to VIM id
769 disk_list = []
770 if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
771 # make a copy of original devices
772 devices_original=[]
773
774 for device in flavor_dict["extended"].get("devices",[]):
775 dev={}
776 dev.update(device)
777 devices_original.append(dev)
778 if 'image' in device:
779 del device['image']
780 if 'image metadata' in device:
781 del device['image metadata']
782 if 'image checksum' in device:
783 del device['image checksum']
784 dev_nb = 0
785 for index in range(0,len(devices_original)) :
786 device=devices_original[index]
787 if "image" not in device and "image name" not in device:
788 # if 'size' in device:
789 disk_list.append({'size': device.get('size', default_volume_size), 'name': device.get('name')})
790 continue
791 image_dict={}
792 image_dict['name']=device.get('image name',flavor_dict['name']+str(dev_nb)+"-img")
793 image_dict['universal_name']=device.get('image name')
794 image_dict['description']=flavor_dict['name']+str(dev_nb)+"-img"
795 image_dict['location']=device.get('image')
796 # image_dict['new_location']=device.get('image location')
797 image_dict['checksum']=device.get('image checksum')
798 image_metadata_dict = device.get('image metadata', None)
799 image_metadata_str = None
800 if image_metadata_dict != None:
801 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
802 image_dict['metadata']=image_metadata_str
803 image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=return_on_error )
804 image_dict["uuid"]=image_mano_id
805 image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True, return_on_error=return_on_error)
806
807 #save disk information (image must be based on and size
808 disk_list.append({'image_id': image_vim_id, 'size': device.get('size', default_volume_size)})
809
810 flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
811 dev_nb += 1
812 if len(flavor_db)>0:
813 #check that this vim_id exist in VIM, if not create
814 flavor_vim_id=flavor_db[0]["vim_id"]
815 try:
816 vim.get_flavor(flavor_vim_id)
817 continue #flavor exist
818 except vimconn.vimconnException:
819 pass
820 #create flavor at vim
821 logger.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim["name"])
822 try:
823 flavor_vim_id = None
824 flavor_vim_id=vim.get_flavor_id_from_data(flavor_dict)
825 flavor_created="false"
826 except vimconn.vimconnException as e:
827 pass
828 try:
829 if not flavor_vim_id:
830 flavor_vim_id = vim.new_flavor(flavor_dict)
831 rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
832 flavor_created="true"
833 except vimconn.vimconnException as e:
834 if return_on_error:
835 logger.error("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
836 raise
837 logger.warn("Error creating flavor at VIM %s: %s.", vim["name"], str(e))
838 flavor_vim_id = None
839 continue
840 #if reach here the flavor has been create or exist
841 if len(flavor_db)==0:
842 #add new vim_id at datacenters_flavors
843 extended_devices_yaml = None
844 if len(disk_list) > 0:
845 extended_devices = dict()
846 extended_devices['disks'] = disk_list
847 extended_devices_yaml = yaml.safe_dump(extended_devices,default_flow_style=True,width=256)
848 mydb.new_row('datacenters_flavors',
849 {'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id, 'vim_id': flavor_vim_id,
850 'created': flavor_created, 'extended': extended_devices_yaml})
851 elif flavor_db[0]["vim_id"]!=flavor_vim_id:
852 #modify existing vim_id at datacenters_flavors
853 mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id},
854 WHERE={'datacenter_vim_id': datacenter_vim_id, 'flavor_id': flavor_mano_id})
855
856 return flavor_vim_id if only_create_at_vim else flavor_mano_id
857
858
859 def get_str(obj, field, length):
860 """
861 Obtain the str value,
862 :param obj:
863 :param length:
864 :return:
865 """
866 value = obj.get(field)
867 if value is not None:
868 value = str(value)[:length]
869 return value
870
871 def _lookfor_or_create_image(db_image, mydb, descriptor):
872 """
873 fill image content at db_image dictionary. Check if the image with this image and checksum exist
874 :param db_image: dictionary to insert data
875 :param mydb: database connector
876 :param descriptor: yang descriptor
877 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
878 """
879
880 db_image["name"] = get_str(descriptor, "image", 255)
881 db_image["checksum"] = get_str(descriptor, "image-checksum", 32)
882 if not db_image["checksum"]: # Ensure that if empty string, None is stored
883 db_image["checksum"] = None
884 if db_image["name"].startswith("/"):
885 db_image["location"] = db_image["name"]
886 existing_images = mydb.get_rows(FROM="images", WHERE={'location': db_image["location"]})
887 else:
888 db_image["universal_name"] = db_image["name"]
889 existing_images = mydb.get_rows(FROM="images", WHERE={'universal_name': db_image['universal_name'],
890 'checksum': db_image['checksum']})
891 if existing_images:
892 return existing_images[0]["uuid"]
893 else:
894 image_uuid = str(uuid4())
895 db_image["uuid"] = image_uuid
896 return None
897
898 def get_resource_allocation_params(quota_descriptor):
899 """
900 read the quota_descriptor from vnfd and fetch the resource allocation properties from the descriptor object
901 :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
902 :return: quota params for limit, reserve, shares from the descriptor object
903 """
904 quota = {}
905 if quota_descriptor.get("limit"):
906 quota["limit"] = int(quota_descriptor["limit"])
907 if quota_descriptor.get("reserve"):
908 quota["reserve"] = int(quota_descriptor["reserve"])
909 if quota_descriptor.get("shares"):
910 quota["shares"] = int(quota_descriptor["shares"])
911 return quota
912
913 def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
914 """
915 Parses an OSM IM vnfd_catalog and insert at DB
916 :param mydb:
917 :param tenant_id:
918 :param vnf_descriptor:
919 :return: The list of cretated vnf ids
920 """
921 try:
922 myvnfd = vnfd_catalog.vnfd()
923 try:
924 pybindJSONDecoder.load_ietf_json(vnf_descriptor, None, None, obj=myvnfd, path_helper=True,
925 skip_unknown=True)
926 except Exception as e:
927 raise NfvoException("Error. Invalid VNF descriptor format " + str(e), httperrors.Bad_Request)
928 db_vnfs = []
929 db_nets = []
930 db_vms = []
931 db_vms_index = 0
932 db_interfaces = []
933 db_images = []
934 db_flavors = []
935 db_ip_profiles_index = 0
936 db_ip_profiles = []
937 uuid_list = []
938 vnfd_uuid_list = []
939 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd:vnfd-catalog")
940 if not vnfd_catalog_descriptor:
941 vnfd_catalog_descriptor = vnf_descriptor.get("vnfd-catalog")
942 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd")
943 if not vnfd_descriptor_list:
944 vnfd_descriptor_list = vnfd_catalog_descriptor.get("vnfd:vnfd")
945 for vnfd_yang in myvnfd.vnfd_catalog.vnfd.values():
946 vnfd = vnfd_yang.get()
947
948 # table vnf
949 vnf_uuid = str(uuid4())
950 uuid_list.append(vnf_uuid)
951 vnfd_uuid_list.append(vnf_uuid)
952 vnfd_id = get_str(vnfd, "id", 255)
953 db_vnf = {
954 "uuid": vnf_uuid,
955 "osm_id": vnfd_id,
956 "name": get_str(vnfd, "name", 255),
957 "description": get_str(vnfd, "description", 255),
958 "tenant_id": tenant_id,
959 "vendor": get_str(vnfd, "vendor", 255),
960 "short_name": get_str(vnfd, "short-name", 255),
961 "descriptor": str(vnf_descriptor)[:60000]
962 }
963
964 for vnfd_descriptor in vnfd_descriptor_list:
965 if vnfd_descriptor["id"] == str(vnfd["id"]):
966 break
967
968 # table ip_profiles (ip-profiles)
969 ip_profile_name2db_table_index = {}
970 for ip_profile in vnfd.get("ip-profiles").values():
971 db_ip_profile = {
972 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
973 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
974 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
975 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
976 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
977 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
978 }
979 dns_list = []
980 for dns in ip_profile["ip-profile-params"]["dns-server"].values():
981 dns_list.append(str(dns.get("address")))
982 db_ip_profile["dns_address"] = ";".join(dns_list)
983 if ip_profile["ip-profile-params"].get('security-group'):
984 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
985 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
986 db_ip_profiles_index += 1
987 db_ip_profiles.append(db_ip_profile)
988
989 # table nets (internal-vld)
990 net_id2uuid = {} # for mapping interface with network
991 for vld in vnfd.get("internal-vld").values():
992 net_uuid = str(uuid4())
993 uuid_list.append(net_uuid)
994 db_net = {
995 "name": get_str(vld, "name", 255),
996 "vnf_id": vnf_uuid,
997 "uuid": net_uuid,
998 "description": get_str(vld, "description", 255),
999 "osm_id": get_str(vld, "id", 255),
1000 "type": "bridge", # TODO adjust depending on connection point type
1001 }
1002 net_id2uuid[vld.get("id")] = net_uuid
1003 db_nets.append(db_net)
1004 # ip-profile, link db_ip_profile with db_sce_net
1005 if vld.get("ip-profile-ref"):
1006 ip_profile_name = vld.get("ip-profile-ref")
1007 if ip_profile_name not in ip_profile_name2db_table_index:
1008 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
1009 "'{}'. Reference to a non-existing 'ip_profiles'".format(
1010 str(vnfd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
1011 httperrors.Bad_Request)
1012 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["net_id"] = net_uuid
1013 else: #check no ip-address has been defined
1014 for icp in vld.get("internal-connection-point").values():
1015 if icp.get("ip-address"):
1016 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
1017 "contains an ip-address but no ip-profile has been defined at VLD".format(
1018 str(vnfd["id"]), str(vld["id"]), str(icp["id"])),
1019 httperrors.Bad_Request)
1020
1021 # connection points vaiable declaration
1022 cp_name2iface_uuid = {}
1023 cp_name2vdu_id = {}
1024 cp_name2vm_uuid = {}
1025 cp_name2db_interface = {}
1026 vdu_id2cp_name = {} # stored only when one external connection point is presented at this VDU
1027
1028 # table vms (vdus)
1029 vdu_id2uuid = {}
1030 vdu_id2db_table_index = {}
1031 mgmt_access = {}
1032 for vdu in vnfd.get("vdu").values():
1033
1034 for vdu_descriptor in vnfd_descriptor["vdu"]:
1035 if vdu_descriptor["id"] == str(vdu["id"]):
1036 break
1037 vm_uuid = str(uuid4())
1038 uuid_list.append(vm_uuid)
1039 vdu_id = get_str(vdu, "id", 255)
1040 db_vm = {
1041 "uuid": vm_uuid,
1042 "osm_id": vdu_id,
1043 "name": get_str(vdu, "name", 255),
1044 "description": get_str(vdu, "description", 255),
1045 "pdu_type": get_str(vdu, "pdu-type", 255),
1046 "vnf_id": vnf_uuid,
1047 }
1048 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
1049 vdu_id2db_table_index[db_vm["osm_id"]] = db_vms_index
1050 if vdu.get("count"):
1051 db_vm["count"] = int(vdu["count"])
1052
1053 # table image
1054 image_present = False
1055 if vdu.get("image"):
1056 image_present = True
1057 db_image = {}
1058 image_uuid = _lookfor_or_create_image(db_image, mydb, vdu)
1059 if not image_uuid:
1060 image_uuid = db_image["uuid"]
1061 db_images.append(db_image)
1062 db_vm["image_id"] = image_uuid
1063 if vdu.get("alternative-images"):
1064 vm_alternative_images = []
1065 for alt_image in vdu.get("alternative-images").values():
1066 db_image = {}
1067 image_uuid = _lookfor_or_create_image(db_image, mydb, alt_image)
1068 if not image_uuid:
1069 image_uuid = db_image["uuid"]
1070 db_images.append(db_image)
1071 vm_alternative_images.append({
1072 "image_id": image_uuid,
1073 "vim_type": str(alt_image["vim-type"]),
1074 # "universal_name": str(alt_image["image"]),
1075 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1076 })
1077
1078 db_vm["image_list"] = yaml.safe_dump(vm_alternative_images, default_flow_style=True, width=256)
1079
1080 # volumes
1081 devices = []
1082 if vdu.get("volumes"):
1083 for volume_key in vdu["volumes"]:
1084 volume = vdu["volumes"][volume_key]
1085 if not image_present:
1086 # Convert the first volume to vnfc.image
1087 image_present = True
1088 db_image = {}
1089 image_uuid = _lookfor_or_create_image(db_image, mydb, volume)
1090 if not image_uuid:
1091 image_uuid = db_image["uuid"]
1092 db_images.append(db_image)
1093 db_vm["image_id"] = image_uuid
1094 else:
1095 # Add Openmano devices
1096 device = {"name": str(volume.get("name"))}
1097 device["type"] = str(volume.get("device-type"))
1098 if volume.get("size"):
1099 device["size"] = int(volume["size"])
1100 if volume.get("image"):
1101 device["image name"] = str(volume["image"])
1102 if volume.get("image-checksum"):
1103 device["image checksum"] = str(volume["image-checksum"])
1104
1105 devices.append(device)
1106
1107 if not db_vm.get("image_id"):
1108 if not db_vm["pdu_type"]:
1109 raise NfvoException("Not defined image for VDU")
1110 # create a fake image
1111
1112 # cloud-init
1113 boot_data = {}
1114 if vdu.get("cloud-init"):
1115 boot_data["user-data"] = str(vdu["cloud-init"])
1116 elif vdu.get("cloud-init-file"):
1117 # TODO Where this file content is present???
1118 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1119 boot_data["user-data"] = str(vdu["cloud-init-file"])
1120
1121 if vdu.get("supplemental-boot-data"):
1122 if vdu["supplemental-boot-data"].get('boot-data-drive'):
1123 boot_data['boot-data-drive'] = True
1124 if vdu["supplemental-boot-data"].get('config-file'):
1125 om_cfgfile_list = list()
1126 for custom_config_file in vdu["supplemental-boot-data"]['config-file'].values():
1127 # TODO Where this file content is present???
1128 cfg_source = str(custom_config_file["source"])
1129 om_cfgfile_list.append({"dest": custom_config_file["dest"],
1130 "content": cfg_source})
1131 boot_data['config-files'] = om_cfgfile_list
1132 if boot_data:
1133 db_vm["boot_data"] = yaml.safe_dump(boot_data, default_flow_style=True, width=256)
1134
1135 db_vms.append(db_vm)
1136 db_vms_index += 1
1137
1138 # table interfaces (internal/external interfaces)
1139 flavor_epa_interfaces = []
1140 # for iface in chain(vdu.get("internal-interface").values(), vdu.get("external-interface").values()):
1141 for iface in vdu.get("interface").values():
1142 flavor_epa_interface = {}
1143 iface_uuid = str(uuid4())
1144 uuid_list.append(iface_uuid)
1145 db_interface = {
1146 "uuid": iface_uuid,
1147 "internal_name": get_str(iface, "name", 255),
1148 "vm_id": vm_uuid,
1149 }
1150 flavor_epa_interface["name"] = db_interface["internal_name"]
1151 if iface.get("virtual-interface").get("vpci"):
1152 db_interface["vpci"] = get_str(iface.get("virtual-interface"), "vpci", 12)
1153 flavor_epa_interface["vpci"] = db_interface["vpci"]
1154
1155 if iface.get("virtual-interface").get("bandwidth"):
1156 bps = int(iface.get("virtual-interface").get("bandwidth"))
1157 db_interface["bw"] = int(math.ceil(bps / 1000000.0))
1158 flavor_epa_interface["bandwidth"] = "{} Mbps".format(db_interface["bw"])
1159
1160 if iface.get("virtual-interface").get("type") == "OM-MGMT":
1161 db_interface["type"] = "mgmt"
1162 elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1163 db_interface["type"] = "bridge"
1164 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1165 elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1166 db_interface["type"] = "data"
1167 db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
1168 flavor_epa_interface["dedicated"] = "no" if iface["virtual-interface"]["type"] == "SR-IOV" \
1169 else "yes"
1170 flavor_epa_interfaces.append(flavor_epa_interface)
1171 else:
1172 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1173 "-interface':'type':'{}'. Interface type is not supported".format(
1174 vnfd_id, vdu_id, iface.get("virtual-interface").get("type")),
1175 httperrors.Bad_Request)
1176
1177 if iface.get("mgmt-interface"):
1178 db_interface["type"] = "mgmt"
1179
1180 if iface.get("external-connection-point-ref"):
1181 try:
1182 cp = vnfd.get("connection-point")[iface.get("external-connection-point-ref")]
1183 db_interface["external_name"] = get_str(cp, "name", 255)
1184 cp_name2iface_uuid[db_interface["external_name"]] = iface_uuid
1185 cp_name2vdu_id[db_interface["external_name"]] = vdu_id
1186 cp_name2vm_uuid[db_interface["external_name"]] = vm_uuid
1187 cp_name2db_interface[db_interface["external_name"]] = db_interface
1188 for cp_descriptor in vnfd_descriptor["connection-point"]:
1189 if cp_descriptor["name"] == db_interface["external_name"]:
1190 break
1191 else:
1192 raise KeyError()
1193
1194 if vdu_id in vdu_id2cp_name:
1195 vdu_id2cp_name[vdu_id] = None # more than two connecdtion point for this VDU
1196 else:
1197 vdu_id2cp_name[vdu_id] = db_interface["external_name"]
1198
1199 # port security
1200 if str(cp_descriptor.get("port-security-enabled")).lower() == "false":
1201 db_interface["port_security"] = 0
1202 elif str(cp_descriptor.get("port-security-enabled")).lower() == "true":
1203 db_interface["port_security"] = 1
1204 except KeyError:
1205 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1206 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1207 " at connection-point".format(
1208 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1209 cp=iface.get("vnfd-connection-point-ref")),
1210 httperrors.Bad_Request)
1211 elif iface.get("internal-connection-point-ref"):
1212 try:
1213 for icp_descriptor in vdu_descriptor["internal-connection-point"]:
1214 if icp_descriptor["id"] == str(iface.get("internal-connection-point-ref")):
1215 break
1216 else:
1217 raise KeyError("does not exist at vdu:internal-connection-point")
1218 icp = None
1219 icp_vld = None
1220 for vld in vnfd.get("internal-vld").values():
1221 for cp in vld.get("internal-connection-point").values():
1222 if cp.get("id-ref") == iface.get("internal-connection-point-ref"):
1223 if icp:
1224 raise KeyError("is referenced by more than one 'internal-vld'")
1225 icp = cp
1226 icp_vld = vld
1227 if not icp:
1228 raise KeyError("is not referenced by any 'internal-vld'")
1229
1230 db_interface["net_id"] = net_id2uuid[icp_vld.get("id")]
1231 if str(icp_descriptor.get("port-security-enabled")).lower() == "false":
1232 db_interface["port_security"] = 0
1233 elif str(icp_descriptor.get("port-security-enabled")).lower() == "true":
1234 db_interface["port_security"] = 1
1235 if icp.get("ip-address"):
1236 if not icp_vld.get("ip-profile-ref"):
1237 raise NfvoException
1238 db_interface["ip_address"] = str(icp.get("ip-address"))
1239 except KeyError as e:
1240 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1241 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1242 " {msg}".format(
1243 vnf=vnfd_id, vdu=vdu_id, iface=iface["name"],
1244 cp=iface.get("internal-connection-point-ref"), msg=str(e)),
1245 httperrors.Bad_Request)
1246 if iface.get("position"):
1247 db_interface["created_at"] = int(iface.get("position")) * 50
1248 if iface.get("mac-address"):
1249 db_interface["mac"] = str(iface.get("mac-address"))
1250 db_interfaces.append(db_interface)
1251
1252 # table flavors
1253 db_flavor = {
1254 "name": get_str(vdu, "name", 250) + "-flv",
1255 "vcpus": int(vdu["vm-flavor"].get("vcpu-count", 1)),
1256 "ram": int(vdu["vm-flavor"].get("memory-mb", 1)),
1257 "disk": int(vdu["vm-flavor"].get("storage-gb", 0)),
1258 }
1259 # TODO revise the case of several numa-node-policy node
1260 extended = {}
1261 numa = {}
1262 if devices:
1263 extended["devices"] = devices
1264 if flavor_epa_interfaces:
1265 numa["interfaces"] = flavor_epa_interfaces
1266 if vdu.get("guest-epa"): # TODO or dedicated_int:
1267 epa_vcpu_set = False
1268 if vdu["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1269 numa_node_policy = vdu["guest-epa"].get("numa-node-policy")
1270 if numa_node_policy.get("node"):
1271 numa_node = next(iter(numa_node_policy["node"].values()))
1272 if numa_node.get("num-cores"):
1273 numa["cores"] = numa_node["num-cores"]
1274 epa_vcpu_set = True
1275 if numa_node.get("paired-threads"):
1276 if numa_node["paired-threads"].get("num-paired-threads"):
1277 numa["paired-threads"] = int(numa_node["paired-threads"]["num-paired-threads"])
1278 epa_vcpu_set = True
1279 if len(numa_node["paired-threads"].get("paired-thread-ids")):
1280 numa["paired-threads-id"] = []
1281 for pair in numa_node["paired-threads"]["paired-thread-ids"].values():
1282 numa["paired-threads-id"].append(
1283 (str(pair["thread-a"]), str(pair["thread-b"]))
1284 )
1285 if numa_node.get("num-threads"):
1286 numa["threads"] = int(numa_node["num-threads"])
1287 epa_vcpu_set = True
1288 if numa_node.get("memory-mb"):
1289 numa["memory"] = max(int(numa_node["memory-mb"] / 1024), 1)
1290 if vdu["guest-epa"].get("mempage-size"):
1291 if vdu["guest-epa"]["mempage-size"] != "SMALL":
1292 numa["memory"] = max(int(db_flavor["ram"] / 1024), 1)
1293 if vdu["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set:
1294 if vdu["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1295 if vdu["guest-epa"].get("cpu-thread-pinning-policy") and \
1296 vdu["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1297 numa["cores"] = max(db_flavor["vcpus"], 1)
1298 else:
1299 numa["threads"] = max(db_flavor["vcpus"], 1)
1300 epa_vcpu_set = True
1301 if vdu["guest-epa"].get("cpu-quota") and not epa_vcpu_set:
1302 cpuquota = get_resource_allocation_params(vdu["guest-epa"].get("cpu-quota"))
1303 if cpuquota:
1304 extended["cpu-quota"] = cpuquota
1305 if vdu["guest-epa"].get("mem-quota"):
1306 vduquota = get_resource_allocation_params(vdu["guest-epa"].get("mem-quota"))
1307 if vduquota:
1308 extended["mem-quota"] = vduquota
1309 if vdu["guest-epa"].get("disk-io-quota"):
1310 diskioquota = get_resource_allocation_params(vdu["guest-epa"].get("disk-io-quota"))
1311 if diskioquota:
1312 extended["disk-io-quota"] = diskioquota
1313 if vdu["guest-epa"].get("vif-quota"):
1314 vifquota = get_resource_allocation_params(vdu["guest-epa"].get("vif-quota"))
1315 if vifquota:
1316 extended["vif-quota"] = vifquota
1317 if numa:
1318 extended["numas"] = [numa]
1319 if extended:
1320 extended_text = yaml.safe_dump(extended, default_flow_style=True, width=256)
1321 db_flavor["extended"] = extended_text
1322 # look if flavor exist
1323 temp_flavor_dict = {'disk': db_flavor.get('disk', 0),
1324 'ram': db_flavor.get('ram'),
1325 'vcpus': db_flavor.get('vcpus'),
1326 'extended': db_flavor.get('extended')
1327 }
1328 existing_flavors = mydb.get_rows(FROM="flavors", WHERE=temp_flavor_dict)
1329 if existing_flavors:
1330 flavor_uuid = existing_flavors[0]["uuid"]
1331 else:
1332 flavor_uuid = str(uuid4())
1333 uuid_list.append(flavor_uuid)
1334 db_flavor["uuid"] = flavor_uuid
1335 db_flavors.append(db_flavor)
1336 db_vm["flavor_id"] = flavor_uuid
1337
1338 # VNF affinity and antiaffinity
1339 for pg in vnfd.get("placement-groups").values():
1340 pg_name = get_str(pg, "name", 255)
1341 for vdu in pg.get("member-vdus").values():
1342 vdu_id = get_str(vdu, "member-vdu-ref", 255)
1343 if vdu_id not in vdu_id2db_table_index:
1344 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1345 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1346 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
1347 httperrors.Bad_Request)
1348 db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
1349 # TODO consider the case of isolation and not colocation
1350 # if pg.get("strategy") == "ISOLATION":
1351
1352 # VNF mgmt configuration
1353 if vnfd["mgmt-interface"].get("vdu-id"):
1354 mgmt_vdu_id = get_str(vnfd["mgmt-interface"], "vdu-id", 255)
1355 if mgmt_vdu_id not in vdu_id2uuid:
1356 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1357 "'{vdu}'. Reference to a non-existing vdu".format(
1358 vnf=vnfd_id, vdu=mgmt_vdu_id),
1359 httperrors.Bad_Request)
1360 mgmt_access["vm_id"] = vdu_id2uuid[mgmt_vdu_id]
1361 mgmt_access["vdu-id"] = mgmt_vdu_id
1362 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1363 if vdu_id2cp_name.get(mgmt_vdu_id):
1364 if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
1365 cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
1366
1367 if vnfd["mgmt-interface"].get("ip-address"):
1368 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
1369 if vnfd["mgmt-interface"].get("cp") and vnfd.get("vdu"):
1370 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
1371 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1372 "Reference to a non-existing connection-point".format(
1373 vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
1374 httperrors.Bad_Request)
1375 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
1376 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
1377 mgmt_access["vdu-id"] = cp_name2vdu_id[vnfd["mgmt-interface"]["cp"]]
1378 # mark this interface as of type mgmt
1379 if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
1380 cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
1381
1382 default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1383 "default-user", 64)
1384 if default_user:
1385 mgmt_access["default_user"] = default_user
1386
1387 required = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1388 "required", 6)
1389 if required:
1390 mgmt_access["required"] = required
1391
1392 password_ = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}),
1393 "password", 64)
1394 if password_:
1395 mgmt_access["password"] = password_
1396
1397 if mgmt_access:
1398 db_vnf["mgmt_access"] = yaml.safe_dump(mgmt_access, default_flow_style=True, width=256)
1399
1400 db_vnfs.append(db_vnf)
1401 db_tables=[
1402 {"vnfs": db_vnfs},
1403 {"nets": db_nets},
1404 {"images": db_images},
1405 {"flavors": db_flavors},
1406 {"ip_profiles": db_ip_profiles},
1407 {"vms": db_vms},
1408 {"interfaces": db_interfaces},
1409 ]
1410
1411 logger.debug("create_vnf Deployment done vnfDict: %s",
1412 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
1413 mydb.new_rows(db_tables, uuid_list)
1414 return vnfd_uuid_list
1415 except NfvoException:
1416 raise
1417 except Exception as e:
1418 logger.error("Exception {}".format(e))
1419 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1420
1421
1422 @deprecated("Use new_vnfd_v3")
1423 def new_vnf(mydb, tenant_id, vnf_descriptor):
1424 global global_config
1425
1426 # Step 1. Check the VNF descriptor
1427 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1)
1428 # Step 2. Check tenant exist
1429 vims = {}
1430 if tenant_id != "any":
1431 check_tenant(mydb, tenant_id)
1432 if "tenant_id" in vnf_descriptor["vnf"]:
1433 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1434 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1435 httperrors.Unauthorized)
1436 else:
1437 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1438 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1439 if global_config["auto_push_VNF_to_VIMs"]:
1440 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1441
1442 # Step 4. Review the descriptor and add missing fields
1443 #print vnf_descriptor
1444 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1445 vnf_name = vnf_descriptor['vnf']['name']
1446 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1447 if "physical" in vnf_descriptor['vnf']:
1448 del vnf_descriptor['vnf']['physical']
1449 #print vnf_descriptor
1450
1451 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1452 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1453 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1454
1455 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1456 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1457 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1458 try:
1459 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1460 for vnfc in vnf_descriptor['vnf']['VNFC']:
1461 VNFCitem={}
1462 VNFCitem["name"] = vnfc['name']
1463 VNFCitem["availability_zone"] = vnfc.get('availability_zone')
1464 VNFCitem["description"] = vnfc.get("description", 'VM {} of the VNF {}'.format(vnfc['name'],vnf_name))
1465
1466 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1467
1468 myflavorDict = {}
1469 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1470 myflavorDict["description"] = VNFCitem["description"]
1471 myflavorDict["ram"] = vnfc.get("ram", 0)
1472 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1473 myflavorDict["disk"] = vnfc.get("disk", 0)
1474 myflavorDict["extended"] = {}
1475
1476 devices = vnfc.get("devices")
1477 if devices != None:
1478 myflavorDict["extended"]["devices"] = devices
1479
1480 # TODO:
1481 # 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
1482 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1483
1484 # Previous code has been commented
1485 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1486 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1487 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1488 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1489 #else:
1490 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1491 # if result2:
1492 # print "Error creating flavor: unknown processor model. Rollback successful."
1493 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1494 # else:
1495 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1496 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1497
1498 if 'numas' in vnfc and len(vnfc['numas'])>0:
1499 myflavorDict['extended']['numas'] = vnfc['numas']
1500
1501 #print myflavorDict
1502
1503 # Step 6.2 New flavors are created in the VIM
1504 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1505
1506 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1507 VNFCitem["flavor_id"] = flavor_id
1508 VNFCDict[vnfc['name']] = VNFCitem
1509
1510 logger.debug("Creating new images in the VIM for each VNFC")
1511 # Step 6.3 New images are created in the VIM
1512 #For each VNFC, we must create the appropriate image.
1513 #This "for" loop might be integrated with the previous one
1514 #In case this integration is made, the VNFCDict might become a VNFClist.
1515 for vnfc in vnf_descriptor['vnf']['VNFC']:
1516 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1517 image_dict={}
1518 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1519 image_dict['universal_name']=vnfc.get('image name')
1520 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1521 image_dict['location']=vnfc.get('VNFC image')
1522 #image_dict['new_location']=vnfc.get('image location')
1523 image_dict['checksum']=vnfc.get('image checksum')
1524 image_metadata_dict = vnfc.get('image metadata', None)
1525 image_metadata_str = None
1526 if image_metadata_dict is not None:
1527 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1528 image_dict['metadata']=image_metadata_str
1529 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1530 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1531 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1532 VNFCDict[vnfc['name']]["image_id"] = image_id
1533 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1534 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1535 if vnfc.get("boot-data"):
1536 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1537
1538
1539 # Step 7. Storing the VNF descriptor in the repository
1540 if "descriptor" not in vnf_descriptor["vnf"]:
1541 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1542
1543 # Step 8. Adding the VNF to the NFVO DB
1544 vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1545 return vnf_id
1546 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1547 _, message = rollback(mydb, vims, rollback_list)
1548 if isinstance(e, db_base_Exception):
1549 error_text = "Exception at database"
1550 elif isinstance(e, KeyError):
1551 error_text = "KeyError exception "
1552 e.http_code = httperrors.Internal_Server_Error
1553 else:
1554 error_text = "Exception at VIM"
1555 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1556 #logger.error("start_scenario %s", error_text)
1557 raise NfvoException(error_text, e.http_code)
1558
1559
1560 @deprecated("Use new_vnfd_v3")
1561 def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
1562 global global_config
1563
1564 # Step 1. Check the VNF descriptor
1565 check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=2)
1566 # Step 2. Check tenant exist
1567 vims = {}
1568 if tenant_id != "any":
1569 check_tenant(mydb, tenant_id)
1570 if "tenant_id" in vnf_descriptor["vnf"]:
1571 if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
1572 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
1573 httperrors.Unauthorized)
1574 else:
1575 vnf_descriptor['vnf']['tenant_id'] = tenant_id
1576 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1577 if global_config["auto_push_VNF_to_VIMs"]:
1578 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1579
1580 # Step 4. Review the descriptor and add missing fields
1581 #print vnf_descriptor
1582 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1583 vnf_name = vnf_descriptor['vnf']['name']
1584 vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
1585 if "physical" in vnf_descriptor['vnf']:
1586 del vnf_descriptor['vnf']['physical']
1587 #print vnf_descriptor
1588
1589 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1590 logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
1591 logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
1592
1593 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1594 VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1595 rollback_list = [] # It will contain the new images created in mano. It is used for rollback
1596 try:
1597 logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1598 for vnfc in vnf_descriptor['vnf']['VNFC']:
1599 VNFCitem={}
1600 VNFCitem["name"] = vnfc['name']
1601 VNFCitem["description"] = vnfc.get("description", 'VM {} of the VNF {}'.format(vnfc['name'],vnf_name))
1602
1603 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1604
1605 myflavorDict = {}
1606 myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1607 myflavorDict["description"] = VNFCitem["description"]
1608 myflavorDict["ram"] = vnfc.get("ram", 0)
1609 myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
1610 myflavorDict["disk"] = vnfc.get("disk", 0)
1611 myflavorDict["extended"] = {}
1612
1613 devices = vnfc.get("devices")
1614 if devices != None:
1615 myflavorDict["extended"]["devices"] = devices
1616
1617 # TODO:
1618 # 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
1619 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1620
1621 # Previous code has been commented
1622 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1623 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1624 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1625 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1626 #else:
1627 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1628 # if result2:
1629 # print "Error creating flavor: unknown processor model. Rollback successful."
1630 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1631 # else:
1632 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1633 myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1634
1635 if 'numas' in vnfc and len(vnfc['numas'])>0:
1636 myflavorDict['extended']['numas'] = vnfc['numas']
1637
1638 #print myflavorDict
1639
1640 # Step 6.2 New flavors are created in the VIM
1641 flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
1642
1643 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1644 VNFCitem["flavor_id"] = flavor_id
1645 VNFCDict[vnfc['name']] = VNFCitem
1646
1647 logger.debug("Creating new images in the VIM for each VNFC")
1648 # Step 6.3 New images are created in the VIM
1649 #For each VNFC, we must create the appropriate image.
1650 #This "for" loop might be integrated with the previous one
1651 #In case this integration is made, the VNFCDict might become a VNFClist.
1652 for vnfc in vnf_descriptor['vnf']['VNFC']:
1653 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1654 image_dict={}
1655 image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
1656 image_dict['universal_name']=vnfc.get('image name')
1657 image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
1658 image_dict['location']=vnfc.get('VNFC image')
1659 #image_dict['new_location']=vnfc.get('image location')
1660 image_dict['checksum']=vnfc.get('image checksum')
1661 image_metadata_dict = vnfc.get('image metadata', None)
1662 image_metadata_str = None
1663 if image_metadata_dict is not None:
1664 image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
1665 image_dict['metadata']=image_metadata_str
1666 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1667 image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
1668 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1669 VNFCDict[vnfc['name']]["image_id"] = image_id
1670 VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
1671 VNFCDict[vnfc['name']]["count"] = vnfc.get('count', 1)
1672 if vnfc.get("boot-data"):
1673 VNFCDict[vnfc['name']]["boot_data"] = yaml.safe_dump(vnfc["boot-data"], default_flow_style=True, width=256)
1674
1675 # Step 7. Storing the VNF descriptor in the repository
1676 if "descriptor" not in vnf_descriptor["vnf"]:
1677 vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
1678
1679 # Step 8. Adding the VNF to the NFVO DB
1680 vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
1681 return vnf_id
1682 except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
1683 _, message = rollback(mydb, vims, rollback_list)
1684 if isinstance(e, db_base_Exception):
1685 error_text = "Exception at database"
1686 elif isinstance(e, KeyError):
1687 error_text = "KeyError exception "
1688 e.http_code = httperrors.Internal_Server_Error
1689 else:
1690 error_text = "Exception at VIM"
1691 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
1692 #logger.error("start_scenario %s", error_text)
1693 raise NfvoException(error_text, e.http_code)
1694
1695
1696 def get_vnf_id(mydb, tenant_id, vnf_id):
1697 #check valid tenant_id
1698 check_tenant(mydb, tenant_id)
1699 #obtain data
1700 where_or = {}
1701 if tenant_id != "any":
1702 where_or["tenant_id"] = tenant_id
1703 where_or["public"] = True
1704 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1705
1706 vnf_id = vnf["uuid"]
1707 filter_keys = ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1708 filtered_content = dict( (k,v) for k,v in vnf.items() if k in filter_keys )
1709 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1710 data={'vnf' : filtered_content}
1711 #GET VM
1712 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
1713 SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1714 'boot_data'),
1715 WHERE={'vnfs.uuid': vnf_id} )
1716 if len(content) != 0:
1717 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1718 # change boot_data into boot-data
1719 for vm in content:
1720 if vm.get("boot_data"):
1721 vm["boot-data"] = yaml.safe_load(vm["boot_data"])
1722 del vm["boot_data"]
1723
1724 data['vnf']['VNFC'] = content
1725 #TODO: GET all the information from a VNFC and include it in the output.
1726
1727 #GET NET
1728 content = mydb.get_rows(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id',
1729 SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1730 WHERE={'vnfs.uuid': vnf_id} )
1731 data['vnf']['nets'] = content
1732
1733 #GET ip-profile for each net
1734 for net in data['vnf']['nets']:
1735 ipprofiles = mydb.get_rows(FROM='ip_profiles',
1736 SELECT=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1737 WHERE={'net_id': net["uuid"]} )
1738 if len(ipprofiles)==1:
1739 net["ip_profile"] = ipprofiles[0]
1740 elif len(ipprofiles)>1:
1741 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net['uuid']), httperrors.Bad_Request)
1742
1743
1744 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1745
1746 #GET External Interfaces
1747 content = mydb.get_rows(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1748 SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1749 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1750 WHERE={'vnfs.uuid': vnf_id, 'interfaces.external_name<>': None} )
1751 #print content
1752 data['vnf']['external-connections'] = content
1753
1754 return data
1755
1756
1757 def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
1758 # Check tenant exist
1759 if tenant_id != "any":
1760 check_tenant(mydb, tenant_id)
1761 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1762 vims = get_vim(mydb, tenant_id, ignore_errors=True)
1763 else:
1764 vims={}
1765
1766 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1767 where_or = {}
1768 if tenant_id != "any":
1769 where_or["tenant_id"] = tenant_id
1770 where_or["public"] = True
1771 vnf = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND")
1772 vnf_id = vnf["uuid"]
1773
1774 # "Getting the list of flavors and tenants of the VNF"
1775 flavorList = get_flavorlist(mydb, vnf_id)
1776 if len(flavorList)==0:
1777 logger.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id)
1778
1779 imageList = get_imagelist(mydb, vnf_id)
1780 if len(imageList)==0:
1781 logger.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id)
1782
1783 deleted = mydb.delete_row_by_id('vnfs', vnf_id)
1784 if deleted == 0:
1785 raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1786
1787 undeletedItems = []
1788 for flavor in flavorList:
1789 #check if flavor is used by other vnf
1790 try:
1791 c = mydb.get_rows(FROM='vms', WHERE={'flavor_id':flavor} )
1792 if len(c) > 0:
1793 logger.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor)
1794 continue
1795 #flavor not used, must be deleted
1796 #delelte at VIM
1797 c = mydb.get_rows(FROM='datacenters_flavors', WHERE={'flavor_id': flavor})
1798 for flavor_vim in c:
1799 if not flavor_vim['created']: # skip this flavor because not created by openmano
1800 continue
1801 # look for vim
1802 myvim = None
1803 for vim in vims.values():
1804 if vim["config"]["datacenter_tenant_id"] == flavor_vim["datacenter_vim_id"]:
1805 myvim = vim
1806 break
1807 if not myvim:
1808 continue
1809 try:
1810 myvim.delete_flavor(flavor_vim["vim_id"])
1811 except vimconn.vimconnNotFoundException:
1812 logger.warn("VIM flavor %s not exist at datacenter %s", flavor_vim["vim_id"],
1813 flavor_vim["datacenter_vim_id"] )
1814 except vimconn.vimconnException as e:
1815 logger.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1816 flavor_vim["vim_id"], flavor_vim["datacenter_vim_id"], type(e).__name__, str(e))
1817 undeletedItems.append("flavor {} from VIM {}".format(flavor_vim["vim_id"],
1818 flavor_vim["datacenter_vim_id"]))
1819 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1820 mydb.delete_row_by_id('flavors', flavor)
1821 except db_base_Exception as e:
1822 logger.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor, str(e))
1823 undeletedItems.append("flavor {}".format(flavor))
1824
1825
1826 for image in imageList:
1827 try:
1828 #check if image is used by other vnf
1829 c = mydb.get_rows(FROM='vms', WHERE=[{'image_id': image}, {'image_list LIKE ': '%' + image + '%'}])
1830 if len(c) > 0:
1831 logger.debug("Image '%s' not deleted because it is being used by another VNF", image)
1832 continue
1833 #image not used, must be deleted
1834 #delelte at VIM
1835 c = mydb.get_rows(FROM='datacenters_images', WHERE={'image_id':image})
1836 for image_vim in c:
1837 if image_vim["datacenter_vim_id"] not in vims: # TODO change to datacenter_tenant_id
1838 continue
1839 if image_vim['created']=='false': #skip this image because not created by openmano
1840 continue
1841 myvim=vims[ image_vim["datacenter_id"] ]
1842 try:
1843 myvim.delete_image(image_vim["vim_id"])
1844 except vimconn.vimconnNotFoundException as e:
1845 logger.warn("VIM image %s not exist at datacenter %s", image_vim["vim_id"], image_vim["datacenter_id"] )
1846 except vimconn.vimconnException as e:
1847 logger.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1848 image_vim["vim_id"], image_vim["datacenter_id"], type(e).__name__, str(e))
1849 undeletedItems.append("image {} from VIM {}".format(image_vim["vim_id"], image_vim["datacenter_id"] ))
1850 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1851 mydb.delete_row_by_id('images', image)
1852 except db_base_Exception as e:
1853 logger.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image, str(e))
1854 undeletedItems.append("image {}".format(image))
1855
1856 return vnf_id + " " + vnf["name"]
1857 #if undeletedItems:
1858 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1859
1860
1861 @deprecated("Not used")
1862 def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
1863 result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
1864 if result < 0:
1865 return result, vims
1866 elif result == 0:
1867 return -httperrors.Not_Found, "datacenter '{}' not found".format(datacenter_name)
1868 myvim = next(iter(vims.values()))
1869 result,servers = myvim.get_hosts_info()
1870 if result < 0:
1871 return result, servers
1872 topology = {'name':myvim['name'] , 'servers': servers}
1873 return result, topology
1874
1875
1876 def get_hosts(mydb, nfvo_tenant_id):
1877 vims = get_vim(mydb, nfvo_tenant_id)
1878 if len(vims) == 0:
1879 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id)), httperrors.Not_Found)
1880 elif len(vims)>1:
1881 #print "nfvo.datacenter_action() error. Several datacenters found"
1882 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict)
1883 myvim = next(iter(vims.values()))
1884 try:
1885 hosts = myvim.get_hosts()
1886 logger.debug('VIM hosts response: '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False))
1887
1888 datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
1889 for host in hosts:
1890 server={'name':host['name'], 'vms':[]}
1891 for vm in host['instances']:
1892 #get internal name and model
1893 try:
1894 c = mydb.get_rows(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1895 WHERE={'vim_vm_id':vm['id']} )
1896 if len(c) == 0:
1897 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm['id']))
1898 continue
1899 server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
1900
1901 except db_base_Exception as e:
1902 logger.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm['id'], str(e)))
1903 datacenter['Datacenters'][0]['servers'].append(server)
1904 #return -400, "en construccion"
1905
1906 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1907 return datacenter
1908 except vimconn.vimconnException as e:
1909 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e)), e.http_code)
1910
1911
1912 @deprecated("Use new_nsd_v3")
1913 def new_scenario(mydb, tenant_id, topo):
1914
1915 # result, vims = get_vim(mydb, tenant_id)
1916 # if result < 0:
1917 # return result, vims
1918 #1: parse input
1919 if tenant_id != "any":
1920 check_tenant(mydb, tenant_id)
1921 if "tenant_id" in topo:
1922 if topo["tenant_id"] != tenant_id:
1923 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo["tenant_id"], tenant_id),
1924 httperrors.Unauthorized)
1925 else:
1926 tenant_id=None
1927
1928 #1.1: get VNFs and external_networks (other_nets).
1929 vnfs={}
1930 other_nets={} #external_networks, bridge_networks and data_networkds
1931 nodes = topo['topology']['nodes']
1932 for k in nodes.keys():
1933 if nodes[k]['type'] == 'VNF':
1934 vnfs[k] = nodes[k]
1935 vnfs[k]['ifaces'] = {}
1936 elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network':
1937 other_nets[k] = nodes[k]
1938 other_nets[k]['external']=True
1939 elif nodes[k]['type'] == 'network':
1940 other_nets[k] = nodes[k]
1941 other_nets[k]['external']=False
1942
1943
1944 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1945 for name,vnf in vnfs.items():
1946 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
1947 error_text = ""
1948 error_pos = "'topology':'nodes':'" + name + "'"
1949 if 'vnf_id' in vnf:
1950 error_text += " 'vnf_id' " + vnf['vnf_id']
1951 where['uuid'] = vnf['vnf_id']
1952 if 'VNF model' in vnf:
1953 error_text += " 'VNF model' " + vnf['VNF model']
1954 where['name'] = vnf['VNF model']
1955 if len(where) == 1:
1956 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos, httperrors.Bad_Request)
1957
1958 vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
1959 FROM='vnfs',
1960 WHERE=where)
1961 if len(vnf_db)==0:
1962 raise NfvoException("unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
1963 elif len(vnf_db)>1:
1964 raise NfvoException("more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
1965 vnf['uuid']=vnf_db[0]['uuid']
1966 vnf['description']=vnf_db[0]['description']
1967 #get external interfaces
1968 ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1969 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1970 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
1971 for ext_iface in ext_ifaces:
1972 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
1973
1974 #1.4 get list of connections
1975 conections = topo['topology']['connections']
1976 conections_list = []
1977 conections_list_name = []
1978 for k in conections.keys():
1979 if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
1980 ifaces_list = conections[k]['nodes'].items()
1981 elif type(conections[k]['nodes'])==list: #list with dictionary
1982 ifaces_list=[]
1983 conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
1984 for k2 in conection_pair_list:
1985 ifaces_list += k2
1986
1987 con_type = conections[k].get("type", "link")
1988 if con_type != "link":
1989 if k in other_nets:
1990 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k)), httperrors.Bad_Request)
1991 other_nets[k] = {'external': False}
1992 if conections[k].get("graph"):
1993 other_nets[k]["graph"] = conections[k]["graph"]
1994 ifaces_list.append( (k, None) )
1995
1996
1997 if con_type == "external_network":
1998 other_nets[k]['external'] = True
1999 if conections[k].get("model"):
2000 other_nets[k]["model"] = conections[k]["model"]
2001 else:
2002 other_nets[k]["model"] = k
2003 if con_type == "dataplane_net" or con_type == "bridge_net":
2004 other_nets[k]["model"] = con_type
2005
2006 conections_list_name.append(k)
2007 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)
2008 #print set(ifaces_list)
2009 #check valid VNF and iface names
2010 for iface in ifaces_list:
2011 if iface[0] not in vnfs and iface[0] not in other_nets :
2012 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
2013 str(k), iface[0]), httperrors.Not_Found)
2014 if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
2015 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
2016 str(k), iface[0], iface[1]), httperrors.Not_Found)
2017
2018 #1.5 unify connections from the pair list to a consolidated list
2019 index=0
2020 while index < len(conections_list):
2021 index2 = index+1
2022 while index2 < len(conections_list):
2023 if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
2024 conections_list[index] |= conections_list[index2]
2025 del conections_list[index2]
2026 del conections_list_name[index2]
2027 else:
2028 index2 += 1
2029 conections_list[index] = list(conections_list[index]) # from set to list again
2030 index += 1
2031 #for k in conections_list:
2032 # print k
2033
2034
2035
2036 #1.6 Delete non external nets
2037 # for k in other_nets.keys():
2038 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
2039 # for con in conections_list:
2040 # delete_indexes=[]
2041 # for index in range(0,len(con)):
2042 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
2043 # for index in delete_indexes:
2044 # del con[index]
2045 # del other_nets[k]
2046 #1.7: Check external_ports are present at database table datacenter_nets
2047 for k,net in other_nets.items():
2048 error_pos = "'topology':'nodes':'" + k + "'"
2049 if net['external']==False:
2050 if 'name' not in net:
2051 net['name']=k
2052 if 'model' not in net:
2053 raise NfvoException("needed a 'model' at " + error_pos, httperrors.Bad_Request)
2054 if net['model']=='bridge_net':
2055 net['type']='bridge';
2056 elif net['model']=='dataplane_net':
2057 net['type']='data';
2058 else:
2059 raise NfvoException("unknown 'model' '"+ net['model'] +"' at " + error_pos, httperrors.Not_Found)
2060 else: #external
2061 #IF we do not want to check that external network exist at datacenter
2062 pass
2063 #ELSE
2064 # error_text = ""
2065 # WHERE_={}
2066 # if 'net_id' in net:
2067 # error_text += " 'net_id' " + net['net_id']
2068 # WHERE_['uuid'] = net['net_id']
2069 # if 'model' in net:
2070 # error_text += " 'model' " + net['model']
2071 # WHERE_['name'] = net['model']
2072 # if len(WHERE_) == 0:
2073 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2074 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2075 # FROM='datacenter_nets', WHERE=WHERE_ )
2076 # if r<0:
2077 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2078 # elif r==0:
2079 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2080 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2081 # elif r>1:
2082 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2083 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2084 # other_nets[k].update(net_db[0])
2085 #ENDIF
2086 net_list={}
2087 net_nb=0 #Number of nets
2088 for con in conections_list:
2089 #check if this is connected to a external net
2090 other_net_index=-1
2091 #print
2092 #print "con", con
2093 for index in range(0,len(con)):
2094 #check if this is connected to a external net
2095 for net_key in other_nets.keys():
2096 if con[index][0]==net_key:
2097 if other_net_index>=0:
2098 error_text = "There is some interface connected both to net '{}' and net '{}'".format(
2099 con[other_net_index][0], net_key)
2100 #print "nfvo.new_scenario " + error_text
2101 raise NfvoException(error_text, httperrors.Bad_Request)
2102 else:
2103 other_net_index = index
2104 net_target = net_key
2105 break
2106 #print "other_net_index", other_net_index
2107 try:
2108 if other_net_index>=0:
2109 del con[other_net_index]
2110 #IF we do not want to check that external network exist at datacenter
2111 if other_nets[net_target]['external'] :
2112 if "name" not in other_nets[net_target]:
2113 other_nets[net_target]['name'] = other_nets[net_target]['model']
2114 if other_nets[net_target]["type"] == "external_network":
2115 if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
2116 other_nets[net_target]["type"] = "data"
2117 else:
2118 other_nets[net_target]["type"] = "bridge"
2119 #ELSE
2120 # if other_nets[net_target]['external'] :
2121 # 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
2122 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2123 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2124 # print "nfvo.new_scenario " + error_text
2125 # return -httperrors.Bad_Request, error_text
2126 #ENDIF
2127 for iface in con:
2128 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2129 else:
2130 #create a net
2131 net_type_bridge=False
2132 net_type_data=False
2133 net_target = "__-__net"+str(net_nb)
2134 net_list[net_target] = {'name': conections_list_name[net_nb], #"net-"+str(net_nb),
2135 'description':"net-{} in scenario {}".format(net_nb,topo['name']),
2136 'external':False}
2137 for iface in con:
2138 vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
2139 iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
2140 if iface_type=='mgmt' or iface_type=='bridge':
2141 net_type_bridge = True
2142 else:
2143 net_type_data = True
2144 if net_type_bridge and net_type_data:
2145 error_text = "Error connection interfaces of bridge type with data type. Firs node {}, iface {}".format(iface[0], iface[1])
2146 #print "nfvo.new_scenario " + error_text
2147 raise NfvoException(error_text, httperrors.Bad_Request)
2148 elif net_type_bridge:
2149 type_='bridge'
2150 else:
2151 type_='data' if len(con)>2 else 'ptp'
2152 net_list[net_target]['type'] = type_
2153 net_nb+=1
2154 except Exception:
2155 error_text = "Error connection node {} : {} does not match any VNF or interface".format(iface[0], iface[1])
2156 #print "nfvo.new_scenario " + error_text
2157 #raise e
2158 raise NfvoException(error_text, httperrors.Bad_Request)
2159
2160 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2161 #1.8.1 obtain management net
2162 mgmt_net = mydb.get_rows(SELECT=('uuid','name','description','type','shared'),
2163 FROM='datacenter_nets', WHERE={'name':'mgmt'} )
2164 #1.8.2 check all interfaces from all vnfs
2165 if len(mgmt_net)>0:
2166 add_mgmt_net = False
2167 for vnf in vnfs.values():
2168 for iface in vnf['ifaces'].values():
2169 if iface['type']=='mgmt' and 'net_key' not in iface:
2170 #iface not connected
2171 iface['net_key'] = 'mgmt'
2172 add_mgmt_net = True
2173 if add_mgmt_net and 'mgmt' not in net_list:
2174 net_list['mgmt']=mgmt_net[0]
2175 net_list['mgmt']['external']=True
2176 net_list['mgmt']['graph']={'visible':False}
2177
2178 net_list.update(other_nets)
2179 #print
2180 #print 'net_list', net_list
2181 #print
2182 #print 'vnfs', vnfs
2183 #print
2184
2185 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2186 c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
2187 'tenant_id':tenant_id, 'name':topo['name'],
2188 'description':topo.get('description',topo['name']),
2189 'public': topo.get('public', False)
2190 })
2191
2192 return c
2193
2194
2195 @deprecated("Use new_nsd_v3")
2196 def new_scenario_v02(mydb, tenant_id, scenario_dict, version):
2197 """ This creates a new scenario for version 0.2 and 0.3"""
2198 scenario = scenario_dict["scenario"]
2199 if tenant_id != "any":
2200 check_tenant(mydb, tenant_id)
2201 if "tenant_id" in scenario:
2202 if scenario["tenant_id"] != tenant_id:
2203 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2204 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2205 scenario["tenant_id"], tenant_id), httperrors.Unauthorized)
2206 else:
2207 tenant_id=None
2208
2209 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2210 for name,vnf in scenario["vnfs"].items():
2211 where = {"OR": {"tenant_id": tenant_id, 'public': "true"}}
2212 error_text = ""
2213 error_pos = "'scenario':'vnfs':'" + name + "'"
2214 if 'vnf_id' in vnf:
2215 error_text += " 'vnf_id' " + vnf['vnf_id']
2216 where['uuid'] = vnf['vnf_id']
2217 if 'vnf_name' in vnf:
2218 error_text += " 'vnf_name' " + vnf['vnf_name']
2219 where['name'] = vnf['vnf_name']
2220 if len(where) == 1:
2221 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, httperrors.Bad_Request)
2222 vnf_db = mydb.get_rows(SELECT=('uuid', 'name', 'description'),
2223 FROM='vnfs',
2224 WHERE=where)
2225 if len(vnf_db) == 0:
2226 raise NfvoException("Unknown" + error_text + " at " + error_pos, httperrors.Not_Found)
2227 elif len(vnf_db) > 1:
2228 raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", httperrors.Conflict)
2229 vnf['uuid'] = vnf_db[0]['uuid']
2230 vnf['description'] = vnf_db[0]['description']
2231 vnf['ifaces'] = {}
2232 # get external interfaces
2233 ext_ifaces = mydb.get_rows(SELECT=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2234 FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2235 WHERE={'vnfs.uuid':vnf['uuid'], 'external_name<>': None} )
2236 for ext_iface in ext_ifaces:
2237 vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type': ext_iface['type']}
2238 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2239
2240 # 2: Insert net_key and ip_address at every vnf interface
2241 for net_name, net in scenario["networks"].items():
2242 net_type_bridge = False
2243 net_type_data = False
2244 for iface_dict in net["interfaces"]:
2245 if version == "0.2":
2246 temp_dict = iface_dict
2247 ip_address = None
2248 elif version == "0.3":
2249 temp_dict = {iface_dict["vnf"] : iface_dict["vnf_interface"]}
2250 ip_address = iface_dict.get('ip_address', None)
2251 for vnf, iface in temp_dict.items():
2252 if vnf not in scenario["vnfs"]:
2253 error_text = "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2254 net_name, vnf)
2255 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2256 raise NfvoException(error_text, httperrors.Not_Found)
2257 if iface not in scenario["vnfs"][vnf]['ifaces']:
2258 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2259 .format(net_name, iface)
2260 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2261 raise NfvoException(error_text, httperrors.Bad_Request)
2262 if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
2263 error_text = "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2264 "'{}'".format(net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
2265 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2266 raise NfvoException(error_text, httperrors.Bad_Request)
2267 scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
2268 scenario["vnfs"][vnf]['ifaces'][iface]['ip_address'] = ip_address
2269 iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
2270 if iface_type == 'mgmt' or iface_type == 'bridge':
2271 net_type_bridge = True
2272 else:
2273 net_type_data = True
2274
2275 if net_type_bridge and net_type_data:
2276 error_text = "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2277 .format(net_name)
2278 # logger.debug("nfvo.new_scenario " + error_text)
2279 raise NfvoException(error_text, httperrors.Bad_Request)
2280 elif net_type_bridge:
2281 type_ = 'bridge'
2282 else:
2283 type_ = 'data' if len(net["interfaces"]) > 2 else 'ptp'
2284
2285 if net.get("implementation"): # for v0.3
2286 if type_ == "bridge" and net["implementation"] == "underlay":
2287 error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2288 "'network':'{}'".format(net_name)
2289 # logger.debug(error_text)
2290 raise NfvoException(error_text, httperrors.Bad_Request)
2291 elif type_ != "bridge" and net["implementation"] == "overlay":
2292 error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2293 "'network':'{}'".format(net_name)
2294 # logger.debug(error_text)
2295 raise NfvoException(error_text, httperrors.Bad_Request)
2296 net.pop("implementation")
2297 if "type" in net and version == "0.3": # for v0.3
2298 if type_ == "data" and net["type"] == "e-line":
2299 error_text = "Error connecting more than 2 interfaces of data type to a network declared as type "\
2300 "'e-line' at 'network':'{}'".format(net_name)
2301 # logger.debug(error_text)
2302 raise NfvoException(error_text, httperrors.Bad_Request)
2303 elif type_ == "ptp" and net["type"] == "e-lan":
2304 type_ = "data"
2305
2306 net['type'] = type_
2307 net['name'] = net_name
2308 net['external'] = net.get('external', False)
2309
2310 # 3: insert at database
2311 scenario["nets"] = scenario["networks"]
2312 scenario['tenant_id'] = tenant_id
2313 scenario_id = mydb.new_scenario(scenario)
2314 return scenario_id
2315
2316
2317 def new_nsd_v3(mydb, tenant_id, nsd_descriptor):
2318 """
2319 Parses an OSM IM nsd_catalog and insert at DB
2320 :param mydb:
2321 :param tenant_id:
2322 :param nsd_descriptor:
2323 :return: The list of created NSD ids
2324 """
2325 try:
2326 mynsd = nsd_catalog.nsd()
2327 try:
2328 pybindJSONDecoder.load_ietf_json(nsd_descriptor, None, None, obj=mynsd, skip_unknown=True)
2329 except Exception as e:
2330 raise NfvoException("Error. Invalid NS descriptor format: " + str(e), httperrors.Bad_Request)
2331 db_scenarios = []
2332 db_sce_nets = []
2333 db_sce_vnfs = []
2334 db_sce_interfaces = []
2335 db_sce_vnffgs = []
2336 db_sce_rsps = []
2337 db_sce_rsp_hops = []
2338 db_sce_classifiers = []
2339 db_sce_classifier_matches = []
2340 db_ip_profiles = []
2341 db_ip_profiles_index = 0
2342 uuid_list = []
2343 nsd_uuid_list = []
2344 for nsd_yang in mynsd.nsd_catalog.nsd.values():
2345 nsd = nsd_yang.get()
2346
2347 # table scenarios
2348 scenario_uuid = str(uuid4())
2349 uuid_list.append(scenario_uuid)
2350 nsd_uuid_list.append(scenario_uuid)
2351 db_scenario = {
2352 "uuid": scenario_uuid,
2353 "osm_id": get_str(nsd, "id", 255),
2354 "name": get_str(nsd, "name", 255),
2355 "description": get_str(nsd, "description", 255),
2356 "tenant_id": tenant_id,
2357 "vendor": get_str(nsd, "vendor", 255),
2358 "short_name": get_str(nsd, "short-name", 255),
2359 "descriptor": str(nsd_descriptor)[:60000],
2360 }
2361 db_scenarios.append(db_scenario)
2362
2363 # table sce_vnfs (constituent-vnfd)
2364 vnf_index2scevnf_uuid = {}
2365 vnf_index2vnf_uuid = {}
2366 for vnf in nsd.get("constituent-vnfd").values():
2367 existing_vnf = mydb.get_rows(FROM="vnfs", WHERE={'osm_id': str(vnf["vnfd-id-ref"])[:255],
2368 'tenant_id': tenant_id})
2369 if not existing_vnf:
2370 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2371 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2372 str(nsd["id"]), str(vnf["vnfd-id-ref"])[:255]),
2373 httperrors.Bad_Request)
2374 sce_vnf_uuid = str(uuid4())
2375 uuid_list.append(sce_vnf_uuid)
2376 db_sce_vnf = {
2377 "uuid": sce_vnf_uuid,
2378 "scenario_id": scenario_uuid,
2379 # "name": get_str(vnf, "member-vnf-index", 255),
2380 "name": existing_vnf[0]["name"][:200] + "." + get_str(vnf, "member-vnf-index", 50),
2381 "vnf_id": existing_vnf[0]["uuid"],
2382 "member_vnf_index": str(vnf["member-vnf-index"]),
2383 # TODO 'start-by-default': True
2384 }
2385 vnf_index2scevnf_uuid[str(vnf['member-vnf-index'])] = sce_vnf_uuid
2386 vnf_index2vnf_uuid[str(vnf['member-vnf-index'])] = existing_vnf[0]["uuid"]
2387 db_sce_vnfs.append(db_sce_vnf)
2388
2389 # table ip_profiles (ip-profiles)
2390 ip_profile_name2db_table_index = {}
2391 for ip_profile in nsd.get("ip-profiles").values():
2392 db_ip_profile = {
2393 "ip_version": str(ip_profile["ip-profile-params"].get("ip-version", "ipv4")),
2394 "subnet_address": str(ip_profile["ip-profile-params"].get("subnet-address")),
2395 "gateway_address": str(ip_profile["ip-profile-params"].get("gateway-address")),
2396 "dhcp_enabled": str(ip_profile["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2397 "dhcp_start_address": str(ip_profile["ip-profile-params"]["dhcp-params"].get("start-address")),
2398 "dhcp_count": str(ip_profile["ip-profile-params"]["dhcp-params"].get("count")),
2399 }
2400 dns_list = []
2401 for dns in ip_profile["ip-profile-params"]["dns-server"].values():
2402 dns_list.append(str(dns.get("address")))
2403 db_ip_profile["dns_address"] = ";".join(dns_list)
2404 if ip_profile["ip-profile-params"].get('security-group'):
2405 db_ip_profile["security_group"] = ip_profile["ip-profile-params"]['security-group']
2406 ip_profile_name2db_table_index[str(ip_profile["name"])] = db_ip_profiles_index
2407 db_ip_profiles_index += 1
2408 db_ip_profiles.append(db_ip_profile)
2409
2410 # table sce_nets (internal-vld)
2411 for vld in nsd.get("vld").values():
2412 sce_net_uuid = str(uuid4())
2413 uuid_list.append(sce_net_uuid)
2414 db_sce_net = {
2415 "uuid": sce_net_uuid,
2416 "name": get_str(vld, "name", 255),
2417 "scenario_id": scenario_uuid,
2418 # "type": #TODO
2419 "multipoint": not vld.get("type") == "ELINE",
2420 "osm_id": get_str(vld, "id", 255),
2421 # "external": #TODO
2422 "description": get_str(vld, "description", 255),
2423 }
2424 # guess type of network
2425 if vld.get("mgmt-network"):
2426 db_sce_net["type"] = "bridge"
2427 db_sce_net["external"] = True
2428 elif vld.get("provider-network").get("overlay-type") == "VLAN":
2429 db_sce_net["type"] = "data"
2430 else:
2431 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2432 db_sce_net["type"] = None
2433 db_sce_nets.append(db_sce_net)
2434
2435 # ip-profile, link db_ip_profile with db_sce_net
2436 if vld.get("ip-profile-ref"):
2437 ip_profile_name = vld.get("ip-profile-ref")
2438 if ip_profile_name not in ip_profile_name2db_table_index:
2439 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2440 " Reference to a non-existing 'ip_profiles'".format(
2441 str(nsd["id"]), str(vld["id"]), str(vld["ip-profile-ref"])),
2442 httperrors.Bad_Request)
2443 db_ip_profiles[ip_profile_name2db_table_index[ip_profile_name]]["sce_net_id"] = sce_net_uuid
2444 elif vld.get("vim-network-name"):
2445 db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255)
2446
2447 # table sce_interfaces (vld:vnfd-connection-point-ref)
2448 for iface in vld.get("vnfd-connection-point-ref").values():
2449 # Check if there are VDUs in the descriptor
2450 vnf_index = str(iface['member-vnf-index-ref'])
2451 existing_vdus = mydb.get_rows(SELECT=('vms.uuid'), FROM="vms", WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index]})
2452 if existing_vdus:
2453 # check correct parameters
2454 if vnf_index not in vnf_index2vnf_uuid:
2455 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2456 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2457 "'nsd':'constituent-vnfd'".format(
2458 str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])),
2459 httperrors.Bad_Request)
2460
2461 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'),
2462 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2463 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2464 'external_name': get_str(iface, "vnfd-connection-point-ref",
2465 255)})
2466 if not existing_ifaces:
2467 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2468 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2469 "connection-point name at VNFD '{}'".format(
2470 str(nsd["id"]), str(vld["id"]), str(iface["vnfd-connection-point-ref"]),
2471 str(iface.get("vnfd-id-ref"))[:255]),
2472 httperrors.Bad_Request)
2473 interface_uuid = existing_ifaces[0]["uuid"]
2474 if existing_ifaces[0]["iface_type"] == "data":
2475 db_sce_net["type"] = "data"
2476 sce_interface_uuid = str(uuid4())
2477 uuid_list.append(sce_net_uuid)
2478 iface_ip_address = None
2479 if iface.get("ip-address"):
2480 iface_ip_address = str(iface.get("ip-address"))
2481 db_sce_interface = {
2482 "uuid": sce_interface_uuid,
2483 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2484 "sce_net_id": sce_net_uuid,
2485 "interface_id": interface_uuid,
2486 "ip_address": iface_ip_address,
2487 }
2488 db_sce_interfaces.append(db_sce_interface)
2489 if not db_sce_net["type"]:
2490 db_sce_net["type"] = "bridge"
2491
2492 # table sce_vnffgs (vnffgd)
2493 for vnffg in nsd.get("vnffgd").values():
2494 sce_vnffg_uuid = str(uuid4())
2495 uuid_list.append(sce_vnffg_uuid)
2496 db_sce_vnffg = {
2497 "uuid": sce_vnffg_uuid,
2498 "name": get_str(vnffg, "name", 255),
2499 "scenario_id": scenario_uuid,
2500 "vendor": get_str(vnffg, "vendor", 255),
2501 "description": get_str(vld, "description", 255),
2502 }
2503 db_sce_vnffgs.append(db_sce_vnffg)
2504
2505 # deal with rsps
2506 for rsp in vnffg.get("rsp").values():
2507 sce_rsp_uuid = str(uuid4())
2508 uuid_list.append(sce_rsp_uuid)
2509 db_sce_rsp = {
2510 "uuid": sce_rsp_uuid,
2511 "name": get_str(rsp, "name", 255),
2512 "sce_vnffg_id": sce_vnffg_uuid,
2513 "id": get_str(rsp, "id", 255), # only useful to link with classifiers; will be removed later in the code
2514 }
2515 db_sce_rsps.append(db_sce_rsp)
2516 for iface in rsp.get("vnfd-connection-point-ref").values():
2517 vnf_index = str(iface['member-vnf-index-ref'])
2518 if_order = int(iface['order'])
2519 # check correct parameters
2520 if vnf_index not in vnf_index2vnf_uuid:
2521 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2522 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2523 "'nsd':'constituent-vnfd'".format(
2524 str(nsd["id"]), str(rsp["id"]), str(iface["member-vnf-index-ref"])),
2525 httperrors.Bad_Request)
2526
2527 ingress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2528 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2529 WHERE={
2530 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2531 'external_name': get_str(iface, "vnfd-ingress-connection-point-ref",
2532 255)})
2533 if not ingress_existing_ifaces:
2534 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2535 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2536 "connection-point name at VNFD '{}'".format(
2537 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-ingress-connection-point-ref"]),
2538 str(iface.get("vnfd-id-ref"))[:255]), httperrors.Bad_Request)
2539
2540 egress_existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2541 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2542 WHERE={
2543 'vnf_id': vnf_index2vnf_uuid[vnf_index],
2544 'external_name': get_str(iface, "vnfd-egress-connection-point-ref",
2545 255)})
2546 if not egress_existing_ifaces:
2547 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2548 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2549 "connection-point name at VNFD '{}'".format(
2550 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-egress-connection-point-ref"]),
2551 str(iface.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request)
2552
2553 ingress_interface_uuid = ingress_existing_ifaces[0]["uuid"]
2554 egress_interface_uuid = egress_existing_ifaces[0]["uuid"]
2555 sce_rsp_hop_uuid = str(uuid4())
2556 uuid_list.append(sce_rsp_hop_uuid)
2557 db_sce_rsp_hop = {
2558 "uuid": sce_rsp_hop_uuid,
2559 "if_order": if_order,
2560 "ingress_interface_id": ingress_interface_uuid,
2561 "egress_interface_id": egress_interface_uuid,
2562 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2563 "sce_rsp_id": sce_rsp_uuid,
2564 }
2565 db_sce_rsp_hops.append(db_sce_rsp_hop)
2566
2567 # deal with classifiers
2568 for classifier in vnffg.get("classifier").values():
2569 sce_classifier_uuid = str(uuid4())
2570 uuid_list.append(sce_classifier_uuid)
2571
2572 # source VNF
2573 vnf_index = str(classifier['member-vnf-index-ref'])
2574 if vnf_index not in vnf_index2vnf_uuid:
2575 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2576 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2577 "'nsd':'constituent-vnfd'".format(
2578 str(nsd["id"]), str(classifier["id"]), str(classifier["member-vnf-index-ref"])),
2579 httperrors.Bad_Request)
2580 existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid',),
2581 FROM="interfaces as i join vms on i.vm_id=vms.uuid",
2582 WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index],
2583 'external_name': get_str(classifier, "vnfd-connection-point-ref",
2584 255)})
2585 if not existing_ifaces:
2586 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2587 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2588 "connection-point name at VNFD '{}'".format(
2589 str(nsd["id"]), str(rsp["id"]), str(iface["vnfd-connection-point-ref"]),
2590 str(iface.get("vnfd-id-ref"))[:255]),
2591 httperrors.Bad_Request)
2592 interface_uuid = existing_ifaces[0]["uuid"]
2593
2594 db_sce_classifier = {
2595 "uuid": sce_classifier_uuid,
2596 "name": get_str(classifier, "name", 255),
2597 "sce_vnffg_id": sce_vnffg_uuid,
2598 "sce_vnf_id": vnf_index2scevnf_uuid[vnf_index],
2599 "interface_id": interface_uuid,
2600 }
2601 rsp_id = get_str(classifier, "rsp-id-ref", 255)
2602 rsp = next((item for item in db_sce_rsps if item["id"] == rsp_id), None)
2603 db_sce_classifier["sce_rsp_id"] = rsp["uuid"]
2604 db_sce_classifiers.append(db_sce_classifier)
2605
2606 for match in classifier.get("match-attributes").values():
2607 sce_classifier_match_uuid = str(uuid4())
2608 uuid_list.append(sce_classifier_match_uuid)
2609 db_sce_classifier_match = {
2610 "uuid": sce_classifier_match_uuid,
2611 "ip_proto": get_str(match, "ip-proto", 2),
2612 "source_ip": get_str(match, "source-ip-address", 16),
2613 "destination_ip": get_str(match, "destination-ip-address", 16),
2614 "source_port": get_str(match, "source-port", 5),
2615 "destination_port": get_str(match, "destination-port", 5),
2616 "sce_classifier_id": sce_classifier_uuid,
2617 }
2618 db_sce_classifier_matches.append(db_sce_classifier_match)
2619 # TODO: vnf/cp keys
2620
2621 # remove unneeded id's in sce_rsps
2622 for rsp in db_sce_rsps:
2623 rsp.pop('id')
2624
2625 db_tables = [
2626 {"scenarios": db_scenarios},
2627 {"sce_nets": db_sce_nets},
2628 {"ip_profiles": db_ip_profiles},
2629 {"sce_vnfs": db_sce_vnfs},
2630 {"sce_interfaces": db_sce_interfaces},
2631 {"sce_vnffgs": db_sce_vnffgs},
2632 {"sce_rsps": db_sce_rsps},
2633 {"sce_rsp_hops": db_sce_rsp_hops},
2634 {"sce_classifiers": db_sce_classifiers},
2635 {"sce_classifier_matches": db_sce_classifier_matches},
2636 ]
2637
2638 logger.debug("new_nsd_v3 done: %s",
2639 yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
2640 mydb.new_rows(db_tables, uuid_list)
2641 return nsd_uuid_list
2642 except NfvoException:
2643 raise
2644 except Exception as e:
2645 logger.error("Exception {}".format(e))
2646 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2647
2648
2649 def edit_scenario(mydb, tenant_id, scenario_id, data):
2650 data["uuid"] = scenario_id
2651 data["tenant_id"] = tenant_id
2652 c = mydb.edit_scenario( data )
2653 return c
2654
2655
2656 @deprecated("Use create_instance")
2657 def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
2658 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2659 datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter, vim_tenant=vim_tenant)
2660 vims = {datacenter_id: myvim}
2661 myvim_tenant = myvim['tenant_id']
2662 datacenter_name = myvim['name']
2663
2664 rollbackList=[]
2665 try:
2666 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2667 scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id=datacenter_id)
2668 scenarioDict['datacenter2tenant'] = { datacenter_id: myvim['config']['datacenter_tenant_id'] }
2669 scenarioDict['datacenter_id'] = datacenter_id
2670 #print '================scenarioDict======================='
2671 #print json.dumps(scenarioDict, indent=4)
2672 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2673
2674 logger.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict['name'],len(scenarioDict['vnfs']))
2675 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2676
2677 auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2678 auxNetDict['scenario'] = {}
2679
2680 logger.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2681 for sce_net in scenarioDict['nets']:
2682 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2683
2684 myNetName = "{}.{}".format(instance_scenario_name, sce_net['name'])
2685 myNetName = myNetName[0:255] #limit length
2686 myNetType = sce_net['type']
2687 myNetDict = {}
2688 myNetDict["name"] = myNetName
2689 myNetDict["type"] = myNetType
2690 myNetDict["tenant_id"] = myvim_tenant
2691 myNetIPProfile = sce_net.get('ip_profile', None)
2692 myProviderNetwork = sce_net.get('provider_network', None)
2693 #TODO:
2694 #We should use the dictionary as input parameter for new_network
2695 #print myNetDict
2696 if not sce_net["external"]:
2697 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile, provider_network_profile=myProviderNetwork)
2698 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2699 sce_net['vim_id'] = network_id
2700 auxNetDict['scenario'][sce_net['uuid']] = network_id
2701 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2702 sce_net["created"] = True
2703 else:
2704 if sce_net['vim_id'] == None:
2705 error_text = "Error, datacenter '{}' does not have external network '{}'.".format(
2706 datacenter_name, sce_net['name'])
2707 _, message = rollback(mydb, vims, rollbackList)
2708 logger.error("nfvo.start_scenario: %s", error_text)
2709 raise NfvoException(error_text, httperrors.Bad_Request)
2710 logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
2711 auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
2712
2713 logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2714 #For each vnf net, we create it and we add it to instanceNetlist.
2715
2716 for sce_vnf in scenarioDict['vnfs']:
2717 for net in sce_vnf['nets']:
2718 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2719
2720 myNetName = "{}.{}".format(instance_scenario_name,net['name'])
2721 myNetName = myNetName[0:255] #limit length
2722 myNetType = net['type']
2723 myNetDict = {}
2724 myNetDict["name"] = myNetName
2725 myNetDict["type"] = myNetType
2726 myNetDict["tenant_id"] = myvim_tenant
2727 myNetIPProfile = net.get('ip_profile', None)
2728 myProviderNetwork = sce_net.get('provider_network', None)
2729 #print myNetDict
2730 #TODO:
2731 #We should use the dictionary as input parameter for new_network
2732 network_id, _ = myvim.new_network(myNetName, myNetType, myNetIPProfile, provider_network_profile=myProviderNetwork)
2733 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2734 net['vim_id'] = network_id
2735 if sce_vnf['uuid'] not in auxNetDict:
2736 auxNetDict[sce_vnf['uuid']] = {}
2737 auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
2738 rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
2739 net["created"] = True
2740
2741 #print "auxNetDict:"
2742 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2743
2744 logger.debug("start_scenario 3. Creating new vm instances in the VIM")
2745 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2746 i = 0
2747 for sce_vnf in scenarioDict['vnfs']:
2748 vnf_availability_zones = []
2749 for vm in sce_vnf['vms']:
2750 vm_av = vm.get('availability_zone')
2751 if vm_av and vm_av not in vnf_availability_zones:
2752 vnf_availability_zones.append(vm_av)
2753
2754 # check if there is enough availability zones available at vim level.
2755 if myvims[datacenter_id].availability_zone and vnf_availability_zones:
2756 if len(vnf_availability_zones) > len(myvims[datacenter_id].availability_zone):
2757 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors.Bad_Request)
2758
2759 for vm in sce_vnf['vms']:
2760 i += 1
2761 myVMDict = {}
2762 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2763 myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
2764 #myVMDict['description'] = vm['description']
2765 myVMDict['description'] = myVMDict['name'][0:99]
2766 if not startvms:
2767 myVMDict['start'] = "no"
2768 myVMDict['name'] = myVMDict['name'][0:255] #limit name length
2769 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2770
2771 #create image at vim in case it not exist
2772 image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
2773 image_id = create_or_use_image(mydb, vims, image_dict, [], True)
2774 vm['vim_image_id'] = image_id
2775
2776 #create flavor at vim in case it not exist
2777 flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
2778 if flavor_dict['extended']!=None:
2779 flavor_dict['extended']= yaml.load(flavor_dict['extended'], Loader=yaml.Loader)
2780 flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
2781 vm['vim_flavor_id'] = flavor_id
2782
2783
2784 myVMDict['imageRef'] = vm['vim_image_id']
2785 myVMDict['flavorRef'] = vm['vim_flavor_id']
2786 myVMDict['networks'] = []
2787 for iface in vm['interfaces']:
2788 netDict = {}
2789 if iface['type']=="data":
2790 netDict['type'] = iface['model']
2791 elif "model" in iface and iface["model"]!=None:
2792 netDict['model']=iface['model']
2793 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2794 #discover type of interface looking at flavor
2795 for numa in flavor_dict.get('extended',{}).get('numas',[]):
2796 for flavor_iface in numa.get('interfaces',[]):
2797 if flavor_iface.get('name') == iface['internal_name']:
2798 if flavor_iface['dedicated'] == 'yes':
2799 netDict['type']="PF" #passthrough
2800 elif flavor_iface['dedicated'] == 'no':
2801 netDict['type']="VF" #siov
2802 elif flavor_iface['dedicated'] == 'yes:sriov':
2803 netDict['type']="VFnotShared" #sriov but only one sriov on the PF
2804 netDict["mac_address"] = flavor_iface.get("mac_address")
2805 break;
2806 netDict["use"]=iface['type']
2807 if netDict["use"]=="data" and not netDict.get("type"):
2808 #print "netDict", netDict
2809 #print "iface", iface
2810 e_text = "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".format(
2811 sce_vnf['name'], vm['name'], iface['internal_name'])
2812 if flavor_dict.get('extended')==None:
2813 raise NfvoException(e_text + "After database migration some information is not available. \
2814 Try to delete and create the scenarios and VNFs again", httperrors.Conflict)
2815 else:
2816 raise NfvoException(e_text, httperrors.Internal_Server_Error)
2817 if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
2818 netDict["type"]="virtual"
2819 if "vpci" in iface and iface["vpci"] is not None:
2820 netDict['vpci'] = iface['vpci']
2821 if "mac" in iface and iface["mac"] is not None:
2822 netDict['mac_address'] = iface['mac']
2823 if "port-security" in iface and iface["port-security"] is not None:
2824 netDict['port_security'] = iface['port-security']
2825 if "floating-ip" in iface and iface["floating-ip"] is not None:
2826 netDict['floating_ip'] = iface['floating-ip']
2827 netDict['name'] = iface['internal_name']
2828 if iface['net_id'] is None:
2829 for vnf_iface in sce_vnf["interfaces"]:
2830 #print iface
2831 #print vnf_iface
2832 if vnf_iface['interface_id']==iface['uuid']:
2833 netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
2834 break
2835 else:
2836 netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
2837 #skip bridge ifaces not connected to any net
2838 #if 'net_id' not in netDict or netDict['net_id']==None:
2839 # continue
2840 myVMDict['networks'].append(netDict)
2841 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2842 #print myVMDict['name']
2843 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2844 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2845 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2846
2847 if 'availability_zone' in myVMDict:
2848 av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
2849 else:
2850 av_index = None
2851
2852 vm_id, _ = myvim.new_vminstance(myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
2853 myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
2854 availability_zone_index=av_index,
2855 availability_zone_list=vnf_availability_zones)
2856 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2857 vm['vim_id'] = vm_id
2858 rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
2859 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2860 for net in myVMDict['networks']:
2861 if "vim_id" in net:
2862 for iface in vm['interfaces']:
2863 if net["name"]==iface["internal_name"]:
2864 iface["vim_id"]=net["vim_id"]
2865 break
2866
2867 logger.debug("start scenario Deployment done")
2868 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2869 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2870 instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
2871 return mydb.get_instance_scenario(instance_id)
2872
2873 except (db_base_Exception, vimconn.vimconnException) as e:
2874 _, message = rollback(mydb, vims, rollbackList)
2875 if isinstance(e, db_base_Exception):
2876 error_text = "Exception at database"
2877 else:
2878 error_text = "Exception at VIM"
2879 error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
2880 #logger.error("start_scenario %s", error_text)
2881 raise NfvoException(error_text, e.http_code)
2882
2883 def unify_cloud_config(cloud_config_preserve, cloud_config):
2884 """ join the cloud config information into cloud_config_preserve.
2885 In case of conflict cloud_config_preserve preserves
2886 None is allowed
2887 """
2888 if not cloud_config_preserve and not cloud_config:
2889 return None
2890
2891 new_cloud_config = {"key-pairs":[], "users":[]}
2892 # key-pairs
2893 if cloud_config_preserve:
2894 for key in cloud_config_preserve.get("key-pairs", () ):
2895 if key not in new_cloud_config["key-pairs"]:
2896 new_cloud_config["key-pairs"].append(key)
2897 if cloud_config:
2898 for key in cloud_config.get("key-pairs", () ):
2899 if key not in new_cloud_config["key-pairs"]:
2900 new_cloud_config["key-pairs"].append(key)
2901 if not new_cloud_config["key-pairs"]:
2902 del new_cloud_config["key-pairs"]
2903
2904 # users
2905 if cloud_config:
2906 new_cloud_config["users"] += cloud_config.get("users", () )
2907 if cloud_config_preserve:
2908 new_cloud_config["users"] += cloud_config_preserve.get("users", () )
2909 index_to_delete = []
2910 users = new_cloud_config.get("users", [])
2911 for index0 in range(0,len(users)):
2912 if index0 in index_to_delete:
2913 continue
2914 for index1 in range(index0+1,len(users)):
2915 if index1 in index_to_delete:
2916 continue
2917 if users[index0]["name"] == users[index1]["name"]:
2918 index_to_delete.append(index1)
2919 for key in users[index1].get("key-pairs",()):
2920 if "key-pairs" not in users[index0]:
2921 users[index0]["key-pairs"] = [key]
2922 elif key not in users[index0]["key-pairs"]:
2923 users[index0]["key-pairs"].append(key)
2924 index_to_delete.sort(reverse=True)
2925 for index in index_to_delete:
2926 del users[index]
2927 if not new_cloud_config["users"]:
2928 del new_cloud_config["users"]
2929
2930 #boot-data-drive
2931 if cloud_config and cloud_config.get("boot-data-drive") != None:
2932 new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
2933 if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
2934 new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
2935
2936 # user-data
2937 new_cloud_config["user-data"] = []
2938 if cloud_config and cloud_config.get("user-data"):
2939 if isinstance(cloud_config["user-data"], list):
2940 new_cloud_config["user-data"] += cloud_config["user-data"]
2941 else:
2942 new_cloud_config["user-data"].append(cloud_config["user-data"])
2943 if cloud_config_preserve and cloud_config_preserve.get("user-data"):
2944 if isinstance(cloud_config_preserve["user-data"], list):
2945 new_cloud_config["user-data"] += cloud_config_preserve["user-data"]
2946 else:
2947 new_cloud_config["user-data"].append(cloud_config_preserve["user-data"])
2948 if not new_cloud_config["user-data"]:
2949 del new_cloud_config["user-data"]
2950
2951 # config files
2952 new_cloud_config["config-files"] = []
2953 if cloud_config and cloud_config.get("config-files") != None:
2954 new_cloud_config["config-files"] += cloud_config["config-files"]
2955 if cloud_config_preserve:
2956 for file in cloud_config_preserve.get("config-files", ()):
2957 for index in range(0, len(new_cloud_config["config-files"])):
2958 if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
2959 new_cloud_config["config-files"][index] = file
2960 break
2961 else:
2962 new_cloud_config["config-files"].append(file)
2963 if not new_cloud_config["config-files"]:
2964 del new_cloud_config["config-files"]
2965 return new_cloud_config
2966
2967
2968 def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
2969 datacenter_id = None
2970 datacenter_name = None
2971 thread = None
2972 try:
2973 if datacenter_tenant_id:
2974 thread_id = datacenter_tenant_id
2975 thread = vim_threads["running"].get(datacenter_tenant_id)
2976 if not thread:
2977 where_={"td.nfvo_tenant_id": tenant_id}
2978 if datacenter_id_name:
2979 if utils.check_valid_uuid(datacenter_id_name):
2980 datacenter_id = datacenter_id_name
2981 where_["dt.datacenter_id"] = datacenter_id
2982 else:
2983 datacenter_name = datacenter_id_name
2984 where_["d.name"] = datacenter_name
2985 if datacenter_tenant_id:
2986 where_["dt.uuid"] = datacenter_tenant_id
2987 datacenters = mydb.get_rows(
2988 SELECT=("dt.uuid as datacenter_tenant_id, d.name as datacenter_name",),
2989 FROM="datacenter_tenan