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