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