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