1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion 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$"
34 from utils
import deprecated
36 import console_proxy_thread
as cli
41 from uuid
import uuid4
42 from db_base
import db_base_Exception
45 from threading
import Lock
47 from lib_osm_openvim
import ovim
as ovim_module
48 from lib_osm_openvim
.ovim
import ovimException
49 from Crypto
.PublicKey
import RSA
51 import osm_im
.vnfd
as vnfd_catalog
52 import osm_im
.nsd
as nsd_catalog
53 from pyangbind
.lib
.serialise
import pybindJSONDecoder
54 from copy
import deepcopy
58 import wim
.wimconn
as wimconn
59 import wim
.wim_thread
as wim_thread
60 from .http_tools
import errors
as httperrors
61 from .wim
.engine
import WimEngine
62 from .wim
.persistence
import WimPersistence
63 from copy
import deepcopy
64 from pprint
import pformat
68 global vimconn_imported
72 global wimconn_imported
75 global default_volume_size
76 default_volume_size
= '5' #size in GB
81 vimconn_imported
= {} # dictionary with VIM type as key, loaded module as value
82 vim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
83 vim_persistent_info
= {}
85 wimconn_imported
= {} # dictionary with WIM type as key, loaded module as value
86 wim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
87 wim_persistent_info
= {}
90 logger
= logging
.getLogger('openmano.nfvo')
97 class NfvoException(httperrors
.HttpMappedError
):
98 """Common Class for NFVO errors"""
104 if task_id
<= last_task_id
:
105 task_id
= last_task_id
+ 0.000001
106 last_task_id
= task_id
107 return "ACTION-{:.6f}".format(task_id
)
108 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
111 def new_task(name
, params
, depends
=None):
113 task_id
= get_task_id()
114 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
116 task
["depends"] = depends
121 return True if id[:5] == "TASK-" else False
124 def get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_name
, tenant_id
):
125 name
= datacenter_name
[:16]
126 if name
not in vim_threads
["names"]:
127 vim_threads
["names"].append(name
)
129 name
= datacenter_name
[:16] + "." + tenant_name
[:16]
130 if name
not in vim_threads
["names"]:
131 vim_threads
["names"].append(name
)
133 name
= datacenter_id
+ "-" + tenant_id
134 vim_threads
["names"].append(name
)
138 def get_non_used_wim_name(wim_name
, wim_id
, tenant_name
, tenant_id
):
140 if name
not in wim_threads
["names"]:
141 wim_threads
["names"].append(name
)
143 name
= wim_name
[:16] + "." + tenant_name
[:16]
144 if name
not in wim_threads
["names"]:
145 wim_threads
["names"].append(name
)
147 name
= wim_id
+ "-" + tenant_id
148 wim_threads
["names"].append(name
)
152 def start_service(mydb
, persistence
=None, wim
=None):
153 global db
, global_config
154 db
= nfvo_db
.nfvo_db()
155 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
159 persistence
.lock
= db_lock
161 persistence
= WimPersistence(db
, lock
=db_lock
)
163 # Initialize openvim for SDN control
164 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
165 # TODO: review ovim.py to delete not needed configuration
166 ovim_configuration
= {
167 'logger_name': 'openmano.ovim',
168 'network_vlan_range_start': 1000,
169 'network_vlan_range_end': 4096,
170 'db_name': global_config
["db_ovim_name"],
171 'db_host': global_config
["db_ovim_host"],
172 'db_user': global_config
["db_ovim_user"],
173 'db_passwd': global_config
["db_ovim_passwd"],
176 'network_type': 'bridge',
177 #TODO: log_level_of should not be needed. To be modified in ovim
178 'log_level_of': 'DEBUG'
181 # starts ovim library
182 ovim
= ovim_module
.ovim(ovim_configuration
)
185 wim_engine
= wim
or WimEngine(persistence
)
186 wim_engine
.ovim
= ovim
190 #delete old unneeded vim_wim_actions
194 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
195 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
196 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
197 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
198 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
199 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
200 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
202 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
203 'datacenter_id': vim
.get('datacenter_id')}
205 extra
.update(yaml
.load(vim
["config"]))
206 if vim
.get('dt_config'):
207 extra
.update(yaml
.load(vim
["dt_config"]))
208 if vim
["type"] not in vimconn_imported
:
211 module
= "vimconn_" + vim
["type"]
212 pkg
= __import__("osm_ro." + module
)
213 vim_conn
= getattr(pkg
, module
)
214 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
215 # vim_conn = imp.load_module(vim["type"], *module_info)
216 vimconn_imported
[vim
["type"]] = vim_conn
217 except (IOError, ImportError) as e
:
218 # if module_info and module_info[0]:
219 # file.close(module_info[0])
220 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
221 vim
["type"], module
, type(e
).__name
__, str(e
)), httperrors
.Bad_Request
)
223 thread_id
= vim
['datacenter_tenant_id']
224 vim_persistent_info
[thread_id
] = {}
227 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
228 myvim
= vimconn_imported
[ vim
["type"] ].vimconnector(
229 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
230 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
231 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
232 user
=vim
['user'], passwd
=vim
['passwd'],
233 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
235 except vimconn
.vimconnException
as e
:
237 logger
.error("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
238 vim
['datacenter_id'], e
))
239 except Exception as e
:
240 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, e
),
241 httperrors
.Internal_Server_Error
)
242 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['vim_tenant_id'], vim
['vim_tenant_name'],
243 vim
['vim_tenant_id'])
244 new_thread
= vim_thread
.vim_thread(task_lock
, thread_name
, vim
['datacenter_name'],
245 vim
['datacenter_tenant_id'], db
=db
, db_lock
=db_lock
, ovim
=ovim
)
247 vim_threads
["running"][thread_id
] = new_thread
249 wim_engine
.start_threads()
250 except db_base_Exception
as e
:
251 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
252 except ovim_module
.ovimException
as e
:
254 if message
[:22] == "DATABASE wrong version":
255 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
256 "at host {dbhost}".format(
257 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
258 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
259 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
260 raise NfvoException(message
, httperrors
.Bad_Request
)
264 global ovim
, global_config
267 for thread_id
, thread
in vim_threads
["running"].items():
268 thread
.insert_task("exit")
269 vim_threads
["deleting"][thread_id
] = thread
270 vim_threads
["running"] = {}
273 wim_engine
.stop_threads()
275 if global_config
and global_config
.get("console_thread"):
276 for thread
in global_config
["console_thread"]:
277 thread
.terminate
= True
280 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
281 global_config
["version_date"] ))
285 Clean unused or old entries at database to avoid unlimited growing
286 :param mydb: database connector
289 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
290 now
= t
.time()-3600*24*7
291 instance_action_id
= None
294 actions_to_delete
= mydb
.get_rows(
295 SELECT
=("item", "item_id", "instance_action_id"),
296 FROM
="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
297 "left join instance_scenarios as i on ia.instance_id=i.uuid",
298 WHERE
={"va.action": "DELETE", "va.modified_at<": now
, "i.uuid": None,
299 "va.status": ("DONE", "SUPERSEDED")},
302 for to_delete
in actions_to_delete
:
303 mydb
.delete_row(FROM
="vim_wim_actions", WHERE
=to_delete
)
304 if instance_action_id
!= to_delete
["instance_action_id"]:
305 instance_action_id
= to_delete
["instance_action_id"]
306 mydb
.delete_row(FROM
="instance_actions", WHERE
={"uuid": instance_action_id
})
307 nb_deleted
+= len(actions_to_delete
)
308 if len(actions_to_delete
) < 100:
311 logger
.debug("Removed {} unused vim_wim_actions".format(nb_deleted
))
314 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
316 return result, content:
317 <0, error_text upon error
318 nb_records, flavor_list on success
321 WHERE_dict
['vnf_id'] = vnf_id
322 if nfvo_tenant
is not None:
323 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
325 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
326 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
327 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
328 #print "get_flavor_list result:", result
329 #print "get_flavor_list content:", content
331 for flavor
in flavors
:
332 flavorList
.append(flavor
['flavor_id'])
336 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
338 Get used images of all vms belonging to this VNFD
339 :param mydb: database conector
340 :param vnf_id: vnfd uuid
341 :param nfvo_tenant: tenant, not used
342 :return: The list of image uuid used
345 vms
= mydb
.get_rows(SELECT
=('image_id','image_list'), FROM
='vms', WHERE
={'vnf_id': vnf_id
})
347 if vm
["image_id"] and vm
["image_id"] not in image_list
:
348 image_list
.append(vm
["image_id"])
350 vm_image_list
= yaml
.load(vm
["image_list"])
351 for image_dict
in vm_image_list
:
352 if image_dict
["image_id"] not in image_list
:
353 image_list
.append(image_dict
["image_id"])
357 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
358 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None, ignore_errors
=False):
359 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
360 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
361 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
362 raise exception upon error
365 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
366 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
367 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
368 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
369 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
370 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
371 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
372 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'
373 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
374 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
375 'user','passwd', 'dt.config as dt_config')
377 from_
= 'datacenters as d'
378 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
380 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
383 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
384 'datacenter_id': vim
.get('datacenter_id'),
385 '_vim_type_internal': vim
.get('type')}
387 extra
.update(yaml
.load(vim
["config"]))
388 if vim
.get('dt_config'):
389 extra
.update(yaml
.load(vim
["dt_config"]))
390 if vim
["type"] not in vimconn_imported
:
393 module
= "vimconn_" + vim
["type"]
394 pkg
= __import__("osm_ro." + module
)
395 vim_conn
= getattr(pkg
, module
)
396 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
397 # vim_conn = imp.load_module(vim["type"], *module_info)
398 vimconn_imported
[vim
["type"]] = vim_conn
399 except (IOError, ImportError) as e
:
400 # if module_info and module_info[0]:
401 # file.close(module_info[0])
403 logger
.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
404 vim
["type"], module
, type(e
).__name
__, str(e
)))
406 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
407 vim
["type"], module
, type(e
).__name
__, str(e
)), httperrors
.Bad_Request
)
410 if 'datacenter_tenant_id' in vim
:
411 thread_id
= vim
["datacenter_tenant_id"]
412 if thread_id
not in vim_persistent_info
:
413 vim_persistent_info
[thread_id
] = {}
414 persistent_info
= vim_persistent_info
[thread_id
]
418 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
419 vim_dict
[ vim
['datacenter_id'] ] = vimconn_imported
[ vim
["type"] ].vimconnector(
420 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
421 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
422 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
423 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
424 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
425 config
=extra
, persistent_info
=persistent_info
427 except Exception as e
:
429 logger
.error("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)))
431 http_code
= httperrors
.Internal_Server_Error
432 if isinstance(e
, vimconn
.vimconnException
):
433 http_code
= e
.http_code
434 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), http_code
)
436 except db_base_Exception
as e
:
437 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
440 def rollback(mydb
, vims
, rollback_list
):
442 #delete things by reverse order
443 for i
in range(len(rollback_list
)-1, -1, -1):
444 item
= rollback_list
[i
]
445 if item
["where"]=="vim":
446 if item
["vim_id"] not in vims
:
448 if is_task_id(item
["uuid"]):
450 vim
= vims
[item
["vim_id"]]
452 if item
["what"]=="image":
453 vim
.delete_image(item
["uuid"])
454 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
455 elif item
["what"]=="flavor":
456 vim
.delete_flavor(item
["uuid"])
457 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
458 elif item
["what"]=="network":
459 vim
.delete_network(item
["uuid"])
460 elif item
["what"]=="vm":
461 vim
.delete_vminstance(item
["uuid"])
462 except vimconn
.vimconnException
as e
:
463 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
464 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
465 except db_base_Exception
as e
:
466 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
470 if item
["what"]=="image":
471 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
472 elif item
["what"]=="flavor":
473 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
474 except db_base_Exception
as e
:
475 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
476 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
477 if len(undeleted_items
)==0:
478 return True," Rollback successful."
480 return False," Rollback fails to delete: " + str(undeleted_items
)
483 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
485 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
487 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
489 #dataplane interfaces
490 for numa
in vnfc
.get("numas",() ):
491 for interface
in numa
.get("interfaces",()):
492 if interface
["name"] in name_dict
:
494 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
495 vnfc
["name"], interface
["name"]),
496 httperrors
.Bad_Request
)
497 name_dict
[ interface
["name"] ] = "underlay"
499 for interface
in vnfc
.get("bridge-ifaces",() ):
500 if interface
["name"] in name_dict
:
502 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
503 vnfc
["name"], interface
["name"]),
504 httperrors
.Bad_Request
)
505 name_dict
[ interface
["name"] ] = "overlay"
506 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
507 # check bood-data info
508 # if "boot-data" in vnfc:
509 # # check that user-data is incompatible with users and config-files
510 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
511 # raise NfvoException(
512 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
513 # httperrors.Bad_Request)
515 #check if the info in external_connections matches with the one in the vnfcs
517 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
518 if external_connection
["name"] in name_list
:
520 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
521 external_connection
["name"]),
522 httperrors
.Bad_Request
)
523 name_list
.append(external_connection
["name"])
524 if external_connection
["VNFC"] not in vnfc_interfaces
:
526 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
527 external_connection
["name"], external_connection
["VNFC"]),
528 httperrors
.Bad_Request
)
530 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
532 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
533 external_connection
["name"],
534 external_connection
["local_iface_name"]),
535 httperrors
.Bad_Request
)
537 #check if the info in internal_connections matches with the one in the vnfcs
539 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
540 if internal_connection
["name"] in name_list
:
542 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
543 internal_connection
["name"]),
544 httperrors
.Bad_Request
)
545 name_list
.append(internal_connection
["name"])
546 #We should check that internal-connections of type "ptp" have only 2 elements
548 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
550 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
551 internal_connection
["name"],
552 'ptp' if vnf_descriptor_version
==1 else 'e-line',
553 'data' if vnf_descriptor_version
==1 else "e-lan"),
554 httperrors
.Bad_Request
)
555 for port
in internal_connection
["elements"]:
557 iface
= port
["local_iface_name"]
558 if vnf
not in vnfc_interfaces
:
560 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
561 internal_connection
["name"], vnf
),
562 httperrors
.Bad_Request
)
563 if iface
not in vnfc_interfaces
[ vnf
]:
565 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
566 internal_connection
["name"], iface
),
567 httperrors
.Bad_Request
)
568 return -httperrors
.Bad_Request
,
569 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
570 if vnfc_interfaces
[vnf
][iface
] == "overlay":
571 internal_connection
["type"] = "bridge"
573 internal_connection
["type"] = "data"
574 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
575 if vnfc_interfaces
[vnf
][iface
] == "overlay":
576 internal_connection
["implementation"] = "overlay"
578 internal_connection
["implementation"] = "underlay"
579 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
580 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
582 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
583 internal_connection
["name"],
584 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
585 'data' if vnf_descriptor_version
==1 else 'underlay'),
586 httperrors
.Bad_Request
)
587 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
588 vnfc_interfaces
[vnf
][iface
] == "underlay":
590 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
591 internal_connection
["name"], iface
,
592 'data' if vnf_descriptor_version
==1 else 'underlay',
593 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
594 httperrors
.Bad_Request
)
597 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
599 if only_create_at_vim
:
600 image_mano_id
= image_dict
['uuid']
601 if return_on_error
== None:
602 return_on_error
= True
604 if image_dict
['location']:
605 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
607 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
609 image_mano_id
= images
[0]['uuid']
611 #create image in MANO DB
612 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
613 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
614 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
616 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
617 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
618 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
619 #create image at every vim
620 for vim_id
,vim
in vims
.iteritems():
621 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
622 image_created
="false"
624 image_db
= mydb
.get_rows(FROM
="datacenters_images",
625 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
626 #look at VIM if this image exist
628 if image_dict
['location'] is not None:
629 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
632 filter_dict
['name'] = image_dict
['universal_name']
633 if image_dict
.get('checksum') != None:
634 filter_dict
['checksum'] = image_dict
['checksum']
635 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
636 vim_images
= vim
.get_image_list(filter_dict
)
637 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
638 if len(vim_images
) > 1:
639 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), httperrors
.Conflict
)
640 elif len(vim_images
) == 0:
641 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
643 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
644 image_vim_id
= vim_images
[0]['id']
646 except vimconn
.vimconnNotFoundException
as e
:
647 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
649 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
650 if image_dict
['location']:
651 image_vim_id
= vim
.new_image(image_dict
)
652 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
655 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
656 raise vimconn
.vimconnException(str(e
))
657 except vimconn
.vimconnException
as e
:
659 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
662 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
664 except vimconn
.vimconnException
as e
:
666 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
668 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
671 #if we reach here, the image has been created or existed
673 #add new vim_id at datacenters_images
674 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
675 'image_id':image_mano_id
,
676 'vim_id': image_vim_id
,
677 'created':image_created
})
678 elif image_db
[0]["vim_id"]!=image_vim_id
:
679 #modify existing vim_id at datacenters_images
680 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
682 return image_vim_id
if only_create_at_vim
else image_mano_id
685 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
686 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',0),
687 'ram':flavor_dict
.get('ram'),
688 'vcpus':flavor_dict
.get('vcpus'),
690 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
691 del flavor_dict
['extended']
692 if 'extended' in flavor_dict
:
693 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
695 #look if flavor exist
696 if only_create_at_vim
:
697 flavor_mano_id
= flavor_dict
['uuid']
698 if return_on_error
== None:
699 return_on_error
= True
701 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
703 flavor_mano_id
= flavors
[0]['uuid']
706 #create one by one the images of aditional disks
707 dev_image_list
=[] #list of images
708 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
710 for device
in flavor_dict
['extended'].get('devices',[]):
711 if "image" not in device
and "image name" not in device
:
714 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
715 image_dict
['universal_name']=device
.get('image name')
716 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
717 image_dict
['location']=device
.get('image')
718 #image_dict['new_location']=vnfc.get('image location')
719 image_dict
['checksum']=device
.get('image checksum')
720 image_metadata_dict
= device
.get('image metadata', None)
721 image_metadata_str
= None
722 if image_metadata_dict
!= None:
723 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
724 image_dict
['metadata']=image_metadata_str
725 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
726 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
727 dev_image_list
.append(image_id
)
729 temp_flavor_dict
['name'] = flavor_dict
['name']
730 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
731 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
732 flavor_mano_id
= content
733 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
734 #create flavor at every vim
735 if 'uuid' in flavor_dict
:
736 del flavor_dict
['uuid']
738 for vim_id
,vim
in vims
.items():
739 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
740 flavor_created
="false"
742 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
743 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
744 #look at VIM if this flavor exist SKIPPED
745 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
747 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
751 # Create the flavor in VIM
752 # Translate images at devices from MANO id to VIM id
754 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
755 # make a copy of original devices
758 for device
in flavor_dict
["extended"].get("devices",[]):
761 devices_original
.append(dev
)
762 if 'image' in device
:
764 if 'image metadata' in device
:
765 del device
['image metadata']
766 if 'image checksum' in device
:
767 del device
['image checksum']
769 for index
in range(0,len(devices_original
)) :
770 device
=devices_original
[index
]
771 if "image" not in device
and "image name" not in device
:
772 # if 'size' in device:
773 disk_list
.append({'size': device
.get('size', default_volume_size
), 'name': device
.get('name')})
776 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
777 image_dict
['universal_name']=device
.get('image name')
778 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
779 image_dict
['location']=device
.get('image')
780 # image_dict['new_location']=device.get('image location')
781 image_dict
['checksum']=device
.get('image checksum')
782 image_metadata_dict
= device
.get('image metadata', None)
783 image_metadata_str
= None
784 if image_metadata_dict
!= None:
785 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
786 image_dict
['metadata']=image_metadata_str
787 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
788 image_dict
["uuid"]=image_mano_id
789 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
791 #save disk information (image must be based on and size
792 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
794 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
797 #check that this vim_id exist in VIM, if not create
798 flavor_vim_id
=flavor_db
[0]["vim_id"]
800 vim
.get_flavor(flavor_vim_id
)
801 continue #flavor exist
802 except vimconn
.vimconnException
:
804 #create flavor at vim
805 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
808 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
809 flavor_create
="false"
810 except vimconn
.vimconnException
as e
:
813 if not flavor_vim_id
:
814 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
815 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
816 flavor_created
="true"
817 except vimconn
.vimconnException
as e
:
819 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
821 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
824 #if reach here the flavor has been create or exist
825 if len(flavor_db
)==0:
826 #add new vim_id at datacenters_flavors
827 extended_devices_yaml
= None
828 if len(disk_list
) > 0:
829 extended_devices
= dict()
830 extended_devices
['disks'] = disk_list
831 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
832 mydb
.new_row('datacenters_flavors',
833 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
834 'created': flavor_created
, 'extended': extended_devices_yaml
})
835 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
836 #modify existing vim_id at datacenters_flavors
837 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
838 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
840 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
843 def get_str(obj
, field
, length
):
845 Obtain the str value,
850 value
= obj
.get(field
)
851 if value
is not None:
852 value
= str(value
)[:length
]
855 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
857 fill image content at db_image dictionary. Check if the image with this image and checksum exist
858 :param db_image: dictionary to insert data
859 :param mydb: database connector
860 :param descriptor: yang descriptor
861 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
864 db_image
["name"] = get_str(descriptor
, "image", 255)
865 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
866 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
867 db_image
["checksum"] = None
868 if db_image
["name"].startswith("/"):
869 db_image
["location"] = db_image
["name"]
870 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
872 db_image
["universal_name"] = db_image
["name"]
873 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
874 'checksum': db_image
['checksum']})
876 return existing_images
[0]["uuid"]
878 image_uuid
= str(uuid4())
879 db_image
["uuid"] = image_uuid
882 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
884 Parses an OSM IM vnfd_catalog and insert at DB
887 :param vnf_descriptor:
888 :return: The list of cretated vnf ids
891 myvnfd
= vnfd_catalog
.vnfd()
893 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
, path_helper
=True)
894 except Exception as e
:
895 raise NfvoException("Error. Invalid VNF descriptor format " + str(e
), httperrors
.Bad_Request
)
903 db_ip_profiles_index
= 0
907 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd:vnfd-catalog")
908 if not vnfd_catalog_descriptor
:
909 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd-catalog")
910 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd")
911 if not vnfd_descriptor_list
:
912 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd:vnfd")
913 for vnfd_yang
in myvnfd
.vnfd_catalog
.vnfd
.itervalues():
914 vnfd
= vnfd_yang
.get()
917 vnf_uuid
= str(uuid4())
918 uuid_list
.append(vnf_uuid
)
919 vnfd_uuid_list
.append(vnf_uuid
)
920 vnfd_id
= get_str(vnfd
, "id", 255)
924 "name": get_str(vnfd
, "name", 255),
925 "description": get_str(vnfd
, "description", 255),
926 "tenant_id": tenant_id
,
927 "vendor": get_str(vnfd
, "vendor", 255),
928 "short_name": get_str(vnfd
, "short-name", 255),
929 "descriptor": str(vnf_descriptor
)[:60000]
932 for vnfd_descriptor
in vnfd_descriptor_list
:
933 if vnfd_descriptor
["id"] == str(vnfd
["id"]):
936 # table ip_profiles (ip-profiles)
937 ip_profile_name2db_table_index
= {}
938 for ip_profile
in vnfd
.get("ip-profiles").itervalues():
940 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
941 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
942 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
943 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
944 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
945 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
948 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
949 dns_list
.append(str(dns
.get("address")))
950 db_ip_profile
["dns_address"] = ";".join(dns_list
)
951 if ip_profile
["ip-profile-params"].get('security-group'):
952 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
953 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
954 db_ip_profiles_index
+= 1
955 db_ip_profiles
.append(db_ip_profile
)
957 # table nets (internal-vld)
958 net_id2uuid
= {} # for mapping interface with network
959 for vld
in vnfd
.get("internal-vld").itervalues():
960 net_uuid
= str(uuid4())
961 uuid_list
.append(net_uuid
)
963 "name": get_str(vld
, "name", 255),
966 "description": get_str(vld
, "description", 255),
967 "osm_id": get_str(vld
, "id", 255),
968 "type": "bridge", # TODO adjust depending on connection point type
970 net_id2uuid
[vld
.get("id")] = net_uuid
971 db_nets
.append(db_net
)
972 # ip-profile, link db_ip_profile with db_sce_net
973 if vld
.get("ip-profile-ref"):
974 ip_profile_name
= vld
.get("ip-profile-ref")
975 if ip_profile_name
not in ip_profile_name2db_table_index
:
976 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
977 "'{}'. Reference to a non-existing 'ip_profiles'".format(
978 str(vnfd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
979 httperrors
.Bad_Request
)
980 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["net_id"] = net_uuid
981 else: #check no ip-address has been defined
982 for icp
in vld
.get("internal-connection-point").itervalues():
983 if icp
.get("ip-address"):
984 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
985 "contains an ip-address but no ip-profile has been defined at VLD".format(
986 str(vnfd
["id"]), str(vld
["id"]), str(icp
["id"])),
987 httperrors
.Bad_Request
)
989 # connection points vaiable declaration
990 cp_name2iface_uuid
= {}
992 cp_name2db_interface
= {}
993 vdu_id2cp_name
= {} # stored only when one external connection point is presented at this VDU
997 vdu_id2db_table_index
= {}
998 for vdu
in vnfd
.get("vdu").itervalues():
1000 for vdu_descriptor
in vnfd_descriptor
["vdu"]:
1001 if vdu_descriptor
["id"] == str(vdu
["id"]):
1003 vm_uuid
= str(uuid4())
1004 uuid_list
.append(vm_uuid
)
1005 vdu_id
= get_str(vdu
, "id", 255)
1009 "name": get_str(vdu
, "name", 255),
1010 "description": get_str(vdu
, "description", 255),
1011 "pdu_type": get_str(vdu
, "pdu-type", 255),
1014 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
1015 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
1016 if vdu
.get("count"):
1017 db_vm
["count"] = int(vdu
["count"])
1020 image_present
= False
1021 if vdu
.get("image"):
1022 image_present
= True
1024 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
1026 image_uuid
= db_image
["uuid"]
1027 db_images
.append(db_image
)
1028 db_vm
["image_id"] = image_uuid
1029 if vdu
.get("alternative-images"):
1030 vm_alternative_images
= []
1031 for alt_image
in vdu
.get("alternative-images").itervalues():
1033 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, alt_image
)
1035 image_uuid
= db_image
["uuid"]
1036 db_images
.append(db_image
)
1037 vm_alternative_images
.append({
1038 "image_id": image_uuid
,
1039 "vim_type": str(alt_image
["vim-type"]),
1040 # "universal_name": str(alt_image["image"]),
1041 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1044 db_vm
["image_list"] = yaml
.safe_dump(vm_alternative_images
, default_flow_style
=True, width
=256)
1048 if vdu
.get("volumes"):
1049 for volume_key
in vdu
["volumes"]:
1050 volume
= vdu
["volumes"][volume_key
]
1051 if not image_present
:
1052 # Convert the first volume to vnfc.image
1053 image_present
= True
1055 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
1057 image_uuid
= db_image
["uuid"]
1058 db_images
.append(db_image
)
1059 db_vm
["image_id"] = image_uuid
1061 # Add Openmano devices
1062 device
= {"name": str(volume
.get("name"))}
1063 device
["type"] = str(volume
.get("device-type"))
1064 if volume
.get("size"):
1065 device
["size"] = int(volume
["size"])
1066 if volume
.get("image"):
1067 device
["image name"] = str(volume
["image"])
1068 if volume
.get("image-checksum"):
1069 device
["image checksum"] = str(volume
["image-checksum"])
1071 devices
.append(device
)
1073 if not db_vm
.get("image_id"):
1074 if not db_vm
["pdu_type"]:
1075 raise NfvoException("Not defined image for VDU")
1076 # create a fake image
1080 if vdu
.get("cloud-init"):
1081 boot_data
["user-data"] = str(vdu
["cloud-init"])
1082 elif vdu
.get("cloud-init-file"):
1083 # TODO Where this file content is present???
1084 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1085 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
1087 if vdu
.get("supplemental-boot-data"):
1088 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
1089 boot_data
['boot-data-drive'] = True
1090 if vdu
["supplemental-boot-data"].get('config-file'):
1091 om_cfgfile_list
= list()
1092 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].itervalues():
1093 # TODO Where this file content is present???
1094 cfg_source
= str(custom_config_file
["source"])
1095 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
1096 "content": cfg_source
})
1097 boot_data
['config-files'] = om_cfgfile_list
1099 db_vm
["boot_data"] = yaml
.safe_dump(boot_data
, default_flow_style
=True, width
=256)
1101 db_vms
.append(db_vm
)
1104 # table interfaces (internal/external interfaces)
1105 flavor_epa_interfaces
= []
1106 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1107 for iface
in vdu
.get("interface").itervalues():
1108 flavor_epa_interface
= {}
1109 iface_uuid
= str(uuid4())
1110 uuid_list
.append(iface_uuid
)
1113 "internal_name": get_str(iface
, "name", 255),
1116 flavor_epa_interface
["name"] = db_interface
["internal_name"]
1117 if iface
.get("virtual-interface").get("vpci"):
1118 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
1119 flavor_epa_interface
["vpci"] = db_interface
["vpci"]
1121 if iface
.get("virtual-interface").get("bandwidth"):
1122 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1123 db_interface
["bw"] = int(math
.ceil(bps
/1000000.0))
1124 flavor_epa_interface
["bandwidth"] = "{} Mbps".format(db_interface
["bw"])
1126 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1127 db_interface
["type"] = "mgmt"
1128 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1129 db_interface
["type"] = "bridge"
1130 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1131 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1132 db_interface
["type"] = "data"
1133 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1134 flavor_epa_interface
["dedicated"] = "no" if iface
["virtual-interface"]["type"] == "SR-IOV" \
1136 flavor_epa_interfaces
.append(flavor_epa_interface
)
1138 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1139 "-interface':'type':'{}'. Interface type is not supported".format(
1140 vnfd_id
, vdu_id
, iface
.get("virtual-interface").get("type")),
1141 httperrors
.Bad_Request
)
1143 if iface
.get("mgmt-interface"):
1144 db_interface
["type"] = "mgmt"
1146 if iface
.get("external-connection-point-ref"):
1148 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1149 db_interface
["external_name"] = get_str(cp
, "name", 255)
1150 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1151 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1152 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1153 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1154 if cp_descriptor
["name"] == db_interface
["external_name"]:
1159 if vdu_id
in vdu_id2cp_name
:
1160 vdu_id2cp_name
[vdu_id
] = None # more than two connecdtion point for this VDU
1162 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1165 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1166 db_interface
["port_security"] = 0
1167 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1168 db_interface
["port_security"] = 1
1170 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1171 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1172 " at connection-point".format(
1173 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1174 cp
=iface
.get("vnfd-connection-point-ref")),
1175 httperrors
.Bad_Request
)
1176 elif iface
.get("internal-connection-point-ref"):
1178 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1179 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1182 raise KeyError("does not exist at vdu:internal-connection-point")
1185 for vld
in vnfd
.get("internal-vld").itervalues():
1186 for cp
in vld
.get("internal-connection-point").itervalues():
1187 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1189 raise KeyError("is referenced by more than one 'internal-vld'")
1193 raise KeyError("is not referenced by any 'internal-vld'")
1195 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1196 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1197 db_interface
["port_security"] = 0
1198 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1199 db_interface
["port_security"] = 1
1200 if icp
.get("ip-address"):
1201 if not icp_vld
.get("ip-profile-ref"):
1203 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1204 except KeyError as e
:
1205 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1206 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1208 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1209 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1210 httperrors
.Bad_Request
)
1211 if iface
.get("position"):
1212 db_interface
["created_at"] = int(iface
.get("position")) * 50
1213 if iface
.get("mac-address"):
1214 db_interface
["mac"] = str(iface
.get("mac-address"))
1215 db_interfaces
.append(db_interface
)
1219 "name": get_str(vdu
, "name", 250) + "-flv",
1220 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1221 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1222 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1224 # TODO revise the case of several numa-node-policy node
1228 extended
["devices"] = devices
1229 if flavor_epa_interfaces
:
1230 numa
["interfaces"] = flavor_epa_interfaces
1231 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1232 epa_vcpu_set
= False
1233 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1234 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1235 if numa_node_policy
.get("node"):
1236 numa_node
= numa_node_policy
["node"].values()[0]
1237 if numa_node
.get("num-cores"):
1238 numa
["cores"] = numa_node
["num-cores"]
1240 if numa_node
.get("paired-threads"):
1241 if numa_node
["paired-threads"].get("num-paired-threads"):
1242 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1244 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1245 numa
["paired-threads-id"] = []
1246 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues():
1247 numa
["paired-threads-id"].append(
1248 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1250 if numa_node
.get("num-threads"):
1251 numa
["threads"] = int(numa_node
["num-threads"])
1253 if numa_node
.get("memory-mb"):
1254 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1255 if vdu
["guest-epa"].get("mempage-size"):
1256 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1257 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1258 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1259 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1260 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1261 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1262 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1264 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1266 extended
["numas"] = [numa
]
1268 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1269 db_flavor
["extended"] = extended_text
1270 # look if flavor exist
1271 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1272 'ram': db_flavor
.get('ram'),
1273 'vcpus': db_flavor
.get('vcpus'),
1274 'extended': db_flavor
.get('extended')
1276 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1277 if existing_flavors
:
1278 flavor_uuid
= existing_flavors
[0]["uuid"]
1280 flavor_uuid
= str(uuid4())
1281 uuid_list
.append(flavor_uuid
)
1282 db_flavor
["uuid"] = flavor_uuid
1283 db_flavors
.append(db_flavor
)
1284 db_vm
["flavor_id"] = flavor_uuid
1286 # VNF affinity and antiaffinity
1287 for pg
in vnfd
.get("placement-groups").itervalues():
1288 pg_name
= get_str(pg
, "name", 255)
1289 for vdu
in pg
.get("member-vdus").itervalues():
1290 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1291 if vdu_id
not in vdu_id2db_table_index
:
1292 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1293 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1294 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1295 httperrors
.Bad_Request
)
1296 if vdu_id2db_table_index
[vdu_id
]:
1297 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1298 # TODO consider the case of isolation and not colocation
1299 # if pg.get("strategy") == "ISOLATION":
1301 # VNF mgmt configuration
1303 if vnfd
["mgmt-interface"].get("vdu-id"):
1304 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1305 if mgmt_vdu_id
not in vdu_id2uuid
:
1306 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1307 "'{vdu}'. Reference to a non-existing vdu".format(
1308 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1309 httperrors
.Bad_Request
)
1310 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1311 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1312 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1313 if cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]:
1314 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1316 if vnfd
["mgmt-interface"].get("ip-address"):
1317 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1318 if vnfd
["mgmt-interface"].get("cp"):
1319 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1320 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1321 "Reference to a non-existing connection-point".format(
1322 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1323 httperrors
.Bad_Request
)
1324 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1325 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1326 # mark this interface as of type mgmt
1327 if cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]:
1328 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1330 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1334 mgmt_access
["default_user"] = default_user
1335 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1338 mgmt_access
["required"] = required
1341 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1343 db_vnfs
.append(db_vnf
)
1347 {"images": db_images
},
1348 {"flavors": db_flavors
},
1349 {"ip_profiles": db_ip_profiles
},
1351 {"interfaces": db_interfaces
},
1354 logger
.debug("create_vnf Deployment done vnfDict: %s",
1355 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1356 mydb
.new_rows(db_tables
, uuid_list
)
1357 return vnfd_uuid_list
1358 except NfvoException
:
1360 except Exception as e
:
1361 logger
.error("Exception {}".format(e
))
1362 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1365 @deprecated("Use new_vnfd_v3")
1366 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1367 global global_config
1369 # Step 1. Check the VNF descriptor
1370 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1371 # Step 2. Check tenant exist
1373 if tenant_id
!= "any":
1374 check_tenant(mydb
, tenant_id
)
1375 if "tenant_id" in vnf_descriptor
["vnf"]:
1376 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1377 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1378 httperrors
.Unauthorized
)
1380 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1381 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1382 if global_config
["auto_push_VNF_to_VIMs"]:
1383 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1385 # Step 4. Review the descriptor and add missing fields
1386 #print vnf_descriptor
1387 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1388 vnf_name
= vnf_descriptor
['vnf']['name']
1389 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1390 if "physical" in vnf_descriptor
['vnf']:
1391 del vnf_descriptor
['vnf']['physical']
1392 #print vnf_descriptor
1394 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1395 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1396 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1398 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1399 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1400 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1402 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1403 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1405 VNFCitem
["name"] = vnfc
['name']
1406 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1407 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1409 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1412 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1413 myflavorDict
["description"] = VNFCitem
["description"]
1414 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1415 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1416 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1417 myflavorDict
["extended"] = {}
1419 devices
= vnfc
.get("devices")
1421 myflavorDict
["extended"]["devices"] = devices
1424 # 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
1425 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1427 # Previous code has been commented
1428 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1429 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1430 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1431 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1433 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1435 # print "Error creating flavor: unknown processor model. Rollback successful."
1436 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1438 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1439 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1441 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1442 myflavorDict
['extended']['numas'] = vnfc
['numas']
1446 # Step 6.2 New flavors are created in the VIM
1447 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1449 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1450 VNFCitem
["flavor_id"] = flavor_id
1451 VNFCDict
[vnfc
['name']] = VNFCitem
1453 logger
.debug("Creating new images in the VIM for each VNFC")
1454 # Step 6.3 New images are created in the VIM
1455 #For each VNFC, we must create the appropriate image.
1456 #This "for" loop might be integrated with the previous one
1457 #In case this integration is made, the VNFCDict might become a VNFClist.
1458 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1459 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1461 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1462 image_dict
['universal_name']=vnfc
.get('image name')
1463 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1464 image_dict
['location']=vnfc
.get('VNFC image')
1465 #image_dict['new_location']=vnfc.get('image location')
1466 image_dict
['checksum']=vnfc
.get('image checksum')
1467 image_metadata_dict
= vnfc
.get('image metadata', None)
1468 image_metadata_str
= None
1469 if image_metadata_dict
is not None:
1470 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1471 image_dict
['metadata']=image_metadata_str
1472 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1473 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1474 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1475 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1476 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1477 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1478 if vnfc
.get("boot-data"):
1479 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1482 # Step 7. Storing the VNF descriptor in the repository
1483 if "descriptor" not in vnf_descriptor
["vnf"]:
1484 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1486 # Step 8. Adding the VNF to the NFVO DB
1487 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1489 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1490 _
, message
= rollback(mydb
, vims
, rollback_list
)
1491 if isinstance(e
, db_base_Exception
):
1492 error_text
= "Exception at database"
1493 elif isinstance(e
, KeyError):
1494 error_text
= "KeyError exception "
1495 e
.http_code
= httperrors
.Internal_Server_Error
1497 error_text
= "Exception at VIM"
1498 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1499 #logger.error("start_scenario %s", error_text)
1500 raise NfvoException(error_text
, e
.http_code
)
1503 @deprecated("Use new_vnfd_v3")
1504 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1505 global global_config
1507 # Step 1. Check the VNF descriptor
1508 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1509 # Step 2. Check tenant exist
1511 if tenant_id
!= "any":
1512 check_tenant(mydb
, tenant_id
)
1513 if "tenant_id" in vnf_descriptor
["vnf"]:
1514 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1515 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1516 httperrors
.Unauthorized
)
1518 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1519 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1520 if global_config
["auto_push_VNF_to_VIMs"]:
1521 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1523 # Step 4. Review the descriptor and add missing fields
1524 #print vnf_descriptor
1525 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1526 vnf_name
= vnf_descriptor
['vnf']['name']
1527 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1528 if "physical" in vnf_descriptor
['vnf']:
1529 del vnf_descriptor
['vnf']['physical']
1530 #print vnf_descriptor
1532 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1533 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1534 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1536 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1537 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1538 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1540 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1541 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1543 VNFCitem
["name"] = vnfc
['name']
1544 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1546 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1549 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1550 myflavorDict
["description"] = VNFCitem
["description"]
1551 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1552 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1553 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1554 myflavorDict
["extended"] = {}
1556 devices
= vnfc
.get("devices")
1558 myflavorDict
["extended"]["devices"] = devices
1561 # 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
1562 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1564 # Previous code has been commented
1565 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1566 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1567 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1568 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1570 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1572 # print "Error creating flavor: unknown processor model. Rollback successful."
1573 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1575 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1576 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1578 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1579 myflavorDict
['extended']['numas'] = vnfc
['numas']
1583 # Step 6.2 New flavors are created in the VIM
1584 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1586 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1587 VNFCitem
["flavor_id"] = flavor_id
1588 VNFCDict
[vnfc
['name']] = VNFCitem
1590 logger
.debug("Creating new images in the VIM for each VNFC")
1591 # Step 6.3 New images are created in the VIM
1592 #For each VNFC, we must create the appropriate image.
1593 #This "for" loop might be integrated with the previous one
1594 #In case this integration is made, the VNFCDict might become a VNFClist.
1595 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1596 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1598 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1599 image_dict
['universal_name']=vnfc
.get('image name')
1600 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1601 image_dict
['location']=vnfc
.get('VNFC image')
1602 #image_dict['new_location']=vnfc.get('image location')
1603 image_dict
['checksum']=vnfc
.get('image checksum')
1604 image_metadata_dict
= vnfc
.get('image metadata', None)
1605 image_metadata_str
= None
1606 if image_metadata_dict
is not None:
1607 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1608 image_dict
['metadata']=image_metadata_str
1609 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1610 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1611 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1612 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1613 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1614 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1615 if vnfc
.get("boot-data"):
1616 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1618 # Step 7. Storing the VNF descriptor in the repository
1619 if "descriptor" not in vnf_descriptor
["vnf"]:
1620 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1622 # Step 8. Adding the VNF to the NFVO DB
1623 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1625 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1626 _
, message
= rollback(mydb
, vims
, rollback_list
)
1627 if isinstance(e
, db_base_Exception
):
1628 error_text
= "Exception at database"
1629 elif isinstance(e
, KeyError):
1630 error_text
= "KeyError exception "
1631 e
.http_code
= httperrors
.Internal_Server_Error
1633 error_text
= "Exception at VIM"
1634 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1635 #logger.error("start_scenario %s", error_text)
1636 raise NfvoException(error_text
, e
.http_code
)
1639 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1640 #check valid tenant_id
1641 check_tenant(mydb
, tenant_id
)
1644 if tenant_id
!= "any":
1645 where_or
["tenant_id"] = tenant_id
1646 where_or
["public"] = True
1647 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1649 vnf_id
= vnf
["uuid"]
1650 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1651 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1652 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1653 data
={'vnf' : filtered_content
}
1655 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1656 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1658 WHERE
={'vnfs.uuid': vnf_id
} )
1659 if len(content
) != 0:
1660 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1661 # change boot_data into boot-data
1663 if vm
.get("boot_data"):
1664 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1667 data
['vnf']['VNFC'] = content
1668 #TODO: GET all the information from a VNFC and include it in the output.
1671 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1672 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1673 WHERE
={'vnfs.uuid': vnf_id
} )
1674 data
['vnf']['nets'] = content
1676 #GET ip-profile for each net
1677 for net
in data
['vnf']['nets']:
1678 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1679 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1680 WHERE
={'net_id': net
["uuid"]} )
1681 if len(ipprofiles
)==1:
1682 net
["ip_profile"] = ipprofiles
[0]
1683 elif len(ipprofiles
)>1:
1684 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
1687 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1689 #GET External Interfaces
1690 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1691 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1692 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1693 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1695 data
['vnf']['external-connections'] = content
1700 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1701 # Check tenant exist
1702 if tenant_id
!= "any":
1703 check_tenant(mydb
, tenant_id
)
1704 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1705 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1709 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1711 if tenant_id
!= "any":
1712 where_or
["tenant_id"] = tenant_id
1713 where_or
["public"] = True
1714 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1715 vnf_id
= vnf
["uuid"]
1717 # "Getting the list of flavors and tenants of the VNF"
1718 flavorList
= get_flavorlist(mydb
, vnf_id
)
1719 if len(flavorList
)==0:
1720 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1722 imageList
= get_imagelist(mydb
, vnf_id
)
1723 if len(imageList
)==0:
1724 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1726 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1728 raise NfvoException("vnf '{}' not found".format(vnf_id
), httperrors
.Not_Found
)
1731 for flavor
in flavorList
:
1732 #check if flavor is used by other vnf
1734 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1736 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1738 #flavor not used, must be deleted
1740 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1741 for flavor_vim
in c
:
1742 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1746 for vim
in vims
.values():
1747 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1753 myvim
.delete_flavor(flavor_vim
["vim_id"])
1754 except vimconn
.vimconnNotFoundException
:
1755 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1756 flavor_vim
["datacenter_vim_id"] )
1757 except vimconn
.vimconnException
as e
:
1758 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1759 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1760 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1761 flavor_vim
["datacenter_vim_id"]))
1762 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1763 mydb
.delete_row_by_id('flavors', flavor
)
1764 except db_base_Exception
as e
:
1765 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1766 undeletedItems
.append("flavor {}".format(flavor
))
1769 for image
in imageList
:
1771 #check if image is used by other vnf
1772 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1774 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1776 #image not used, must be deleted
1778 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1780 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1782 if image_vim
['created']=='false': #skip this image because not created by openmano
1784 myvim
=vims
[ image_vim
["datacenter_id"] ]
1786 myvim
.delete_image(image_vim
["vim_id"])
1787 except vimconn
.vimconnNotFoundException
as e
:
1788 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1789 except vimconn
.vimconnException
as e
:
1790 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1791 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1792 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1793 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1794 mydb
.delete_row_by_id('images', image
)
1795 except db_base_Exception
as e
:
1796 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1797 undeletedItems
.append("image %s" % image
)
1799 return vnf_id
+ " " + vnf
["name"]
1801 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1804 @deprecated("Not used")
1805 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1806 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1810 return -httperrors
.Not_Found
, "datacenter '%s' not found" % datacenter_name
1811 myvim
= vims
.values()[0]
1812 result
,servers
= myvim
.get_hosts_info()
1814 return result
, servers
1815 topology
= {'name':myvim
['name'] , 'servers': servers
}
1816 return result
, topology
1819 def get_hosts(mydb
, nfvo_tenant_id
):
1820 vims
= get_vim(mydb
, nfvo_tenant_id
)
1822 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), httperrors
.Not_Found
)
1824 #print "nfvo.datacenter_action() error. Several datacenters found"
1825 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
1826 myvim
= vims
.values()[0]
1828 hosts
= myvim
.get_hosts()
1829 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1831 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1833 server
={'name':host
['name'], 'vms':[]}
1834 for vm
in host
['instances']:
1835 #get internal name and model
1837 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1838 WHERE
={'vim_vm_id':vm
['id']} )
1840 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1842 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1844 except db_base_Exception
as e
:
1845 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1846 datacenter
['Datacenters'][0]['servers'].append(server
)
1847 #return -400, "en construccion"
1849 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1851 except vimconn
.vimconnException
as e
:
1852 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1855 @deprecated("Use new_nsd_v3")
1856 def new_scenario(mydb
, tenant_id
, topo
):
1858 # result, vims = get_vim(mydb, tenant_id)
1860 # return result, vims
1862 if tenant_id
!= "any":
1863 check_tenant(mydb
, tenant_id
)
1864 if "tenant_id" in topo
:
1865 if topo
["tenant_id"] != tenant_id
:
1866 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1867 httperrors
.Unauthorized
)
1871 #1.1: get VNFs and external_networks (other_nets).
1873 other_nets
={} #external_networks, bridge_networks and data_networkds
1874 nodes
= topo
['topology']['nodes']
1875 for k
in nodes
.keys():
1876 if nodes
[k
]['type'] == 'VNF':
1878 vnfs
[k
]['ifaces'] = {}
1879 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1880 other_nets
[k
] = nodes
[k
]
1881 other_nets
[k
]['external']=True
1882 elif nodes
[k
]['type'] == 'network':
1883 other_nets
[k
] = nodes
[k
]
1884 other_nets
[k
]['external']=False
1887 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1888 for name
,vnf
in vnfs
.items():
1889 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1891 error_pos
= "'topology':'nodes':'" + name
+ "'"
1893 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1894 where
['uuid'] = vnf
['vnf_id']
1895 if 'VNF model' in vnf
:
1896 error_text
+= " 'VNF model' " + vnf
['VNF model']
1897 where
['name'] = vnf
['VNF model']
1899 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, httperrors
.Bad_Request
)
1901 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1905 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
1907 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
1908 vnf
['uuid']=vnf_db
[0]['uuid']
1909 vnf
['description']=vnf_db
[0]['description']
1910 #get external interfaces
1911 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1912 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1913 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1914 for ext_iface
in ext_ifaces
:
1915 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1917 #1.4 get list of connections
1918 conections
= topo
['topology']['connections']
1919 conections_list
= []
1920 conections_list_name
= []
1921 for k
in conections
.keys():
1922 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1923 ifaces_list
= conections
[k
]['nodes'].items()
1924 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1926 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1927 for k2
in conection_pair_list
:
1930 con_type
= conections
[k
].get("type", "link")
1931 if con_type
!= "link":
1933 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), httperrors
.Bad_Request
)
1934 other_nets
[k
] = {'external': False}
1935 if conections
[k
].get("graph"):
1936 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1937 ifaces_list
.append( (k
, None) )
1940 if con_type
== "external_network":
1941 other_nets
[k
]['external'] = True
1942 if conections
[k
].get("model"):
1943 other_nets
[k
]["model"] = conections
[k
]["model"]
1945 other_nets
[k
]["model"] = k
1946 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1947 other_nets
[k
]["model"] = con_type
1949 conections_list_name
.append(k
)
1950 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)
1951 #print set(ifaces_list)
1952 #check valid VNF and iface names
1953 for iface
in ifaces_list
:
1954 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1955 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1956 str(k
), iface
[0]), httperrors
.Not_Found
)
1957 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1958 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1959 str(k
), iface
[0], iface
[1]), httperrors
.Not_Found
)
1961 #1.5 unify connections from the pair list to a consolidated list
1963 while index
< len(conections_list
):
1965 while index2
< len(conections_list
):
1966 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1967 conections_list
[index
] |
= conections_list
[index2
]
1968 del conections_list
[index2
]
1969 del conections_list_name
[index2
]
1972 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1974 #for k in conections_list:
1979 #1.6 Delete non external nets
1980 # for k in other_nets.keys():
1981 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1982 # for con in conections_list:
1984 # for index in range(0,len(con)):
1985 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1986 # for index in delete_indexes:
1989 #1.7: Check external_ports are present at database table datacenter_nets
1990 for k
,net
in other_nets
.items():
1991 error_pos
= "'topology':'nodes':'" + k
+ "'"
1992 if net
['external']==False:
1993 if 'name' not in net
:
1995 if 'model' not in net
:
1996 raise NfvoException("needed a 'model' at " + error_pos
, httperrors
.Bad_Request
)
1997 if net
['model']=='bridge_net':
1998 net
['type']='bridge';
1999 elif net
['model']=='dataplane_net':
2002 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, httperrors
.Not_Found
)
2004 #IF we do not want to check that external network exist at datacenter
2009 # if 'net_id' in net:
2010 # error_text += " 'net_id' " + net['net_id']
2011 # WHERE_['uuid'] = net['net_id']
2012 # if 'model' in net:
2013 # error_text += " 'model' " + net['model']
2014 # WHERE_['name'] = net['model']
2015 # if len(WHERE_) == 0:
2016 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2017 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2018 # FROM='datacenter_nets', WHERE=WHERE_ )
2020 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2022 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2023 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2025 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2026 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2027 # other_nets[k].update(net_db[0])
2030 net_nb
=0 #Number of nets
2031 for con
in conections_list
:
2032 #check if this is connected to a external net
2036 for index
in range(0,len(con
)):
2037 #check if this is connected to a external net
2038 for net_key
in other_nets
.keys():
2039 if con
[index
][0]==net_key
:
2040 if other_net_index
>=0:
2041 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
2042 #print "nfvo.new_scenario " + error_text
2043 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2045 other_net_index
= index
2046 net_target
= net_key
2048 #print "other_net_index", other_net_index
2050 if other_net_index
>=0:
2051 del con
[other_net_index
]
2052 #IF we do not want to check that external network exist at datacenter
2053 if other_nets
[net_target
]['external'] :
2054 if "name" not in other_nets
[net_target
]:
2055 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
2056 if other_nets
[net_target
]["type"] == "external_network":
2057 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
2058 other_nets
[net_target
]["type"] = "data"
2060 other_nets
[net_target
]["type"] = "bridge"
2062 # if other_nets[net_target]['external'] :
2063 # 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
2064 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2065 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2066 # print "nfvo.new_scenario " + error_text
2067 # return -httperrors.Bad_Request, error_text
2070 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2073 net_type_bridge
=False
2075 net_target
= "__-__net"+str(net_nb
)
2076 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2077 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
2080 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2081 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2082 if iface_type
=='mgmt' or iface_type
=='bridge':
2083 net_type_bridge
= True
2085 net_type_data
= True
2086 if net_type_bridge
and net_type_data
:
2087 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
2088 #print "nfvo.new_scenario " + error_text
2089 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2090 elif net_type_bridge
:
2093 type_
='data' if len(con
)>2 else 'ptp'
2094 net_list
[net_target
]['type'] = type_
2097 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
2098 #print "nfvo.new_scenario " + error_text
2100 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2102 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2103 #1.8.1 obtain management net
2104 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2105 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2106 #1.8.2 check all interfaces from all vnfs
2108 add_mgmt_net
= False
2109 for vnf
in vnfs
.values():
2110 for iface
in vnf
['ifaces'].values():
2111 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2112 #iface not connected
2113 iface
['net_key'] = 'mgmt'
2115 if add_mgmt_net
and 'mgmt' not in net_list
:
2116 net_list
['mgmt']=mgmt_net
[0]
2117 net_list
['mgmt']['external']=True
2118 net_list
['mgmt']['graph']={'visible':False}
2120 net_list
.update(other_nets
)
2122 #print 'net_list', net_list
2127 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2128 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2129 'tenant_id':tenant_id
, 'name':topo
['name'],
2130 'description':topo
.get('description',topo
['name']),
2131 'public': topo
.get('public', False)
2137 @deprecated("Use new_nsd_v3")
2138 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2139 """ This creates a new scenario for version 0.2 and 0.3"""
2140 scenario
= scenario_dict
["scenario"]
2141 if tenant_id
!= "any":
2142 check_tenant(mydb
, tenant_id
)
2143 if "tenant_id" in scenario
:
2144 if scenario
["tenant_id"] != tenant_id
:
2145 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2146 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2147 scenario
["tenant_id"], tenant_id
), httperrors
.Unauthorized
)
2151 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2152 for name
,vnf
in scenario
["vnfs"].iteritems():
2153 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2155 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2157 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2158 where
['uuid'] = vnf
['vnf_id']
2159 if 'vnf_name' in vnf
:
2160 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2161 where
['name'] = vnf
['vnf_name']
2163 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, httperrors
.Bad_Request
)
2164 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2167 if len(vnf_db
) == 0:
2168 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
2169 elif len(vnf_db
) > 1:
2170 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
2171 vnf
['uuid'] = vnf_db
[0]['uuid']
2172 vnf
['description'] = vnf_db
[0]['description']
2174 # get external interfaces
2175 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2176 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2177 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2178 for ext_iface
in ext_ifaces
:
2179 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2180 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2182 # 2: Insert net_key and ip_address at every vnf interface
2183 for net_name
, net
in scenario
["networks"].items():
2184 net_type_bridge
= False
2185 net_type_data
= False
2186 for iface_dict
in net
["interfaces"]:
2187 if version
== "0.2":
2188 temp_dict
= iface_dict
2190 elif version
== "0.3":
2191 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2192 ip_address
= iface_dict
.get('ip_address', None)
2193 for vnf
, iface
in temp_dict
.items():
2194 if vnf
not in scenario
["vnfs"]:
2195 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2197 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2198 raise NfvoException(error_text
, httperrors
.Not_Found
)
2199 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2200 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2201 .format(net_name
, iface
)
2202 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2203 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2204 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2205 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2206 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2207 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2208 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2209 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2210 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2211 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2212 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2213 net_type_bridge
= True
2215 net_type_data
= True
2217 if net_type_bridge
and net_type_data
:
2218 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2220 # logger.debug("nfvo.new_scenario " + error_text)
2221 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2222 elif net_type_bridge
:
2225 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2227 if net
.get("implementation"): # for v0.3
2228 if type_
== "bridge" and net
["implementation"] == "underlay":
2229 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2230 "'network':'{}'".format(net_name
)
2231 # logger.debug(error_text)
2232 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2233 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2234 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2235 "'network':'{}'".format(net_name
)
2236 # logger.debug(error_text)
2237 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2238 net
.pop("implementation")
2239 if "type" in net
and version
== "0.3": # for v0.3
2240 if type_
== "data" and net
["type"] == "e-line":
2241 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2242 "'e-line' at 'network':'{}'".format(net_name
)
2243 # logger.debug(error_text)
2244 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2245 elif type_
== "ptp" and net
["type"] == "e-lan":
2249 net
['name'] = net_name
2250 net
['external'] = net
.get('external', False)
2252 # 3: insert at database
2253 scenario
["nets"] = scenario
["networks"]
2254 scenario
['tenant_id'] = tenant_id
2255 scenario_id
= mydb
.new_scenario(scenario
)
2259 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2261 Parses an OSM IM nsd_catalog and insert at DB
2264 :param nsd_descriptor:
2265 :return: The list of created NSD ids
2268 mynsd
= nsd_catalog
.nsd()
2270 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
2271 except Exception as e
:
2272 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), httperrors
.Bad_Request
)
2276 db_sce_interfaces
= []
2279 db_sce_rsp_hops
= []
2280 db_sce_classifiers
= []
2281 db_sce_classifier_matches
= []
2283 db_ip_profiles_index
= 0
2286 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.itervalues():
2287 nsd
= nsd_yang
.get()
2290 scenario_uuid
= str(uuid4())
2291 uuid_list
.append(scenario_uuid
)
2292 nsd_uuid_list
.append(scenario_uuid
)
2294 "uuid": scenario_uuid
,
2295 "osm_id": get_str(nsd
, "id", 255),
2296 "name": get_str(nsd
, "name", 255),
2297 "description": get_str(nsd
, "description", 255),
2298 "tenant_id": tenant_id
,
2299 "vendor": get_str(nsd
, "vendor", 255),
2300 "short_name": get_str(nsd
, "short-name", 255),
2301 "descriptor": str(nsd_descriptor
)[:60000],
2303 db_scenarios
.append(db_scenario
)
2305 # table sce_vnfs (constituent-vnfd)
2306 vnf_index2scevnf_uuid
= {}
2307 vnf_index2vnf_uuid
= {}
2308 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2309 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2310 'tenant_id': tenant_id
})
2311 if not existing_vnf
:
2312 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2313 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2314 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2315 httperrors
.Bad_Request
)
2316 sce_vnf_uuid
= str(uuid4())
2317 uuid_list
.append(sce_vnf_uuid
)
2319 "uuid": sce_vnf_uuid
,
2320 "scenario_id": scenario_uuid
,
2321 # "name": get_str(vnf, "member-vnf-index", 255),
2322 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2323 "vnf_id": existing_vnf
[0]["uuid"],
2324 "member_vnf_index": str(vnf
["member-vnf-index"]),
2325 # TODO 'start-by-default': True
2327 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2328 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2329 db_sce_vnfs
.append(db_sce_vnf
)
2331 # table ip_profiles (ip-profiles)
2332 ip_profile_name2db_table_index
= {}
2333 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2335 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2336 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2337 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2338 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2339 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2340 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2343 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2344 dns_list
.append(str(dns
.get("address")))
2345 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2346 if ip_profile
["ip-profile-params"].get('security-group'):
2347 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2348 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2349 db_ip_profiles_index
+= 1
2350 db_ip_profiles
.append(db_ip_profile
)
2352 # table sce_nets (internal-vld)
2353 for vld
in nsd
.get("vld").itervalues():
2354 sce_net_uuid
= str(uuid4())
2355 uuid_list
.append(sce_net_uuid
)
2357 "uuid": sce_net_uuid
,
2358 "name": get_str(vld
, "name", 255),
2359 "scenario_id": scenario_uuid
,
2361 "multipoint": not vld
.get("type") == "ELINE",
2362 "osm_id": get_str(vld
, "id", 255),
2364 "description": get_str(vld
, "description", 255),
2366 # guess type of network
2367 if vld
.get("mgmt-network"):
2368 db_sce_net
["type"] = "bridge"
2369 db_sce_net
["external"] = True
2370 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2371 db_sce_net
["type"] = "data"
2373 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2374 db_sce_net
["type"] = None
2375 db_sce_nets
.append(db_sce_net
)
2377 # ip-profile, link db_ip_profile with db_sce_net
2378 if vld
.get("ip-profile-ref"):
2379 ip_profile_name
= vld
.get("ip-profile-ref")
2380 if ip_profile_name
not in ip_profile_name2db_table_index
:
2381 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2382 " Reference to a non-existing 'ip_profiles'".format(
2383 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2384 httperrors
.Bad_Request
)
2385 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2386 elif vld
.get("vim-network-name"):
2387 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2389 # table sce_interfaces (vld:vnfd-connection-point-ref)
2390 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2391 vnf_index
= str(iface
['member-vnf-index-ref'])
2392 # check correct parameters
2393 if vnf_index
not in vnf_index2vnf_uuid
:
2394 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2395 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2396 "'nsd':'constituent-vnfd'".format(
2397 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2398 httperrors
.Bad_Request
)
2400 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2401 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2402 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2403 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2405 if not existing_ifaces
:
2406 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2407 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2408 "connection-point name at VNFD '{}'".format(
2409 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2410 str(iface
.get("vnfd-id-ref"))[:255]),
2411 httperrors
.Bad_Request
)
2412 interface_uuid
= existing_ifaces
[0]["uuid"]
2413 if existing_ifaces
[0]["iface_type"] == "data" and not db_sce_net
["type"]:
2414 db_sce_net
["type"] = "data"
2415 sce_interface_uuid
= str(uuid4())
2416 uuid_list
.append(sce_net_uuid
)
2417 iface_ip_address
= None
2418 if iface
.get("ip-address"):
2419 iface_ip_address
= str(iface
.get("ip-address"))
2420 db_sce_interface
= {
2421 "uuid": sce_interface_uuid
,
2422 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2423 "sce_net_id": sce_net_uuid
,
2424 "interface_id": interface_uuid
,
2425 "ip_address": iface_ip_address
,
2427 db_sce_interfaces
.append(db_sce_interface
)
2428 if not db_sce_net
["type"]:
2429 db_sce_net
["type"] = "bridge"
2431 # table sce_vnffgs (vnffgd)
2432 for vnffg
in nsd
.get("vnffgd").itervalues():
2433 sce_vnffg_uuid
= str(uuid4())
2434 uuid_list
.append(sce_vnffg_uuid
)
2436 "uuid": sce_vnffg_uuid
,
2437 "name": get_str(vnffg
, "name", 255),
2438 "scenario_id": scenario_uuid
,
2439 "vendor": get_str(vnffg
, "vendor", 255),
2440 "description": get_str(vld
, "description", 255),
2442 db_sce_vnffgs
.append(db_sce_vnffg
)
2446 for rsp
in vnffg
.get("rsp").itervalues():
2447 sce_rsp_uuid
= str(uuid4())
2448 uuid_list
.append(sce_rsp_uuid
)
2450 "uuid": sce_rsp_uuid
,
2451 "name": get_str(rsp
, "name", 255),
2452 "sce_vnffg_id": sce_vnffg_uuid
,
2453 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2455 db_sce_rsps
.append(db_sce_rsp
)
2456 db_sce_rsp_hops
= []
2457 for iface
in rsp
.get("vnfd-connection-point-ref").itervalues():
2458 vnf_index
= str(iface
['member-vnf-index-ref'])
2459 if_order
= int(iface
['order'])
2460 # check correct parameters
2461 if vnf_index
not in vnf_index2vnf_uuid
:
2462 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2463 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2464 "'nsd':'constituent-vnfd'".format(
2465 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2466 httperrors
.Bad_Request
)
2468 ingress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2469 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2471 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2472 'external_name': get_str(iface
, "vnfd-ingress-connection-point-ref",
2474 if not ingress_existing_ifaces
:
2475 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2476 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2477 "connection-point name at VNFD '{}'".format(
2478 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-ingress-connection-point-ref"]),
2479 str(iface
.get("vnfd-id-ref"))[:255]), httperrors
.Bad_Request
)
2481 egress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2482 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2484 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2485 'external_name': get_str(iface
, "vnfd-egress-connection-point-ref",
2487 if not egress_existing_ifaces
:
2488 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2489 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2490 "connection-point name at VNFD '{}'".format(
2491 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-egress-connection-point-ref"]),
2492 str(iface
.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request
)
2494 ingress_interface_uuid
= ingress_existing_ifaces
[0]["uuid"]
2495 egress_interface_uuid
= egress_existing_ifaces
[0]["uuid"]
2496 sce_rsp_hop_uuid
= str(uuid4())
2497 uuid_list
.append(sce_rsp_hop_uuid
)
2499 "uuid": sce_rsp_hop_uuid
,
2500 "if_order": if_order
,
2501 "ingress_interface_id": ingress_interface_uuid
,
2502 "egress_interface_id": egress_interface_uuid
,
2503 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2504 "sce_rsp_id": sce_rsp_uuid
,
2506 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2508 # deal with classifiers
2509 db_sce_classifiers
= []
2510 for classifier
in vnffg
.get("classifier").itervalues():
2511 sce_classifier_uuid
= str(uuid4())
2512 uuid_list
.append(sce_classifier_uuid
)
2515 vnf_index
= str(classifier
['member-vnf-index-ref'])
2516 if vnf_index
not in vnf_index2vnf_uuid
:
2517 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2518 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2519 "'nsd':'constituent-vnfd'".format(
2520 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2521 httperrors
.Bad_Request
)
2522 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2523 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2524 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2525 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2527 if not existing_ifaces
:
2528 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2529 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2530 "connection-point name at VNFD '{}'".format(
2531 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2532 str(iface
.get("vnfd-id-ref"))[:255]),
2533 httperrors
.Bad_Request
)
2534 interface_uuid
= existing_ifaces
[0]["uuid"]
2536 db_sce_classifier
= {
2537 "uuid": sce_classifier_uuid
,
2538 "name": get_str(classifier
, "name", 255),
2539 "sce_vnffg_id": sce_vnffg_uuid
,
2540 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2541 "interface_id": interface_uuid
,
2543 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2544 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2545 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2546 db_sce_classifiers
.append(db_sce_classifier
)
2548 db_sce_classifier_matches
= []
2549 for match
in classifier
.get("match-attributes").itervalues():
2550 sce_classifier_match_uuid
= str(uuid4())
2551 uuid_list
.append(sce_classifier_match_uuid
)
2552 db_sce_classifier_match
= {
2553 "uuid": sce_classifier_match_uuid
,
2554 "ip_proto": get_str(match
, "ip-proto", 2),
2555 "source_ip": get_str(match
, "source-ip-address", 16),
2556 "destination_ip": get_str(match
, "destination-ip-address", 16),
2557 "source_port": get_str(match
, "source-port", 5),
2558 "destination_port": get_str(match
, "destination-port", 5),
2559 "sce_classifier_id": sce_classifier_uuid
,
2561 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2564 # remove unneeded id's in sce_rsps
2565 for rsp
in db_sce_rsps
:
2569 {"scenarios": db_scenarios
},
2570 {"sce_nets": db_sce_nets
},
2571 {"ip_profiles": db_ip_profiles
},
2572 {"sce_vnfs": db_sce_vnfs
},
2573 {"sce_interfaces": db_sce_interfaces
},
2574 {"sce_vnffgs": db_sce_vnffgs
},
2575 {"sce_rsps": db_sce_rsps
},
2576 {"sce_rsp_hops": db_sce_rsp_hops
},
2577 {"sce_classifiers": db_sce_classifiers
},
2578 {"sce_classifier_matches": db_sce_classifier_matches
},
2581 logger
.debug("new_nsd_v3 done: %s",
2582 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2583 mydb
.new_rows(db_tables
, uuid_list
)
2584 return nsd_uuid_list
2585 except NfvoException
:
2587 except Exception as e
:
2588 logger
.error("Exception {}".format(e
))
2589 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2592 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2593 data
["uuid"] = scenario_id
2594 data
["tenant_id"] = tenant_id
2595 c
= mydb
.edit_scenario( data
)
2599 @deprecated("Use create_instance")
2600 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2601 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2602 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2603 vims
= {datacenter_id
: myvim
}
2604 myvim_tenant
= myvim
['tenant_id']
2605 datacenter_name
= myvim
['name']
2609 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2610 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2611 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2612 scenarioDict
['datacenter_id'] = datacenter_id
2613 #print '================scenarioDict======================='
2614 #print json.dumps(scenarioDict, indent=4)
2615 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2617 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2618 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2620 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2621 auxNetDict
['scenario'] = {}
2623 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2624 for sce_net
in scenarioDict
['nets']:
2625 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2627 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2628 myNetName
= myNetName
[0:255] #limit length
2629 myNetType
= sce_net
['type']
2631 myNetDict
["name"] = myNetName
2632 myNetDict
["type"] = myNetType
2633 myNetDict
["tenant_id"] = myvim_tenant
2634 myNetIPProfile
= sce_net
.get('ip_profile', None)
2636 #We should use the dictionary as input parameter for new_network
2638 if not sce_net
["external"]:
2639 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2640 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2641 sce_net
['vim_id'] = network_id
2642 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2643 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2644 sce_net
["created"] = True
2646 if sce_net
['vim_id'] == None:
2647 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2648 _
, message
= rollback(mydb
, vims
, rollbackList
)
2649 logger
.error("nfvo.start_scenario: %s", error_text
)
2650 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2651 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2652 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2654 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2655 #For each vnf net, we create it and we add it to instanceNetlist.
2657 for sce_vnf
in scenarioDict
['vnfs']:
2658 for net
in sce_vnf
['nets']:
2659 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2661 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2662 myNetName
= myNetName
[0:255] #limit length
2663 myNetType
= net
['type']
2665 myNetDict
["name"] = myNetName
2666 myNetDict
["type"] = myNetType
2667 myNetDict
["tenant_id"] = myvim_tenant
2668 myNetIPProfile
= net
.get('ip_profile', None)
2671 #We should use the dictionary as input parameter for new_network
2672 network_id
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2673 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2674 net
['vim_id'] = network_id
2675 if sce_vnf
['uuid'] not in auxNetDict
:
2676 auxNetDict
[sce_vnf
['uuid']] = {}
2677 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2678 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2679 net
["created"] = True
2681 #print "auxNetDict:"
2682 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2684 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2685 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2687 for sce_vnf
in scenarioDict
['vnfs']:
2688 vnf_availability_zones
= []
2689 for vm
in sce_vnf
['vms']:
2690 vm_av
= vm
.get('availability_zone')
2691 if vm_av
and vm_av
not in vnf_availability_zones
:
2692 vnf_availability_zones
.append(vm_av
)
2694 # check if there is enough availability zones available at vim level.
2695 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2696 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2697 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
2699 for vm
in sce_vnf
['vms']:
2702 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2703 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2704 #myVMDict['description'] = vm['description']
2705 myVMDict
['description'] = myVMDict
['name'][0:99]
2707 myVMDict
['start'] = "no"
2708 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2709 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2711 #create image at vim in case it not exist
2712 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2713 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2714 vm
['vim_image_id'] = image_id
2716 #create flavor at vim in case it not exist
2717 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2718 if flavor_dict
['extended']!=None:
2719 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2720 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2721 vm
['vim_flavor_id'] = flavor_id
2724 myVMDict
['imageRef'] = vm
['vim_image_id']
2725 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2726 myVMDict
['networks'] = []
2727 for iface
in vm
['interfaces']:
2729 if iface
['type']=="data":
2730 netDict
['type'] = iface
['model']
2731 elif "model" in iface
and iface
["model"]!=None:
2732 netDict
['model']=iface
['model']
2733 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2734 #discover type of interface looking at flavor
2735 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2736 for flavor_iface
in numa
.get('interfaces',[]):
2737 if flavor_iface
.get('name') == iface
['internal_name']:
2738 if flavor_iface
['dedicated'] == 'yes':
2739 netDict
['type']="PF" #passthrough
2740 elif flavor_iface
['dedicated'] == 'no':
2741 netDict
['type']="VF" #siov
2742 elif flavor_iface
['dedicated'] == 'yes:sriov':
2743 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2744 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2746 netDict
["use"]=iface
['type']
2747 if netDict
["use"]=="data" and not netDict
.get("type"):
2748 #print "netDict", netDict
2749 #print "iface", iface
2750 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'])
2751 if flavor_dict
.get('extended')==None:
2752 raise NfvoException(e_text
+ "After database migration some information is not available. \
2753 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
2755 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
2756 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2757 netDict
["type"]="virtual"
2758 if "vpci" in iface
and iface
["vpci"] is not None:
2759 netDict
['vpci'] = iface
['vpci']
2760 if "mac" in iface
and iface
["mac"] is not None:
2761 netDict
['mac_address'] = iface
['mac']
2762 if "port-security" in iface
and iface
["port-security"] is not None:
2763 netDict
['port_security'] = iface
['port-security']
2764 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2765 netDict
['floating_ip'] = iface
['floating-ip']
2766 netDict
['name'] = iface
['internal_name']
2767 if iface
['net_id'] is None:
2768 for vnf_iface
in sce_vnf
["interfaces"]:
2771 if vnf_iface
['interface_id']==iface
['uuid']:
2772 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2775 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2776 #skip bridge ifaces not connected to any net
2777 #if 'net_id' not in netDict or netDict['net_id']==None:
2779 myVMDict
['networks'].append(netDict
)
2780 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2781 #print myVMDict['name']
2782 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2783 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2784 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2786 if 'availability_zone' in myVMDict
:
2787 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2791 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2792 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2793 availability_zone_index
=av_index
,
2794 availability_zone_list
=vnf_availability_zones
)
2795 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2796 vm
['vim_id'] = vm_id
2797 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2798 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2799 for net
in myVMDict
['networks']:
2801 for iface
in vm
['interfaces']:
2802 if net
["name"]==iface
["internal_name"]:
2803 iface
["vim_id"]=net
["vim_id"]
2806 logger
.debug("start scenario Deployment done")
2807 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2808 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2809 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2810 return mydb
.get_instance_scenario(instance_id
)
2812 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2813 _
, message
= rollback(mydb
, vims
, rollbackList
)
2814 if isinstance(e
, db_base_Exception
):
2815 error_text
= "Exception at database"
2817 error_text
= "Exception at VIM"
2818 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2819 #logger.error("start_scenario %s", error_text)
2820 raise NfvoException(error_text
, e
.http_code
)
2822 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2823 """ join the cloud config information into cloud_config_preserve.
2824 In case of conflict cloud_config_preserve preserves
2827 if not cloud_config_preserve
and not cloud_config
:
2830 new_cloud_config
= {"key-pairs":[], "users":[]}
2832 if cloud_config_preserve
:
2833 for key
in cloud_config_preserve
.get("key-pairs", () ):
2834 if key
not in new_cloud_config
["key-pairs"]:
2835 new_cloud_config
["key-pairs"].append(key
)
2837 for key
in cloud_config
.get("key-pairs", () ):
2838 if key
not in new_cloud_config
["key-pairs"]:
2839 new_cloud_config
["key-pairs"].append(key
)
2840 if not new_cloud_config
["key-pairs"]:
2841 del new_cloud_config
["key-pairs"]
2845 new_cloud_config
["users"] += cloud_config
.get("users", () )
2846 if cloud_config_preserve
:
2847 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2848 index_to_delete
= []
2849 users
= new_cloud_config
.get("users", [])
2850 for index0
in range(0,len(users
)):
2851 if index0
in index_to_delete
:
2853 for index1
in range(index0
+1,len(users
)):
2854 if index1
in index_to_delete
:
2856 if users
[index0
]["name"] == users
[index1
]["name"]:
2857 index_to_delete
.append(index1
)
2858 for key
in users
[index1
].get("key-pairs",()):
2859 if "key-pairs" not in users
[index0
]:
2860 users
[index0
]["key-pairs"] = [key
]
2861 elif key
not in users
[index0
]["key-pairs"]:
2862 users
[index0
]["key-pairs"].append(key
)
2863 index_to_delete
.sort(reverse
=True)
2864 for index
in index_to_delete
:
2866 if not new_cloud_config
["users"]:
2867 del new_cloud_config
["users"]
2870 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2871 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2872 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2873 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2876 new_cloud_config
["user-data"] = []
2877 if cloud_config
and cloud_config
.get("user-data"):
2878 if isinstance(cloud_config
["user-data"], list):
2879 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2881 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2882 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2883 if isinstance(cloud_config_preserve
["user-data"], list):
2884 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2886 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2887 if not new_cloud_config
["user-data"]:
2888 del new_cloud_config
["user-data"]
2891 new_cloud_config
["config-files"] = []
2892 if cloud_config
and cloud_config
.get("config-files") != None:
2893 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2894 if cloud_config_preserve
:
2895 for file in cloud_config_preserve
.get("config-files", ()):
2896 for index
in range(0, len(new_cloud_config
["config-files"])):
2897 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2898 new_cloud_config
["config-files"][index
] = file
2901 new_cloud_config
["config-files"].append(file)
2902 if not new_cloud_config
["config-files"]:
2903 del new_cloud_config
["config-files"]
2904 return new_cloud_config
2907 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2908 datacenter_id
= None
2909 datacenter_name
= None
2912 if datacenter_tenant_id
:
2913 thread_id
= datacenter_tenant_id
2914 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2916 where_
={"td.nfvo_tenant_id": tenant_id
}
2917 if datacenter_id_name
:
2918 if utils
.check_valid_uuid(datacenter_id_name
):
2919 datacenter_id
= datacenter_id_name
2920 where_
["dt.datacenter_id"] = datacenter_id
2922 datacenter_name
= datacenter_id_name
2923 where_
["d.name"] = datacenter_name
2924 if datacenter_tenant_id
:
2925 where_
["dt.uuid"] = datacenter_tenant_id
2926 datacenters
= mydb
.get_rows(
2927 SELECT
=("dt.uuid as datacenter_tenant_id",),
2928 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2929 "join datacenters as d on d.uuid=dt.datacenter_id",
2931 if len(datacenters
) > 1:
2932 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2934 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2935 thread
= vim_threads
["running"].get(thread_id
)
2937 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2938 return thread_id
, thread
2939 except db_base_Exception
as e
:
2940 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2943 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
2945 if utils
.check_valid_uuid(datacenter_id_name
):
2946 WHERE_dict
['d.uuid'] = datacenter_id_name
2948 WHERE_dict
['d.name'] = datacenter_id_name
2951 WHERE_dict
['nfvo_tenant_id'] = tenant_id
2952 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2953 " dt on td.datacenter_tenant_id=dt.uuid"
2955 from_
= 'datacenters as d'
2956 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid, d.name as name",), WHERE
=WHERE_dict
)
2957 if len(vimaccounts
) == 0:
2958 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2959 elif len(vimaccounts
)>1:
2960 #print "nfvo.datacenter_action() error. Several datacenters found"
2961 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2962 return vimaccounts
[0]["uuid"], vimaccounts
[0]["name"]
2965 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2966 datacenter_id
= None
2967 datacenter_name
= None
2968 if datacenter_id_name
:
2969 if utils
.check_valid_uuid(datacenter_id_name
):
2970 datacenter_id
= datacenter_id_name
2972 datacenter_name
= datacenter_id_name
2973 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2975 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2977 #print "nfvo.datacenter_action() error. Several datacenters found"
2978 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2979 return vims
.keys()[0], vims
.values()[0]
2983 """Takes dict d and updates it with the values in dict u.
2984 It merges all depth levels"""
2985 for k
, v
in u
.iteritems():
2986 if isinstance(v
, collections
.Mapping
):
2987 r
= update(d
.get(k
, {}), v
)
2994 def create_instance(mydb
, tenant_id
, instance_dict
):
2995 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2996 # logger.debug("Creating instance...")
2997 scenario
= instance_dict
["scenario"]
2999 # find main datacenter
3001 myvim_threads_id
= {}
3002 datacenter
= instance_dict
.get("datacenter")
3003 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3004 myvims
[default_datacenter_id
] = vim
3005 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
3006 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
3007 # myvim_tenant = myvim['tenant_id']
3010 # print "Checking that the scenario exists and getting the scenario dictionary"
3011 if isinstance(scenario
, str):
3012 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
3013 datacenter_id
=default_datacenter_id
)
3015 scenarioDict
= scenario
3016 scenarioDict
["uuid"] = None
3018 # logger.debug(">>>>>> Dictionaries before merging")
3019 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3020 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
3022 db_instance_vnfs
= []
3023 db_instance_vms
= []
3024 db_instance_interfaces
= []
3025 db_instance_sfis
= []
3026 db_instance_sfs
= []
3027 db_instance_classifications
= []
3028 db_instance_sfps
= []
3033 instance_name
= instance_dict
["name"]
3034 instance_uuid
= str(uuid4())
3035 uuid_list
.append(instance_uuid
)
3036 db_instance_scenario
= {
3037 "uuid": instance_uuid
,
3038 "name": instance_name
,
3039 "tenant_id": tenant_id
,
3040 "scenario_id": scenarioDict
['uuid'],
3041 "datacenter_id": default_datacenter_id
,
3042 # filled bellow 'datacenter_tenant_id'
3043 "description": instance_dict
.get("description"),
3045 if scenarioDict
.get("cloud-config"):
3046 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
3047 default_flow_style
=True, width
=256)
3048 instance_action_id
= get_task_id()
3049 db_instance_action
= {
3050 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3051 "tenant_id": tenant_id
,
3052 "instance_id": instance_uuid
,
3053 "description": "CREATE",
3056 # Auxiliary dictionaries from x to y
3057 sce_net2instance
= {}
3058 net2task_id
= {'scenario': {}}
3059 # Mapping between local networks and WIMs
3062 def ip_profile_IM2RO(ip_profile_im
):
3063 # translate from input format to database format
3065 if 'subnet-address' in ip_profile_im
:
3066 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
3067 if 'ip-version' in ip_profile_im
:
3068 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
3069 if 'gateway-address' in ip_profile_im
:
3070 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
3071 if 'dns-address' in ip_profile_im
:
3072 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
3073 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
3074 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
3075 if 'dhcp' in ip_profile_im
:
3076 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
3077 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
3078 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
3079 return ip_profile_ro
3081 # logger.debug("Creating instance from scenario-dict:\n%s",
3082 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3084 # 0 check correct parameters
3085 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3086 for scenario_net
in scenarioDict
['nets']:
3087 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3090 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3091 httperrors
.Bad_Request
)
3092 if "sites" not in net_instance_desc
:
3093 net_instance_desc
["sites"] = [ {} ]
3094 site_without_datacenter_field
= False
3095 for site
in net_instance_desc
["sites"]:
3096 if site
.get("datacenter"):
3097 site
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3098 if site
["datacenter"] not in myvims
:
3099 # Add this datacenter to myvims
3100 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3102 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3103 site
["datacenter"] = d
# change name to id
3105 if site_without_datacenter_field
:
3106 raise NfvoException("Found more than one entries without datacenter field at "
3107 "instance:networks:{}:sites".format(net_name
), httperrors
.Bad_Request
)
3108 site_without_datacenter_field
= True
3109 site
["datacenter"] = default_datacenter_id
# change name to id
3111 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
3112 for scenario_vnf
in scenarioDict
['vnfs']:
3113 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3116 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), httperrors
.Bad_Request
)
3117 if "datacenter" in vnf_instance_desc
:
3118 # Add this datacenter to myvims
3119 vnf_instance_desc
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3120 if vnf_instance_desc
["datacenter"] not in myvims
:
3121 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3123 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3124 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3126 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).iteritems():
3127 for scenario_net
in scenario_vnf
['nets']:
3128 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3131 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), httperrors
.Bad_Request
)
3132 if net_instance_desc
.get("vim-network-name"):
3133 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3134 if net_instance_desc
.get("vim-network-id"):
3135 scenario_net
["vim-network-id"] = net_instance_desc
["vim-network-id"]
3136 if net_instance_desc
.get("name"):
3137 scenario_net
["name"] = net_instance_desc
["name"]
3138 if 'ip-profile' in net_instance_desc
:
3139 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3140 if 'ip_profile' not in scenario_net
:
3141 scenario_net
['ip_profile'] = ipprofile_db
3143 update(scenario_net
['ip_profile'], ipprofile_db
)
3145 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).iteritems():
3146 for scenario_vm
in scenario_vnf
['vms']:
3147 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3150 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3151 scenario_vm
["instance_parameters"] = vdu_instance_desc
3152 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).iteritems():
3153 for scenario_interface
in scenario_vm
['interfaces']:
3154 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3155 scenario_interface
.update(iface_instance_desc
)
3158 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3160 # 0.1 parse cloud-config parameters
3161 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3163 # 0.2 merge instance information into scenario
3164 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3165 # However, this is not possible yet.
3166 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3167 for scenario_net
in scenarioDict
['nets']:
3168 if net_name
== scenario_net
["name"]:
3169 if 'ip-profile' in net_instance_desc
:
3170 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3171 if 'ip_profile' not in scenario_net
:
3172 scenario_net
['ip_profile'] = ipprofile_db
3174 update(scenario_net
['ip_profile'], ipprofile_db
)
3175 for interface
in net_instance_desc
.get('interfaces', ()):
3176 if 'ip_address' in interface
:
3177 for vnf
in scenarioDict
['vnfs']:
3178 if interface
['vnf'] == vnf
['name']:
3179 for vnf_interface
in vnf
['interfaces']:
3180 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3181 vnf_interface
['ip_address'] = interface
['ip_address']
3183 # logger.debug(">>>>>>>> Merged dictionary")
3184 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3185 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3187 # 1. Creating new nets (sce_nets) in the VIM"
3188 number_mgmt_networks
= 0
3189 db_instance_nets
= []
3190 for sce_net
in scenarioDict
['nets']:
3191 sce_net_uuid
= sce_net
.get('uuid', sce_net
["name"])
3192 # get involved datacenters where this network need to be created
3193 involved_datacenters
= []
3194 for sce_vnf
in scenarioDict
.get("vnfs", ()):
3195 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3196 if vnf_datacenter
in involved_datacenters
:
3198 if sce_vnf
.get("interfaces"):
3199 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3200 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3201 involved_datacenters
.append(vnf_datacenter
)
3203 if not involved_datacenters
:
3204 involved_datacenters
.append(default_datacenter_id
)
3207 # TODO: use this information during network creation
3208 wim_account_id
= None
3209 if len(involved_datacenters
) > 1 and 'uuid' in sce_net
:
3210 # OBS: sce_net without uuid are used internally to VNFs
3211 # and the assumption is that VNFs will not be split among
3212 # different datacenters
3213 wim_account_id
= wim_engine
.find_suitable_wim_account(
3214 involved_datacenters
, tenant_id
)
3215 wim_usage
[sce_net
['uuid']] = wim_account_id
3219 if instance_dict
.get("networks") and instance_dict
["networks"].get(sce_net
["name"]):
3220 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3221 net_name
= descriptor_net
.get("vim-network-name")
3222 # add datacenters from instantiation parameters
3223 if descriptor_net
.get("sites"):
3224 for site
in descriptor_net
["sites"]:
3225 if site
.get("datacenter") and site
["datacenter"] not in involved_datacenters
:
3226 involved_datacenters
.append(site
["datacenter"])
3227 sce_net2instance
[sce_net_uuid
] = {}
3228 net2task_id
['scenario'][sce_net_uuid
] = {}
3230 if sce_net
["external"]:
3231 number_mgmt_networks
+= 1
3233 for datacenter_id
in involved_datacenters
:
3235 netmap_create
= None
3236 if descriptor_net
.get("sites"):
3237 for site
in descriptor_net
["sites"]:
3238 if site
.get("datacenter") == datacenter_id
:
3239 netmap_use
= site
.get("netmap-use")
3240 netmap_create
= site
.get("netmap-create")
3243 vim
= myvims
[datacenter_id
]
3244 myvim_thread_id
= myvim_threads_id
[datacenter_id
]
3246 net_type
= sce_net
['type']
3248 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3251 if sce_net
["external"]:
3252 net_name
= sce_net
["name"]
3254 net_name
= "{}-{}".format(instance_name
, sce_net
["name"])
3255 net_name
= net_name
[:255] # limit length
3257 if netmap_use
or netmap_create
:
3258 create_network
= False
3259 lookfor_network
= False
3261 lookfor_network
= True
3262 if utils
.check_valid_uuid(netmap_use
):
3263 lookfor_filter
["id"] = netmap_use
3265 lookfor_filter
["name"] = netmap_use
3267 create_network
= True
3268 net_vim_name
= net_name
3269 if isinstance(netmap_create
, str):
3270 net_vim_name
= netmap_create
3271 elif sce_net
.get("vim_network_name"):
3272 create_network
= False
3273 lookfor_network
= True
3274 lookfor_filter
["name"] = sce_net
.get("vim_network_name")
3275 elif sce_net
["external"]:
3276 if sce_net
.get('vim_id'):
3277 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3278 create_network
= False
3279 lookfor_network
= True
3280 lookfor_filter
["id"] = sce_net
['vim_id']
3281 elif vim
["config"].get("management_network_id") or vim
["config"].get("management_network_name"):
3282 if number_mgmt_networks
> 1:
3283 raise NfvoException("Found several VLD of type mgmt. "
3284 "You must concrete what vim-network must be use for each one",
3285 httperrors
.Bad_Request
)
3286 create_network
= False
3287 lookfor_network
= True
3288 if vim
["config"].get("management_network_id"):
3289 lookfor_filter
["id"] = vim
["config"]["management_network_id"]
3291 lookfor_filter
["name"] = vim
["config"]["management_network_name"]
3293 # There is not a netmap, look at datacenter for a net with this name and create if not found
3294 create_network
= True
3295 lookfor_network
= True
3296 lookfor_filter
["name"] = sce_net
["name"]
3297 net_vim_name
= sce_net
["name"]
3299 net_vim_name
= net_name
3300 create_network
= True
3301 lookfor_network
= False
3305 task_action
= "CREATE"
3306 task_extra
["params"] = (net_vim_name
, net_type
, sce_net
.get('ip_profile', None))
3308 task_extra
["find"] = (lookfor_filter
,)
3309 elif lookfor_network
:
3310 task_action
= "FIND"
3311 task_extra
["params"] = (lookfor_filter
,)
3313 # fill database content
3314 net_uuid
= str(uuid4())
3315 uuid_list
.append(net_uuid
)
3316 sce_net2instance
[sce_net_uuid
][datacenter_id
] = net_uuid
3320 "vim_name": net_vim_name
,
3321 "instance_scenario_id": instance_uuid
,
3322 "sce_net_id": sce_net
.get("uuid"),
3323 "created": create_network
,
3324 'datacenter_id': datacenter_id
,
3325 'datacenter_tenant_id': myvim_thread_id
,
3326 'status': 'BUILD' # if create_network else "ACTIVE"
3328 db_instance_nets
.append(db_net
)
3330 "instance_action_id": instance_action_id
,
3331 "status": "SCHEDULED",
3332 "task_index": task_index
,
3333 "datacenter_vim_id": myvim_thread_id
,
3334 "action": task_action
,
3335 "item": "instance_nets",
3336 "item_id": net_uuid
,
3337 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3339 net2task_id
['scenario'][sce_net_uuid
][datacenter_id
] = task_index
3341 db_vim_actions
.append(db_vim_action
)
3343 if 'ip_profile' in sce_net
:
3345 'instance_net_id': net_uuid
,
3346 'ip_version': sce_net
['ip_profile']['ip_version'],
3347 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3348 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3349 'dns_address': sce_net
['ip_profile']['dns_address'],
3350 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3351 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3352 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3354 db_ip_profiles
.append(db_ip_profile
)
3358 "default_datacenter_id": default_datacenter_id
,
3359 "myvim_threads_id": myvim_threads_id
,
3360 "instance_uuid": instance_uuid
,
3361 "instance_name": instance_name
,
3362 "instance_action_id": instance_action_id
,
3364 "cloud_config": cloud_config
,
3365 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3366 "instance_parameters": instance_dict
,
3369 "task_index": task_index
,
3370 "uuid_list": uuid_list
,
3371 "db_instance_nets": db_instance_nets
,
3372 "db_vim_actions": db_vim_actions
,
3373 "db_ip_profiles": db_ip_profiles
,
3374 "db_instance_vnfs": db_instance_vnfs
,
3375 "db_instance_vms": db_instance_vms
,
3376 "db_instance_interfaces": db_instance_interfaces
,
3377 "net2task_id": net2task_id
,
3378 "sce_net2instance": sce_net2instance
,
3380 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3381 for sce_vnf
in scenarioDict
.get('vnfs', ()): # sce_vnf_list:
3382 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3383 task_index
= vnf_params_out
["task_index"]
3384 uuid_list
= vnf_params_out
["uuid_list"]
3387 # task_depends_on = []
3388 for vnffg
in scenarioDict
.get('vnffgs', ()):
3389 for rsp
in vnffg
['rsps']:
3391 for cp
in rsp
['connection_points']:
3392 count
= mydb
.get_rows(
3394 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3395 "on interfaces.uuid=h.ingress_interface_id",
3396 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3397 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3398 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3400 for instance_vm
in instance_vms
:
3401 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3403 dependencies
.append(action
['task_index'])
3404 # TODO: throw exception if count != len(instance_vms)
3405 # TODO: and action shouldn't ever be None
3407 for i
in range(count
):
3409 sfi_uuid
= str(uuid4())
3411 "ingress_interface_id": cp
["ingress_interface_id"],
3412 "egress_interface_id": cp
["egress_interface_id"]
3414 uuid_list
.append(sfi_uuid
)
3417 "instance_scenario_id": instance_uuid
,
3418 'sce_rsp_hop_id': cp
['uuid'],
3419 'datacenter_id': datacenter_id
,
3420 'datacenter_tenant_id': myvim_thread_id
,
3421 "vim_sfi_id": None, # vim thread will populate
3423 db_instance_sfis
.append(db_sfi
)
3425 "instance_action_id": instance_action_id
,
3426 "task_index": task_index
,
3427 "datacenter_vim_id": myvim_thread_id
,
3429 "status": "SCHEDULED",
3430 "item": "instance_sfis",
3431 "item_id": sfi_uuid
,
3432 "extra": yaml
.safe_dump({"params": extra_params
, "depends_on": [dependencies
[i
]]},
3433 default_flow_style
=True, width
=256)
3435 sfis_created
.append(task_index
)
3437 db_vim_actions
.append(db_vim_action
)
3439 sf_uuid
= str(uuid4())
3440 uuid_list
.append(sf_uuid
)
3443 "instance_scenario_id": instance_uuid
,
3444 'sce_rsp_hop_id': cp
['uuid'],
3445 'datacenter_id': datacenter_id
,
3446 'datacenter_tenant_id': myvim_thread_id
,
3447 "vim_sf_id": None, # vim thread will populate
3449 db_instance_sfs
.append(db_sf
)
3451 "instance_action_id": instance_action_id
,
3452 "task_index": task_index
,
3453 "datacenter_vim_id": myvim_thread_id
,
3455 "status": "SCHEDULED",
3456 "item": "instance_sfs",
3458 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3459 default_flow_style
=True, width
=256)
3461 sfs_created
.append(task_index
)
3463 db_vim_actions
.append(db_vim_action
)
3464 classifier
= rsp
['classifier']
3466 # TODO the following ~13 lines can be reused for the sfi case
3467 count
= mydb
.get_rows(
3468 SELECT
=('vms.count'),
3469 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3470 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3471 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3472 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3474 for instance_vm
in instance_vms
:
3475 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3477 dependencies
.append(action
['task_index'])
3478 # TODO: throw exception if count != len(instance_vms)
3479 # TODO: and action shouldn't ever be None
3480 classifications_created
= []
3481 for i
in range(count
):
3482 for match
in classifier
['matches']:
3483 # create classifications
3484 classification_uuid
= str(uuid4())
3485 uuid_list
.append(classification_uuid
)
3486 db_classification
= {
3487 "uuid": classification_uuid
,
3488 "instance_scenario_id": instance_uuid
,
3489 'sce_classifier_match_id': match
['uuid'],
3490 'datacenter_id': datacenter_id
,
3491 'datacenter_tenant_id': myvim_thread_id
,
3492 "vim_classification_id": None, # vim thread will populate
3494 db_instance_classifications
.append(db_classification
)
3495 classification_params
= {
3496 "ip_proto": match
["ip_proto"],
3497 "source_ip": match
["source_ip"],
3498 "destination_ip": match
["destination_ip"],
3499 "source_port": match
["source_port"],
3500 "destination_port": match
["destination_port"]
3503 "instance_action_id": instance_action_id
,
3504 "task_index": task_index
,
3505 "datacenter_vim_id": myvim_thread_id
,
3507 "status": "SCHEDULED",
3508 "item": "instance_classifications",
3509 "item_id": classification_uuid
,
3510 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3511 default_flow_style
=True, width
=256)
3513 classifications_created
.append(task_index
)
3515 db_vim_actions
.append(db_vim_action
)
3518 sfp_uuid
= str(uuid4())
3519 uuid_list
.append(sfp_uuid
)
3522 "instance_scenario_id": instance_uuid
,
3523 'sce_rsp_id': rsp
['uuid'],
3524 'datacenter_id': datacenter_id
,
3525 'datacenter_tenant_id': myvim_thread_id
,
3526 "vim_sfp_id": None, # vim thread will populate
3528 db_instance_sfps
.append(db_sfp
)
3530 "instance_action_id": instance_action_id
,
3531 "task_index": task_index
,
3532 "datacenter_vim_id": myvim_thread_id
,
3534 "status": "SCHEDULED",
3535 "item": "instance_sfps",
3536 "item_id": sfp_uuid
,
3537 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3538 default_flow_style
=True, width
=256)
3541 db_vim_actions
.append(db_vim_action
)
3542 db_instance_action
["number_tasks"] = task_index
3545 logger
.debug('wim_usage:\n%s\n\n', pformat(wim_usage
))
3546 wan_links
= wim_engine
.derive_wan_links(wim_usage
, db_instance_nets
, tenant_id
)
3547 wim_actions
= wim_engine
.create_actions(wan_links
)
3548 wim_actions
, db_instance_action
= (
3549 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
3552 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3554 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3555 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3557 {"instance_scenarios": db_instance_scenario
},
3558 {"instance_vnfs": db_instance_vnfs
},
3559 {"instance_nets": db_instance_nets
},
3560 {"ip_profiles": db_ip_profiles
},
3561 {"instance_vms": db_instance_vms
},
3562 {"instance_interfaces": db_instance_interfaces
},
3563 {"instance_actions": db_instance_action
},
3564 {"instance_sfis": db_instance_sfis
},
3565 {"instance_sfs": db_instance_sfs
},
3566 {"instance_classifications": db_instance_classifications
},
3567 {"instance_sfps": db_instance_sfps
},
3568 {"instance_wim_nets": wan_links
},
3569 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
3572 logger
.debug("create_instance done DB tables: %s",
3573 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3574 mydb
.new_rows(db_tables
, uuid_list
)
3575 for myvim_thread_id
in myvim_threads_id
.values():
3576 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3578 wim_engine
.dispatch(wim_actions
)
3580 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3581 returned_instance
["action_id"] = instance_action_id
3582 return returned_instance
3583 except (NfvoException
, vimconn
.vimconnException
, db_base_Exception
) as e
:
3584 message
= rollback(mydb
, myvims
, rollbackList
)
3585 if isinstance(e
, db_base_Exception
):
3586 error_text
= "database Exception"
3587 elif isinstance(e
, vimconn
.vimconnException
):
3588 error_text
= "VIM Exception"
3590 error_text
= "Exception"
3591 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3592 # logger.error("create_instance: %s", error_text)
3594 raise NfvoException(error_text
, e
.http_code
)
3597 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3598 default_datacenter_id
= params
["default_datacenter_id"]
3599 myvim_threads_id
= params
["myvim_threads_id"]
3600 instance_uuid
= params
["instance_uuid"]
3601 instance_name
= params
["instance_name"]
3602 instance_action_id
= params
["instance_action_id"]
3603 myvims
= params
["myvims"]
3604 cloud_config
= params
["cloud_config"]
3605 RO_pub_key
= params
["RO_pub_key"]
3607 task_index
= params_out
["task_index"]
3608 uuid_list
= params_out
["uuid_list"]
3609 db_instance_nets
= params_out
["db_instance_nets"]
3610 db_vim_actions
= params_out
["db_vim_actions"]
3611 db_ip_profiles
= params_out
["db_ip_profiles"]
3612 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3613 db_instance_vms
= params_out
["db_instance_vms"]
3614 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3615 net2task_id
= params_out
["net2task_id"]
3616 sce_net2instance
= params_out
["sce_net2instance"]
3618 vnf_net2instance
= {}
3620 # 2. Creating new nets (vnf internal nets) in the VIM"
3621 # For each vnf net, we create it and we add it to instanceNetlist.
3622 if sce_vnf
.get("datacenter"):
3623 datacenter_id
= sce_vnf
["datacenter"]
3624 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3626 datacenter_id
= default_datacenter_id
3627 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3628 for net
in sce_vnf
['nets']:
3630 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3631 # net_name = descriptor_net.get("name")
3634 net_name
= "{}-{}".format(instance_name
, net
["name"])
3635 net_name
= net_name
[:255] # limit length
3636 net_type
= net
['type']
3638 if sce_vnf
['uuid'] not in vnf_net2instance
:
3639 vnf_net2instance
[sce_vnf
['uuid']] = {}
3640 if sce_vnf
['uuid'] not in net2task_id
:
3641 net2task_id
[sce_vnf
['uuid']] = {}
3642 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3644 # fill database content
3645 net_uuid
= str(uuid4())
3646 uuid_list
.append(net_uuid
)
3647 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3651 "vim_name": net_name
,
3652 "instance_scenario_id": instance_uuid
,
3653 "net_id": net
["uuid"],
3655 'datacenter_id': datacenter_id
,
3656 'datacenter_tenant_id': myvim_thread_id
,
3658 db_instance_nets
.append(db_net
)
3661 if net
.get("vim-network-name"):
3662 lookfor_filter
["name"] = net
["vim-network-name"]
3663 if net
.get("vim-network-id"):
3664 lookfor_filter
["id"] = net
["vim-network-id"]
3666 task_action
= "FIND"
3667 task_extra
= {"params": (lookfor_filter
,)}
3669 task_action
= "CREATE"
3670 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3673 "instance_action_id": instance_action_id
,
3674 "task_index": task_index
,
3675 "datacenter_vim_id": myvim_thread_id
,
3676 "status": "SCHEDULED",
3677 "action": task_action
,
3678 "item": "instance_nets",
3679 "item_id": net_uuid
,
3680 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3683 db_vim_actions
.append(db_vim_action
)
3685 if 'ip_profile' in net
:
3687 'instance_net_id': net_uuid
,
3688 'ip_version': net
['ip_profile']['ip_version'],
3689 'subnet_address': net
['ip_profile']['subnet_address'],
3690 'gateway_address': net
['ip_profile']['gateway_address'],
3691 'dns_address': net
['ip_profile']['dns_address'],
3692 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3693 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3694 'dhcp_count': net
['ip_profile']['dhcp_count'],
3696 db_ip_profiles
.append(db_ip_profile
)
3698 # print "vnf_net2instance:"
3699 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3701 # 3. Creating new vm instances in the VIM
3702 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3704 if sce_vnf
.get('mgmt_access'):
3705 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3706 vnf_availability_zones
= []
3707 for vm
in sce_vnf
.get('vms'):
3708 vm_av
= vm
.get('availability_zone')
3709 if vm_av
and vm_av
not in vnf_availability_zones
:
3710 vnf_availability_zones
.append(vm_av
)
3712 # check if there is enough availability zones available at vim level.
3713 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3714 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3715 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
3717 if sce_vnf
.get("datacenter"):
3718 vim
= myvims
[sce_vnf
["datacenter"]]
3719 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3720 datacenter_id
= sce_vnf
["datacenter"]
3722 vim
= myvims
[default_datacenter_id
]
3723 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3724 datacenter_id
= default_datacenter_id
3725 sce_vnf
["datacenter_id"] = datacenter_id
3728 vnf_uuid
= str(uuid4())
3729 uuid_list
.append(vnf_uuid
)
3732 'instance_scenario_id': instance_uuid
,
3733 'vnf_id': sce_vnf
['vnf_id'],
3734 'sce_vnf_id': sce_vnf
['uuid'],
3735 'datacenter_id': datacenter_id
,
3736 'datacenter_tenant_id': myvim_thread_id
,
3738 db_instance_vnfs
.append(db_instance_vnf
)
3740 for vm
in sce_vnf
['vms']:
3742 if vm
.get("pdu_type"):
3746 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3747 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3748 myVMDict
['description'] = myVMDict
['name'][0:99]
3750 # myVMDict['start'] = "no"
3751 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3752 myVMDict
['name'] = vm
["instance_parameters"].get("name")
3753 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
3754 # create image at vim in case it not exist
3755 image_uuid
= vm
['image_id']
3756 if vm
.get("image_list"):
3757 for alternative_image
in vm
["image_list"]:
3758 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
3759 image_uuid
= alternative_image
['image_id']
3761 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
3762 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
3763 vm
['vim_image_id'] = image_id
3765 # create flavor at vim in case it not exist
3766 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
3767 if flavor_dict
['extended'] != None:
3768 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'])
3769 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
3771 # Obtain information for additional disks
3772 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
3773 WHERE
={'vim_id': flavor_id
})
3774 if not extended_flavor_dict
:
3775 raise NfvoException("flavor '{}' not found".format(flavor_id
), httperrors
.Not_Found
)
3777 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3778 myVMDict
['disks'] = None
3779 extended_info
= extended_flavor_dict
[0]['extended']
3780 if extended_info
!= None:
3781 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
3782 if 'disks' in extended_flavor_dict_yaml
:
3783 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
3784 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
3785 for disk
in myVMDict
['disks']:
3786 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
3787 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
3789 vm
['vim_flavor_id'] = flavor_id
3790 myVMDict
['imageRef'] = vm
['vim_image_id']
3791 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
3792 myVMDict
['availability_zone'] = vm
.get('availability_zone')
3793 myVMDict
['networks'] = []
3794 task_depends_on
= []
3795 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3796 is_management_vm
= False
3798 for iface
in vm
['interfaces']:
3800 if iface
['type'] == "data":
3801 netDict
['type'] = iface
['model']
3802 elif "model" in iface
and iface
["model"] != None:
3803 netDict
['model'] = iface
['model']
3804 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3805 # is obtained from iterface table model
3806 # discover type of interface looking at flavor
3807 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
3808 for flavor_iface
in numa
.get('interfaces', []):
3809 if flavor_iface
.get('name') == iface
['internal_name']:
3810 if flavor_iface
['dedicated'] == 'yes':
3811 netDict
['type'] = "PF" # passthrough
3812 elif flavor_iface
['dedicated'] == 'no':
3813 netDict
['type'] = "VF" # siov
3814 elif flavor_iface
['dedicated'] == 'yes:sriov':
3815 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
3816 netDict
["mac_address"] = flavor_iface
.get("mac_address")
3818 netDict
["use"] = iface
['type']
3819 if netDict
["use"] == "data" and not netDict
.get("type"):
3820 # print "netDict", netDict
3821 # print "iface", iface
3822 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3823 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
3824 if flavor_dict
.get('extended') == None:
3825 raise NfvoException(e_text
+ "After database migration some information is not available. \
3826 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
3828 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
3829 if netDict
["use"] == "mgmt":
3830 is_management_vm
= True
3831 netDict
["type"] = "virtual"
3832 if netDict
["use"] == "bridge":
3833 netDict
["type"] = "virtual"
3834 if iface
.get("vpci"):
3835 netDict
['vpci'] = iface
['vpci']
3836 if iface
.get("mac"):
3837 netDict
['mac_address'] = iface
['mac']
3838 if iface
.get("mac_address"):
3839 netDict
['mac_address'] = iface
['mac_address']
3840 if iface
.get("ip_address"):
3841 netDict
['ip_address'] = iface
['ip_address']
3842 if iface
.get("port-security") is not None:
3843 netDict
['port_security'] = iface
['port-security']
3844 if iface
.get("floating-ip") is not None:
3845 netDict
['floating_ip'] = iface
['floating-ip']
3846 netDict
['name'] = iface
['internal_name']
3847 if iface
['net_id'] is None:
3848 for vnf_iface
in sce_vnf
["interfaces"]:
3851 if vnf_iface
['interface_id'] == iface
['uuid']:
3852 netDict
['net_id'] = "TASK-{}".format(
3853 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3854 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
3855 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3858 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3859 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
3860 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3861 # skip bridge ifaces not connected to any net
3862 if 'net_id' not in netDict
or netDict
['net_id'] == None:
3864 myVMDict
['networks'].append(netDict
)
3867 # 'instance_vm_id': instance_vm_uuid,
3868 "instance_net_id": instance_net_id
,
3869 'interface_id': iface
['uuid'],
3870 # 'vim_interface_id': ,
3871 'type': 'external' if iface
['external_name'] is not None else 'internal',
3872 'ip_address': iface
.get('ip_address'),
3873 'mac_address': iface
.get('mac'),
3874 'floating_ip': int(iface
.get('floating-ip', False)),
3875 'port_security': int(iface
.get('port-security', True))
3877 db_vm_ifaces
.append(db_vm_iface
)
3878 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3879 # print myVMDict['name']
3880 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3881 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3882 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3884 # We add the RO key to cloud_config if vnf will need ssh access
3885 cloud_config_vm
= cloud_config
3886 if is_management_vm
and params
["instance_parameters"].get("mgmt_keys"):
3887 cloud_config_vm
= unify_cloud_config({"key-pairs": params
["instance_parameters"]["mgmt_keys"]},
3890 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("mgmt_keys"):
3891 cloud_config_vm
= unify_cloud_config({"key-pairs": vm
["instance_parameters"]["mgmt_keys"]},
3893 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3894 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3895 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
3896 if vm
.get("boot_data"):
3897 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
3899 if myVMDict
.get('availability_zone'):
3900 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3903 for vm_index
in range(0, vm
.get('count', 1)):
3904 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
3905 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
3906 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
3907 myVMDict
['disks'], av_index
, vnf_availability_zones
)
3908 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3909 for net
in myVMDict
['networks']:
3911 for iface
in vm
['interfaces']:
3912 if net
["name"] == iface
["internal_name"]:
3913 iface
["vim_id"] = net
["vim_id"]
3915 vm_uuid
= str(uuid4())
3916 uuid_list
.append(vm_uuid
)
3919 'instance_vnf_id': vnf_uuid
,
3920 # TODO delete "vim_vm_id": vm_id,
3921 "vm_id": vm
["uuid"],
3922 "vim_name": vm_name
,
3925 db_instance_vms
.append(db_vm
)
3928 for db_vm_iface
in db_vm_ifaces
:
3929 iface_uuid
= str(uuid4())
3930 uuid_list
.append(iface_uuid
)
3931 db_vm_iface_instance
= {
3933 "instance_vm_id": vm_uuid
3935 db_vm_iface_instance
.update(db_vm_iface
)
3936 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3937 ip
= db_vm_iface_instance
.get("ip_address")
3942 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
3943 db_vm_iface_instance
["ip_address"] = ip
3945 db_vm_iface_instance
["ip_address"] = None
3946 db_instance_interfaces
.append(db_vm_iface_instance
)
3947 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
3951 "instance_action_id": instance_action_id
,
3952 "task_index": task_index
,
3953 "datacenter_vim_id": myvim_thread_id
,
3955 "status": "SCHEDULED",
3956 "item": "instance_vms",
3958 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
3959 default_flow_style
=True, width
=256)
3962 db_vim_actions
.append(db_vim_action
)
3963 params_out
["task_index"] = task_index
3964 params_out
["uuid_list"] = uuid_list
3967 def delete_instance(mydb
, tenant_id
, instance_id
):
3968 # print "Checking that the instance_id exists and getting the instance dictionary"
3969 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3970 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3971 tenant_id
= instanceDict
["tenant_id"]
3974 # We need to retrieve the WIM Actions now, before the instance_scenario is
3975 # deleted. The reason for that is that: ON CASCADE rules will delete the
3976 # instance_wim_nets record in the database
3977 wim_actions
= wim_engine
.delete_actions(instance_scenario_id
=instance_id
)
3980 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3981 # 1. Delete from Database
3982 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3984 # 2. delete from VIM
3988 vimthread_affected
= {}
3989 net2vm_dependencies
= {}
3992 instance_action_id
= get_task_id()
3994 db_instance_action
= {
3995 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3996 "tenant_id": tenant_id
,
3997 "instance_id": instance_id
,
3998 "description": "DELETE",
3999 # "number_tasks": 0 # filled bellow
4002 # 2.1 deleting VNFFGs
4003 for sfp
in instanceDict
.get('sfps', ()):
4004 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
4005 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4006 if datacenter_key
not in myvims
:
4008 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4009 except NfvoException
as e
:
4010 logger
.error(str(e
))
4012 myvim_threads
[datacenter_key
] = myvim_thread
4013 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
4014 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
4016 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
4017 myvims
[datacenter_key
] = None
4019 myvims
[datacenter_key
] = vims
.values()[0]
4020 myvim
= myvims
[datacenter_key
]
4021 myvim_thread
= myvim_threads
[datacenter_key
]
4024 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
4026 extra
= {"params": (sfp
['vim_sfp_id'])}
4028 "instance_action_id": instance_action_id
,
4029 "task_index": task_index
,
4030 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
4032 "status": "SCHEDULED",
4033 "item": "instance_sfps",
4034 "item_id": sfp
["uuid"],
4035 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4038 db_vim_actions
.append(db_vim_action
)
4040 for classification
in instanceDict
['classifications']:
4041 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4042 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4043 if datacenter_key
not in myvims
:
4045 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4046 except NfvoException
as e
:
4047 logger
.error(str(e
))
4049 myvim_threads
[datacenter_key
] = myvim_thread
4050 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4051 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4053 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"],
4054 classification
["datacenter_tenant_id"]))
4055 myvims
[datacenter_key
] = None
4057 myvims
[datacenter_key
] = vims
.values()[0]
4058 myvim
= myvims
[datacenter_key
]
4059 myvim_thread
= myvim_threads
[datacenter_key
]
4062 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'],
4063 classification
["datacenter_id"])
4065 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4066 extra
= {"params": (classification
['vim_classification_id']), "depends_on": depends_on
}
4068 "instance_action_id": instance_action_id
,
4069 "task_index": task_index
,
4070 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4072 "status": "SCHEDULED",
4073 "item": "instance_classifications",
4074 "item_id": classification
["uuid"],
4075 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4078 db_vim_actions
.append(db_vim_action
)
4080 for sf
in instanceDict
.get('sfs', ()):
4081 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
4082 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4083 if datacenter_key
not in myvims
:
4085 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4086 except NfvoException
as e
:
4087 logger
.error(str(e
))
4089 myvim_threads
[datacenter_key
] = myvim_thread
4090 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
4091 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
4093 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
4094 myvims
[datacenter_key
] = None
4096 myvims
[datacenter_key
] = vims
.values()[0]
4097 myvim
= myvims
[datacenter_key
]
4098 myvim_thread
= myvim_threads
[datacenter_key
]
4101 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
4103 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4104 extra
= {"params": (sf
['vim_sf_id']), "depends_on": depends_on
}
4106 "instance_action_id": instance_action_id
,
4107 "task_index": task_index
,
4108 "datacenter_vim_id": sf
["datacenter_tenant_id"],
4110 "status": "SCHEDULED",
4111 "item": "instance_sfs",
4112 "item_id": sf
["uuid"],
4113 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4116 db_vim_actions
.append(db_vim_action
)
4118 for sfi
in instanceDict
.get('sfis', ()):
4119 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4120 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4121 if datacenter_key
not in myvims
:
4123 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4124 except NfvoException
as e
:
4125 logger
.error(str(e
))
4127 myvim_threads
[datacenter_key
] = myvim_thread
4128 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4129 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4131 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4132 myvims
[datacenter_key
] = None
4134 myvims
[datacenter_key
] = vims
.values()[0]
4135 myvim
= myvims
[datacenter_key
]
4136 myvim_thread
= myvim_threads
[datacenter_key
]
4139 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4141 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfs"]
4142 extra
= {"params": (sfi
['vim_sfi_id']), "depends_on": depends_on
}
4144 "instance_action_id": instance_action_id
,
4145 "task_index": task_index
,
4146 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4148 "status": "SCHEDULED",
4149 "item": "instance_sfis",
4150 "item_id": sfi
["uuid"],
4151 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4154 db_vim_actions
.append(db_vim_action
)
4158 for sce_vnf
in instanceDict
.get('vnfs', ()):
4159 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4160 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
4161 if datacenter_key
not in myvims
:
4163 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4164 except NfvoException
as e
:
4165 logger
.error(str(e
))
4167 myvim_threads
[datacenter_key
] = myvim_thread
4168 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
4169 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
4171 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
4172 sce_vnf
["datacenter_tenant_id"]))
4173 myvims
[datacenter_key
] = None
4175 myvims
[datacenter_key
] = vims
.values()[0]
4176 myvim
= myvims
[datacenter_key
]
4177 myvim_thread
= myvim_threads
[datacenter_key
]
4179 for vm
in sce_vnf
['vms']:
4181 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
4183 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4185 "instance_action_id": instance_action_id
,
4186 "task_index": task_index
,
4187 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
4189 "status": "SCHEDULED",
4190 "item": "instance_vms",
4191 "item_id": vm
["uuid"],
4192 "extra": yaml
.safe_dump({"params": vm
["interfaces"], "depends_on": sfi_dependencies
},
4193 default_flow_style
=True, width
=256)
4195 db_vim_actions
.append(db_vim_action
)
4196 for interface
in vm
["interfaces"]:
4197 if not interface
.get("instance_net_id"):
4199 if interface
["instance_net_id"] not in net2vm_dependencies
:
4200 net2vm_dependencies
[interface
["instance_net_id"]] = []
4201 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
4206 for net
in instanceDict
['nets']:
4207 vimthread_affected
[net
["datacenter_tenant_id"]] = None
4208 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
4209 if datacenter_key
not in myvims
:
4211 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, net
["datacenter_id"], net
["datacenter_tenant_id"])
4212 except NfvoException
as e
:
4213 logger
.error(str(e
))
4215 myvim_threads
[datacenter_key
] = myvim_thread
4216 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
4217 datacenter_tenant_id
=net
["datacenter_tenant_id"])
4219 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
4220 myvims
[datacenter_key
] = None
4222 myvims
[datacenter_key
] = vims
.values()[0]
4223 myvim
= myvims
[datacenter_key
]
4224 myvim_thread
= myvim_threads
[datacenter_key
]
4227 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
4229 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
4230 if net2vm_dependencies
.get(net
["uuid"]):
4231 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
4232 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4233 if len(sfi_dependencies
) > 0:
4234 if "depends_on" in extra
:
4235 extra
["depends_on"] += sfi_dependencies
4237 extra
["depends_on"] = sfi_dependencies
4239 "instance_action_id": instance_action_id
,
4240 "task_index": task_index
,
4241 "datacenter_vim_id": net
["datacenter_tenant_id"],
4243 "status": "SCHEDULED",
4244 "item": "instance_nets",
4245 "item_id": net
["uuid"],
4246 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4249 db_vim_actions
.append(db_vim_action
)
4251 db_instance_action
["number_tasks"] = task_index
4254 wim_actions
, db_instance_action
= (
4255 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
4259 {"instance_actions": db_instance_action
},
4260 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
4263 logger
.debug("delete_instance done DB tables: %s",
4264 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4265 mydb
.new_rows(db_tables
, ())
4266 for myvim_thread_id
in vimthread_affected
.keys():
4267 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4269 wim_engine
.dispatch(wim_actions
)
4271 if len(error_msg
) > 0:
4272 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4273 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4275 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4277 def get_instance_id(mydb
, tenant_id
, instance_id
):
4279 #check valid tenant_id
4280 check_tenant(mydb
, tenant_id
)
4283 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4284 for net
in instance_dict
["nets"]:
4285 if net
.get("sdn_net_id"):
4286 net_sdn
= ovim
.show_network(net
["sdn_net_id"])
4288 "admin_state_up": net_sdn
.get("admin_state_up"),
4289 "flows": net_sdn
.get("flows"),
4290 "last_error": net_sdn
.get("last_error"),
4291 "ports": net_sdn
.get("ports"),
4292 "type": net_sdn
.get("type"),
4293 "status": net_sdn
.get("status"),
4294 "vlan": net_sdn
.get("vlan"),
4296 return instance_dict
4298 @deprecated("Instance is automatically refreshed by vim_threads")
4299 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4300 '''Refreshes a scenario instance. It modifies instanceDict'''
4302 - 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
4305 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4306 # #print "nfvo.refresh_instance begins"
4307 # #print json.dumps(instanceDict, indent=4)
4309 # #print "Getting the VIM URL and the VIM tenant_id"
4312 # # 1. Getting VIM vm and net list
4313 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4316 # for sce_vnf in instanceDict['vnfs']:
4317 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4318 # if datacenter_key not in vm_list:
4319 # vm_list[datacenter_key] = []
4320 # if datacenter_key not in myvims:
4321 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4322 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4323 # if len(vims) == 0:
4324 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4325 # myvims[datacenter_key] = None
4327 # myvims[datacenter_key] = vims.values()[0]
4328 # for vm in sce_vnf['vms']:
4329 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4330 # vms_notupdated.append(vm["uuid"])
4332 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4333 # nets_notupdated=[]
4335 # for net in instanceDict['nets']:
4336 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4337 # if datacenter_key not in net_list:
4338 # net_list[datacenter_key] = []
4339 # if datacenter_key not in myvims:
4340 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4341 # datacenter_tenant_id=net["datacenter_tenant_id"])
4342 # if len(vims) == 0:
4343 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4344 # myvims[datacenter_key] = None
4346 # myvims[datacenter_key] = vims.values()[0]
4348 # net_list[datacenter_key].append(net['vim_net_id'])
4349 # nets_notupdated.append(net["uuid"])
4351 # # 1. Getting the status of all VMs
4353 # for datacenter_key in myvims:
4354 # if not vm_list.get(datacenter_key):
4358 # if not myvims[datacenter_key]:
4359 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4362 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4364 # except vimconn.vimconnException as e:
4365 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4366 # failed_message = str(e)
4368 # for vm in vm_list[datacenter_key]:
4369 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4371 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4372 # for sce_vnf in instanceDict['vnfs']:
4373 # for vm in sce_vnf['vms']:
4374 # vm_id = vm['vim_vm_id']
4375 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4376 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4377 # has_mgmt_iface = False
4378 # for iface in vm["interfaces"]:
4379 # if iface["type"]=="mgmt":
4380 # has_mgmt_iface = True
4381 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4382 # vm_dict[vm_id]['status'] = "ACTIVE"
4383 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4384 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4385 # 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'):
4386 # vm['status'] = vm_dict[vm_id]['status']
4387 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4388 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4389 # # 2.1. Update in openmano DB the VMs whose status changed
4391 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4392 # vms_notupdated.remove(vm["uuid"])
4394 # vms_updated.append(vm["uuid"])
4395 # except db_base_Exception as e:
4396 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4397 # # 2.2. Update in openmano DB the interface VMs
4398 # for interface in interfaces:
4399 # #translate from vim_net_id to instance_net_id
4400 # network_id_list=[]
4401 # for net in instanceDict['nets']:
4402 # if net["vim_net_id"] == interface["vim_net_id"]:
4403 # network_id_list.append(net["uuid"])
4404 # if not network_id_list:
4406 # del interface["vim_net_id"]
4408 # for network_id in network_id_list:
4409 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4410 # except db_base_Exception as e:
4411 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4413 # # 3. Getting the status of all nets
4415 # for datacenter_key in myvims:
4416 # if not net_list.get(datacenter_key):
4419 # failed_message = ""
4420 # if not myvims[datacenter_key]:
4421 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4424 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4426 # except vimconn.vimconnException as e:
4427 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4428 # failed_message = str(e)
4430 # for net in net_list[datacenter_key]:
4431 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4433 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4434 # # TODO: update nets inside a vnf
4435 # for net in instanceDict['nets']:
4436 # net_id = net['vim_net_id']
4437 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4438 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4439 # 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'):
4440 # net['status'] = net_dict[net_id]['status']
4441 # net['error_msg'] = net_dict[net_id].get('error_msg')
4442 # net['vim_info'] = net_dict[net_id].get('vim_info')
4443 # # 5.1. Update in openmano DB the nets whose status changed
4445 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4446 # nets_notupdated.remove(net["uuid"])
4448 # nets_updated.append(net["uuid"])
4449 # except db_base_Exception as e:
4450 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4452 # # Returns appropriate output
4453 # #print "nfvo.refresh_instance finishes"
4454 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4455 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4456 instance_id
= instanceDict
['uuid']
4457 # if len(vms_notupdated)+len(nets_notupdated)>0:
4458 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4459 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4461 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4463 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4464 #print "Checking that the instance_id exists and getting the instance dictionary"
4465 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4466 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4468 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4469 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4471 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), httperrors
.Not_Found
)
4472 myvim
= vims
.values()[0]
4477 myvim_threads_id
= {}
4478 if action_dict
.get("vdu-scaling"):
4479 db_instance_vms
= []
4481 db_instance_interfaces
= []
4482 instance_action_id
= get_task_id()
4483 db_instance_action
= {
4484 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4485 "tenant_id": nfvo_tenant
,
4486 "instance_id": instance_id
,
4487 "description": "SCALE",
4489 vm_result
["instance_action_id"] = instance_action_id
4490 vm_result
["created"] = []
4491 vm_result
["deleted"] = []
4493 for vdu
in action_dict
["vdu-scaling"]:
4494 vdu_id
= vdu
.get("vdu-id")
4495 osm_vdu_id
= vdu
.get("osm_vdu_id")
4496 member_vnf_index
= vdu
.get("member-vnf-index")
4497 vdu_count
= vdu
.get("count", 1)
4499 target_vms
= mydb
.get_rows(
4500 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4501 WHERE
={"vms.uuid": vdu_id
},
4502 ORDER_BY
="vms.created_at"
4505 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), httperrors
.Not_Found
)
4507 if not osm_vdu_id
and not member_vnf_index
:
4508 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4509 target_vms
= mydb
.get_rows(
4510 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4511 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4512 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4513 " join vms on ivms.vm_id=vms.uuid",
4514 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
,
4515 "ivnfs.instance_scenario_id": instance_id
},
4516 ORDER_BY
="ivms.created_at"
4519 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), httperrors
.Not_Found
)
4520 vdu_id
= target_vms
[-1]["uuid"]
4521 target_vm
= target_vms
[-1]
4522 datacenter
= target_vm
["datacenter_id"]
4523 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4525 if vdu
["type"] == "delete":
4526 for index
in range(0, vdu_count
):
4527 target_vm
= target_vms
[-1-index
]
4528 vdu_id
= target_vm
["uuid"]
4530 vm_interfaces
= None
4531 for sce_vnf
in instanceDict
['vnfs']:
4532 for vm
in sce_vnf
['vms']:
4533 if vm
["uuid"] == vdu_id
:
4534 vm_interfaces
= vm
["interfaces"]
4538 "instance_action_id": instance_action_id
,
4539 "task_index": task_index
,
4540 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4542 "status": "SCHEDULED",
4543 "item": "instance_vms",
4545 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4546 default_flow_style
=True, width
=256)
4549 db_vim_actions
.append(db_vim_action
)
4550 vm_result
["deleted"].append(vdu_id
)
4551 # delete from database
4552 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4554 else: # vdu["type"] == "create":
4556 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4558 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
=where
)
4559 if not vim_action_to_clone
:
4560 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), httperrors
.Internal_Server_Error
)
4561 vim_action_to_clone
= vim_action_to_clone
[0]
4562 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4564 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4565 # TODO do the same for flavor and image when available
4566 task_depends_on
= []
4567 task_params
= extra
["params"]
4568 task_params_networks
= deepcopy(task_params
[5])
4569 for iface
in task_params
[5]:
4570 if iface
["net_id"].startswith("TASK-"):
4571 if "." not in iface
["net_id"]:
4572 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4573 iface
["net_id"][5:]))
4574 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4575 iface
["net_id"][5:])
4577 task_depends_on
.append(iface
["net_id"][5:])
4578 if "mac_address" in iface
:
4579 del iface
["mac_address"]
4581 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4582 for index
in range(0, vdu_count
):
4583 vm_uuid
= str(uuid4())
4584 vm_name
= target_vm
.get('vim_name')
4586 suffix
= vm_name
.rfind("-")
4587 vm_name
= vm_name
[:suffix
+1] + str(index
+ 1 + int(vm_name
[suffix
+1:]))
4592 'instance_vnf_id': target_vm
['instance_vnf_id'],
4593 'vm_id': target_vm
['vm_id'],
4596 db_instance_vms
.append(db_instance_vm
)
4598 for vm_iface
in vm_ifaces_to_clone
:
4599 iface_uuid
= str(uuid4())
4600 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4603 'instance_vm_id': vm_uuid
,
4604 "instance_net_id": vm_iface
["instance_net_id"],
4605 'interface_id': vm_iface
['interface_id'],
4606 'type': vm_iface
['type'],
4607 'floating_ip': vm_iface
['floating_ip'],
4608 'port_security': vm_iface
['port_security']
4610 db_instance_interfaces
.append(db_vm_iface
)
4611 task_params_copy
= deepcopy(task_params
)
4612 for iface
in task_params_copy
[5]:
4613 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4614 # increment ip_address
4615 if "ip_address" in iface
:
4616 ip
= iface
.get("ip_address")
4621 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4622 iface
["ip_address"] = ip
4624 iface
["ip_address"] = None
4626 task_params_copy
[0] = vm_name
4628 "instance_action_id": instance_action_id
,
4629 "task_index": task_index
,
4630 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4632 "status": "SCHEDULED",
4633 "item": "instance_vms",
4637 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4640 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4643 db_vim_actions
.append(db_vim_action
)
4644 vm_result
["created"].append(vm_uuid
)
4646 db_instance_action
["number_tasks"] = task_index
4648 {"instance_vms": db_instance_vms
},
4649 {"instance_interfaces": db_instance_interfaces
},
4650 {"instance_actions": db_instance_action
},
4652 # {"instance_sfis": db_instance_sfis},
4653 # {"instance_sfs": db_instance_sfs},
4654 # {"instance_classifications": db_instance_classifications},
4655 # {"instance_sfps": db_instance_sfps},
4656 {"vim_wim_actions": db_vim_actions
}
4658 logger
.debug("create_vdu done DB tables: %s",
4659 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4660 mydb
.new_rows(db_tables
, [])
4661 for myvim_thread
in myvim_threads_id
.values():
4662 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4666 input_vnfs
= action_dict
.pop("vnfs", [])
4667 input_vms
= action_dict
.pop("vms", [])
4668 action_over_all
= True if not input_vnfs
and not input_vms
else False
4669 for sce_vnf
in instanceDict
['vnfs']:
4670 for vm
in sce_vnf
['vms']:
4671 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4672 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4673 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
4676 if "add_public_key" in action_dict
:
4678 if sce_vnf
.get('mgmt_access'):
4679 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'])
4680 ssh_access
= mgmt_access
['config-access']['ssh-access']
4681 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4683 if ssh_access
['required'] and ssh_access
['default-user']:
4684 if 'ip_address' in vm
:
4685 mgmt_ip
= vm
['ip_address'].split(';')
4686 password
= mgmt_access
['config-access'].get('password')
4687 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4688 myvim
.inject_user_key(mgmt_ip
[0], ssh_access
['default-user'],
4689 action_dict
['add_public_key'],
4690 password
=password
, ro_key
=priv_RO_key
)
4692 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4693 httperrors
.Internal_Server_Error
)
4695 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4696 httperrors
.Internal_Server_Error
)
4698 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4699 httperrors
.Internal_Server_Error
)
4701 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
4702 if "console" in action_dict
:
4703 if not global_config
["http_console_proxy"]:
4704 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4705 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4706 protocol
=data
["protocol"],
4707 ip
= data
["server"],
4708 port
= data
["port"],
4709 suffix
= data
["suffix"]),
4713 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
4714 vm_result
[ vm
['uuid'] ] = {"vim_result": -httperrors
.Unauthorized
,
4715 "description": "this console is only reachable by local interface",
4720 #print "console data", data
4722 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
4723 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4724 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4725 protocol
=data
["protocol"],
4726 ip
= global_config
["http_console_host"],
4727 port
= console_thread
.port
,
4728 suffix
= data
["suffix"]),
4732 except NfvoException
as e
:
4733 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4737 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
4739 except vimconn
.vimconnException
as e
:
4740 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4743 if vm_ok
==0: #all goes wrong
4748 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
4750 if nfvo_tenant
and nfvo_tenant
!= "any":
4751 filter["tenant_id"] = nfvo_tenant
4752 if instance_id
and instance_id
!= "any":
4753 filter["instance_id"] = instance_id
4755 filter["uuid"] = action_id
4756 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
4759 raise NfvoException("Not found any action with this criteria", httperrors
.Not_Found
)
4760 vim_wim_actions
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
={"instance_action_id": action_id
})
4761 rows
[0]["vim_wim_actions"] = vim_wim_actions
4762 # for backward compatibility set vim_actions = vim_wim_actions
4763 rows
[0]["vim_actions"] = vim_wim_actions
4764 return {"actions": rows
}
4767 def create_or_use_console_proxy_thread(console_server
, console_port
):
4768 #look for a non-used port
4769 console_thread_key
= console_server
+ ":" + str(console_port
)
4770 if console_thread_key
in global_config
["console_thread"]:
4771 #global_config["console_thread"][console_thread_key].start_timeout()
4772 return global_config
["console_thread"][console_thread_key
]
4774 for port
in global_config
["console_port_iterator"]():
4775 #print "create_or_use_console_proxy_thread() port:", port
4776 if port
in global_config
["console_ports"]:
4779 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
4781 global_config
["console_thread"][console_thread_key
] = clithread
4782 global_config
["console_ports"][port
] = console_thread_key
4784 except cli
.ConsoleProxyExceptionPortUsed
as e
:
4785 #port used, try with onoher
4787 except cli
.ConsoleProxyException
as e
:
4788 raise NfvoException(str(e
), httperrors
.Bad_Request
)
4789 raise NfvoException("Not found any free 'http_console_ports'", httperrors
.Conflict
)
4792 def check_tenant(mydb
, tenant_id
):
4793 '''check that tenant exists at database'''
4794 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
4796 raise NfvoException("tenant '{}' not found".format(tenant_id
), httperrors
.Not_Found
)
4799 def new_tenant(mydb
, tenant_dict
):
4801 tenant_uuid
= str(uuid4())
4802 tenant_dict
['uuid'] = tenant_uuid
4804 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
4805 tenant_dict
['RO_pub_key'] = pub_key
4806 tenant_dict
['encrypted_RO_priv_key'] = priv_key
4807 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
4808 except db_base_Exception
as e
:
4809 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
4812 def delete_tenant(mydb
, tenant
):
4813 #get nfvo_tenant info
4815 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
4816 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
4817 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
4820 def new_datacenter(mydb
, datacenter_descriptor
):
4821 sdn_port_mapping
= None
4822 if "config" in datacenter_descriptor
:
4823 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
4824 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
4826 # Check that datacenter-type is correct
4827 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
4828 # module_info = None
4830 module
= "vimconn_" + datacenter_type
4831 pkg
= __import__("osm_ro." + module
)
4832 # vim_conn = getattr(pkg, module)
4833 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
4834 except (IOError, ImportError):
4835 # if module_info and module_info[0]:
4836 # file.close(module_info[0])
4837 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type
,
4839 httperrors
.Bad_Request
)
4841 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
4842 if sdn_port_mapping
:
4844 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
4845 except Exception as e
:
4846 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
4848 return datacenter_id
4851 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
4852 # obtain data, check that only one exist
4853 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
4856 datacenter_id
= datacenter
['uuid']
4857 where
= {'uuid': datacenter
['uuid']}
4858 remove_port_mapping
= False
4859 new_sdn_port_mapping
= None
4860 if "config" in datacenter_descriptor
:
4861 if datacenter_descriptor
['config'] != None:
4863 new_config_dict
= datacenter_descriptor
["config"]
4864 if "sdn-port-mapping" in new_config_dict
:
4865 remove_port_mapping
= True
4866 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
4867 # delete null fields
4869 for k
in new_config_dict
:
4870 if new_config_dict
[k
] is None:
4872 if k
== 'sdn-controller':
4873 remove_port_mapping
= True
4875 config_text
= datacenter
.get("config")
4878 config_dict
= yaml
.load(config_text
)
4879 config_dict
.update(new_config_dict
)
4880 # delete null fields
4883 except Exception as e
:
4884 raise NfvoException("Bad format at datacenter:config " + str(e
), httperrors
.Bad_Request
)
4886 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
4888 datacenter_descriptor
["config"] = None
4889 if remove_port_mapping
:
4891 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
4892 except ovimException
as e
:
4893 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
4895 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
4896 if new_sdn_port_mapping
:
4898 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
4899 except ovimException
as e
:
4901 mydb
.update_rows('datacenters', datacenter
, where
)
4902 raise NfvoException("Error adding datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
4903 return datacenter_id
4906 def delete_datacenter(mydb
, datacenter
):
4907 #get nfvo_tenant info
4908 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
4909 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
4911 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
4912 except ovimException
as e
:
4913 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
))
4914 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
4917 def create_vim_account(mydb
, nfvo_tenant
, datacenter_id
, name
=None, vim_id
=None, vim_tenant
=None, vim_tenant_name
=None,
4918 vim_username
=None, vim_password
=None, config
=None):
4919 # get datacenter info
4921 if not datacenter_id
:
4923 raise NfvoException("You must provide 'vim_id", http_code
=httperrors
.Bad_Request
)
4924 datacenter_id
= vim_id
4925 datacenter_id
, datacenter_name
= get_datacenter_uuid(mydb
, None, datacenter_id
)
4927 create_vim_tenant
= True if not vim_tenant
and not vim_tenant_name
else False
4929 # get nfvo_tenant info
4930 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
4931 if vim_tenant_name
==None:
4932 vim_tenant_name
=tenant_dict
['name']
4934 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
4935 # #check that this association does not exist before
4936 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4937 # if len(tenants_datacenters)>0:
4938 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
4940 vim_tenant_id_exist_atdb
=False
4941 if not create_vim_tenant
:
4942 where_
={"datacenter_id": datacenter_id
}
4943 if vim_tenant
!=None:
4944 where_
["vim_tenant_id"] = vim_tenant
4945 if vim_tenant_name
!=None:
4946 where_
["vim_tenant_name"] = vim_tenant_name
4947 #check if vim_tenant_id is already at database
4948 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
4949 if len(datacenter_tenants_dict
)>=1:
4950 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
4951 vim_tenant_id_exist_atdb
=True
4952 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4954 datacenter_tenants_dict
= {}
4955 #insert at table datacenter_tenants
4956 else: #if vim_tenant==None:
4957 #create tenant at VIM if not provided
4959 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
,
4960 vim_passwd
=vim_password
)
4961 datacenter_name
= myvim
["name"]
4962 vim_tenant
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
4963 except vimconn
.vimconnException
as e
:
4964 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), httperrors
.Internal_Server_Error
)
4965 datacenter_tenants_dict
= {}
4966 datacenter_tenants_dict
["created"]="true"
4968 #fill datacenter_tenants table
4969 if not vim_tenant_id_exist_atdb
:
4970 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant
4971 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
4972 datacenter_tenants_dict
["user"] = vim_username
4973 datacenter_tenants_dict
["passwd"] = vim_password
4974 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
4976 datacenter_tenants_dict
["name"] = name
4978 datacenter_tenants_dict
["name"] = datacenter_name
4980 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
4981 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
4982 datacenter_tenants_dict
["uuid"] = id_
4984 #fill tenants_datacenters table
4985 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
4986 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
4987 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
4990 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
4991 new_thread
= vim_thread
.vim_thread(task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
4992 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
4994 thread_id
= datacenter_tenants_dict
["uuid"]
4995 vim_threads
["running"][thread_id
] = new_thread
4997 except vimconn
.vimconnException
as e
:
4998 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5001 def edit_vim_account(mydb
, nfvo_tenant
, datacenter_tenant_id
, datacenter_id
=None, name
=None, vim_tenant
=None,
5002 vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
5004 # get vim_account; check is valid for this tenant
5005 from_
= "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5006 where_
= {"td.nfvo_tenant_id": nfvo_tenant
}
5007 if datacenter_tenant_id
:
5008 where_
["dt.uuid"] = datacenter_tenant_id
5010 where_
["dt.datacenter_id"] = datacenter_id
5011 vim_accounts
= mydb
.get_rows(SELECT
="dt.uuid as uuid, config", FROM
=from_
, WHERE
=where_
)
5012 if not vim_accounts
:
5013 raise NfvoException("vim_account not found for this tenant", http_code
=httperrors
.Not_Found
)
5014 elif len(vim_accounts
) > 1:
5015 raise NfvoException("found more than one vim_account for this tenant", http_code
=httperrors
.Conflict
)
5016 datacenter_tenant_id
= vim_accounts
[0]["uuid"]
5017 original_config
= vim_accounts
[0]["config"]
5021 original_config_dict
= yaml
.load(original_config
)
5022 original_config_dict
.update(config
)
5023 update
["config"] = yaml
.safe_dump(original_config_dict
, default_flow_style
=True, width
=256)
5025 update_
['name'] = name
5027 update_
['vim_tenant_id'] = vim_tenant
5029 update_
['vim_tenant_name'] = vim_tenant_name
5031 update_
['user'] = vim_username
5033 update_
['passwd'] = vim_password
5035 mydb
.update_rows("datacenter_tenants", UPDATE
=update_
, WHERE
={"uuid": datacenter_tenant_id
})
5037 vim_threads
["running"][datacenter_tenant_id
].insert_task("reload")
5038 return datacenter_tenant_id
5040 def delete_vim_account(mydb
, tenant_id
, vim_account_id
, datacenter
=None):
5041 #get nfvo_tenant info
5042 if not tenant_id
or tenant_id
=="any":
5045 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
5046 tenant_uuid
= tenant_dict
['uuid']
5048 #check that this association exist before
5049 tenants_datacenter_dict
= {}
5051 datacenter_id
, _
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
5052 tenants_datacenter_dict
["datacenter_id"] = datacenter_id
5053 elif vim_account_id
:
5054 tenants_datacenter_dict
["datacenter_tenant_id"] = vim_account_id
5056 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
5057 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5058 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
5059 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), httperrors
.Not_Found
)
5061 #delete this association
5062 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5064 #get vim_tenant info and deletes
5066 for tenant_datacenter_item
in tenant_datacenter_list
:
5067 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5068 #try to delete vim:tenant
5070 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5071 if vim_tenant_dict
['created']=='true':
5072 #delete tenant at VIM if created by NFVO
5074 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5075 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
5076 except vimconn
.vimconnException
as e
:
5077 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
5078 logger
.warn(warning
)
5079 except db_base_Exception
as e
:
5080 logger
.error("Cannot delete datacenter_tenants " + str(e
))
5081 pass # the error will be caused because dependencies, vim_tenant can not be deleted
5082 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
5083 thread
= vim_threads
["running"].get(thread_id
)
5085 thread
.insert_task("exit")
5086 vim_threads
["deleting"][thread_id
] = thread
5087 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
5090 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
5092 #get datacenter info
5093 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5095 if 'net-update' in action_dict
:
5097 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
5099 except vimconn
.vimconnException
as e
:
5100 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5101 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5102 #update nets Change from VIM format to NFVO format
5105 net_nfvo
={'datacenter_id': datacenter_id
}
5106 net_nfvo
['name'] = net
['name']
5107 #net_nfvo['description']= net['name']
5108 net_nfvo
['vim_net_id'] = net
['id']
5109 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5110 net_nfvo
['shared'] = net
['shared']
5111 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5112 net_list
.append(net_nfvo
)
5113 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
5114 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
5116 elif 'net-edit' in action_dict
:
5117 net
= action_dict
['net-edit'].pop('net')
5118 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5119 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
5120 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5122 elif 'net-delete' in action_dict
:
5123 net
= action_dict
['net-deelte'].get('net')
5124 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5125 result
= mydb
.delete_row(FROM
='datacenter_nets',
5126 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5130 raise NfvoException("Unknown action " + str(action_dict
), httperrors
.Bad_Request
)
5133 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
5134 #get datacenter info
5135 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5137 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
5138 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
5139 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
5143 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
5144 #get datacenter info
5145 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5148 action_dict
= action_dict
["netmap"]
5149 if 'vim_id' in action_dict
:
5150 filter_dict
["id"] = action_dict
['vim_id']
5151 if 'vim_name' in action_dict
:
5152 filter_dict
["name"] = action_dict
['vim_name']
5154 filter_dict
["shared"] = True
5157 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
5158 except vimconn
.vimconnException
as e
:
5159 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5160 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5161 if len(vim_nets
)>1 and action_dict
:
5162 raise NfvoException("more than two networks found, specify with vim_id", httperrors
.Conflict
)
5163 elif len(vim_nets
)==0: # and action_dict:
5164 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), httperrors
.Not_Found
)
5166 for net
in vim_nets
:
5167 net_nfvo
={'datacenter_id': datacenter_id
}
5168 if action_dict
and "name" in action_dict
:
5169 net_nfvo
['name'] = action_dict
['name']
5171 net_nfvo
['name'] = net
['name']
5172 #net_nfvo['description']= net['name']
5173 net_nfvo
['vim_net_id'] = net
['id']
5174 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5175 net_nfvo
['shared'] = net
['shared']
5176 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5178 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
5179 net_nfvo
["status"] = "OK"
5180 net_nfvo
["uuid"] = net_id
5181 except db_base_Exception
as e
:
5185 net_nfvo
["status"] = "FAIL: " + str(e
)
5186 net_list
.append(net_nfvo
)
5189 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
5190 # obtain all network data
5192 if utils
.check_valid_uuid(network_id
):
5193 filter_dict
= {"id": network_id
}
5195 filter_dict
= {"name": network_id
}
5197 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5198 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
5199 except vimconn
.vimconnException
as e
:
5200 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
5202 # ensure the network is defined
5203 if len(network
) == 0:
5204 raise NfvoException("Network {} is not present in the system".format(network_id
),
5205 httperrors
.Bad_Request
)
5207 # ensure there is only one network with the provided name
5208 if len(network
) > 1:
5209 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), httperrors
.Bad_Request
)
5211 # ensure it is a dataplane network
5212 if network
[0]['type'] != 'data':
5215 # ensure we use the id
5216 network_id
= network
[0]['id']
5218 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5219 # and with instance_scenario_id==NULL
5220 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5221 search_dict
= {'vim_net_id': network_id
}
5224 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5225 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5226 except db_base_Exception
as e
:
5227 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5228 network_id
) + str(e
), e
.http_code
)
5232 if net
['sdn_net_id'] != None:
5234 sdn_net_id
= net
['sdn_net_id']
5236 if sdn_net_counter
== 0:
5238 elif sdn_net_counter
== 1:
5241 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5242 network_id
), httperrors
.Internal_Server_Error
)
5244 def get_sdn_controller_id(mydb
, datacenter
):
5245 # Obtain sdn controller id
5246 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5250 return yaml
.load(config
).get('sdn-controller')
5252 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5254 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5255 if not sdn_network_id
:
5256 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), httperrors
.Internal_Server_Error
)
5258 #Obtain sdn controller id
5259 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5260 if not controller_id
:
5261 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), httperrors
.Internal_Server_Error
)
5263 #Obtain sdn controller info
5264 sdn_controller
= ovim
.show_of_controller(controller_id
)
5267 'name': 'external_port',
5268 'net_id': sdn_network_id
,
5269 'ofc_id': controller_id
,
5270 'switch_dpid': sdn_controller
['dpid'],
5271 'switch_port': descriptor
['port']
5274 if 'vlan' in descriptor
:
5275 port_data
['vlan'] = descriptor
['vlan']
5276 if 'mac' in descriptor
:
5277 port_data
['mac'] = descriptor
['mac']
5279 result
= ovim
.new_port(port_data
)
5280 except ovimException
as e
:
5281 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5282 sdn_network_id
, network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5283 except db_base_Exception
as e
:
5284 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5285 network_id
) + str(e
), e
.http_code
)
5287 return 'Port uuid: '+ result
5289 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5291 filter = {'uuid': port_id
}
5293 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5294 if not sdn_network_id
:
5295 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5296 httperrors
.Internal_Server_Error
)
5297 #in case no port_id is specified only ports marked as 'external_port' will be detached
5298 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5301 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5302 except ovimException
as e
:
5303 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5304 httperrors
.Internal_Server_Error
)
5306 if len(port_list
) == 0:
5307 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5308 httperrors
.Bad_Request
)
5311 for port
in port_list
:
5313 port_uuid_list
.append(port
['uuid'])
5314 ovim
.delete_port(port
['uuid'])
5315 except ovimException
as e
:
5316 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5318 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5320 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5321 #get datacenter info
5322 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5325 if utils
.check_valid_uuid(name
):
5326 filter_dict
["id"] = name
5328 filter_dict
["name"] = name
5330 if item
=="networks":
5331 #filter_dict['tenant_id'] = myvim['tenant_id']
5332 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5334 if len(content
) == 0:
5335 raise NfvoException("Network {} is not present in the system. ".format(name
),
5336 httperrors
.Bad_Request
)
5338 #Update the networks with the attached ports
5340 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5341 if sdn_network_id
!= None:
5343 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5344 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5345 except ovimException
as e
:
5346 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5347 #Remove field name and if port name is external_port save it as 'type'
5348 for port
in port_list
:
5349 if port
['name'] == 'external_port':
5350 port
['type'] = "External"
5352 net
['sdn_network_id'] = sdn_network_id
5353 net
['sdn_attached_ports'] = port_list
5355 elif item
=="tenants":
5356 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5357 elif item
== "images":
5359 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5361 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5362 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5363 if name
and len(content
)==1:
5364 return {item
[:-1]: content
[0]}
5365 elif name
and len(content
)==0:
5366 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
5369 return {item
: content
}
5370 except vimconn
.vimconnException
as e
:
5371 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
5372 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5375 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5376 #get datacenter info
5377 if tenant_id
== "any":
5380 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5382 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5383 logger
.debug("vim_action_delete vim response: " + str(content
))
5384 items
= content
.values()[0]
5385 if type(items
)==list and len(items
)==0:
5386 raise NfvoException("Not found " + item
, httperrors
.Not_Found
)
5387 elif type(items
)==list and len(items
)>1:
5388 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), httperrors
.Not_Found
)
5389 else: # it is a dict
5390 item_id
= items
["id"]
5391 item_name
= str(items
.get("name"))
5394 if item
=="networks":
5395 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5396 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5397 if sdn_network_id
!= None:
5398 #Delete any port attachment to this network
5400 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5401 except ovimException
as e
:
5402 raise NfvoException(
5403 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5404 httperrors
.Internal_Server_Error
)
5406 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5407 for port
in port_list
:
5408 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5410 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5412 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
5413 except db_base_Exception
as e
:
5414 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
5415 str(e
), e
.http_code
)
5417 #Delete the SDN network
5419 ovim
.delete_network(sdn_network_id
)
5420 except ovimException
as e
:
5421 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5422 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5423 httperrors
.Internal_Server_Error
)
5425 content
= myvim
.delete_network(item_id
)
5426 elif item
=="tenants":
5427 content
= myvim
.delete_tenant(item_id
)
5428 elif item
== "images":
5429 content
= myvim
.delete_image(item_id
)
5431 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5432 except vimconn
.vimconnException
as e
:
5433 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5434 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5436 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5439 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5440 #get datacenter info
5441 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5442 if tenant_id
== "any":
5444 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5446 if item
=="networks":
5447 net
= descriptor
["network"]
5448 net_name
= net
.pop("name")
5449 net_type
= net
.pop("type", "bridge")
5450 net_public
= net
.pop("shared", False)
5451 net_ipprofile
= net
.pop("ip_profile", None)
5452 net_vlan
= net
.pop("vlan", None)
5453 content
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
5455 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5456 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5457 #obtain datacenter_tenant_id
5458 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5459 FROM
='datacenter_tenants',
5460 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5463 sdn_network
['vlan'] = net_vlan
5464 sdn_network
['type'] = net_type
5465 sdn_network
['name'] = net_name
5466 sdn_network
['region'] = datacenter_tenant_id
5467 ovim_content
= ovim
.new_network(sdn_network
)
5468 except ovimException
as e
:
5469 logger
.error("ovimException creating SDN network={} ".format(
5470 sdn_network
) + str(e
), exc_info
=True)
5471 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5472 httperrors
.Internal_Server_Error
)
5474 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5475 # use instance_scenario_id=None to distinguish from real instaces of nets
5476 correspondence
= {'instance_scenario_id': None,
5477 'sdn_net_id': ovim_content
,
5478 'vim_net_id': content
,
5479 'datacenter_tenant_id': datacenter_tenant_id
5482 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5483 except db_base_Exception
as e
:
5484 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5485 correspondence
, e
), e
.http_code
)
5486 elif item
=="tenants":
5487 tenant
= descriptor
["tenant"]
5488 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5490 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5491 except vimconn
.vimconnException
as e
:
5492 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5494 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5496 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5497 data
= ovim
.new_of_controller(sdn_controller
)
5498 logger
.debug('New SDN controller created with uuid {}'.format(data
))
5501 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5502 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5503 msg
= 'SDN controller {} updated'.format(data
)
5507 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5508 if controller_id
== None:
5509 data
= ovim
.get_of_controllers()
5511 data
= ovim
.show_of_controller(controller_id
)
5513 msg
= 'SDN controller list:\n {}'.format(data
)
5517 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5518 select_
= ('uuid', 'config')
5519 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5520 for datacenter
in datacenters
:
5521 if datacenter
['config']:
5522 config
= yaml
.load(datacenter
['config'])
5523 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5524 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), httperrors
.Conflict
)
5526 data
= ovim
.delete_of_controller(controller_id
)
5527 msg
= 'SDN controller {} deleted'.format(data
)
5531 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5532 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5533 if len(controller
) < 1:
5534 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), httperrors
.Not_Found
)
5537 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
5539 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), httperrors
.Bad_Request
)
5541 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5542 switch_dpid
= sdn_controller
["dpid"]
5545 for compute_node
in sdn_port_mapping
:
5546 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5548 element
["compute_node"] = compute_node
["compute_node"]
5549 for port
in compute_node
["ports"]:
5550 pci
= port
.get("pci")
5551 element
["switch_port"] = port
.get("switch_port")
5552 element
["switch_mac"] = port
.get("switch_mac")
5553 if not pci
or not (element
["switch_port"] or element
["switch_mac"]):
5554 raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'"
5555 " or 'switch_mac'", httperrors
.Bad_Request
)
5556 for pci_expanded
in utils
.expand_brackets(pci
):
5557 element
["pci"] = pci_expanded
5558 maps
.append(dict(element
))
5560 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
5562 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5563 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
5566 "sdn-controller": None,
5567 "datacenter-id": datacenter_id
,
5569 "ports_mapping": list()
5572 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5573 if datacenter
['config']:
5574 config
= yaml
.load(datacenter
['config'])
5575 if 'sdn-controller' in config
:
5576 controller_id
= config
['sdn-controller']
5577 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5578 result
["sdn-controller"] = controller_id
5579 result
["dpid"] = sdn_controller
["dpid"]
5581 if result
["sdn-controller"] == None:
5582 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), httperrors
.Bad_Request
)
5583 if result
["dpid"] == None:
5584 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5585 httperrors
.Internal_Server_Error
)
5590 ports_correspondence_dict
= dict()
5592 if result
["sdn-controller"] != link
["ofc_id"]:
5593 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5594 if result
["dpid"] != link
["switch_dpid"]:
5595 raise NfvoException("The dpid specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5597 element
["pci"] = link
["pci"]
5598 if link
["switch_port"]:
5599 element
["switch_port"] = link
["switch_port"]
5600 if link
["switch_mac"]:
5601 element
["switch_mac"] = link
["switch_mac"]
5603 if not link
["compute_node"] in ports_correspondence_dict
:
5605 content
["compute_node"] = link
["compute_node"]
5606 content
["ports"] = list()
5607 ports_correspondence_dict
[link
["compute_node"]] = content
5609 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
5611 for key
in sorted(ports_correspondence_dict
):
5612 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5616 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5617 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})
5619 def create_RO_keypair(tenant_id
):
5621 Creates a public / private keys for a RO tenant and returns their values
5623 tenant_id: ID of the tenant
5625 public_key: Public key for the RO tenant
5626 private_key: Encrypted private key for RO tenant
5630 key
= RSA
.generate(bits
)
5632 public_key
= key
.publickey().exportKey('OpenSSH')
5633 if isinstance(public_key
, ValueError):
5634 raise NfvoException("Unable to create public key: {}".format(public_key
), httperrors
.Internal_Server_Error
)
5635 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5636 except (ValueError, NameError) as e
:
5637 raise NfvoException("Unable to create private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5638 return public_key
, private_key
5640 def decrypt_key (key
, tenant_id
):
5642 Decrypts an encrypted RSA key
5644 key: Private key to be decrypted
5645 tenant_id: ID of the tenant
5647 unencrypted_key: Unencrypted private key for RO tenant
5650 key
= RSA
.importKey(key
,tenant_id
)
5651 unencrypted_key
= key
.exportKey('PEM')
5652 if isinstance(unencrypted_key
, ValueError):
5653 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), httperrors
.Internal_Server_Error
)
5654 except ValueError as e
:
5655 raise NfvoException("Unable to decrypt the private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5656 return unencrypted_key