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