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