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