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