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