1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
27 __author__
="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28 __date__
="$16-sep-2014 22:05:01$"
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
42 from uuid
import uuid4
43 from db_base
import db_base_Exception
46 from threading
import Lock
48 from lib_osm_openvim
import ovim
as ovim_module
49 from lib_osm_openvim
.ovim
import ovimException
50 from Crypto
.PublicKey
import RSA
52 import osm_im
.vnfd
as vnfd_catalog
53 import osm_im
.nsd
as nsd_catalog
54 from pyangbind
.lib
.serialise
import pybindJSONDecoder
55 from copy
import deepcopy
59 global vimconn_imported
61 global default_volume_size
62 default_volume_size
= '5' #size in GB
67 vimconn_imported
= {} # dictionary with VIM type as key, loaded module as value
68 vim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
69 vim_persistent_info
= {}
70 logger
= logging
.getLogger('openmano.nfvo')
76 class NfvoException(Exception):
77 def __init__(self
, message
, http_code
):
78 self
.http_code
= http_code
79 Exception.__init
__(self
, message
)
85 if task_id
<= last_task_id
:
86 task_id
= last_task_id
+ 0.000001
87 last_task_id
= task_id
88 return "ACTION-{:.6f}".format(task_id
)
89 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
92 def new_task(name
, params
, depends
=None):
94 task_id
= get_task_id()
95 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
97 task
["depends"] = depends
102 return True if id[:5] == "TASK-" else False
105 def get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_name
, tenant_id
):
106 name
= datacenter_name
[:16]
107 if name
not in vim_threads
["names"]:
108 vim_threads
["names"].append(name
)
110 name
= datacenter_name
[:16] + "." + tenant_name
[:16]
111 if name
not in vim_threads
["names"]:
112 vim_threads
["names"].append(name
)
114 name
= datacenter_id
+ "-" + tenant_id
115 vim_threads
["names"].append(name
)
119 def start_service(mydb
):
120 global db
, global_config
121 db
= nfvo_db
.nfvo_db()
122 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
125 # Initialize openvim for SDN control
126 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
127 # TODO: review ovim.py to delete not needed configuration
128 ovim_configuration
= {
129 'logger_name': 'openmano.ovim',
130 'network_vlan_range_start': 1000,
131 'network_vlan_range_end': 4096,
132 'db_name': global_config
["db_ovim_name"],
133 'db_host': global_config
["db_ovim_host"],
134 'db_user': global_config
["db_ovim_user"],
135 'db_passwd': global_config
["db_ovim_passwd"],
138 'network_type': 'bridge',
139 #TODO: log_level_of should not be needed. To be modified in ovim
140 'log_level_of': 'DEBUG'
143 # starts ovim library
144 ovim
= ovim_module
.ovim(ovim_configuration
)
147 #delete old unneeded vim_actions
151 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
152 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
153 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
154 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
155 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
156 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
157 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
159 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
160 'datacenter_id': vim
.get('datacenter_id')}
162 extra
.update(yaml
.load(vim
["config"]))
163 if vim
.get('dt_config'):
164 extra
.update(yaml
.load(vim
["dt_config"]))
165 if vim
["type"] not in vimconn_imported
:
168 module
= "vimconn_" + vim
["type"]
169 pkg
= __import__("osm_ro." + module
)
170 vim_conn
= getattr(pkg
, module
)
171 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
172 # vim_conn = imp.load_module(vim["type"], *module_info)
173 vimconn_imported
[vim
["type"]] = vim_conn
174 except (IOError, ImportError) as e
:
175 # if module_info and module_info[0]:
176 # file.close(module_info[0])
177 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
178 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
180 thread_id
= vim
['datacenter_tenant_id']
181 vim_persistent_info
[thread_id
] = {}
184 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
185 myvim
= vimconn_imported
[ vim
["type"] ].vimconnector(
186 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
187 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
188 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
189 user
=vim
['user'], passwd
=vim
['passwd'],
190 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
192 except vimconn
.vimconnException
as e
:
194 logger
.error("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
195 vim
['datacenter_id'], e
))
196 except Exception as e
:
197 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, e
),
198 HTTP_Internal_Server_Error
)
199 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['vim_tenant_id'], vim
['vim_tenant_name'],
200 vim
['vim_tenant_id'])
201 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, vim
['datacenter_name'],
202 vim
['datacenter_tenant_id'], db
=db
, db_lock
=db_lock
, ovim
=ovim
)
204 vim_threads
["running"][thread_id
] = new_thread
205 except db_base_Exception
as e
:
206 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
207 except ovim_module
.ovimException
as e
:
209 if message
[:22] == "DATABASE wrong version":
210 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
211 "at host {dbhost}".format(
212 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
213 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
214 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
215 raise NfvoException(message
, HTTP_Bad_Request
)
219 global ovim
, global_config
222 for thread_id
,thread
in vim_threads
["running"].items():
223 thread
.insert_task("exit")
224 vim_threads
["deleting"][thread_id
] = thread
225 vim_threads
["running"] = {}
226 if global_config
and global_config
.get("console_thread"):
227 for thread
in global_config
["console_thread"]:
228 thread
.terminate
= True
231 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
232 global_config
["version_date"] ))
236 Clean unused or old entries at database to avoid unlimited growing
237 :param mydb: database connector
240 # get and delete unused vim_actions: all elements deleted, one week before, instance not present
241 now
= t
.time()-3600*24*7
242 instance_action_id
= None
245 actions_to_delete
= mydb
.get_rows(
246 SELECT
=("item", "item_id", "instance_action_id"),
247 FROM
="vim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
248 "left join instance_scenarios as i on ia.instance_id=i.uuid",
249 WHERE
={"va.action": "DELETE", "va.modified_at<": now
, "i.uuid": None,
250 "va.status": ("DONE", "SUPERSEDED")},
253 for to_delete
in actions_to_delete
:
254 mydb
.delete_row(FROM
="vim_actions", WHERE
=to_delete
)
255 if instance_action_id
!= to_delete
["instance_action_id"]:
256 instance_action_id
= to_delete
["instance_action_id"]
257 mydb
.delete_row(FROM
="instance_actions", WHERE
={"uuid": instance_action_id
})
258 nb_deleted
+= len(actions_to_delete
)
259 if len(actions_to_delete
) < 100:
262 logger
.debug("Removed {} unused vim_actions".format(nb_deleted
))
266 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
268 return result, content:
269 <0, error_text upon error
270 nb_records, flavor_list on success
273 WHERE_dict
['vnf_id'] = vnf_id
274 if nfvo_tenant
is not None:
275 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
277 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
278 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
279 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
280 #print "get_flavor_list result:", result
281 #print "get_flavor_list content:", content
283 for flavor
in flavors
:
284 flavorList
.append(flavor
['flavor_id'])
288 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
290 Get used images of all vms belonging to this VNFD
291 :param mydb: database conector
292 :param vnf_id: vnfd uuid
293 :param nfvo_tenant: tenant, not used
294 :return: The list of image uuid used
297 vms
= mydb
.get_rows(SELECT
=('image_id','image_list'), FROM
='vms', WHERE
={'vnf_id': vnf_id
})
299 if vm
["image_id"] not in image_list
:
300 image_list
.append(vm
["image_id"])
302 vm_image_list
= yaml
.load(vm
["image_list"])
303 for image_dict
in vm_image_list
:
304 if image_dict
["image_id"] not in image_list
:
305 image_list
.append(image_dict
["image_id"])
309 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
310 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None, ignore_errors
=False):
311 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
312 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
313 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
314 raise exception upon error
317 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
318 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
319 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
320 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
321 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
322 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
323 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
324 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
325 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
326 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
327 'user','passwd', 'dt.config as dt_config')
329 from_
= 'datacenters as d'
330 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
332 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
335 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
336 'datacenter_id': vim
.get('datacenter_id'),
337 '_vim_type_internal': vim
.get('type')}
339 extra
.update(yaml
.load(vim
["config"]))
340 if vim
.get('dt_config'):
341 extra
.update(yaml
.load(vim
["dt_config"]))
342 if vim
["type"] not in vimconn_imported
:
345 module
= "vimconn_" + vim
["type"]
346 pkg
= __import__("osm_ro." + module
)
347 vim_conn
= getattr(pkg
, module
)
348 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
349 # vim_conn = imp.load_module(vim["type"], *module_info)
350 vimconn_imported
[vim
["type"]] = vim_conn
351 except (IOError, ImportError) as e
:
352 # if module_info and module_info[0]:
353 # file.close(module_info[0])
355 logger
.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
356 vim
["type"], module
, type(e
).__name
__, str(e
)))
358 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
359 vim
["type"], module
, type(e
).__name
__, str(e
)), HTTP_Bad_Request
)
362 if 'datacenter_tenant_id' in vim
:
363 thread_id
= vim
["datacenter_tenant_id"]
364 if thread_id
not in vim_persistent_info
:
365 vim_persistent_info
[thread_id
] = {}
366 persistent_info
= vim_persistent_info
[thread_id
]
370 # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
371 vim_dict
[ vim
['datacenter_id'] ] = vimconn_imported
[ vim
["type"] ].vimconnector(
372 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
373 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
374 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
375 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
376 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
377 config
=extra
, persistent_info
=persistent_info
379 except Exception as e
:
381 logger
.error("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)))
383 http_code
= HTTP_Internal_Server_Error
384 if isinstance(e
, vimconn
.vimconnException
):
385 http_code
= e
.http_code
386 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), http_code
)
388 except db_base_Exception
as e
:
389 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
392 def rollback(mydb
, vims
, rollback_list
):
394 #delete things by reverse order
395 for i
in range(len(rollback_list
)-1, -1, -1):
396 item
= rollback_list
[i
]
397 if item
["where"]=="vim":
398 if item
["vim_id"] not in vims
:
400 if is_task_id(item
["uuid"]):
402 vim
= vims
[item
["vim_id"]]
404 if item
["what"]=="image":
405 vim
.delete_image(item
["uuid"])
406 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
407 elif item
["what"]=="flavor":
408 vim
.delete_flavor(item
["uuid"])
409 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
410 elif item
["what"]=="network":
411 vim
.delete_network(item
["uuid"])
412 elif item
["what"]=="vm":
413 vim
.delete_vminstance(item
["uuid"])
414 except vimconn
.vimconnException
as e
:
415 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
416 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
417 except db_base_Exception
as e
:
418 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
422 if item
["what"]=="image":
423 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
424 elif item
["what"]=="flavor":
425 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
426 except db_base_Exception
as e
:
427 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
428 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
429 if len(undeleted_items
)==0:
430 return True," Rollback successful."
432 return False," Rollback fails to delete: " + str(undeleted_items
)
435 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
437 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
439 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
441 #dataplane interfaces
442 for numa
in vnfc
.get("numas",() ):
443 for interface
in numa
.get("interfaces",()):
444 if interface
["name"] in name_dict
:
446 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
447 vnfc
["name"], interface
["name"]),
449 name_dict
[ interface
["name"] ] = "underlay"
451 for interface
in vnfc
.get("bridge-ifaces",() ):
452 if interface
["name"] in name_dict
:
454 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
455 vnfc
["name"], interface
["name"]),
457 name_dict
[ interface
["name"] ] = "overlay"
458 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
459 # check bood-data info
460 # if "boot-data" in vnfc:
461 # # check that user-data is incompatible with users and config-files
462 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
463 # raise NfvoException(
464 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
467 #check if the info in external_connections matches with the one in the vnfcs
469 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
470 if external_connection
["name"] in name_list
:
472 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
473 external_connection
["name"]),
475 name_list
.append(external_connection
["name"])
476 if external_connection
["VNFC"] not in vnfc_interfaces
:
478 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
479 external_connection
["name"], external_connection
["VNFC"]),
482 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
484 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
485 external_connection
["name"],
486 external_connection
["local_iface_name"]),
489 #check if the info in internal_connections matches with the one in the vnfcs
491 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
492 if internal_connection
["name"] in name_list
:
494 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
495 internal_connection
["name"]),
497 name_list
.append(internal_connection
["name"])
498 #We should check that internal-connections of type "ptp" have only 2 elements
500 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
502 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
503 internal_connection
["name"],
504 'ptp' if vnf_descriptor_version
==1 else 'e-line',
505 'data' if vnf_descriptor_version
==1 else "e-lan"),
507 for port
in internal_connection
["elements"]:
509 iface
= port
["local_iface_name"]
510 if vnf
not in vnfc_interfaces
:
512 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
513 internal_connection
["name"], vnf
),
515 if iface
not in vnfc_interfaces
[ vnf
]:
517 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
518 internal_connection
["name"], iface
),
520 return -HTTP_Bad_Request
,
521 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
522 if vnfc_interfaces
[vnf
][iface
] == "overlay":
523 internal_connection
["type"] = "bridge"
525 internal_connection
["type"] = "data"
526 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
527 if vnfc_interfaces
[vnf
][iface
] == "overlay":
528 internal_connection
["implementation"] = "overlay"
530 internal_connection
["implementation"] = "underlay"
531 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
532 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
534 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
535 internal_connection
["name"],
536 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
537 'data' if vnf_descriptor_version
==1 else 'underlay'),
539 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
540 vnfc_interfaces
[vnf
][iface
] == "underlay":
542 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
543 internal_connection
["name"], iface
,
544 'data' if vnf_descriptor_version
==1 else 'underlay',
545 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
549 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
551 if only_create_at_vim
:
552 image_mano_id
= image_dict
['uuid']
553 if return_on_error
== None:
554 return_on_error
= True
556 if image_dict
['location']:
557 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
559 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
561 image_mano_id
= images
[0]['uuid']
563 #create image in MANO DB
564 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
565 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
566 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
568 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
569 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
570 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
571 #create image at every vim
572 for vim_id
,vim
in vims
.iteritems():
573 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
574 image_created
="false"
576 image_db
= mydb
.get_rows(FROM
="datacenters_images",
577 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
578 #look at VIM if this image exist
580 if image_dict
['location'] is not None:
581 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
584 filter_dict
['name'] = image_dict
['universal_name']
585 if image_dict
.get('checksum') != None:
586 filter_dict
['checksum'] = image_dict
['checksum']
587 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
588 vim_images
= vim
.get_image_list(filter_dict
)
589 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
590 if len(vim_images
) > 1:
591 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), HTTP_Conflict
)
592 elif len(vim_images
) == 0:
593 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
595 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
596 image_vim_id
= vim_images
[0]['id']
598 except vimconn
.vimconnNotFoundException
as e
:
599 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
601 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
602 if image_dict
['location']:
603 image_vim_id
= vim
.new_image(image_dict
)
604 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
607 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
608 raise vimconn
.vimconnException(str(e
))
609 except vimconn
.vimconnException
as e
:
611 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
614 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
616 except vimconn
.vimconnException
as e
:
618 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
620 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
623 #if we reach here, the image has been created or existed
625 #add new vim_id at datacenters_images
626 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
627 'image_id':image_mano_id
,
628 'vim_id': image_vim_id
,
629 'created':image_created
})
630 elif image_db
[0]["vim_id"]!=image_vim_id
:
631 #modify existing vim_id at datacenters_images
632 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
634 return image_vim_id
if only_create_at_vim
else image_mano_id
637 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
638 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',0),
639 'ram':flavor_dict
.get('ram'),
640 'vcpus':flavor_dict
.get('vcpus'),
642 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
643 del flavor_dict
['extended']
644 if 'extended' in flavor_dict
:
645 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
647 #look if flavor exist
648 if only_create_at_vim
:
649 flavor_mano_id
= flavor_dict
['uuid']
650 if return_on_error
== None:
651 return_on_error
= True
653 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
655 flavor_mano_id
= flavors
[0]['uuid']
658 #create one by one the images of aditional disks
659 dev_image_list
=[] #list of images
660 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
662 for device
in flavor_dict
['extended'].get('devices',[]):
663 if "image" not in device
and "image name" not in device
:
666 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
667 image_dict
['universal_name']=device
.get('image name')
668 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
669 image_dict
['location']=device
.get('image')
670 #image_dict['new_location']=vnfc.get('image location')
671 image_dict
['checksum']=device
.get('image checksum')
672 image_metadata_dict
= device
.get('image metadata', None)
673 image_metadata_str
= None
674 if image_metadata_dict
!= None:
675 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
676 image_dict
['metadata']=image_metadata_str
677 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
678 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
679 dev_image_list
.append(image_id
)
681 temp_flavor_dict
['name'] = flavor_dict
['name']
682 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
683 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
684 flavor_mano_id
= content
685 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
686 #create flavor at every vim
687 if 'uuid' in flavor_dict
:
688 del flavor_dict
['uuid']
690 for vim_id
,vim
in vims
.items():
691 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
692 flavor_created
="false"
694 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
695 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
696 #look at VIM if this flavor exist SKIPPED
697 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
699 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
703 # Create the flavor in VIM
704 # Translate images at devices from MANO id to VIM id
706 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
707 # make a copy of original devices
710 for device
in flavor_dict
["extended"].get("devices",[]):
713 devices_original
.append(dev
)
714 if 'image' in device
:
716 if 'image metadata' in device
:
717 del device
['image metadata']
718 if 'image checksum' in device
:
719 del device
['image checksum']
721 for index
in range(0,len(devices_original
)) :
722 device
=devices_original
[index
]
723 if "image" not in device
and "image name" not in device
:
725 disk_list
.append({'size': device
.get('size', default_volume_size
), 'name': device
.get('name')})
728 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
729 image_dict
['universal_name']=device
.get('image name')
730 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
731 image_dict
['location']=device
.get('image')
732 # image_dict['new_location']=device.get('image location')
733 image_dict
['checksum']=device
.get('image checksum')
734 image_metadata_dict
= device
.get('image metadata', None)
735 image_metadata_str
= None
736 if image_metadata_dict
!= None:
737 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
738 image_dict
['metadata']=image_metadata_str
739 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
740 image_dict
["uuid"]=image_mano_id
741 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
743 #save disk information (image must be based on and size
744 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
746 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
749 #check that this vim_id exist in VIM, if not create
750 flavor_vim_id
=flavor_db
[0]["vim_id"]
752 vim
.get_flavor(flavor_vim_id
)
753 continue #flavor exist
754 except vimconn
.vimconnException
:
756 #create flavor at vim
757 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
760 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
761 flavor_create
="false"
762 except vimconn
.vimconnException
as e
:
765 if not flavor_vim_id
:
766 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
767 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
768 flavor_created
="true"
769 except vimconn
.vimconnException
as e
:
771 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
773 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
776 #if reach here the flavor has been create or exist
777 if len(flavor_db
)==0:
778 #add new vim_id at datacenters_flavors
779 extended_devices_yaml
= None
780 if len(disk_list
) > 0:
781 extended_devices
= dict()
782 extended_devices
['disks'] = disk_list
783 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
784 mydb
.new_row('datacenters_flavors',
785 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
786 'created': flavor_created
, 'extended': extended_devices_yaml
})
787 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
788 #modify existing vim_id at datacenters_flavors
789 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
790 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
792 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
795 def get_str(obj
, field
, length
):
797 Obtain the str value,
802 value
= obj
.get(field
)
803 if value
is not None:
804 value
= str(value
)[:length
]
807 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
809 fill image content at db_image dictionary. Check if the image with this image and checksum exist
810 :param db_image: dictionary to insert data
811 :param mydb: database connector
812 :param descriptor: yang descriptor
813 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
816 db_image
["name"] = get_str(descriptor
, "image", 255)
817 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
818 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
819 db_image
["checksum"] = None
820 if db_image
["name"].startswith("/"):
821 db_image
["location"] = db_image
["name"]
822 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
824 db_image
["universal_name"] = db_image
["name"]
825 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
826 'checksum': db_image
['checksum']})
828 return existing_images
[0]["uuid"]
830 image_uuid
= str(uuid4())
831 db_image
["uuid"] = image_uuid
834 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
836 Parses an OSM IM vnfd_catalog and insert at DB
839 :param vnf_descriptor:
840 :return: The list of cretated vnf ids
843 myvnfd
= vnfd_catalog
.vnfd()
845 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
, path_helper
=True)
846 except Exception as e
:
847 raise NfvoException("Error. Invalid VNF descriptor format " + str(e
), HTTP_Bad_Request
)
855 db_ip_profiles_index
= 0
859 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd:vnfd-catalog")
860 if not vnfd_catalog_descriptor
:
861 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd-catalog")
862 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd")
863 if not vnfd_descriptor_list
:
864 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd:vnfd")
865 for vnfd_yang
in myvnfd
.vnfd_catalog
.vnfd
.itervalues():
866 vnfd
= vnfd_yang
.get()
869 vnf_uuid
= str(uuid4())
870 uuid_list
.append(vnf_uuid
)
871 vnfd_uuid_list
.append(vnf_uuid
)
872 vnfd_id
= get_str(vnfd
, "id", 255)
876 "name": get_str(vnfd
, "name", 255),
877 "description": get_str(vnfd
, "description", 255),
878 "tenant_id": tenant_id
,
879 "vendor": get_str(vnfd
, "vendor", 255),
880 "short_name": get_str(vnfd
, "short-name", 255),
881 "descriptor": str(vnf_descriptor
)[:60000]
884 for vnfd_descriptor
in vnfd_descriptor_list
:
885 if vnfd_descriptor
["id"] == str(vnfd
["id"]):
888 # table ip_profiles (ip-profiles)
889 ip_profile_name2db_table_index
= {}
890 for ip_profile
in vnfd
.get("ip-profiles").itervalues():
892 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
893 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
894 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
895 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
896 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
897 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
900 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
901 dns_list
.append(str(dns
.get("address")))
902 db_ip_profile
["dns_address"] = ";".join(dns_list
)
903 if ip_profile
["ip-profile-params"].get('security-group'):
904 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
905 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
906 db_ip_profiles_index
+= 1
907 db_ip_profiles
.append(db_ip_profile
)
909 # table nets (internal-vld)
910 net_id2uuid
= {} # for mapping interface with network
911 for vld
in vnfd
.get("internal-vld").itervalues():
912 net_uuid
= str(uuid4())
913 uuid_list
.append(net_uuid
)
915 "name": get_str(vld
, "name", 255),
918 "description": get_str(vld
, "description", 255),
919 "osm_id": get_str(vld
, "id", 255),
920 "type": "bridge", # TODO adjust depending on connection point type
922 net_id2uuid
[vld
.get("id")] = net_uuid
923 db_nets
.append(db_net
)
924 # ip-profile, link db_ip_profile with db_sce_net
925 if vld
.get("ip-profile-ref"):
926 ip_profile_name
= vld
.get("ip-profile-ref")
927 if ip_profile_name
not in ip_profile_name2db_table_index
:
928 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
929 "'{}'. Reference to a non-existing 'ip_profiles'".format(
930 str(vnfd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
932 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["net_id"] = net_uuid
933 else: #check no ip-address has been defined
934 for icp
in vld
.get("internal-connection-point").itervalues():
935 if icp
.get("ip-address"):
936 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
937 "contains an ip-address but no ip-profile has been defined at VLD".format(
938 str(vnfd
["id"]), str(vld
["id"]), str(icp
["id"])),
941 # connection points vaiable declaration
942 cp_name2iface_uuid
= {}
944 cp_name2db_interface
= {}
948 vdu_id2db_table_index
= {}
949 for vdu
in vnfd
.get("vdu").itervalues():
951 for vdu_descriptor
in vnfd_descriptor
["vdu"]:
952 if vdu_descriptor
["id"] == str(vdu
["id"]):
954 vm_uuid
= str(uuid4())
955 uuid_list
.append(vm_uuid
)
956 vdu_id
= get_str(vdu
, "id", 255)
960 "name": get_str(vdu
, "name", 255),
961 "description": get_str(vdu
, "description", 255),
964 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
965 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
967 db_vm
["count"] = int(vdu
["count"])
970 image_present
= False
974 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
976 image_uuid
= db_image
["uuid"]
977 db_images
.append(db_image
)
978 db_vm
["image_id"] = image_uuid
979 if vdu
.get("alternative-images"):
980 vm_alternative_images
= []
981 for alt_image
in vdu
.get("alternative-images").itervalues():
983 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, alt_image
)
985 image_uuid
= db_image
["uuid"]
986 db_images
.append(db_image
)
987 vm_alternative_images
.append({
988 "image_id": image_uuid
,
989 "vim_type": str(alt_image
["vim-type"]),
990 # "universal_name": str(alt_image["image"]),
991 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
994 db_vm
["image_list"] = yaml
.safe_dump(vm_alternative_images
, default_flow_style
=True, width
=256)
998 if vdu
.get("volumes"):
999 for volume_key
in vdu
["volumes"]:
1000 volume
= vdu
["volumes"][volume_key
]
1001 if not image_present
:
1002 # Convert the first volume to vnfc.image
1003 image_present
= True
1005 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
1007 image_uuid
= db_image
["uuid"]
1008 db_images
.append(db_image
)
1009 db_vm
["image_id"] = image_uuid
1011 # Add Openmano devices
1012 device
= {"name": str(volume
.get("name"))}
1013 device
["type"] = str(volume
.get("device-type"))
1014 if volume
.get("size"):
1015 device
["size"] = int(volume
["size"])
1016 if volume
.get("image"):
1017 device
["image name"] = str(volume
["image"])
1018 if volume
.get("image-checksum"):
1019 device
["image checksum"] = str(volume
["image-checksum"])
1021 devices
.append(device
)
1025 if vdu
.get("cloud-init"):
1026 boot_data
["user-data"] = str(vdu
["cloud-init"])
1027 elif vdu
.get("cloud-init-file"):
1028 # TODO Where this file content is present???
1029 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1030 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
1032 if vdu
.get("supplemental-boot-data"):
1033 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
1034 boot_data
['boot-data-drive'] = True
1035 if vdu
["supplemental-boot-data"].get('config-file'):
1036 om_cfgfile_list
= list()
1037 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].itervalues():
1038 # TODO Where this file content is present???
1039 cfg_source
= str(custom_config_file
["source"])
1040 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
1041 "content": cfg_source
})
1042 boot_data
['config-files'] = om_cfgfile_list
1044 db_vm
["boot_data"] = yaml
.safe_dump(boot_data
, default_flow_style
=True, width
=256)
1046 db_vms
.append(db_vm
)
1049 # table interfaces (internal/external interfaces)
1050 flavor_epa_interfaces
= []
1051 vdu_id2cp_name
= {} # stored only when one external connection point is presented at this VDU
1052 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1053 for iface
in vdu
.get("interface").itervalues():
1054 flavor_epa_interface
= {}
1055 iface_uuid
= str(uuid4())
1056 uuid_list
.append(iface_uuid
)
1059 "internal_name": get_str(iface
, "name", 255),
1062 flavor_epa_interface
["name"] = db_interface
["internal_name"]
1063 if iface
.get("virtual-interface").get("vpci"):
1064 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
1065 flavor_epa_interface
["vpci"] = db_interface
["vpci"]
1067 if iface
.get("virtual-interface").get("bandwidth"):
1068 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1069 db_interface
["bw"] = int(math
.ceil(bps
/1000000.0))
1070 flavor_epa_interface
["bandwidth"] = "{} Mbps".format(db_interface
["bw"])
1072 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1073 db_interface
["type"] = "mgmt"
1074 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
1075 db_interface
["type"] = "bridge"
1076 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1077 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1078 db_interface
["type"] = "data"
1079 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1080 flavor_epa_interface
["dedicated"] = "no" if iface
["virtual-interface"]["type"] == "SR-IOV" \
1082 flavor_epa_interfaces
.append(flavor_epa_interface
)
1084 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1085 "-interface':'type':'{}'. Interface type is not supported".format(
1086 vnfd_id
, vdu_id
, iface
.get("virtual-interface").get("type")),
1089 if iface
.get("external-connection-point-ref"):
1091 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1092 db_interface
["external_name"] = get_str(cp
, "name", 255)
1093 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1094 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1095 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1096 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1097 if cp_descriptor
["name"] == db_interface
["external_name"]:
1102 if vdu_id
in vdu_id2cp_name
:
1103 vdu_id2cp_name
[vdu_id
] = None # more than two connecdtion point for this VDU
1105 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1108 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1109 db_interface
["port_security"] = 0
1110 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1111 db_interface
["port_security"] = 1
1113 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1114 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1115 " at connection-point".format(
1116 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1117 cp
=iface
.get("vnfd-connection-point-ref")),
1119 elif iface
.get("internal-connection-point-ref"):
1121 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1122 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1125 raise KeyError("does not exist at vdu:internal-connection-point")
1128 for vld
in vnfd
.get("internal-vld").itervalues():
1129 for cp
in vld
.get("internal-connection-point").itervalues():
1130 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1132 raise KeyError("is referenced by more than one 'internal-vld'")
1136 raise KeyError("is not referenced by any 'internal-vld'")
1138 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1139 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1140 db_interface
["port_security"] = 0
1141 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1142 db_interface
["port_security"] = 1
1143 if icp
.get("ip-address"):
1144 if not icp_vld
.get("ip-profile-ref"):
1146 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1147 except KeyError as e
:
1148 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1149 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1151 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1152 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1154 if iface
.get("position"):
1155 db_interface
["created_at"] = int(iface
.get("position")) * 50
1156 if iface
.get("mac-address"):
1157 db_interface
["mac"] = str(iface
.get("mac-address"))
1158 db_interfaces
.append(db_interface
)
1162 "name": get_str(vdu
, "name", 250) + "-flv",
1163 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1164 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1165 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1167 # TODO revise the case of several numa-node-policy node
1171 extended
["devices"] = devices
1172 if flavor_epa_interfaces
:
1173 numa
["interfaces"] = flavor_epa_interfaces
1174 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1175 epa_vcpu_set
= False
1176 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1177 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1178 if numa_node_policy
.get("node"):
1179 numa_node
= numa_node_policy
["node"].values()[0]
1180 if numa_node
.get("num-cores"):
1181 numa
["cores"] = numa_node
["num-cores"]
1183 if numa_node
.get("paired-threads"):
1184 if numa_node
["paired-threads"].get("num-paired-threads"):
1185 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1187 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1188 numa
["paired-threads-id"] = []
1189 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues():
1190 numa
["paired-threads-id"].append(
1191 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1193 if numa_node
.get("num-threads"):
1194 numa
["threads"] = int(numa_node
["num-threads"])
1196 if numa_node
.get("memory-mb"):
1197 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1198 if vdu
["guest-epa"].get("mempage-size"):
1199 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1200 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1201 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1202 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1203 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1204 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1205 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1207 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1209 extended
["numas"] = [numa
]
1211 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1212 db_flavor
["extended"] = extended_text
1213 # look if flavor exist
1214 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1215 'ram': db_flavor
.get('ram'),
1216 'vcpus': db_flavor
.get('vcpus'),
1217 'extended': db_flavor
.get('extended')
1219 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1220 if existing_flavors
:
1221 flavor_uuid
= existing_flavors
[0]["uuid"]
1223 flavor_uuid
= str(uuid4())
1224 uuid_list
.append(flavor_uuid
)
1225 db_flavor
["uuid"] = flavor_uuid
1226 db_flavors
.append(db_flavor
)
1227 db_vm
["flavor_id"] = flavor_uuid
1229 # VNF affinity and antiaffinity
1230 for pg
in vnfd
.get("placement-groups").itervalues():
1231 pg_name
= get_str(pg
, "name", 255)
1232 for vdu
in pg
.get("member-vdus").itervalues():
1233 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1234 if vdu_id
not in vdu_id2db_table_index
:
1235 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1236 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1237 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1239 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1240 # TODO consider the case of isolation and not colocation
1241 # if pg.get("strategy") == "ISOLATION":
1243 # VNF mgmt configuration
1245 if vnfd
["mgmt-interface"].get("vdu-id"):
1246 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1247 if mgmt_vdu_id
not in vdu_id2uuid
:
1248 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1249 "'{vdu}'. Reference to a non-existing vdu".format(
1250 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1252 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1253 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1254 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1255 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1257 if vnfd
["mgmt-interface"].get("ip-address"):
1258 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1259 if vnfd
["mgmt-interface"].get("cp"):
1260 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1261 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
1262 "Reference to a non-existing connection-point".format(
1263 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1265 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1266 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1267 # mark this interface as of type mgmt
1268 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1270 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1274 mgmt_access
["default_user"] = default_user
1275 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1278 mgmt_access
["required"] = required
1281 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1283 db_vnfs
.append(db_vnf
)
1287 {"images": db_images
},
1288 {"flavors": db_flavors
},
1289 {"ip_profiles": db_ip_profiles
},
1291 {"interfaces": db_interfaces
},
1294 logger
.debug("create_vnf Deployment done vnfDict: %s",
1295 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1296 mydb
.new_rows(db_tables
, uuid_list
)
1297 return vnfd_uuid_list
1298 except NfvoException
:
1300 except Exception as e
:
1301 logger
.error("Exception {}".format(e
))
1302 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1305 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1306 global global_config
1308 # Step 1. Check the VNF descriptor
1309 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1310 # Step 2. Check tenant exist
1312 if tenant_id
!= "any":
1313 check_tenant(mydb
, tenant_id
)
1314 if "tenant_id" in vnf_descriptor
["vnf"]:
1315 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1316 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1319 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1320 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1321 if global_config
["auto_push_VNF_to_VIMs"]:
1322 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1324 # Step 4. Review the descriptor and add missing fields
1325 #print vnf_descriptor
1326 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1327 vnf_name
= vnf_descriptor
['vnf']['name']
1328 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1329 if "physical" in vnf_descriptor
['vnf']:
1330 del vnf_descriptor
['vnf']['physical']
1331 #print vnf_descriptor
1333 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1334 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1335 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1337 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1338 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1339 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1341 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1342 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1344 VNFCitem
["name"] = vnfc
['name']
1345 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1346 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1348 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1351 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1352 myflavorDict
["description"] = VNFCitem
["description"]
1353 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1354 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1355 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1356 myflavorDict
["extended"] = {}
1358 devices
= vnfc
.get("devices")
1360 myflavorDict
["extended"]["devices"] = devices
1363 # 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
1364 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1366 # Previous code has been commented
1367 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1368 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1369 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1370 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1372 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1374 # print "Error creating flavor: unknown processor model. Rollback successful."
1375 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1377 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1378 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1380 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1381 myflavorDict
['extended']['numas'] = vnfc
['numas']
1385 # Step 6.2 New flavors are created in the VIM
1386 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1388 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1389 VNFCitem
["flavor_id"] = flavor_id
1390 VNFCDict
[vnfc
['name']] = VNFCitem
1392 logger
.debug("Creating new images in the VIM for each VNFC")
1393 # Step 6.3 New images are created in the VIM
1394 #For each VNFC, we must create the appropriate image.
1395 #This "for" loop might be integrated with the previous one
1396 #In case this integration is made, the VNFCDict might become a VNFClist.
1397 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1398 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1400 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1401 image_dict
['universal_name']=vnfc
.get('image name')
1402 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1403 image_dict
['location']=vnfc
.get('VNFC image')
1404 #image_dict['new_location']=vnfc.get('image location')
1405 image_dict
['checksum']=vnfc
.get('image checksum')
1406 image_metadata_dict
= vnfc
.get('image metadata', None)
1407 image_metadata_str
= None
1408 if image_metadata_dict
is not None:
1409 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1410 image_dict
['metadata']=image_metadata_str
1411 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1412 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1413 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1414 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1415 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1416 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1417 if vnfc
.get("boot-data"):
1418 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1421 # Step 7. Storing the VNF descriptor in the repository
1422 if "descriptor" not in vnf_descriptor
["vnf"]:
1423 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1425 # Step 8. Adding the VNF to the NFVO DB
1426 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1428 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1429 _
, message
= rollback(mydb
, vims
, rollback_list
)
1430 if isinstance(e
, db_base_Exception
):
1431 error_text
= "Exception at database"
1432 elif isinstance(e
, KeyError):
1433 error_text
= "KeyError exception "
1434 e
.http_code
= HTTP_Internal_Server_Error
1436 error_text
= "Exception at VIM"
1437 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1438 #logger.error("start_scenario %s", error_text)
1439 raise NfvoException(error_text
, e
.http_code
)
1442 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1443 global global_config
1445 # Step 1. Check the VNF descriptor
1446 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1447 # Step 2. Check tenant exist
1449 if tenant_id
!= "any":
1450 check_tenant(mydb
, tenant_id
)
1451 if "tenant_id" in vnf_descriptor
["vnf"]:
1452 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1453 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1456 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1457 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1458 if global_config
["auto_push_VNF_to_VIMs"]:
1459 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1461 # Step 4. Review the descriptor and add missing fields
1462 #print vnf_descriptor
1463 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1464 vnf_name
= vnf_descriptor
['vnf']['name']
1465 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1466 if "physical" in vnf_descriptor
['vnf']:
1467 del vnf_descriptor
['vnf']['physical']
1468 #print vnf_descriptor
1470 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1471 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1472 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1474 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1475 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1476 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1478 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1479 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1481 VNFCitem
["name"] = vnfc
['name']
1482 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1484 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1487 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1488 myflavorDict
["description"] = VNFCitem
["description"]
1489 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1490 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1491 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1492 myflavorDict
["extended"] = {}
1494 devices
= vnfc
.get("devices")
1496 myflavorDict
["extended"]["devices"] = devices
1499 # 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
1500 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1502 # Previous code has been commented
1503 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1504 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1505 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1506 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1508 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1510 # print "Error creating flavor: unknown processor model. Rollback successful."
1511 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1513 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1514 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1516 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1517 myflavorDict
['extended']['numas'] = vnfc
['numas']
1521 # Step 6.2 New flavors are created in the VIM
1522 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1524 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1525 VNFCitem
["flavor_id"] = flavor_id
1526 VNFCDict
[vnfc
['name']] = VNFCitem
1528 logger
.debug("Creating new images in the VIM for each VNFC")
1529 # Step 6.3 New images are created in the VIM
1530 #For each VNFC, we must create the appropriate image.
1531 #This "for" loop might be integrated with the previous one
1532 #In case this integration is made, the VNFCDict might become a VNFClist.
1533 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1534 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1536 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1537 image_dict
['universal_name']=vnfc
.get('image name')
1538 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1539 image_dict
['location']=vnfc
.get('VNFC image')
1540 #image_dict['new_location']=vnfc.get('image location')
1541 image_dict
['checksum']=vnfc
.get('image checksum')
1542 image_metadata_dict
= vnfc
.get('image metadata', None)
1543 image_metadata_str
= None
1544 if image_metadata_dict
is not None:
1545 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1546 image_dict
['metadata']=image_metadata_str
1547 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1548 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1549 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1550 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1551 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1552 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1553 if vnfc
.get("boot-data"):
1554 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1556 # Step 7. Storing the VNF descriptor in the repository
1557 if "descriptor" not in vnf_descriptor
["vnf"]:
1558 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1560 # Step 8. Adding the VNF to the NFVO DB
1561 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1563 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1564 _
, message
= rollback(mydb
, vims
, rollback_list
)
1565 if isinstance(e
, db_base_Exception
):
1566 error_text
= "Exception at database"
1567 elif isinstance(e
, KeyError):
1568 error_text
= "KeyError exception "
1569 e
.http_code
= HTTP_Internal_Server_Error
1571 error_text
= "Exception at VIM"
1572 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1573 #logger.error("start_scenario %s", error_text)
1574 raise NfvoException(error_text
, e
.http_code
)
1577 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1578 #check valid tenant_id
1579 check_tenant(mydb
, tenant_id
)
1582 if tenant_id
!= "any":
1583 where_or
["tenant_id"] = tenant_id
1584 where_or
["public"] = True
1585 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1587 vnf_id
= vnf
["uuid"]
1588 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1589 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1590 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1591 data
={'vnf' : filtered_content
}
1593 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1594 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1596 WHERE
={'vnfs.uuid': vnf_id
} )
1598 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1599 # change boot_data into boot-data
1601 if vm
.get("boot_data"):
1602 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1605 data
['vnf']['VNFC'] = content
1606 #TODO: GET all the information from a VNFC and include it in the output.
1609 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1610 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1611 WHERE
={'vnfs.uuid': vnf_id
} )
1612 data
['vnf']['nets'] = content
1614 #GET ip-profile for each net
1615 for net
in data
['vnf']['nets']:
1616 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1617 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1618 WHERE
={'net_id': net
["uuid"]} )
1619 if len(ipprofiles
)==1:
1620 net
["ip_profile"] = ipprofiles
[0]
1621 elif len(ipprofiles
)>1:
1622 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), HTTP_Bad_Request
)
1625 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1627 #GET External Interfaces
1628 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1629 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1630 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1631 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1633 data
['vnf']['external-connections'] = content
1638 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1639 # Check tenant exist
1640 if tenant_id
!= "any":
1641 check_tenant(mydb
, tenant_id
)
1642 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1643 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1647 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1649 if tenant_id
!= "any":
1650 where_or
["tenant_id"] = tenant_id
1651 where_or
["public"] = True
1652 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1653 vnf_id
= vnf
["uuid"]
1655 # "Getting the list of flavors and tenants of the VNF"
1656 flavorList
= get_flavorlist(mydb
, vnf_id
)
1657 if len(flavorList
)==0:
1658 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1660 imageList
= get_imagelist(mydb
, vnf_id
)
1661 if len(imageList
)==0:
1662 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1664 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1666 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1669 for flavor
in flavorList
:
1670 #check if flavor is used by other vnf
1672 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1674 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1676 #flavor not used, must be deleted
1678 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1679 for flavor_vim
in c
:
1680 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1684 for vim
in vims
.values():
1685 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1691 myvim
.delete_flavor(flavor_vim
["vim_id"])
1692 except vimconn
.vimconnNotFoundException
:
1693 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1694 flavor_vim
["datacenter_vim_id"] )
1695 except vimconn
.vimconnException
as e
:
1696 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1697 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1698 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1699 flavor_vim
["datacenter_vim_id"]))
1700 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1701 mydb
.delete_row_by_id('flavors', flavor
)
1702 except db_base_Exception
as e
:
1703 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1704 undeletedItems
.append("flavor {}".format(flavor
))
1707 for image
in imageList
:
1709 #check if image is used by other vnf
1710 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1712 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1714 #image not used, must be deleted
1716 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1718 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1720 if image_vim
['created']=='false': #skip this image because not created by openmano
1722 myvim
=vims
[ image_vim
["datacenter_id"] ]
1724 myvim
.delete_image(image_vim
["vim_id"])
1725 except vimconn
.vimconnNotFoundException
as e
:
1726 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1727 except vimconn
.vimconnException
as e
:
1728 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1729 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1730 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1731 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1732 mydb
.delete_row_by_id('images', image
)
1733 except db_base_Exception
as e
:
1734 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1735 undeletedItems
.append("image %s" % image
)
1737 return vnf_id
+ " " + vnf
["name"]
1739 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1742 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1743 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1747 return -HTTP_Not_Found
, "datacenter '%s' not found" % datacenter_name
1748 myvim
= vims
.values()[0]
1749 result
,servers
= myvim
.get_hosts_info()
1751 return result
, servers
1752 topology
= {'name':myvim
['name'] , 'servers': servers
}
1753 return result
, topology
1756 def get_hosts(mydb
, nfvo_tenant_id
):
1757 vims
= get_vim(mydb
, nfvo_tenant_id
)
1759 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), HTTP_Not_Found
)
1761 #print "nfvo.datacenter_action() error. Several datacenters found"
1762 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
1763 myvim
= vims
.values()[0]
1765 hosts
= myvim
.get_hosts()
1766 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1768 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1770 server
={'name':host
['name'], 'vms':[]}
1771 for vm
in host
['instances']:
1772 #get internal name and model
1774 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1775 WHERE
={'vim_vm_id':vm
['id']} )
1777 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1779 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1781 except db_base_Exception
as e
:
1782 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1783 datacenter
['Datacenters'][0]['servers'].append(server
)
1784 #return -400, "en construccion"
1786 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1788 except vimconn
.vimconnException
as e
:
1789 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1792 def new_scenario(mydb
, tenant_id
, topo
):
1794 # result, vims = get_vim(mydb, tenant_id)
1796 # return result, vims
1798 if tenant_id
!= "any":
1799 check_tenant(mydb
, tenant_id
)
1800 if "tenant_id" in topo
:
1801 if topo
["tenant_id"] != tenant_id
:
1802 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1807 #1.1: get VNFs and external_networks (other_nets).
1809 other_nets
={} #external_networks, bridge_networks and data_networkds
1810 nodes
= topo
['topology']['nodes']
1811 for k
in nodes
.keys():
1812 if nodes
[k
]['type'] == 'VNF':
1814 vnfs
[k
]['ifaces'] = {}
1815 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1816 other_nets
[k
] = nodes
[k
]
1817 other_nets
[k
]['external']=True
1818 elif nodes
[k
]['type'] == 'network':
1819 other_nets
[k
] = nodes
[k
]
1820 other_nets
[k
]['external']=False
1823 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1824 for name
,vnf
in vnfs
.items():
1825 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1827 error_pos
= "'topology':'nodes':'" + name
+ "'"
1829 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1830 where
['uuid'] = vnf
['vnf_id']
1831 if 'VNF model' in vnf
:
1832 error_text
+= " 'VNF model' " + vnf
['VNF model']
1833 where
['name'] = vnf
['VNF model']
1835 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, HTTP_Bad_Request
)
1837 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1841 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1843 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1844 vnf
['uuid']=vnf_db
[0]['uuid']
1845 vnf
['description']=vnf_db
[0]['description']
1846 #get external interfaces
1847 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1848 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1849 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1850 for ext_iface
in ext_ifaces
:
1851 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1853 #1.4 get list of connections
1854 conections
= topo
['topology']['connections']
1855 conections_list
= []
1856 conections_list_name
= []
1857 for k
in conections
.keys():
1858 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1859 ifaces_list
= conections
[k
]['nodes'].items()
1860 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1862 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1863 for k2
in conection_pair_list
:
1866 con_type
= conections
[k
].get("type", "link")
1867 if con_type
!= "link":
1869 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), HTTP_Bad_Request
)
1870 other_nets
[k
] = {'external': False}
1871 if conections
[k
].get("graph"):
1872 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1873 ifaces_list
.append( (k
, None) )
1876 if con_type
== "external_network":
1877 other_nets
[k
]['external'] = True
1878 if conections
[k
].get("model"):
1879 other_nets
[k
]["model"] = conections
[k
]["model"]
1881 other_nets
[k
]["model"] = k
1882 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1883 other_nets
[k
]["model"] = con_type
1885 conections_list_name
.append(k
)
1886 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)
1887 #print set(ifaces_list)
1888 #check valid VNF and iface names
1889 for iface
in ifaces_list
:
1890 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1891 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1892 str(k
), iface
[0]), HTTP_Not_Found
)
1893 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1894 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1895 str(k
), iface
[0], iface
[1]), HTTP_Not_Found
)
1897 #1.5 unify connections from the pair list to a consolidated list
1899 while index
< len(conections_list
):
1901 while index2
< len(conections_list
):
1902 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1903 conections_list
[index
] |
= conections_list
[index2
]
1904 del conections_list
[index2
]
1905 del conections_list_name
[index2
]
1908 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1910 #for k in conections_list:
1915 #1.6 Delete non external nets
1916 # for k in other_nets.keys():
1917 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1918 # for con in conections_list:
1920 # for index in range(0,len(con)):
1921 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1922 # for index in delete_indexes:
1925 #1.7: Check external_ports are present at database table datacenter_nets
1926 for k
,net
in other_nets
.items():
1927 error_pos
= "'topology':'nodes':'" + k
+ "'"
1928 if net
['external']==False:
1929 if 'name' not in net
:
1931 if 'model' not in net
:
1932 raise NfvoException("needed a 'model' at " + error_pos
, HTTP_Bad_Request
)
1933 if net
['model']=='bridge_net':
1934 net
['type']='bridge';
1935 elif net
['model']=='dataplane_net':
1938 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, HTTP_Not_Found
)
1940 #IF we do not want to check that external network exist at datacenter
1945 # if 'net_id' in net:
1946 # error_text += " 'net_id' " + net['net_id']
1947 # WHERE_['uuid'] = net['net_id']
1948 # if 'model' in net:
1949 # error_text += " 'model' " + net['model']
1950 # WHERE_['name'] = net['model']
1951 # if len(WHERE_) == 0:
1952 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1953 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1954 # FROM='datacenter_nets', WHERE=WHERE_ )
1956 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1958 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1959 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1961 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1962 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1963 # other_nets[k].update(net_db[0])
1966 net_nb
=0 #Number of nets
1967 for con
in conections_list
:
1968 #check if this is connected to a external net
1972 for index
in range(0,len(con
)):
1973 #check if this is connected to a external net
1974 for net_key
in other_nets
.keys():
1975 if con
[index
][0]==net_key
:
1976 if other_net_index
>=0:
1977 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
1978 #print "nfvo.new_scenario " + error_text
1979 raise NfvoException(error_text
, HTTP_Bad_Request
)
1981 other_net_index
= index
1982 net_target
= net_key
1984 #print "other_net_index", other_net_index
1986 if other_net_index
>=0:
1987 del con
[other_net_index
]
1988 #IF we do not want to check that external network exist at datacenter
1989 if other_nets
[net_target
]['external'] :
1990 if "name" not in other_nets
[net_target
]:
1991 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
1992 if other_nets
[net_target
]["type"] == "external_network":
1993 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
1994 other_nets
[net_target
]["type"] = "data"
1996 other_nets
[net_target
]["type"] = "bridge"
1998 # if other_nets[net_target]['external'] :
1999 # 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
2000 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2001 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2002 # print "nfvo.new_scenario " + error_text
2003 # return -HTTP_Bad_Request, error_text
2006 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2009 net_type_bridge
=False
2011 net_target
= "__-__net"+str(net_nb
)
2012 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2013 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
2016 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2017 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2018 if iface_type
=='mgmt' or iface_type
=='bridge':
2019 net_type_bridge
= True
2021 net_type_data
= True
2022 if net_type_bridge
and net_type_data
:
2023 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
2024 #print "nfvo.new_scenario " + error_text
2025 raise NfvoException(error_text
, HTTP_Bad_Request
)
2026 elif net_type_bridge
:
2029 type_
='data' if len(con
)>2 else 'ptp'
2030 net_list
[net_target
]['type'] = type_
2033 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
2034 #print "nfvo.new_scenario " + error_text
2036 raise NfvoException(error_text
, HTTP_Bad_Request
)
2038 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2039 #1.8.1 obtain management net
2040 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2041 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2042 #1.8.2 check all interfaces from all vnfs
2044 add_mgmt_net
= False
2045 for vnf
in vnfs
.values():
2046 for iface
in vnf
['ifaces'].values():
2047 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2048 #iface not connected
2049 iface
['net_key'] = 'mgmt'
2051 if add_mgmt_net
and 'mgmt' not in net_list
:
2052 net_list
['mgmt']=mgmt_net
[0]
2053 net_list
['mgmt']['external']=True
2054 net_list
['mgmt']['graph']={'visible':False}
2056 net_list
.update(other_nets
)
2058 #print 'net_list', net_list
2063 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2064 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2065 'tenant_id':tenant_id
, 'name':topo
['name'],
2066 'description':topo
.get('description',topo
['name']),
2067 'public': topo
.get('public', False)
2073 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2074 """ This creates a new scenario for version 0.2 and 0.3"""
2075 scenario
= scenario_dict
["scenario"]
2076 if tenant_id
!= "any":
2077 check_tenant(mydb
, tenant_id
)
2078 if "tenant_id" in scenario
:
2079 if scenario
["tenant_id"] != tenant_id
:
2080 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2081 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2082 scenario
["tenant_id"], tenant_id
), HTTP_Unauthorized
)
2086 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2087 for name
,vnf
in scenario
["vnfs"].iteritems():
2088 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2090 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2092 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2093 where
['uuid'] = vnf
['vnf_id']
2094 if 'vnf_name' in vnf
:
2095 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2096 where
['name'] = vnf
['vnf_name']
2098 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, HTTP_Bad_Request
)
2099 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2102 if len(vnf_db
) == 0:
2103 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
2104 elif len(vnf_db
) > 1:
2105 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
2106 vnf
['uuid'] = vnf_db
[0]['uuid']
2107 vnf
['description'] = vnf_db
[0]['description']
2109 # get external interfaces
2110 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2111 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2112 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2113 for ext_iface
in ext_ifaces
:
2114 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2115 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2117 # 2: Insert net_key and ip_address at every vnf interface
2118 for net_name
, net
in scenario
["networks"].items():
2119 net_type_bridge
= False
2120 net_type_data
= False
2121 for iface_dict
in net
["interfaces"]:
2122 if version
== "0.2":
2123 temp_dict
= iface_dict
2125 elif version
== "0.3":
2126 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2127 ip_address
= iface_dict
.get('ip_address', None)
2128 for vnf
, iface
in temp_dict
.items():
2129 if vnf
not in scenario
["vnfs"]:
2130 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2132 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2133 raise NfvoException(error_text
, HTTP_Not_Found
)
2134 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2135 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2136 .format(net_name
, iface
)
2137 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2138 raise NfvoException(error_text
, HTTP_Bad_Request
)
2139 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2140 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2141 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2142 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2143 raise NfvoException(error_text
, HTTP_Bad_Request
)
2144 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2145 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2146 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2147 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2148 net_type_bridge
= True
2150 net_type_data
= True
2152 if net_type_bridge
and net_type_data
:
2153 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2155 # logger.debug("nfvo.new_scenario " + error_text)
2156 raise NfvoException(error_text
, HTTP_Bad_Request
)
2157 elif net_type_bridge
:
2160 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2162 if net
.get("implementation"): # for v0.3
2163 if type_
== "bridge" and net
["implementation"] == "underlay":
2164 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2165 "'network':'{}'".format(net_name
)
2166 # logger.debug(error_text)
2167 raise NfvoException(error_text
, HTTP_Bad_Request
)
2168 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2169 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2170 "'network':'{}'".format(net_name
)
2171 # logger.debug(error_text)
2172 raise NfvoException(error_text
, HTTP_Bad_Request
)
2173 net
.pop("implementation")
2174 if "type" in net
and version
== "0.3": # for v0.3
2175 if type_
== "data" and net
["type"] == "e-line":
2176 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2177 "'e-line' at 'network':'{}'".format(net_name
)
2178 # logger.debug(error_text)
2179 raise NfvoException(error_text
, HTTP_Bad_Request
)
2180 elif type_
== "ptp" and net
["type"] == "e-lan":
2184 net
['name'] = net_name
2185 net
['external'] = net
.get('external', False)
2187 # 3: insert at database
2188 scenario
["nets"] = scenario
["networks"]
2189 scenario
['tenant_id'] = tenant_id
2190 scenario_id
= mydb
.new_scenario(scenario
)
2194 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2196 Parses an OSM IM nsd_catalog and insert at DB
2199 :param nsd_descriptor:
2200 :return: The list of created NSD ids
2203 mynsd
= nsd_catalog
.nsd()
2205 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
2206 except Exception as e
:
2207 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), HTTP_Bad_Request
)
2211 db_sce_interfaces
= []
2214 db_sce_rsp_hops
= []
2215 db_sce_classifiers
= []
2216 db_sce_classifier_matches
= []
2218 db_ip_profiles_index
= 0
2221 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.itervalues():
2222 nsd
= nsd_yang
.get()
2225 scenario_uuid
= str(uuid4())
2226 uuid_list
.append(scenario_uuid
)
2227 nsd_uuid_list
.append(scenario_uuid
)
2229 "uuid": scenario_uuid
,
2230 "osm_id": get_str(nsd
, "id", 255),
2231 "name": get_str(nsd
, "name", 255),
2232 "description": get_str(nsd
, "description", 255),
2233 "tenant_id": tenant_id
,
2234 "vendor": get_str(nsd
, "vendor", 255),
2235 "short_name": get_str(nsd
, "short-name", 255),
2236 "descriptor": str(nsd_descriptor
)[:60000],
2238 db_scenarios
.append(db_scenario
)
2240 # table sce_vnfs (constituent-vnfd)
2241 vnf_index2scevnf_uuid
= {}
2242 vnf_index2vnf_uuid
= {}
2243 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2244 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2245 'tenant_id': tenant_id
})
2246 if not existing_vnf
:
2247 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2248 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2249 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2251 sce_vnf_uuid
= str(uuid4())
2252 uuid_list
.append(sce_vnf_uuid
)
2254 "uuid": sce_vnf_uuid
,
2255 "scenario_id": scenario_uuid
,
2256 # "name": get_str(vnf, "member-vnf-index", 255),
2257 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2258 "vnf_id": existing_vnf
[0]["uuid"],
2259 "member_vnf_index": str(vnf
["member-vnf-index"]),
2260 # TODO 'start-by-default': True
2262 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2263 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2264 db_sce_vnfs
.append(db_sce_vnf
)
2266 # table ip_profiles (ip-profiles)
2267 ip_profile_name2db_table_index
= {}
2268 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2270 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2271 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2272 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2273 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2274 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2275 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2278 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2279 dns_list
.append(str(dns
.get("address")))
2280 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2281 if ip_profile
["ip-profile-params"].get('security-group'):
2282 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2283 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2284 db_ip_profiles_index
+= 1
2285 db_ip_profiles
.append(db_ip_profile
)
2287 # table sce_nets (internal-vld)
2288 for vld
in nsd
.get("vld").itervalues():
2289 sce_net_uuid
= str(uuid4())
2290 uuid_list
.append(sce_net_uuid
)
2292 "uuid": sce_net_uuid
,
2293 "name": get_str(vld
, "name", 255),
2294 "scenario_id": scenario_uuid
,
2296 "multipoint": not vld
.get("type") == "ELINE",
2297 "osm_id": get_str(vld
, "id", 255),
2299 "description": get_str(vld
, "description", 255),
2301 # guess type of network
2302 if vld
.get("mgmt-network"):
2303 db_sce_net
["type"] = "bridge"
2304 db_sce_net
["external"] = True
2305 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2306 db_sce_net
["type"] = "data"
2308 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2309 db_sce_net
["type"] = None
2310 db_sce_nets
.append(db_sce_net
)
2312 # ip-profile, link db_ip_profile with db_sce_net
2313 if vld
.get("ip-profile-ref"):
2314 ip_profile_name
= vld
.get("ip-profile-ref")
2315 if ip_profile_name
not in ip_profile_name2db_table_index
:
2316 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2317 " Reference to a non-existing 'ip_profiles'".format(
2318 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2320 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2321 elif vld
.get("vim-network-name"):
2322 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2324 # table sce_interfaces (vld:vnfd-connection-point-ref)
2325 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2326 vnf_index
= str(iface
['member-vnf-index-ref'])
2327 # check correct parameters
2328 if vnf_index
not in vnf_index2vnf_uuid
:
2329 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2330 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2331 "'nsd':'constituent-vnfd'".format(
2332 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2335 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2336 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2337 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2338 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2340 if not existing_ifaces
:
2341 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2342 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2343 "connection-point name at VNFD '{}'".format(
2344 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2345 str(iface
.get("vnfd-id-ref"))[:255]),
2347 interface_uuid
= existing_ifaces
[0]["uuid"]
2348 if existing_ifaces
[0]["iface_type"] == "data" and not db_sce_net
["type"]:
2349 db_sce_net
["type"] = "data"
2350 sce_interface_uuid
= str(uuid4())
2351 uuid_list
.append(sce_net_uuid
)
2352 iface_ip_address
= None
2353 if iface
.get("ip-address"):
2354 iface_ip_address
= str(iface
.get("ip-address"))
2355 db_sce_interface
= {
2356 "uuid": sce_interface_uuid
,
2357 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2358 "sce_net_id": sce_net_uuid
,
2359 "interface_id": interface_uuid
,
2360 "ip_address": iface_ip_address
,
2362 db_sce_interfaces
.append(db_sce_interface
)
2363 if not db_sce_net
["type"]:
2364 db_sce_net
["type"] = "bridge"
2366 # table sce_vnffgs (vnffgd)
2367 for vnffg
in nsd
.get("vnffgd").itervalues():
2368 sce_vnffg_uuid
= str(uuid4())
2369 uuid_list
.append(sce_vnffg_uuid
)
2371 "uuid": sce_vnffg_uuid
,
2372 "name": get_str(vnffg
, "name", 255),
2373 "scenario_id": scenario_uuid
,
2374 "vendor": get_str(vnffg
, "vendor", 255),
2375 "description": get_str(vld
, "description", 255),
2377 db_sce_vnffgs
.append(db_sce_vnffg
)
2381 for rsp
in vnffg
.get("rsp").itervalues():
2382 sce_rsp_uuid
= str(uuid4())
2383 uuid_list
.append(sce_rsp_uuid
)
2385 "uuid": sce_rsp_uuid
,
2386 "name": get_str(rsp
, "name", 255),
2387 "sce_vnffg_id": sce_vnffg_uuid
,
2388 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2390 db_sce_rsps
.append(db_sce_rsp
)
2391 db_sce_rsp_hops
= []
2392 for iface
in rsp
.get("vnfd-connection-point-ref").itervalues():
2393 vnf_index
= str(iface
['member-vnf-index-ref'])
2394 if_order
= int(iface
['order'])
2395 # check correct parameters
2396 if vnf_index
not in vnf_index2vnf_uuid
:
2397 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2398 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2399 "'nsd':'constituent-vnfd'".format(
2400 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2403 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2404 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2405 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2406 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2408 if not existing_ifaces
:
2409 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2410 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2411 "connection-point name at VNFD '{}'".format(
2412 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2413 str(iface
.get("vnfd-id-ref"))[:255]),
2415 interface_uuid
= existing_ifaces
[0]["uuid"]
2416 sce_rsp_hop_uuid
= str(uuid4())
2417 uuid_list
.append(sce_rsp_hop_uuid
)
2419 "uuid": sce_rsp_hop_uuid
,
2420 "if_order": if_order
,
2421 "interface_id": interface_uuid
,
2422 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2423 "sce_rsp_id": sce_rsp_uuid
,
2425 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2427 # deal with classifiers
2428 db_sce_classifiers
= []
2429 for classifier
in vnffg
.get("classifier").itervalues():
2430 sce_classifier_uuid
= str(uuid4())
2431 uuid_list
.append(sce_classifier_uuid
)
2434 vnf_index
= str(classifier
['member-vnf-index-ref'])
2435 if vnf_index
not in vnf_index2vnf_uuid
:
2436 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2437 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2438 "'nsd':'constituent-vnfd'".format(
2439 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2441 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2442 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2443 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2444 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2446 if not existing_ifaces
:
2447 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2448 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2449 "connection-point name at VNFD '{}'".format(
2450 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2451 str(iface
.get("vnfd-id-ref"))[:255]),
2453 interface_uuid
= existing_ifaces
[0]["uuid"]
2455 db_sce_classifier
= {
2456 "uuid": sce_classifier_uuid
,
2457 "name": get_str(classifier
, "name", 255),
2458 "sce_vnffg_id": sce_vnffg_uuid
,
2459 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2460 "interface_id": interface_uuid
,
2462 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2463 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2464 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2465 db_sce_classifiers
.append(db_sce_classifier
)
2467 db_sce_classifier_matches
= []
2468 for match
in classifier
.get("match-attributes").itervalues():
2469 sce_classifier_match_uuid
= str(uuid4())
2470 uuid_list
.append(sce_classifier_match_uuid
)
2471 db_sce_classifier_match
= {
2472 "uuid": sce_classifier_match_uuid
,
2473 "ip_proto": get_str(match
, "ip-proto", 2),
2474 "source_ip": get_str(match
, "source-ip-address", 16),
2475 "destination_ip": get_str(match
, "destination-ip-address", 16),
2476 "source_port": get_str(match
, "source-port", 5),
2477 "destination_port": get_str(match
, "destination-port", 5),
2478 "sce_classifier_id": sce_classifier_uuid
,
2480 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2483 # remove unneeded id's in sce_rsps
2484 for rsp
in db_sce_rsps
:
2488 {"scenarios": db_scenarios
},
2489 {"sce_nets": db_sce_nets
},
2490 {"ip_profiles": db_ip_profiles
},
2491 {"sce_vnfs": db_sce_vnfs
},
2492 {"sce_interfaces": db_sce_interfaces
},
2493 {"sce_vnffgs": db_sce_vnffgs
},
2494 {"sce_rsps": db_sce_rsps
},
2495 {"sce_rsp_hops": db_sce_rsp_hops
},
2496 {"sce_classifiers": db_sce_classifiers
},
2497 {"sce_classifier_matches": db_sce_classifier_matches
},
2500 logger
.debug("new_nsd_v3 done: %s",
2501 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2502 mydb
.new_rows(db_tables
, uuid_list
)
2503 return nsd_uuid_list
2504 except NfvoException
:
2506 except Exception as e
:
2507 logger
.error("Exception {}".format(e
))
2508 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2511 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2512 data
["uuid"] = scenario_id
2513 data
["tenant_id"] = tenant_id
2514 c
= mydb
.edit_scenario( data
)
2518 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2519 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2520 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2521 vims
= {datacenter_id
: myvim
}
2522 myvim_tenant
= myvim
['tenant_id']
2523 datacenter_name
= myvim
['name']
2527 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2528 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2529 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2530 scenarioDict
['datacenter_id'] = datacenter_id
2531 #print '================scenarioDict======================='
2532 #print json.dumps(scenarioDict, indent=4)
2533 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2535 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2536 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2538 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2539 auxNetDict
['scenario'] = {}
2541 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2542 for sce_net
in scenarioDict
['nets']:
2543 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2545 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2546 myNetName
= myNetName
[0:255] #limit length
2547 myNetType
= sce_net
['type']
2549 myNetDict
["name"] = myNetName
2550 myNetDict
["type"] = myNetType
2551 myNetDict
["tenant_id"] = myvim_tenant
2552 myNetIPProfile
= sce_net
.get('ip_profile', None)
2554 #We should use the dictionary as input parameter for new_network
2556 if not sce_net
["external"]:
2557 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2558 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2559 sce_net
['vim_id'] = network_id
2560 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2561 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2562 sce_net
["created"] = True
2564 if sce_net
['vim_id'] == None:
2565 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2566 _
, message
= rollback(mydb
, vims
, rollbackList
)
2567 logger
.error("nfvo.start_scenario: %s", error_text
)
2568 raise NfvoException(error_text
, HTTP_Bad_Request
)
2569 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2570 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2572 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2573 #For each vnf net, we create it and we add it to instanceNetlist.
2575 for sce_vnf
in scenarioDict
['vnfs']:
2576 for net
in sce_vnf
['nets']:
2577 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2579 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2580 myNetName
= myNetName
[0:255] #limit length
2581 myNetType
= net
['type']
2583 myNetDict
["name"] = myNetName
2584 myNetDict
["type"] = myNetType
2585 myNetDict
["tenant_id"] = myvim_tenant
2586 myNetIPProfile
= net
.get('ip_profile', None)
2589 #We should use the dictionary as input parameter for new_network
2590 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2591 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2592 net
['vim_id'] = network_id
2593 if sce_vnf
['uuid'] not in auxNetDict
:
2594 auxNetDict
[sce_vnf
['uuid']] = {}
2595 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2596 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2597 net
["created"] = True
2599 #print "auxNetDict:"
2600 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2602 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2603 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2605 for sce_vnf
in scenarioDict
['vnfs']:
2606 vnf_availability_zones
= []
2607 for vm
in sce_vnf
['vms']:
2608 vm_av
= vm
.get('availability_zone')
2609 if vm_av
and vm_av
not in vnf_availability_zones
:
2610 vnf_availability_zones
.append(vm_av
)
2612 # check if there is enough availability zones available at vim level.
2613 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2614 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2615 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2617 for vm
in sce_vnf
['vms']:
2620 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2621 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2622 #myVMDict['description'] = vm['description']
2623 myVMDict
['description'] = myVMDict
['name'][0:99]
2625 myVMDict
['start'] = "no"
2626 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2627 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2629 #create image at vim in case it not exist
2630 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2631 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2632 vm
['vim_image_id'] = image_id
2634 #create flavor at vim in case it not exist
2635 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2636 if flavor_dict
['extended']!=None:
2637 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2638 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2639 vm
['vim_flavor_id'] = flavor_id
2642 myVMDict
['imageRef'] = vm
['vim_image_id']
2643 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2644 myVMDict
['networks'] = []
2645 for iface
in vm
['interfaces']:
2647 if iface
['type']=="data":
2648 netDict
['type'] = iface
['model']
2649 elif "model" in iface
and iface
["model"]!=None:
2650 netDict
['model']=iface
['model']
2651 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2652 #discover type of interface looking at flavor
2653 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2654 for flavor_iface
in numa
.get('interfaces',[]):
2655 if flavor_iface
.get('name') == iface
['internal_name']:
2656 if flavor_iface
['dedicated'] == 'yes':
2657 netDict
['type']="PF" #passthrough
2658 elif flavor_iface
['dedicated'] == 'no':
2659 netDict
['type']="VF" #siov
2660 elif flavor_iface
['dedicated'] == 'yes:sriov':
2661 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2662 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2664 netDict
["use"]=iface
['type']
2665 if netDict
["use"]=="data" and not netDict
.get("type"):
2666 #print "netDict", netDict
2667 #print "iface", iface
2668 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'])
2669 if flavor_dict
.get('extended')==None:
2670 raise NfvoException(e_text
+ "After database migration some information is not available. \
2671 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2673 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2674 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2675 netDict
["type"]="virtual"
2676 if "vpci" in iface
and iface
["vpci"] is not None:
2677 netDict
['vpci'] = iface
['vpci']
2678 if "mac" in iface
and iface
["mac"] is not None:
2679 netDict
['mac_address'] = iface
['mac']
2680 if "port-security" in iface
and iface
["port-security"] is not None:
2681 netDict
['port_security'] = iface
['port-security']
2682 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2683 netDict
['floating_ip'] = iface
['floating-ip']
2684 netDict
['name'] = iface
['internal_name']
2685 if iface
['net_id'] is None:
2686 for vnf_iface
in sce_vnf
["interfaces"]:
2689 if vnf_iface
['interface_id']==iface
['uuid']:
2690 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2693 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2694 #skip bridge ifaces not connected to any net
2695 #if 'net_id' not in netDict or netDict['net_id']==None:
2697 myVMDict
['networks'].append(netDict
)
2698 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2699 #print myVMDict['name']
2700 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2701 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2702 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2704 if 'availability_zone' in myVMDict
:
2705 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2709 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2710 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2711 availability_zone_index
=av_index
,
2712 availability_zone_list
=vnf_availability_zones
)
2713 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2714 vm
['vim_id'] = vm_id
2715 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2716 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2717 for net
in myVMDict
['networks']:
2719 for iface
in vm
['interfaces']:
2720 if net
["name"]==iface
["internal_name"]:
2721 iface
["vim_id"]=net
["vim_id"]
2724 logger
.debug("start scenario Deployment done")
2725 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2726 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2727 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2728 return mydb
.get_instance_scenario(instance_id
)
2730 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2731 _
, message
= rollback(mydb
, vims
, rollbackList
)
2732 if isinstance(e
, db_base_Exception
):
2733 error_text
= "Exception at database"
2735 error_text
= "Exception at VIM"
2736 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2737 #logger.error("start_scenario %s", error_text)
2738 raise NfvoException(error_text
, e
.http_code
)
2740 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2741 """ join the cloud config information into cloud_config_preserve.
2742 In case of conflict cloud_config_preserve preserves
2745 if not cloud_config_preserve
and not cloud_config
:
2748 new_cloud_config
= {"key-pairs":[], "users":[]}
2750 if cloud_config_preserve
:
2751 for key
in cloud_config_preserve
.get("key-pairs", () ):
2752 if key
not in new_cloud_config
["key-pairs"]:
2753 new_cloud_config
["key-pairs"].append(key
)
2755 for key
in cloud_config
.get("key-pairs", () ):
2756 if key
not in new_cloud_config
["key-pairs"]:
2757 new_cloud_config
["key-pairs"].append(key
)
2758 if not new_cloud_config
["key-pairs"]:
2759 del new_cloud_config
["key-pairs"]
2763 new_cloud_config
["users"] += cloud_config
.get("users", () )
2764 if cloud_config_preserve
:
2765 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2766 index_to_delete
= []
2767 users
= new_cloud_config
.get("users", [])
2768 for index0
in range(0,len(users
)):
2769 if index0
in index_to_delete
:
2771 for index1
in range(index0
+1,len(users
)):
2772 if index1
in index_to_delete
:
2774 if users
[index0
]["name"] == users
[index1
]["name"]:
2775 index_to_delete
.append(index1
)
2776 for key
in users
[index1
].get("key-pairs",()):
2777 if "key-pairs" not in users
[index0
]:
2778 users
[index0
]["key-pairs"] = [key
]
2779 elif key
not in users
[index0
]["key-pairs"]:
2780 users
[index0
]["key-pairs"].append(key
)
2781 index_to_delete
.sort(reverse
=True)
2782 for index
in index_to_delete
:
2784 if not new_cloud_config
["users"]:
2785 del new_cloud_config
["users"]
2788 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2789 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2790 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2791 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2794 new_cloud_config
["user-data"] = []
2795 if cloud_config
and cloud_config
.get("user-data"):
2796 if isinstance(cloud_config
["user-data"], list):
2797 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2799 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2800 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2801 if isinstance(cloud_config_preserve
["user-data"], list):
2802 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2804 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2805 if not new_cloud_config
["user-data"]:
2806 del new_cloud_config
["user-data"]
2809 new_cloud_config
["config-files"] = []
2810 if cloud_config
and cloud_config
.get("config-files") != None:
2811 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2812 if cloud_config_preserve
:
2813 for file in cloud_config_preserve
.get("config-files", ()):
2814 for index
in range(0, len(new_cloud_config
["config-files"])):
2815 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2816 new_cloud_config
["config-files"][index
] = file
2819 new_cloud_config
["config-files"].append(file)
2820 if not new_cloud_config
["config-files"]:
2821 del new_cloud_config
["config-files"]
2822 return new_cloud_config
2825 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2826 datacenter_id
= None
2827 datacenter_name
= None
2830 if datacenter_tenant_id
:
2831 thread_id
= datacenter_tenant_id
2832 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2834 where_
={"td.nfvo_tenant_id": tenant_id
}
2835 if datacenter_id_name
:
2836 if utils
.check_valid_uuid(datacenter_id_name
):
2837 datacenter_id
= datacenter_id_name
2838 where_
["dt.datacenter_id"] = datacenter_id
2840 datacenter_name
= datacenter_id_name
2841 where_
["d.name"] = datacenter_name
2842 if datacenter_tenant_id
:
2843 where_
["dt.uuid"] = datacenter_tenant_id
2844 datacenters
= mydb
.get_rows(
2845 SELECT
=("dt.uuid as datacenter_tenant_id",),
2846 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2847 "join datacenters as d on d.uuid=dt.datacenter_id",
2849 if len(datacenters
) > 1:
2850 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2852 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2853 thread
= vim_threads
["running"].get(thread_id
)
2855 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2856 return thread_id
, thread
2857 except db_base_Exception
as e
:
2858 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2861 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
2863 if utils
.check_valid_uuid(datacenter_id_name
):
2864 WHERE_dict
['d.uuid'] = datacenter_id_name
2866 WHERE_dict
['d.name'] = datacenter_id_name
2869 WHERE_dict
['nfvo_tenant_id'] = tenant_id
2870 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2871 " dt on td.datacenter_tenant_id=dt.uuid"
2873 from_
= 'datacenters as d'
2874 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid",), WHERE
=WHERE_dict
)
2875 if len(vimaccounts
) == 0:
2876 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2877 elif len(vimaccounts
)>1:
2878 #print "nfvo.datacenter_action() error. Several datacenters found"
2879 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2880 return vimaccounts
[0]["uuid"]
2883 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2884 datacenter_id
= None
2885 datacenter_name
= None
2886 if datacenter_id_name
:
2887 if utils
.check_valid_uuid(datacenter_id_name
):
2888 datacenter_id
= datacenter_id_name
2890 datacenter_name
= datacenter_id_name
2891 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2893 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2895 #print "nfvo.datacenter_action() error. Several datacenters found"
2896 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2897 return vims
.keys()[0], vims
.values()[0]
2901 '''Takes dict d and updates it with the values in dict u.'''
2902 '''It merges all depth levels'''
2903 for k
, v
in u
.iteritems():
2904 if isinstance(v
, collections
.Mapping
):
2905 r
= update(d
.get(k
, {}), v
)
2912 def create_instance(mydb
, tenant_id
, instance_dict
):
2913 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2914 # logger.debug("Creating instance...")
2915 scenario
= instance_dict
["scenario"]
2917 # find main datacenter
2919 myvim_threads_id
= {}
2920 datacenter
= instance_dict
.get("datacenter")
2921 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
2922 myvims
[default_datacenter_id
] = vim
2923 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
2924 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
2925 # myvim_tenant = myvim['tenant_id']
2928 # print "Checking that the scenario exists and getting the scenario dictionary"
2929 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
2930 datacenter_id
=default_datacenter_id
)
2932 # logger.debug(">>>>>> Dictionaries before merging")
2933 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2934 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
2936 db_instance_vnfs
= []
2937 db_instance_vms
= []
2938 db_instance_interfaces
= []
2939 db_instance_sfis
= []
2940 db_instance_sfs
= []
2941 db_instance_classifications
= []
2942 db_instance_sfps
= []
2947 instance_name
= instance_dict
["name"]
2948 instance_uuid
= str(uuid4())
2949 uuid_list
.append(instance_uuid
)
2950 db_instance_scenario
= {
2951 "uuid": instance_uuid
,
2952 "name": instance_name
,
2953 "tenant_id": tenant_id
,
2954 "scenario_id": scenarioDict
['uuid'],
2955 "datacenter_id": default_datacenter_id
,
2956 # filled bellow 'datacenter_tenant_id'
2957 "description": instance_dict
.get("description"),
2959 if scenarioDict
.get("cloud-config"):
2960 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
2961 default_flow_style
=True, width
=256)
2962 instance_action_id
= get_task_id()
2963 db_instance_action
= {
2964 "uuid": instance_action_id
, # same uuid for the instance and the action on create
2965 "tenant_id": tenant_id
,
2966 "instance_id": instance_uuid
,
2967 "description": "CREATE",
2970 # Auxiliary dictionaries from x to y
2971 sce_net2instance
= {}
2972 net2task_id
= {'scenario': {}}
2974 def ip_profile_IM2RO(ip_profile_im
):
2975 # translate from input format to database format
2977 if 'subnet-address' in ip_profile_im
:
2978 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
2979 if 'ip-version' in ip_profile_im
:
2980 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
2981 if 'gateway-address' in ip_profile_im
:
2982 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
2983 if 'dns-address' in ip_profile_im
:
2984 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
2985 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
2986 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
2987 if 'dhcp' in ip_profile_im
:
2988 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
2989 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
2990 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
2991 return ip_profile_ro
2993 # logger.debug("Creating instance from scenario-dict:\n%s",
2994 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2996 # 0 check correct parameters
2997 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
2998 for scenario_net
in scenarioDict
['nets']:
2999 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3002 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3004 if "sites" not in net_instance_desc
:
3005 net_instance_desc
["sites"] = [ {} ]
3006 site_without_datacenter_field
= False
3007 for site
in net_instance_desc
["sites"]:
3008 if site
.get("datacenter"):
3009 site
["datacenter"] = get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3010 if site
["datacenter"] not in myvims
:
3011 # Add this datacenter to myvims
3012 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3014 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3015 site
["datacenter"] = d
# change name to id
3017 if site_without_datacenter_field
:
3018 raise NfvoException("Found more than one entries without datacenter field at "
3019 "instance:networks:{}:sites".format(net_name
), HTTP_Bad_Request
)
3020 site_without_datacenter_field
= True
3021 site
["datacenter"] = default_datacenter_id
# change name to id
3023 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
3024 for scenario_vnf
in scenarioDict
['vnfs']:
3025 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3028 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), HTTP_Bad_Request
)
3029 if "datacenter" in vnf_instance_desc
:
3030 # Add this datacenter to myvims
3031 vnf_instance_desc
["datacenter"] = get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3032 if vnf_instance_desc
["datacenter"] not in myvims
:
3033 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3035 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3036 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3038 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).iteritems():
3039 for scenario_net
in scenario_vnf
['nets']:
3040 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3043 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), HTTP_Bad_Request
)
3044 if net_instance_desc
.get("vim-network-name"):
3045 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3046 if net_instance_desc
.get("name"):
3047 scenario_net
["name"] = net_instance_desc
["name"]
3048 if 'ip-profile' in net_instance_desc
:
3049 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3050 if 'ip_profile' not in scenario_net
:
3051 scenario_net
['ip_profile'] = ipprofile_db
3053 update(scenario_net
['ip_profile'], ipprofile_db
)
3055 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).iteritems():
3056 for scenario_vm
in scenario_vnf
['vms']:
3057 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3060 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), HTTP_Bad_Request
)
3061 scenario_vm
["instance_parameters"] = vdu_instance_desc
3062 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).iteritems():
3063 for scenario_interface
in scenario_vm
['interfaces']:
3064 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3065 scenario_interface
.update(iface_instance_desc
)
3068 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), HTTP_Bad_Request
)
3070 # 0.1 parse cloud-config parameters
3071 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3073 # 0.2 merge instance information into scenario
3074 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3075 # However, this is not possible yet.
3076 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3077 for scenario_net
in scenarioDict
['nets']:
3078 if net_name
== scenario_net
["name"]:
3079 if 'ip-profile' in net_instance_desc
:
3080 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3081 if 'ip_profile' not in scenario_net
:
3082 scenario_net
['ip_profile'] = ipprofile_db
3084 update(scenario_net
['ip_profile'], ipprofile_db
)
3085 for interface
in net_instance_desc
.get('interfaces', ()):
3086 if 'ip_address' in interface
:
3087 for vnf
in scenarioDict
['vnfs']:
3088 if interface
['vnf'] == vnf
['name']:
3089 for vnf_interface
in vnf
['interfaces']:
3090 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3091 vnf_interface
['ip_address'] = interface
['ip_address']
3093 # logger.debug(">>>>>>>> Merged dictionary")
3094 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3095 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3097 # 1. Creating new nets (sce_nets) in the VIM"
3098 number_mgmt_networks
= 0
3099 db_instance_nets
= []
3100 for sce_net
in scenarioDict
['nets']:
3101 # get involved datacenters where this network need to be created
3102 involved_datacenters
= []
3103 for sce_vnf
in scenarioDict
.get("vnfs"):
3104 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3105 if vnf_datacenter
in involved_datacenters
:
3107 if sce_vnf
.get("interfaces"):
3108 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3109 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3110 involved_datacenters
.append(vnf_datacenter
)
3114 if instance_dict
.get("networks") and instance_dict
["networks"].get(sce_net
["name"]):
3115 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3116 net_name
= descriptor_net
.get("vim-network-name")
3117 sce_net2instance
[sce_net
['uuid']] = {}
3118 net2task_id
['scenario'][sce_net
['uuid']] = {}
3120 if sce_net
["external"]:
3121 number_mgmt_networks
+= 1
3123 for datacenter_id
in involved_datacenters
:
3125 netmap_create
= None
3126 if descriptor_net
.get("sites"):
3127 for site
in descriptor_net
["sites"]:
3128 if site
.get("datacenter") == datacenter_id
:
3129 netmap_use
= site
.get("netmap-use")
3130 netmap_create
= site
.get("netmap-create")
3133 vim
= myvims
[datacenter_id
]
3134 myvim_thread_id
= myvim_threads_id
[datacenter_id
]
3136 net_type
= sce_net
['type']
3137 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3140 if sce_net
["external"]:
3141 net_name
= sce_net
["name"]
3143 net_name
= "{}-{}".format(instance_name
, sce_net
["name"])
3144 net_name
= net_name
[:255] # limit length
3146 if netmap_use
or netmap_create
:
3147 create_network
= False
3148 lookfor_network
= False
3150 lookfor_network
= True
3151 if utils
.check_valid_uuid(netmap_use
):
3152 lookfor_filter
["id"] = netmap_use
3154 lookfor_filter
["name"] = netmap_use
3156 create_network
= True
3157 net_vim_name
= net_name
3158 if isinstance(netmap_create
, str):
3159 net_vim_name
= netmap_create
3160 elif sce_net
.get("vim_network_name"):
3161 create_network
= False
3162 lookfor_network
= True
3163 lookfor_filter
["name"] = sce_net
.get("vim_network_name")
3164 elif sce_net
["external"]:
3165 if sce_net
['vim_id'] is not None:
3166 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3167 create_network
= False
3168 lookfor_network
= True
3169 lookfor_filter
["id"] = sce_net
['vim_id']
3170 elif vim
["config"].get("management_network_id") or vim
["config"].get("management_network_name"):
3171 if number_mgmt_networks
> 1:
3172 raise NfvoException("Found several VLD of type mgmt. "
3173 "You must concrete what vim-network must be use for each one",
3175 create_network
= False
3176 lookfor_network
= True
3177 if vim
["config"].get("management_network_id"):
3178 lookfor_filter
["id"] = vim
["config"]["management_network_id"]
3180 lookfor_filter
["name"] = vim
["config"]["management_network_name"]
3182 # There is not a netmap, look at datacenter for a net with this name and create if not found
3183 create_network
= True
3184 lookfor_network
= True
3185 lookfor_filter
["name"] = sce_net
["name"]
3186 net_vim_name
= sce_net
["name"]
3188 net_vim_name
= net_name
3189 create_network
= True
3190 lookfor_network
= False
3194 task_action
= "CREATE"
3195 task_extra
["params"] = (net_vim_name
, net_type
, sce_net
.get('ip_profile', None))
3197 task_extra
["find"] = (lookfor_filter
,)
3198 elif lookfor_network
:
3199 task_action
= "FIND"
3200 task_extra
["params"] = (lookfor_filter
,)
3202 # fill database content
3203 net_uuid
= str(uuid4())
3204 uuid_list
.append(net_uuid
)
3205 sce_net2instance
[sce_net
['uuid']][datacenter_id
] = net_uuid
3209 "instance_scenario_id": instance_uuid
,
3210 "sce_net_id": sce_net
["uuid"],
3211 "created": create_network
,
3212 'datacenter_id': datacenter_id
,
3213 'datacenter_tenant_id': myvim_thread_id
,
3214 'status': 'BUILD' # if create_network else "ACTIVE"
3216 db_instance_nets
.append(db_net
)
3218 "instance_action_id": instance_action_id
,
3219 "status": "SCHEDULED",
3220 "task_index": task_index
,
3221 "datacenter_vim_id": myvim_thread_id
,
3222 "action": task_action
,
3223 "item": "instance_nets",
3224 "item_id": net_uuid
,
3225 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3227 net2task_id
['scenario'][sce_net
['uuid']][datacenter_id
] = task_index
3229 db_vim_actions
.append(db_vim_action
)
3231 if 'ip_profile' in sce_net
:
3233 'instance_net_id': net_uuid
,
3234 'ip_version': sce_net
['ip_profile']['ip_version'],
3235 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3236 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3237 'dns_address': sce_net
['ip_profile']['dns_address'],
3238 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3239 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3240 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3242 db_ip_profiles
.append(db_ip_profile
)
3246 "default_datacenter_id": default_datacenter_id
,
3247 "myvim_threads_id": myvim_threads_id
,
3248 "instance_uuid": instance_uuid
,
3249 "instance_name": instance_name
,
3250 "instance_action_id": instance_action_id
,
3252 "cloud_config": cloud_config
,
3253 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3256 "task_index": task_index
,
3257 "uuid_list": uuid_list
,
3258 "db_instance_nets": db_instance_nets
,
3259 "db_vim_actions": db_vim_actions
,
3260 "db_ip_profiles": db_ip_profiles
,
3261 "db_instance_vnfs": db_instance_vnfs
,
3262 "db_instance_vms": db_instance_vms
,
3263 "db_instance_interfaces": db_instance_interfaces
,
3264 "net2task_id": net2task_id
,
3265 "sce_net2instance": sce_net2instance
,
3267 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3268 for sce_vnf
in scenarioDict
['vnfs']: # sce_vnf_list:
3269 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3270 task_index
= vnf_params_out
["task_index"]
3271 uuid_list
= vnf_params_out
["uuid_list"]
3274 # task_depends_on = []
3275 for vnffg
in scenarioDict
['vnffgs']:
3276 for rsp
in vnffg
['rsps']:
3278 for cp
in rsp
['connection_points']:
3279 count
= mydb
.get_rows(
3280 SELECT
=('vms.count'),
3281 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id",
3282 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3283 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3284 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3286 for instance_vm
in instance_vms
:
3287 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3289 dependencies
.append(action
['task_index'])
3290 # TODO: throw exception if count != len(instance_vms)
3291 # TODO: and action shouldn't ever be None
3293 for i
in range(count
):
3295 sfi_uuid
= str(uuid4())
3296 uuid_list
.append(sfi_uuid
)
3299 "instance_scenario_id": instance_uuid
,
3300 'sce_rsp_hop_id': cp
['uuid'],
3301 'datacenter_id': datacenter_id
,
3302 'datacenter_tenant_id': myvim_thread_id
,
3303 "vim_sfi_id": None, # vim thread will populate
3305 db_instance_sfis
.append(db_sfi
)
3307 "instance_action_id": instance_action_id
,
3308 "task_index": task_index
,
3309 "datacenter_vim_id": myvim_thread_id
,
3311 "status": "SCHEDULED",
3312 "item": "instance_sfis",
3313 "item_id": sfi_uuid
,
3314 "extra": yaml
.safe_dump({"params": "", "depends_on": [dependencies
[i
]]},
3315 default_flow_style
=True, width
=256)
3317 sfis_created
.append(task_index
)
3319 db_vim_actions
.append(db_vim_action
)
3321 sf_uuid
= str(uuid4())
3322 uuid_list
.append(sf_uuid
)
3325 "instance_scenario_id": instance_uuid
,
3326 'sce_rsp_hop_id': cp
['uuid'],
3327 'datacenter_id': datacenter_id
,
3328 'datacenter_tenant_id': myvim_thread_id
,
3329 "vim_sf_id": None, # vim thread will populate
3331 db_instance_sfs
.append(db_sf
)
3333 "instance_action_id": instance_action_id
,
3334 "task_index": task_index
,
3335 "datacenter_vim_id": myvim_thread_id
,
3337 "status": "SCHEDULED",
3338 "item": "instance_sfs",
3340 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3341 default_flow_style
=True, width
=256)
3343 sfs_created
.append(task_index
)
3345 db_vim_actions
.append(db_vim_action
)
3346 classifier
= rsp
['classifier']
3348 # TODO the following ~13 lines can be reused for the sfi case
3349 count
= mydb
.get_rows(
3350 SELECT
=('vms.count'),
3351 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3352 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3353 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3354 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3356 for instance_vm
in instance_vms
:
3357 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3359 dependencies
.append(action
['task_index'])
3360 # TODO: throw exception if count != len(instance_vms)
3361 # TODO: and action shouldn't ever be None
3362 classifications_created
= []
3363 for i
in range(count
):
3364 for match
in classifier
['matches']:
3365 # create classifications
3366 classification_uuid
= str(uuid4())
3367 uuid_list
.append(classification_uuid
)
3368 db_classification
= {
3369 "uuid": classification_uuid
,
3370 "instance_scenario_id": instance_uuid
,
3371 'sce_classifier_match_id': match
['uuid'],
3372 'datacenter_id': datacenter_id
,
3373 'datacenter_tenant_id': myvim_thread_id
,
3374 "vim_classification_id": None, # vim thread will populate
3376 db_instance_classifications
.append(db_classification
)
3377 classification_params
= {
3378 "ip_proto": match
["ip_proto"],
3379 "source_ip": match
["source_ip"],
3380 "destination_ip": match
["destination_ip"],
3381 "source_port": match
["source_port"],
3382 "destination_port": match
["destination_port"]
3385 "instance_action_id": instance_action_id
,
3386 "task_index": task_index
,
3387 "datacenter_vim_id": myvim_thread_id
,
3389 "status": "SCHEDULED",
3390 "item": "instance_classifications",
3391 "item_id": classification_uuid
,
3392 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3393 default_flow_style
=True, width
=256)
3395 classifications_created
.append(task_index
)
3397 db_vim_actions
.append(db_vim_action
)
3400 sfp_uuid
= str(uuid4())
3401 uuid_list
.append(sfp_uuid
)
3404 "instance_scenario_id": instance_uuid
,
3405 'sce_rsp_id': rsp
['uuid'],
3406 'datacenter_id': datacenter_id
,
3407 'datacenter_tenant_id': myvim_thread_id
,
3408 "vim_sfp_id": None, # vim thread will populate
3410 db_instance_sfps
.append(db_sfp
)
3412 "instance_action_id": instance_action_id
,
3413 "task_index": task_index
,
3414 "datacenter_vim_id": myvim_thread_id
,
3416 "status": "SCHEDULED",
3417 "item": "instance_sfps",
3418 "item_id": sfp_uuid
,
3419 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3420 default_flow_style
=True, width
=256)
3423 db_vim_actions
.append(db_vim_action
)
3425 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3427 db_instance_action
["number_tasks"] = task_index
3428 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3429 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3431 {"instance_scenarios": db_instance_scenario
},
3432 {"instance_vnfs": db_instance_vnfs
},
3433 {"instance_nets": db_instance_nets
},
3434 {"ip_profiles": db_ip_profiles
},
3435 {"instance_vms": db_instance_vms
},
3436 {"instance_interfaces": db_instance_interfaces
},
3437 {"instance_actions": db_instance_action
},
3438 {"instance_sfis": db_instance_sfis
},
3439 {"instance_sfs": db_instance_sfs
},
3440 {"instance_classifications": db_instance_classifications
},
3441 {"instance_sfps": db_instance_sfps
},
3442 {"vim_actions": db_vim_actions
}
3445 logger
.debug("create_instance done DB tables: %s",
3446 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3447 mydb
.new_rows(db_tables
, uuid_list
)
3448 for myvim_thread_id
in myvim_threads_id
.values():
3449 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3451 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3452 returned_instance
["action_id"] = instance_action_id
3453 return returned_instance
3454 except (NfvoException
, vimconn
.vimconnException
, db_base_Exception
) as e
:
3455 message
= rollback(mydb
, myvims
, rollbackList
)
3456 if isinstance(e
, db_base_Exception
):
3457 error_text
= "database Exception"
3458 elif isinstance(e
, vimconn
.vimconnException
):
3459 error_text
= "VIM Exception"
3461 error_text
= "Exception"
3462 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3463 # logger.error("create_instance: %s", error_text)
3464 raise NfvoException(error_text
, e
.http_code
)
3467 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3468 default_datacenter_id
= params
["default_datacenter_id"]
3469 myvim_threads_id
= params
["myvim_threads_id"]
3470 instance_uuid
= params
["instance_uuid"]
3471 instance_name
= params
["instance_name"]
3472 instance_action_id
= params
["instance_action_id"]
3473 myvims
= params
["myvims"]
3474 cloud_config
= params
["cloud_config"]
3475 RO_pub_key
= params
["RO_pub_key"]
3477 task_index
= params_out
["task_index"]
3478 uuid_list
= params_out
["uuid_list"]
3479 db_instance_nets
= params_out
["db_instance_nets"]
3480 db_vim_actions
= params_out
["db_vim_actions"]
3481 db_ip_profiles
= params_out
["db_ip_profiles"]
3482 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3483 db_instance_vms
= params_out
["db_instance_vms"]
3484 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3485 net2task_id
= params_out
["net2task_id"]
3486 sce_net2instance
= params_out
["sce_net2instance"]
3488 vnf_net2instance
= {}
3490 # 2. Creating new nets (vnf internal nets) in the VIM"
3491 # For each vnf net, we create it and we add it to instanceNetlist.
3492 if sce_vnf
.get("datacenter"):
3493 datacenter_id
= sce_vnf
["datacenter"]
3494 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3496 datacenter_id
= default_datacenter_id
3497 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3498 for net
in sce_vnf
['nets']:
3500 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3501 # net_name = descriptor_net.get("name")
3504 net_name
= "{}-{}".format(instance_name
, net
["name"])
3505 net_name
= net_name
[:255] # limit length
3506 net_type
= net
['type']
3508 if sce_vnf
['uuid'] not in vnf_net2instance
:
3509 vnf_net2instance
[sce_vnf
['uuid']] = {}
3510 if sce_vnf
['uuid'] not in net2task_id
:
3511 net2task_id
[sce_vnf
['uuid']] = {}
3512 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3514 # fill database content
3515 net_uuid
= str(uuid4())
3516 uuid_list
.append(net_uuid
)
3517 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3521 "instance_scenario_id": instance_uuid
,
3522 "net_id": net
["uuid"],
3524 'datacenter_id': datacenter_id
,
3525 'datacenter_tenant_id': myvim_thread_id
,
3527 db_instance_nets
.append(db_net
)
3529 if net
.get("vim-network-name"):
3530 lookfor_filter
= {"name": net
["vim-network-name"]}
3531 task_action
= "FIND"
3532 task_extra
= {"params": (lookfor_filter
,)}
3534 task_action
= "CREATE"
3535 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3538 "instance_action_id": instance_action_id
,
3539 "task_index": task_index
,
3540 "datacenter_vim_id": myvim_thread_id
,
3541 "status": "SCHEDULED",
3542 "action": task_action
,
3543 "item": "instance_nets",
3544 "item_id": net_uuid
,
3545 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3548 db_vim_actions
.append(db_vim_action
)
3550 if 'ip_profile' in net
:
3552 'instance_net_id': net_uuid
,
3553 'ip_version': net
['ip_profile']['ip_version'],
3554 'subnet_address': net
['ip_profile']['subnet_address'],
3555 'gateway_address': net
['ip_profile']['gateway_address'],
3556 'dns_address': net
['ip_profile']['dns_address'],
3557 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3558 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3559 'dhcp_count': net
['ip_profile']['dhcp_count'],
3561 db_ip_profiles
.append(db_ip_profile
)
3563 # print "vnf_net2instance:"
3564 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3566 # 3. Creating new vm instances in the VIM
3567 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3569 if sce_vnf
.get('mgmt_access'):
3570 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3571 vnf_availability_zones
= []
3572 for vm
in sce_vnf
['vms']:
3573 vm_av
= vm
.get('availability_zone')
3574 if vm_av
and vm_av
not in vnf_availability_zones
:
3575 vnf_availability_zones
.append(vm_av
)
3577 # check if there is enough availability zones available at vim level.
3578 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3579 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3580 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
3582 if sce_vnf
.get("datacenter"):
3583 vim
= myvims
[sce_vnf
["datacenter"]]
3584 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3585 datacenter_id
= sce_vnf
["datacenter"]
3587 vim
= myvims
[default_datacenter_id
]
3588 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3589 datacenter_id
= default_datacenter_id
3590 sce_vnf
["datacenter_id"] = datacenter_id
3593 vnf_uuid
= str(uuid4())
3594 uuid_list
.append(vnf_uuid
)
3597 'instance_scenario_id': instance_uuid
,
3598 'vnf_id': sce_vnf
['vnf_id'],
3599 'sce_vnf_id': sce_vnf
['uuid'],
3600 'datacenter_id': datacenter_id
,
3601 'datacenter_tenant_id': myvim_thread_id
,
3603 db_instance_vnfs
.append(db_instance_vnf
)
3605 for vm
in sce_vnf
['vms']:
3607 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3608 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3609 myVMDict
['description'] = myVMDict
['name'][0:99]
3611 # myVMDict['start'] = "no"
3612 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3613 myVMDict
['name'] = vm
["instance_parameters"].get("name")
3614 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
3615 # create image at vim in case it not exist
3616 image_uuid
= vm
['image_id']
3617 if vm
.get("image_list"):
3618 for alternative_image
in vm
["image_list"]:
3619 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
3620 image_uuid
= alternative_image
['image_id']
3622 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
3623 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
3624 vm
['vim_image_id'] = image_id
3626 # create flavor at vim in case it not exist
3627 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
3628 if flavor_dict
['extended'] != None:
3629 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'])
3630 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
3632 # Obtain information for additional disks
3633 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
3634 WHERE
={'vim_id': flavor_id
})
3635 if not extended_flavor_dict
:
3636 raise NfvoException("flavor '{}' not found".format(flavor_id
), HTTP_Not_Found
)
3638 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3639 myVMDict
['disks'] = None
3640 extended_info
= extended_flavor_dict
[0]['extended']
3641 if extended_info
!= None:
3642 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
3643 if 'disks' in extended_flavor_dict_yaml
:
3644 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
3645 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
3646 for disk
in myVMDict
['disks']:
3647 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
3648 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
3650 vm
['vim_flavor_id'] = flavor_id
3651 myVMDict
['imageRef'] = vm
['vim_image_id']
3652 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
3653 myVMDict
['availability_zone'] = vm
.get('availability_zone')
3654 myVMDict
['networks'] = []
3655 task_depends_on
= []
3656 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3658 for iface
in vm
['interfaces']:
3660 if iface
['type'] == "data":
3661 netDict
['type'] = iface
['model']
3662 elif "model" in iface
and iface
["model"] != None:
3663 netDict
['model'] = iface
['model']
3664 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3665 # is obtained from iterface table model
3666 # discover type of interface looking at flavor
3667 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
3668 for flavor_iface
in numa
.get('interfaces', []):
3669 if flavor_iface
.get('name') == iface
['internal_name']:
3670 if flavor_iface
['dedicated'] == 'yes':
3671 netDict
['type'] = "PF" # passthrough
3672 elif flavor_iface
['dedicated'] == 'no':
3673 netDict
['type'] = "VF" # siov
3674 elif flavor_iface
['dedicated'] == 'yes:sriov':
3675 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
3676 netDict
["mac_address"] = flavor_iface
.get("mac_address")
3678 netDict
["use"] = iface
['type']
3679 if netDict
["use"] == "data" and not netDict
.get("type"):
3680 # print "netDict", netDict
3681 # print "iface", iface
3682 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3683 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
3684 if flavor_dict
.get('extended') == None:
3685 raise NfvoException(e_text
+ "After database migration some information is not available. \
3686 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
3688 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
3689 if netDict
["use"] == "mgmt" or netDict
["use"] == "bridge":
3690 netDict
["type"] = "virtual"
3691 if iface
.get("vpci"):
3692 netDict
['vpci'] = iface
['vpci']
3693 if iface
.get("mac"):
3694 netDict
['mac_address'] = iface
['mac']
3695 if iface
.get("ip_address"):
3696 netDict
['ip_address'] = iface
['ip_address']
3697 if iface
.get("port-security") is not None:
3698 netDict
['port_security'] = iface
['port-security']
3699 if iface
.get("floating-ip") is not None:
3700 netDict
['floating_ip'] = iface
['floating-ip']
3701 netDict
['name'] = iface
['internal_name']
3702 if iface
['net_id'] is None:
3703 for vnf_iface
in sce_vnf
["interfaces"]:
3706 if vnf_iface
['interface_id'] == iface
['uuid']:
3707 netDict
['net_id'] = "TASK-{}".format(
3708 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3709 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
3710 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3713 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3714 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
3715 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3716 # skip bridge ifaces not connected to any net
3717 if 'net_id' not in netDict
or netDict
['net_id'] == None:
3719 myVMDict
['networks'].append(netDict
)
3722 # 'instance_vm_id': instance_vm_uuid,
3723 "instance_net_id": instance_net_id
,
3724 'interface_id': iface
['uuid'],
3725 # 'vim_interface_id': ,
3726 'type': 'external' if iface
['external_name'] is not None else 'internal',
3727 'ip_address': iface
.get('ip_address'),
3728 'mac_address': iface
.get('mac'),
3729 'floating_ip': int(iface
.get('floating-ip', False)),
3730 'port_security': int(iface
.get('port-security', True))
3732 db_vm_ifaces
.append(db_vm_iface
)
3733 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3734 # print myVMDict['name']
3735 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3736 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3737 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3739 # We add the RO key to cloud_config if vnf will need ssh access
3740 cloud_config_vm
= cloud_config
3741 if ssh_access
and ssh_access
['required'] and ssh_access
['default-user'] and tenant
[0].get('RO_pub_key'):
3742 RO_key
= {"key-pairs": [tenant
[0]['RO_pub_key']]}
3743 cloud_config_vm
= unify_cloud_config(cloud_config_vm
, RO_key
)
3744 if vm
.get("boot_data"):
3745 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
3747 if myVMDict
.get('availability_zone'):
3748 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3751 for vm_index
in range(0, vm
.get('count', 1)):
3752 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
3753 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
3754 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
3755 myVMDict
['disks'], av_index
, vnf_availability_zones
)
3756 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3757 for net
in myVMDict
['networks']:
3759 for iface
in vm
['interfaces']:
3760 if net
["name"] == iface
["internal_name"]:
3761 iface
["vim_id"] = net
["vim_id"]
3763 vm_uuid
= str(uuid4())
3764 uuid_list
.append(vm_uuid
)
3767 'instance_vnf_id': vnf_uuid
,
3768 # TODO delete "vim_vm_id": vm_id,
3769 "vm_id": vm
["uuid"],
3770 "vim_name": vm_name
,
3773 db_instance_vms
.append(db_vm
)
3776 for db_vm_iface
in db_vm_ifaces
:
3777 iface_uuid
= str(uuid4())
3778 uuid_list
.append(iface_uuid
)
3779 db_vm_iface_instance
= {
3781 "instance_vm_id": vm_uuid
3783 db_vm_iface_instance
.update(db_vm_iface
)
3784 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3785 ip
= db_vm_iface_instance
.get("ip_address")
3790 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
3791 db_vm_iface_instance
["ip_address"] = ip
3793 db_vm_iface_instance
["ip_address"] = None
3794 db_instance_interfaces
.append(db_vm_iface_instance
)
3795 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
3799 "instance_action_id": instance_action_id
,
3800 "task_index": task_index
,
3801 "datacenter_vim_id": myvim_thread_id
,
3803 "status": "SCHEDULED",
3804 "item": "instance_vms",
3806 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
3807 default_flow_style
=True, width
=256)
3810 db_vim_actions
.append(db_vim_action
)
3811 params_out
["task_index"] = task_index
3812 params_out
["uuid_list"] = uuid_list
3815 def delete_instance(mydb
, tenant_id
, instance_id
):
3816 # print "Checking that the instance_id exists and getting the instance dictionary"
3817 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3818 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3819 tenant_id
= instanceDict
["tenant_id"]
3820 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3821 # 1. Delete from Database
3822 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3824 # 2. delete from VIM
3828 vimthread_affected
= {}
3829 net2vm_dependencies
= {}
3832 instance_action_id
= get_task_id()
3834 db_instance_action
= {
3835 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3836 "tenant_id": tenant_id
,
3837 "instance_id": instance_id
,
3838 "description": "DELETE",
3839 # "number_tasks": 0 # filled bellow
3844 for sce_vnf
in instanceDict
['vnfs']:
3845 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3846 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
3847 if datacenter_key
not in myvims
:
3849 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3850 except NfvoException
as e
:
3851 logger
.error(str(e
))
3853 myvim_threads
[datacenter_key
] = myvim_thread
3854 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
3855 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
3857 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
3858 sce_vnf
["datacenter_tenant_id"]))
3859 myvims
[datacenter_key
] = None
3861 myvims
[datacenter_key
] = vims
.values()[0]
3862 myvim
= myvims
[datacenter_key
]
3863 myvim_thread
= myvim_threads
[datacenter_key
]
3864 for vm
in sce_vnf
['vms']:
3866 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3869 "instance_action_id": instance_action_id
,
3870 "task_index": task_index
,
3871 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
3873 "status": "SCHEDULED",
3874 "item": "instance_vms",
3875 "item_id": vm
["uuid"],
3876 "extra": yaml
.safe_dump({"params": vm
["interfaces"]},
3877 default_flow_style
=True, width
=256)
3879 db_vim_actions
.append(db_vim_action
)
3880 for interface
in vm
["interfaces"]:
3881 if not interface
.get("instance_net_id"):
3883 if interface
["instance_net_id"] not in net2vm_dependencies
:
3884 net2vm_dependencies
[interface
["instance_net_id"]] = []
3885 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
3890 for net
in instanceDict
['nets']:
3891 vimthread_affected
[net
["datacenter_tenant_id"]] = None
3892 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
3893 if datacenter_key
not in myvims
:
3895 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3896 except NfvoException
as e
:
3897 logger
.error(str(e
))
3899 myvim_threads
[datacenter_key
] = myvim_thread
3900 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
3901 datacenter_tenant_id
=net
["datacenter_tenant_id"])
3903 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
3904 myvims
[datacenter_key
] = None
3906 myvims
[datacenter_key
] = vims
.values()[0]
3907 myvim
= myvims
[datacenter_key
]
3908 myvim_thread
= myvim_threads
[datacenter_key
]
3911 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
3913 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
3914 if net2vm_dependencies
.get(net
["uuid"]):
3915 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
3917 "instance_action_id": instance_action_id
,
3918 "task_index": task_index
,
3919 "datacenter_vim_id": net
["datacenter_tenant_id"],
3921 "status": "SCHEDULED",
3922 "item": "instance_nets",
3923 "item_id": net
["uuid"],
3924 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
3927 db_vim_actions
.append(db_vim_action
)
3929 # 2.3 deleting VNFFGs
3931 for sfp
in instanceDict
.get('sfps', ()):
3932 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
3933 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
3934 if datacenter_key
not in myvims
:
3936 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
3937 except NfvoException
as e
:
3938 logger
.error(str(e
))
3940 myvim_threads
[datacenter_key
] = myvim_thread
3941 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
3942 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
3944 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
3945 myvims
[datacenter_key
] = None
3947 myvims
[datacenter_key
] = vims
.values()[0]
3948 myvim
= myvims
[datacenter_key
]
3949 myvim_thread
= myvim_threads
[datacenter_key
]
3952 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
3954 extra
= {"params": (sfp
['vim_sfp_id'])}
3956 "instance_action_id": instance_action_id
,
3957 "task_index": task_index
,
3958 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
3960 "status": "SCHEDULED",
3961 "item": "instance_sfps",
3962 "item_id": sfp
["uuid"],
3963 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
3966 db_vim_actions
.append(db_vim_action
)
3968 for sf
in instanceDict
.get('sfs', ()):
3969 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
3970 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
3971 if datacenter_key
not in myvims
:
3973 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
3974 except NfvoException
as e
:
3975 logger
.error(str(e
))
3977 myvim_threads
[datacenter_key
] = myvim_thread
3978 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
3979 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
3981 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
3982 myvims
[datacenter_key
] = None
3984 myvims
[datacenter_key
] = vims
.values()[0]
3985 myvim
= myvims
[datacenter_key
]
3986 myvim_thread
= myvim_threads
[datacenter_key
]
3989 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
3991 extra
= {"params": (sf
['vim_sf_id'])}
3993 "instance_action_id": instance_action_id
,
3994 "task_index": task_index
,
3995 "datacenter_vim_id": sf
["datacenter_tenant_id"],
3997 "status": "SCHEDULED",
3998 "item": "instance_sfs",
3999 "item_id": sf
["uuid"],
4000 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4003 db_vim_actions
.append(db_vim_action
)
4005 for sfi
in instanceDict
.get('sfis', ()):
4006 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4007 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4008 if datacenter_key
not in myvims
:
4010 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4011 except NfvoException
as e
:
4012 logger
.error(str(e
))
4014 myvim_threads
[datacenter_key
] = myvim_thread
4015 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4016 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4018 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4019 myvims
[datacenter_key
] = None
4021 myvims
[datacenter_key
] = vims
.values()[0]
4022 myvim
= myvims
[datacenter_key
]
4023 myvim_thread
= myvim_threads
[datacenter_key
]
4026 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4028 extra
= {"params": (sfi
['vim_sfi_id'])}
4030 "instance_action_id": instance_action_id
,
4031 "task_index": task_index
,
4032 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4034 "status": "SCHEDULED",
4035 "item": "instance_sfis",
4036 "item_id": sfi
["uuid"],
4037 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4040 db_vim_actions
.append(db_vim_action
)
4042 for classification
in instanceDict
['classifications']:
4043 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4044 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4045 if datacenter_key
not in myvims
:
4047 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4048 except NfvoException
as e
:
4049 logger
.error(str(e
))
4051 myvim_threads
[datacenter_key
] = myvim_thread
4052 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4053 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4055 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"], classification
["datacenter_tenant_id"]))
4056 myvims
[datacenter_key
] = None
4058 myvims
[datacenter_key
] = vims
.values()[0]
4059 myvim
= myvims
[datacenter_key
]
4060 myvim_thread
= myvim_threads
[datacenter_key
]
4063 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'], classification
["datacenter_id"])
4065 extra
= {"params": (classification
['vim_classification_id'])}
4067 "instance_action_id": instance_action_id
,
4068 "task_index": task_index
,
4069 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4071 "status": "SCHEDULED",
4072 "item": "instance_classifications",
4073 "item_id": classification
["uuid"],
4074 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4077 db_vim_actions
.append(db_vim_action
)
4079 db_instance_action
["number_tasks"] = task_index
4081 {"instance_actions": db_instance_action
},
4082 {"vim_actions": db_vim_actions
}
4085 logger
.debug("delete_instance done DB tables: %s",
4086 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4087 mydb
.new_rows(db_tables
, ())
4088 for myvim_thread_id
in vimthread_affected
.keys():
4089 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4091 if len(error_msg
) > 0:
4092 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4093 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4095 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4097 def get_instance_id(mydb
, tenant_id
, instance_id
):
4099 #check valid tenant_id
4100 check_tenant(mydb
, tenant_id
)
4103 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4104 for net
in instance_dict
["nets"]:
4105 if net
.get("sdn_net_id"):
4106 net_sdn
= ovim
.show_network(net
["sdn_net_id"])
4108 "admin_state_up": net_sdn
.get("admin_state_up"),
4109 "flows": net_sdn
.get("flows"),
4110 "last_error": net_sdn
.get("last_error"),
4111 "ports": net_sdn
.get("ports"),
4112 "type": net_sdn
.get("type"),
4113 "status": net_sdn
.get("status"),
4114 "vlan": net_sdn
.get("vlan"),
4116 return instance_dict
4118 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4119 '''Refreshes a scenario instance. It modifies instanceDict'''
4121 - 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
4124 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4125 # #print "nfvo.refresh_instance begins"
4126 # #print json.dumps(instanceDict, indent=4)
4128 # #print "Getting the VIM URL and the VIM tenant_id"
4131 # # 1. Getting VIM vm and net list
4132 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4135 # for sce_vnf in instanceDict['vnfs']:
4136 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4137 # if datacenter_key not in vm_list:
4138 # vm_list[datacenter_key] = []
4139 # if datacenter_key not in myvims:
4140 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4141 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4142 # if len(vims) == 0:
4143 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4144 # myvims[datacenter_key] = None
4146 # myvims[datacenter_key] = vims.values()[0]
4147 # for vm in sce_vnf['vms']:
4148 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4149 # vms_notupdated.append(vm["uuid"])
4151 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4152 # nets_notupdated=[]
4154 # for net in instanceDict['nets']:
4155 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4156 # if datacenter_key not in net_list:
4157 # net_list[datacenter_key] = []
4158 # if datacenter_key not in myvims:
4159 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4160 # datacenter_tenant_id=net["datacenter_tenant_id"])
4161 # if len(vims) == 0:
4162 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4163 # myvims[datacenter_key] = None
4165 # myvims[datacenter_key] = vims.values()[0]
4167 # net_list[datacenter_key].append(net['vim_net_id'])
4168 # nets_notupdated.append(net["uuid"])
4170 # # 1. Getting the status of all VMs
4172 # for datacenter_key in myvims:
4173 # if not vm_list.get(datacenter_key):
4177 # if not myvims[datacenter_key]:
4178 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4181 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4183 # except vimconn.vimconnException as e:
4184 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4185 # failed_message = str(e)
4187 # for vm in vm_list[datacenter_key]:
4188 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4190 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4191 # for sce_vnf in instanceDict['vnfs']:
4192 # for vm in sce_vnf['vms']:
4193 # vm_id = vm['vim_vm_id']
4194 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4195 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4196 # has_mgmt_iface = False
4197 # for iface in vm["interfaces"]:
4198 # if iface["type"]=="mgmt":
4199 # has_mgmt_iface = True
4200 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4201 # vm_dict[vm_id]['status'] = "ACTIVE"
4202 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4203 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4204 # 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'):
4205 # vm['status'] = vm_dict[vm_id]['status']
4206 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4207 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4208 # # 2.1. Update in openmano DB the VMs whose status changed
4210 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4211 # vms_notupdated.remove(vm["uuid"])
4213 # vms_updated.append(vm["uuid"])
4214 # except db_base_Exception as e:
4215 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4216 # # 2.2. Update in openmano DB the interface VMs
4217 # for interface in interfaces:
4218 # #translate from vim_net_id to instance_net_id
4219 # network_id_list=[]
4220 # for net in instanceDict['nets']:
4221 # if net["vim_net_id"] == interface["vim_net_id"]:
4222 # network_id_list.append(net["uuid"])
4223 # if not network_id_list:
4225 # del interface["vim_net_id"]
4227 # for network_id in network_id_list:
4228 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4229 # except db_base_Exception as e:
4230 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4232 # # 3. Getting the status of all nets
4234 # for datacenter_key in myvims:
4235 # if not net_list.get(datacenter_key):
4238 # failed_message = ""
4239 # if not myvims[datacenter_key]:
4240 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4243 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4245 # except vimconn.vimconnException as e:
4246 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4247 # failed_message = str(e)
4249 # for net in net_list[datacenter_key]:
4250 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4252 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4253 # # TODO: update nets inside a vnf
4254 # for net in instanceDict['nets']:
4255 # net_id = net['vim_net_id']
4256 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4257 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4258 # 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'):
4259 # net['status'] = net_dict[net_id]['status']
4260 # net['error_msg'] = net_dict[net_id].get('error_msg')
4261 # net['vim_info'] = net_dict[net_id].get('vim_info')
4262 # # 5.1. Update in openmano DB the nets whose status changed
4264 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4265 # nets_notupdated.remove(net["uuid"])
4267 # nets_updated.append(net["uuid"])
4268 # except db_base_Exception as e:
4269 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4271 # # Returns appropriate output
4272 # #print "nfvo.refresh_instance finishes"
4273 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4274 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4275 instance_id
= instanceDict
['uuid']
4276 # if len(vms_notupdated)+len(nets_notupdated)>0:
4277 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4278 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4280 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4282 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4283 #print "Checking that the instance_id exists and getting the instance dictionary"
4284 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4285 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4287 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4288 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4290 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), HTTP_Not_Found
)
4291 myvim
= vims
.values()[0]
4296 myvim_threads_id
= {}
4297 if action_dict
.get("vdu-scaling"):
4298 db_instance_vms
= []
4300 db_instance_interfaces
= []
4301 instance_action_id
= get_task_id()
4302 db_instance_action
= {
4303 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4304 "tenant_id": nfvo_tenant
,
4305 "instance_id": instance_id
,
4306 "description": "SCALE",
4308 vm_result
["instance_action_id"] = instance_action_id
4310 for vdu
in action_dict
["vdu-scaling"]:
4311 vdu_id
= vdu
.get("vdu-id")
4312 osm_vdu_id
= vdu
.get("osm_vdu_id")
4313 member_vnf_index
= vdu
.get("member-vnf-index")
4314 vdu_count
= vdu
.get("count", 1)
4316 target_vm
= mydb
.get_rows(
4317 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4318 WHERE
={"vms.uuid": vdu_id
},
4319 ORDER_BY
="vms.created_at"
4322 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), HTTP_Not_Found
)
4324 if not osm_vdu_id
and not member_vnf_index
:
4325 raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4326 target_vm
= mydb
.get_rows(
4327 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4328 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4329 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4330 " join vms on ivms.vm_id=vms.uuid",
4331 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
},
4332 ORDER_BY
="ivms.created_at"
4335 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), HTTP_Not_Found
)
4336 vdu_id
= target_vm
[-1]["uuid"]
4337 vm_result
[vdu_id
] = {"created": [], "deleted": [], "description": "scheduled"}
4338 target_vm
= target_vm
[-1]
4339 datacenter
= target_vm
["datacenter_id"]
4340 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4341 if vdu
["type"] == "delete":
4343 vm_interfaces
= None
4344 for sce_vnf
in instanceDict
['vnfs']:
4345 for vm
in sce_vnf
['vms']:
4346 if vm
["uuid"] == vdu_id
:
4347 vm_interfaces
= vm
["interfaces"]
4351 "instance_action_id": instance_action_id
,
4352 "task_index": task_index
,
4353 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4355 "status": "SCHEDULED",
4356 "item": "instance_vms",
4357 "item_id": target_vm
["uuid"],
4358 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4359 default_flow_style
=True, width
=256)
4362 db_vim_actions
.append(db_vim_action
)
4363 vm_result
[vdu_id
]["deleted"].append(vdu_id
)
4364 # delete from database
4365 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4367 else: # vdu["type"] == "create":
4369 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4371 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_actions", WHERE
=where
)
4372 if not vim_action_to_clone
:
4373 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), HTTP_Internal_Server_Error
)
4374 vim_action_to_clone
= vim_action_to_clone
[0]
4375 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4377 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4378 # TODO do the same for flavor and image when available
4379 task_depends_on
= []
4380 task_params
= extra
["params"]
4381 task_params_networks
= deepcopy(task_params
[5])
4382 for iface
in task_params
[5]:
4383 if iface
["net_id"].startswith("TASK-"):
4384 if "." not in iface
["net_id"]:
4385 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4386 iface
["net_id"][5:]))
4387 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4388 iface
["net_id"][5:])
4390 task_depends_on
.append(iface
["net_id"][5:])
4391 if "mac_address" in iface
:
4392 del iface
["mac_address"]
4394 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4395 for index
in range(0, vdu_count
):
4396 vm_uuid
= str(uuid4())
4397 vm_name
= target_vm
.get('vim_name')
4399 suffix
= vm_name
.rfind("-")
4400 vm_name
= vm_name
[:suffix
+1] + str(1 + int(vm_name
[suffix
+1:]))
4405 'instance_vnf_id': target_vm
['instance_vnf_id'],
4406 'vm_id': target_vm
['vm_id'],
4409 db_instance_vms
.append(db_instance_vm
)
4411 for vm_iface
in vm_ifaces_to_clone
:
4412 iface_uuid
= str(uuid4())
4413 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4416 'instance_vm_id': vm_uuid
,
4417 "instance_net_id": vm_iface
["instance_net_id"],
4418 'interface_id': vm_iface
['interface_id'],
4419 'type': vm_iface
['type'],
4420 'floating_ip': vm_iface
['floating_ip'],
4421 'port_security': vm_iface
['port_security']
4423 db_instance_interfaces
.append(db_vm_iface
)
4424 task_params_copy
= deepcopy(task_params
)
4425 for iface
in task_params_copy
[5]:
4426 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4427 # increment ip_address
4428 if "ip_address" in iface
:
4429 ip
= iface
.get("ip_address")
4434 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4435 iface
["ip_address"] = ip
4437 iface
["ip_address"] = None
4439 task_params_copy
[0] = vm_name
4441 "instance_action_id": instance_action_id
,
4442 "task_index": task_index
,
4443 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4445 "status": "SCHEDULED",
4446 "item": "instance_vms",
4450 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4453 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4456 db_vim_actions
.append(db_vim_action
)
4457 vm_result
[vdu_id
]["created"].append(vm_uuid
)
4459 db_instance_action
["number_tasks"] = task_index
4461 {"instance_vms": db_instance_vms
},
4462 {"instance_interfaces": db_instance_interfaces
},
4463 {"instance_actions": db_instance_action
},
4465 # {"instance_sfis": db_instance_sfis},
4466 # {"instance_sfs": db_instance_sfs},
4467 # {"instance_classifications": db_instance_classifications},
4468 # {"instance_sfps": db_instance_sfps},
4469 {"vim_actions": db_vim_actions
}
4471 logger
.debug("create_vdu done DB tables: %s",
4472 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4473 mydb
.new_rows(db_tables
, [])
4474 for myvim_thread
in myvim_threads_id
.values():
4475 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4479 input_vnfs
= action_dict
.pop("vnfs", [])
4480 input_vms
= action_dict
.pop("vms", [])
4481 action_over_all
= True if not input_vnfs
and not input_vms
else False
4482 for sce_vnf
in instanceDict
['vnfs']:
4483 for vm
in sce_vnf
['vms']:
4484 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4485 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4486 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
4489 if "add_public_key" in action_dict
:
4491 if sce_vnf
.get('mgmt_access'):
4492 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'])
4493 ssh_access
= mgmt_access
['config-access']['ssh-access']
4494 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4496 if ssh_access
['required'] and ssh_access
['default-user']:
4497 if 'ip_address' in vm
:
4498 mgmt_ip
= vm
['ip_address'].split(';')
4499 password
= mgmt_access
['config-access'].get('password')
4500 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4501 myvim
.inject_user_key(mgmt_ip
[0], ssh_access
['default-user'],
4502 action_dict
['add_public_key'],
4503 password
=password
, ro_key
=priv_RO_key
)
4505 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4506 HTTP_Internal_Server_Error
)
4508 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4509 HTTP_Internal_Server_Error
)
4511 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4512 HTTP_Internal_Server_Error
)
4514 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
4515 if "console" in action_dict
:
4516 if not global_config
["http_console_proxy"]:
4517 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4518 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4519 protocol
=data
["protocol"],
4520 ip
= data
["server"],
4521 port
= data
["port"],
4522 suffix
= data
["suffix"]),
4526 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
4527 vm_result
[ vm
['uuid'] ] = {"vim_result": -HTTP_Unauthorized
,
4528 "description": "this console is only reachable by local interface",
4533 #print "console data", data
4535 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
4536 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4537 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4538 protocol
=data
["protocol"],
4539 ip
= global_config
["http_console_host"],
4540 port
= console_thread
.port
,
4541 suffix
= data
["suffix"]),
4545 except NfvoException
as e
:
4546 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4550 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
4552 except vimconn
.vimconnException
as e
:
4553 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4556 if vm_ok
==0: #all goes wrong
4561 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
4563 if nfvo_tenant
and nfvo_tenant
!= "any":
4564 filter["tenant_id"] = nfvo_tenant
4565 if instance_id
and instance_id
!= "any":
4566 filter["instance_id"] = instance_id
4568 filter["uuid"] = action_id
4569 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
4572 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found
)
4573 vim_actions
= mydb
.get_rows(FROM
="vim_actions", WHERE
={"instance_action_id": action_id
})
4574 rows
[0]["vim_actions"] = vim_actions
4575 return {"actions": rows
}
4578 def create_or_use_console_proxy_thread(console_server
, console_port
):
4579 #look for a non-used port
4580 console_thread_key
= console_server
+ ":" + str(console_port
)
4581 if console_thread_key
in global_config
["console_thread"]:
4582 #global_config["console_thread"][console_thread_key].start_timeout()
4583 return global_config
["console_thread"][console_thread_key
]
4585 for port
in global_config
["console_port_iterator"]():
4586 #print "create_or_use_console_proxy_thread() port:", port
4587 if port
in global_config
["console_ports"]:
4590 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
4592 global_config
["console_thread"][console_thread_key
] = clithread
4593 global_config
["console_ports"][port
] = console_thread_key
4595 except cli
.ConsoleProxyExceptionPortUsed
as e
:
4596 #port used, try with onoher
4598 except cli
.ConsoleProxyException
as e
:
4599 raise NfvoException(str(e
), HTTP_Bad_Request
)
4600 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict
)
4603 def check_tenant(mydb
, tenant_id
):
4604 '''check that tenant exists at database'''
4605 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
4607 raise NfvoException("tenant '{}' not found".format(tenant_id
), HTTP_Not_Found
)
4610 def new_tenant(mydb
, tenant_dict
):
4612 tenant_uuid
= str(uuid4())
4613 tenant_dict
['uuid'] = tenant_uuid
4615 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
4616 tenant_dict
['RO_pub_key'] = pub_key
4617 tenant_dict
['encrypted_RO_priv_key'] = priv_key
4618 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
4619 except db_base_Exception
as e
:
4620 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
4623 def delete_tenant(mydb
, tenant
):
4624 #get nfvo_tenant info
4626 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
4627 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
4628 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
4631 def new_datacenter(mydb
, datacenter_descriptor
):
4632 sdn_port_mapping
= None
4633 if "config" in datacenter_descriptor
:
4634 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
4635 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
4637 # Check that datacenter-type is correct
4638 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
4639 # module_info = None
4641 module
= "vimconn_" + datacenter_type
4642 pkg
= __import__("osm_ro." + module
)
4643 # vim_conn = getattr(pkg, module)
4644 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
4645 except (IOError, ImportError):
4646 # if module_info and module_info[0]:
4647 # file.close(module_info[0])
4648 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type
,
4652 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
4653 if sdn_port_mapping
:
4655 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
4656 except Exception as e
:
4657 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
4659 return datacenter_id
4662 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
4663 # obtain data, check that only one exist
4664 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
4667 datacenter_id
= datacenter
['uuid']
4668 where
={'uuid': datacenter
['uuid']}
4669 remove_port_mapping
= False
4670 new_sdn_port_mapping
= None
4671 if "config" in datacenter_descriptor
:
4672 if datacenter_descriptor
['config'] != None:
4674 new_config_dict
= datacenter_descriptor
["config"]
4675 if "sdn-port-mapping" in new_config_dict
:
4676 remove_port_mapping
= True
4677 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
4680 for k
in new_config_dict
:
4681 if new_config_dict
[k
] == None:
4683 if k
== 'sdn-controller':
4684 remove_port_mapping
= True
4686 config_text
= datacenter
.get("config")
4689 config_dict
= yaml
.load(config_text
)
4690 config_dict
.update(new_config_dict
)
4694 except Exception as e
:
4695 raise NfvoException("Bad format at datacenter:config " + str(e
), HTTP_Bad_Request
)
4697 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
4699 datacenter_descriptor
["config"] = None
4700 if remove_port_mapping
:
4702 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
4703 except ovimException
as e
:
4704 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
4706 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
4707 if new_sdn_port_mapping
:
4709 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
4710 except ovimException
as e
:
4711 logger
.error("Error adding datacenter-port-mapping " + str(e
))
4712 return datacenter_id
4715 def delete_datacenter(mydb
, datacenter
):
4716 #get nfvo_tenant info
4717 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
4718 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
4720 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
4721 except ovimException
as e
:
4722 logger
.error("Error deleting datacenter-port-mapping " + str(e
))
4723 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
4726 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):
4727 # get datacenter info
4729 datacenter_id
= get_datacenter_uuid(mydb
, None, datacenter
)
4731 create_vim_tenant
= True if not vim_tenant_id
and not vim_tenant_name
else False
4733 # get nfvo_tenant info
4734 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
4735 if vim_tenant_name
==None:
4736 vim_tenant_name
=tenant_dict
['name']
4738 #check that this association does not exist before
4739 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
4740 tenants_datacenters
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
4741 if len(tenants_datacenters
)>0:
4742 raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Conflict
)
4744 vim_tenant_id_exist_atdb
=False
4745 if not create_vim_tenant
:
4746 where_
={"datacenter_id": datacenter_id
}
4747 if vim_tenant_id
!=None:
4748 where_
["vim_tenant_id"] = vim_tenant_id
4749 if vim_tenant_name
!=None:
4750 where_
["vim_tenant_name"] = vim_tenant_name
4751 #check if vim_tenant_id is already at database
4752 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
4753 if len(datacenter_tenants_dict
)>=1:
4754 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
4755 vim_tenant_id_exist_atdb
=True
4756 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4758 datacenter_tenants_dict
= {}
4759 #insert at table datacenter_tenants
4760 else: #if vim_tenant_id==None:
4761 #create tenant at VIM if not provided
4763 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
,
4764 vim_passwd
=vim_password
)
4765 datacenter_name
= myvim
["name"]
4766 vim_tenant_id
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
4767 except vimconn
.vimconnException
as e
:
4768 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), HTTP_Internal_Server_Error
)
4769 datacenter_tenants_dict
= {}
4770 datacenter_tenants_dict
["created"]="true"
4772 #fill datacenter_tenants table
4773 if not vim_tenant_id_exist_atdb
:
4774 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant_id
4775 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
4776 datacenter_tenants_dict
["user"] = vim_username
4777 datacenter_tenants_dict
["passwd"] = vim_password
4778 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
4780 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
4781 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
4782 datacenter_tenants_dict
["uuid"] = id_
4784 #fill tenants_datacenters table
4785 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
4786 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
4787 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
4789 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_dict
['uuid'], datacenter_id
) # reload data
4790 datacenter_name
= myvim
["name"]
4791 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
4792 new_thread
= vim_thread
.vim_thread(myvim
, task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
4793 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
4795 thread_id
= datacenter_tenants_dict
["uuid"]
4796 vim_threads
["running"][thread_id
] = new_thread
4797 return datacenter_id
4798 except vimconn
.vimconnException
as e
:
4799 raise NfvoException(str(e
), HTTP_Bad_Request
)
4802 def edit_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=None, vim_tenant_name
=None,
4803 vim_username
=None, vim_password
=None, config
=None):
4804 #Obtain the data of this datacenter_tenant_id
4805 vim_data
= mydb
.get_rows(
4806 SELECT
=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user",
4807 "datacenter_tenants.passwd", "datacenter_tenants.config"),
4808 FROM
="datacenter_tenants JOIN tenants_datacenters ON datacenter_tenants.uuid=tenants_datacenters.datacenter_tenant_id",
4809 WHERE
={"tenants_datacenters.nfvo_tenant_id": nfvo_tenant
,
4810 "tenants_datacenters.datacenter_id": datacenter_id
})
4812 logger
.debug(str(vim_data
))
4813 if len(vim_data
) < 1:
4814 raise NfvoException("Datacenter {} is not attached for tenant {}".format(datacenter_id
, nfvo_tenant
), HTTP_Conflict
)
4818 v
['config'] = yaml
.load(v
['config'])
4821 v
['vim_tenant_id'] = vim_tenant_id
4823 v
['vim_tenant_name'] = vim_tenant_name
4825 v
['user'] = vim_username
4827 v
['passwd'] = vim_password
4831 v
['config'].update(config
)
4833 logger
.debug(str(v
))
4834 deassociate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'])
4835 associate_datacenter_to_tenant(mydb
, nfvo_tenant
, datacenter_id
, vim_tenant_id
=v
['vim_tenant_id'], vim_tenant_name
=v
['vim_tenant_name'],
4836 vim_username
=v
['user'], vim_password
=v
['passwd'], config
=v
['config'])
4838 return datacenter_id
4840 def deassociate_datacenter_to_tenant(mydb
, tenant_id
, datacenter
, vim_tenant_id
=None):
4841 #get nfvo_tenant info
4842 if not tenant_id
or tenant_id
=="any":
4845 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
4846 tenant_uuid
= tenant_dict
['uuid']
4848 datacenter_id
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
4849 #check that this association exist before
4850 tenants_datacenter_dict
={"datacenter_id": datacenter_id
}
4852 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
4853 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
4854 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
4855 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Not_Found
)
4857 #delete this association
4858 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
4860 #get vim_tenant info and deletes
4862 for tenant_datacenter_item
in tenant_datacenter_list
:
4863 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
4864 #try to delete vim:tenant
4866 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
4867 if vim_tenant_dict
['created']=='true':
4868 #delete tenant at VIM if created by NFVO
4870 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4871 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
4872 except vimconn
.vimconnException
as e
:
4873 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
4874 logger
.warn(warning
)
4875 except db_base_Exception
as e
:
4876 logger
.error("Cannot delete datacenter_tenants " + str(e
))
4877 pass # the error will be caused because dependencies, vim_tenant can not be deleted
4878 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
4879 thread
= vim_threads
["running"].get(thread_id
)
4881 thread
.insert_task("exit")
4882 vim_threads
["deleting"][thread_id
] = thread
4883 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
4886 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
4888 #get datacenter info
4889 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4891 if 'net-update' in action_dict
:
4893 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
4895 except vimconn
.vimconnException
as e
:
4896 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
4897 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
4898 #update nets Change from VIM format to NFVO format
4901 net_nfvo
={'datacenter_id': datacenter_id
}
4902 net_nfvo
['name'] = net
['name']
4903 #net_nfvo['description']= net['name']
4904 net_nfvo
['vim_net_id'] = net
['id']
4905 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4906 net_nfvo
['shared'] = net
['shared']
4907 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
4908 net_list
.append(net_nfvo
)
4909 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
4910 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
4912 elif 'net-edit' in action_dict
:
4913 net
= action_dict
['net-edit'].pop('net')
4914 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
4915 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
4916 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
4918 elif 'net-delete' in action_dict
:
4919 net
= action_dict
['net-deelte'].get('net')
4920 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
4921 result
= mydb
.delete_row(FROM
='datacenter_nets',
4922 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
4926 raise NfvoException("Unknown action " + str(action_dict
), HTTP_Bad_Request
)
4929 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
4930 #get datacenter info
4931 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4933 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
4934 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
4935 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
4939 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
4940 #get datacenter info
4941 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4944 action_dict
= action_dict
["netmap"]
4945 if 'vim_id' in action_dict
:
4946 filter_dict
["id"] = action_dict
['vim_id']
4947 if 'vim_name' in action_dict
:
4948 filter_dict
["name"] = action_dict
['vim_name']
4950 filter_dict
["shared"] = True
4953 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
4954 except vimconn
.vimconnException
as e
:
4955 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
4956 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
4957 if len(vim_nets
)>1 and action_dict
:
4958 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict
)
4959 elif len(vim_nets
)==0: # and action_dict:
4960 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), HTTP_Not_Found
)
4962 for net
in vim_nets
:
4963 net_nfvo
={'datacenter_id': datacenter_id
}
4964 if action_dict
and "name" in action_dict
:
4965 net_nfvo
['name'] = action_dict
['name']
4967 net_nfvo
['name'] = net
['name']
4968 #net_nfvo['description']= net['name']
4969 net_nfvo
['vim_net_id'] = net
['id']
4970 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4971 net_nfvo
['shared'] = net
['shared']
4972 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
4974 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
4975 net_nfvo
["status"] = "OK"
4976 net_nfvo
["uuid"] = net_id
4977 except db_base_Exception
as e
:
4981 net_nfvo
["status"] = "FAIL: " + str(e
)
4982 net_list
.append(net_nfvo
)
4985 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
4986 # obtain all network data
4988 if utils
.check_valid_uuid(network_id
):
4989 filter_dict
= {"id": network_id
}
4991 filter_dict
= {"name": network_id
}
4993 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4994 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
4995 except vimconn
.vimconnException
as e
:
4996 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
4998 # ensure the network is defined
4999 if len(network
) == 0:
5000 raise NfvoException("Network {} is not present in the system".format(network_id
),
5003 # ensure there is only one network with the provided name
5004 if len(network
) > 1:
5005 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), HTTP_Bad_Request
)
5007 # ensure it is a dataplane network
5008 if network
[0]['type'] != 'data':
5011 # ensure we use the id
5012 network_id
= network
[0]['id']
5014 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5015 # and with instance_scenario_id==NULL
5016 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5017 search_dict
= {'vim_net_id': network_id
}
5020 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5021 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5022 except db_base_Exception
as e
:
5023 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5024 network_id
) + str(e
), e
.http_code
)
5028 if net
['sdn_net_id'] != None:
5030 sdn_net_id
= net
['sdn_net_id']
5032 if sdn_net_counter
== 0:
5034 elif sdn_net_counter
== 1:
5037 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5038 network_id
), HTTP_Internal_Server_Error
)
5040 def get_sdn_controller_id(mydb
, datacenter
):
5041 # Obtain sdn controller id
5042 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5046 return yaml
.load(config
).get('sdn-controller')
5048 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5050 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5051 if not sdn_network_id
:
5052 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), HTTP_Internal_Server_Error
)
5054 #Obtain sdn controller id
5055 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5056 if not controller_id
:
5057 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), HTTP_Internal_Server_Error
)
5059 #Obtain sdn controller info
5060 sdn_controller
= ovim
.show_of_controller(controller_id
)
5063 'name': 'external_port',
5064 'net_id': sdn_network_id
,
5065 'ofc_id': controller_id
,
5066 'switch_dpid': sdn_controller
['dpid'],
5067 'switch_port': descriptor
['port']
5070 if 'vlan' in descriptor
:
5071 port_data
['vlan'] = descriptor
['vlan']
5072 if 'mac' in descriptor
:
5073 port_data
['mac'] = descriptor
['mac']
5075 result
= ovim
.new_port(port_data
)
5076 except ovimException
as e
:
5077 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5078 sdn_network_id
, network_id
) + str(e
), HTTP_Internal_Server_Error
)
5079 except db_base_Exception
as e
:
5080 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5081 network_id
) + str(e
), e
.http_code
)
5083 return 'Port uuid: '+ result
5085 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5087 filter = {'uuid': port_id
}
5089 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5090 if not sdn_network_id
:
5091 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5092 HTTP_Internal_Server_Error
)
5093 #in case no port_id is specified only ports marked as 'external_port' will be detached
5094 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5097 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5098 except ovimException
as e
:
5099 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5100 HTTP_Internal_Server_Error
)
5102 if len(port_list
) == 0:
5103 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5107 for port
in port_list
:
5109 port_uuid_list
.append(port
['uuid'])
5110 ovim
.delete_port(port
['uuid'])
5111 except ovimException
as e
:
5112 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), HTTP_Internal_Server_Error
)
5114 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5116 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5117 #get datacenter info
5118 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5121 if utils
.check_valid_uuid(name
):
5122 filter_dict
["id"] = name
5124 filter_dict
["name"] = name
5126 if item
=="networks":
5127 #filter_dict['tenant_id'] = myvim['tenant_id']
5128 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5130 if len(content
) == 0:
5131 raise NfvoException("Network {} is not present in the system. ".format(name
),
5134 #Update the networks with the attached ports
5136 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5137 if sdn_network_id
!= None:
5139 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5140 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5141 except ovimException
as e
:
5142 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), HTTP_Internal_Server_Error
)
5143 #Remove field name and if port name is external_port save it as 'type'
5144 for port
in port_list
:
5145 if port
['name'] == 'external_port':
5146 port
['type'] = "External"
5148 net
['sdn_network_id'] = sdn_network_id
5149 net
['sdn_attached_ports'] = port_list
5151 elif item
=="tenants":
5152 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5153 elif item
== "images":
5155 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5157 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5158 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5159 if name
and len(content
)==1:
5160 return {item
[:-1]: content
[0]}
5161 elif name
and len(content
)==0:
5162 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
5165 return {item
: content
}
5166 except vimconn
.vimconnException
as e
:
5167 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
5168 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5171 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5172 #get datacenter info
5173 if tenant_id
== "any":
5176 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5178 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5179 logger
.debug("vim_action_delete vim response: " + str(content
))
5180 items
= content
.values()[0]
5181 if type(items
)==list and len(items
)==0:
5182 raise NfvoException("Not found " + item
, HTTP_Not_Found
)
5183 elif type(items
)==list and len(items
)>1:
5184 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), HTTP_Not_Found
)
5185 else: # it is a dict
5186 item_id
= items
["id"]
5187 item_name
= str(items
.get("name"))
5190 if item
=="networks":
5191 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5192 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5193 if sdn_network_id
!= None:
5194 #Delete any port attachment to this network
5196 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5197 except ovimException
as e
:
5198 raise NfvoException(
5199 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5200 HTTP_Internal_Server_Error
)
5202 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5203 for port
in port_list
:
5204 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5206 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5208 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
5209 except db_base_Exception
as e
:
5210 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
5211 str(e
), e
.http_code
)
5213 #Delete the SDN network
5215 ovim
.delete_network(sdn_network_id
)
5216 except ovimException
as e
:
5217 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5218 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5219 HTTP_Internal_Server_Error
)
5221 content
= myvim
.delete_network(item_id
)
5222 elif item
=="tenants":
5223 content
= myvim
.delete_tenant(item_id
)
5224 elif item
== "images":
5225 content
= myvim
.delete_image(item_id
)
5227 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5228 except vimconn
.vimconnException
as e
:
5229 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5230 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5232 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5235 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5236 #get datacenter info
5237 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5238 if tenant_id
== "any":
5240 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5242 if item
=="networks":
5243 net
= descriptor
["network"]
5244 net_name
= net
.pop("name")
5245 net_type
= net
.pop("type", "bridge")
5246 net_public
= net
.pop("shared", False)
5247 net_ipprofile
= net
.pop("ip_profile", None)
5248 net_vlan
= net
.pop("vlan", None)
5249 content
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
5251 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5252 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5253 #obtain datacenter_tenant_id
5254 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5255 FROM
='datacenter_tenants',
5256 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5259 sdn_network
['vlan'] = net_vlan
5260 sdn_network
['type'] = net_type
5261 sdn_network
['name'] = net_name
5262 sdn_network
['region'] = datacenter_tenant_id
5263 ovim_content
= ovim
.new_network(sdn_network
)
5264 except ovimException
as e
:
5265 logger
.error("ovimException creating SDN network={} ".format(
5266 sdn_network
) + str(e
), exc_info
=True)
5267 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5268 HTTP_Internal_Server_Error
)
5270 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5271 # use instance_scenario_id=None to distinguish from real instaces of nets
5272 correspondence
= {'instance_scenario_id': None,
5273 'sdn_net_id': ovim_content
,
5274 'vim_net_id': content
,
5275 'datacenter_tenant_id': datacenter_tenant_id
5278 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5279 except db_base_Exception
as e
:
5280 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5281 correspondence
, e
), e
.http_code
)
5282 elif item
=="tenants":
5283 tenant
= descriptor
["tenant"]
5284 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5286 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5287 except vimconn
.vimconnException
as e
:
5288 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5290 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5292 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5293 data
= ovim
.new_of_controller(sdn_controller
)
5294 logger
.debug('New SDN controller created with uuid {}'.format(data
))
5297 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5298 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5299 msg
= 'SDN controller {} updated'.format(data
)
5303 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5304 if controller_id
== None:
5305 data
= ovim
.get_of_controllers()
5307 data
= ovim
.show_of_controller(controller_id
)
5309 msg
= 'SDN controller list:\n {}'.format(data
)
5313 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5314 select_
= ('uuid', 'config')
5315 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5316 for datacenter
in datacenters
:
5317 if datacenter
['config']:
5318 config
= yaml
.load(datacenter
['config'])
5319 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5320 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), HTTP_Conflict
)
5322 data
= ovim
.delete_of_controller(controller_id
)
5323 msg
= 'SDN controller {} deleted'.format(data
)
5327 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5328 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5329 if len(controller
) < 1:
5330 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), HTTP_Not_Found
)
5333 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
5335 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), HTTP_Bad_Request
)
5337 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5338 switch_dpid
= sdn_controller
["dpid"]
5341 for compute_node
in sdn_port_mapping
:
5342 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5344 element
["compute_node"] = compute_node
["compute_node"]
5345 for port
in compute_node
["ports"]:
5346 pci
= port
.get("pci")
5347 element
["switch_port"] = port
.get("switch_port")
5348 element
["switch_mac"] = port
.get("switch_mac")
5349 if not pci
or not (element
["switch_port"] or element
["switch_mac"]):
5350 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
5351 " or 'switch_mac'", HTTP_Bad_Request
)
5352 for pci_expanded
in utils
.expand_brackets(pci
):
5353 element
["pci"] = pci_expanded
5354 maps
.append(dict(element
))
5356 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
5358 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5359 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
5362 "sdn-controller": None,
5363 "datacenter-id": datacenter_id
,
5365 "ports_mapping": list()
5368 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5369 if datacenter
['config']:
5370 config
= yaml
.load(datacenter
['config'])
5371 if 'sdn-controller' in config
:
5372 controller_id
= config
['sdn-controller']
5373 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5374 result
["sdn-controller"] = controller_id
5375 result
["dpid"] = sdn_controller
["dpid"]
5377 if result
["sdn-controller"] == None:
5378 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), HTTP_Bad_Request
)
5379 if result
["dpid"] == None:
5380 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5381 HTTP_Internal_Server_Error
)
5386 ports_correspondence_dict
= dict()
5388 if result
["sdn-controller"] != link
["ofc_id"]:
5389 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error
)
5390 if result
["dpid"] != link
["switch_dpid"]:
5391 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error
)
5393 element
["pci"] = link
["pci"]
5394 if link
["switch_port"]:
5395 element
["switch_port"] = link
["switch_port"]
5396 if link
["switch_mac"]:
5397 element
["switch_mac"] = link
["switch_mac"]
5399 if not link
["compute_node"] in ports_correspondence_dict
:
5401 content
["compute_node"] = link
["compute_node"]
5402 content
["ports"] = list()
5403 ports_correspondence_dict
[link
["compute_node"]] = content
5405 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
5407 for key
in sorted(ports_correspondence_dict
):
5408 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5412 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5413 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})
5415 def create_RO_keypair(tenant_id
):
5417 Creates a public / private keys for a RO tenant and returns their values
5419 tenant_id: ID of the tenant
5421 public_key: Public key for the RO tenant
5422 private_key: Encrypted private key for RO tenant
5426 key
= RSA
.generate(bits
)
5428 public_key
= key
.publickey().exportKey('OpenSSH')
5429 if isinstance(public_key
, ValueError):
5430 raise NfvoException("Unable to create public key: {}".format(public_key
), HTTP_Internal_Server_Error
)
5431 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5432 except (ValueError, NameError) as e
:
5433 raise NfvoException("Unable to create private key: {}".format(e
), HTTP_Internal_Server_Error
)
5434 return public_key
, private_key
5436 def decrypt_key (key
, tenant_id
):
5438 Decrypts an encrypted RSA key
5440 key: Private key to be decrypted
5441 tenant_id: ID of the tenant
5443 unencrypted_key: Unencrypted private key for RO tenant
5446 key
= RSA
.importKey(key
,tenant_id
)
5447 unencrypted_key
= key
.exportKey('PEM')
5448 if isinstance(unencrypted_key
, ValueError):
5449 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), HTTP_Internal_Server_Error
)
5450 except ValueError as e
:
5451 raise NfvoException("Unable to decrypt the private key: {}".format(e
), HTTP_Internal_Server_Error
)
5452 return unencrypted_key