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