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(lock
=db_lock
)
156 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
159 persistence
= persistence
or WimPersistence(db
)
161 # Initialize openvim for SDN control
162 # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
163 # TODO: review ovim.py to delete not needed configuration
164 ovim_configuration
= {
165 'logger_name': 'openmano.ovim',
166 'network_vlan_range_start': 1000,
167 'network_vlan_range_end': 4096,
168 'db_name': global_config
["db_ovim_name"],
169 'db_host': global_config
["db_ovim_host"],
170 'db_user': global_config
["db_ovim_user"],
171 'db_passwd': global_config
["db_ovim_passwd"],
174 'network_type': 'bridge',
175 #TODO: log_level_of should not be needed. To be modified in ovim
176 'log_level_of': 'DEBUG'
179 # starts ovim library
180 ovim
= ovim_module
.ovim(ovim_configuration
)
183 wim_engine
= wim
or WimEngine(persistence
)
184 wim_engine
.ovim
= ovim
188 #delete old unneeded vim_wim_actions
192 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
193 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
194 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
195 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
196 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
197 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
198 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
200 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
201 'datacenter_id': vim
.get('datacenter_id')}
203 extra
.update(yaml
.load(vim
["config"]))
204 if vim
.get('dt_config'):
205 extra
.update(yaml
.load(vim
["dt_config"]))
206 if vim
["type"] not in vimconn_imported
:
209 module
= "vimconn_" + vim
["type"]
210 pkg
= __import__("osm_ro." + module
)
211 vim_conn
= getattr(pkg
, module
)
212 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
213 # vim_conn = imp.load_module(vim["type"], *module_info)
214 vimconn_imported
[vim
["type"]] = vim_conn
215 except (IOError, ImportError) as e
:
216 # if module_info and module_info[0]:
217 # file.close(module_info[0])
218 raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format(
219 vim
["type"], module
, type(e
).__name
__, str(e
)), httperrors
.Bad_Request
)
221 thread_id
= vim
['datacenter_tenant_id']
222 vim_persistent_info
[thread_id
] = {}
225 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
226 myvim
= vimconn_imported
[ vim
["type"] ].vimconnector(
227 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
228 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
229 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
230 user
=vim
['user'], passwd
=vim
['passwd'],
231 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
233 except vimconn
.vimconnException
as e
:
235 logger
.error("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
236 vim
['datacenter_id'], e
))
237 except Exception as e
:
238 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, e
),
239 httperrors
.Internal_Server_Error
)
240 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['vim_tenant_id'], vim
['vim_tenant_name'],
241 vim
['vim_tenant_id'])
242 new_thread
= vim_thread
.vim_thread(task_lock
, thread_name
, vim
['datacenter_name'],
243 vim
['datacenter_tenant_id'], db
=db
, db_lock
=db_lock
, ovim
=ovim
)
245 vim_threads
["running"][thread_id
] = new_thread
247 wim_engine
.start_threads()
248 except db_base_Exception
as e
:
249 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
250 except ovim_module
.ovimException
as e
:
252 if message
[:22] == "DATABASE wrong version":
253 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
254 "at host {dbhost}".format(
255 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
256 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
257 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
258 raise NfvoException(message
, httperrors
.Bad_Request
)
262 global ovim
, global_config
265 for thread_id
, thread
in vim_threads
["running"].items():
266 thread
.insert_task("exit")
267 vim_threads
["deleting"][thread_id
] = thread
268 vim_threads
["running"] = {}
271 wim_engine
.stop_threads()
273 if global_config
and global_config
.get("console_thread"):
274 for thread
in global_config
["console_thread"]:
275 thread
.terminate
= True
278 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
279 global_config
["version_date"] ))
283 Clean unused or old entries at database to avoid unlimited growing
284 :param mydb: database connector
287 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
288 now
= t
.time()-3600*24*7
289 instance_action_id
= None
292 actions_to_delete
= mydb
.get_rows(
293 SELECT
=("item", "item_id", "instance_action_id"),
294 FROM
="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
295 "left join instance_scenarios as i on ia.instance_id=i.uuid",
296 WHERE
={"va.action": "DELETE", "va.modified_at<": now
, "i.uuid": None,
297 "va.status": ("DONE", "SUPERSEDED")},
300 for to_delete
in actions_to_delete
:
301 mydb
.delete_row(FROM
="vim_wim_actions", WHERE
=to_delete
)
302 if instance_action_id
!= to_delete
["instance_action_id"]:
303 instance_action_id
= to_delete
["instance_action_id"]
304 mydb
.delete_row(FROM
="instance_actions", WHERE
={"uuid": instance_action_id
})
305 nb_deleted
+= len(actions_to_delete
)
306 if len(actions_to_delete
) < 100:
309 logger
.debug("Removed {} unused vim_wim_actions".format(nb_deleted
))
312 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
314 return result, content:
315 <0, error_text upon error
316 nb_records, flavor_list on success
319 WHERE_dict
['vnf_id'] = vnf_id
320 if nfvo_tenant
is not None:
321 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
323 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
324 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
325 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
326 #print "get_flavor_list result:", result
327 #print "get_flavor_list content:", content
329 for flavor
in flavors
:
330 flavorList
.append(flavor
['flavor_id'])
334 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
336 Get used images of all vms belonging to this VNFD
337 :param mydb: database conector
338 :param vnf_id: vnfd uuid
339 :param nfvo_tenant: tenant, not used
340 :return: The list of image uuid used
343 vms
= mydb
.get_rows(SELECT
=('image_id','image_list'), FROM
='vms', WHERE
={'vnf_id': vnf_id
})
345 if vm
["image_id"] and vm
["image_id"] not in image_list
:
346 image_list
.append(vm
["image_id"])
348 vm_image_list
= yaml
.load(vm
["image_list"])
349 for image_dict
in vm_image_list
:
350 if image_dict
["image_id"] not in image_list
:
351 image_list
.append(image_dict
["image_id"])
355 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
356 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None, ignore_errors
=False):
357 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
358 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
359 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
360 raise exception upon error
363 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
364 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
365 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
366 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
367 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
368 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
369 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
370 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'
371 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
372 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
373 'user','passwd', 'dt.config as dt_config')
375 from_
= 'datacenters as d'
376 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
378 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
381 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
382 'datacenter_id': vim
.get('datacenter_id'),
383 '_vim_type_internal': vim
.get('type')}
385 extra
.update(yaml
.load(vim
["config"]))
386 if vim
.get('dt_config'):
387 extra
.update(yaml
.load(vim
["dt_config"]))
388 if vim
["type"] not in vimconn_imported
:
391 module
= "vimconn_" + vim
["type"]
392 pkg
= __import__("osm_ro." + module
)
393 vim_conn
= getattr(pkg
, module
)
394 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
395 # vim_conn = imp.load_module(vim["type"], *module_info)
396 vimconn_imported
[vim
["type"]] = vim_conn
397 except (IOError, ImportError) as e
:
398 # if module_info and module_info[0]:
399 # file.close(module_info[0])
401 logger
.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
402 vim
["type"], module
, type(e
).__name
__, str(e
)))
404 raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
405 vim
["type"], module
, type(e
).__name
__, str(e
)), httperrors
.Bad_Request
)
408 if 'datacenter_tenant_id' in vim
:
409 thread_id
= vim
["datacenter_tenant_id"]
410 if thread_id
not in vim_persistent_info
:
411 vim_persistent_info
[thread_id
] = {}
412 persistent_info
= vim_persistent_info
[thread_id
]
416 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
417 vim_dict
[ vim
['datacenter_id'] ] = vimconn_imported
[ vim
["type"] ].vimconnector(
418 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
419 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
420 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
421 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
422 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
423 config
=extra
, persistent_info
=persistent_info
425 except Exception as e
:
427 logger
.error("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)))
429 http_code
= httperrors
.Internal_Server_Error
430 if isinstance(e
, vimconn
.vimconnException
):
431 http_code
= e
.http_code
432 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), http_code
)
434 except db_base_Exception
as e
:
435 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
438 def rollback(mydb
, vims
, rollback_list
):
440 #delete things by reverse order
441 for i
in range(len(rollback_list
)-1, -1, -1):
442 item
= rollback_list
[i
]
443 if item
["where"]=="vim":
444 if item
["vim_id"] not in vims
:
446 if is_task_id(item
["uuid"]):
448 vim
= vims
[item
["vim_id"]]
450 if item
["what"]=="image":
451 vim
.delete_image(item
["uuid"])
452 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
453 elif item
["what"]=="flavor":
454 vim
.delete_flavor(item
["uuid"])
455 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
456 elif item
["what"]=="network":
457 vim
.delete_network(item
["uuid"])
458 elif item
["what"]=="vm":
459 vim
.delete_vminstance(item
["uuid"])
460 except vimconn
.vimconnException
as e
:
461 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
462 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
463 except db_base_Exception
as e
:
464 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
468 if item
["what"]=="image":
469 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
470 elif item
["what"]=="flavor":
471 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
472 except db_base_Exception
as e
:
473 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
474 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
475 if len(undeleted_items
)==0:
476 return True," Rollback successful."
478 return False," Rollback fails to delete: " + str(undeleted_items
)
481 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
483 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
485 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
487 #dataplane interfaces
488 for numa
in vnfc
.get("numas",() ):
489 for interface
in numa
.get("interfaces",()):
490 if interface
["name"] in name_dict
:
492 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
493 vnfc
["name"], interface
["name"]),
494 httperrors
.Bad_Request
)
495 name_dict
[ interface
["name"] ] = "underlay"
497 for interface
in vnfc
.get("bridge-ifaces",() ):
498 if interface
["name"] in name_dict
:
500 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
501 vnfc
["name"], interface
["name"]),
502 httperrors
.Bad_Request
)
503 name_dict
[ interface
["name"] ] = "overlay"
504 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
505 # check bood-data info
506 # if "boot-data" in vnfc:
507 # # check that user-data is incompatible with users and config-files
508 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
509 # raise NfvoException(
510 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
511 # httperrors.Bad_Request)
513 #check if the info in external_connections matches with the one in the vnfcs
515 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
516 if external_connection
["name"] in name_list
:
518 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
519 external_connection
["name"]),
520 httperrors
.Bad_Request
)
521 name_list
.append(external_connection
["name"])
522 if external_connection
["VNFC"] not in vnfc_interfaces
:
524 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
525 external_connection
["name"], external_connection
["VNFC"]),
526 httperrors
.Bad_Request
)
528 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
530 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
531 external_connection
["name"],
532 external_connection
["local_iface_name"]),
533 httperrors
.Bad_Request
)
535 #check if the info in internal_connections matches with the one in the vnfcs
537 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
538 if internal_connection
["name"] in name_list
:
540 "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection".format(
541 internal_connection
["name"]),
542 httperrors
.Bad_Request
)
543 name_list
.append(internal_connection
["name"])
544 #We should check that internal-connections of type "ptp" have only 2 elements
546 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
548 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
549 internal_connection
["name"],
550 'ptp' if vnf_descriptor_version
==1 else 'e-line',
551 'data' if vnf_descriptor_version
==1 else "e-lan"),
552 httperrors
.Bad_Request
)
553 for port
in internal_connection
["elements"]:
555 iface
= port
["local_iface_name"]
556 if vnf
not in vnfc_interfaces
:
558 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
559 internal_connection
["name"], vnf
),
560 httperrors
.Bad_Request
)
561 if iface
not in vnfc_interfaces
[ vnf
]:
563 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
564 internal_connection
["name"], iface
),
565 httperrors
.Bad_Request
)
566 return -httperrors
.Bad_Request
,
567 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
568 if vnfc_interfaces
[vnf
][iface
] == "overlay":
569 internal_connection
["type"] = "bridge"
571 internal_connection
["type"] = "data"
572 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
573 if vnfc_interfaces
[vnf
][iface
] == "overlay":
574 internal_connection
["implementation"] = "overlay"
576 internal_connection
["implementation"] = "underlay"
577 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
578 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
580 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
581 internal_connection
["name"],
582 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
583 'data' if vnf_descriptor_version
==1 else 'underlay'),
584 httperrors
.Bad_Request
)
585 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
586 vnfc_interfaces
[vnf
][iface
] == "underlay":
588 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
589 internal_connection
["name"], iface
,
590 'data' if vnf_descriptor_version
==1 else 'underlay',
591 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
592 httperrors
.Bad_Request
)
595 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
597 if only_create_at_vim
:
598 image_mano_id
= image_dict
['uuid']
599 if return_on_error
== None:
600 return_on_error
= True
602 if image_dict
['location']:
603 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
605 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
607 image_mano_id
= images
[0]['uuid']
609 #create image in MANO DB
610 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
611 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
612 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
614 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
615 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
616 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
617 #create image at every vim
618 for vim_id
,vim
in vims
.iteritems():
619 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
620 image_created
="false"
622 image_db
= mydb
.get_rows(FROM
="datacenters_images",
623 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
624 #look at VIM if this image exist
626 if image_dict
['location'] is not None:
627 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
630 filter_dict
['name'] = image_dict
['universal_name']
631 if image_dict
.get('checksum') != None:
632 filter_dict
['checksum'] = image_dict
['checksum']
633 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
634 vim_images
= vim
.get_image_list(filter_dict
)
635 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
636 if len(vim_images
) > 1:
637 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), httperrors
.Conflict
)
638 elif len(vim_images
) == 0:
639 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
641 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
642 image_vim_id
= vim_images
[0]['id']
644 except vimconn
.vimconnNotFoundException
as e
:
645 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
647 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
648 if image_dict
['location']:
649 image_vim_id
= vim
.new_image(image_dict
)
650 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
653 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
654 raise vimconn
.vimconnException(str(e
))
655 except vimconn
.vimconnException
as e
:
657 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
660 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
662 except vimconn
.vimconnException
as e
:
664 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
666 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
669 #if we reach here, the image has been created or existed
671 #add new vim_id at datacenters_images
672 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
673 'image_id':image_mano_id
,
674 'vim_id': image_vim_id
,
675 'created':image_created
})
676 elif image_db
[0]["vim_id"]!=image_vim_id
:
677 #modify existing vim_id at datacenters_images
678 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
680 return image_vim_id
if only_create_at_vim
else image_mano_id
683 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
684 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',0),
685 'ram':flavor_dict
.get('ram'),
686 'vcpus':flavor_dict
.get('vcpus'),
688 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
689 del flavor_dict
['extended']
690 if 'extended' in flavor_dict
:
691 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
693 #look if flavor exist
694 if only_create_at_vim
:
695 flavor_mano_id
= flavor_dict
['uuid']
696 if return_on_error
== None:
697 return_on_error
= True
699 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
701 flavor_mano_id
= flavors
[0]['uuid']
704 #create one by one the images of aditional disks
705 dev_image_list
=[] #list of images
706 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
708 for device
in flavor_dict
['extended'].get('devices',[]):
709 if "image" not in device
and "image name" not in device
:
712 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
713 image_dict
['universal_name']=device
.get('image name')
714 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
715 image_dict
['location']=device
.get('image')
716 #image_dict['new_location']=vnfc.get('image location')
717 image_dict
['checksum']=device
.get('image checksum')
718 image_metadata_dict
= device
.get('image metadata', None)
719 image_metadata_str
= None
720 if image_metadata_dict
!= None:
721 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
722 image_dict
['metadata']=image_metadata_str
723 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
724 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
725 dev_image_list
.append(image_id
)
727 temp_flavor_dict
['name'] = flavor_dict
['name']
728 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
729 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
730 flavor_mano_id
= content
731 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
732 #create flavor at every vim
733 if 'uuid' in flavor_dict
:
734 del flavor_dict
['uuid']
736 for vim_id
,vim
in vims
.items():
737 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
738 flavor_created
="false"
740 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
741 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
742 #look at VIM if this flavor exist SKIPPED
743 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
745 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
749 # Create the flavor in VIM
750 # Translate images at devices from MANO id to VIM id
752 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
753 # make a copy of original devices
756 for device
in flavor_dict
["extended"].get("devices",[]):
759 devices_original
.append(dev
)
760 if 'image' in device
:
762 if 'image metadata' in device
:
763 del device
['image metadata']
764 if 'image checksum' in device
:
765 del device
['image checksum']
767 for index
in range(0,len(devices_original
)) :
768 device
=devices_original
[index
]
769 if "image" not in device
and "image name" not in device
:
770 # if 'size' in device:
771 disk_list
.append({'size': device
.get('size', default_volume_size
), 'name': device
.get('name')})
774 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
775 image_dict
['universal_name']=device
.get('image name')
776 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
777 image_dict
['location']=device
.get('image')
778 # image_dict['new_location']=device.get('image location')
779 image_dict
['checksum']=device
.get('image checksum')
780 image_metadata_dict
= device
.get('image metadata', None)
781 image_metadata_str
= None
782 if image_metadata_dict
!= None:
783 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
784 image_dict
['metadata']=image_metadata_str
785 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
786 image_dict
["uuid"]=image_mano_id
787 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
789 #save disk information (image must be based on and size
790 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
792 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
795 #check that this vim_id exist in VIM, if not create
796 flavor_vim_id
=flavor_db
[0]["vim_id"]
798 vim
.get_flavor(flavor_vim_id
)
799 continue #flavor exist
800 except vimconn
.vimconnException
:
802 #create flavor at vim
803 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
806 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
807 flavor_create
="false"
808 except vimconn
.vimconnException
as e
:
811 if not flavor_vim_id
:
812 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
813 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
814 flavor_created
="true"
815 except vimconn
.vimconnException
as e
:
817 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
819 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
822 #if reach here the flavor has been create or exist
823 if len(flavor_db
)==0:
824 #add new vim_id at datacenters_flavors
825 extended_devices_yaml
= None
826 if len(disk_list
) > 0:
827 extended_devices
= dict()
828 extended_devices
['disks'] = disk_list
829 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
830 mydb
.new_row('datacenters_flavors',
831 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
832 'created': flavor_created
, 'extended': extended_devices_yaml
})
833 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
834 #modify existing vim_id at datacenters_flavors
835 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
836 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
838 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
841 def get_str(obj
, field
, length
):
843 Obtain the str value,
848 value
= obj
.get(field
)
849 if value
is not None:
850 value
= str(value
)[:length
]
853 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
855 fill image content at db_image dictionary. Check if the image with this image and checksum exist
856 :param db_image: dictionary to insert data
857 :param mydb: database connector
858 :param descriptor: yang descriptor
859 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
862 db_image
["name"] = get_str(descriptor
, "image", 255)
863 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
864 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
865 db_image
["checksum"] = None
866 if db_image
["name"].startswith("/"):
867 db_image
["location"] = db_image
["name"]
868 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
870 db_image
["universal_name"] = db_image
["name"]
871 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
872 'checksum': db_image
['checksum']})
874 return existing_images
[0]["uuid"]
876 image_uuid
= str(uuid4())
877 db_image
["uuid"] = image_uuid
880 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
882 Parses an OSM IM vnfd_catalog and insert at DB
885 :param vnf_descriptor:
886 :return: The list of cretated vnf ids
889 myvnfd
= vnfd_catalog
.vnfd()
891 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
, path_helper
=True)
892 except Exception as e
:
893 raise NfvoException("Error. Invalid VNF descriptor format " + str(e
), httperrors
.Bad_Request
)
901 db_ip_profiles_index
= 0
905 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd:vnfd-catalog")
906 if not vnfd_catalog_descriptor
:
907 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd-catalog")
908 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd")
909 if not vnfd_descriptor_list
:
910 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd:vnfd")
911 for vnfd_yang
in myvnfd
.vnfd_catalog
.vnfd
.itervalues():
912 vnfd
= vnfd_yang
.get()
915 vnf_uuid
= str(uuid4())
916 uuid_list
.append(vnf_uuid
)
917 vnfd_uuid_list
.append(vnf_uuid
)
918 vnfd_id
= get_str(vnfd
, "id", 255)
922 "name": get_str(vnfd
, "name", 255),
923 "description": get_str(vnfd
, "description", 255),
924 "tenant_id": tenant_id
,
925 "vendor": get_str(vnfd
, "vendor", 255),
926 "short_name": get_str(vnfd
, "short-name", 255),
927 "descriptor": str(vnf_descriptor
)[:60000]
930 for vnfd_descriptor
in vnfd_descriptor_list
:
931 if vnfd_descriptor
["id"] == str(vnfd
["id"]):
934 # table ip_profiles (ip-profiles)
935 ip_profile_name2db_table_index
= {}
936 for ip_profile
in vnfd
.get("ip-profiles").itervalues():
938 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
939 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
940 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
941 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
942 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
943 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
946 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
947 dns_list
.append(str(dns
.get("address")))
948 db_ip_profile
["dns_address"] = ";".join(dns_list
)
949 if ip_profile
["ip-profile-params"].get('security-group'):
950 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
951 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
952 db_ip_profiles_index
+= 1
953 db_ip_profiles
.append(db_ip_profile
)
955 # table nets (internal-vld)
956 net_id2uuid
= {} # for mapping interface with network
957 for vld
in vnfd
.get("internal-vld").itervalues():
958 net_uuid
= str(uuid4())
959 uuid_list
.append(net_uuid
)
961 "name": get_str(vld
, "name", 255),
964 "description": get_str(vld
, "description", 255),
965 "osm_id": get_str(vld
, "id", 255),
966 "type": "bridge", # TODO adjust depending on connection point type
968 net_id2uuid
[vld
.get("id")] = net_uuid
969 db_nets
.append(db_net
)
970 # ip-profile, link db_ip_profile with db_sce_net
971 if vld
.get("ip-profile-ref"):
972 ip_profile_name
= vld
.get("ip-profile-ref")
973 if ip_profile_name
not in ip_profile_name2db_table_index
:
974 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
975 "'{}'. Reference to a non-existing 'ip_profiles'".format(
976 str(vnfd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
977 httperrors
.Bad_Request
)
978 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["net_id"] = net_uuid
979 else: #check no ip-address has been defined
980 for icp
in vld
.get("internal-connection-point").itervalues():
981 if icp
.get("ip-address"):
982 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
983 "contains an ip-address but no ip-profile has been defined at VLD".format(
984 str(vnfd
["id"]), str(vld
["id"]), str(icp
["id"])),
985 httperrors
.Bad_Request
)
987 # connection points vaiable declaration
988 cp_name2iface_uuid
= {}
990 cp_name2db_interface
= {}
991 vdu_id2cp_name
= {} # stored only when one external connection point is presented at this VDU
995 vdu_id2db_table_index
= {}
996 for vdu
in vnfd
.get("vdu").itervalues():
998 for vdu_descriptor
in vnfd_descriptor
["vdu"]:
999 if vdu_descriptor
["id"] == str(vdu
["id"]):
1001 vm_uuid
= str(uuid4())
1002 uuid_list
.append(vm_uuid
)
1003 vdu_id
= get_str(vdu
, "id", 255)
1007 "name": get_str(vdu
, "name", 255),
1008 "description": get_str(vdu
, "description", 255),
1009 "pdu_type": get_str(vdu
, "pdu-type", 255),
1012 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
1013 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
1014 if vdu
.get("count"):
1015 db_vm
["count"] = int(vdu
["count"])
1018 image_present
= False
1019 if vdu
.get("image"):
1020 image_present
= True
1022 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
1024 image_uuid
= db_image
["uuid"]
1025 db_images
.append(db_image
)
1026 db_vm
["image_id"] = image_uuid
1027 if vdu
.get("alternative-images"):
1028 vm_alternative_images
= []
1029 for alt_image
in vdu
.get("alternative-images").itervalues():
1031 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, alt_image
)
1033 image_uuid
= db_image
["uuid"]
1034 db_images
.append(db_image
)
1035 vm_alternative_images
.append({
1036 "image_id": image_uuid
,
1037 "vim_type": str(alt_image
["vim-type"]),
1038 # "universal_name": str(alt_image["image"]),
1039 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1042 db_vm
["image_list"] = yaml
.safe_dump(vm_alternative_images
, default_flow_style
=True, width
=256)
1046 if vdu
.get("volumes"):
1047 for volume_key
in vdu
["volumes"]:
1048 volume
= vdu
["volumes"][volume_key
]
1049 if not image_present
:
1050 # Convert the first volume to vnfc.image
1051 image_present
= True
1053 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
1055 image_uuid
= db_image
["uuid"]
1056 db_images
.append(db_image
)
1057 db_vm
["image_id"] = image_uuid
1059 # Add Openmano devices
1060 device
= {"name": str(volume
.get("name"))}
1061 device
["type"] = str(volume
.get("device-type"))
1062 if volume
.get("size"):
1063 device
["size"] = int(volume
["size"])
1064 if volume
.get("image"):
1065 device
["image name"] = str(volume
["image"])
1066 if volume
.get("image-checksum"):
1067 device
["image checksum"] = str(volume
["image-checksum"])
1069 devices
.append(device
)
1071 if not db_vm
.get("image_id"):
1072 if not db_vm
["pdu_type"]:
1073 raise NfvoException("Not defined image for VDU")
1074 # create a fake image
1078 if vdu
.get("cloud-init"):
1079 boot_data
["user-data"] = str(vdu
["cloud-init"])
1080 elif vdu
.get("cloud-init-file"):
1081 # TODO Where this file content is present???
1082 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1083 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
1085 if vdu
.get("supplemental-boot-data"):
1086 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
1087 boot_data
['boot-data-drive'] = True
1088 if vdu
["supplemental-boot-data"].get('config-file'):
1089 om_cfgfile_list
= list()
1090 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].itervalues():
1091 # TODO Where this file content is present???
1092 cfg_source
= str(custom_config_file
["source"])
1093 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
1094 "content": cfg_source
})
1095 boot_data
['config-files'] = om_cfgfile_list
1097 db_vm
["boot_data"] = yaml
.safe_dump(boot_data
, default_flow_style
=True, width
=256)
1099 db_vms
.append(db_vm
)
1102 # table interfaces (internal/external interfaces)
1103 flavor_epa_interfaces
= []
1104 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
1105 for iface
in vdu
.get("interface").itervalues():
1106 flavor_epa_interface
= {}
1107 iface_uuid
= str(uuid4())
1108 uuid_list
.append(iface_uuid
)
1111 "internal_name": get_str(iface
, "name", 255),
1114 flavor_epa_interface
["name"] = db_interface
["internal_name"]
1115 if iface
.get("virtual-interface").get("vpci"):
1116 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
1117 flavor_epa_interface
["vpci"] = db_interface
["vpci"]
1119 if iface
.get("virtual-interface").get("bandwidth"):
1120 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1121 db_interface
["bw"] = int(math
.ceil(bps
/1000000.0))
1122 flavor_epa_interface
["bandwidth"] = "{} Mbps".format(db_interface
["bw"])
1124 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1125 db_interface
["type"] = "mgmt"
1126 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1127 db_interface
["type"] = "bridge"
1128 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1129 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1130 db_interface
["type"] = "data"
1131 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1132 flavor_epa_interface
["dedicated"] = "no" if iface
["virtual-interface"]["type"] == "SR-IOV" \
1134 flavor_epa_interfaces
.append(flavor_epa_interface
)
1136 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1137 "-interface':'type':'{}'. Interface type is not supported".format(
1138 vnfd_id
, vdu_id
, iface
.get("virtual-interface").get("type")),
1139 httperrors
.Bad_Request
)
1141 if iface
.get("mgmt-interface"):
1142 db_interface
["type"] = "mgmt"
1144 if iface
.get("external-connection-point-ref"):
1146 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1147 db_interface
["external_name"] = get_str(cp
, "name", 255)
1148 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1149 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1150 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1151 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1152 if cp_descriptor
["name"] == db_interface
["external_name"]:
1157 if vdu_id
in vdu_id2cp_name
:
1158 vdu_id2cp_name
[vdu_id
] = None # more than two connecdtion point for this VDU
1160 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1163 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1164 db_interface
["port_security"] = 0
1165 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1166 db_interface
["port_security"] = 1
1168 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1169 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1170 " at connection-point".format(
1171 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1172 cp
=iface
.get("vnfd-connection-point-ref")),
1173 httperrors
.Bad_Request
)
1174 elif iface
.get("internal-connection-point-ref"):
1176 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1177 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1180 raise KeyError("does not exist at vdu:internal-connection-point")
1183 for vld
in vnfd
.get("internal-vld").itervalues():
1184 for cp
in vld
.get("internal-connection-point").itervalues():
1185 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1187 raise KeyError("is referenced by more than one 'internal-vld'")
1191 raise KeyError("is not referenced by any 'internal-vld'")
1193 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1194 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1195 db_interface
["port_security"] = 0
1196 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1197 db_interface
["port_security"] = 1
1198 if icp
.get("ip-address"):
1199 if not icp_vld
.get("ip-profile-ref"):
1201 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1202 except KeyError as e
:
1203 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1204 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1206 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1207 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1208 httperrors
.Bad_Request
)
1209 if iface
.get("position"):
1210 db_interface
["created_at"] = int(iface
.get("position")) * 50
1211 if iface
.get("mac-address"):
1212 db_interface
["mac"] = str(iface
.get("mac-address"))
1213 db_interfaces
.append(db_interface
)
1217 "name": get_str(vdu
, "name", 250) + "-flv",
1218 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1219 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1220 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1222 # TODO revise the case of several numa-node-policy node
1226 extended
["devices"] = devices
1227 if flavor_epa_interfaces
:
1228 numa
["interfaces"] = flavor_epa_interfaces
1229 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1230 epa_vcpu_set
= False
1231 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1232 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1233 if numa_node_policy
.get("node"):
1234 numa_node
= numa_node_policy
["node"].values()[0]
1235 if numa_node
.get("num-cores"):
1236 numa
["cores"] = numa_node
["num-cores"]
1238 if numa_node
.get("paired-threads"):
1239 if numa_node
["paired-threads"].get("num-paired-threads"):
1240 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1242 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1243 numa
["paired-threads-id"] = []
1244 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].itervalues():
1245 numa
["paired-threads-id"].append(
1246 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1248 if numa_node
.get("num-threads"):
1249 numa
["threads"] = int(numa_node
["num-threads"])
1251 if numa_node
.get("memory-mb"):
1252 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1253 if vdu
["guest-epa"].get("mempage-size"):
1254 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1255 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1256 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1257 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1258 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1259 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1260 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1262 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1264 extended
["numas"] = [numa
]
1266 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1267 db_flavor
["extended"] = extended_text
1268 # look if flavor exist
1269 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1270 'ram': db_flavor
.get('ram'),
1271 'vcpus': db_flavor
.get('vcpus'),
1272 'extended': db_flavor
.get('extended')
1274 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1275 if existing_flavors
:
1276 flavor_uuid
= existing_flavors
[0]["uuid"]
1278 flavor_uuid
= str(uuid4())
1279 uuid_list
.append(flavor_uuid
)
1280 db_flavor
["uuid"] = flavor_uuid
1281 db_flavors
.append(db_flavor
)
1282 db_vm
["flavor_id"] = flavor_uuid
1284 # VNF affinity and antiaffinity
1285 for pg
in vnfd
.get("placement-groups").itervalues():
1286 pg_name
= get_str(pg
, "name", 255)
1287 for vdu
in pg
.get("member-vdus").itervalues():
1288 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1289 if vdu_id
not in vdu_id2db_table_index
:
1290 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1291 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1292 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1293 httperrors
.Bad_Request
)
1294 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1295 # TODO consider the case of isolation and not colocation
1296 # if pg.get("strategy") == "ISOLATION":
1298 # VNF mgmt configuration
1300 if vnfd
["mgmt-interface"].get("vdu-id"):
1301 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1302 if mgmt_vdu_id
not in vdu_id2uuid
:
1303 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1304 "'{vdu}'. Reference to a non-existing vdu".format(
1305 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1306 httperrors
.Bad_Request
)
1307 mgmt_access
["vm_id"] = vdu_id2uuid
[vnfd
["mgmt-interface"]["vdu-id"]]
1308 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1309 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1310 if cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]:
1311 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1313 if vnfd
["mgmt-interface"].get("ip-address"):
1314 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1315 if vnfd
["mgmt-interface"].get("cp"):
1316 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1317 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1318 "Reference to a non-existing connection-point".format(
1319 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1320 httperrors
.Bad_Request
)
1321 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1322 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1323 # mark this interface as of type mgmt
1324 if cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]:
1325 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1327 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1331 mgmt_access
["default_user"] = default_user
1332 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1335 mgmt_access
["required"] = required
1338 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1340 db_vnfs
.append(db_vnf
)
1344 {"images": db_images
},
1345 {"flavors": db_flavors
},
1346 {"ip_profiles": db_ip_profiles
},
1348 {"interfaces": db_interfaces
},
1351 logger
.debug("create_vnf Deployment done vnfDict: %s",
1352 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1353 mydb
.new_rows(db_tables
, uuid_list
)
1354 return vnfd_uuid_list
1355 except NfvoException
:
1357 except Exception as e
:
1358 logger
.error("Exception {}".format(e
))
1359 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1362 @deprecated("Use new_vnfd_v3")
1363 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1364 global global_config
1366 # Step 1. Check the VNF descriptor
1367 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1368 # Step 2. Check tenant exist
1370 if tenant_id
!= "any":
1371 check_tenant(mydb
, tenant_id
)
1372 if "tenant_id" in vnf_descriptor
["vnf"]:
1373 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1374 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1375 httperrors
.Unauthorized
)
1377 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1378 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1379 if global_config
["auto_push_VNF_to_VIMs"]:
1380 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1382 # Step 4. Review the descriptor and add missing fields
1383 #print vnf_descriptor
1384 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1385 vnf_name
= vnf_descriptor
['vnf']['name']
1386 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1387 if "physical" in vnf_descriptor
['vnf']:
1388 del vnf_descriptor
['vnf']['physical']
1389 #print vnf_descriptor
1391 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1392 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1393 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1395 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1396 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1397 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1399 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1400 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1402 VNFCitem
["name"] = vnfc
['name']
1403 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1404 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1406 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1409 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1410 myflavorDict
["description"] = VNFCitem
["description"]
1411 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1412 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1413 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1414 myflavorDict
["extended"] = {}
1416 devices
= vnfc
.get("devices")
1418 myflavorDict
["extended"]["devices"] = devices
1421 # 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
1422 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1424 # Previous code has been commented
1425 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1426 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1427 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1428 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1430 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1432 # print "Error creating flavor: unknown processor model. Rollback successful."
1433 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1435 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1436 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1438 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1439 myflavorDict
['extended']['numas'] = vnfc
['numas']
1443 # Step 6.2 New flavors are created in the VIM
1444 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1446 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1447 VNFCitem
["flavor_id"] = flavor_id
1448 VNFCDict
[vnfc
['name']] = VNFCitem
1450 logger
.debug("Creating new images in the VIM for each VNFC")
1451 # Step 6.3 New images are created in the VIM
1452 #For each VNFC, we must create the appropriate image.
1453 #This "for" loop might be integrated with the previous one
1454 #In case this integration is made, the VNFCDict might become a VNFClist.
1455 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1456 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1458 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1459 image_dict
['universal_name']=vnfc
.get('image name')
1460 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1461 image_dict
['location']=vnfc
.get('VNFC image')
1462 #image_dict['new_location']=vnfc.get('image location')
1463 image_dict
['checksum']=vnfc
.get('image checksum')
1464 image_metadata_dict
= vnfc
.get('image metadata', None)
1465 image_metadata_str
= None
1466 if image_metadata_dict
is not None:
1467 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1468 image_dict
['metadata']=image_metadata_str
1469 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1470 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1471 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1472 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1473 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1474 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1475 if vnfc
.get("boot-data"):
1476 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1479 # Step 7. Storing the VNF descriptor in the repository
1480 if "descriptor" not in vnf_descriptor
["vnf"]:
1481 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1483 # Step 8. Adding the VNF to the NFVO DB
1484 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1486 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1487 _
, message
= rollback(mydb
, vims
, rollback_list
)
1488 if isinstance(e
, db_base_Exception
):
1489 error_text
= "Exception at database"
1490 elif isinstance(e
, KeyError):
1491 error_text
= "KeyError exception "
1492 e
.http_code
= httperrors
.Internal_Server_Error
1494 error_text
= "Exception at VIM"
1495 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1496 #logger.error("start_scenario %s", error_text)
1497 raise NfvoException(error_text
, e
.http_code
)
1500 @deprecated("Use new_vnfd_v3")
1501 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1502 global global_config
1504 # Step 1. Check the VNF descriptor
1505 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1506 # Step 2. Check tenant exist
1508 if tenant_id
!= "any":
1509 check_tenant(mydb
, tenant_id
)
1510 if "tenant_id" in vnf_descriptor
["vnf"]:
1511 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1512 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1513 httperrors
.Unauthorized
)
1515 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1516 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1517 if global_config
["auto_push_VNF_to_VIMs"]:
1518 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1520 # Step 4. Review the descriptor and add missing fields
1521 #print vnf_descriptor
1522 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1523 vnf_name
= vnf_descriptor
['vnf']['name']
1524 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1525 if "physical" in vnf_descriptor
['vnf']:
1526 del vnf_descriptor
['vnf']['physical']
1527 #print vnf_descriptor
1529 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1530 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1531 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1533 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1534 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1535 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1537 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1538 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1540 VNFCitem
["name"] = vnfc
['name']
1541 VNFCitem
["description"] = vnfc
.get("description", 'VM %s of the VNF %s' %(vnfc
['name'],vnf_name
))
1543 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1546 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1547 myflavorDict
["description"] = VNFCitem
["description"]
1548 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1549 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1550 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1551 myflavorDict
["extended"] = {}
1553 devices
= vnfc
.get("devices")
1555 myflavorDict
["extended"]["devices"] = devices
1558 # 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
1559 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1561 # Previous code has been commented
1562 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1563 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1564 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1565 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1567 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1569 # print "Error creating flavor: unknown processor model. Rollback successful."
1570 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1572 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1573 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1575 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1576 myflavorDict
['extended']['numas'] = vnfc
['numas']
1580 # Step 6.2 New flavors are created in the VIM
1581 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1583 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1584 VNFCitem
["flavor_id"] = flavor_id
1585 VNFCDict
[vnfc
['name']] = VNFCitem
1587 logger
.debug("Creating new images in the VIM for each VNFC")
1588 # Step 6.3 New images are created in the VIM
1589 #For each VNFC, we must create the appropriate image.
1590 #This "for" loop might be integrated with the previous one
1591 #In case this integration is made, the VNFCDict might become a VNFClist.
1592 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1593 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1595 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1596 image_dict
['universal_name']=vnfc
.get('image name')
1597 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1598 image_dict
['location']=vnfc
.get('VNFC image')
1599 #image_dict['new_location']=vnfc.get('image location')
1600 image_dict
['checksum']=vnfc
.get('image checksum')
1601 image_metadata_dict
= vnfc
.get('image metadata', None)
1602 image_metadata_str
= None
1603 if image_metadata_dict
is not None:
1604 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1605 image_dict
['metadata']=image_metadata_str
1606 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1607 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1608 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1609 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1610 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1611 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1612 if vnfc
.get("boot-data"):
1613 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1615 # Step 7. Storing the VNF descriptor in the repository
1616 if "descriptor" not in vnf_descriptor
["vnf"]:
1617 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1619 # Step 8. Adding the VNF to the NFVO DB
1620 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1622 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1623 _
, message
= rollback(mydb
, vims
, rollback_list
)
1624 if isinstance(e
, db_base_Exception
):
1625 error_text
= "Exception at database"
1626 elif isinstance(e
, KeyError):
1627 error_text
= "KeyError exception "
1628 e
.http_code
= httperrors
.Internal_Server_Error
1630 error_text
= "Exception at VIM"
1631 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1632 #logger.error("start_scenario %s", error_text)
1633 raise NfvoException(error_text
, e
.http_code
)
1636 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1637 #check valid tenant_id
1638 check_tenant(mydb
, tenant_id
)
1641 if tenant_id
!= "any":
1642 where_or
["tenant_id"] = tenant_id
1643 where_or
["public"] = True
1644 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1646 vnf_id
= vnf
["uuid"]
1647 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1648 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.iteritems() if k
in filter_keys
)
1649 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1650 data
={'vnf' : filtered_content
}
1652 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1653 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1655 WHERE
={'vnfs.uuid': vnf_id
} )
1656 if len(content
) != 0:
1657 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1658 # change boot_data into boot-data
1660 if vm
.get("boot_data"):
1661 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1664 data
['vnf']['VNFC'] = content
1665 #TODO: GET all the information from a VNFC and include it in the output.
1668 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1669 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1670 WHERE
={'vnfs.uuid': vnf_id
} )
1671 data
['vnf']['nets'] = content
1673 #GET ip-profile for each net
1674 for net
in data
['vnf']['nets']:
1675 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1676 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1677 WHERE
={'net_id': net
["uuid"]} )
1678 if len(ipprofiles
)==1:
1679 net
["ip_profile"] = ipprofiles
[0]
1680 elif len(ipprofiles
)>1:
1681 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
1684 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1686 #GET External Interfaces
1687 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1688 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1689 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1690 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1692 data
['vnf']['external-connections'] = content
1697 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1698 # Check tenant exist
1699 if tenant_id
!= "any":
1700 check_tenant(mydb
, tenant_id
)
1701 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1702 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1706 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1708 if tenant_id
!= "any":
1709 where_or
["tenant_id"] = tenant_id
1710 where_or
["public"] = True
1711 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1712 vnf_id
= vnf
["uuid"]
1714 # "Getting the list of flavors and tenants of the VNF"
1715 flavorList
= get_flavorlist(mydb
, vnf_id
)
1716 if len(flavorList
)==0:
1717 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1719 imageList
= get_imagelist(mydb
, vnf_id
)
1720 if len(imageList
)==0:
1721 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1723 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1725 raise NfvoException("vnf '{}' not found".format(vnf_id
), httperrors
.Not_Found
)
1728 for flavor
in flavorList
:
1729 #check if flavor is used by other vnf
1731 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1733 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1735 #flavor not used, must be deleted
1737 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1738 for flavor_vim
in c
:
1739 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1743 for vim
in vims
.values():
1744 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1750 myvim
.delete_flavor(flavor_vim
["vim_id"])
1751 except vimconn
.vimconnNotFoundException
:
1752 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1753 flavor_vim
["datacenter_vim_id"] )
1754 except vimconn
.vimconnException
as e
:
1755 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1756 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1757 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1758 flavor_vim
["datacenter_vim_id"]))
1759 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1760 mydb
.delete_row_by_id('flavors', flavor
)
1761 except db_base_Exception
as e
:
1762 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1763 undeletedItems
.append("flavor {}".format(flavor
))
1766 for image
in imageList
:
1768 #check if image is used by other vnf
1769 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1771 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1773 #image not used, must be deleted
1775 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1777 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1779 if image_vim
['created']=='false': #skip this image because not created by openmano
1781 myvim
=vims
[ image_vim
["datacenter_id"] ]
1783 myvim
.delete_image(image_vim
["vim_id"])
1784 except vimconn
.vimconnNotFoundException
as e
:
1785 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1786 except vimconn
.vimconnException
as e
:
1787 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1788 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1789 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1790 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1791 mydb
.delete_row_by_id('images', image
)
1792 except db_base_Exception
as e
:
1793 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1794 undeletedItems
.append("image %s" % image
)
1796 return vnf_id
+ " " + vnf
["name"]
1798 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1801 @deprecated("Not used")
1802 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1803 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1807 return -httperrors
.Not_Found
, "datacenter '%s' not found" % datacenter_name
1808 myvim
= vims
.values()[0]
1809 result
,servers
= myvim
.get_hosts_info()
1811 return result
, servers
1812 topology
= {'name':myvim
['name'] , 'servers': servers
}
1813 return result
, topology
1816 def get_hosts(mydb
, nfvo_tenant_id
):
1817 vims
= get_vim(mydb
, nfvo_tenant_id
)
1819 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), httperrors
.Not_Found
)
1821 #print "nfvo.datacenter_action() error. Several datacenters found"
1822 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
1823 myvim
= vims
.values()[0]
1825 hosts
= myvim
.get_hosts()
1826 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1828 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1830 server
={'name':host
['name'], 'vms':[]}
1831 for vm
in host
['instances']:
1832 #get internal name and model
1834 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1835 WHERE
={'vim_vm_id':vm
['id']} )
1837 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1839 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1841 except db_base_Exception
as e
:
1842 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1843 datacenter
['Datacenters'][0]['servers'].append(server
)
1844 #return -400, "en construccion"
1846 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1848 except vimconn
.vimconnException
as e
:
1849 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1852 @deprecated("Use new_nsd_v3")
1853 def new_scenario(mydb
, tenant_id
, topo
):
1855 # result, vims = get_vim(mydb, tenant_id)
1857 # return result, vims
1859 if tenant_id
!= "any":
1860 check_tenant(mydb
, tenant_id
)
1861 if "tenant_id" in topo
:
1862 if topo
["tenant_id"] != tenant_id
:
1863 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1864 httperrors
.Unauthorized
)
1868 #1.1: get VNFs and external_networks (other_nets).
1870 other_nets
={} #external_networks, bridge_networks and data_networkds
1871 nodes
= topo
['topology']['nodes']
1872 for k
in nodes
.keys():
1873 if nodes
[k
]['type'] == 'VNF':
1875 vnfs
[k
]['ifaces'] = {}
1876 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1877 other_nets
[k
] = nodes
[k
]
1878 other_nets
[k
]['external']=True
1879 elif nodes
[k
]['type'] == 'network':
1880 other_nets
[k
] = nodes
[k
]
1881 other_nets
[k
]['external']=False
1884 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1885 for name
,vnf
in vnfs
.items():
1886 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1888 error_pos
= "'topology':'nodes':'" + name
+ "'"
1890 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1891 where
['uuid'] = vnf
['vnf_id']
1892 if 'VNF model' in vnf
:
1893 error_text
+= " 'VNF model' " + vnf
['VNF model']
1894 where
['name'] = vnf
['VNF model']
1896 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, httperrors
.Bad_Request
)
1898 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1902 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
1904 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
1905 vnf
['uuid']=vnf_db
[0]['uuid']
1906 vnf
['description']=vnf_db
[0]['description']
1907 #get external interfaces
1908 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1909 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1910 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1911 for ext_iface
in ext_ifaces
:
1912 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1914 #1.4 get list of connections
1915 conections
= topo
['topology']['connections']
1916 conections_list
= []
1917 conections_list_name
= []
1918 for k
in conections
.keys():
1919 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1920 ifaces_list
= conections
[k
]['nodes'].items()
1921 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1923 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1924 for k2
in conection_pair_list
:
1927 con_type
= conections
[k
].get("type", "link")
1928 if con_type
!= "link":
1930 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), httperrors
.Bad_Request
)
1931 other_nets
[k
] = {'external': False}
1932 if conections
[k
].get("graph"):
1933 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1934 ifaces_list
.append( (k
, None) )
1937 if con_type
== "external_network":
1938 other_nets
[k
]['external'] = True
1939 if conections
[k
].get("model"):
1940 other_nets
[k
]["model"] = conections
[k
]["model"]
1942 other_nets
[k
]["model"] = k
1943 if con_type
== "dataplane_net" or con_type
== "bridge_net":
1944 other_nets
[k
]["model"] = con_type
1946 conections_list_name
.append(k
)
1947 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)
1948 #print set(ifaces_list)
1949 #check valid VNF and iface names
1950 for iface
in ifaces_list
:
1951 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
1952 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
1953 str(k
), iface
[0]), httperrors
.Not_Found
)
1954 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
1955 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
1956 str(k
), iface
[0], iface
[1]), httperrors
.Not_Found
)
1958 #1.5 unify connections from the pair list to a consolidated list
1960 while index
< len(conections_list
):
1962 while index2
< len(conections_list
):
1963 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
1964 conections_list
[index
] |
= conections_list
[index2
]
1965 del conections_list
[index2
]
1966 del conections_list_name
[index2
]
1969 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
1971 #for k in conections_list:
1976 #1.6 Delete non external nets
1977 # for k in other_nets.keys():
1978 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
1979 # for con in conections_list:
1981 # for index in range(0,len(con)):
1982 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
1983 # for index in delete_indexes:
1986 #1.7: Check external_ports are present at database table datacenter_nets
1987 for k
,net
in other_nets
.items():
1988 error_pos
= "'topology':'nodes':'" + k
+ "'"
1989 if net
['external']==False:
1990 if 'name' not in net
:
1992 if 'model' not in net
:
1993 raise NfvoException("needed a 'model' at " + error_pos
, httperrors
.Bad_Request
)
1994 if net
['model']=='bridge_net':
1995 net
['type']='bridge';
1996 elif net
['model']=='dataplane_net':
1999 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, httperrors
.Not_Found
)
2001 #IF we do not want to check that external network exist at datacenter
2006 # if 'net_id' in net:
2007 # error_text += " 'net_id' " + net['net_id']
2008 # WHERE_['uuid'] = net['net_id']
2009 # if 'model' in net:
2010 # error_text += " 'model' " + net['model']
2011 # WHERE_['name'] = net['model']
2012 # if len(WHERE_) == 0:
2013 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2014 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2015 # FROM='datacenter_nets', WHERE=WHERE_ )
2017 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2019 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2020 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2022 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2023 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2024 # other_nets[k].update(net_db[0])
2027 net_nb
=0 #Number of nets
2028 for con
in conections_list
:
2029 #check if this is connected to a external net
2033 for index
in range(0,len(con
)):
2034 #check if this is connected to a external net
2035 for net_key
in other_nets
.keys():
2036 if con
[index
][0]==net_key
:
2037 if other_net_index
>=0:
2038 error_text
="There is some interface connected both to net '%s' and net '%s'" % (con
[other_net_index
][0], net_key
)
2039 #print "nfvo.new_scenario " + error_text
2040 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2042 other_net_index
= index
2043 net_target
= net_key
2045 #print "other_net_index", other_net_index
2047 if other_net_index
>=0:
2048 del con
[other_net_index
]
2049 #IF we do not want to check that external network exist at datacenter
2050 if other_nets
[net_target
]['external'] :
2051 if "name" not in other_nets
[net_target
]:
2052 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
2053 if other_nets
[net_target
]["type"] == "external_network":
2054 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
2055 other_nets
[net_target
]["type"] = "data"
2057 other_nets
[net_target
]["type"] = "bridge"
2059 # if other_nets[net_target]['external'] :
2060 # 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
2061 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2062 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2063 # print "nfvo.new_scenario " + error_text
2064 # return -httperrors.Bad_Request, error_text
2067 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2070 net_type_bridge
=False
2072 net_target
= "__-__net"+str(net_nb
)
2073 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2074 'description':"net-%s in scenario %s" %(net_nb
,topo
['name']),
2077 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2078 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2079 if iface_type
=='mgmt' or iface_type
=='bridge':
2080 net_type_bridge
= True
2082 net_type_data
= True
2083 if net_type_bridge
and net_type_data
:
2084 error_text
= "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface
[0], iface
[1])
2085 #print "nfvo.new_scenario " + error_text
2086 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2087 elif net_type_bridge
:
2090 type_
='data' if len(con
)>2 else 'ptp'
2091 net_list
[net_target
]['type'] = type_
2094 error_text
= "Error connection node %s : %s does not match any VNF or interface" % (iface
[0], iface
[1])
2095 #print "nfvo.new_scenario " + error_text
2097 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2099 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2100 #1.8.1 obtain management net
2101 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2102 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2103 #1.8.2 check all interfaces from all vnfs
2105 add_mgmt_net
= False
2106 for vnf
in vnfs
.values():
2107 for iface
in vnf
['ifaces'].values():
2108 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2109 #iface not connected
2110 iface
['net_key'] = 'mgmt'
2112 if add_mgmt_net
and 'mgmt' not in net_list
:
2113 net_list
['mgmt']=mgmt_net
[0]
2114 net_list
['mgmt']['external']=True
2115 net_list
['mgmt']['graph']={'visible':False}
2117 net_list
.update(other_nets
)
2119 #print 'net_list', net_list
2124 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2125 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2126 'tenant_id':tenant_id
, 'name':topo
['name'],
2127 'description':topo
.get('description',topo
['name']),
2128 'public': topo
.get('public', False)
2134 @deprecated("Use new_nsd_v3")
2135 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2136 """ This creates a new scenario for version 0.2 and 0.3"""
2137 scenario
= scenario_dict
["scenario"]
2138 if tenant_id
!= "any":
2139 check_tenant(mydb
, tenant_id
)
2140 if "tenant_id" in scenario
:
2141 if scenario
["tenant_id"] != tenant_id
:
2142 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2143 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2144 scenario
["tenant_id"], tenant_id
), httperrors
.Unauthorized
)
2148 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2149 for name
,vnf
in scenario
["vnfs"].iteritems():
2150 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2152 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2154 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2155 where
['uuid'] = vnf
['vnf_id']
2156 if 'vnf_name' in vnf
:
2157 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2158 where
['name'] = vnf
['vnf_name']
2160 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, httperrors
.Bad_Request
)
2161 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2164 if len(vnf_db
) == 0:
2165 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
2166 elif len(vnf_db
) > 1:
2167 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
2168 vnf
['uuid'] = vnf_db
[0]['uuid']
2169 vnf
['description'] = vnf_db
[0]['description']
2171 # get external interfaces
2172 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2173 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2174 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2175 for ext_iface
in ext_ifaces
:
2176 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2177 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2179 # 2: Insert net_key and ip_address at every vnf interface
2180 for net_name
, net
in scenario
["networks"].items():
2181 net_type_bridge
= False
2182 net_type_data
= False
2183 for iface_dict
in net
["interfaces"]:
2184 if version
== "0.2":
2185 temp_dict
= iface_dict
2187 elif version
== "0.3":
2188 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2189 ip_address
= iface_dict
.get('ip_address', None)
2190 for vnf
, iface
in temp_dict
.items():
2191 if vnf
not in scenario
["vnfs"]:
2192 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2194 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2195 raise NfvoException(error_text
, httperrors
.Not_Found
)
2196 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2197 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2198 .format(net_name
, iface
)
2199 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2200 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2201 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2202 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2203 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2204 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2205 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2206 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2207 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2208 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2209 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2210 net_type_bridge
= True
2212 net_type_data
= True
2214 if net_type_bridge
and net_type_data
:
2215 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2217 # logger.debug("nfvo.new_scenario " + error_text)
2218 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2219 elif net_type_bridge
:
2222 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2224 if net
.get("implementation"): # for v0.3
2225 if type_
== "bridge" and net
["implementation"] == "underlay":
2226 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2227 "'network':'{}'".format(net_name
)
2228 # logger.debug(error_text)
2229 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2230 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2231 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2232 "'network':'{}'".format(net_name
)
2233 # logger.debug(error_text)
2234 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2235 net
.pop("implementation")
2236 if "type" in net
and version
== "0.3": # for v0.3
2237 if type_
== "data" and net
["type"] == "e-line":
2238 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2239 "'e-line' at 'network':'{}'".format(net_name
)
2240 # logger.debug(error_text)
2241 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2242 elif type_
== "ptp" and net
["type"] == "e-lan":
2246 net
['name'] = net_name
2247 net
['external'] = net
.get('external', False)
2249 # 3: insert at database
2250 scenario
["nets"] = scenario
["networks"]
2251 scenario
['tenant_id'] = tenant_id
2252 scenario_id
= mydb
.new_scenario(scenario
)
2256 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2258 Parses an OSM IM nsd_catalog and insert at DB
2261 :param nsd_descriptor:
2262 :return: The list of created NSD ids
2265 mynsd
= nsd_catalog
.nsd()
2267 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
)
2268 except Exception as e
:
2269 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), httperrors
.Bad_Request
)
2273 db_sce_interfaces
= []
2276 db_sce_rsp_hops
= []
2277 db_sce_classifiers
= []
2278 db_sce_classifier_matches
= []
2280 db_ip_profiles_index
= 0
2283 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.itervalues():
2284 nsd
= nsd_yang
.get()
2287 scenario_uuid
= str(uuid4())
2288 uuid_list
.append(scenario_uuid
)
2289 nsd_uuid_list
.append(scenario_uuid
)
2291 "uuid": scenario_uuid
,
2292 "osm_id": get_str(nsd
, "id", 255),
2293 "name": get_str(nsd
, "name", 255),
2294 "description": get_str(nsd
, "description", 255),
2295 "tenant_id": tenant_id
,
2296 "vendor": get_str(nsd
, "vendor", 255),
2297 "short_name": get_str(nsd
, "short-name", 255),
2298 "descriptor": str(nsd_descriptor
)[:60000],
2300 db_scenarios
.append(db_scenario
)
2302 # table sce_vnfs (constituent-vnfd)
2303 vnf_index2scevnf_uuid
= {}
2304 vnf_index2vnf_uuid
= {}
2305 for vnf
in nsd
.get("constituent-vnfd").itervalues():
2306 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2307 'tenant_id': tenant_id
})
2308 if not existing_vnf
:
2309 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2310 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2311 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2312 httperrors
.Bad_Request
)
2313 sce_vnf_uuid
= str(uuid4())
2314 uuid_list
.append(sce_vnf_uuid
)
2316 "uuid": sce_vnf_uuid
,
2317 "scenario_id": scenario_uuid
,
2318 # "name": get_str(vnf, "member-vnf-index", 255),
2319 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2320 "vnf_id": existing_vnf
[0]["uuid"],
2321 "member_vnf_index": str(vnf
["member-vnf-index"]),
2322 # TODO 'start-by-default': True
2324 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2325 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2326 db_sce_vnfs
.append(db_sce_vnf
)
2328 # table ip_profiles (ip-profiles)
2329 ip_profile_name2db_table_index
= {}
2330 for ip_profile
in nsd
.get("ip-profiles").itervalues():
2332 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2333 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2334 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2335 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2336 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2337 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2340 for dns
in ip_profile
["ip-profile-params"]["dns-server"].itervalues():
2341 dns_list
.append(str(dns
.get("address")))
2342 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2343 if ip_profile
["ip-profile-params"].get('security-group'):
2344 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2345 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2346 db_ip_profiles_index
+= 1
2347 db_ip_profiles
.append(db_ip_profile
)
2349 # table sce_nets (internal-vld)
2350 for vld
in nsd
.get("vld").itervalues():
2351 sce_net_uuid
= str(uuid4())
2352 uuid_list
.append(sce_net_uuid
)
2354 "uuid": sce_net_uuid
,
2355 "name": get_str(vld
, "name", 255),
2356 "scenario_id": scenario_uuid
,
2358 "multipoint": not vld
.get("type") == "ELINE",
2359 "osm_id": get_str(vld
, "id", 255),
2361 "description": get_str(vld
, "description", 255),
2363 # guess type of network
2364 if vld
.get("mgmt-network"):
2365 db_sce_net
["type"] = "bridge"
2366 db_sce_net
["external"] = True
2367 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2368 db_sce_net
["type"] = "data"
2370 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2371 db_sce_net
["type"] = None
2372 db_sce_nets
.append(db_sce_net
)
2374 # ip-profile, link db_ip_profile with db_sce_net
2375 if vld
.get("ip-profile-ref"):
2376 ip_profile_name
= vld
.get("ip-profile-ref")
2377 if ip_profile_name
not in ip_profile_name2db_table_index
:
2378 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2379 " Reference to a non-existing 'ip_profiles'".format(
2380 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2381 httperrors
.Bad_Request
)
2382 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2383 elif vld
.get("vim-network-name"):
2384 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2386 # table sce_interfaces (vld:vnfd-connection-point-ref)
2387 for iface
in vld
.get("vnfd-connection-point-ref").itervalues():
2388 vnf_index
= str(iface
['member-vnf-index-ref'])
2389 # check correct parameters
2390 if vnf_index
not in vnf_index2vnf_uuid
:
2391 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2392 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2393 "'nsd':'constituent-vnfd'".format(
2394 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2395 httperrors
.Bad_Request
)
2397 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2398 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2399 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2400 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2402 if not existing_ifaces
:
2403 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2404 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2405 "connection-point name at VNFD '{}'".format(
2406 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2407 str(iface
.get("vnfd-id-ref"))[:255]),
2408 httperrors
.Bad_Request
)
2409 interface_uuid
= existing_ifaces
[0]["uuid"]
2410 if existing_ifaces
[0]["iface_type"] == "data":
2411 db_sce_net
["type"] = "data"
2412 sce_interface_uuid
= str(uuid4())
2413 uuid_list
.append(sce_net_uuid
)
2414 iface_ip_address
= None
2415 if iface
.get("ip-address"):
2416 iface_ip_address
= str(iface
.get("ip-address"))
2417 db_sce_interface
= {
2418 "uuid": sce_interface_uuid
,
2419 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2420 "sce_net_id": sce_net_uuid
,
2421 "interface_id": interface_uuid
,
2422 "ip_address": iface_ip_address
,
2424 db_sce_interfaces
.append(db_sce_interface
)
2425 if not db_sce_net
["type"]:
2426 db_sce_net
["type"] = "bridge"
2428 # table sce_vnffgs (vnffgd)
2429 for vnffg
in nsd
.get("vnffgd").itervalues():
2430 sce_vnffg_uuid
= str(uuid4())
2431 uuid_list
.append(sce_vnffg_uuid
)
2433 "uuid": sce_vnffg_uuid
,
2434 "name": get_str(vnffg
, "name", 255),
2435 "scenario_id": scenario_uuid
,
2436 "vendor": get_str(vnffg
, "vendor", 255),
2437 "description": get_str(vld
, "description", 255),
2439 db_sce_vnffgs
.append(db_sce_vnffg
)
2442 for rsp
in vnffg
.get("rsp").itervalues():
2443 sce_rsp_uuid
= str(uuid4())
2444 uuid_list
.append(sce_rsp_uuid
)
2446 "uuid": sce_rsp_uuid
,
2447 "name": get_str(rsp
, "name", 255),
2448 "sce_vnffg_id": sce_vnffg_uuid
,
2449 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2451 db_sce_rsps
.append(db_sce_rsp
)
2452 for iface
in rsp
.get("vnfd-connection-point-ref").itervalues():
2453 vnf_index
= str(iface
['member-vnf-index-ref'])
2454 if_order
= int(iface
['order'])
2455 # check correct parameters
2456 if vnf_index
not in vnf_index2vnf_uuid
:
2457 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2458 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2459 "'nsd':'constituent-vnfd'".format(
2460 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2461 httperrors
.Bad_Request
)
2463 ingress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2464 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2466 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2467 'external_name': get_str(iface
, "vnfd-ingress-connection-point-ref",
2469 if not ingress_existing_ifaces
:
2470 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2471 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2472 "connection-point name at VNFD '{}'".format(
2473 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-ingress-connection-point-ref"]),
2474 str(iface
.get("vnfd-id-ref"))[:255]), httperrors
.Bad_Request
)
2476 egress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2477 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2479 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2480 'external_name': get_str(iface
, "vnfd-egress-connection-point-ref",
2482 if not egress_existing_ifaces
:
2483 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2484 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2485 "connection-point name at VNFD '{}'".format(
2486 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-egress-connection-point-ref"]),
2487 str(iface
.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request
)
2489 ingress_interface_uuid
= ingress_existing_ifaces
[0]["uuid"]
2490 egress_interface_uuid
= egress_existing_ifaces
[0]["uuid"]
2491 sce_rsp_hop_uuid
= str(uuid4())
2492 uuid_list
.append(sce_rsp_hop_uuid
)
2494 "uuid": sce_rsp_hop_uuid
,
2495 "if_order": if_order
,
2496 "ingress_interface_id": ingress_interface_uuid
,
2497 "egress_interface_id": egress_interface_uuid
,
2498 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2499 "sce_rsp_id": sce_rsp_uuid
,
2501 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2503 # deal with classifiers
2504 for classifier
in vnffg
.get("classifier").itervalues():
2505 sce_classifier_uuid
= str(uuid4())
2506 uuid_list
.append(sce_classifier_uuid
)
2509 vnf_index
= str(classifier
['member-vnf-index-ref'])
2510 if vnf_index
not in vnf_index2vnf_uuid
:
2511 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2512 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2513 "'nsd':'constituent-vnfd'".format(
2514 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2515 httperrors
.Bad_Request
)
2516 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2517 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2518 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2519 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2521 if not existing_ifaces
:
2522 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2523 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2524 "connection-point name at VNFD '{}'".format(
2525 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2526 str(iface
.get("vnfd-id-ref"))[:255]),
2527 httperrors
.Bad_Request
)
2528 interface_uuid
= existing_ifaces
[0]["uuid"]
2530 db_sce_classifier
= {
2531 "uuid": sce_classifier_uuid
,
2532 "name": get_str(classifier
, "name", 255),
2533 "sce_vnffg_id": sce_vnffg_uuid
,
2534 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2535 "interface_id": interface_uuid
,
2537 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2538 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2539 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2540 db_sce_classifiers
.append(db_sce_classifier
)
2542 for match
in classifier
.get("match-attributes").itervalues():
2543 sce_classifier_match_uuid
= str(uuid4())
2544 uuid_list
.append(sce_classifier_match_uuid
)
2545 db_sce_classifier_match
= {
2546 "uuid": sce_classifier_match_uuid
,
2547 "ip_proto": get_str(match
, "ip-proto", 2),
2548 "source_ip": get_str(match
, "source-ip-address", 16),
2549 "destination_ip": get_str(match
, "destination-ip-address", 16),
2550 "source_port": get_str(match
, "source-port", 5),
2551 "destination_port": get_str(match
, "destination-port", 5),
2552 "sce_classifier_id": sce_classifier_uuid
,
2554 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2557 # remove unneeded id's in sce_rsps
2558 for rsp
in db_sce_rsps
:
2562 {"scenarios": db_scenarios
},
2563 {"sce_nets": db_sce_nets
},
2564 {"ip_profiles": db_ip_profiles
},
2565 {"sce_vnfs": db_sce_vnfs
},
2566 {"sce_interfaces": db_sce_interfaces
},
2567 {"sce_vnffgs": db_sce_vnffgs
},
2568 {"sce_rsps": db_sce_rsps
},
2569 {"sce_rsp_hops": db_sce_rsp_hops
},
2570 {"sce_classifiers": db_sce_classifiers
},
2571 {"sce_classifier_matches": db_sce_classifier_matches
},
2574 logger
.debug("new_nsd_v3 done: %s",
2575 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2576 mydb
.new_rows(db_tables
, uuid_list
)
2577 return nsd_uuid_list
2578 except NfvoException
:
2580 except Exception as e
:
2581 logger
.error("Exception {}".format(e
))
2582 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2585 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2586 data
["uuid"] = scenario_id
2587 data
["tenant_id"] = tenant_id
2588 c
= mydb
.edit_scenario( data
)
2592 @deprecated("Use create_instance")
2593 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2594 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2595 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2596 vims
= {datacenter_id
: myvim
}
2597 myvim_tenant
= myvim
['tenant_id']
2598 datacenter_name
= myvim
['name']
2602 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2603 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2604 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2605 scenarioDict
['datacenter_id'] = datacenter_id
2606 #print '================scenarioDict======================='
2607 #print json.dumps(scenarioDict, indent=4)
2608 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2610 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2611 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2613 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2614 auxNetDict
['scenario'] = {}
2616 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2617 for sce_net
in scenarioDict
['nets']:
2618 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2620 myNetName
= "%s.%s" % (instance_scenario_name
, sce_net
['name'])
2621 myNetName
= myNetName
[0:255] #limit length
2622 myNetType
= sce_net
['type']
2624 myNetDict
["name"] = myNetName
2625 myNetDict
["type"] = myNetType
2626 myNetDict
["tenant_id"] = myvim_tenant
2627 myNetIPProfile
= sce_net
.get('ip_profile', None)
2629 #We should use the dictionary as input parameter for new_network
2631 if not sce_net
["external"]:
2632 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2633 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2634 sce_net
['vim_id'] = network_id
2635 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2636 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2637 sce_net
["created"] = True
2639 if sce_net
['vim_id'] == None:
2640 error_text
= "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name
, sce_net
['name'])
2641 _
, message
= rollback(mydb
, vims
, rollbackList
)
2642 logger
.error("nfvo.start_scenario: %s", error_text
)
2643 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2644 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2645 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2647 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2648 #For each vnf net, we create it and we add it to instanceNetlist.
2650 for sce_vnf
in scenarioDict
['vnfs']:
2651 for net
in sce_vnf
['nets']:
2652 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2654 myNetName
= "%s.%s" % (instance_scenario_name
,net
['name'])
2655 myNetName
= myNetName
[0:255] #limit length
2656 myNetType
= net
['type']
2658 myNetDict
["name"] = myNetName
2659 myNetDict
["type"] = myNetType
2660 myNetDict
["tenant_id"] = myvim_tenant
2661 myNetIPProfile
= net
.get('ip_profile', None)
2664 #We should use the dictionary as input parameter for new_network
2665 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
)
2666 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2667 net
['vim_id'] = network_id
2668 if sce_vnf
['uuid'] not in auxNetDict
:
2669 auxNetDict
[sce_vnf
['uuid']] = {}
2670 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2671 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2672 net
["created"] = True
2674 #print "auxNetDict:"
2675 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2677 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2678 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2680 for sce_vnf
in scenarioDict
['vnfs']:
2681 vnf_availability_zones
= []
2682 for vm
in sce_vnf
['vms']:
2683 vm_av
= vm
.get('availability_zone')
2684 if vm_av
and vm_av
not in vnf_availability_zones
:
2685 vnf_availability_zones
.append(vm_av
)
2687 # check if there is enough availability zones available at vim level.
2688 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2689 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2690 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
2692 for vm
in sce_vnf
['vms']:
2695 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2696 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2697 #myVMDict['description'] = vm['description']
2698 myVMDict
['description'] = myVMDict
['name'][0:99]
2700 myVMDict
['start'] = "no"
2701 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2702 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2704 #create image at vim in case it not exist
2705 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2706 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2707 vm
['vim_image_id'] = image_id
2709 #create flavor at vim in case it not exist
2710 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2711 if flavor_dict
['extended']!=None:
2712 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'])
2713 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2714 vm
['vim_flavor_id'] = flavor_id
2717 myVMDict
['imageRef'] = vm
['vim_image_id']
2718 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2719 myVMDict
['networks'] = []
2720 for iface
in vm
['interfaces']:
2722 if iface
['type']=="data":
2723 netDict
['type'] = iface
['model']
2724 elif "model" in iface
and iface
["model"]!=None:
2725 netDict
['model']=iface
['model']
2726 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2727 #discover type of interface looking at flavor
2728 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2729 for flavor_iface
in numa
.get('interfaces',[]):
2730 if flavor_iface
.get('name') == iface
['internal_name']:
2731 if flavor_iface
['dedicated'] == 'yes':
2732 netDict
['type']="PF" #passthrough
2733 elif flavor_iface
['dedicated'] == 'no':
2734 netDict
['type']="VF" #siov
2735 elif flavor_iface
['dedicated'] == 'yes:sriov':
2736 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2737 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2739 netDict
["use"]=iface
['type']
2740 if netDict
["use"]=="data" and not netDict
.get("type"):
2741 #print "netDict", netDict
2742 #print "iface", iface
2743 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'])
2744 if flavor_dict
.get('extended')==None:
2745 raise NfvoException(e_text
+ "After database migration some information is not available. \
2746 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
2748 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
2749 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2750 netDict
["type"]="virtual"
2751 if "vpci" in iface
and iface
["vpci"] is not None:
2752 netDict
['vpci'] = iface
['vpci']
2753 if "mac" in iface
and iface
["mac"] is not None:
2754 netDict
['mac_address'] = iface
['mac']
2755 if "port-security" in iface
and iface
["port-security"] is not None:
2756 netDict
['port_security'] = iface
['port-security']
2757 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2758 netDict
['floating_ip'] = iface
['floating-ip']
2759 netDict
['name'] = iface
['internal_name']
2760 if iface
['net_id'] is None:
2761 for vnf_iface
in sce_vnf
["interfaces"]:
2764 if vnf_iface
['interface_id']==iface
['uuid']:
2765 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2768 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2769 #skip bridge ifaces not connected to any net
2770 #if 'net_id' not in netDict or netDict['net_id']==None:
2772 myVMDict
['networks'].append(netDict
)
2773 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2774 #print myVMDict['name']
2775 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2776 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2777 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2779 if 'availability_zone' in myVMDict
:
2780 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2784 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2785 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2786 availability_zone_index
=av_index
,
2787 availability_zone_list
=vnf_availability_zones
)
2788 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2789 vm
['vim_id'] = vm_id
2790 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2791 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2792 for net
in myVMDict
['networks']:
2794 for iface
in vm
['interfaces']:
2795 if net
["name"]==iface
["internal_name"]:
2796 iface
["vim_id"]=net
["vim_id"]
2799 logger
.debug("start scenario Deployment done")
2800 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2801 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2802 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2803 return mydb
.get_instance_scenario(instance_id
)
2805 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2806 _
, message
= rollback(mydb
, vims
, rollbackList
)
2807 if isinstance(e
, db_base_Exception
):
2808 error_text
= "Exception at database"
2810 error_text
= "Exception at VIM"
2811 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2812 #logger.error("start_scenario %s", error_text)
2813 raise NfvoException(error_text
, e
.http_code
)
2815 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2816 """ join the cloud config information into cloud_config_preserve.
2817 In case of conflict cloud_config_preserve preserves
2820 if not cloud_config_preserve
and not cloud_config
:
2823 new_cloud_config
= {"key-pairs":[], "users":[]}
2825 if cloud_config_preserve
:
2826 for key
in cloud_config_preserve
.get("key-pairs", () ):
2827 if key
not in new_cloud_config
["key-pairs"]:
2828 new_cloud_config
["key-pairs"].append(key
)
2830 for key
in cloud_config
.get("key-pairs", () ):
2831 if key
not in new_cloud_config
["key-pairs"]:
2832 new_cloud_config
["key-pairs"].append(key
)
2833 if not new_cloud_config
["key-pairs"]:
2834 del new_cloud_config
["key-pairs"]
2838 new_cloud_config
["users"] += cloud_config
.get("users", () )
2839 if cloud_config_preserve
:
2840 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2841 index_to_delete
= []
2842 users
= new_cloud_config
.get("users", [])
2843 for index0
in range(0,len(users
)):
2844 if index0
in index_to_delete
:
2846 for index1
in range(index0
+1,len(users
)):
2847 if index1
in index_to_delete
:
2849 if users
[index0
]["name"] == users
[index1
]["name"]:
2850 index_to_delete
.append(index1
)
2851 for key
in users
[index1
].get("key-pairs",()):
2852 if "key-pairs" not in users
[index0
]:
2853 users
[index0
]["key-pairs"] = [key
]
2854 elif key
not in users
[index0
]["key-pairs"]:
2855 users
[index0
]["key-pairs"].append(key
)
2856 index_to_delete
.sort(reverse
=True)
2857 for index
in index_to_delete
:
2859 if not new_cloud_config
["users"]:
2860 del new_cloud_config
["users"]
2863 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2864 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2865 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2866 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2869 new_cloud_config
["user-data"] = []
2870 if cloud_config
and cloud_config
.get("user-data"):
2871 if isinstance(cloud_config
["user-data"], list):
2872 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2874 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2875 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2876 if isinstance(cloud_config_preserve
["user-data"], list):
2877 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2879 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2880 if not new_cloud_config
["user-data"]:
2881 del new_cloud_config
["user-data"]
2884 new_cloud_config
["config-files"] = []
2885 if cloud_config
and cloud_config
.get("config-files") != None:
2886 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2887 if cloud_config_preserve
:
2888 for file in cloud_config_preserve
.get("config-files", ()):
2889 for index
in range(0, len(new_cloud_config
["config-files"])):
2890 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2891 new_cloud_config
["config-files"][index
] = file
2894 new_cloud_config
["config-files"].append(file)
2895 if not new_cloud_config
["config-files"]:
2896 del new_cloud_config
["config-files"]
2897 return new_cloud_config
2900 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2901 datacenter_id
= None
2902 datacenter_name
= None
2905 if datacenter_tenant_id
:
2906 thread_id
= datacenter_tenant_id
2907 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2909 where_
={"td.nfvo_tenant_id": tenant_id
}
2910 if datacenter_id_name
:
2911 if utils
.check_valid_uuid(datacenter_id_name
):
2912 datacenter_id
= datacenter_id_name
2913 where_
["dt.datacenter_id"] = datacenter_id
2915 datacenter_name
= datacenter_id_name
2916 where_
["d.name"] = datacenter_name
2917 if datacenter_tenant_id
:
2918 where_
["dt.uuid"] = datacenter_tenant_id
2919 datacenters
= mydb
.get_rows(
2920 SELECT
=("dt.uuid as datacenter_tenant_id",),
2921 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2922 "join datacenters as d on d.uuid=dt.datacenter_id",
2924 if len(datacenters
) > 1:
2925 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2927 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2928 thread
= vim_threads
["running"].get(thread_id
)
2930 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2931 return thread_id
, thread
2932 except db_base_Exception
as e
:
2933 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
2936 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
2938 if utils
.check_valid_uuid(datacenter_id_name
):
2939 WHERE_dict
['d.uuid'] = datacenter_id_name
2941 WHERE_dict
['d.name'] = datacenter_id_name
2944 WHERE_dict
['nfvo_tenant_id'] = tenant_id
2945 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
2946 " dt on td.datacenter_tenant_id=dt.uuid"
2948 from_
= 'datacenters as d'
2949 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid, d.name as name",), WHERE
=WHERE_dict
)
2950 if len(vimaccounts
) == 0:
2951 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2952 elif len(vimaccounts
)>1:
2953 #print "nfvo.datacenter_action() error. Several datacenters found"
2954 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2955 return vimaccounts
[0]["uuid"], vimaccounts
[0]["name"]
2958 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
2959 datacenter_id
= None
2960 datacenter_name
= None
2961 if datacenter_id_name
:
2962 if utils
.check_valid_uuid(datacenter_id_name
):
2963 datacenter_id
= datacenter_id_name
2965 datacenter_name
= datacenter_id_name
2966 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
2968 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
2970 #print "nfvo.datacenter_action() error. Several datacenters found"
2971 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2972 return vims
.keys()[0], vims
.values()[0]
2976 """Takes dict d and updates it with the values in dict u.
2977 It merges all depth levels"""
2978 for k
, v
in u
.iteritems():
2979 if isinstance(v
, collections
.Mapping
):
2980 r
= update(d
.get(k
, {}), v
)
2987 def create_instance(mydb
, tenant_id
, instance_dict
):
2988 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2989 # logger.debug("Creating instance...")
2990 scenario
= instance_dict
["scenario"]
2992 # find main datacenter
2994 myvim_threads_id
= {}
2995 datacenter
= instance_dict
.get("datacenter")
2996 default_wim_account
= instance_dict
.get("wim_account")
2997 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
2998 myvims
[default_datacenter_id
] = vim
2999 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
3000 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
3001 # myvim_tenant = myvim['tenant_id']
3004 # print "Checking that the scenario exists and getting the scenario dictionary"
3005 if isinstance(scenario
, str):
3006 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
3007 datacenter_id
=default_datacenter_id
)
3009 scenarioDict
= scenario
3010 scenarioDict
["uuid"] = None
3012 # logger.debug(">>>>>> Dictionaries before merging")
3013 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3014 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
3016 db_instance_vnfs
= []
3017 db_instance_vms
= []
3018 db_instance_interfaces
= []
3019 db_instance_sfis
= []
3020 db_instance_sfs
= []
3021 db_instance_classifications
= []
3022 db_instance_sfps
= []
3027 instance_name
= instance_dict
["name"]
3028 instance_uuid
= str(uuid4())
3029 uuid_list
.append(instance_uuid
)
3030 db_instance_scenario
= {
3031 "uuid": instance_uuid
,
3032 "name": instance_name
,
3033 "tenant_id": tenant_id
,
3034 "scenario_id": scenarioDict
['uuid'],
3035 "datacenter_id": default_datacenter_id
,
3036 # filled bellow 'datacenter_tenant_id'
3037 "description": instance_dict
.get("description"),
3039 if scenarioDict
.get("cloud-config"):
3040 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
3041 default_flow_style
=True, width
=256)
3042 instance_action_id
= get_task_id()
3043 db_instance_action
= {
3044 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3045 "tenant_id": tenant_id
,
3046 "instance_id": instance_uuid
,
3047 "description": "CREATE",
3050 # Auxiliary dictionaries from x to y
3051 sce_net2instance
= {}
3052 net2task_id
= {'scenario': {}}
3053 # Mapping between local networks and WIMs
3056 def ip_profile_IM2RO(ip_profile_im
):
3057 # translate from input format to database format
3059 if 'subnet-address' in ip_profile_im
:
3060 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
3061 if 'ip-version' in ip_profile_im
:
3062 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
3063 if 'gateway-address' in ip_profile_im
:
3064 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
3065 if 'dns-address' in ip_profile_im
:
3066 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
3067 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
3068 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
3069 if 'dhcp' in ip_profile_im
:
3070 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
3071 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
3072 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
3073 return ip_profile_ro
3075 # logger.debug("Creating instance from scenario-dict:\n%s",
3076 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3078 # 0 check correct parameters
3079 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3080 for scenario_net
in scenarioDict
['nets']:
3081 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3084 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3085 httperrors
.Bad_Request
)
3086 if "sites" not in net_instance_desc
:
3087 net_instance_desc
["sites"] = [ {} ]
3088 site_without_datacenter_field
= False
3089 for site
in net_instance_desc
["sites"]:
3090 if site
.get("datacenter"):
3091 site
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3092 if site
["datacenter"] not in myvims
:
3093 # Add this datacenter to myvims
3094 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3096 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3097 site
["datacenter"] = d
# change name to id
3099 if site_without_datacenter_field
:
3100 raise NfvoException("Found more than one entries without datacenter field at "
3101 "instance:networks:{}:sites".format(net_name
), httperrors
.Bad_Request
)
3102 site_without_datacenter_field
= True
3103 site
["datacenter"] = default_datacenter_id
# change name to id
3105 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).iteritems():
3106 for scenario_vnf
in scenarioDict
['vnfs']:
3107 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3110 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), httperrors
.Bad_Request
)
3111 if "datacenter" in vnf_instance_desc
:
3112 # Add this datacenter to myvims
3113 vnf_instance_desc
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3114 if vnf_instance_desc
["datacenter"] not in myvims
:
3115 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3117 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3118 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3120 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).iteritems():
3121 for scenario_net
in scenario_vnf
['nets']:
3122 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3125 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), httperrors
.Bad_Request
)
3126 if net_instance_desc
.get("vim-network-name"):
3127 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3128 if net_instance_desc
.get("vim-network-id"):
3129 scenario_net
["vim-network-id"] = net_instance_desc
["vim-network-id"]
3130 if net_instance_desc
.get("name"):
3131 scenario_net
["name"] = net_instance_desc
["name"]
3132 if 'ip-profile' in net_instance_desc
:
3133 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3134 if 'ip_profile' not in scenario_net
:
3135 scenario_net
['ip_profile'] = ipprofile_db
3137 update(scenario_net
['ip_profile'], ipprofile_db
)
3139 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).iteritems():
3140 for scenario_vm
in scenario_vnf
['vms']:
3141 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3144 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3145 scenario_vm
["instance_parameters"] = vdu_instance_desc
3146 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).iteritems():
3147 for scenario_interface
in scenario_vm
['interfaces']:
3148 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3149 scenario_interface
.update(iface_instance_desc
)
3152 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3154 # 0.1 parse cloud-config parameters
3155 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3157 # 0.2 merge instance information into scenario
3158 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3159 # However, this is not possible yet.
3160 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).iteritems():
3161 for scenario_net
in scenarioDict
['nets']:
3162 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3163 if "wim_account" in net_instance_desc
and net_instance_desc
["wim_account"] is not None:
3164 scenario_net
["wim_account"] = net_instance_desc
["wim_account"]
3165 if 'ip-profile' in net_instance_desc
:
3166 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3167 if 'ip_profile' not in scenario_net
:
3168 scenario_net
['ip_profile'] = ipprofile_db
3170 update(scenario_net
['ip_profile'], ipprofile_db
)
3171 for interface
in net_instance_desc
.get('interfaces', ()):
3172 if 'ip_address' in interface
:
3173 for vnf
in scenarioDict
['vnfs']:
3174 if interface
['vnf'] == vnf
['name']:
3175 for vnf_interface
in vnf
['interfaces']:
3176 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3177 vnf_interface
['ip_address'] = interface
['ip_address']
3179 # logger.debug(">>>>>>>> Merged dictionary")
3180 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3181 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3183 # 1. Creating new nets (sce_nets) in the VIM"
3184 number_mgmt_networks
= 0
3185 db_instance_nets
= []
3186 for sce_net
in scenarioDict
['nets']:
3187 sce_net_uuid
= sce_net
.get('uuid', sce_net
["name"])
3188 # get involved datacenters where this network need to be created
3189 involved_datacenters
= []
3190 for sce_vnf
in scenarioDict
.get("vnfs", ()):
3191 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3192 if vnf_datacenter
in involved_datacenters
:
3194 if sce_vnf
.get("interfaces"):
3195 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3196 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3197 involved_datacenters
.append(vnf_datacenter
)
3199 if not involved_datacenters
:
3200 involved_datacenters
.append(default_datacenter_id
)
3201 target_wim_account
= sce_net
.get("wim_account", default_wim_account
)
3204 # TODO: use this information during network creation
3205 wim_account_id
= wim_account_name
= None
3206 if len(involved_datacenters
) > 1 and 'uuid' in sce_net
:
3207 if target_wim_account
is None or target_wim_account
is True: # automatic selection of WIM
3208 # OBS: sce_net without uuid are used internally to VNFs
3209 # and the assumption is that VNFs will not be split among
3210 # different datacenters
3211 wim_account
= wim_engine
.find_suitable_wim_account(
3212 involved_datacenters
, tenant_id
)
3213 wim_account_id
= wim_account
['uuid']
3214 wim_account_name
= wim_account
['name']
3215 wim_usage
[sce_net
['uuid']] = wim_account_id
3216 elif isinstance(target_wim_account
, str): # manual selection of WIM
3217 wim_account
.persist
.get_wim_account_by(target_wim_account
, tenant_id
)
3218 wim_account_id
= wim_account
['uuid']
3219 wim_account_name
= wim_account
['name']
3220 wim_usage
[sce_net
['uuid']] = wim_account_id
3221 else: # not WIM usage
3222 wim_usage
[sce_net
['uuid']] = False
3226 if instance_dict
.get("networks") and instance_dict
["networks"].get(sce_net
["name"]):
3227 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3228 net_name
= descriptor_net
.get("vim-network-name")
3229 # add datacenters from instantiation parameters
3230 if descriptor_net
.get("sites"):
3231 for site
in descriptor_net
["sites"]:
3232 if site
.get("datacenter") and site
["datacenter"] not in involved_datacenters
:
3233 involved_datacenters
.append(site
["datacenter"])
3234 sce_net2instance
[sce_net_uuid
] = {}
3235 net2task_id
['scenario'][sce_net_uuid
] = {}
3237 if sce_net
["external"]:
3238 number_mgmt_networks
+= 1
3240 for datacenter_id
in involved_datacenters
:
3242 netmap_create
= None
3243 if descriptor_net
.get("sites"):
3244 for site
in descriptor_net
["sites"]:
3245 if site
.get("datacenter") == datacenter_id
:
3246 netmap_use
= site
.get("netmap-use")
3247 netmap_create
= site
.get("netmap-create")
3250 vim
= myvims
[datacenter_id
]
3251 myvim_thread_id
= myvim_threads_id
[datacenter_id
]
3253 net_type
= sce_net
['type']
3255 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3258 if sce_net
["external"]:
3259 net_name
= sce_net
["name"]
3261 net_name
= "{}-{}".format(instance_name
, sce_net
["name"])
3262 net_name
= net_name
[:255] # limit length
3264 if netmap_use
or netmap_create
:
3265 create_network
= False
3266 lookfor_network
= False
3268 lookfor_network
= True
3269 if utils
.check_valid_uuid(netmap_use
):
3270 lookfor_filter
["id"] = netmap_use
3272 lookfor_filter
["name"] = netmap_use
3274 create_network
= True
3275 net_vim_name
= net_name
3276 if isinstance(netmap_create
, str):
3277 net_vim_name
= netmap_create
3278 elif sce_net
.get("vim_network_name"):
3279 create_network
= False
3280 lookfor_network
= True
3281 lookfor_filter
["name"] = sce_net
.get("vim_network_name")
3282 elif sce_net
["external"]:
3283 if sce_net
.get('vim_id'):
3284 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3285 create_network
= False
3286 lookfor_network
= True
3287 lookfor_filter
["id"] = sce_net
['vim_id']
3288 elif vim
["config"].get("management_network_id") or vim
["config"].get("management_network_name"):
3289 if number_mgmt_networks
> 1:
3290 raise NfvoException("Found several VLD of type mgmt. "
3291 "You must concrete what vim-network must be use for each one",
3292 httperrors
.Bad_Request
)
3293 create_network
= False
3294 lookfor_network
= True
3295 if vim
["config"].get("management_network_id"):
3296 lookfor_filter
["id"] = vim
["config"]["management_network_id"]
3298 lookfor_filter
["name"] = vim
["config"]["management_network_name"]
3300 # There is not a netmap, look at datacenter for a net with this name and create if not found
3301 create_network
= True
3302 lookfor_network
= True
3303 lookfor_filter
["name"] = sce_net
["name"]
3304 net_vim_name
= sce_net
["name"]
3306 net_vim_name
= net_name
3307 create_network
= True
3308 lookfor_network
= False
3312 task_action
= "CREATE"
3313 task_extra
["params"] = (net_vim_name
, net_type
, sce_net
.get('ip_profile', None), wim_account_name
)
3315 task_extra
["find"] = (lookfor_filter
,)
3316 elif lookfor_network
:
3317 task_action
= "FIND"
3318 task_extra
["params"] = (lookfor_filter
,)
3320 # fill database content
3321 net_uuid
= str(uuid4())
3322 uuid_list
.append(net_uuid
)
3323 sce_net2instance
[sce_net_uuid
][datacenter_id
] = net_uuid
3327 "vim_name": net_vim_name
,
3328 "instance_scenario_id": instance_uuid
,
3329 "sce_net_id": sce_net
.get("uuid"),
3330 "created": create_network
,
3331 'datacenter_id': datacenter_id
,
3332 'datacenter_tenant_id': myvim_thread_id
,
3333 'status': 'BUILD' # if create_network else "ACTIVE"
3335 db_instance_nets
.append(db_net
)
3337 "instance_action_id": instance_action_id
,
3338 "status": "SCHEDULED",
3339 "task_index": task_index
,
3340 "datacenter_vim_id": myvim_thread_id
,
3341 "action": task_action
,
3342 "item": "instance_nets",
3343 "item_id": net_uuid
,
3344 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3346 net2task_id
['scenario'][sce_net_uuid
][datacenter_id
] = task_index
3348 db_vim_actions
.append(db_vim_action
)
3350 if 'ip_profile' in sce_net
:
3352 'instance_net_id': net_uuid
,
3353 'ip_version': sce_net
['ip_profile']['ip_version'],
3354 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3355 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3356 'dns_address': sce_net
['ip_profile']['dns_address'],
3357 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3358 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3359 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3361 db_ip_profiles
.append(db_ip_profile
)
3365 "default_datacenter_id": default_datacenter_id
,
3366 "myvim_threads_id": myvim_threads_id
,
3367 "instance_uuid": instance_uuid
,
3368 "instance_name": instance_name
,
3369 "instance_action_id": instance_action_id
,
3371 "cloud_config": cloud_config
,
3372 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3373 "instance_parameters": instance_dict
,
3376 "task_index": task_index
,
3377 "uuid_list": uuid_list
,
3378 "db_instance_nets": db_instance_nets
,
3379 "db_vim_actions": db_vim_actions
,
3380 "db_ip_profiles": db_ip_profiles
,
3381 "db_instance_vnfs": db_instance_vnfs
,
3382 "db_instance_vms": db_instance_vms
,
3383 "db_instance_interfaces": db_instance_interfaces
,
3384 "net2task_id": net2task_id
,
3385 "sce_net2instance": sce_net2instance
,
3387 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3388 for sce_vnf
in scenarioDict
.get('vnfs', ()): # sce_vnf_list:
3389 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3390 task_index
= vnf_params_out
["task_index"]
3391 uuid_list
= vnf_params_out
["uuid_list"]
3394 # task_depends_on = []
3395 for vnffg
in scenarioDict
.get('vnffgs', ()):
3396 for rsp
in vnffg
['rsps']:
3398 for cp
in rsp
['connection_points']:
3399 count
= mydb
.get_rows(
3401 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3402 "on interfaces.uuid=h.ingress_interface_id",
3403 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3404 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3405 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3407 for instance_vm
in instance_vms
:
3408 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3410 dependencies
.append(action
['task_index'])
3411 # TODO: throw exception if count != len(instance_vms)
3412 # TODO: and action shouldn't ever be None
3414 for i
in range(count
):
3416 sfi_uuid
= str(uuid4())
3418 "ingress_interface_id": cp
["ingress_interface_id"],
3419 "egress_interface_id": cp
["egress_interface_id"]
3421 uuid_list
.append(sfi_uuid
)
3424 "instance_scenario_id": instance_uuid
,
3425 'sce_rsp_hop_id': cp
['uuid'],
3426 'datacenter_id': datacenter_id
,
3427 'datacenter_tenant_id': myvim_thread_id
,
3428 "vim_sfi_id": None, # vim thread will populate
3430 db_instance_sfis
.append(db_sfi
)
3432 "instance_action_id": instance_action_id
,
3433 "task_index": task_index
,
3434 "datacenter_vim_id": myvim_thread_id
,
3436 "status": "SCHEDULED",
3437 "item": "instance_sfis",
3438 "item_id": sfi_uuid
,
3439 "extra": yaml
.safe_dump({"params": extra_params
, "depends_on": [dependencies
[i
]]},
3440 default_flow_style
=True, width
=256)
3442 sfis_created
.append(task_index
)
3444 db_vim_actions
.append(db_vim_action
)
3446 sf_uuid
= str(uuid4())
3447 uuid_list
.append(sf_uuid
)
3450 "instance_scenario_id": instance_uuid
,
3451 'sce_rsp_hop_id': cp
['uuid'],
3452 'datacenter_id': datacenter_id
,
3453 'datacenter_tenant_id': myvim_thread_id
,
3454 "vim_sf_id": None, # vim thread will populate
3456 db_instance_sfs
.append(db_sf
)
3458 "instance_action_id": instance_action_id
,
3459 "task_index": task_index
,
3460 "datacenter_vim_id": myvim_thread_id
,
3462 "status": "SCHEDULED",
3463 "item": "instance_sfs",
3465 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3466 default_flow_style
=True, width
=256)
3468 sfs_created
.append(task_index
)
3470 db_vim_actions
.append(db_vim_action
)
3471 classifier
= rsp
['classifier']
3473 # TODO the following ~13 lines can be reused for the sfi case
3474 count
= mydb
.get_rows(
3475 SELECT
=('vms.count'),
3476 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3477 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3478 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3479 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3481 for instance_vm
in instance_vms
:
3482 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3484 dependencies
.append(action
['task_index'])
3485 # TODO: throw exception if count != len(instance_vms)
3486 # TODO: and action shouldn't ever be None
3487 classifications_created
= []
3488 for i
in range(count
):
3489 for match
in classifier
['matches']:
3490 # create classifications
3491 classification_uuid
= str(uuid4())
3492 uuid_list
.append(classification_uuid
)
3493 db_classification
= {
3494 "uuid": classification_uuid
,
3495 "instance_scenario_id": instance_uuid
,
3496 'sce_classifier_match_id': match
['uuid'],
3497 'datacenter_id': datacenter_id
,
3498 'datacenter_tenant_id': myvim_thread_id
,
3499 "vim_classification_id": None, # vim thread will populate
3501 db_instance_classifications
.append(db_classification
)
3502 classification_params
= {
3503 "ip_proto": match
["ip_proto"],
3504 "source_ip": match
["source_ip"],
3505 "destination_ip": match
["destination_ip"],
3506 "source_port": match
["source_port"],
3507 "destination_port": match
["destination_port"]
3510 "instance_action_id": instance_action_id
,
3511 "task_index": task_index
,
3512 "datacenter_vim_id": myvim_thread_id
,
3514 "status": "SCHEDULED",
3515 "item": "instance_classifications",
3516 "item_id": classification_uuid
,
3517 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3518 default_flow_style
=True, width
=256)
3520 classifications_created
.append(task_index
)
3522 db_vim_actions
.append(db_vim_action
)
3525 sfp_uuid
= str(uuid4())
3526 uuid_list
.append(sfp_uuid
)
3529 "instance_scenario_id": instance_uuid
,
3530 'sce_rsp_id': rsp
['uuid'],
3531 'datacenter_id': datacenter_id
,
3532 'datacenter_tenant_id': myvim_thread_id
,
3533 "vim_sfp_id": None, # vim thread will populate
3535 db_instance_sfps
.append(db_sfp
)
3537 "instance_action_id": instance_action_id
,
3538 "task_index": task_index
,
3539 "datacenter_vim_id": myvim_thread_id
,
3541 "status": "SCHEDULED",
3542 "item": "instance_sfps",
3543 "item_id": sfp_uuid
,
3544 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3545 default_flow_style
=True, width
=256)
3548 db_vim_actions
.append(db_vim_action
)
3549 db_instance_action
["number_tasks"] = task_index
3552 logger
.debug('wim_usage:\n%s\n\n', pformat(wim_usage
))
3553 wan_links
= wim_engine
.derive_wan_links(wim_usage
, db_instance_nets
, tenant_id
)
3554 wim_actions
= wim_engine
.create_actions(wan_links
)
3555 wim_actions
, db_instance_action
= (
3556 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
3559 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3561 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3562 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3564 {"instance_scenarios": db_instance_scenario
},
3565 {"instance_vnfs": db_instance_vnfs
},
3566 {"instance_nets": db_instance_nets
},
3567 {"ip_profiles": db_ip_profiles
},
3568 {"instance_vms": db_instance_vms
},
3569 {"instance_interfaces": db_instance_interfaces
},
3570 {"instance_actions": db_instance_action
},
3571 {"instance_sfis": db_instance_sfis
},
3572 {"instance_sfs": db_instance_sfs
},
3573 {"instance_classifications": db_instance_classifications
},
3574 {"instance_sfps": db_instance_sfps
},
3575 {"instance_wim_nets": wan_links
},
3576 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
3579 logger
.debug("create_instance done DB tables: %s",
3580 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3581 mydb
.new_rows(db_tables
, uuid_list
)
3582 for myvim_thread_id
in myvim_threads_id
.values():
3583 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3585 wim_engine
.dispatch(wim_actions
)
3587 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3588 returned_instance
["action_id"] = instance_action_id
3589 return returned_instance
3590 except (NfvoException
, vimconn
.vimconnException
, wimconn
.WimConnectorError
, db_base_Exception
) as e
:
3591 message
= rollback(mydb
, myvims
, rollbackList
)
3592 if isinstance(e
, db_base_Exception
):
3593 error_text
= "database Exception"
3594 elif isinstance(e
, vimconn
.vimconnException
):
3595 error_text
= "VIM Exception"
3596 elif isinstance(e
, wimconn
.WimConnectorError
):
3597 error_text
= "WIM Exception"
3599 error_text
= "Exception"
3600 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3601 # logger.error("create_instance: %s", error_text)
3603 raise NfvoException(error_text
, e
.http_code
)
3606 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3607 default_datacenter_id
= params
["default_datacenter_id"]
3608 myvim_threads_id
= params
["myvim_threads_id"]
3609 instance_uuid
= params
["instance_uuid"]
3610 instance_name
= params
["instance_name"]
3611 instance_action_id
= params
["instance_action_id"]
3612 myvims
= params
["myvims"]
3613 cloud_config
= params
["cloud_config"]
3614 RO_pub_key
= params
["RO_pub_key"]
3616 task_index
= params_out
["task_index"]
3617 uuid_list
= params_out
["uuid_list"]
3618 db_instance_nets
= params_out
["db_instance_nets"]
3619 db_vim_actions
= params_out
["db_vim_actions"]
3620 db_ip_profiles
= params_out
["db_ip_profiles"]
3621 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3622 db_instance_vms
= params_out
["db_instance_vms"]
3623 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3624 net2task_id
= params_out
["net2task_id"]
3625 sce_net2instance
= params_out
["sce_net2instance"]
3627 vnf_net2instance
= {}
3629 # 2. Creating new nets (vnf internal nets) in the VIM"
3630 # For each vnf net, we create it and we add it to instanceNetlist.
3631 if sce_vnf
.get("datacenter"):
3632 datacenter_id
= sce_vnf
["datacenter"]
3633 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3635 datacenter_id
= default_datacenter_id
3636 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3637 for net
in sce_vnf
['nets']:
3639 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3640 # net_name = descriptor_net.get("name")
3643 net_name
= "{}-{}".format(instance_name
, net
["name"])
3644 net_name
= net_name
[:255] # limit length
3645 net_type
= net
['type']
3647 if sce_vnf
['uuid'] not in vnf_net2instance
:
3648 vnf_net2instance
[sce_vnf
['uuid']] = {}
3649 if sce_vnf
['uuid'] not in net2task_id
:
3650 net2task_id
[sce_vnf
['uuid']] = {}
3651 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3653 # fill database content
3654 net_uuid
= str(uuid4())
3655 uuid_list
.append(net_uuid
)
3656 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3660 "vim_name": net_name
,
3661 "instance_scenario_id": instance_uuid
,
3662 "net_id": net
["uuid"],
3664 'datacenter_id': datacenter_id
,
3665 'datacenter_tenant_id': myvim_thread_id
,
3667 db_instance_nets
.append(db_net
)
3670 if net
.get("vim-network-name"):
3671 lookfor_filter
["name"] = net
["vim-network-name"]
3672 if net
.get("vim-network-id"):
3673 lookfor_filter
["id"] = net
["vim-network-id"]
3675 task_action
= "FIND"
3676 task_extra
= {"params": (lookfor_filter
,)}
3678 task_action
= "CREATE"
3679 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3682 "instance_action_id": instance_action_id
,
3683 "task_index": task_index
,
3684 "datacenter_vim_id": myvim_thread_id
,
3685 "status": "SCHEDULED",
3686 "action": task_action
,
3687 "item": "instance_nets",
3688 "item_id": net_uuid
,
3689 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3692 db_vim_actions
.append(db_vim_action
)
3694 if 'ip_profile' in net
:
3696 'instance_net_id': net_uuid
,
3697 'ip_version': net
['ip_profile']['ip_version'],
3698 'subnet_address': net
['ip_profile']['subnet_address'],
3699 'gateway_address': net
['ip_profile']['gateway_address'],
3700 'dns_address': net
['ip_profile']['dns_address'],
3701 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3702 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3703 'dhcp_count': net
['ip_profile']['dhcp_count'],
3705 db_ip_profiles
.append(db_ip_profile
)
3707 # print "vnf_net2instance:"
3708 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3710 # 3. Creating new vm instances in the VIM
3711 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3713 if sce_vnf
.get('mgmt_access'):
3714 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3715 vnf_availability_zones
= []
3716 for vm
in sce_vnf
.get('vms'):
3717 vm_av
= vm
.get('availability_zone')
3718 if vm_av
and vm_av
not in vnf_availability_zones
:
3719 vnf_availability_zones
.append(vm_av
)
3721 # check if there is enough availability zones available at vim level.
3722 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3723 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3724 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
3726 if sce_vnf
.get("datacenter"):
3727 vim
= myvims
[sce_vnf
["datacenter"]]
3728 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3729 datacenter_id
= sce_vnf
["datacenter"]
3731 vim
= myvims
[default_datacenter_id
]
3732 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3733 datacenter_id
= default_datacenter_id
3734 sce_vnf
["datacenter_id"] = datacenter_id
3737 vnf_uuid
= str(uuid4())
3738 uuid_list
.append(vnf_uuid
)
3741 'instance_scenario_id': instance_uuid
,
3742 'vnf_id': sce_vnf
['vnf_id'],
3743 'sce_vnf_id': sce_vnf
['uuid'],
3744 'datacenter_id': datacenter_id
,
3745 'datacenter_tenant_id': myvim_thread_id
,
3747 db_instance_vnfs
.append(db_instance_vnf
)
3749 for vm
in sce_vnf
['vms']:
3751 if vm
.get("pdu_type"):
3755 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3756 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3757 myVMDict
['description'] = myVMDict
['name'][0:99]
3759 # myVMDict['start'] = "no"
3760 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3761 myVMDict
['name'] = vm
["instance_parameters"].get("name")
3762 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
3763 # create image at vim in case it not exist
3764 image_uuid
= vm
['image_id']
3765 if vm
.get("image_list"):
3766 for alternative_image
in vm
["image_list"]:
3767 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
3768 image_uuid
= alternative_image
['image_id']
3770 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
3771 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
3772 vm
['vim_image_id'] = image_id
3774 # create flavor at vim in case it not exist
3775 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
3776 if flavor_dict
['extended'] != None:
3777 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'])
3778 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
3780 # Obtain information for additional disks
3781 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
3782 WHERE
={'vim_id': flavor_id
})
3783 if not extended_flavor_dict
:
3784 raise NfvoException("flavor '{}' not found".format(flavor_id
), httperrors
.Not_Found
)
3786 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
3787 myVMDict
['disks'] = None
3788 extended_info
= extended_flavor_dict
[0]['extended']
3789 if extended_info
!= None:
3790 extended_flavor_dict_yaml
= yaml
.load(extended_info
)
3791 if 'disks' in extended_flavor_dict_yaml
:
3792 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
3793 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
3794 for disk
in myVMDict
['disks']:
3795 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
3796 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
3798 vm
['vim_flavor_id'] = flavor_id
3799 myVMDict
['imageRef'] = vm
['vim_image_id']
3800 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
3801 myVMDict
['availability_zone'] = vm
.get('availability_zone')
3802 myVMDict
['networks'] = []
3803 task_depends_on
= []
3804 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
3805 is_management_vm
= False
3807 for iface
in vm
['interfaces']:
3809 if iface
['type'] == "data":
3810 netDict
['type'] = iface
['model']
3811 elif "model" in iface
and iface
["model"] != None:
3812 netDict
['model'] = iface
['model']
3813 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
3814 # is obtained from iterface table model
3815 # discover type of interface looking at flavor
3816 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
3817 for flavor_iface
in numa
.get('interfaces', []):
3818 if flavor_iface
.get('name') == iface
['internal_name']:
3819 if flavor_iface
['dedicated'] == 'yes':
3820 netDict
['type'] = "PF" # passthrough
3821 elif flavor_iface
['dedicated'] == 'no':
3822 netDict
['type'] = "VF" # siov
3823 elif flavor_iface
['dedicated'] == 'yes:sriov':
3824 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
3825 netDict
["mac_address"] = flavor_iface
.get("mac_address")
3827 netDict
["use"] = iface
['type']
3828 if netDict
["use"] == "data" and not netDict
.get("type"):
3829 # print "netDict", netDict
3830 # print "iface", iface
3831 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
3832 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
3833 if flavor_dict
.get('extended') == None:
3834 raise NfvoException(e_text
+ "After database migration some information is not available. \
3835 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
3837 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
3838 if netDict
["use"] == "mgmt":
3839 is_management_vm
= True
3840 netDict
["type"] = "virtual"
3841 if netDict
["use"] == "bridge":
3842 netDict
["type"] = "virtual"
3843 if iface
.get("vpci"):
3844 netDict
['vpci'] = iface
['vpci']
3845 if iface
.get("mac"):
3846 netDict
['mac_address'] = iface
['mac']
3847 if iface
.get("mac_address"):
3848 netDict
['mac_address'] = iface
['mac_address']
3849 if iface
.get("ip_address"):
3850 netDict
['ip_address'] = iface
['ip_address']
3851 if iface
.get("port-security") is not None:
3852 netDict
['port_security'] = iface
['port-security']
3853 if iface
.get("floating-ip") is not None:
3854 netDict
['floating_ip'] = iface
['floating-ip']
3855 netDict
['name'] = iface
['internal_name']
3856 if iface
['net_id'] is None:
3857 for vnf_iface
in sce_vnf
["interfaces"]:
3860 if vnf_iface
['interface_id'] == iface
['uuid']:
3861 netDict
['net_id'] = "TASK-{}".format(
3862 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3863 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
3864 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
3867 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3868 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
3869 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
3870 # skip bridge ifaces not connected to any net
3871 if 'net_id' not in netDict
or netDict
['net_id'] == None:
3873 myVMDict
['networks'].append(netDict
)
3876 # 'instance_vm_id': instance_vm_uuid,
3877 "instance_net_id": instance_net_id
,
3878 'interface_id': iface
['uuid'],
3879 # 'vim_interface_id': ,
3880 'type': 'external' if iface
['external_name'] is not None else 'internal',
3881 'ip_address': iface
.get('ip_address'),
3882 'mac_address': iface
.get('mac'),
3883 'floating_ip': int(iface
.get('floating-ip', False)),
3884 'port_security': int(iface
.get('port-security', True))
3886 db_vm_ifaces
.append(db_vm_iface
)
3887 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3888 # print myVMDict['name']
3889 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
3890 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
3891 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
3893 # We add the RO key to cloud_config if vnf will need ssh access
3894 cloud_config_vm
= cloud_config
3895 if is_management_vm
and params
["instance_parameters"].get("mgmt_keys"):
3896 cloud_config_vm
= unify_cloud_config({"key-pairs": params
["instance_parameters"]["mgmt_keys"]},
3899 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("mgmt_keys"):
3900 cloud_config_vm
= unify_cloud_config({"key-pairs": vm
["instance_parameters"]["mgmt_keys"]},
3902 # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
3903 # RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
3904 # cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
3905 if vm
.get("boot_data"):
3906 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
3908 if myVMDict
.get('availability_zone'):
3909 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
3912 for vm_index
in range(0, vm
.get('count', 1)):
3913 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
3914 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
3915 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
3916 myVMDict
['disks'], av_index
, vnf_availability_zones
)
3917 # put interface uuid back to scenario[vnfs][vms[[interfaces]
3918 for net
in myVMDict
['networks']:
3920 for iface
in vm
['interfaces']:
3921 if net
["name"] == iface
["internal_name"]:
3922 iface
["vim_id"] = net
["vim_id"]
3924 vm_uuid
= str(uuid4())
3925 uuid_list
.append(vm_uuid
)
3928 'instance_vnf_id': vnf_uuid
,
3929 # TODO delete "vim_vm_id": vm_id,
3930 "vm_id": vm
["uuid"],
3931 "vim_name": vm_name
,
3934 db_instance_vms
.append(db_vm
)
3937 for db_vm_iface
in db_vm_ifaces
:
3938 iface_uuid
= str(uuid4())
3939 uuid_list
.append(iface_uuid
)
3940 db_vm_iface_instance
= {
3942 "instance_vm_id": vm_uuid
3944 db_vm_iface_instance
.update(db_vm_iface
)
3945 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
3946 ip
= db_vm_iface_instance
.get("ip_address")
3951 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
3952 db_vm_iface_instance
["ip_address"] = ip
3954 db_vm_iface_instance
["ip_address"] = None
3955 db_instance_interfaces
.append(db_vm_iface_instance
)
3956 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
3960 "instance_action_id": instance_action_id
,
3961 "task_index": task_index
,
3962 "datacenter_vim_id": myvim_thread_id
,
3964 "status": "SCHEDULED",
3965 "item": "instance_vms",
3967 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
3968 default_flow_style
=True, width
=256)
3971 db_vim_actions
.append(db_vim_action
)
3972 params_out
["task_index"] = task_index
3973 params_out
["uuid_list"] = uuid_list
3976 def delete_instance(mydb
, tenant_id
, instance_id
):
3977 # print "Checking that the instance_id exists and getting the instance dictionary"
3978 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
3979 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
3980 tenant_id
= instanceDict
["tenant_id"]
3983 # We need to retrieve the WIM Actions now, before the instance_scenario is
3984 # deleted. The reason for that is that: ON CASCADE rules will delete the
3985 # instance_wim_nets record in the database
3986 wim_actions
= wim_engine
.delete_actions(instance_scenario_id
=instance_id
)
3989 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3990 # 1. Delete from Database
3991 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
3993 # 2. delete from VIM
3997 vimthread_affected
= {}
3998 net2vm_dependencies
= {}
4001 instance_action_id
= get_task_id()
4003 db_instance_action
= {
4004 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4005 "tenant_id": tenant_id
,
4006 "instance_id": instance_id
,
4007 "description": "DELETE",
4008 # "number_tasks": 0 # filled bellow
4011 # 2.1 deleting VNFFGs
4012 for sfp
in instanceDict
.get('sfps', ()):
4013 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
4014 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4015 if datacenter_key
not in myvims
:
4017 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4018 except NfvoException
as e
:
4019 logger
.error(str(e
))
4021 myvim_threads
[datacenter_key
] = myvim_thread
4022 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
4023 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
4025 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
4026 myvims
[datacenter_key
] = None
4028 myvims
[datacenter_key
] = vims
.values()[0]
4029 myvim
= myvims
[datacenter_key
]
4030 myvim_thread
= myvim_threads
[datacenter_key
]
4033 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
4035 extra
= {"params": (sfp
['vim_sfp_id'])}
4037 "instance_action_id": instance_action_id
,
4038 "task_index": task_index
,
4039 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
4041 "status": "SCHEDULED",
4042 "item": "instance_sfps",
4043 "item_id": sfp
["uuid"],
4044 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4047 db_vim_actions
.append(db_vim_action
)
4049 for classification
in instanceDict
['classifications']:
4050 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4051 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4052 if datacenter_key
not in myvims
:
4054 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4055 except NfvoException
as e
:
4056 logger
.error(str(e
))
4058 myvim_threads
[datacenter_key
] = myvim_thread
4059 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4060 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4062 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"],
4063 classification
["datacenter_tenant_id"]))
4064 myvims
[datacenter_key
] = None
4066 myvims
[datacenter_key
] = vims
.values()[0]
4067 myvim
= myvims
[datacenter_key
]
4068 myvim_thread
= myvim_threads
[datacenter_key
]
4071 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'],
4072 classification
["datacenter_id"])
4074 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4075 extra
= {"params": (classification
['vim_classification_id']), "depends_on": depends_on
}
4077 "instance_action_id": instance_action_id
,
4078 "task_index": task_index
,
4079 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4081 "status": "SCHEDULED",
4082 "item": "instance_classifications",
4083 "item_id": classification
["uuid"],
4084 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4087 db_vim_actions
.append(db_vim_action
)
4089 for sf
in instanceDict
.get('sfs', ()):
4090 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
4091 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4092 if datacenter_key
not in myvims
:
4094 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4095 except NfvoException
as e
:
4096 logger
.error(str(e
))
4098 myvim_threads
[datacenter_key
] = myvim_thread
4099 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
4100 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
4102 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
4103 myvims
[datacenter_key
] = None
4105 myvims
[datacenter_key
] = vims
.values()[0]
4106 myvim
= myvims
[datacenter_key
]
4107 myvim_thread
= myvim_threads
[datacenter_key
]
4110 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
4112 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4113 extra
= {"params": (sf
['vim_sf_id']), "depends_on": depends_on
}
4115 "instance_action_id": instance_action_id
,
4116 "task_index": task_index
,
4117 "datacenter_vim_id": sf
["datacenter_tenant_id"],
4119 "status": "SCHEDULED",
4120 "item": "instance_sfs",
4121 "item_id": sf
["uuid"],
4122 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4125 db_vim_actions
.append(db_vim_action
)
4127 for sfi
in instanceDict
.get('sfis', ()):
4128 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4129 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4130 if datacenter_key
not in myvims
:
4132 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4133 except NfvoException
as e
:
4134 logger
.error(str(e
))
4136 myvim_threads
[datacenter_key
] = myvim_thread
4137 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4138 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4140 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4141 myvims
[datacenter_key
] = None
4143 myvims
[datacenter_key
] = vims
.values()[0]
4144 myvim
= myvims
[datacenter_key
]
4145 myvim_thread
= myvim_threads
[datacenter_key
]
4148 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4150 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfs"]
4151 extra
= {"params": (sfi
['vim_sfi_id']), "depends_on": depends_on
}
4153 "instance_action_id": instance_action_id
,
4154 "task_index": task_index
,
4155 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4157 "status": "SCHEDULED",
4158 "item": "instance_sfis",
4159 "item_id": sfi
["uuid"],
4160 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4163 db_vim_actions
.append(db_vim_action
)
4167 for sce_vnf
in instanceDict
.get('vnfs', ()):
4168 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4169 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
4170 if datacenter_key
not in myvims
:
4172 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4173 except NfvoException
as e
:
4174 logger
.error(str(e
))
4176 myvim_threads
[datacenter_key
] = myvim_thread
4177 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
4178 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
4180 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
4181 sce_vnf
["datacenter_tenant_id"]))
4182 myvims
[datacenter_key
] = None
4184 myvims
[datacenter_key
] = vims
.values()[0]
4185 myvim
= myvims
[datacenter_key
]
4186 myvim_thread
= myvim_threads
[datacenter_key
]
4188 for vm
in sce_vnf
['vms']:
4190 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
4192 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4194 "instance_action_id": instance_action_id
,
4195 "task_index": task_index
,
4196 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
4198 "status": "SCHEDULED",
4199 "item": "instance_vms",
4200 "item_id": vm
["uuid"],
4201 "extra": yaml
.safe_dump({"params": vm
["interfaces"], "depends_on": sfi_dependencies
},
4202 default_flow_style
=True, width
=256)
4204 db_vim_actions
.append(db_vim_action
)
4205 for interface
in vm
["interfaces"]:
4206 if not interface
.get("instance_net_id"):
4208 if interface
["instance_net_id"] not in net2vm_dependencies
:
4209 net2vm_dependencies
[interface
["instance_net_id"]] = []
4210 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
4215 for net
in instanceDict
['nets']:
4216 vimthread_affected
[net
["datacenter_tenant_id"]] = None
4217 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
4218 if datacenter_key
not in myvims
:
4220 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, net
["datacenter_id"], net
["datacenter_tenant_id"])
4221 except NfvoException
as e
:
4222 logger
.error(str(e
))
4224 myvim_threads
[datacenter_key
] = myvim_thread
4225 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
4226 datacenter_tenant_id
=net
["datacenter_tenant_id"])
4228 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
4229 myvims
[datacenter_key
] = None
4231 myvims
[datacenter_key
] = vims
.values()[0]
4232 myvim
= myvims
[datacenter_key
]
4233 myvim_thread
= myvim_threads
[datacenter_key
]
4236 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
4238 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
4239 if net2vm_dependencies
.get(net
["uuid"]):
4240 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
4241 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4242 if len(sfi_dependencies
) > 0:
4243 if "depends_on" in extra
:
4244 extra
["depends_on"] += sfi_dependencies
4246 extra
["depends_on"] = sfi_dependencies
4248 "instance_action_id": instance_action_id
,
4249 "task_index": task_index
,
4250 "datacenter_vim_id": net
["datacenter_tenant_id"],
4252 "status": "SCHEDULED",
4253 "item": "instance_nets",
4254 "item_id": net
["uuid"],
4255 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4258 db_vim_actions
.append(db_vim_action
)
4260 db_instance_action
["number_tasks"] = task_index
4263 wim_actions
, db_instance_action
= (
4264 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
4268 {"instance_actions": db_instance_action
},
4269 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
4272 logger
.debug("delete_instance done DB tables: %s",
4273 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4274 mydb
.new_rows(db_tables
, ())
4275 for myvim_thread_id
in vimthread_affected
.keys():
4276 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4278 wim_engine
.dispatch(wim_actions
)
4280 if len(error_msg
) > 0:
4281 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4282 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4284 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4286 def get_instance_id(mydb
, tenant_id
, instance_id
):
4288 #check valid tenant_id
4289 check_tenant(mydb
, tenant_id
)
4292 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4293 for net
in instance_dict
["nets"]:
4294 if net
.get("sdn_net_id"):
4295 net_sdn
= ovim
.show_network(net
["sdn_net_id"])
4297 "admin_state_up": net_sdn
.get("admin_state_up"),
4298 "flows": net_sdn
.get("flows"),
4299 "last_error": net_sdn
.get("last_error"),
4300 "ports": net_sdn
.get("ports"),
4301 "type": net_sdn
.get("type"),
4302 "status": net_sdn
.get("status"),
4303 "vlan": net_sdn
.get("vlan"),
4305 return instance_dict
4307 @deprecated("Instance is automatically refreshed by vim_threads")
4308 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4309 '''Refreshes a scenario instance. It modifies instanceDict'''
4311 - 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
4314 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4315 # #print "nfvo.refresh_instance begins"
4316 # #print json.dumps(instanceDict, indent=4)
4318 # #print "Getting the VIM URL and the VIM tenant_id"
4321 # # 1. Getting VIM vm and net list
4322 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4325 # for sce_vnf in instanceDict['vnfs']:
4326 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4327 # if datacenter_key not in vm_list:
4328 # vm_list[datacenter_key] = []
4329 # if datacenter_key not in myvims:
4330 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4331 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4332 # if len(vims) == 0:
4333 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4334 # myvims[datacenter_key] = None
4336 # myvims[datacenter_key] = vims.values()[0]
4337 # for vm in sce_vnf['vms']:
4338 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4339 # vms_notupdated.append(vm["uuid"])
4341 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4342 # nets_notupdated=[]
4344 # for net in instanceDict['nets']:
4345 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4346 # if datacenter_key not in net_list:
4347 # net_list[datacenter_key] = []
4348 # if datacenter_key not in myvims:
4349 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4350 # datacenter_tenant_id=net["datacenter_tenant_id"])
4351 # if len(vims) == 0:
4352 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4353 # myvims[datacenter_key] = None
4355 # myvims[datacenter_key] = vims.values()[0]
4357 # net_list[datacenter_key].append(net['vim_net_id'])
4358 # nets_notupdated.append(net["uuid"])
4360 # # 1. Getting the status of all VMs
4362 # for datacenter_key in myvims:
4363 # if not vm_list.get(datacenter_key):
4367 # if not myvims[datacenter_key]:
4368 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4371 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4373 # except vimconn.vimconnException as e:
4374 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4375 # failed_message = str(e)
4377 # for vm in vm_list[datacenter_key]:
4378 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4380 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4381 # for sce_vnf in instanceDict['vnfs']:
4382 # for vm in sce_vnf['vms']:
4383 # vm_id = vm['vim_vm_id']
4384 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4385 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4386 # has_mgmt_iface = False
4387 # for iface in vm["interfaces"]:
4388 # if iface["type"]=="mgmt":
4389 # has_mgmt_iface = True
4390 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4391 # vm_dict[vm_id]['status'] = "ACTIVE"
4392 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4393 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4394 # 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'):
4395 # vm['status'] = vm_dict[vm_id]['status']
4396 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4397 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4398 # # 2.1. Update in openmano DB the VMs whose status changed
4400 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4401 # vms_notupdated.remove(vm["uuid"])
4403 # vms_updated.append(vm["uuid"])
4404 # except db_base_Exception as e:
4405 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4406 # # 2.2. Update in openmano DB the interface VMs
4407 # for interface in interfaces:
4408 # #translate from vim_net_id to instance_net_id
4409 # network_id_list=[]
4410 # for net in instanceDict['nets']:
4411 # if net["vim_net_id"] == interface["vim_net_id"]:
4412 # network_id_list.append(net["uuid"])
4413 # if not network_id_list:
4415 # del interface["vim_net_id"]
4417 # for network_id in network_id_list:
4418 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4419 # except db_base_Exception as e:
4420 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4422 # # 3. Getting the status of all nets
4424 # for datacenter_key in myvims:
4425 # if not net_list.get(datacenter_key):
4428 # failed_message = ""
4429 # if not myvims[datacenter_key]:
4430 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4433 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4435 # except vimconn.vimconnException as e:
4436 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4437 # failed_message = str(e)
4439 # for net in net_list[datacenter_key]:
4440 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4442 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4443 # # TODO: update nets inside a vnf
4444 # for net in instanceDict['nets']:
4445 # net_id = net['vim_net_id']
4446 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4447 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4448 # 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'):
4449 # net['status'] = net_dict[net_id]['status']
4450 # net['error_msg'] = net_dict[net_id].get('error_msg')
4451 # net['vim_info'] = net_dict[net_id].get('vim_info')
4452 # # 5.1. Update in openmano DB the nets whose status changed
4454 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4455 # nets_notupdated.remove(net["uuid"])
4457 # nets_updated.append(net["uuid"])
4458 # except db_base_Exception as e:
4459 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4461 # # Returns appropriate output
4462 # #print "nfvo.refresh_instance finishes"
4463 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4464 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4465 instance_id
= instanceDict
['uuid']
4466 # if len(vms_notupdated)+len(nets_notupdated)>0:
4467 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4468 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4470 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4472 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4473 #print "Checking that the instance_id exists and getting the instance dictionary"
4474 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4475 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4477 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4478 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4480 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), httperrors
.Not_Found
)
4481 myvim
= vims
.values()[0]
4486 myvim_threads_id
= {}
4487 if action_dict
.get("vdu-scaling"):
4488 db_instance_vms
= []
4490 db_instance_interfaces
= []
4491 instance_action_id
= get_task_id()
4492 db_instance_action
= {
4493 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4494 "tenant_id": nfvo_tenant
,
4495 "instance_id": instance_id
,
4496 "description": "SCALE",
4498 vm_result
["instance_action_id"] = instance_action_id
4499 vm_result
["created"] = []
4500 vm_result
["deleted"] = []
4502 for vdu
in action_dict
["vdu-scaling"]:
4503 vdu_id
= vdu
.get("vdu-id")
4504 osm_vdu_id
= vdu
.get("osm_vdu_id")
4505 member_vnf_index
= vdu
.get("member-vnf-index")
4506 vdu_count
= vdu
.get("count", 1)
4508 target_vms
= mydb
.get_rows(
4509 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4510 WHERE
={"vms.uuid": vdu_id
},
4511 ORDER_BY
="vms.created_at"
4514 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), httperrors
.Not_Found
)
4516 if not osm_vdu_id
and not member_vnf_index
:
4517 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4518 target_vms
= mydb
.get_rows(
4519 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4520 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4521 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4522 " join vms on ivms.vm_id=vms.uuid",
4523 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
,
4524 "ivnfs.instance_scenario_id": instance_id
},
4525 ORDER_BY
="ivms.created_at"
4528 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), httperrors
.Not_Found
)
4529 vdu_id
= target_vms
[-1]["uuid"]
4530 target_vm
= target_vms
[-1]
4531 datacenter
= target_vm
["datacenter_id"]
4532 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4534 if vdu
["type"] == "delete":
4535 for index
in range(0, vdu_count
):
4536 target_vm
= target_vms
[-1-index
]
4537 vdu_id
= target_vm
["uuid"]
4539 vm_interfaces
= None
4540 for sce_vnf
in instanceDict
['vnfs']:
4541 for vm
in sce_vnf
['vms']:
4542 if vm
["uuid"] == vdu_id
:
4543 vm_interfaces
= vm
["interfaces"]
4547 "instance_action_id": instance_action_id
,
4548 "task_index": task_index
,
4549 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4551 "status": "SCHEDULED",
4552 "item": "instance_vms",
4554 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4555 default_flow_style
=True, width
=256)
4558 db_vim_actions
.append(db_vim_action
)
4559 vm_result
["deleted"].append(vdu_id
)
4560 # delete from database
4561 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4563 else: # vdu["type"] == "create":
4565 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4567 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
=where
)
4568 if not vim_action_to_clone
:
4569 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), httperrors
.Internal_Server_Error
)
4570 vim_action_to_clone
= vim_action_to_clone
[0]
4571 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4573 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4574 # TODO do the same for flavor and image when available
4575 task_depends_on
= []
4576 task_params
= extra
["params"]
4577 task_params_networks
= deepcopy(task_params
[5])
4578 for iface
in task_params
[5]:
4579 if iface
["net_id"].startswith("TASK-"):
4580 if "." not in iface
["net_id"]:
4581 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4582 iface
["net_id"][5:]))
4583 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4584 iface
["net_id"][5:])
4586 task_depends_on
.append(iface
["net_id"][5:])
4587 if "mac_address" in iface
:
4588 del iface
["mac_address"]
4590 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4591 for index
in range(0, vdu_count
):
4592 vm_uuid
= str(uuid4())
4593 vm_name
= target_vm
.get('vim_name')
4595 suffix
= vm_name
.rfind("-")
4596 vm_name
= vm_name
[:suffix
+1] + str(index
+ 1 + int(vm_name
[suffix
+1:]))
4601 'instance_vnf_id': target_vm
['instance_vnf_id'],
4602 'vm_id': target_vm
['vm_id'],
4605 db_instance_vms
.append(db_instance_vm
)
4607 for vm_iface
in vm_ifaces_to_clone
:
4608 iface_uuid
= str(uuid4())
4609 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4612 'instance_vm_id': vm_uuid
,
4613 "instance_net_id": vm_iface
["instance_net_id"],
4614 'interface_id': vm_iface
['interface_id'],
4615 'type': vm_iface
['type'],
4616 'floating_ip': vm_iface
['floating_ip'],
4617 'port_security': vm_iface
['port_security']
4619 db_instance_interfaces
.append(db_vm_iface
)
4620 task_params_copy
= deepcopy(task_params
)
4621 for iface
in task_params_copy
[5]:
4622 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4623 # increment ip_address
4624 if "ip_address" in iface
:
4625 ip
= iface
.get("ip_address")
4630 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4631 iface
["ip_address"] = ip
4633 iface
["ip_address"] = None
4635 task_params_copy
[0] = vm_name
4637 "instance_action_id": instance_action_id
,
4638 "task_index": task_index
,
4639 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4641 "status": "SCHEDULED",
4642 "item": "instance_vms",
4646 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4649 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4652 db_vim_actions
.append(db_vim_action
)
4653 vm_result
["created"].append(vm_uuid
)
4655 db_instance_action
["number_tasks"] = task_index
4657 {"instance_vms": db_instance_vms
},
4658 {"instance_interfaces": db_instance_interfaces
},
4659 {"instance_actions": db_instance_action
},
4661 # {"instance_sfis": db_instance_sfis},
4662 # {"instance_sfs": db_instance_sfs},
4663 # {"instance_classifications": db_instance_classifications},
4664 # {"instance_sfps": db_instance_sfps},
4665 {"vim_wim_actions": db_vim_actions
}
4667 logger
.debug("create_vdu done DB tables: %s",
4668 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4669 mydb
.new_rows(db_tables
, [])
4670 for myvim_thread
in myvim_threads_id
.values():
4671 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4675 input_vnfs
= action_dict
.pop("vnfs", [])
4676 input_vms
= action_dict
.pop("vms", [])
4677 action_over_all
= True if not input_vnfs
and not input_vms
else False
4678 for sce_vnf
in instanceDict
['vnfs']:
4679 for vm
in sce_vnf
['vms']:
4680 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4681 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4682 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
:
4685 if "add_public_key" in action_dict
:
4687 if sce_vnf
.get('mgmt_access'):
4688 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'])
4689 ssh_access
= mgmt_access
['config-access']['ssh-access']
4690 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4692 if ssh_access
['required'] and ssh_access
['default-user']:
4693 if 'ip_address' in vm
:
4694 mgmt_ip
= vm
['ip_address'].split(';')
4695 password
= mgmt_access
['config-access'].get('password')
4696 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4697 myvim
.inject_user_key(mgmt_ip
[0], ssh_access
['default-user'],
4698 action_dict
['add_public_key'],
4699 password
=password
, ro_key
=priv_RO_key
)
4701 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4702 httperrors
.Internal_Server_Error
)
4704 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4705 httperrors
.Internal_Server_Error
)
4707 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4708 httperrors
.Internal_Server_Error
)
4710 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
4711 if "console" in action_dict
:
4712 if not global_config
["http_console_proxy"]:
4713 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4714 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4715 protocol
=data
["protocol"],
4716 ip
= data
["server"],
4717 port
= data
["port"],
4718 suffix
= data
["suffix"]),
4722 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
4723 vm_result
[ vm
['uuid'] ] = {"vim_result": -httperrors
.Unauthorized
,
4724 "description": "this console is only reachable by local interface",
4729 #print "console data", data
4731 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
4732 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4733 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4734 protocol
=data
["protocol"],
4735 ip
= global_config
["http_console_host"],
4736 port
= console_thread
.port
,
4737 suffix
= data
["suffix"]),
4741 except NfvoException
as e
:
4742 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4746 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
4748 except vimconn
.vimconnException
as e
:
4749 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
4752 if vm_ok
==0: #all goes wrong
4757 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
4759 if nfvo_tenant
and nfvo_tenant
!= "any":
4760 filter["tenant_id"] = nfvo_tenant
4761 if instance_id
and instance_id
!= "any":
4762 filter["instance_id"] = instance_id
4764 filter["uuid"] = action_id
4765 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
4768 raise NfvoException("Not found any action with this criteria", httperrors
.Not_Found
)
4769 vim_wim_actions
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
={"instance_action_id": action_id
})
4770 rows
[0]["vim_wim_actions"] = vim_wim_actions
4771 # for backward compatibility set vim_actions = vim_wim_actions
4772 rows
[0]["vim_actions"] = vim_wim_actions
4773 return {"actions": rows
}
4776 def create_or_use_console_proxy_thread(console_server
, console_port
):
4777 #look for a non-used port
4778 console_thread_key
= console_server
+ ":" + str(console_port
)
4779 if console_thread_key
in global_config
["console_thread"]:
4780 #global_config["console_thread"][console_thread_key].start_timeout()
4781 return global_config
["console_thread"][console_thread_key
]
4783 for port
in global_config
["console_port_iterator"]():
4784 #print "create_or_use_console_proxy_thread() port:", port
4785 if port
in global_config
["console_ports"]:
4788 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
4790 global_config
["console_thread"][console_thread_key
] = clithread
4791 global_config
["console_ports"][port
] = console_thread_key
4793 except cli
.ConsoleProxyExceptionPortUsed
as e
:
4794 #port used, try with onoher
4796 except cli
.ConsoleProxyException
as e
:
4797 raise NfvoException(str(e
), httperrors
.Bad_Request
)
4798 raise NfvoException("Not found any free 'http_console_ports'", httperrors
.Conflict
)
4801 def check_tenant(mydb
, tenant_id
):
4802 '''check that tenant exists at database'''
4803 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
4805 raise NfvoException("tenant '{}' not found".format(tenant_id
), httperrors
.Not_Found
)
4808 def new_tenant(mydb
, tenant_dict
):
4810 tenant_uuid
= str(uuid4())
4811 tenant_dict
['uuid'] = tenant_uuid
4813 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
4814 tenant_dict
['RO_pub_key'] = pub_key
4815 tenant_dict
['encrypted_RO_priv_key'] = priv_key
4816 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
4817 except db_base_Exception
as e
:
4818 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
4821 def delete_tenant(mydb
, tenant
):
4822 #get nfvo_tenant info
4824 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
4825 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
4826 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
4829 def new_datacenter(mydb
, datacenter_descriptor
):
4830 sdn_port_mapping
= None
4831 if "config" in datacenter_descriptor
:
4832 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
4833 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
4835 # Check that datacenter-type is correct
4836 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
4837 # module_info = None
4839 module
= "vimconn_" + datacenter_type
4840 pkg
= __import__("osm_ro." + module
)
4841 # vim_conn = getattr(pkg, module)
4842 # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]])
4843 except (IOError, ImportError):
4844 # if module_info and module_info[0]:
4845 # file.close(module_info[0])
4846 raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}.py' not installed".format(datacenter_type
,
4848 httperrors
.Bad_Request
)
4850 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
4851 if sdn_port_mapping
:
4853 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
4854 except Exception as e
:
4855 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
4857 return datacenter_id
4860 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
4861 # obtain data, check that only one exist
4862 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
4865 datacenter_id
= datacenter
['uuid']
4866 where
= {'uuid': datacenter
['uuid']}
4867 remove_port_mapping
= False
4868 new_sdn_port_mapping
= None
4869 if "config" in datacenter_descriptor
:
4870 if datacenter_descriptor
['config'] != None:
4872 new_config_dict
= datacenter_descriptor
["config"]
4873 if "sdn-port-mapping" in new_config_dict
:
4874 remove_port_mapping
= True
4875 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
4876 # delete null fields
4878 for k
in new_config_dict
:
4879 if new_config_dict
[k
] is None:
4881 if k
== 'sdn-controller':
4882 remove_port_mapping
= True
4884 config_text
= datacenter
.get("config")
4887 config_dict
= yaml
.load(config_text
)
4888 config_dict
.update(new_config_dict
)
4889 # delete null fields
4892 except Exception as e
:
4893 raise NfvoException("Bad format at datacenter:config " + str(e
), httperrors
.Bad_Request
)
4895 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
4897 datacenter_descriptor
["config"] = None
4898 if remove_port_mapping
:
4900 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
4901 except ovimException
as e
:
4902 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
4904 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
4905 if new_sdn_port_mapping
:
4907 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
4908 except ovimException
as e
:
4910 mydb
.update_rows('datacenters', datacenter
, where
)
4911 raise NfvoException("Error adding datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
4912 return datacenter_id
4915 def delete_datacenter(mydb
, datacenter
):
4916 #get nfvo_tenant info
4917 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
4918 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
4920 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
4921 except ovimException
as e
:
4922 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
))
4923 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
4926 def create_vim_account(mydb
, nfvo_tenant
, datacenter_id
, name
=None, vim_id
=None, vim_tenant
=None, vim_tenant_name
=None,
4927 vim_username
=None, vim_password
=None, config
=None):
4928 # get datacenter info
4930 if not datacenter_id
:
4932 raise NfvoException("You must provide 'vim_id", http_code
=httperrors
.Bad_Request
)
4933 datacenter_id
= vim_id
4934 datacenter_id
, datacenter_name
= get_datacenter_uuid(mydb
, None, datacenter_id
)
4936 create_vim_tenant
= True if not vim_tenant
and not vim_tenant_name
else False
4938 # get nfvo_tenant info
4939 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
4940 if vim_tenant_name
==None:
4941 vim_tenant_name
=tenant_dict
['name']
4943 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
4944 # #check that this association does not exist before
4945 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
4946 # if len(tenants_datacenters)>0:
4947 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
4949 vim_tenant_id_exist_atdb
=False
4950 if not create_vim_tenant
:
4951 where_
={"datacenter_id": datacenter_id
}
4952 if vim_tenant
!=None:
4953 where_
["vim_tenant_id"] = vim_tenant
4954 if vim_tenant_name
!=None:
4955 where_
["vim_tenant_name"] = vim_tenant_name
4956 #check if vim_tenant_id is already at database
4957 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
4958 if len(datacenter_tenants_dict
)>=1:
4959 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
4960 vim_tenant_id_exist_atdb
=True
4961 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
4963 datacenter_tenants_dict
= {}
4964 #insert at table datacenter_tenants
4965 else: #if vim_tenant==None:
4966 #create tenant at VIM if not provided
4968 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
,
4969 vim_passwd
=vim_password
)
4970 datacenter_name
= myvim
["name"]
4971 vim_tenant
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
4972 except vimconn
.vimconnException
as e
:
4973 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), httperrors
.Internal_Server_Error
)
4974 datacenter_tenants_dict
= {}
4975 datacenter_tenants_dict
["created"]="true"
4977 #fill datacenter_tenants table
4978 if not vim_tenant_id_exist_atdb
:
4979 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant
4980 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
4981 datacenter_tenants_dict
["user"] = vim_username
4982 datacenter_tenants_dict
["passwd"] = vim_password
4983 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
4985 datacenter_tenants_dict
["name"] = name
4987 datacenter_tenants_dict
["name"] = datacenter_name
4989 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
4990 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
4991 datacenter_tenants_dict
["uuid"] = id_
4993 #fill tenants_datacenters table
4994 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
4995 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
4996 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
4999 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
, tenant_dict
['name'], tenant_dict
['uuid'])
5000 new_thread
= vim_thread
.vim_thread(task_lock
, thread_name
, datacenter_name
, datacenter_tenant_id
,
5001 db
=db
, db_lock
=db_lock
, ovim
=ovim
)
5003 thread_id
= datacenter_tenants_dict
["uuid"]
5004 vim_threads
["running"][thread_id
] = new_thread
5006 except vimconn
.vimconnException
as e
:
5007 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5010 def edit_vim_account(mydb
, nfvo_tenant
, datacenter_tenant_id
, datacenter_id
=None, name
=None, vim_tenant
=None,
5011 vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
5013 # get vim_account; check is valid for this tenant
5014 from_
= "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5015 where_
= {"td.nfvo_tenant_id": nfvo_tenant
}
5016 if datacenter_tenant_id
:
5017 where_
["dt.uuid"] = datacenter_tenant_id
5019 where_
["dt.datacenter_id"] = datacenter_id
5020 vim_accounts
= mydb
.get_rows(SELECT
="dt.uuid as uuid, config", FROM
=from_
, WHERE
=where_
)
5021 if not vim_accounts
:
5022 raise NfvoException("vim_account not found for this tenant", http_code
=httperrors
.Not_Found
)
5023 elif len(vim_accounts
) > 1:
5024 raise NfvoException("found more than one vim_account for this tenant", http_code
=httperrors
.Conflict
)
5025 datacenter_tenant_id
= vim_accounts
[0]["uuid"]
5026 original_config
= vim_accounts
[0]["config"]
5030 original_config_dict
= yaml
.load(original_config
)
5031 original_config_dict
.update(config
)
5032 update
["config"] = yaml
.safe_dump(original_config_dict
, default_flow_style
=True, width
=256)
5034 update_
['name'] = name
5036 update_
['vim_tenant_id'] = vim_tenant
5038 update_
['vim_tenant_name'] = vim_tenant_name
5040 update_
['user'] = vim_username
5042 update_
['passwd'] = vim_password
5044 mydb
.update_rows("datacenter_tenants", UPDATE
=update_
, WHERE
={"uuid": datacenter_tenant_id
})
5046 vim_threads
["running"][datacenter_tenant_id
].insert_task("reload")
5047 return datacenter_tenant_id
5049 def delete_vim_account(mydb
, tenant_id
, vim_account_id
, datacenter
=None):
5050 #get nfvo_tenant info
5051 if not tenant_id
or tenant_id
=="any":
5054 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
5055 tenant_uuid
= tenant_dict
['uuid']
5057 #check that this association exist before
5058 tenants_datacenter_dict
= {}
5060 datacenter_id
, _
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
5061 tenants_datacenter_dict
["datacenter_id"] = datacenter_id
5062 elif vim_account_id
:
5063 tenants_datacenter_dict
["datacenter_tenant_id"] = vim_account_id
5065 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
5066 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5067 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
5068 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), httperrors
.Not_Found
)
5070 #delete this association
5071 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5073 #get vim_tenant info and deletes
5075 for tenant_datacenter_item
in tenant_datacenter_list
:
5076 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5077 #try to delete vim:tenant
5079 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5080 if vim_tenant_dict
['created']=='true':
5081 #delete tenant at VIM if created by NFVO
5083 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5084 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
5085 except vimconn
.vimconnException
as e
:
5086 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
5087 logger
.warn(warning
)
5088 except db_base_Exception
as e
:
5089 logger
.error("Cannot delete datacenter_tenants " + str(e
))
5090 pass # the error will be caused because dependencies, vim_tenant can not be deleted
5091 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
5092 thread
= vim_threads
["running"].get(thread_id
)
5094 thread
.insert_task("exit")
5095 vim_threads
["deleting"][thread_id
] = thread
5096 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
5099 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
5101 #get datacenter info
5102 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5104 if 'net-update' in action_dict
:
5106 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
5108 except vimconn
.vimconnException
as e
:
5109 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5110 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5111 #update nets Change from VIM format to NFVO format
5114 net_nfvo
={'datacenter_id': datacenter_id
}
5115 net_nfvo
['name'] = net
['name']
5116 #net_nfvo['description']= net['name']
5117 net_nfvo
['vim_net_id'] = net
['id']
5118 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5119 net_nfvo
['shared'] = net
['shared']
5120 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5121 net_list
.append(net_nfvo
)
5122 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
5123 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
5125 elif 'net-edit' in action_dict
:
5126 net
= action_dict
['net-edit'].pop('net')
5127 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5128 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
5129 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5131 elif 'net-delete' in action_dict
:
5132 net
= action_dict
['net-deelte'].get('net')
5133 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5134 result
= mydb
.delete_row(FROM
='datacenter_nets',
5135 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5139 raise NfvoException("Unknown action " + str(action_dict
), httperrors
.Bad_Request
)
5142 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
5143 #get datacenter info
5144 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5146 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
5147 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
5148 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
5152 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
5153 #get datacenter info
5154 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5157 action_dict
= action_dict
["netmap"]
5158 if 'vim_id' in action_dict
:
5159 filter_dict
["id"] = action_dict
['vim_id']
5160 if 'vim_name' in action_dict
:
5161 filter_dict
["name"] = action_dict
['vim_name']
5163 filter_dict
["shared"] = True
5166 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
5167 except vimconn
.vimconnException
as e
:
5168 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5169 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5170 if len(vim_nets
)>1 and action_dict
:
5171 raise NfvoException("more than two networks found, specify with vim_id", httperrors
.Conflict
)
5172 elif len(vim_nets
)==0: # and action_dict:
5173 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), httperrors
.Not_Found
)
5175 for net
in vim_nets
:
5176 net_nfvo
={'datacenter_id': datacenter_id
}
5177 if action_dict
and "name" in action_dict
:
5178 net_nfvo
['name'] = action_dict
['name']
5180 net_nfvo
['name'] = net
['name']
5181 #net_nfvo['description']= net['name']
5182 net_nfvo
['vim_net_id'] = net
['id']
5183 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5184 net_nfvo
['shared'] = net
['shared']
5185 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5187 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
5188 net_nfvo
["status"] = "OK"
5189 net_nfvo
["uuid"] = net_id
5190 except db_base_Exception
as e
:
5194 net_nfvo
["status"] = "FAIL: " + str(e
)
5195 net_list
.append(net_nfvo
)
5198 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
5199 # obtain all network data
5201 if utils
.check_valid_uuid(network_id
):
5202 filter_dict
= {"id": network_id
}
5204 filter_dict
= {"name": network_id
}
5206 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5207 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
5208 except vimconn
.vimconnException
as e
:
5209 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
5211 # ensure the network is defined
5212 if len(network
) == 0:
5213 raise NfvoException("Network {} is not present in the system".format(network_id
),
5214 httperrors
.Bad_Request
)
5216 # ensure there is only one network with the provided name
5217 if len(network
) > 1:
5218 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), httperrors
.Bad_Request
)
5220 # ensure it is a dataplane network
5221 if network
[0]['type'] != 'data':
5224 # ensure we use the id
5225 network_id
= network
[0]['id']
5227 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5228 # and with instance_scenario_id==NULL
5229 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5230 search_dict
= {'vim_net_id': network_id
}
5233 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5234 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5235 except db_base_Exception
as e
:
5236 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5237 network_id
) + str(e
), e
.http_code
)
5241 if net
['sdn_net_id'] != None:
5243 sdn_net_id
= net
['sdn_net_id']
5245 if sdn_net_counter
== 0:
5247 elif sdn_net_counter
== 1:
5250 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5251 network_id
), httperrors
.Internal_Server_Error
)
5253 def get_sdn_controller_id(mydb
, datacenter
):
5254 # Obtain sdn controller id
5255 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5259 return yaml
.load(config
).get('sdn-controller')
5261 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5263 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5264 if not sdn_network_id
:
5265 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), httperrors
.Internal_Server_Error
)
5267 #Obtain sdn controller id
5268 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5269 if not controller_id
:
5270 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), httperrors
.Internal_Server_Error
)
5272 #Obtain sdn controller info
5273 sdn_controller
= ovim
.show_of_controller(controller_id
)
5276 'name': 'external_port',
5277 'net_id': sdn_network_id
,
5278 'ofc_id': controller_id
,
5279 'switch_dpid': sdn_controller
['dpid'],
5280 'switch_port': descriptor
['port']
5283 if 'vlan' in descriptor
:
5284 port_data
['vlan'] = descriptor
['vlan']
5285 if 'mac' in descriptor
:
5286 port_data
['mac'] = descriptor
['mac']
5288 result
= ovim
.new_port(port_data
)
5289 except ovimException
as e
:
5290 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5291 sdn_network_id
, network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5292 except db_base_Exception
as e
:
5293 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5294 network_id
) + str(e
), e
.http_code
)
5296 return 'Port uuid: '+ result
5298 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5300 filter = {'uuid': port_id
}
5302 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5303 if not sdn_network_id
:
5304 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5305 httperrors
.Internal_Server_Error
)
5306 #in case no port_id is specified only ports marked as 'external_port' will be detached
5307 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5310 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5311 except ovimException
as e
:
5312 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5313 httperrors
.Internal_Server_Error
)
5315 if len(port_list
) == 0:
5316 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5317 httperrors
.Bad_Request
)
5320 for port
in port_list
:
5322 port_uuid_list
.append(port
['uuid'])
5323 ovim
.delete_port(port
['uuid'])
5324 except ovimException
as e
:
5325 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5327 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5329 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5330 #get datacenter info
5331 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5334 if utils
.check_valid_uuid(name
):
5335 filter_dict
["id"] = name
5337 filter_dict
["name"] = name
5339 if item
=="networks":
5340 #filter_dict['tenant_id'] = myvim['tenant_id']
5341 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5343 if len(content
) == 0:
5344 raise NfvoException("Network {} is not present in the system. ".format(name
),
5345 httperrors
.Bad_Request
)
5347 #Update the networks with the attached ports
5349 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5350 if sdn_network_id
!= None:
5352 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5353 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5354 except ovimException
as e
:
5355 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5356 #Remove field name and if port name is external_port save it as 'type'
5357 for port
in port_list
:
5358 if port
['name'] == 'external_port':
5359 port
['type'] = "External"
5361 net
['sdn_network_id'] = sdn_network_id
5362 net
['sdn_attached_ports'] = port_list
5364 elif item
=="tenants":
5365 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5366 elif item
== "images":
5368 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5370 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5371 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5372 if name
and len(content
)==1:
5373 return {item
[:-1]: content
[0]}
5374 elif name
and len(content
)==0:
5375 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.iteritems())),
5378 return {item
: content
}
5379 except vimconn
.vimconnException
as e
:
5380 print "vim_action Not possible to get_%s_list from VIM: %s " % (item
, str(e
))
5381 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5384 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5385 #get datacenter info
5386 if tenant_id
== "any":
5389 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5391 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5392 logger
.debug("vim_action_delete vim response: " + str(content
))
5393 items
= content
.values()[0]
5394 if type(items
)==list and len(items
)==0:
5395 raise NfvoException("Not found " + item
, httperrors
.Not_Found
)
5396 elif type(items
)==list and len(items
)>1:
5397 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), httperrors
.Not_Found
)
5398 else: # it is a dict
5399 item_id
= items
["id"]
5400 item_name
= str(items
.get("name"))
5403 if item
=="networks":
5404 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5405 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5406 if sdn_network_id
!= None:
5407 #Delete any port attachment to this network
5409 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5410 except ovimException
as e
:
5411 raise NfvoException(
5412 "ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5413 httperrors
.Internal_Server_Error
)
5415 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5416 for port
in port_list
:
5417 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5419 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5421 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id
, 'vim_net_id': item_id
})
5422 except db_base_Exception
as e
:
5423 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence
) +
5424 str(e
), e
.http_code
)
5426 #Delete the SDN network
5428 ovim
.delete_network(sdn_network_id
)
5429 except ovimException
as e
:
5430 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5431 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5432 httperrors
.Internal_Server_Error
)
5434 content
= myvim
.delete_network(item_id
)
5435 elif item
=="tenants":
5436 content
= myvim
.delete_tenant(item_id
)
5437 elif item
== "images":
5438 content
= myvim
.delete_image(item_id
)
5440 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5441 except vimconn
.vimconnException
as e
:
5442 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5443 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5445 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5448 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5449 #get datacenter info
5450 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5451 if tenant_id
== "any":
5453 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5455 if item
=="networks":
5456 net
= descriptor
["network"]
5457 net_name
= net
.pop("name")
5458 net_type
= net
.pop("type", "bridge")
5459 net_public
= net
.pop("shared", False)
5460 net_ipprofile
= net
.pop("ip_profile", None)
5461 net_vlan
= net
.pop("vlan", None)
5462 content
, _
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, vlan
=net_vlan
) #, **net)
5464 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5465 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5466 #obtain datacenter_tenant_id
5467 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5468 FROM
='datacenter_tenants',
5469 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5472 sdn_network
['vlan'] = net_vlan
5473 sdn_network
['type'] = net_type
5474 sdn_network
['name'] = net_name
5475 sdn_network
['region'] = datacenter_tenant_id
5476 ovim_content
= ovim
.new_network(sdn_network
)
5477 except ovimException
as e
:
5478 logger
.error("ovimException creating SDN network={} ".format(
5479 sdn_network
) + str(e
), exc_info
=True)
5480 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5481 httperrors
.Internal_Server_Error
)
5483 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5484 # use instance_scenario_id=None to distinguish from real instaces of nets
5485 correspondence
= {'instance_scenario_id': None,
5486 'sdn_net_id': ovim_content
,
5487 'vim_net_id': content
,
5488 'datacenter_tenant_id': datacenter_tenant_id
5491 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5492 except db_base_Exception
as e
:
5493 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5494 correspondence
, e
), e
.http_code
)
5495 elif item
=="tenants":
5496 tenant
= descriptor
["tenant"]
5497 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5499 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5500 except vimconn
.vimconnException
as e
:
5501 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5503 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5505 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5506 data
= ovim
.new_of_controller(sdn_controller
)
5507 logger
.debug('New SDN controller created with uuid {}'.format(data
))
5510 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5511 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5512 msg
= 'SDN controller {} updated'.format(data
)
5516 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5517 if controller_id
== None:
5518 data
= ovim
.get_of_controllers()
5520 data
= ovim
.show_of_controller(controller_id
)
5522 msg
= 'SDN controller list:\n {}'.format(data
)
5526 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5527 select_
= ('uuid', 'config')
5528 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5529 for datacenter
in datacenters
:
5530 if datacenter
['config']:
5531 config
= yaml
.load(datacenter
['config'])
5532 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5533 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), httperrors
.Conflict
)
5535 data
= ovim
.delete_of_controller(controller_id
)
5536 msg
= 'SDN controller {} deleted'.format(data
)
5540 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5541 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5542 if len(controller
) < 1:
5543 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), httperrors
.Not_Found
)
5546 sdn_controller_id
= yaml
.load(controller
[0]["config"])["sdn-controller"]
5548 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), httperrors
.Bad_Request
)
5550 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5551 switch_dpid
= sdn_controller
["dpid"]
5554 for compute_node
in sdn_port_mapping
:
5555 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5557 element
["compute_node"] = compute_node
["compute_node"]
5558 for port
in compute_node
["ports"]:
5559 pci
= port
.get("pci")
5560 element
["switch_port"] = port
.get("switch_port")
5561 element
["switch_mac"] = port
.get("switch_mac")
5562 if not element
["switch_port"] and not element
["switch_mac"]:
5563 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors
.Bad_Request
)
5564 for pci_expanded
in utils
.expand_brackets(pci
):
5565 element
["pci"] = pci_expanded
5566 maps
.append(dict(element
))
5568 return ovim
.set_of_port_mapping(maps
, ofc_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, region
=datacenter_id
)
5570 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5571 maps
= ovim
.get_of_port_mappings(db_filter
={"region": datacenter_id
})
5574 "sdn-controller": None,
5575 "datacenter-id": datacenter_id
,
5577 "ports_mapping": list()
5580 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5581 if datacenter
['config']:
5582 config
= yaml
.load(datacenter
['config'])
5583 if 'sdn-controller' in config
:
5584 controller_id
= config
['sdn-controller']
5585 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5586 result
["sdn-controller"] = controller_id
5587 result
["dpid"] = sdn_controller
["dpid"]
5589 if result
["sdn-controller"] == None:
5590 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), httperrors
.Bad_Request
)
5591 if result
["dpid"] == None:
5592 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5593 httperrors
.Internal_Server_Error
)
5598 ports_correspondence_dict
= dict()
5600 if result
["sdn-controller"] != link
["ofc_id"]:
5601 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5602 if result
["dpid"] != link
["switch_dpid"]:
5603 raise NfvoException("The dpid specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5605 element
["pci"] = link
["pci"]
5606 if link
["switch_port"]:
5607 element
["switch_port"] = link
["switch_port"]
5608 if link
["switch_mac"]:
5609 element
["switch_mac"] = link
["switch_mac"]
5611 if not link
["compute_node"] in ports_correspondence_dict
:
5613 content
["compute_node"] = link
["compute_node"]
5614 content
["ports"] = list()
5615 ports_correspondence_dict
[link
["compute_node"]] = content
5617 ports_correspondence_dict
[link
["compute_node"]]["ports"].append(element
)
5619 for key
in sorted(ports_correspondence_dict
):
5620 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5624 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5625 return ovim
.clear_of_port_mapping(db_filter
={"region":datacenter_id
})
5627 def create_RO_keypair(tenant_id
):
5629 Creates a public / private keys for a RO tenant and returns their values
5631 tenant_id: ID of the tenant
5633 public_key: Public key for the RO tenant
5634 private_key: Encrypted private key for RO tenant
5638 key
= RSA
.generate(bits
)
5640 public_key
= key
.publickey().exportKey('OpenSSH')
5641 if isinstance(public_key
, ValueError):
5642 raise NfvoException("Unable to create public key: {}".format(public_key
), httperrors
.Internal_Server_Error
)
5643 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5644 except (ValueError, NameError) as e
:
5645 raise NfvoException("Unable to create private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5646 return public_key
, private_key
5648 def decrypt_key (key
, tenant_id
):
5650 Decrypts an encrypted RSA key
5652 key: Private key to be decrypted
5653 tenant_id: ID of the tenant
5655 unencrypted_key: Unencrypted private key for RO tenant
5658 key
= RSA
.importKey(key
,tenant_id
)
5659 unencrypted_key
= key
.exportKey('PEM')
5660 if isinstance(unencrypted_key
, ValueError):
5661 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), httperrors
.Internal_Server_Error
)
5662 except ValueError as e
:
5663 raise NfvoException("Unable to decrypt the private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5664 return unencrypted_key