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