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(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("mgmt-interface"):
1090 db_interface
["type"] = "mgmt"
1092 if iface
.get("external-connection-point-ref"):
1094 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1095 db_interface
["external_name"] = get_str(cp
, "name", 255)
1096 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1097 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1098 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1099 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1100 if cp_descriptor
["name"] == db_interface
["external_name"]:
1105 if vdu_id
in vdu_id2cp_name
:
1106 vdu_id2cp_name
[vdu_id
] = None # more than two connecdtion point for this VDU
1108 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1111 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1112 db_interface
["port_security"] = 0
1113 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1114 db_interface
["port_security"] = 1
1116 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1117 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1118 " at connection-point".format(
1119 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1120 cp
=iface
.get("vnfd-connection-point-ref")),
1122 elif iface
.get("internal-connection-point-ref"):
1124 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1125 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1128 raise KeyError("does not exist at vdu:internal-connection-point")
1131 for vld
in vnfd
.get("internal-vld").itervalues():
1132 for cp
in vld
.get("internal-connection-point").itervalues():
1133 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1135 raise KeyError("is referenced by more than one 'internal-vld'")
1139 raise KeyError("is not referenced by any 'internal-vld'")
1141 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1142 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1143 db_interface
["port_security"] = 0
1144 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1145 db_interface
["port_security"] = 1
1146 if icp
.get("ip-address"):
1147 if not icp_vld
.get("ip-profile-ref"):
1149 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1150 except KeyError as e
:
1151 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1152 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1154 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1155 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1157 if iface
.get("position"):
1158 db_interface
["created_at"] = int(iface
.get("position")) * 50
1159 if iface
.get("mac-address"):
1160 db_interface
["mac"] = str(iface
.get("mac-address"))
1161 db_interfaces
.append(db_interface
)
1165 "name": get_str(vdu
, "name", 250) + "-flv",
1166 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1167 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1168 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1170 # TODO revise the case of several numa-node-policy node
1174 extended
["devices"] = devices
1175 if flavor_epa_interfaces
:
1176 numa
["interfaces"] = flavor_epa_interfaces
1177 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1178 epa_vcpu_set
= False
1179 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1180 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1181 if numa_node_policy
.get("node"):
1182 numa_node
= numa_node_policy
["node"].values()[0]
1183 if numa_node
.get("num-cores"):
1184 numa
["cores"] = numa_node
["num-cores"]
1186 if numa_node
.get("paired-threads"):
1187 if numa_node
["paired-threads"].get("num-paired-threads"):
1188 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1190 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1191 numa
["paired-threads-id"] = []
1192 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues():
1193 numa
["paired-threads-id"].append(
1194 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1196 if numa_node
.get("num-threads"):
1197 numa
["threads"] = int(numa_node
["num-threads"])
1199 if numa_node
.get("memory-mb"):
1200 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1201 if vdu
["guest-epa"].get("mempage-size"):
1202 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1203 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1204 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1205 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1206 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1207 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1208 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1210 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1212 extended
["numas"] = [numa
]
1214 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1215 db_flavor
["extended"] = extended_text
1216 # look if flavor exist
1217 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1218 'ram': db_flavor
.get('ram'),
1219 'vcpus': db_flavor
.get('vcpus'),
1220 'extended': db_flavor
.get('extended')
1222 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1223 if existing_flavors
:
1224 flavor_uuid
= existing_flavors
[0]["uuid"]
1226 flavor_uuid
= str(uuid4())
1227 uuid_list
.append(flavor_uuid
)
1228 db_flavor
["uuid"] = flavor_uuid
1229 db_flavors
.append(db_flavor
)
1230 db_vm
["flavor_id"] = flavor_uuid
1232 # VNF affinity and antiaffinity
1233 for pg
in vnfd
.get("placement-groups").itervalues():
1234 pg_name
= get_str(pg
, "name", 255)
1235 for vdu
in pg
.get("member-vdus").itervalues():
1236 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1237 if vdu_id
not in vdu_id2db_table_index
:
1238 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1239 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1240 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1242 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1243 # TODO consider the case of isolation and not colocation
1244 # if pg.get("strategy") == "ISOLATION":
1246 # VNF mgmt configuration
1248 if vnfd
["mgmt-interface"].get("vdu-id"):
1249 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1250 if mgmt_vdu_id
not in vdu_id2uuid
:
1251 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1252 "'{vdu}'. Reference to a non-existing vdu".format(
1253 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1255 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1256 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1257 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1258 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1260 if vnfd
["mgmt-interface"].get("ip-address"):
1261 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1262 if vnfd
["mgmt-interface"].get("cp"):
1263 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1264 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
1265 "Reference to a non-existing connection-point".format(
1266 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1268 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1269 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1270 # mark this interface as of type mgmt
1271 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1273 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1277 mgmt_access
["default_user"] = default_user
1278 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1281 mgmt_access
["required"] = required
1284 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1286 db_vnfs
.append(db_vnf
)
1290 {"images": db_images
},
1291 {"flavors": db_flavors
},
1292 {"ip_profiles": db_ip_profiles
},
1294 {"interfaces": db_interfaces
},
1297 logger
.debug("create_vnf Deployment done vnfDict: %s",
1298 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1299 mydb
.new_rows(db_tables
, uuid_list
)
1300 return vnfd_uuid_list
1301 except NfvoException
:
1303 except Exception as e
:
1304 logger
.error("Exception {}".format(e
))
1305 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
1308 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1309 global global_config
1311 # Step 1. Check the VNF descriptor
1312 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1313 # Step 2. Check tenant exist
1315 if tenant_id
!= "any":
1316 check_tenant(mydb
, tenant_id
)
1317 if "tenant_id" in vnf_descriptor
["vnf"]:
1318 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1319 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1322 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1323 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1324 if global_config
["auto_push_VNF_to_VIMs"]:
1325 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1327 # Step 4. Review the descriptor and add missing fields
1328 #print vnf_descriptor
1329 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1330 vnf_name
= vnf_descriptor
['vnf']['name']
1331 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1332 if "physical" in vnf_descriptor
['vnf']:
1333 del vnf_descriptor
['vnf']['physical']
1334 #print vnf_descriptor
1336 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1337 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1338 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1340 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1341 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1342 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1344 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1345 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1347 VNFCitem
["name"] = vnfc
['name']
1348 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1349 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1351 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1354 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1355 myflavorDict
["description"] = VNFCitem
["description"]
1356 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1357 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1358 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1359 myflavorDict
["extended"] = {}
1361 devices
= vnfc
.get("devices")
1363 myflavorDict
["extended"]["devices"] = devices
1366 # 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
1367 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1369 # Previous code has been commented
1370 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1371 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1372 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1373 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1375 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1377 # print "Error creating flavor: unknown processor model. Rollback successful."
1378 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1380 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1381 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1383 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1384 myflavorDict
['extended']['numas'] = vnfc
['numas']
1388 # Step 6.2 New flavors are created in the VIM
1389 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1391 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1392 VNFCitem
["flavor_id"] = flavor_id
1393 VNFCDict
[vnfc
['name']] = VNFCitem
1395 logger
.debug("Creating new images in the VIM for each VNFC")
1396 # Step 6.3 New images are created in the VIM
1397 #For each VNFC, we must create the appropriate image.
1398 #This "for" loop might be integrated with the previous one
1399 #In case this integration is made, the VNFCDict might become a VNFClist.
1400 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1401 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1403 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1404 image_dict
['universal_name']=vnfc
.get('image name')
1405 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1406 image_dict
['location']=vnfc
.get('VNFC image')
1407 #image_dict['new_location']=vnfc.get('image location')
1408 image_dict
['checksum']=vnfc
.get('image checksum')
1409 image_metadata_dict
= vnfc
.get('image metadata', None)
1410 image_metadata_str
= None
1411 if image_metadata_dict
is not None:
1412 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1413 image_dict
['metadata']=image_metadata_str
1414 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1415 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1416 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1417 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1418 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1419 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1420 if vnfc
.get("boot-data"):
1421 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1424 # Step 7. Storing the VNF descriptor in the repository
1425 if "descriptor" not in vnf_descriptor
["vnf"]:
1426 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1428 # Step 8. Adding the VNF to the NFVO DB
1429 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1431 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1432 _
, message
= rollback(mydb
, vims
, rollback_list
)
1433 if isinstance(e
, db_base_Exception
):
1434 error_text
= "Exception at database"
1435 elif isinstance(e
, KeyError):
1436 error_text
= "KeyError exception "
1437 e
.http_code
= HTTP_Internal_Server_Error
1439 error_text
= "Exception at VIM"
1440 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1441 #logger.error("start_scenario %s", error_text)
1442 raise NfvoException(error_text
, e
.http_code
)
1445 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1446 global global_config
1448 # Step 1. Check the VNF descriptor
1449 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1450 # Step 2. Check tenant exist
1452 if tenant_id
!= "any":
1453 check_tenant(mydb
, tenant_id
)
1454 if "tenant_id" in vnf_descriptor
["vnf"]:
1455 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1456 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1459 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1460 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1461 if global_config
["auto_push_VNF_to_VIMs"]:
1462 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1464 # Step 4. Review the descriptor and add missing fields
1465 #print vnf_descriptor
1466 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1467 vnf_name
= vnf_descriptor
['vnf']['name']
1468 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1469 if "physical" in vnf_descriptor
['vnf']:
1470 del vnf_descriptor
['vnf']['physical']
1471 #print vnf_descriptor
1473 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1474 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1475 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1477 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1478 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1479 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1481 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1482 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1484 VNFCitem
["name"] = vnfc
['name']
1485 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1487 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1490 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1491 myflavorDict
["description"] = VNFCitem
["description"]
1492 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1493 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1494 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1495 myflavorDict
["extended"] = {}
1497 devices
= vnfc
.get("devices")
1499 myflavorDict
["extended"]["devices"] = devices
1502 # 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
1503 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1505 # Previous code has been commented
1506 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1507 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1508 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1509 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1511 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1513 # print "Error creating flavor: unknown processor model. Rollback successful."
1514 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1516 # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1517 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1519 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1520 myflavorDict
['extended']['numas'] = vnfc
['numas']
1524 # Step 6.2 New flavors are created in the VIM
1525 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1527 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1528 VNFCitem
["flavor_id"] = flavor_id
1529 VNFCDict
[vnfc
['name']] = VNFCitem
1531 logger
.debug("Creating new images in the VIM for each VNFC")
1532 # Step 6.3 New images are created in the VIM
1533 #For each VNFC, we must create the appropriate image.
1534 #This "for" loop might be integrated with the previous one
1535 #In case this integration is made, the VNFCDict might become a VNFClist.
1536 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1537 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1539 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1540 image_dict
['universal_name']=vnfc
.get('image name')
1541 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1542 image_dict
['location']=vnfc
.get('VNFC image')
1543 #image_dict['new_location']=vnfc.get('image location')
1544 image_dict
['checksum']=vnfc
.get('image checksum')
1545 image_metadata_dict
= vnfc
.get('image metadata', None)
1546 image_metadata_str
= None
1547 if image_metadata_dict
is not None:
1548 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1549 image_dict
['metadata']=image_metadata_str
1550 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1551 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1552 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1553 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1554 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1555 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1556 if vnfc
.get("boot-data"):
1557 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1559 # Step 7. Storing the VNF descriptor in the repository
1560 if "descriptor" not in vnf_descriptor
["vnf"]:
1561 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1563 # Step 8. Adding the VNF to the NFVO DB
1564 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1566 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1567 _
, message
= rollback(mydb
, vims
, rollback_list
)
1568 if isinstance(e
, db_base_Exception
):
1569 error_text
= "Exception at database"
1570 elif isinstance(e
, KeyError):
1571 error_text
= "KeyError exception "
1572 e
.http_code
= HTTP_Internal_Server_Error
1574 error_text
= "Exception at VIM"
1575 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1576 #logger.error("start_scenario %s", error_text)
1577 raise NfvoException(error_text
, e
.http_code
)
1580 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1581 #check valid tenant_id
1582 check_tenant(mydb
, tenant_id
)
1585 if tenant_id
!= "any":
1586 where_or
["tenant_id"] = tenant_id
1587 where_or
["public"] = True
1588 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1590 vnf_id
= vnf
["uuid"]
1591 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1592 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1593 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1594 data
={'vnf' : filtered_content
}
1596 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1597 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1599 WHERE
={'vnfs.uuid': vnf_id
} )
1601 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1602 # change boot_data into boot-data
1604 if vm
.get("boot_data"):
1605 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1608 data
['vnf']['VNFC'] = content
1609 #TODO: GET all the information from a VNFC and include it in the output.
1612 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1613 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1614 WHERE
={'vnfs.uuid': vnf_id
} )
1615 data
['vnf']['nets'] = content
1617 #GET ip-profile for each net
1618 for net
in data
['vnf']['nets']:
1619 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1620 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1621 WHERE
={'net_id': net
["uuid"]} )
1622 if len(ipprofiles
)==1:
1623 net
["ip_profile"] = ipprofiles
[0]
1624 elif len(ipprofiles
)>1:
1625 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), HTTP_Bad_Request
)
1628 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1630 #GET External Interfaces
1631 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1632 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1633 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1634 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1636 data
['vnf']['external-connections'] = content
1641 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1642 # Check tenant exist
1643 if tenant_id
!= "any":
1644 check_tenant(mydb
, tenant_id
)
1645 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1646 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1650 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1652 if tenant_id
!= "any":
1653 where_or
["tenant_id"] = tenant_id
1654 where_or
["public"] = True
1655 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1656 vnf_id
= vnf
["uuid"]
1658 # "Getting the list of flavors and tenants of the VNF"
1659 flavorList
= get_flavorlist(mydb
, vnf_id
)
1660 if len(flavorList
)==0:
1661 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1663 imageList
= get_imagelist(mydb
, vnf_id
)
1664 if len(imageList
)==0:
1665 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1667 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1669 raise NfvoException("vnf '{}' not found".format(vnf_id
), HTTP_Not_Found
)
1672 for flavor
in flavorList
:
1673 #check if flavor is used by other vnf
1675 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1677 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1679 #flavor not used, must be deleted
1681 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1682 for flavor_vim
in c
:
1683 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1687 for vim
in vims
.values():
1688 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1694 myvim
.delete_flavor(flavor_vim
["vim_id"])
1695 except vimconn
.vimconnNotFoundException
:
1696 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1697 flavor_vim
["datacenter_vim_id"] )
1698 except vimconn
.vimconnException
as e
:
1699 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1700 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1701 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1702 flavor_vim
["datacenter_vim_id"]))
1703 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1704 mydb
.delete_row_by_id('flavors', flavor
)
1705 except db_base_Exception
as e
:
1706 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1707 undeletedItems
.append("flavor {}".format(flavor
))
1710 for image
in imageList
:
1712 #check if image is used by other vnf
1713 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1715 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1717 #image not used, must be deleted
1719 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1721 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1723 if image_vim
['created']=='false': #skip this image because not created by openmano
1725 myvim
=vims
[ image_vim
["datacenter_id"] ]
1727 myvim
.delete_image(image_vim
["vim_id"])
1728 except vimconn
.vimconnNotFoundException
as e
:
1729 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1730 except vimconn
.vimconnException
as e
:
1731 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1732 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1733 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1734 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1735 mydb
.delete_row_by_id('images', image
)
1736 except db_base_Exception
as e
:
1737 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1738 undeletedItems
.append("image %s" % image
)
1740 return vnf_id
+ " " + vnf
["name"]
1742 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1745 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1746 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1750 return -HTTP_Not_Found
, "datacenter '%s' not found" % datacenter_name
1751 myvim
= vims
.values()[0]
1752 result
,servers
= myvim
.get_hosts_info()
1754 return result
, servers
1755 topology
= {'name':myvim
['name'] , 'servers': servers
}
1756 return result
, topology
1759 def get_hosts(mydb
, nfvo_tenant_id
):
1760 vims
= get_vim(mydb
, nfvo_tenant_id
)
1762 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), HTTP_Not_Found
)
1764 #print "nfvo.datacenter_action() error. Several datacenters found"
1765 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
1766 myvim
= vims
.values()[0]
1768 hosts
= myvim
.get_hosts()
1769 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1771 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1773 server
={'name':host
['name'], 'vms':[]}
1774 for vm
in host
['instances']:
1775 #get internal name and model
1777 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1778 WHERE
={'vim_vm_id':vm
['id']} )
1780 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1782 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1784 except db_base_Exception
as e
:
1785 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1786 datacenter
['Datacenters'][0]['servers'].append(server
)
1787 #return -400, "en construccion"
1789 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1791 except vimconn
.vimconnException
as e
:
1792 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1795 def new_scenario(mydb
, tenant_id
, topo
):
1797 # result, vims = get_vim(mydb, tenant_id)
1799 # return result, vims
1801 if tenant_id
!= "any":
1802 check_tenant(mydb
, tenant_id
)
1803 if "tenant_id" in topo
:
1804 if topo
["tenant_id"] != tenant_id
:
1805 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1810 #1.1: get VNFs and external_networks (other_nets).
1812 other_nets
={} #external_networks, bridge_networks and data_networkds
1813 nodes
= topo
['topology']['nodes']
1814 for k
in nodes
.keys():
1815 if nodes
[k
]['type'] == 'VNF':
1817 vnfs
[k
]['ifaces'] = {}
1818 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1819 other_nets
[k
] = nodes
[k
]
1820 other_nets
[k
]['external']=True
1821 elif nodes
[k
]['type'] == 'network':
1822 other_nets
[k
] = nodes
[k
]
1823 other_nets
[k
]['external']=False
1826 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1827 for name
,vnf
in vnfs
.items():
1828 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1830 error_pos
= "'topology':'nodes':'" + name
+ "'"
1832 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1833 where
['uuid'] = vnf
['vnf_id']
1834 if 'VNF model' in vnf
:
1835 error_text
+= " 'VNF model' " + vnf
['VNF model']
1836 where
['name'] = vnf
['VNF model']
1838 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, HTTP_Bad_Request
)
1840 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1844 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
1846 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
1847 vnf
['uuid']=vnf_db
[0]['uuid']
1848 vnf
['description']=vnf_db
[0]['description']
1849 #get external interfaces
1850 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1851 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1852 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1853 for ext_iface
in ext_ifaces
:
1854 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1856 #1.4 get list of connections
1857 conections
= topo
['topology']['connections']
1858 conections_list
= []
1859 conections_list_name
= []
1860 for k
in conections
.keys():
1861 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1862 ifaces_list
= conections
[k
]['nodes'].items()
1863 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1865 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1866 for k2
in conection_pair_list
:
1869 con_type
= conections
[k
].get("type", "link")
1870 if con_type
!= "link":
1872 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), HTTP_Bad_Request
)
1873 other_nets
[k
] = {'external': False}
1874 if conections
[k
].get("graph"):
1875 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1876 ifaces_list
.append( (k
, None) )
1879 if con_type
== "external_network":
1880 other_nets
[k
]['external'] = True
1881 if conections
[k
].get("model"):
1882 other_nets
[k
]["model"] = conections
[k
]["model"]
1884 other_nets
[k
]["model"] = k
1885 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1886 other_nets
[k
]["model"] = con_type
1888 conections_list_name
.append(k
)
1889 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)
1890 #print set(ifaces_list)
1891 #check valid VNF and iface names
1892 for iface
in ifaces_list
:
1893 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1894 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1895 str(k
), iface
[0]), HTTP_Not_Found
)
1896 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1897 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1898 str(k
), iface
[0], iface
[1]), HTTP_Not_Found
)
1900 #1.5 unify connections from the pair list to a consolidated list
1902 while index
< len(conections_list
):
1904 while index2
< len(conections_list
):
1905 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1906 conections_list
[index
] |
= conections_list
[index2
]
1907 del conections_list
[index2
]
1908 del conections_list_name
[index2
]
1911 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1913 #for k in conections_list:
1918 #1.6 Delete non external nets
1919 # for k in other_nets.keys():
1920 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1921 # for con in conections_list:
1923 # for index in range(0,len(con)):
1924 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1925 # for index in delete_indexes:
1928 #1.7: Check external_ports are present at database table datacenter_nets
1929 for k
,net
in other_nets
.items():
1930 error_pos
= "'topology':'nodes':'" + k
+ "'"
1931 if net
['external']==False:
1932 if 'name' not in net
:
1934 if 'model' not in net
:
1935 raise NfvoException("needed a 'model' at " + error_pos
, HTTP_Bad_Request
)
1936 if net
['model']=='bridge_net':
1937 net
['type']='bridge';
1938 elif net
['model']=='dataplane_net':
1941 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, HTTP_Not_Found
)
1943 #IF we do not want to check that external network exist at datacenter
1948 # if 'net_id' in net:
1949 # error_text += " 'net_id' " + net['net_id']
1950 # WHERE_['uuid'] = net['net_id']
1951 # if 'model' in net:
1952 # error_text += " 'model' " + net['model']
1953 # WHERE_['name'] = net['model']
1954 # if len(WHERE_) == 0:
1955 # return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
1956 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
1957 # FROM='datacenter_nets', WHERE=WHERE_ )
1959 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
1961 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
1962 # return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
1964 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
1965 # return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
1966 # other_nets[k].update(net_db[0])
1969 net_nb
=0 #Number of nets
1970 for con
in conections_list
:
1971 #check if this is connected to a external net
1975 for index
in range(0,len(con
)):
1976 #check if this is connected to a external net
1977 for net_key
in other_nets
.keys():
1978 if con
[index
][0]==net_key
:
1979 if other_net_index
>=0:
1980 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
1981 #print "nfvo.new_scenario " + error_text
1982 raise NfvoException(error_text
, HTTP_Bad_Request
)
1984 other_net_index
= index
1985 net_target
= net_key
1987 #print "other_net_index", other_net_index
1989 if other_net_index
>=0:
1990 del con
[other_net_index
]
1991 #IF we do not want to check that external network exist at datacenter
1992 if other_nets
[net_target
]['external'] :
1993 if "name" not in other_nets
[net_target
]:
1994 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
1995 if other_nets
[net_target
]["type"] == "external_network":
1996 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
1997 other_nets
[net_target
]["type"] = "data"
1999 other_nets
[net_target
]["type"] = "bridge"
2001 # if other_nets[net_target]['external'] :
2002 # 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
2003 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2004 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2005 # print "nfvo.new_scenario " + error_text
2006 # return -HTTP_Bad_Request, error_text
2009 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2012 net_type_bridge
=False
2014 net_target
= "__-__net"+str(net_nb
)
2015 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2016 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
2019 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2020 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2021 if iface_type
=='mgmt' or iface_type
=='bridge':
2022 net_type_bridge
= True
2024 net_type_data
= True
2025 if net_type_bridge
and net_type_data
:
2026 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
2027 #print "nfvo.new_scenario " + error_text
2028 raise NfvoException(error_text
, HTTP_Bad_Request
)
2029 elif net_type_bridge
:
2032 type_
='data' if len(con
)>2 else 'ptp'
2033 net_list
[net_target
]['type'] = type_
2036 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
2037 #print "nfvo.new_scenario " + error_text
2039 raise NfvoException(error_text
, HTTP_Bad_Request
)
2041 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2042 #1.8.1 obtain management net
2043 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2044 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2045 #1.8.2 check all interfaces from all vnfs
2047 add_mgmt_net
= False
2048 for vnf
in vnfs
.values():
2049 for iface
in vnf
['ifaces'].values():
2050 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2051 #iface not connected
2052 iface
['net_key'] = 'mgmt'
2054 if add_mgmt_net
and 'mgmt' not in net_list
:
2055 net_list
['mgmt']=mgmt_net
[0]
2056 net_list
['mgmt']['external']=True
2057 net_list
['mgmt']['graph']={'visible':False}
2059 net_list
.update(other_nets
)
2061 #print 'net_list', net_list
2066 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2067 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2068 'tenant_id':tenant_id
, 'name':topo
['name'],
2069 'description':topo
.get('description',topo
['name']),
2070 'public': topo
.get('public', False)
2076 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2077 """ This creates a new scenario for version 0.2 and 0.3"""
2078 scenario
= scenario_dict
["scenario"]
2079 if tenant_id
!= "any":
2080 check_tenant(mydb
, tenant_id
)
2081 if "tenant_id" in scenario
:
2082 if scenario
["tenant_id"] != tenant_id
:
2083 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2084 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2085 scenario
["tenant_id"], tenant_id
), HTTP_Unauthorized
)
2089 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2090 for name
,vnf
in scenario
["vnfs"].iteritems():
2091 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2093 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2095 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2096 where
['uuid'] = vnf
['vnf_id']
2097 if 'vnf_name' in vnf
:
2098 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2099 where
['name'] = vnf
['vnf_name']
2101 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, HTTP_Bad_Request
)
2102 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2105 if len(vnf_db
) == 0:
2106 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, HTTP_Not_Found
)
2107 elif len(vnf_db
) > 1:
2108 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", HTTP_Conflict
)
2109 vnf
['uuid'] = vnf_db
[0]['uuid']
2110 vnf
['description'] = vnf_db
[0]['description']
2112 # get external interfaces
2113 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2114 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2115 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2116 for ext_iface
in ext_ifaces
:
2117 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2118 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2120 # 2: Insert net_key and ip_address at every vnf interface
2121 for net_name
, net
in scenario
["networks"].items():
2122 net_type_bridge
= False
2123 net_type_data
= False
2124 for iface_dict
in net
["interfaces"]:
2125 if version
== "0.2":
2126 temp_dict
= iface_dict
2128 elif version
== "0.3":
2129 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2130 ip_address
= iface_dict
.get('ip_address', None)
2131 for vnf
, iface
in temp_dict
.items():
2132 if vnf
not in scenario
["vnfs"]:
2133 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2135 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2136 raise NfvoException(error_text
, HTTP_Not_Found
)
2137 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2138 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2139 .format(net_name
, iface
)
2140 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2141 raise NfvoException(error_text
, HTTP_Bad_Request
)
2142 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2143 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2144 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2145 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2146 raise NfvoException(error_text
, HTTP_Bad_Request
)
2147 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2148 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2149 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2150 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2151 net_type_bridge
= True
2153 net_type_data
= True
2155 if net_type_bridge
and net_type_data
:
2156 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2158 # logger.debug("nfvo.new_scenario " + error_text)
2159 raise NfvoException(error_text
, HTTP_Bad_Request
)
2160 elif net_type_bridge
:
2163 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2165 if net
.get("implementation"): # for v0.3
2166 if type_
== "bridge" and net
["implementation"] == "underlay":
2167 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2168 "'network':'{}'".format(net_name
)
2169 # logger.debug(error_text)
2170 raise NfvoException(error_text
, HTTP_Bad_Request
)
2171 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2172 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2173 "'network':'{}'".format(net_name
)
2174 # logger.debug(error_text)
2175 raise NfvoException(error_text
, HTTP_Bad_Request
)
2176 net
.pop("implementation")
2177 if "type" in net
and version
== "0.3": # for v0.3
2178 if type_
== "data" and net
["type"] == "e-line":
2179 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2180 "'e-line' at 'network':'{}'".format(net_name
)
2181 # logger.debug(error_text)
2182 raise NfvoException(error_text
, HTTP_Bad_Request
)
2183 elif type_
== "ptp" and net
["type"] == "e-lan":
2187 net
['name'] = net_name
2188 net
['external'] = net
.get('external', False)
2190 # 3: insert at database
2191 scenario
["nets"] = scenario
["networks"]
2192 scenario
['tenant_id'] = tenant_id
2193 scenario_id
= mydb
.new_scenario(scenario
)
2197 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2199 Parses an OSM IM nsd_catalog and insert at DB
2202 :param nsd_descriptor:
2203 :return: The list of created NSD ids
2206 mynsd
= nsd_catalog
.nsd()
2208 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
2209 except Exception as e
:
2210 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), HTTP_Bad_Request
)
2214 db_sce_interfaces
= []
2217 db_sce_rsp_hops
= []
2218 db_sce_classifiers
= []
2219 db_sce_classifier_matches
= []
2221 db_ip_profiles_index
= 0
2224 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.itervalues():
2225 nsd
= nsd_yang
.get()
2228 scenario_uuid
= str(uuid4())
2229 uuid_list
.append(scenario_uuid
)
2230 nsd_uuid_list
.append(scenario_uuid
)
2232 "uuid": scenario_uuid
,
2233 "osm_id": get_str(nsd
, "id", 255),
2234 "name": get_str(nsd
, "name", 255),
2235 "description": get_str(nsd
, "description", 255),
2236 "tenant_id": tenant_id
,
2237 "vendor": get_str(nsd
, "vendor", 255),
2238 "short_name": get_str(nsd
, "short-name", 255),
2239 "descriptor": str(nsd_descriptor
)[:60000],
2241 db_scenarios
.append(db_scenario
)
2243 # table sce_vnfs (constituent-vnfd)
2244 vnf_index2scevnf_uuid
= {}
2245 vnf_index2vnf_uuid
= {}
2246 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2247 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2248 'tenant_id': tenant_id
})
2249 if not existing_vnf
:
2250 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2251 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2252 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2254 sce_vnf_uuid
= str(uuid4())
2255 uuid_list
.append(sce_vnf_uuid
)
2257 "uuid": sce_vnf_uuid
,
2258 "scenario_id": scenario_uuid
,
2259 # "name": get_str(vnf, "member-vnf-index", 255),
2260 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2261 "vnf_id": existing_vnf
[0]["uuid"],
2262 "member_vnf_index": str(vnf
["member-vnf-index"]),
2263 # TODO 'start-by-default': True
2265 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2266 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2267 db_sce_vnfs
.append(db_sce_vnf
)
2269 # table ip_profiles (ip-profiles)
2270 ip_profile_name2db_table_index
= {}
2271 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2273 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2274 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2275 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2276 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2277 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2278 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2281 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2282 dns_list
.append(str(dns
.get("address")))
2283 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2284 if ip_profile
["ip-profile-params"].get('security-group'):
2285 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2286 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2287 db_ip_profiles_index
+= 1
2288 db_ip_profiles
.append(db_ip_profile
)
2290 # table sce_nets (internal-vld)
2291 for vld
in nsd
.get("vld").itervalues():
2292 sce_net_uuid
= str(uuid4())
2293 uuid_list
.append(sce_net_uuid
)
2295 "uuid": sce_net_uuid
,
2296 "name": get_str(vld
, "name", 255),
2297 "scenario_id": scenario_uuid
,
2299 "multipoint": not vld
.get("type") == "ELINE",
2300 "osm_id": get_str(vld
, "id", 255),
2302 "description": get_str(vld
, "description", 255),
2304 # guess type of network
2305 if vld
.get("mgmt-network"):
2306 db_sce_net
["type"] = "bridge"
2307 db_sce_net
["external"] = True
2308 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2309 db_sce_net
["type"] = "data"
2311 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2312 db_sce_net
["type"] = None
2313 db_sce_nets
.append(db_sce_net
)
2315 # ip-profile, link db_ip_profile with db_sce_net
2316 if vld
.get("ip-profile-ref"):
2317 ip_profile_name
= vld
.get("ip-profile-ref")
2318 if ip_profile_name
not in ip_profile_name2db_table_index
:
2319 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2320 " Reference to a non-existing 'ip_profiles'".format(
2321 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2323 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2324 elif vld
.get("vim-network-name"):
2325 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2327 # table sce_interfaces (vld:vnfd-connection-point-ref)
2328 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2329 vnf_index
= str(iface
['member-vnf-index-ref'])
2330 # check correct parameters
2331 if vnf_index
not in vnf_index2vnf_uuid
:
2332 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2333 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2334 "'nsd':'constituent-vnfd'".format(
2335 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2338 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2339 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2340 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2341 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2343 if not existing_ifaces
:
2344 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2345 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2346 "connection-point name at VNFD '{}'".format(
2347 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2348 str(iface
.get("vnfd-id-ref"))[:255]),
2350 interface_uuid
= existing_ifaces
[0]["uuid"]
2351 if existing_ifaces
[0]["iface_type"] == "data" and not db_sce_net
["type"]:
2352 db_sce_net
["type"] = "data"
2353 sce_interface_uuid
= str(uuid4())
2354 uuid_list
.append(sce_net_uuid
)
2355 iface_ip_address
= None
2356 if iface
.get("ip-address"):
2357 iface_ip_address
= str(iface
.get("ip-address"))
2358 db_sce_interface
= {
2359 "uuid": sce_interface_uuid
,
2360 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2361 "sce_net_id": sce_net_uuid
,
2362 "interface_id": interface_uuid
,
2363 "ip_address": iface_ip_address
,
2365 db_sce_interfaces
.append(db_sce_interface
)
2366 if not db_sce_net
["type"]:
2367 db_sce_net
["type"] = "bridge"
2369 # table sce_vnffgs (vnffgd)
2370 for vnffg
in nsd
.get("vnffgd").itervalues():
2371 sce_vnffg_uuid
= str(uuid4())
2372 uuid_list
.append(sce_vnffg_uuid
)
2374 "uuid": sce_vnffg_uuid
,
2375 "name": get_str(vnffg
, "name", 255),
2376 "scenario_id": scenario_uuid
,
2377 "vendor": get_str(vnffg
, "vendor", 255),
2378 "description": get_str(vld
, "description", 255),
2380 db_sce_vnffgs
.append(db_sce_vnffg
)
2384 for rsp
in vnffg
.get("rsp").itervalues():
2385 sce_rsp_uuid
= str(uuid4())
2386 uuid_list
.append(sce_rsp_uuid
)
2388 "uuid": sce_rsp_uuid
,
2389 "name": get_str(rsp
, "name", 255),
2390 "sce_vnffg_id": sce_vnffg_uuid
,
2391 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2393 db_sce_rsps
.append(db_sce_rsp
)
2394 db_sce_rsp_hops
= []
2395 for iface
in rsp
.get("vnfd-connection-point-ref").itervalues():
2396 vnf_index
= str(iface
['member-vnf-index-ref'])
2397 if_order
= int(iface
['order'])
2398 # check correct parameters
2399 if vnf_index
not in vnf_index2vnf_uuid
:
2400 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2401 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2402 "'nsd':'constituent-vnfd'".format(
2403 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2406 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2407 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2408 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2409 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2411 if not existing_ifaces
:
2412 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2413 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2414 "connection-point name at VNFD '{}'".format(
2415 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2416 str(iface
.get("vnfd-id-ref"))[:255]),
2418 interface_uuid
= existing_ifaces
[0]["uuid"]
2419 sce_rsp_hop_uuid
= str(uuid4())
2420 uuid_list
.append(sce_rsp_hop_uuid
)
2422 "uuid": sce_rsp_hop_uuid
,
2423 "if_order": if_order
,
2424 "interface_id": interface_uuid
,
2425 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2426 "sce_rsp_id": sce_rsp_uuid
,
2428 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2430 # deal with classifiers
2431 db_sce_classifiers
= []
2432 for classifier
in vnffg
.get("classifier").itervalues():
2433 sce_classifier_uuid
= str(uuid4())
2434 uuid_list
.append(sce_classifier_uuid
)
2437 vnf_index
= str(classifier
['member-vnf-index-ref'])
2438 if vnf_index
not in vnf_index2vnf_uuid
:
2439 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2440 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2441 "'nsd':'constituent-vnfd'".format(
2442 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2444 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2445 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2446 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2447 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2449 if not existing_ifaces
:
2450 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2451 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2452 "connection-point name at VNFD '{}'".format(
2453 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2454 str(iface
.get("vnfd-id-ref"))[:255]),
2456 interface_uuid
= existing_ifaces
[0]["uuid"]
2458 db_sce_classifier
= {
2459 "uuid": sce_classifier_uuid
,
2460 "name": get_str(classifier
, "name", 255),
2461 "sce_vnffg_id": sce_vnffg_uuid
,
2462 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2463 "interface_id": interface_uuid
,
2465 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2466 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2467 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2468 db_sce_classifiers
.append(db_sce_classifier
)
2470 db_sce_classifier_matches
= []
2471 for match
in classifier
.get("match-attributes").itervalues():
2472 sce_classifier_match_uuid
= str(uuid4())
2473 uuid_list
.append(sce_classifier_match_uuid
)
2474 db_sce_classifier_match
= {
2475 "uuid": sce_classifier_match_uuid
,
2476 "ip_proto": get_str(match
, "ip-proto", 2),
2477 "source_ip": get_str(match
, "source-ip-address", 16),
2478 "destination_ip": get_str(match
, "destination-ip-address", 16),
2479 "source_port": get_str(match
, "source-port", 5),
2480 "destination_port": get_str(match
, "destination-port", 5),
2481 "sce_classifier_id": sce_classifier_uuid
,
2483 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2486 # remove unneeded id's in sce_rsps
2487 for rsp
in db_sce_rsps
:
2491 {"scenarios": db_scenarios
},
2492 {"sce_nets": db_sce_nets
},
2493 {"ip_profiles": db_ip_profiles
},
2494 {"sce_vnfs": db_sce_vnfs
},
2495 {"sce_interfaces": db_sce_interfaces
},
2496 {"sce_vnffgs": db_sce_vnffgs
},
2497 {"sce_rsps": db_sce_rsps
},
2498 {"sce_rsp_hops": db_sce_rsp_hops
},
2499 {"sce_classifiers": db_sce_classifiers
},
2500 {"sce_classifier_matches": db_sce_classifier_matches
},
2503 logger
.debug("new_nsd_v3 done: %s",
2504 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2505 mydb
.new_rows(db_tables
, uuid_list
)
2506 return nsd_uuid_list
2507 except NfvoException
:
2509 except Exception as e
:
2510 logger
.error("Exception {}".format(e
))
2511 raise # NfvoException("Exception {}".format(e), HTTP_Bad_Request)
2514 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2515 data
["uuid"] = scenario_id
2516 data
["tenant_id"] = tenant_id
2517 c
= mydb
.edit_scenario( data
)
2521 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2522 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2523 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2524 vims
= {datacenter_id
: myvim
}
2525 myvim_tenant
= myvim
['tenant_id']
2526 datacenter_name
= myvim
['name']
2530 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2531 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2532 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2533 scenarioDict
['datacenter_id'] = datacenter_id
2534 #print '================scenarioDict======================='
2535 #print json.dumps(scenarioDict, indent=4)
2536 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2538 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2539 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2541 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2542 auxNetDict
['scenario'] = {}
2544 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2545 for sce_net
in scenarioDict
['nets']:
2546 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2548 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2549 myNetName
= myNetName
[0:255] #limit length
2550 myNetType
= sce_net
['type']
2552 myNetDict
["name"] = myNetName
2553 myNetDict
["type"] = myNetType
2554 myNetDict
["tenant_id"] = myvim_tenant
2555 myNetIPProfile
= sce_net
.get('ip_profile', None)
2557 #We should use the dictionary as input parameter for new_network
2559 if not sce_net
["external"]:
2560 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2561 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2562 sce_net
['vim_id'] = network_id
2563 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2564 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2565 sce_net
["created"] = True
2567 if sce_net
['vim_id'] == None:
2568 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2569 _
, message
= rollback(mydb
, vims
, rollbackList
)
2570 logger
.error("nfvo.start_scenario: %s", error_text
)
2571 raise NfvoException(error_text
, HTTP_Bad_Request
)
2572 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2573 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2575 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2576 #For each vnf net, we create it and we add it to instanceNetlist.
2578 for sce_vnf
in scenarioDict
['vnfs']:
2579 for net
in sce_vnf
['nets']:
2580 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2582 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2583 myNetName
= myNetName
[0:255] #limit length
2584 myNetType
= net
['type']
2586 myNetDict
["name"] = myNetName
2587 myNetDict
["type"] = myNetType
2588 myNetDict
["tenant_id"] = myvim_tenant
2589 myNetIPProfile
= net
.get('ip_profile', None)
2592 #We should use the dictionary as input parameter for new_network
2593 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2594 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2595 net
['vim_id'] = network_id
2596 if sce_vnf
['uuid'] not in auxNetDict
:
2597 auxNetDict
[sce_vnf
['uuid']] = {}
2598 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2599 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2600 net
["created"] = True
2602 #print "auxNetDict:"
2603 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2605 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2606 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2608 for sce_vnf
in scenarioDict
['vnfs']:
2609 vnf_availability_zones
= []
2610 for vm
in sce_vnf
['vms']:
2611 vm_av
= vm
.get('availability_zone')
2612 if vm_av
and vm_av
not in vnf_availability_zones
:
2613 vnf_availability_zones
.append(vm_av
)
2615 # check if there is enough availability zones available at vim level.
2616 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2617 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2618 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
2620 for vm
in sce_vnf
['vms']:
2623 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2624 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2625 #myVMDict['description'] = vm['description']
2626 myVMDict
['description'] = myVMDict
['name'][0:99]
2628 myVMDict
['start'] = "no"
2629 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2630 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2632 #create image at vim in case it not exist
2633 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2634 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2635 vm
['vim_image_id'] = image_id
2637 #create flavor at vim in case it not exist
2638 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2639 if flavor_dict
['extended']!=None:
2640 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2641 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2642 vm
['vim_flavor_id'] = flavor_id
2645 myVMDict
['imageRef'] = vm
['vim_image_id']
2646 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2647 myVMDict
['networks'] = []
2648 for iface
in vm
['interfaces']:
2650 if iface
['type']=="data":
2651 netDict
['type'] = iface
['model']
2652 elif "model" in iface
and iface
["model"]!=None:
2653 netDict
['model']=iface
['model']
2654 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2655 #discover type of interface looking at flavor
2656 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2657 for flavor_iface
in numa
.get('interfaces',[]):
2658 if flavor_iface
.get('name') == iface
['internal_name']:
2659 if flavor_iface
['dedicated'] == 'yes':
2660 netDict
['type']="PF" #passthrough
2661 elif flavor_iface
['dedicated'] == 'no':
2662 netDict
['type']="VF" #siov
2663 elif flavor_iface
['dedicated'] == 'yes:sriov':
2664 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2665 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2667 netDict
["use"]=iface
['type']
2668 if netDict
["use"]=="data" and not netDict
.get("type"):
2669 #print "netDict", netDict
2670 #print "iface", iface
2671 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'])
2672 if flavor_dict
.get('extended')==None:
2673 raise NfvoException(e_text
+ "After database migration some information is not available. \
2674 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
2676 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
2677 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2678 netDict
["type"]="virtual"
2679 if "vpci" in iface
and iface
["vpci"] is not None:
2680 netDict
['vpci'] = iface
['vpci']
2681 if "mac" in iface
and iface
["mac"] is not None:
2682 netDict
['mac_address'] = iface
['mac']
2683 if "port-security" in iface
and iface
["port-security"] is not None:
2684 netDict
['port_security'] = iface
['port-security']
2685 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2686 netDict
['floating_ip'] = iface
['floating-ip']
2687 netDict
['name'] = iface
['internal_name']
2688 if iface
['net_id'] is None:
2689 for vnf_iface
in sce_vnf
["interfaces"]:
2692 if vnf_iface
['interface_id']==iface
['uuid']:
2693 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2696 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2697 #skip bridge ifaces not connected to any net
2698 #if 'net_id' not in netDict or netDict['net_id']==None:
2700 myVMDict
['networks'].append(netDict
)
2701 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2702 #print myVMDict['name']
2703 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2704 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2705 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2707 if 'availability_zone' in myVMDict
:
2708 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2712 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2713 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2714 availability_zone_index
=av_index
,
2715 availability_zone_list
=vnf_availability_zones
)
2716 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2717 vm
['vim_id'] = vm_id
2718 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2719 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2720 for net
in myVMDict
['networks']:
2722 for iface
in vm
['interfaces']:
2723 if net
["name"]==iface
["internal_name"]:
2724 iface
["vim_id"]=net
["vim_id"]
2727 logger
.debug("start scenario Deployment done")
2728 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2729 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2730 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2731 return mydb
.get_instance_scenario(instance_id
)
2733 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2734 _
, message
= rollback(mydb
, vims
, rollbackList
)
2735 if isinstance(e
, db_base_Exception
):
2736 error_text
= "Exception at database"
2738 error_text
= "Exception at VIM"
2739 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2740 #logger.error("start_scenario %s", error_text)
2741 raise NfvoException(error_text
, e
.http_code
)
2743 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2744 """ join the cloud config information into cloud_config_preserve.
2745 In case of conflict cloud_config_preserve preserves
2748 if not cloud_config_preserve
and not cloud_config
:
2751 new_cloud_config
= {"key-pairs":[], "users":[]}
2753 if cloud_config_preserve
:
2754 for key
in cloud_config_preserve
.get("key-pairs", () ):
2755 if key
not in new_cloud_config
["key-pairs"]:
2756 new_cloud_config
["key-pairs"].append(key
)
2758 for key
in cloud_config
.get("key-pairs", () ):
2759 if key
not in new_cloud_config
["key-pairs"]:
2760 new_cloud_config
["key-pairs"].append(key
)
2761 if not new_cloud_config
["key-pairs"]:
2762 del new_cloud_config
["key-pairs"]
2766 new_cloud_config
["users"] += cloud_config
.get("users", () )
2767 if cloud_config_preserve
:
2768 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2769 index_to_delete
= []
2770 users
= new_cloud_config
.get("users", [])
2771 for index0
in range(0,len(users
)):
2772 if index0
in index_to_delete
:
2774 for index1
in range(index0
+1,len(users
)):
2775 if index1
in index_to_delete
:
2777 if users
[index0
]["name"] == users
[index1
]["name"]:
2778 index_to_delete
.append(index1
)
2779 for key
in users
[index1
].get("key-pairs",()):
2780 if "key-pairs" not in users
[index0
]:
2781 users
[index0
]["key-pairs"] = [key
]
2782 elif key
not in users
[index0
]["key-pairs"]:
2783 users
[index0
]["key-pairs"].append(key
)
2784 index_to_delete
.sort(reverse
=True)
2785 for index
in index_to_delete
:
2787 if not new_cloud_config
["users"]:
2788 del new_cloud_config
["users"]
2791 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2792 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2793 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2794 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2797 new_cloud_config
["user-data"] = []
2798 if cloud_config
and cloud_config
.get("user-data"):
2799 if isinstance(cloud_config
["user-data"], list):
2800 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2802 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2803 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2804 if isinstance(cloud_config_preserve
["user-data"], list):
2805 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2807 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2808 if not new_cloud_config
["user-data"]:
2809 del new_cloud_config
["user-data"]
2812 new_cloud_config
["config-files"] = []
2813 if cloud_config
and cloud_config
.get("config-files") != None:
2814 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2815 if cloud_config_preserve
:
2816 for file in cloud_config_preserve
.get("config-files", ()):
2817 for index
in range(0, len(new_cloud_config
["config-files"])):
2818 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2819 new_cloud_config
["config-files"][index
] = file
2822 new_cloud_config
["config-files"].append(file)
2823 if not new_cloud_config
["config-files"]:
2824 del new_cloud_config
["config-files"]
2825 return new_cloud_config
2828 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2829 datacenter_id
= None
2830 datacenter_name
= None
2833 if datacenter_tenant_id
:
2834 thread_id
= datacenter_tenant_id
2835 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2837 where_
={"td.nfvo_tenant_id": tenant_id
}
2838 if datacenter_id_name
:
2839 if utils
.check_valid_uuid(datacenter_id_name
):
2840 datacenter_id
= datacenter_id_name
2841 where_
["dt.datacenter_id"] = datacenter_id
2843 datacenter_name
= datacenter_id_name
2844 where_
["d.name"] = datacenter_name
2845 if datacenter_tenant_id
:
2846 where_
["dt.uuid"] = datacenter_tenant_id
2847 datacenters
= mydb
.get_rows(
2848 SELECT
=("dt.uuid as datacenter_tenant_id",),
2849 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2850 "join datacenters as d on d.uuid=dt.datacenter_id",
2852 if len(datacenters
) > 1:
2853 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2855 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2856 thread
= vim_threads
["running"].get(thread_id
)
2858 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2859 return thread_id
, thread
2860 except db_base_Exception
as e
:
2861 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2864 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
2866 if utils
.check_valid_uuid(datacenter_id_name
):
2867 WHERE_dict
['d.uuid'] = datacenter_id_name
2869 WHERE_dict
['d.name'] = datacenter_id_name
2872 WHERE_dict
['nfvo_tenant_id'] = tenant_id
2873 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2874 " dt on td.datacenter_tenant_id=dt.uuid"
2876 from_
= 'datacenters as d'
2877 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid, d.name as name",), WHERE
=WHERE_dict
)
2878 if len(vimaccounts
) == 0:
2879 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2880 elif len(vimaccounts
)>1:
2881 #print "nfvo.datacenter_action() error. Several datacenters found"
2882 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2883 return vimaccounts
[0]["uuid"], vimaccounts
[0]["name"]
2886 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2887 datacenter_id
= None
2888 datacenter_name
= None
2889 if datacenter_id_name
:
2890 if utils
.check_valid_uuid(datacenter_id_name
):
2891 datacenter_id
= datacenter_id_name
2893 datacenter_name
= datacenter_id_name
2894 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2896 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), HTTP_Not_Found
)
2898 #print "nfvo.datacenter_action() error. Several datacenters found"
2899 raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict
)
2900 return vims
.keys()[0], vims
.values()[0]
2904 '''Takes dict d and updates it with the values in dict u.'''
2905 '''It merges all depth levels'''
2906 for k
, v
in u
.iteritems():
2907 if isinstance(v
, collections
.Mapping
):
2908 r
= update(d
.get(k
, {}), v
)
2915 def create_instance(mydb
, tenant_id
, instance_dict
):
2916 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2917 # logger.debug("Creating instance...")
2918 scenario
= instance_dict
["scenario"]
2920 # find main datacenter
2922 myvim_threads_id
= {}
2923 datacenter
= instance_dict
.get("datacenter")
2924 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
2925 myvims
[default_datacenter_id
] = vim
2926 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
2927 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
2928 # myvim_tenant = myvim['tenant_id']
2931 # print "Checking that the scenario exists and getting the scenario dictionary"
2932 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
2933 datacenter_id
=default_datacenter_id
)
2935 # logger.debug(">>>>>> Dictionaries before merging")
2936 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
2937 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
2939 db_instance_vnfs
= []
2940 db_instance_vms
= []
2941 db_instance_interfaces
= []
2942 db_instance_sfis
= []
2943 db_instance_sfs
= []
2944 db_instance_classifications
= []
2945 db_instance_sfps
= []
2950 instance_name
= instance_dict
["name"]
2951 instance_uuid
= str(uuid4())
2952 uuid_list
.append(instance_uuid
)
2953 db_instance_scenario
= {
2954 "uuid": instance_uuid
,
2955 "name": instance_name
,
2956 "tenant_id": tenant_id
,
2957 "scenario_id": scenarioDict
['uuid'],
2958 "datacenter_id": default_datacenter_id
,
2959 # filled bellow 'datacenter_tenant_id'
2960 "description": instance_dict
.get("description"),
2962 if scenarioDict
.get("cloud-config"):
2963 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
2964 default_flow_style
=True, width
=256)
2965 instance_action_id
= get_task_id()
2966 db_instance_action
= {
2967 "uuid": instance_action_id
, # same uuid for the instance and the action on create
2968 "tenant_id": tenant_id
,
2969 "instance_id": instance_uuid
,
2970 "description": "CREATE",
2973 # Auxiliary dictionaries from x to y
2974 sce_net2instance
= {}
2975 net2task_id
= {'scenario': {}}
2977 def ip_profile_IM2RO(ip_profile_im
):
2978 # translate from input format to database format
2980 if 'subnet-address' in ip_profile_im
:
2981 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
2982 if 'ip-version' in ip_profile_im
:
2983 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
2984 if 'gateway-address' in ip_profile_im
:
2985 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
2986 if 'dns-address' in ip_profile_im
:
2987 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
2988 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
2989 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
2990 if 'dhcp' in ip_profile_im
:
2991 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
2992 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
2993 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
2994 return ip_profile_ro
2996 # logger.debug("Creating instance from scenario-dict:\n%s",
2997 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
2999 # 0 check correct parameters
3000 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3001 for scenario_net
in scenarioDict
['nets']:
3002 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3005 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3007 if "sites" not in net_instance_desc
:
3008 net_instance_desc
["sites"] = [ {} ]
3009 site_without_datacenter_field
= False
3010 for site
in net_instance_desc
["sites"]:
3011 if site
.get("datacenter"):
3012 site
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3013 if site
["datacenter"] not in myvims
:
3014 # Add this datacenter to myvims
3015 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3017 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3018 site
["datacenter"] = d
# change name to id
3020 if site_without_datacenter_field
:
3021 raise NfvoException("Found more than one entries without datacenter field at "
3022 "instance:networks:{}:sites".format(net_name
), HTTP_Bad_Request
)
3023 site_without_datacenter_field
= True
3024 site
["datacenter"] = default_datacenter_id
# change name to id
3026 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
3027 for scenario_vnf
in scenarioDict
['vnfs']:
3028 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3031 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), HTTP_Bad_Request
)
3032 if "datacenter" in vnf_instance_desc
:
3033 # Add this datacenter to myvims
3034 vnf_instance_desc
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3035 if vnf_instance_desc
["datacenter"] not in myvims
:
3036 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3038 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3039 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3041 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).iteritems():
3042 for scenario_net
in scenario_vnf
['nets']:
3043 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3046 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), HTTP_Bad_Request
)
3047 if net_instance_desc
.get("vim-network-name"):
3048 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3049 if net_instance_desc
.get("name"):
3050 scenario_net
["name"] = net_instance_desc
["name"]
3051 if 'ip-profile' in net_instance_desc
:
3052 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3053 if 'ip_profile' not in scenario_net
:
3054 scenario_net
['ip_profile'] = ipprofile_db
3056 update(scenario_net
['ip_profile'], ipprofile_db
)
3058 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).iteritems():
3059 for scenario_vm
in scenario_vnf
['vms']:
3060 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3063 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), HTTP_Bad_Request
)
3064 scenario_vm
["instance_parameters"] = vdu_instance_desc
3065 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).iteritems():
3066 for scenario_interface
in scenario_vm
['interfaces']:
3067 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3068 scenario_interface
.update(iface_instance_desc
)
3071 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), HTTP_Bad_Request
)
3073 # 0.1 parse cloud-config parameters
3074 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3076 # 0.2 merge instance information into scenario
3077 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3078 # However, this is not possible yet.
3079 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3080 for scenario_net
in scenarioDict
['nets']:
3081 if net_name
== scenario_net
["name"]:
3082 if 'ip-profile' in net_instance_desc
:
3083 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3084 if 'ip_profile' not in scenario_net
:
3085 scenario_net
['ip_profile'] = ipprofile_db
3087 update(scenario_net
['ip_profile'], ipprofile_db
)
3088 for interface
in net_instance_desc
.get('interfaces', ()):
3089 if 'ip_address' in interface
:
3090 for vnf
in scenarioDict
['vnfs']:
3091 if interface
['vnf'] == vnf
['name']:
3092 for vnf_interface
in vnf
['interfaces']:
3093 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3094 vnf_interface
['ip_address'] = interface
['ip_address']
3096 # logger.debug(">>>>>>>> Merged dictionary")
3097 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3098 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3100 # 1. Creating new nets (sce_nets) in the VIM"
3101 number_mgmt_networks
= 0
3102 db_instance_nets
= []
3103 for sce_net
in scenarioDict
['nets']:
3104 # get involved datacenters where this network need to be created
3105 involved_datacenters
= []
3106 for sce_vnf
in scenarioDict
.get("vnfs"):
3107 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3108 if vnf_datacenter
in involved_datacenters
:
3110 if sce_vnf
.get("interfaces"):
3111 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3112 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3113 involved_datacenters
.append(vnf_datacenter
)
3117 if instance_dict
.get("networks") and instance_dict
["networks"].get(sce_net
["name"]):
3118 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3119 net_name
= descriptor_net
.get("vim-network-name")
3120 sce_net2instance
[sce_net
['uuid']] = {}
3121 net2task_id
['scenario'][sce_net
['uuid']] = {}
3123 if sce_net
["external"]:
3124 number_mgmt_networks
+= 1
3126 for datacenter_id
in involved_datacenters
:
3128 netmap_create
= None
3129 if descriptor_net
.get("sites"):
3130 for site
in descriptor_net
["sites"]:
3131 if site
.get("datacenter") == datacenter_id
:
3132 netmap_use
= site
.get("netmap-use")
3133 netmap_create
= site
.get("netmap-create")
3136 vim
= myvims
[datacenter_id
]
3137 myvim_thread_id
= myvim_threads_id
[datacenter_id
]
3139 net_type
= sce_net
['type']
3140 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3143 if sce_net
["external"]:
3144 net_name
= sce_net
["name"]
3146 net_name
= "{}-{}".format(instance_name
, sce_net
["name"])
3147 net_name
= net_name
[:255] # limit length
3149 if netmap_use
or netmap_create
:
3150 create_network
= False
3151 lookfor_network
= False
3153 lookfor_network
= True
3154 if utils
.check_valid_uuid(netmap_use
):
3155 lookfor_filter
["id"] = netmap_use
3157 lookfor_filter
["name"] = netmap_use
3159 create_network
= True
3160 net_vim_name
= net_name
3161 if isinstance(netmap_create
, str):
3162 net_vim_name
= netmap_create
3163 elif sce_net
.get("vim_network_name"):
3164 create_network
= False
3165 lookfor_network
= True
3166 lookfor_filter
["name"] = sce_net
.get("vim_network_name")
3167 elif sce_net
["external"]:
3168 if sce_net
['vim_id'] is not None:
3169 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3170 create_network
= False
3171 lookfor_network
= True
3172 lookfor_filter
["id"] = sce_net
['vim_id']
3173 elif vim
["config"].get("management_network_id") or vim
["config"].get("management_network_name"):
3174 if number_mgmt_networks
> 1:
3175 raise NfvoException("Found several VLD of type mgmt. "
3176 "You must concrete what vim-network must be use for each one",
3178 create_network
= False
3179 lookfor_network
= True
3180 if vim
["config"].get("management_network_id"):
3181 lookfor_filter
["id"] = vim
["config"]["management_network_id"]
3183 lookfor_filter
["name"] = vim
["config"]["management_network_name"]
3185 # There is not a netmap, look at datacenter for a net with this name and create if not found
3186 create_network
= True
3187 lookfor_network
= True
3188 lookfor_filter
["name"] = sce_net
["name"]
3189 net_vim_name
= sce_net
["name"]
3191 net_vim_name
= net_name
3192 create_network
= True
3193 lookfor_network
= False
3197 task_action
= "CREATE"
3198 task_extra
["params"] = (net_vim_name
, net_type
, sce_net
.get('ip_profile', None))
3200 task_extra
["find"] = (lookfor_filter
,)
3201 elif lookfor_network
:
3202 task_action
= "FIND"
3203 task_extra
["params"] = (lookfor_filter
,)
3205 # fill database content
3206 net_uuid
= str(uuid4())
3207 uuid_list
.append(net_uuid
)
3208 sce_net2instance
[sce_net
['uuid']][datacenter_id
] = net_uuid
3212 "instance_scenario_id": instance_uuid
,
3213 "sce_net_id": sce_net
["uuid"],
3214 "created": create_network
,
3215 'datacenter_id': datacenter_id
,
3216 'datacenter_tenant_id': myvim_thread_id
,
3217 'status': 'BUILD' # if create_network else "ACTIVE"
3219 db_instance_nets
.append(db_net
)
3221 "instance_action_id": instance_action_id
,
3222 "status": "SCHEDULED",
3223 "task_index": task_index
,
3224 "datacenter_vim_id": myvim_thread_id
,
3225 "action": task_action
,
3226 "item": "instance_nets",
3227 "item_id": net_uuid
,
3228 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3230 net2task_id
['scenario'][sce_net
['uuid']][datacenter_id
] = task_index
3232 db_vim_actions
.append(db_vim_action
)
3234 if 'ip_profile' in sce_net
:
3236 'instance_net_id': net_uuid
,
3237 'ip_version': sce_net
['ip_profile']['ip_version'],
3238 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3239 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3240 'dns_address': sce_net
['ip_profile']['dns_address'],
3241 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3242 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3243 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3245 db_ip_profiles
.append(db_ip_profile
)
3249 "default_datacenter_id": default_datacenter_id
,
3250 "myvim_threads_id": myvim_threads_id
,
3251 "instance_uuid": instance_uuid
,
3252 "instance_name": instance_name
,
3253 "instance_action_id": instance_action_id
,
3255 "cloud_config": cloud_config
,
3256 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3259 "task_index": task_index
,
3260 "uuid_list": uuid_list
,
3261 "db_instance_nets": db_instance_nets
,
3262 "db_vim_actions": db_vim_actions
,
3263 "db_ip_profiles": db_ip_profiles
,
3264 "db_instance_vnfs": db_instance_vnfs
,
3265 "db_instance_vms": db_instance_vms
,
3266 "db_instance_interfaces": db_instance_interfaces
,
3267 "net2task_id": net2task_id
,
3268 "sce_net2instance": sce_net2instance
,
3270 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3271 for sce_vnf
in scenarioDict
['vnfs']: # sce_vnf_list:
3272 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3273 task_index
= vnf_params_out
["task_index"]
3274 uuid_list
= vnf_params_out
["uuid_list"]
3277 # task_depends_on = []
3278 for vnffg
in scenarioDict
['vnffgs']:
3279 for rsp
in vnffg
['rsps']:
3281 for cp
in rsp
['connection_points']:
3282 count
= mydb
.get_rows(
3283 SELECT
=('vms.count'),
3284 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h on interfaces.uuid=h.interface_id",
3285 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3286 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3287 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3289 for instance_vm
in instance_vms
:
3290 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3292 dependencies
.append(action
['task_index'])
3293 # TODO: throw exception if count != len(instance_vms)
3294 # TODO: and action shouldn't ever be None
3296 for i
in range(count
):
3298 sfi_uuid
= str(uuid4())
3299 uuid_list
.append(sfi_uuid
)
3302 "instance_scenario_id": instance_uuid
,
3303 'sce_rsp_hop_id': cp
['uuid'],
3304 'datacenter_id': datacenter_id
,
3305 'datacenter_tenant_id': myvim_thread_id
,
3306 "vim_sfi_id": None, # vim thread will populate
3308 db_instance_sfis
.append(db_sfi
)
3310 "instance_action_id": instance_action_id
,
3311 "task_index": task_index
,
3312 "datacenter_vim_id": myvim_thread_id
,
3314 "status": "SCHEDULED",
3315 "item": "instance_sfis",
3316 "item_id": sfi_uuid
,
3317 "extra": yaml
.safe_dump({"params": "", "depends_on": [dependencies
[i
]]},
3318 default_flow_style
=True, width
=256)
3320 sfis_created
.append(task_index
)
3322 db_vim_actions
.append(db_vim_action
)
3324 sf_uuid
= str(uuid4())
3325 uuid_list
.append(sf_uuid
)
3328 "instance_scenario_id": instance_uuid
,
3329 'sce_rsp_hop_id': cp
['uuid'],
3330 'datacenter_id': datacenter_id
,
3331 'datacenter_tenant_id': myvim_thread_id
,
3332 "vim_sf_id": None, # vim thread will populate
3334 db_instance_sfs
.append(db_sf
)
3336 "instance_action_id": instance_action_id
,
3337 "task_index": task_index
,
3338 "datacenter_vim_id": myvim_thread_id
,
3340 "status": "SCHEDULED",
3341 "item": "instance_sfs",
3343 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3344 default_flow_style
=True, width
=256)
3346 sfs_created
.append(task_index
)
3348 db_vim_actions
.append(db_vim_action
)
3349 classifier
= rsp
['classifier']
3351 # TODO the following ~13 lines can be reused for the sfi case
3352 count
= mydb
.get_rows(
3353 SELECT
=('vms.count'),
3354 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3355 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3356 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3357 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3359 for instance_vm
in instance_vms
:
3360 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3362 dependencies
.append(action
['task_index'])
3363 # TODO: throw exception if count != len(instance_vms)
3364 # TODO: and action shouldn't ever be None
3365 classifications_created
= []
3366 for i
in range(count
):
3367 for match
in classifier
['matches']:
3368 # create classifications
3369 classification_uuid
= str(uuid4())
3370 uuid_list
.append(classification_uuid
)
3371 db_classification
= {
3372 "uuid": classification_uuid
,
3373 "instance_scenario_id": instance_uuid
,
3374 'sce_classifier_match_id': match
['uuid'],
3375 'datacenter_id': datacenter_id
,
3376 'datacenter_tenant_id': myvim_thread_id
,
3377 "vim_classification_id": None, # vim thread will populate
3379 db_instance_classifications
.append(db_classification
)
3380 classification_params
= {
3381 "ip_proto": match
["ip_proto"],
3382 "source_ip": match
["source_ip"],
3383 "destination_ip": match
["destination_ip"],
3384 "source_port": match
["source_port"],
3385 "destination_port": match
["destination_port"]
3388 "instance_action_id": instance_action_id
,
3389 "task_index": task_index
,
3390 "datacenter_vim_id": myvim_thread_id
,
3392 "status": "SCHEDULED",
3393 "item": "instance_classifications",
3394 "item_id": classification_uuid
,
3395 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3396 default_flow_style
=True, width
=256)
3398 classifications_created
.append(task_index
)
3400 db_vim_actions
.append(db_vim_action
)
3403 sfp_uuid
= str(uuid4())
3404 uuid_list
.append(sfp_uuid
)
3407 "instance_scenario_id": instance_uuid
,
3408 'sce_rsp_id': rsp
['uuid'],
3409 'datacenter_id': datacenter_id
,
3410 'datacenter_tenant_id': myvim_thread_id
,
3411 "vim_sfp_id": None, # vim thread will populate
3413 db_instance_sfps
.append(db_sfp
)
3415 "instance_action_id": instance_action_id
,
3416 "task_index": task_index
,
3417 "datacenter_vim_id": myvim_thread_id
,
3419 "status": "SCHEDULED",
3420 "item": "instance_sfps",
3421 "item_id": sfp_uuid
,
3422 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3423 default_flow_style
=True, width
=256)
3426 db_vim_actions
.append(db_vim_action
)
3428 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3430 db_instance_action
["number_tasks"] = task_index
3431 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3432 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3434 {"instance_scenarios": db_instance_scenario
},
3435 {"instance_vnfs": db_instance_vnfs
},
3436 {"instance_nets": db_instance_nets
},
3437 {"ip_profiles": db_ip_profiles
},
3438 {"instance_vms": db_instance_vms
},
3439 {"instance_interfaces": db_instance_interfaces
},
3440 {"instance_actions": db_instance_action
},
3441 {"instance_sfis": db_instance_sfis
},
3442 {"instance_sfs": db_instance_sfs
},
3443 {"instance_classifications": db_instance_classifications
},
3444 {"instance_sfps": db_instance_sfps
},
3445 {"vim_actions": db_vim_actions
}
3448 logger
.debug("create_instance done DB tables: %s",
3449 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3450 mydb
.new_rows(db_tables
, uuid_list
)
3451 for myvim_thread_id
in myvim_threads_id
.values():
3452 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3454 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3455 returned_instance
["action_id"] = instance_action_id
3456 return returned_instance
3457 except (NfvoException
, vimconn
.vimconnException
, db_base_Exception
) as e
:
3458 message
= rollback(mydb
, myvims
, rollbackList
)
3459 if isinstance(e
, db_base_Exception
):
3460 error_text
= "database Exception"
3461 elif isinstance(e
, vimconn
.vimconnException
):
3462 error_text
= "VIM Exception"
3464 error_text
= "Exception"
3465 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3466 # logger.error("create_instance: %s", error_text)
3467 raise NfvoException(error_text
, e
.http_code
)
3470 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3471 default_datacenter_id
= params
["default_datacenter_id"]
3472 myvim_threads_id
= params
["myvim_threads_id"]
3473 instance_uuid
= params
["instance_uuid"]
3474 instance_name
= params
["instance_name"]
3475 instance_action_id
= params
["instance_action_id"]
3476 myvims
= params
["myvims"]
3477 cloud_config
= params
["cloud_config"]
3478 RO_pub_key
= params
["RO_pub_key"]
3480 task_index
= params_out
["task_index"]
3481 uuid_list
= params_out
["uuid_list"]
3482 db_instance_nets
= params_out
["db_instance_nets"]
3483 db_vim_actions
= params_out
["db_vim_actions"]
3484 db_ip_profiles
= params_out
["db_ip_profiles"]
3485 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3486 db_instance_vms
= params_out
["db_instance_vms"]
3487 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3488 net2task_id
= params_out
["net2task_id"]
3489 sce_net2instance
= params_out
["sce_net2instance"]
3491 vnf_net2instance
= {}
3493 # 2. Creating new nets (vnf internal nets) in the VIM"
3494 # For each vnf net, we create it and we add it to instanceNetlist.
3495 if sce_vnf
.get("datacenter"):
3496 datacenter_id
= sce_vnf
["datacenter"]
3497 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3499 datacenter_id
= default_datacenter_id
3500 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3501 for net
in sce_vnf
['nets']:
3503 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3504 # net_name = descriptor_net.get("name")
3507 net_name
= "{}-{}".format(instance_name
, net
["name"])
3508 net_name
= net_name
[:255] # limit length
3509 net_type
= net
['type']
3511 if sce_vnf
['uuid'] not in vnf_net2instance
:
3512 vnf_net2instance
[sce_vnf
['uuid']] = {}
3513 if sce_vnf
['uuid'] not in net2task_id
:
3514 net2task_id
[sce_vnf
['uuid']] = {}
3515 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3517 # fill database content
3518 net_uuid
= str(uuid4())
3519 uuid_list
.append(net_uuid
)
3520 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3524 "instance_scenario_id": instance_uuid
,
3525 "net_id": net
["uuid"],
3527 'datacenter_id': datacenter_id
,
3528 'datacenter_tenant_id': myvim_thread_id
,
3530 db_instance_nets
.append(db_net
)
3532 if net
.get("vim-network-name"):
3533 lookfor_filter
= {"name": net
["vim-network-name"]}
3534 task_action
= "FIND"
3535 task_extra
= {"params": (lookfor_filter
,)}
3537 task_action
= "CREATE"
3538 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3541 "instance_action_id": instance_action_id
,
3542 "task_index": task_index
,
3543 "datacenter_vim_id": myvim_thread_id
,
3544 "status": "SCHEDULED",
3545 "action": task_action
,
3546 "item": "instance_nets",
3547 "item_id": net_uuid
,
3548 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3551 db_vim_actions
.append(db_vim_action
)
3553 if 'ip_profile' in net
:
3555 'instance_net_id': net_uuid
,
3556 'ip_version': net
['ip_profile']['ip_version'],
3557 'subnet_address': net
['ip_profile']['subnet_address'],
3558 'gateway_address': net
['ip_profile']['gateway_address'],
3559 'dns_address': net
['ip_profile']['dns_address'],
3560 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3561 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3562 'dhcp_count': net
['ip_profile']['dhcp_count'],
3564 db_ip_profiles
.append(db_ip_profile
)
3566 # print "vnf_net2instance:"
3567 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3569 # 3. Creating new vm instances in the VIM
3570 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3572 if sce_vnf
.get('mgmt_access'):
3573 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3574 vnf_availability_zones
= []
3575 for vm
in sce_vnf
['vms']:
3576 vm_av
= vm
.get('availability_zone')
3577 if vm_av
and vm_av
not in vnf_availability_zones
:
3578 vnf_availability_zones
.append(vm_av
)
3580 # check if there is enough availability zones available at vim level.
3581 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3582 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3583 raise NfvoException('No enough availability zones at VIM for this deployment', HTTP_Bad_Request
)
3585 if sce_vnf
.get("datacenter"):
3586 vim
= myvims
[sce_vnf
["datacenter"]]
3587 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3588 datacenter_id
= sce_vnf
["datacenter"]
3590 vim
= myvims
[default_datacenter_id
]
3591 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3592 datacenter_id
= default_datacenter_id
3593 sce_vnf
["datacenter_id"] = datacenter_id
3596 vnf_uuid
= str(uuid4())
3597 uuid_list
.append(vnf_uuid
)
3600 'instance_scenario_id': instance_uuid
,
3601 'vnf_id': sce_vnf
['vnf_id'],
3602 'sce_vnf_id': sce_vnf
['uuid'],
3603 'datacenter_id': datacenter_id
,
3604 'datacenter_tenant_id': myvim_thread_id
,
3606 db_instance_vnfs
.append(db_instance_vnf
)
3608 for vm
in sce_vnf
['vms']:
3610 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3611 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3612 myVMDict
['description'] = myVMDict
['name'][0:99]
3614 # myVMDict['start'] = "no"
3615 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3616 myVMDict
['name'] = vm
["instance_parameters"].get("name")
3617 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
3618 # create image at vim in case it not exist
3619 image_uuid
= vm
['image_id']
3620 if vm
.get("image_list"):
3621 for alternative_image
in vm
["image_list"]:
3622 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
3623 image_uuid
= alternative_image
['image_id']
3625 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
3626 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
3627 vm
['vim_image_id'] = image_id
3629 # create flavor at vim in case it not exist
3630 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
3631 if flavor_dict
['extended'] != None:
3632 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'])
3633 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
3635 # Obtain information for additional disks
3636 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
3637 WHERE
={'vim_id': flavor_id
})
3638 if not extended_flavor_dict
:
3639 raise NfvoException("flavor '{}' not found".format(flavor_id
), HTTP_Not_Found
)
3641 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3642 myVMDict
['disks'] = None
3643 extended_info
= extended_flavor_dict
[0]['extended']
3644 if extended_info
!= None:
3645 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
3646 if 'disks' in extended_flavor_dict_yaml
:
3647 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
3648 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
3649 for disk
in myVMDict
['disks']:
3650 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
3651 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
3653 vm
['vim_flavor_id'] = flavor_id
3654 myVMDict
['imageRef'] = vm
['vim_image_id']
3655 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
3656 myVMDict
['availability_zone'] = vm
.get('availability_zone')
3657 myVMDict
['networks'] = []
3658 task_depends_on
= []
3659 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3661 for iface
in vm
['interfaces']:
3663 if iface
['type'] == "data":
3664 netDict
['type'] = iface
['model']
3665 elif "model" in iface
and iface
["model"] != None:
3666 netDict
['model'] = iface
['model']
3667 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3668 # is obtained from iterface table model
3669 # discover type of interface looking at flavor
3670 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
3671 for flavor_iface
in numa
.get('interfaces', []):
3672 if flavor_iface
.get('name') == iface
['internal_name']:
3673 if flavor_iface
['dedicated'] == 'yes':
3674 netDict
['type'] = "PF" # passthrough
3675 elif flavor_iface
['dedicated'] == 'no':
3676 netDict
['type'] = "VF" # siov
3677 elif flavor_iface
['dedicated'] == 'yes:sriov':
3678 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
3679 netDict
["mac_address"] = flavor_iface
.get("mac_address")
3681 netDict
["use"] = iface
['type']
3682 if netDict
["use"] == "data" and not netDict
.get("type"):
3683 # print "netDict", netDict
3684 # print "iface", iface
3685 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3686 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
3687 if flavor_dict
.get('extended') == None:
3688 raise NfvoException(e_text
+ "After database migration some information is not available. \
3689 Try to delete and create the scenarios and VNFs again", HTTP_Conflict
)
3691 raise NfvoException(e_text
, HTTP_Internal_Server_Error
)
3692 if netDict
["use"] == "mgmt" or netDict
["use"] == "bridge":
3693 netDict
["type"] = "virtual"
3694 if iface
.get("vpci"):
3695 netDict
['vpci'] = iface
['vpci']
3696 if iface
.get("mac"):
3697 netDict
['mac_address'] = iface
['mac']
3698 if iface
.get("ip_address"):
3699 netDict
['ip_address'] = iface
['ip_address']
3700 if iface
.get("port-security") is not None:
3701 netDict
['port_security'] = iface
['port-security']
3702 if iface
.get("floating-ip") is not None:
3703 netDict
['floating_ip'] = iface
['floating-ip']
3704 netDict
['name'] = iface
['internal_name']
3705 if iface
['net_id'] is None:
3706 for vnf_iface
in sce_vnf
["interfaces"]:
3709 if vnf_iface
['interface_id'] == iface
['uuid']:
3710 netDict
['net_id'] = "TASK-{}".format(
3711 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3712 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
3713 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3716 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3717 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
3718 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3719 # skip bridge ifaces not connected to any net
3720 if 'net_id' not in netDict
or netDict
['net_id'] == None:
3722 myVMDict
['networks'].append(netDict
)
3725 # 'instance_vm_id': instance_vm_uuid,
3726 "instance_net_id": instance_net_id
,
3727 'interface_id': iface
['uuid'],
3728 # 'vim_interface_id': ,
3729 'type': 'external' if iface
['external_name'] is not None else 'internal',
3730 'ip_address': iface
.get('ip_address'),
3731 'mac_address': iface
.get('mac'),
3732 'floating_ip': int(iface
.get('floating-ip', False)),
3733 'port_security': int(iface
.get('port-security', True))
3735 db_vm_ifaces
.append(db_vm_iface
)
3736 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3737 # print myVMDict['name']
3738 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3739 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3740 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3742 # We add the RO key to cloud_config if vnf will need ssh access
3743 cloud_config_vm
= cloud_config
3744 if ssh_access
and ssh_access
['required'] and ssh_access
['default-user'] and tenant
[0].get('RO_pub_key'):
3745 RO_key
= {"key-pairs": [tenant
[0]['RO_pub_key']]}
3746 cloud_config_vm
= unify_cloud_config(cloud_config_vm
, RO_key
)
3747 if vm
.get("boot_data"):
3748 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
3750 if myVMDict
.get('availability_zone'):
3751 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3754 for vm_index
in range(0, vm
.get('count', 1)):
3755 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
3756 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
3757 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
3758 myVMDict
['disks'], av_index
, vnf_availability_zones
)
3759 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3760 for net
in myVMDict
['networks']:
3762 for iface
in vm
['interfaces']:
3763 if net
["name"] == iface
["internal_name"]:
3764 iface
["vim_id"] = net
["vim_id"]
3766 vm_uuid
= str(uuid4())
3767 uuid_list
.append(vm_uuid
)
3770 'instance_vnf_id': vnf_uuid
,
3771 # TODO delete "vim_vm_id": vm_id,
3772 "vm_id": vm
["uuid"],
3773 "vim_name": vm_name
,
3776 db_instance_vms
.append(db_vm
)
3779 for db_vm_iface
in db_vm_ifaces
:
3780 iface_uuid
= str(uuid4())
3781 uuid_list
.append(iface_uuid
)
3782 db_vm_iface_instance
= {
3784 "instance_vm_id": vm_uuid
3786 db_vm_iface_instance
.update(db_vm_iface
)
3787 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3788 ip
= db_vm_iface_instance
.get("ip_address")
3793 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
3794 db_vm_iface_instance
["ip_address"] = ip
3796 db_vm_iface_instance
["ip_address"] = None
3797 db_instance_interfaces
.append(db_vm_iface_instance
)
3798 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
3802 "instance_action_id": instance_action_id
,
3803 "task_index": task_index
,
3804 "datacenter_vim_id": myvim_thread_id
,
3806 "status": "SCHEDULED",
3807 "item": "instance_vms",
3809 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
3810 default_flow_style
=True, width
=256)
3813 db_vim_actions
.append(db_vim_action
)
3814 params_out
["task_index"] = task_index
3815 params_out
["uuid_list"] = uuid_list
3818 def delete_instance(mydb
, tenant_id
, instance_id
):
3819 # print "Checking that the instance_id exists and getting the instance dictionary"
3820 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3821 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3822 tenant_id
= instanceDict
["tenant_id"]
3823 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3824 # 1. Delete from Database
3825 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3827 # 2. delete from VIM
3831 vimthread_affected
= {}
3832 net2vm_dependencies
= {}
3835 instance_action_id
= get_task_id()
3837 db_instance_action
= {
3838 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3839 "tenant_id": tenant_id
,
3840 "instance_id": instance_id
,
3841 "description": "DELETE",
3842 # "number_tasks": 0 # filled bellow
3847 for sce_vnf
in instanceDict
['vnfs']:
3848 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3849 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
3850 if datacenter_key
not in myvims
:
3852 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3853 except NfvoException
as e
:
3854 logger
.error(str(e
))
3856 myvim_threads
[datacenter_key
] = myvim_thread
3857 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
3858 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
3860 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
3861 sce_vnf
["datacenter_tenant_id"]))
3862 myvims
[datacenter_key
] = None
3864 myvims
[datacenter_key
] = vims
.values()[0]
3865 myvim
= myvims
[datacenter_key
]
3866 myvim_thread
= myvim_threads
[datacenter_key
]
3867 for vm
in sce_vnf
['vms']:
3869 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
3872 "instance_action_id": instance_action_id
,
3873 "task_index": task_index
,
3874 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
3876 "status": "SCHEDULED",
3877 "item": "instance_vms",
3878 "item_id": vm
["uuid"],
3879 "extra": yaml
.safe_dump({"params": vm
["interfaces"]},
3880 default_flow_style
=True, width
=256)
3882 db_vim_actions
.append(db_vim_action
)
3883 for interface
in vm
["interfaces"]:
3884 if not interface
.get("instance_net_id"):
3886 if interface
["instance_net_id"] not in net2vm_dependencies
:
3887 net2vm_dependencies
[interface
["instance_net_id"]] = []
3888 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
3893 for net
in instanceDict
['nets']:
3894 vimthread_affected
[net
["datacenter_tenant_id"]] = None
3895 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
3896 if datacenter_key
not in myvims
:
3898 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
3899 except NfvoException
as e
:
3900 logger
.error(str(e
))
3902 myvim_threads
[datacenter_key
] = myvim_thread
3903 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
3904 datacenter_tenant_id
=net
["datacenter_tenant_id"])
3906 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
3907 myvims
[datacenter_key
] = None
3909 myvims
[datacenter_key
] = vims
.values()[0]
3910 myvim
= myvims
[datacenter_key
]
3911 myvim_thread
= myvim_threads
[datacenter_key
]
3914 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
3916 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
3917 if net2vm_dependencies
.get(net
["uuid"]):
3918 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
3920 "instance_action_id": instance_action_id
,
3921 "task_index": task_index
,
3922 "datacenter_vim_id": net
["datacenter_tenant_id"],
3924 "status": "SCHEDULED",
3925 "item": "instance_nets",
3926 "item_id": net
["uuid"],
3927 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
3930 db_vim_actions
.append(db_vim_action
)
3932 # 2.3 deleting VNFFGs
3934 for sfp
in instanceDict
.get('sfps', ()):
3935 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
3936 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
3937 if datacenter_key
not in myvims
:
3939 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
3940 except NfvoException
as e
:
3941 logger
.error(str(e
))
3943 myvim_threads
[datacenter_key
] = myvim_thread
3944 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
3945 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
3947 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
3948 myvims
[datacenter_key
] = None
3950 myvims
[datacenter_key
] = vims
.values()[0]
3951 myvim
= myvims
[datacenter_key
]
3952 myvim_thread
= myvim_threads
[datacenter_key
]
3955 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
3957 extra
= {"params": (sfp
['vim_sfp_id'])}
3959 "instance_action_id": instance_action_id
,
3960 "task_index": task_index
,
3961 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
3963 "status": "SCHEDULED",
3964 "item": "instance_sfps",
3965 "item_id": sfp
["uuid"],
3966 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
3969 db_vim_actions
.append(db_vim_action
)
3971 for sf
in instanceDict
.get('sfs', ()):
3972 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
3973 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
3974 if datacenter_key
not in myvims
:
3976 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
3977 except NfvoException
as e
:
3978 logger
.error(str(e
))
3980 myvim_threads
[datacenter_key
] = myvim_thread
3981 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
3982 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
3984 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
3985 myvims
[datacenter_key
] = None
3987 myvims
[datacenter_key
] = vims
.values()[0]
3988 myvim
= myvims
[datacenter_key
]
3989 myvim_thread
= myvim_threads
[datacenter_key
]
3992 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
3994 extra
= {"params": (sf
['vim_sf_id'])}
3996 "instance_action_id": instance_action_id
,
3997 "task_index": task_index
,
3998 "datacenter_vim_id": sf
["datacenter_tenant_id"],
4000 "status": "SCHEDULED",
4001 "item": "instance_sfs",
4002 "item_id": sf
["uuid"],
4003 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4006 db_vim_actions
.append(db_vim_action
)
4008 for sfi
in instanceDict
.get('sfis', ()):
4009 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4010 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4011 if datacenter_key
not in myvims
:
4013 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4014 except NfvoException
as e
:
4015 logger
.error(str(e
))
4017 myvim_threads
[datacenter_key
] = myvim_thread
4018 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4019 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4021 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4022 myvims
[datacenter_key
] = None
4024 myvims
[datacenter_key
] = vims
.values()[0]
4025 myvim
= myvims
[datacenter_key
]
4026 myvim_thread
= myvim_threads
[datacenter_key
]
4029 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4031 extra
= {"params": (sfi
['vim_sfi_id'])}
4033 "instance_action_id": instance_action_id
,
4034 "task_index": task_index
,
4035 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4037 "status": "SCHEDULED",
4038 "item": "instance_sfis",
4039 "item_id": sfi
["uuid"],
4040 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4043 db_vim_actions
.append(db_vim_action
)
4045 for classification
in instanceDict
['classifications']:
4046 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4047 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4048 if datacenter_key
not in myvims
:
4050 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4051 except NfvoException
as e
:
4052 logger
.error(str(e
))
4054 myvim_threads
[datacenter_key
] = myvim_thread
4055 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4056 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4058 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"], classification
["datacenter_tenant_id"]))
4059 myvims
[datacenter_key
] = None
4061 myvims
[datacenter_key
] = vims
.values()[0]
4062 myvim
= myvims
[datacenter_key
]
4063 myvim_thread
= myvim_threads
[datacenter_key
]
4066 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'], classification
["datacenter_id"])
4068 extra
= {"params": (classification
['vim_classification_id'])}
4070 "instance_action_id": instance_action_id
,
4071 "task_index": task_index
,
4072 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4074 "status": "SCHEDULED",
4075 "item": "instance_classifications",
4076 "item_id": classification
["uuid"],
4077 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4080 db_vim_actions
.append(db_vim_action
)
4082 db_instance_action
["number_tasks"] = task_index
4084 {"instance_actions": db_instance_action
},
4085 {"vim_actions": db_vim_actions
}
4088 logger
.debug("delete_instance done DB tables: %s",
4089 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4090 mydb
.new_rows(db_tables
, ())
4091 for myvim_thread_id
in vimthread_affected
.keys():
4092 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4094 if len(error_msg
) > 0:
4095 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4096 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4098 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4100 def get_instance_id(mydb
, tenant_id
, instance_id
):
4102 #check valid tenant_id
4103 check_tenant(mydb
, tenant_id
)
4106 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4107 for net
in instance_dict
["nets"]:
4108 if net
.get("sdn_net_id"):
4109 net_sdn
= ovim
.show_network(net
["sdn_net_id"])
4111 "admin_state_up": net_sdn
.get("admin_state_up"),
4112 "flows": net_sdn
.get("flows"),
4113 "last_error": net_sdn
.get("last_error"),
4114 "ports": net_sdn
.get("ports"),
4115 "type": net_sdn
.get("type"),
4116 "status": net_sdn
.get("status"),
4117 "vlan": net_sdn
.get("vlan"),
4119 return instance_dict
4121 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4122 '''Refreshes a scenario instance. It modifies instanceDict'''
4124 - 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
4127 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4128 # #print "nfvo.refresh_instance begins"
4129 # #print json.dumps(instanceDict, indent=4)
4131 # #print "Getting the VIM URL and the VIM tenant_id"
4134 # # 1. Getting VIM vm and net list
4135 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4138 # for sce_vnf in instanceDict['vnfs']:
4139 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4140 # if datacenter_key not in vm_list:
4141 # vm_list[datacenter_key] = []
4142 # if datacenter_key not in myvims:
4143 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4144 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4145 # if len(vims) == 0:
4146 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4147 # myvims[datacenter_key] = None
4149 # myvims[datacenter_key] = vims.values()[0]
4150 # for vm in sce_vnf['vms']:
4151 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4152 # vms_notupdated.append(vm["uuid"])
4154 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4155 # nets_notupdated=[]
4157 # for net in instanceDict['nets']:
4158 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4159 # if datacenter_key not in net_list:
4160 # net_list[datacenter_key] = []
4161 # if datacenter_key not in myvims:
4162 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4163 # datacenter_tenant_id=net["datacenter_tenant_id"])
4164 # if len(vims) == 0:
4165 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4166 # myvims[datacenter_key] = None
4168 # myvims[datacenter_key] = vims.values()[0]
4170 # net_list[datacenter_key].append(net['vim_net_id'])
4171 # nets_notupdated.append(net["uuid"])
4173 # # 1. Getting the status of all VMs
4175 # for datacenter_key in myvims:
4176 # if not vm_list.get(datacenter_key):
4180 # if not myvims[datacenter_key]:
4181 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4184 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4186 # except vimconn.vimconnException as e:
4187 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4188 # failed_message = str(e)
4190 # for vm in vm_list[datacenter_key]:
4191 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4193 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4194 # for sce_vnf in instanceDict['vnfs']:
4195 # for vm in sce_vnf['vms']:
4196 # vm_id = vm['vim_vm_id']
4197 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4198 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4199 # has_mgmt_iface = False
4200 # for iface in vm["interfaces"]:
4201 # if iface["type"]=="mgmt":
4202 # has_mgmt_iface = True
4203 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4204 # vm_dict[vm_id]['status'] = "ACTIVE"
4205 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4206 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4207 # 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'):
4208 # vm['status'] = vm_dict[vm_id]['status']
4209 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4210 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4211 # # 2.1. Update in openmano DB the VMs whose status changed
4213 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4214 # vms_notupdated.remove(vm["uuid"])
4216 # vms_updated.append(vm["uuid"])
4217 # except db_base_Exception as e:
4218 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4219 # # 2.2. Update in openmano DB the interface VMs
4220 # for interface in interfaces:
4221 # #translate from vim_net_id to instance_net_id
4222 # network_id_list=[]
4223 # for net in instanceDict['nets']:
4224 # if net["vim_net_id"] == interface["vim_net_id"]:
4225 # network_id_list.append(net["uuid"])
4226 # if not network_id_list:
4228 # del interface["vim_net_id"]
4230 # for network_id in network_id_list:
4231 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4232 # except db_base_Exception as e:
4233 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4235 # # 3. Getting the status of all nets
4237 # for datacenter_key in myvims:
4238 # if not net_list.get(datacenter_key):
4241 # failed_message = ""
4242 # if not myvims[datacenter_key]:
4243 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4246 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4248 # except vimconn.vimconnException as e:
4249 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4250 # failed_message = str(e)
4252 # for net in net_list[datacenter_key]:
4253 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4255 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4256 # # TODO: update nets inside a vnf
4257 # for net in instanceDict['nets']:
4258 # net_id = net['vim_net_id']
4259 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4260 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4261 # 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'):
4262 # net['status'] = net_dict[net_id]['status']
4263 # net['error_msg'] = net_dict[net_id].get('error_msg')
4264 # net['vim_info'] = net_dict[net_id].get('vim_info')
4265 # # 5.1. Update in openmano DB the nets whose status changed
4267 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4268 # nets_notupdated.remove(net["uuid"])
4270 # nets_updated.append(net["uuid"])
4271 # except db_base_Exception as e:
4272 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4274 # # Returns appropriate output
4275 # #print "nfvo.refresh_instance finishes"
4276 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4277 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4278 instance_id
= instanceDict
['uuid']
4279 # if len(vms_notupdated)+len(nets_notupdated)>0:
4280 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4281 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4283 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4285 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4286 #print "Checking that the instance_id exists and getting the instance dictionary"
4287 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4288 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4290 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4291 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4293 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), HTTP_Not_Found
)
4294 myvim
= vims
.values()[0]
4299 myvim_threads_id
= {}
4300 if action_dict
.get("vdu-scaling"):
4301 db_instance_vms
= []
4303 db_instance_interfaces
= []
4304 instance_action_id
= get_task_id()
4305 db_instance_action
= {
4306 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4307 "tenant_id": nfvo_tenant
,
4308 "instance_id": instance_id
,
4309 "description": "SCALE",
4311 vm_result
["instance_action_id"] = instance_action_id
4313 for vdu
in action_dict
["vdu-scaling"]:
4314 vdu_id
= vdu
.get("vdu-id")
4315 osm_vdu_id
= vdu
.get("osm_vdu_id")
4316 member_vnf_index
= vdu
.get("member-vnf-index")
4317 vdu_count
= vdu
.get("count", 1)
4319 target_vm
= mydb
.get_rows(
4320 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4321 WHERE
={"vms.uuid": vdu_id
},
4322 ORDER_BY
="vms.created_at"
4325 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), HTTP_Not_Found
)
4327 if not osm_vdu_id
and not member_vnf_index
:
4328 raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4329 target_vm
= mydb
.get_rows(
4330 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4331 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4332 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4333 " join vms on ivms.vm_id=vms.uuid",
4334 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
},
4335 ORDER_BY
="ivms.created_at"
4338 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), HTTP_Not_Found
)
4339 vdu_id
= target_vm
[-1]["uuid"]
4340 vm_result
[vdu_id
] = {"created": [], "deleted": [], "description": "scheduled"}
4341 target_vm
= target_vm
[-1]
4342 datacenter
= target_vm
["datacenter_id"]
4343 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4344 if vdu
["type"] == "delete":
4346 vm_interfaces
= None
4347 for sce_vnf
in instanceDict
['vnfs']:
4348 for vm
in sce_vnf
['vms']:
4349 if vm
["uuid"] == vdu_id
:
4350 vm_interfaces
= vm
["interfaces"]
4354 "instance_action_id": instance_action_id
,
4355 "task_index": task_index
,
4356 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4358 "status": "SCHEDULED",
4359 "item": "instance_vms",
4360 "item_id": target_vm
["uuid"],
4361 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4362 default_flow_style
=True, width
=256)
4365 db_vim_actions
.append(db_vim_action
)
4366 vm_result
[vdu_id
]["deleted"].append(vdu_id
)
4367 # delete from database
4368 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4370 else: # vdu["type"] == "create":
4372 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4374 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_actions", WHERE
=where
)
4375 if not vim_action_to_clone
:
4376 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), HTTP_Internal_Server_Error
)
4377 vim_action_to_clone
= vim_action_to_clone
[0]
4378 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4380 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4381 # TODO do the same for flavor and image when available
4382 task_depends_on
= []
4383 task_params
= extra
["params"]
4384 task_params_networks
= deepcopy(task_params
[5])
4385 for iface
in task_params
[5]:
4386 if iface
["net_id"].startswith("TASK-"):
4387 if "." not in iface
["net_id"]:
4388 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4389 iface
["net_id"][5:]))
4390 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4391 iface
["net_id"][5:])
4393 task_depends_on
.append(iface
["net_id"][5:])
4394 if "mac_address" in iface
:
4395 del iface
["mac_address"]
4397 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4398 for index
in range(0, vdu_count
):
4399 vm_uuid
= str(uuid4())
4400 vm_name
= target_vm
.get('vim_name')
4402 suffix
= vm_name
.rfind("-")
4403 vm_name
= vm_name
[:suffix
+1] + str(1 + int(vm_name
[suffix
+1:]))
4408 'instance_vnf_id': target_vm
['instance_vnf_id'],
4409 'vm_id': target_vm
['vm_id'],
4412 db_instance_vms
.append(db_instance_vm
)
4414 for vm_iface
in vm_ifaces_to_clone
:
4415 iface_uuid
= str(uuid4())
4416 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4419 'instance_vm_id': vm_uuid
,
4420 "instance_net_id": vm_iface
["instance_net_id"],
4421 'interface_id': vm_iface
['interface_id'],
4422 'type': vm_iface
['type'],
4423 'floating_ip': vm_iface
['floating_ip'],
4424 'port_security': vm_iface
['port_security']
4426 db_instance_interfaces
.append(db_vm_iface
)
4427 task_params_copy
= deepcopy(task_params
)
4428 for iface
in task_params_copy
[5]:
4429 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4430 # increment ip_address
4431 if "ip_address" in iface
:
4432 ip
= iface
.get("ip_address")
4437 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4438 iface
["ip_address"] = ip
4440 iface
["ip_address"] = None
4442 task_params_copy
[0] = vm_name
4444 "instance_action_id": instance_action_id
,
4445 "task_index": task_index
,
4446 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4448 "status": "SCHEDULED",
4449 "item": "instance_vms",
4453 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4456 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4459 db_vim_actions
.append(db_vim_action
)
4460 vm_result
[vdu_id
]["created"].append(vm_uuid
)
4462 db_instance_action
["number_tasks"] = task_index
4464 {"instance_vms": db_instance_vms
},
4465 {"instance_interfaces": db_instance_interfaces
},
4466 {"instance_actions": db_instance_action
},
4468 # {"instance_sfis": db_instance_sfis},
4469 # {"instance_sfs": db_instance_sfs},
4470 # {"instance_classifications": db_instance_classifications},
4471 # {"instance_sfps": db_instance_sfps},
4472 {"vim_actions": db_vim_actions
}
4474 logger
.debug("create_vdu done DB tables: %s",
4475 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4476 mydb
.new_rows(db_tables
, [])
4477 for myvim_thread
in myvim_threads_id
.values():
4478 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4482 input_vnfs
= action_dict
.pop("vnfs", [])
4483 input_vms
= action_dict
.pop("vms", [])
4484 action_over_all
= True if not input_vnfs
and not input_vms
else False
4485 for sce_vnf
in instanceDict
['vnfs']:
4486 for vm
in sce_vnf
['vms']:
4487 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4488 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4489 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
4492 if "add_public_key" in action_dict
:
4494 if sce_vnf
.get('mgmt_access'):
4495 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'])
4496 ssh_access
= mgmt_access
['config-access']['ssh-access']
4497 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4499 if ssh_access
['required'] and ssh_access
['default-user']:
4500 if 'ip_address' in vm
:
4501 mgmt_ip
= vm
['ip_address'].split(';')
4502 password
= mgmt_access
['config-access'].get('password')
4503 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4504 myvim
.inject_user_key(mgmt_ip
[0], ssh_access
['default-user'],
4505 action_dict
['add_public_key'],
4506 password
=password
, ro_key
=priv_RO_key
)
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 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4515 HTTP_Internal_Server_Error
)
4517 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
4518 if "console" in action_dict
:
4519 if not global_config
["http_console_proxy"]:
4520 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4521 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4522 protocol
=data
["protocol"],
4523 ip
= data
["server"],
4524 port
= data
["port"],
4525 suffix
= data
["suffix"]),
4529 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
4530 vm_result
[ vm
['uuid'] ] = {"vim_result": -HTTP_Unauthorized
,
4531 "description": "this console is only reachable by local interface",
4536 #print "console data", data
4538 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
4539 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4540 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4541 protocol
=data
["protocol"],
4542 ip
= global_config
["http_console_host"],
4543 port
= console_thread
.port
,
4544 suffix
= data
["suffix"]),
4548 except NfvoException
as e
:
4549 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4553 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
4555 except vimconn
.vimconnException
as e
:
4556 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4559 if vm_ok
==0: #all goes wrong
4564 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
4566 if nfvo_tenant
and nfvo_tenant
!= "any":
4567 filter["tenant_id"] = nfvo_tenant
4568 if instance_id
and instance_id
!= "any":
4569 filter["instance_id"] = instance_id
4571 filter["uuid"] = action_id
4572 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
4575 raise NfvoException("Not found any action with this criteria", HTTP_Not_Found
)
4576 vim_actions
= mydb
.get_rows(FROM
="vim_actions", WHERE
={"instance_action_id": action_id
})
4577 rows
[0]["vim_actions"] = vim_actions
4578 return {"actions": rows
}
4581 def create_or_use_console_proxy_thread(console_server
, console_port
):
4582 #look for a non-used port
4583 console_thread_key
= console_server
+ ":" + str(console_port
)
4584 if console_thread_key
in global_config
["console_thread"]:
4585 #global_config["console_thread"][console_thread_key].start_timeout()
4586 return global_config
["console_thread"][console_thread_key
]
4588 for port
in global_config
["console_port_iterator"]():
4589 #print "create_or_use_console_proxy_thread() port:", port
4590 if port
in global_config
["console_ports"]:
4593 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
4595 global_config
["console_thread"][console_thread_key
] = clithread
4596 global_config
["console_ports"][port
] = console_thread_key
4598 except cli
.ConsoleProxyExceptionPortUsed
as e
:
4599 #port used, try with onoher
4601 except cli
.ConsoleProxyException
as e
:
4602 raise NfvoException(str(e
), HTTP_Bad_Request
)
4603 raise NfvoException("Not found any free 'http_console_ports'", HTTP_Conflict
)
4606 def check_tenant(mydb
, tenant_id
):
4607 '''check that tenant exists at database'''
4608 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
4610 raise NfvoException("tenant '{}' not found".format(tenant_id
), HTTP_Not_Found
)
4613 def new_tenant(mydb
, tenant_dict
):
4615 tenant_uuid
= str(uuid4())
4616 tenant_dict
['uuid'] = tenant_uuid
4618 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
4619 tenant_dict
['RO_pub_key'] = pub_key
4620 tenant_dict
['encrypted_RO_priv_key'] = priv_key
4621 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
4622 except db_base_Exception
as e
:
4623 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
4626 def delete_tenant(mydb
, tenant
):
4627 #get nfvo_tenant info
4629 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
4630 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
4631 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
4634 def new_datacenter(mydb
, datacenter_descriptor
):
4635 sdn_port_mapping
= None
4636 if "config" in datacenter_descriptor
:
4637 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
4638 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
4640 # Check that datacenter-type is correct
4641 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
4642 # module_info = None
4644 module
= "vimconn_" + datacenter_type
4645 pkg
= __import__("osm_ro." + module
)
4646 # vim_conn = getattr(pkg, module)
4647 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
4648 except (IOError, ImportError):
4649 # if module_info and module_info[0]:
4650 # file.close(module_info[0])
4651 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type
,
4655 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
4656 if sdn_port_mapping
:
4658 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
4659 except Exception as e
:
4660 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
4662 return datacenter_id
4665 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
4666 # obtain data, check that only one exist
4667 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
4670 datacenter_id
= datacenter
['uuid']
4671 where
= {'uuid': datacenter
['uuid']}
4672 remove_port_mapping
= False
4673 new_sdn_port_mapping
= None
4674 if "config" in datacenter_descriptor
:
4675 if datacenter_descriptor
['config'] != None:
4677 new_config_dict
= datacenter_descriptor
["config"]
4678 if "sdn-port-mapping" in new_config_dict
:
4679 remove_port_mapping
= True
4680 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
4681 # delete null fields
4683 for k
in new_config_dict
:
4684 if new_config_dict
[k
] is None:
4686 if k
== 'sdn-controller':
4687 remove_port_mapping
= True
4689 config_text
= datacenter
.get("config")
4692 config_dict
= yaml
.load(config_text
)
4693 config_dict
.update(new_config_dict
)
4694 # delete null fields
4697 except Exception as e
:
4698 raise NfvoException("Bad format at datacenter:config " + str(e
), HTTP_Bad_Request
)
4700 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
4702 datacenter_descriptor
["config"] = None
4703 if remove_port_mapping
:
4705 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
4706 except ovimException
as e
:
4707 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
), HTTP_Conflict
)
4709 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
4710 if new_sdn_port_mapping
:
4712 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
4713 except ovimException
as e
:
4715 mydb
.update_rows('datacenters', datacenter
, where
)
4716 raise NfvoException("Error adding datacenter-port-mapping " + str(e
), HTTP_Conflict
)
4717 return datacenter_id
4720 def delete_datacenter(mydb
, datacenter
):
4721 #get nfvo_tenant info
4722 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
4723 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
4725 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
4726 except ovimException
as e
:
4727 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
))
4728 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
4731 def create_vim_account(mydb
, nfvo_tenant
, datacenter_id
, name
=None, vim_id
=None, vim_tenant
=None, vim_tenant_name
=None,
4732 vim_username
=None, vim_password
=None, config
=None):
4733 # get datacenter info
4735 if not datacenter_id
:
4737 raise NfvoException("You must provide 'vim_id", http_code
=HTTP_Bad_Request
)
4738 datacenter_id
= vim_id
4739 datacenter_id
, datacenter_name
= get_datacenter_uuid(mydb
, None, datacenter_id
)
4741 create_vim_tenant
= True if not vim_tenant
and not vim_tenant_name
else False
4743 # get nfvo_tenant info
4744 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
4745 if vim_tenant_name
==None:
4746 vim_tenant_name
=tenant_dict
['name']
4748 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
4749 # #check that this association does not exist before
4750 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4751 # if len(tenants_datacenters)>0:
4752 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), HTTP_Conflict)
4754 vim_tenant_id_exist_atdb
=False
4755 if not create_vim_tenant
:
4756 where_
={"datacenter_id": datacenter_id
}
4757 if vim_tenant
!=None:
4758 where_
["vim_tenant_id"] = vim_tenant
4759 if vim_tenant_name
!=None:
4760 where_
["vim_tenant_name"] = vim_tenant_name
4761 #check if vim_tenant_id is already at database
4762 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
4763 if len(datacenter_tenants_dict
)>=1:
4764 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
4765 vim_tenant_id_exist_atdb
=True
4766 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4768 datacenter_tenants_dict
= {}
4769 #insert at table datacenter_tenants
4770 else: #if vim_tenant==None:
4771 #create tenant at VIM if not provided
4773 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
,
4774 vim_passwd
=vim_password
)
4775 datacenter_name
= myvim
["name"]
4776 vim_tenant
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
4777 except vimconn
.vimconnException
as e
:
4778 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant
, str(e
)), HTTP_Internal_Server_Error
)
4779 datacenter_tenants_dict
= {}
4780 datacenter_tenants_dict
["created"]="true"
4782 #fill datacenter_tenants table
4783 if not vim_tenant_id_exist_atdb
:
4784 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant
4785 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
4786 datacenter_tenants_dict
["user"] = vim_username
4787 datacenter_tenants_dict
["passwd"] = vim_password
4788 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
4790 datacenter_tenants_dict
["name"] = name
4792 datacenter_tenants_dict
["name"] = datacenter_name
4794 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
4795 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
4796 datacenter_tenants_dict
["uuid"] = id_
4798 #fill tenants_datacenters table
4799 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
4800 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
4801 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
4804 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
4805 new_thread
= vim_thread
.vim_thread(task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
4806 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
4808 thread_id
= datacenter_tenants_dict
["uuid"]
4809 vim_threads
["running"][thread_id
] = new_thread
4811 except vimconn
.vimconnException
as e
:
4812 raise NfvoException(str(e
), HTTP_Bad_Request
)
4815 def edit_vim_account(mydb
, nfvo_tenant
, datacenter_tenant_id
, datacenter_id
=None, name
=None, vim_tenant
=None,
4816 vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
4818 # get vim_account; check is valid for this tenant
4819 from_
= "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
4820 where_
= {"td.nfvo_tenant_id": nfvo_tenant
}
4821 if datacenter_tenant_id
:
4822 where_
["dt.uuid"] = datacenter_tenant_id
4824 where_
["dt.datacenter_id"] = datacenter_id
4825 vim_accounts
= mydb
.get_rows(SELECT
="dt.uuid as uuid, config", FROM
=from_
, WHERE
=where_
)
4826 if not vim_accounts
:
4827 raise NfvoException("vim_account not found for this tenant", http_code
=HTTP_Not_Found
)
4828 elif len(vim_accounts
) > 1:
4829 raise NfvoException("found more than one vim_account for this tenant", http_code
=HTTP_Conflict
)
4830 datacenter_tenant_id
= vim_accounts
[0]["uuid"]
4831 original_config
= vim_accounts
[0]["config"]
4835 original_config_dict
= yaml
.load(original_config
)
4836 original_config_dict
.update(config
)
4837 update
["config"] = yaml
.safe_dump(original_config_dict
, default_flow_style
=True, width
=256)
4839 update_
['name'] = name
4841 update_
['vim_tenant_id'] = vim_tenant
4843 update_
['vim_tenant_name'] = vim_tenant_name
4845 update_
['user'] = vim_username
4847 update_
['passwd'] = vim_password
4849 mydb
.update_rows("datacenter_tenants", UPDATE
=update_
, WHERE
={"uuid": datacenter_tenant_id
})
4851 vim_threads
["running"][datacenter_tenant_id
].insert_task("reload")
4852 return datacenter_tenant_id
4854 def delete_vim_account(mydb
, tenant_id
, vim_account_id
, datacenter
=None):
4855 #get nfvo_tenant info
4856 if not tenant_id
or tenant_id
=="any":
4859 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
4860 tenant_uuid
= tenant_dict
['uuid']
4862 #check that this association exist before
4863 tenants_datacenter_dict
= {}
4865 datacenter_id
, _
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
4866 tenants_datacenter_dict
["datacenter_id"] = datacenter_id
4867 elif vim_account_id
:
4868 tenants_datacenter_dict
["datacenter_tenant_id"] = vim_account_id
4870 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
4871 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
4872 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
4873 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), HTTP_Not_Found
)
4875 #delete this association
4876 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
4878 #get vim_tenant info and deletes
4880 for tenant_datacenter_item
in tenant_datacenter_list
:
4881 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
4882 #try to delete vim:tenant
4884 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
4885 if vim_tenant_dict
['created']=='true':
4886 #delete tenant at VIM if created by NFVO
4888 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4889 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
4890 except vimconn
.vimconnException
as e
:
4891 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
4892 logger
.warn(warning
)
4893 except db_base_Exception
as e
:
4894 logger
.error("Cannot delete datacenter_tenants " + str(e
))
4895 pass # the error will be caused because dependencies, vim_tenant can not be deleted
4896 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
4897 thread
= vim_threads
["running"].get(thread_id
)
4899 thread
.insert_task("exit")
4900 vim_threads
["deleting"][thread_id
] = thread
4901 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
4904 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
4906 #get datacenter info
4907 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4909 if 'net-update' in action_dict
:
4911 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
4913 except vimconn
.vimconnException
as e
:
4914 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
4915 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
4916 #update nets Change from VIM format to NFVO format
4919 net_nfvo
={'datacenter_id': datacenter_id
}
4920 net_nfvo
['name'] = net
['name']
4921 #net_nfvo['description']= net['name']
4922 net_nfvo
['vim_net_id'] = net
['id']
4923 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4924 net_nfvo
['shared'] = net
['shared']
4925 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
4926 net_list
.append(net_nfvo
)
4927 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
4928 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
4930 elif 'net-edit' in action_dict
:
4931 net
= action_dict
['net-edit'].pop('net')
4932 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
4933 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
4934 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
4936 elif 'net-delete' in action_dict
:
4937 net
= action_dict
['net-deelte'].get('net')
4938 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
4939 result
= mydb
.delete_row(FROM
='datacenter_nets',
4940 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
4944 raise NfvoException("Unknown action " + str(action_dict
), HTTP_Bad_Request
)
4947 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
4948 #get datacenter info
4949 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4951 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
4952 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
4953 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
4957 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
4958 #get datacenter info
4959 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
4962 action_dict
= action_dict
["netmap"]
4963 if 'vim_id' in action_dict
:
4964 filter_dict
["id"] = action_dict
['vim_id']
4965 if 'vim_name' in action_dict
:
4966 filter_dict
["name"] = action_dict
['vim_name']
4968 filter_dict
["shared"] = True
4971 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
4972 except vimconn
.vimconnException
as e
:
4973 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
4974 raise NfvoException(str(e
), HTTP_Internal_Server_Error
)
4975 if len(vim_nets
)>1 and action_dict
:
4976 raise NfvoException("more than two networks found, specify with vim_id", HTTP_Conflict
)
4977 elif len(vim_nets
)==0: # and action_dict:
4978 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), HTTP_Not_Found
)
4980 for net
in vim_nets
:
4981 net_nfvo
={'datacenter_id': datacenter_id
}
4982 if action_dict
and "name" in action_dict
:
4983 net_nfvo
['name'] = action_dict
['name']
4985 net_nfvo
['name'] = net
['name']
4986 #net_nfvo['description']= net['name']
4987 net_nfvo
['vim_net_id'] = net
['id']
4988 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
4989 net_nfvo
['shared'] = net
['shared']
4990 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
4992 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
4993 net_nfvo
["status"] = "OK"
4994 net_nfvo
["uuid"] = net_id
4995 except db_base_Exception
as e
:
4999 net_nfvo
["status"] = "FAIL: " + str(e
)
5000 net_list
.append(net_nfvo
)
5003 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
5004 # obtain all network data
5006 if utils
.check_valid_uuid(network_id
):
5007 filter_dict
= {"id": network_id
}
5009 filter_dict
= {"name": network_id
}
5011 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5012 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
5013 except vimconn
.vimconnException
as e
:
5014 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
5016 # ensure the network is defined
5017 if len(network
) == 0:
5018 raise NfvoException("Network {} is not present in the system".format(network_id
),
5021 # ensure there is only one network with the provided name
5022 if len(network
) > 1:
5023 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), HTTP_Bad_Request
)
5025 # ensure it is a dataplane network
5026 if network
[0]['type'] != 'data':
5029 # ensure we use the id
5030 network_id
= network
[0]['id']
5032 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5033 # and with instance_scenario_id==NULL
5034 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5035 search_dict
= {'vim_net_id': network_id
}
5038 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5039 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5040 except db_base_Exception
as e
:
5041 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5042 network_id
) + str(e
), e
.http_code
)
5046 if net
['sdn_net_id'] != None:
5048 sdn_net_id
= net
['sdn_net_id']
5050 if sdn_net_counter
== 0:
5052 elif sdn_net_counter
== 1:
5055 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5056 network_id
), HTTP_Internal_Server_Error
)
5058 def get_sdn_controller_id(mydb
, datacenter
):
5059 # Obtain sdn controller id
5060 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5064 return yaml
.load(config
).get('sdn-controller')
5066 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5068 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5069 if not sdn_network_id
:
5070 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), HTTP_Internal_Server_Error
)
5072 #Obtain sdn controller id
5073 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5074 if not controller_id
:
5075 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), HTTP_Internal_Server_Error
)
5077 #Obtain sdn controller info
5078 sdn_controller
= ovim
.show_of_controller(controller_id
)
5081 'name': 'external_port',
5082 'net_id': sdn_network_id
,
5083 'ofc_id': controller_id
,
5084 'switch_dpid': sdn_controller
['dpid'],
5085 'switch_port': descriptor
['port']
5088 if 'vlan' in descriptor
:
5089 port_data
['vlan'] = descriptor
['vlan']
5090 if 'mac' in descriptor
:
5091 port_data
['mac'] = descriptor
['mac']
5093 result
= ovim
.new_port(port_data
)
5094 except ovimException
as e
:
5095 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5096 sdn_network_id
, network_id
) + str(e
), HTTP_Internal_Server_Error
)
5097 except db_base_Exception
as e
:
5098 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5099 network_id
) + str(e
), e
.http_code
)
5101 return 'Port uuid: '+ result
5103 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5105 filter = {'uuid': port_id
}
5107 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5108 if not sdn_network_id
:
5109 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5110 HTTP_Internal_Server_Error
)
5111 #in case no port_id is specified only ports marked as 'external_port' will be detached
5112 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5115 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5116 except ovimException
as e
:
5117 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5118 HTTP_Internal_Server_Error
)
5120 if len(port_list
) == 0:
5121 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5125 for port
in port_list
:
5127 port_uuid_list
.append(port
['uuid'])
5128 ovim
.delete_port(port
['uuid'])
5129 except ovimException
as e
:
5130 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), HTTP_Internal_Server_Error
)
5132 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5134 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5135 #get datacenter info
5136 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5139 if utils
.check_valid_uuid(name
):
5140 filter_dict
["id"] = name
5142 filter_dict
["name"] = name
5144 if item
=="networks":
5145 #filter_dict['tenant_id'] = myvim['tenant_id']
5146 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5148 if len(content
) == 0:
5149 raise NfvoException("Network {} is not present in the system. ".format(name
),
5152 #Update the networks with the attached ports
5154 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5155 if sdn_network_id
!= None:
5157 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5158 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5159 except ovimException
as e
:
5160 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), HTTP_Internal_Server_Error
)
5161 #Remove field name and if port name is external_port save it as 'type'
5162 for port
in port_list
:
5163 if port
['name'] == 'external_port':
5164 port
['type'] = "External"
5166 net
['sdn_network_id'] = sdn_network_id
5167 net
['sdn_attached_ports'] = port_list
5169 elif item
=="tenants":
5170 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5171 elif item
== "images":
5173 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5175 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5176 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5177 if name
and len(content
)==1:
5178 return {item
[:-1]: content
[0]}
5179 elif name
and len(content
)==0:
5180 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
5183 return {item
: content
}
5184 except vimconn
.vimconnException
as e
:
5185 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
5186 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5189 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5190 #get datacenter info
5191 if tenant_id
== "any":
5194 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5196 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5197 logger
.debug("vim_action_delete vim response: " + str(content
))
5198 items
= content
.values()[0]
5199 if type(items
)==list and len(items
)==0:
5200 raise NfvoException("Not found " + item
, HTTP_Not_Found
)
5201 elif type(items
)==list and len(items
)>1:
5202 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), HTTP_Not_Found
)
5203 else: # it is a dict
5204 item_id
= items
["id"]
5205 item_name
= str(items
.get("name"))
5208 if item
=="networks":
5209 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5210 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5211 if sdn_network_id
!= None:
5212 #Delete any port attachment to this network
5214 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5215 except ovimException
as e
:
5216 raise NfvoException(
5217 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5218 HTTP_Internal_Server_Error
)
5220 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5221 for port
in port_list
:
5222 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5224 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5226 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
5227 except db_base_Exception
as e
:
5228 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
5229 str(e
), e
.http_code
)
5231 #Delete the SDN network
5233 ovim
.delete_network(sdn_network_id
)
5234 except ovimException
as e
:
5235 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5236 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5237 HTTP_Internal_Server_Error
)
5239 content
= myvim
.delete_network(item_id
)
5240 elif item
=="tenants":
5241 content
= myvim
.delete_tenant(item_id
)
5242 elif item
== "images":
5243 content
= myvim
.delete_image(item_id
)
5245 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5246 except vimconn
.vimconnException
as e
:
5247 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5248 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5250 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5253 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5254 #get datacenter info
5255 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5256 if tenant_id
== "any":
5258 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5260 if item
=="networks":
5261 net
= descriptor
["network"]
5262 net_name
= net
.pop("name")
5263 net_type
= net
.pop("type", "bridge")
5264 net_public
= net
.pop("shared", False)
5265 net_ipprofile
= net
.pop("ip_profile", None)
5266 net_vlan
= net
.pop("vlan", None)
5267 content
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
5269 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5270 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5271 #obtain datacenter_tenant_id
5272 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5273 FROM
='datacenter_tenants',
5274 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5277 sdn_network
['vlan'] = net_vlan
5278 sdn_network
['type'] = net_type
5279 sdn_network
['name'] = net_name
5280 sdn_network
['region'] = datacenter_tenant_id
5281 ovim_content
= ovim
.new_network(sdn_network
)
5282 except ovimException
as e
:
5283 logger
.error("ovimException creating SDN network={} ".format(
5284 sdn_network
) + str(e
), exc_info
=True)
5285 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5286 HTTP_Internal_Server_Error
)
5288 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5289 # use instance_scenario_id=None to distinguish from real instaces of nets
5290 correspondence
= {'instance_scenario_id': None,
5291 'sdn_net_id': ovim_content
,
5292 'vim_net_id': content
,
5293 'datacenter_tenant_id': datacenter_tenant_id
5296 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5297 except db_base_Exception
as e
:
5298 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5299 correspondence
, e
), e
.http_code
)
5300 elif item
=="tenants":
5301 tenant
= descriptor
["tenant"]
5302 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5304 raise NfvoException(item
+ "?", HTTP_Method_Not_Allowed
)
5305 except vimconn
.vimconnException
as e
:
5306 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5308 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5310 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5311 data
= ovim
.new_of_controller(sdn_controller
)
5312 logger
.debug('New SDN controller created with uuid {}'.format(data
))
5315 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5316 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5317 msg
= 'SDN controller {} updated'.format(data
)
5321 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5322 if controller_id
== None:
5323 data
= ovim
.get_of_controllers()
5325 data
= ovim
.show_of_controller(controller_id
)
5327 msg
= 'SDN controller list:\n {}'.format(data
)
5331 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5332 select_
= ('uuid', 'config')
5333 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5334 for datacenter
in datacenters
:
5335 if datacenter
['config']:
5336 config
= yaml
.load(datacenter
['config'])
5337 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5338 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), HTTP_Conflict
)
5340 data
= ovim
.delete_of_controller(controller_id
)
5341 msg
= 'SDN controller {} deleted'.format(data
)
5345 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5346 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5347 if len(controller
) < 1:
5348 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), HTTP_Not_Found
)
5351 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
5353 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), HTTP_Bad_Request
)
5355 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5356 switch_dpid
= sdn_controller
["dpid"]
5359 for compute_node
in sdn_port_mapping
:
5360 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5362 element
["compute_node"] = compute_node
["compute_node"]
5363 for port
in compute_node
["ports"]:
5364 pci
= port
.get("pci")
5365 element
["switch_port"] = port
.get("switch_port")
5366 element
["switch_mac"] = port
.get("switch_mac")
5367 if not pci
or not (element
["switch_port"] or element
["switch_mac"]):
5368 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
5369 " or 'switch_mac'", HTTP_Bad_Request
)
5370 for pci_expanded
in utils
.expand_brackets(pci
):
5371 element
["pci"] = pci_expanded
5372 maps
.append(dict(element
))
5374 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
5376 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5377 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
5380 "sdn-controller": None,
5381 "datacenter-id": datacenter_id
,
5383 "ports_mapping": list()
5386 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5387 if datacenter
['config']:
5388 config
= yaml
.load(datacenter
['config'])
5389 if 'sdn-controller' in config
:
5390 controller_id
= config
['sdn-controller']
5391 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5392 result
["sdn-controller"] = controller_id
5393 result
["dpid"] = sdn_controller
["dpid"]
5395 if result
["sdn-controller"] == None:
5396 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), HTTP_Bad_Request
)
5397 if result
["dpid"] == None:
5398 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5399 HTTP_Internal_Server_Error
)
5404 ports_correspondence_dict
= dict()
5406 if result
["sdn-controller"] != link
["ofc_id"]:
5407 raise NfvoException("The sdn-controller specified for different port mappings differ", HTTP_Internal_Server_Error
)
5408 if result
["dpid"] != link
["switch_dpid"]:
5409 raise NfvoException("The dpid specified for different port mappings differ", HTTP_Internal_Server_Error
)
5411 element
["pci"] = link
["pci"]
5412 if link
["switch_port"]:
5413 element
["switch_port"] = link
["switch_port"]
5414 if link
["switch_mac"]:
5415 element
["switch_mac"] = link
["switch_mac"]
5417 if not link
["compute_node"] in ports_correspondence_dict
:
5419 content
["compute_node"] = link
["compute_node"]
5420 content
["ports"] = list()
5421 ports_correspondence_dict
[link
["compute_node"]] = content
5423 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
5425 for key
in sorted(ports_correspondence_dict
):
5426 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5430 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5431 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})
5433 def create_RO_keypair(tenant_id
):
5435 Creates a public / private keys for a RO tenant and returns their values
5437 tenant_id: ID of the tenant
5439 public_key: Public key for the RO tenant
5440 private_key: Encrypted private key for RO tenant
5444 key
= RSA
.generate(bits
)
5446 public_key
= key
.publickey().exportKey('OpenSSH')
5447 if isinstance(public_key
, ValueError):
5448 raise NfvoException("Unable to create public key: {}".format(public_key
), HTTP_Internal_Server_Error
)
5449 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5450 except (ValueError, NameError) as e
:
5451 raise NfvoException("Unable to create private key: {}".format(e
), HTTP_Internal_Server_Error
)
5452 return public_key
, private_key
5454 def decrypt_key (key
, tenant_id
):
5456 Decrypts an encrypted RSA key
5458 key: Private key to be decrypted
5459 tenant_id: ID of the tenant
5461 unencrypted_key: Unencrypted private key for RO tenant
5464 key
= RSA
.importKey(key
,tenant_id
)
5465 unencrypted_key
= key
.exportKey('PEM')
5466 if isinstance(unencrypted_key
, ValueError):
5467 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), HTTP_Internal_Server_Error
)
5468 except ValueError as e
:
5469 raise NfvoException("Unable to decrypt the private key: {}".format(e
), HTTP_Internal_Server_Error
)
5470 return unencrypted_key