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