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