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$"
33 from random
import choice
as random_choice
34 from osm_ro
import utils
35 from osm_ro
.utils
import deprecated
36 from osm_ro
.vim_thread
import vim_thread
37 import osm_ro
.console_proxy_thread
as cli
38 from osm_ro
import vimconn
42 from uuid
import uuid4
43 from osm_ro
.db_base
import db_base_Exception
45 from osm_ro
import nfvo_db
46 from threading
import Lock
49 from osm_ro
.sdn
import Sdn
, SdnException
as ovimException
50 # from lib_osm_openvim.ovim import ovimException
51 # from unittest.mock import MagicMock
52 # class ovimException(Exception):
56 from Crypto
.PublicKey
import RSA
58 import osm_im
.vnfd
as vnfd_catalog
59 import osm_im
.nsd
as nsd_catalog
60 from pyangbind
.lib
.serialise
import pybindJSONDecoder
61 from copy
import deepcopy
62 from pkg_resources
import iter_entry_points
66 from .wim
import sdnconn
67 from .wim
.wimconn_dummy
import DummyConnector
68 from .wim
.failing_connector
import FailingConnector
69 from .http_tools
import errors
as httperrors
70 from .wim
.engine
import WimEngine
71 from .wim
.persistence
import WimPersistence
72 from copy
import deepcopy
73 from pprint
import pformat
80 global sdnconn_imported
83 global default_volume_size
84 default_volume_size
= '5' #size in GB
89 plugins
= {} # dictionary with VIM type as key, loaded module as value
90 vim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
91 vim_persistent_info
= {}
93 sdnconn_imported
= {} # dictionary with WIM type as key, loaded module as value
94 wim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
95 wim_persistent_info
= {}
98 logger
= logging
.getLogger('openmano.nfvo')
106 class NfvoException(httperrors
.HttpMappedError
):
107 """Common Class for NFVO errors"""
109 def _load_plugin(name
, type="vim"):
110 # type can be vim or sdn
113 for v
in iter_entry_points('osm_ro{}.plugins'.format(type), name
):
114 plugins
[name
] = v
.load()
115 except Exception as e
:
116 logger
.critical("Cannot load osm_{}: {}".format(name
, e
))
118 plugins
[name
] = FailingConnector("Cannot load osm_{}: {}".format(name
, e
))
119 if name
and name
not in plugins
:
120 error_text
= "Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been" \
121 " registered".format(t
=type, n
=name
)
122 logger
.critical(error_text
)
123 plugins
[name
] = FailingConnector(error_text
)
124 # raise NfvoException("Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been registered".
125 # format(t=type, n=name), httperrors.Bad_Request)
130 if task_id
<= last_task_id
:
131 task_id
= last_task_id
+ 0.000001
132 last_task_id
= task_id
133 return "ACTION-{:.6f}".format(task_id
)
134 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
137 def new_task(name
, params
, depends
=None):
139 task_id
= get_task_id()
140 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
142 task
["depends"] = depends
147 return True if id[:5] == "TASK-" else False
149 def get_process_id():
151 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
152 will provide a random one
155 # Try getting docker id. If fails, get pid
157 with
open("/proc/self/cgroup", "r") as f
:
158 text_id_
= f
.readline()
159 _
, _
, text_id
= text_id_
.rpartition("/")
160 text_id
= text_id
.replace("\n", "")[:12]
166 return "".join(random_choice("0123456789abcdef") for _
in range(12))
168 def get_non_used_vim_name(datacenter_name
, datacenter_id
):
169 return "{}:{}:{}".format(
170 worker_id
[:12], datacenter_id
.replace("-", "")[:32], datacenter_name
[:16]
174 def get_non_used_wim_name(wim_name
, wim_id
, tenant_name
, tenant_id
):
176 if name
not in wim_threads
["names"]:
177 wim_threads
["names"].append(name
)
179 name
= wim_name
[:16] + "." + tenant_name
[:16]
180 if name
not in wim_threads
["names"]:
181 wim_threads
["names"].append(name
)
183 name
= wim_id
+ "-" + tenant_id
184 wim_threads
["names"].append(name
)
188 def start_service(mydb
, persistence
=None, wim
=None):
189 global db
, global_config
, plugins
, ovim
, worker_id
190 db
= nfvo_db
.nfvo_db(lock
=db_lock
)
192 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
194 persistence
= persistence
or WimPersistence(db
)
197 worker_id
= get_process_id()
198 if "rosdn_dummy" not in plugins
:
199 plugins
["rosdn_dummy"] = DummyConnector
200 # starts ovim library
201 ovim
= Sdn(db
, plugins
)
204 wim_engine
= wim
or WimEngine(persistence
, plugins
)
205 wim_engine
.ovim
= ovim
209 #delete old unneeded vim_wim_actions
213 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
214 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
215 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
216 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
217 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
218 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
219 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
221 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
222 'datacenter_id': vim
.get('datacenter_id')}
224 extra
.update(yaml
.load(vim
["config"], Loader
=yaml
.Loader
))
225 if vim
.get('dt_config'):
226 extra
.update(yaml
.load(vim
["dt_config"], Loader
=yaml
.Loader
))
227 plugin_name
= "rovim_" + vim
["type"]
228 if plugin_name
not in plugins
:
229 _load_plugin(plugin_name
, type="vim")
231 thread_id
= vim
['datacenter_tenant_id']
232 vim_persistent_info
[thread_id
] = {}
235 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
236 myvim
= plugins
[plugin_name
].vimconnector(
237 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
238 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
239 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
240 user
=vim
['user'], passwd
=vim
['passwd'],
241 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
243 except vimconn
.vimconnException
as e
:
245 logger
.error("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
246 vim
['datacenter_id'], e
))
247 except Exception as e
:
248 logger
.critical("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
249 vim
['datacenter_id'], e
))
250 # raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
251 # httperrors.Internal_Server_Error)
252 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['datacenter_id'])
253 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, None,
254 vim
['datacenter_tenant_id'], db
=db
)
256 vim_threads
["running"][thread_id
] = new_thread
257 wims
= mydb
.get_rows(FROM
="wim_accounts join wims on wim_accounts.wim_id=wims.uuid",
258 WHERE
={"sdn": "true"},
259 SELECT
=("wim_accounts.uuid as uuid", "type", "wim_accounts.name as name"))
261 plugin_name
= "rosdn_" + wim
["type"]
262 if plugin_name
not in plugins
:
263 _load_plugin(plugin_name
, type="sdn")
265 thread_id
= wim
['uuid']
266 thread_name
= get_non_used_vim_name(wim
['name'], wim
['uuid'])
267 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, wim
['uuid'], None, db
=db
)
269 vim_threads
["running"][thread_id
] = new_thread
270 wim_engine
.start_threads()
271 except db_base_Exception
as e
:
272 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
273 except ovimException
as e
:
275 if message
[:22] == "DATABASE wrong version":
276 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
277 "at host {dbhost}".format(
278 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
279 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
280 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
281 raise NfvoException(message
, httperrors
.Bad_Request
)
285 global ovim
, global_config
288 for thread_id
, thread
in vim_threads
["running"].items():
289 thread
.insert_task("exit")
290 vim_threads
["deleting"][thread_id
] = thread
291 vim_threads
["running"] = {}
294 wim_engine
.stop_threads()
296 if global_config
and global_config
.get("console_thread"):
297 for thread
in global_config
["console_thread"]:
298 thread
.terminate
= True
301 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
302 global_config
["version_date"] ))
306 Clean unused or old entries at database to avoid unlimited growing
307 :param mydb: database connector
310 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
311 now
= t
.time()-3600*24*7
312 instance_action_id
= None
315 actions_to_delete
= mydb
.get_rows(
316 SELECT
=("item", "item_id", "instance_action_id"),
317 FROM
="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
318 "left join instance_scenarios as i on ia.instance_id=i.uuid",
319 WHERE
={"va.action": "DELETE", "va.modified_at<": now
, "i.uuid": None,
320 "va.status": ("DONE", "SUPERSEDED")},
323 for to_delete
in actions_to_delete
:
324 mydb
.delete_row(FROM
="vim_wim_actions", WHERE
=to_delete
)
325 if instance_action_id
!= to_delete
["instance_action_id"]:
326 instance_action_id
= to_delete
["instance_action_id"]
327 mydb
.delete_row(FROM
="instance_actions", WHERE
={"uuid": instance_action_id
})
328 nb_deleted
+= len(actions_to_delete
)
329 if len(actions_to_delete
) < 100:
332 mydb
.update_rows("vim_wim_actions", UPDATE
={"worker": None}, WHERE
={"worker<>": None})
335 logger
.debug("Removed {} unused vim_wim_actions".format(nb_deleted
))
338 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
340 return result, content:
341 <0, error_text upon error
342 nb_records, flavor_list on success
345 WHERE_dict
['vnf_id'] = vnf_id
346 if nfvo_tenant
is not None:
347 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
349 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
350 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
351 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
352 #print "get_flavor_list result:", result
353 #print "get_flavor_list content:", content
355 for flavor
in flavors
:
356 flavorList
.append(flavor
['flavor_id'])
360 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
362 Get used images of all vms belonging to this VNFD
363 :param mydb: database conector
364 :param vnf_id: vnfd uuid
365 :param nfvo_tenant: tenant, not used
366 :return: The list of image uuid used
369 vms
= mydb
.get_rows(SELECT
=('image_id','image_list'), FROM
='vms', WHERE
={'vnf_id': vnf_id
})
371 if vm
["image_id"] and vm
["image_id"] not in image_list
:
372 image_list
.append(vm
["image_id"])
374 vm_image_list
= yaml
.load(vm
["image_list"], Loader
=yaml
.Loader
)
375 for image_dict
in vm_image_list
:
376 if image_dict
["image_id"] not in image_list
:
377 image_list
.append(image_dict
["image_id"])
381 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
382 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None, ignore_errors
=False):
383 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
384 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
385 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
386 raise exception upon error
390 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
391 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
392 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
393 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
394 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
395 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
396 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
397 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'
398 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
399 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
400 'user','passwd', 'dt.config as dt_config')
402 from_
= 'datacenters as d'
403 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
405 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
408 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
409 'datacenter_id': vim
.get('datacenter_id'),
410 '_vim_type_internal': vim
.get('type')}
412 extra
.update(yaml
.load(vim
["config"], Loader
=yaml
.Loader
))
413 if vim
.get('dt_config'):
414 extra
.update(yaml
.load(vim
["dt_config"], Loader
=yaml
.Loader
))
415 plugin_name
= "rovim_" + vim
["type"]
416 if plugin_name
not in plugins
:
418 _load_plugin(plugin_name
, type="vim")
419 except NfvoException
as e
:
421 logger
.error("{}".format(e
))
426 if 'datacenter_tenant_id' in vim
:
427 thread_id
= vim
["datacenter_tenant_id"]
428 if thread_id
not in vim_persistent_info
:
429 vim_persistent_info
[thread_id
] = {}
430 persistent_info
= vim_persistent_info
[thread_id
]
434 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
435 vim_dict
[vim
['datacenter_id']] = plugins
[plugin_name
].vimconnector(
436 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
437 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
438 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
439 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
440 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
441 config
=extra
, persistent_info
=persistent_info
443 except Exception as e
:
445 logger
.error("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)))
447 http_code
= httperrors
.Internal_Server_Error
448 if isinstance(e
, vimconn
.vimconnException
):
449 http_code
= e
.http_code
450 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), http_code
)
452 except db_base_Exception
as e
:
453 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
456 def rollback(mydb
, vims
, rollback_list
):
458 #delete things by reverse order
459 for i
in range(len(rollback_list
)-1, -1, -1):
460 item
= rollback_list
[i
]
461 if item
["where"]=="vim":
462 if item
["vim_id"] not in vims
:
464 if is_task_id(item
["uuid"]):
466 vim
= vims
[item
["vim_id"]]
468 if item
["what"]=="image":
469 vim
.delete_image(item
["uuid"])
470 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
471 elif item
["what"]=="flavor":
472 vim
.delete_flavor(item
["uuid"])
473 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
474 elif item
["what"]=="network":
475 vim
.delete_network(item
["uuid"])
476 elif item
["what"]=="vm":
477 vim
.delete_vminstance(item
["uuid"])
478 except vimconn
.vimconnException
as e
:
479 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
480 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
481 except db_base_Exception
as e
:
482 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
486 if item
["what"]=="image":
487 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
488 elif item
["what"]=="flavor":
489 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
490 except db_base_Exception
as e
:
491 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
492 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
493 if len(undeleted_items
)==0:
494 return True," Rollback successful."
496 return False," Rollback fails to delete: " + str(undeleted_items
)
499 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
501 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
503 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
505 #dataplane interfaces
506 for numa
in vnfc
.get("numas",() ):
507 for interface
in numa
.get("interfaces",()):
508 if interface
["name"] in name_dict
:
510 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
511 vnfc
["name"], interface
["name"]),
512 httperrors
.Bad_Request
)
513 name_dict
[ interface
["name"] ] = "underlay"
515 for interface
in vnfc
.get("bridge-ifaces",() ):
516 if interface
["name"] in name_dict
:
518 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
519 vnfc
["name"], interface
["name"]),
520 httperrors
.Bad_Request
)
521 name_dict
[ interface
["name"] ] = "overlay"
522 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
523 # check bood-data info
524 # if "boot-data" in vnfc:
525 # # check that user-data is incompatible with users and config-files
526 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
527 # raise NfvoException(
528 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
529 # httperrors.Bad_Request)
531 #check if the info in external_connections matches with the one in the vnfcs
533 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
534 if external_connection
["name"] in name_list
:
536 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
537 external_connection
["name"]),
538 httperrors
.Bad_Request
)
539 name_list
.append(external_connection
["name"])
540 if external_connection
["VNFC"] not in vnfc_interfaces
:
542 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
543 external_connection
["name"], external_connection
["VNFC"]),
544 httperrors
.Bad_Request
)
546 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
548 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
549 external_connection
["name"],
550 external_connection
["local_iface_name"]),
551 httperrors
.Bad_Request
)
553 #check if the info in internal_connections matches with the one in the vnfcs
555 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
556 if internal_connection
["name"] in name_list
:
558 "Error at vnf:internal-connections:name, value '{}' already used as an internal-connection".format(
559 internal_connection
["name"]),
560 httperrors
.Bad_Request
)
561 name_list
.append(internal_connection
["name"])
562 #We should check that internal-connections of type "ptp" have only 2 elements
564 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
566 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
567 internal_connection
["name"],
568 'ptp' if vnf_descriptor_version
==1 else 'e-line',
569 'data' if vnf_descriptor_version
==1 else "e-lan"),
570 httperrors
.Bad_Request
)
571 for port
in internal_connection
["elements"]:
573 iface
= port
["local_iface_name"]
574 if vnf
not in vnfc_interfaces
:
576 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
577 internal_connection
["name"], vnf
),
578 httperrors
.Bad_Request
)
579 if iface
not in vnfc_interfaces
[ vnf
]:
581 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
582 internal_connection
["name"], iface
),
583 httperrors
.Bad_Request
)
584 return -httperrors
.Bad_Request
,
585 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
586 if vnfc_interfaces
[vnf
][iface
] == "overlay":
587 internal_connection
["type"] = "bridge"
589 internal_connection
["type"] = "data"
590 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
591 if vnfc_interfaces
[vnf
][iface
] == "overlay":
592 internal_connection
["implementation"] = "overlay"
594 internal_connection
["implementation"] = "underlay"
595 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
596 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
598 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
599 internal_connection
["name"],
600 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
601 'data' if vnf_descriptor_version
==1 else 'underlay'),
602 httperrors
.Bad_Request
)
603 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
604 vnfc_interfaces
[vnf
][iface
] == "underlay":
606 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
607 internal_connection
["name"], iface
,
608 'data' if vnf_descriptor_version
==1 else 'underlay',
609 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
610 httperrors
.Bad_Request
)
613 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
615 if only_create_at_vim
:
616 image_mano_id
= image_dict
['uuid']
617 if return_on_error
== None:
618 return_on_error
= True
620 if image_dict
['location']:
621 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
623 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
625 image_mano_id
= images
[0]['uuid']
627 #create image in MANO DB
628 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
629 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
630 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
632 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
633 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
634 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
635 #create image at every vim
636 for vim_id
,vim
in vims
.items():
637 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
638 image_created
="false"
640 image_db
= mydb
.get_rows(FROM
="datacenters_images",
641 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
642 #look at VIM if this image exist
644 if image_dict
['location'] is not None:
645 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
648 filter_dict
['name'] = image_dict
['universal_name']
649 if image_dict
.get('checksum') != None:
650 filter_dict
['checksum'] = image_dict
['checksum']
651 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
652 vim_images
= vim
.get_image_list(filter_dict
)
653 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
654 if len(vim_images
) > 1:
655 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), httperrors
.Conflict
)
656 elif len(vim_images
) == 0:
657 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
659 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
660 image_vim_id
= vim_images
[0]['id']
662 except vimconn
.vimconnNotFoundException
as e
:
663 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
665 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
666 if image_dict
['location']:
667 image_vim_id
= vim
.new_image(image_dict
)
668 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
671 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
672 raise vimconn
.vimconnException(str(e
))
673 except vimconn
.vimconnException
as e
:
675 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
678 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
680 except vimconn
.vimconnException
as e
:
682 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
684 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
687 #if we reach here, the image has been created or existed
689 #add new vim_id at datacenters_images
690 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
691 'image_id':image_mano_id
,
692 'vim_id': image_vim_id
,
693 'created':image_created
})
694 elif image_db
[0]["vim_id"]!=image_vim_id
:
695 #modify existing vim_id at datacenters_images
696 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
698 return image_vim_id
if only_create_at_vim
else image_mano_id
701 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
702 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',0),
703 'ram':flavor_dict
.get('ram'),
704 'vcpus':flavor_dict
.get('vcpus'),
706 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
707 del flavor_dict
['extended']
708 if 'extended' in flavor_dict
:
709 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
711 #look if flavor exist
712 if only_create_at_vim
:
713 flavor_mano_id
= flavor_dict
['uuid']
714 if return_on_error
== None:
715 return_on_error
= True
717 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
719 flavor_mano_id
= flavors
[0]['uuid']
722 #create one by one the images of aditional disks
723 dev_image_list
=[] #list of images
724 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
726 for device
in flavor_dict
['extended'].get('devices',[]):
727 if "image" not in device
and "image name" not in device
:
730 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
731 image_dict
['universal_name']=device
.get('image name')
732 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
733 image_dict
['location']=device
.get('image')
734 #image_dict['new_location']=vnfc.get('image location')
735 image_dict
['checksum']=device
.get('image checksum')
736 image_metadata_dict
= device
.get('image metadata', None)
737 image_metadata_str
= None
738 if image_metadata_dict
!= None:
739 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
740 image_dict
['metadata']=image_metadata_str
741 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
742 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
743 dev_image_list
.append(image_id
)
745 temp_flavor_dict
['name'] = flavor_dict
['name']
746 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
747 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
748 flavor_mano_id
= content
749 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
750 #create flavor at every vim
751 if 'uuid' in flavor_dict
:
752 del flavor_dict
['uuid']
754 for vim_id
,vim
in vims
.items():
755 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
756 flavor_created
="false"
758 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
759 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
760 #look at VIM if this flavor exist SKIPPED
761 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
763 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
767 # Create the flavor in VIM
768 # Translate images at devices from MANO id to VIM id
770 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
771 # make a copy of original devices
774 for device
in flavor_dict
["extended"].get("devices",[]):
777 devices_original
.append(dev
)
778 if 'image' in device
:
780 if 'image metadata' in device
:
781 del device
['image metadata']
782 if 'image checksum' in device
:
783 del device
['image checksum']
785 for index
in range(0,len(devices_original
)) :
786 device
=devices_original
[index
]
787 if "image" not in device
and "image name" not in device
:
788 # if 'size' in device:
789 disk_list
.append({'size': device
.get('size', default_volume_size
), 'name': device
.get('name')})
792 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
793 image_dict
['universal_name']=device
.get('image name')
794 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
795 image_dict
['location']=device
.get('image')
796 # image_dict['new_location']=device.get('image location')
797 image_dict
['checksum']=device
.get('image checksum')
798 image_metadata_dict
= device
.get('image metadata', None)
799 image_metadata_str
= None
800 if image_metadata_dict
!= None:
801 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
802 image_dict
['metadata']=image_metadata_str
803 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
804 image_dict
["uuid"]=image_mano_id
805 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
807 #save disk information (image must be based on and size
808 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
810 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
813 #check that this vim_id exist in VIM, if not create
814 flavor_vim_id
=flavor_db
[0]["vim_id"]
816 vim
.get_flavor(flavor_vim_id
)
817 continue #flavor exist
818 except vimconn
.vimconnException
:
820 #create flavor at vim
821 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
824 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
825 flavor_created
="false"
826 except vimconn
.vimconnException
as e
:
829 if not flavor_vim_id
:
830 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
831 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
832 flavor_created
="true"
833 except vimconn
.vimconnException
as e
:
835 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
837 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
840 #if reach here the flavor has been create or exist
841 if len(flavor_db
)==0:
842 #add new vim_id at datacenters_flavors
843 extended_devices_yaml
= None
844 if len(disk_list
) > 0:
845 extended_devices
= dict()
846 extended_devices
['disks'] = disk_list
847 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
848 mydb
.new_row('datacenters_flavors',
849 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
850 'created': flavor_created
, 'extended': extended_devices_yaml
})
851 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
852 #modify existing vim_id at datacenters_flavors
853 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
854 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
856 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
859 def get_str(obj
, field
, length
):
861 Obtain the str value,
866 value
= obj
.get(field
)
867 if value
is not None:
868 value
= str(value
)[:length
]
871 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
873 fill image content at db_image dictionary. Check if the image with this image and checksum exist
874 :param db_image: dictionary to insert data
875 :param mydb: database connector
876 :param descriptor: yang descriptor
877 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
880 db_image
["name"] = get_str(descriptor
, "image", 255)
881 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
882 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
883 db_image
["checksum"] = None
884 if db_image
["name"].startswith("/"):
885 db_image
["location"] = db_image
["name"]
886 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
888 db_image
["universal_name"] = db_image
["name"]
889 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
890 'checksum': db_image
['checksum']})
892 return existing_images
[0]["uuid"]
894 image_uuid
= str(uuid4())
895 db_image
["uuid"] = image_uuid
898 def get_resource_allocation_params(quota_descriptor
):
900 read the quota_descriptor from vnfd and fetch the resource allocation properties from the descriptor object
901 :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
902 :return: quota params for limit, reserve, shares from the descriptor object
905 if quota_descriptor
.get("limit"):
906 quota
["limit"] = int(quota_descriptor
["limit"])
907 if quota_descriptor
.get("reserve"):
908 quota
["reserve"] = int(quota_descriptor
["reserve"])
909 if quota_descriptor
.get("shares"):
910 quota
["shares"] = int(quota_descriptor
["shares"])
913 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
915 Parses an OSM IM vnfd_catalog and insert at DB
918 :param vnf_descriptor:
919 :return: The list of cretated vnf ids
922 myvnfd
= vnfd_catalog
.vnfd()
924 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
, path_helper
=True,
926 except Exception as e
:
927 raise NfvoException("Error. Invalid VNF descriptor format " + str(e
), httperrors
.Bad_Request
)
935 db_ip_profiles_index
= 0
939 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd:vnfd-catalog")
940 if not vnfd_catalog_descriptor
:
941 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd-catalog")
942 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd")
943 if not vnfd_descriptor_list
:
944 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd:vnfd")
945 for vnfd_yang
in myvnfd
.vnfd_catalog
.vnfd
.values():
946 vnfd
= vnfd_yang
.get()
949 vnf_uuid
= str(uuid4())
950 uuid_list
.append(vnf_uuid
)
951 vnfd_uuid_list
.append(vnf_uuid
)
952 vnfd_id
= get_str(vnfd
, "id", 255)
956 "name": get_str(vnfd
, "name", 255),
957 "description": get_str(vnfd
, "description", 255),
958 "tenant_id": tenant_id
,
959 "vendor": get_str(vnfd
, "vendor", 255),
960 "short_name": get_str(vnfd
, "short-name", 255),
961 "descriptor": str(vnf_descriptor
)[:60000]
964 for vnfd_descriptor
in vnfd_descriptor_list
:
965 if vnfd_descriptor
["id"] == str(vnfd
["id"]):
968 # table ip_profiles (ip-profiles)
969 ip_profile_name2db_table_index
= {}
970 for ip_profile
in vnfd
.get("ip-profiles").values():
972 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
973 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
974 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
975 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
976 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
977 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
980 for dns
in ip_profile
["ip-profile-params"]["dns-server"].values():
981 dns_list
.append(str(dns
.get("address")))
982 db_ip_profile
["dns_address"] = ";".join(dns_list
)
983 if ip_profile
["ip-profile-params"].get('security-group'):
984 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
985 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
986 db_ip_profiles_index
+= 1
987 db_ip_profiles
.append(db_ip_profile
)
989 # table nets (internal-vld)
990 net_id2uuid
= {} # for mapping interface with network
991 net_id2index
= {} # for mapping interface with network
992 for vld
in vnfd
.get("internal-vld").values():
993 net_uuid
= str(uuid4())
994 uuid_list
.append(net_uuid
)
996 "name": get_str(vld
, "name", 255),
999 "description": get_str(vld
, "description", 255),
1000 "osm_id": get_str(vld
, "id", 255),
1001 "type": "bridge", # TODO adjust depending on connection point type
1003 net_id2uuid
[vld
.get("id")] = net_uuid
1004 net_id2index
[vld
.get("id")] = len(db_nets
)
1005 db_nets
.append(db_net
)
1006 # ip-profile, link db_ip_profile with db_sce_net
1007 if vld
.get("ip-profile-ref"):
1008 ip_profile_name
= vld
.get("ip-profile-ref")
1009 if ip_profile_name
not in ip_profile_name2db_table_index
:
1010 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
1011 "'{}'. Reference to a non-existing 'ip_profiles'".format(
1012 str(vnfd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
1013 httperrors
.Bad_Request
)
1014 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["net_id"] = net_uuid
1015 else: #check no ip-address has been defined
1016 for icp
in vld
.get("internal-connection-point").values():
1017 if icp
.get("ip-address"):
1018 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
1019 "contains an ip-address but no ip-profile has been defined at VLD".format(
1020 str(vnfd
["id"]), str(vld
["id"]), str(icp
["id"])),
1021 httperrors
.Bad_Request
)
1023 # connection points vaiable declaration
1024 cp_name2iface_uuid
= {}
1026 cp_name2vm_uuid
= {}
1027 cp_name2db_interface
= {}
1028 vdu_id2cp_name
= {} # stored only when one external connection point is presented at this VDU
1032 vdu_id2db_table_index
= {}
1034 for vdu
in vnfd
.get("vdu").values():
1036 for vdu_descriptor
in vnfd_descriptor
["vdu"]:
1037 if vdu_descriptor
["id"] == str(vdu
["id"]):
1039 vm_uuid
= str(uuid4())
1040 uuid_list
.append(vm_uuid
)
1041 vdu_id
= get_str(vdu
, "id", 255)
1045 "name": get_str(vdu
, "name", 255),
1046 "description": get_str(vdu
, "description", 255),
1047 "pdu_type": get_str(vdu
, "pdu-type", 255),
1050 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
1051 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
1052 if vdu
.get("count"):
1053 db_vm
["count"] = int(vdu
["count"])
1056 image_present
= False
1057 if vdu
.get("image"):
1058 image_present
= True
1060 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
1062 image_uuid
= db_image
["uuid"]
1063 db_images
.append(db_image
)
1064 db_vm
["image_id"] = image_uuid
1065 if vdu
.get("alternative-images"):
1066 vm_alternative_images
= []
1067 for alt_image
in vdu
.get("alternative-images").values():
1069 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, alt_image
)
1071 image_uuid
= db_image
["uuid"]
1072 db_images
.append(db_image
)
1073 vm_alternative_images
.append({
1074 "image_id": image_uuid
,
1075 "vim_type": str(alt_image
["vim-type"]),
1076 # "universal_name": str(alt_image["image"]),
1077 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1080 db_vm
["image_list"] = yaml
.safe_dump(vm_alternative_images
, default_flow_style
=True, width
=256)
1084 if vdu
.get("volumes"):
1085 for volume_key
in vdu
["volumes"]:
1086 volume
= vdu
["volumes"][volume_key
]
1087 if not image_present
:
1088 # Convert the first volume to vnfc.image
1089 image_present
= True
1091 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
1093 image_uuid
= db_image
["uuid"]
1094 db_images
.append(db_image
)
1095 db_vm
["image_id"] = image_uuid
1097 # Add Openmano devices
1098 device
= {"name": str(volume
.get("name"))}
1099 device
["type"] = str(volume
.get("device-type"))
1100 if volume
.get("size"):
1101 device
["size"] = int(volume
["size"])
1102 if volume
.get("image"):
1103 device
["image name"] = str(volume
["image"])
1104 if volume
.get("image-checksum"):
1105 device
["image checksum"] = str(volume
["image-checksum"])
1107 devices
.append(device
)
1109 if not db_vm
.get("image_id"):
1110 if not db_vm
["pdu_type"]:
1111 raise NfvoException("Not defined image for VDU")
1112 # create a fake image
1116 if vdu
.get("cloud-init"):
1117 boot_data
["user-data"] = str(vdu
["cloud-init"])
1118 elif vdu
.get("cloud-init-file"):
1119 # TODO Where this file content is present???
1120 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1121 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
1123 if vdu
.get("supplemental-boot-data"):
1124 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
1125 boot_data
['boot-data-drive'] = True
1126 if vdu
["supplemental-boot-data"].get('config-file'):
1127 om_cfgfile_list
= list()
1128 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].values():
1129 # TODO Where this file content is present???
1130 cfg_source
= str(custom_config_file
["source"])
1131 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
1132 "content": cfg_source
})
1133 boot_data
['config-files'] = om_cfgfile_list
1135 db_vm
["boot_data"] = yaml
.safe_dump(boot_data
, default_flow_style
=True, width
=256)
1137 db_vms
.append(db_vm
)
1140 # table interfaces (internal/external interfaces)
1141 flavor_epa_interfaces
= []
1142 # for iface in chain(vdu.get("internal-interface").values(), vdu.get("external-interface").values()):
1143 for iface
in vdu
.get("interface").values():
1144 flavor_epa_interface
= {}
1145 iface_uuid
= str(uuid4())
1146 uuid_list
.append(iface_uuid
)
1149 "internal_name": get_str(iface
, "name", 255),
1152 flavor_epa_interface
["name"] = db_interface
["internal_name"]
1153 if iface
.get("virtual-interface").get("vpci"):
1154 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
1155 flavor_epa_interface
["vpci"] = db_interface
["vpci"]
1157 if iface
.get("virtual-interface").get("bandwidth"):
1158 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1159 db_interface
["bw"] = int(math
.ceil(bps
/ 1000000.0))
1160 flavor_epa_interface
["bandwidth"] = "{} Mbps".format(db_interface
["bw"])
1162 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1163 db_interface
["type"] = "mgmt"
1164 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1165 db_interface
["type"] = "bridge"
1166 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1167 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1168 db_interface
["type"] = "data"
1169 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1170 flavor_epa_interface
["dedicated"] = "no" if iface
["virtual-interface"]["type"] == "SR-IOV" \
1172 flavor_epa_interfaces
.append(flavor_epa_interface
)
1174 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1175 "-interface':'type':'{}'. Interface type is not supported".format(
1176 vnfd_id
, vdu_id
, iface
.get("virtual-interface").get("type")),
1177 httperrors
.Bad_Request
)
1179 if iface
.get("mgmt-interface"):
1180 db_interface
["type"] = "mgmt"
1182 if iface
.get("external-connection-point-ref"):
1184 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1185 db_interface
["external_name"] = get_str(cp
, "name", 255)
1186 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1187 cp_name2vdu_id
[db_interface
["external_name"]] = vdu_id
1188 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1189 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1190 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1191 if cp_descriptor
["name"] == db_interface
["external_name"]:
1196 if vdu_id
in vdu_id2cp_name
:
1197 vdu_id2cp_name
[vdu_id
] = None # more than two connection point for this VDU
1199 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1202 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1203 db_interface
["port_security"] = 0
1204 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1205 db_interface
["port_security"] = 1
1207 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1208 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1209 " at connection-point".format(
1210 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1211 cp
=iface
.get("vnfd-connection-point-ref")),
1212 httperrors
.Bad_Request
)
1213 elif iface
.get("internal-connection-point-ref"):
1215 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1216 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1219 raise KeyError("does not exist at vdu:internal-connection-point")
1222 for vld
in vnfd
.get("internal-vld").values():
1223 for cp
in vld
.get("internal-connection-point").values():
1224 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1226 raise KeyError("is referenced by more than one 'internal-vld'")
1230 raise KeyError("is not referenced by any 'internal-vld'")
1232 # set network type as data
1233 if iface
.get("virtual-interface") and iface
["virtual-interface"].get("type") in \
1234 ("SR-IOV", "PCI-PASSTHROUGH"):
1235 db_nets
[net_id2index
[icp_vld
.get("id")]]["type"] = "data"
1236 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1237 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1238 db_interface
["port_security"] = 0
1239 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1240 db_interface
["port_security"] = 1
1241 if icp
.get("ip-address"):
1242 if not icp_vld
.get("ip-profile-ref"):
1244 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1245 except KeyError as e
:
1246 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1247 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1249 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1250 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1251 httperrors
.Bad_Request
)
1252 if iface
.get("position"):
1253 db_interface
["created_at"] = int(iface
.get("position")) * 50
1254 if iface
.get("mac-address"):
1255 db_interface
["mac"] = str(iface
.get("mac-address"))
1256 db_interfaces
.append(db_interface
)
1260 "name": get_str(vdu
, "name", 250) + "-flv",
1261 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1262 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1263 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1265 # TODO revise the case of several numa-node-policy node
1269 extended
["devices"] = devices
1270 if flavor_epa_interfaces
:
1271 numa
["interfaces"] = flavor_epa_interfaces
1272 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1273 epa_vcpu_set
= False
1274 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1275 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1276 if numa_node_policy
.get("node"):
1277 numa_node
= next(iter(numa_node_policy
["node"].values()))
1278 if numa_node
.get("num-cores"):
1279 numa
["cores"] = numa_node
["num-cores"]
1281 if numa_node
.get("paired-threads"):
1282 if numa_node
["paired-threads"].get("num-paired-threads"):
1283 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1285 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1286 numa
["paired-threads-id"] = []
1287 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].values():
1288 numa
["paired-threads-id"].append(
1289 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1291 if numa_node
.get("num-threads"):
1292 numa
["threads"] = int(numa_node
["num-threads"])
1294 if numa_node
.get("memory-mb"):
1295 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1296 if vdu
["guest-epa"].get("mempage-size"):
1297 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1298 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1299 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1300 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1301 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1302 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1303 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1305 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1307 if vdu
["guest-epa"].get("cpu-quota") and not epa_vcpu_set
:
1308 cpuquota
= get_resource_allocation_params(vdu
["guest-epa"].get("cpu-quota"))
1310 extended
["cpu-quota"] = cpuquota
1311 if vdu
["guest-epa"].get("mem-quota"):
1312 vduquota
= get_resource_allocation_params(vdu
["guest-epa"].get("mem-quota"))
1314 extended
["mem-quota"] = vduquota
1315 if vdu
["guest-epa"].get("disk-io-quota"):
1316 diskioquota
= get_resource_allocation_params(vdu
["guest-epa"].get("disk-io-quota"))
1318 extended
["disk-io-quota"] = diskioquota
1319 if vdu
["guest-epa"].get("vif-quota"):
1320 vifquota
= get_resource_allocation_params(vdu
["guest-epa"].get("vif-quota"))
1322 extended
["vif-quota"] = vifquota
1324 extended
["numas"] = [numa
]
1326 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1327 db_flavor
["extended"] = extended_text
1328 # look if flavor exist
1329 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1330 'ram': db_flavor
.get('ram'),
1331 'vcpus': db_flavor
.get('vcpus'),
1332 'extended': db_flavor
.get('extended')
1334 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1335 if existing_flavors
:
1336 flavor_uuid
= existing_flavors
[0]["uuid"]
1338 flavor_uuid
= str(uuid4())
1339 uuid_list
.append(flavor_uuid
)
1340 db_flavor
["uuid"] = flavor_uuid
1341 db_flavors
.append(db_flavor
)
1342 db_vm
["flavor_id"] = flavor_uuid
1344 # VNF affinity and antiaffinity
1345 for pg
in vnfd
.get("placement-groups").values():
1346 pg_name
= get_str(pg
, "name", 255)
1347 for vdu
in pg
.get("member-vdus").values():
1348 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1349 if vdu_id
not in vdu_id2db_table_index
:
1350 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1351 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1352 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1353 httperrors
.Bad_Request
)
1354 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1355 # TODO consider the case of isolation and not colocation
1356 # if pg.get("strategy") == "ISOLATION":
1358 # VNF mgmt configuration
1359 if vnfd
["mgmt-interface"].get("vdu-id"):
1360 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1361 if mgmt_vdu_id
not in vdu_id2uuid
:
1362 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1363 "'{vdu}'. Reference to a non-existing vdu".format(
1364 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1365 httperrors
.Bad_Request
)
1366 mgmt_access
["vm_id"] = vdu_id2uuid
[mgmt_vdu_id
]
1367 mgmt_access
["vdu-id"] = mgmt_vdu_id
1368 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1369 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1370 if cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]:
1371 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1373 if vnfd
["mgmt-interface"].get("ip-address"):
1374 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1375 if vnfd
["mgmt-interface"].get("cp") and vnfd
.get("vdu"):
1376 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1377 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1378 "Reference to a non-existing connection-point".format(
1379 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1380 httperrors
.Bad_Request
)
1381 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1382 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1383 mgmt_access
["vdu-id"] = cp_name2vdu_id
[vnfd
["mgmt-interface"]["cp"]]
1384 # mark this interface as of type mgmt
1385 if cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]:
1386 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1388 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1391 mgmt_access
["default_user"] = default_user
1393 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1396 mgmt_access
["required"] = required
1398 password_
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}),
1401 mgmt_access
["password"] = password_
1404 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1406 db_vnfs
.append(db_vnf
)
1410 {"images": db_images
},
1411 {"flavors": db_flavors
},
1412 {"ip_profiles": db_ip_profiles
},
1414 {"interfaces": db_interfaces
},
1417 logger
.debug("create_vnf Deployment done vnfDict: %s",
1418 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1419 mydb
.new_rows(db_tables
, uuid_list
)
1420 return vnfd_uuid_list
1421 except NfvoException
:
1423 except Exception as e
:
1424 logger
.error("Exception {}".format(e
))
1425 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1428 @deprecated("Use new_vnfd_v3")
1429 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1430 global global_config
1432 # Step 1. Check the VNF descriptor
1433 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1434 # Step 2. Check tenant exist
1436 if tenant_id
!= "any":
1437 check_tenant(mydb
, tenant_id
)
1438 if "tenant_id" in vnf_descriptor
["vnf"]:
1439 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1440 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1441 httperrors
.Unauthorized
)
1443 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1444 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1445 if global_config
["auto_push_VNF_to_VIMs"]:
1446 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1448 # Step 4. Review the descriptor and add missing fields
1449 #print vnf_descriptor
1450 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1451 vnf_name
= vnf_descriptor
['vnf']['name']
1452 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1453 if "physical" in vnf_descriptor
['vnf']:
1454 del vnf_descriptor
['vnf']['physical']
1455 #print vnf_descriptor
1457 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1458 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1459 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1461 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1462 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1463 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1465 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1466 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1468 VNFCitem
["name"] = vnfc
['name']
1469 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1470 VNFCitem
["description"] = vnfc
.get("description", 'VM {} of the VNF {}'.format(vnfc
['name'],vnf_name
))
1472 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1475 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1476 myflavorDict
["description"] = VNFCitem
["description"]
1477 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1478 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1479 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1480 myflavorDict
["extended"] = {}
1482 devices
= vnfc
.get("devices")
1484 myflavorDict
["extended"]["devices"] = devices
1487 # 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
1488 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1490 # Previous code has been commented
1491 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1492 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1493 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1494 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1496 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1498 # print "Error creating flavor: unknown processor model. Rollback successful."
1499 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1501 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1502 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1504 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1505 myflavorDict
['extended']['numas'] = vnfc
['numas']
1509 # Step 6.2 New flavors are created in the VIM
1510 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1512 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1513 VNFCitem
["flavor_id"] = flavor_id
1514 VNFCDict
[vnfc
['name']] = VNFCitem
1516 logger
.debug("Creating new images in the VIM for each VNFC")
1517 # Step 6.3 New images are created in the VIM
1518 #For each VNFC, we must create the appropriate image.
1519 #This "for" loop might be integrated with the previous one
1520 #In case this integration is made, the VNFCDict might become a VNFClist.
1521 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1522 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1524 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1525 image_dict
['universal_name']=vnfc
.get('image name')
1526 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1527 image_dict
['location']=vnfc
.get('VNFC image')
1528 #image_dict['new_location']=vnfc.get('image location')
1529 image_dict
['checksum']=vnfc
.get('image checksum')
1530 image_metadata_dict
= vnfc
.get('image metadata', None)
1531 image_metadata_str
= None
1532 if image_metadata_dict
is not None:
1533 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1534 image_dict
['metadata']=image_metadata_str
1535 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1536 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1537 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1538 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1539 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1540 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1541 if vnfc
.get("boot-data"):
1542 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1545 # Step 7. Storing the VNF descriptor in the repository
1546 if "descriptor" not in vnf_descriptor
["vnf"]:
1547 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1549 # Step 8. Adding the VNF to the NFVO DB
1550 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1552 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1553 _
, message
= rollback(mydb
, vims
, rollback_list
)
1554 if isinstance(e
, db_base_Exception
):
1555 error_text
= "Exception at database"
1556 elif isinstance(e
, KeyError):
1557 error_text
= "KeyError exception "
1558 e
.http_code
= httperrors
.Internal_Server_Error
1560 error_text
= "Exception at VIM"
1561 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1562 #logger.error("start_scenario %s", error_text)
1563 raise NfvoException(error_text
, e
.http_code
)
1566 @deprecated("Use new_vnfd_v3")
1567 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1568 global global_config
1570 # Step 1. Check the VNF descriptor
1571 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1572 # Step 2. Check tenant exist
1574 if tenant_id
!= "any":
1575 check_tenant(mydb
, tenant_id
)
1576 if "tenant_id" in vnf_descriptor
["vnf"]:
1577 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1578 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1579 httperrors
.Unauthorized
)
1581 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1582 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1583 if global_config
["auto_push_VNF_to_VIMs"]:
1584 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1586 # Step 4. Review the descriptor and add missing fields
1587 #print vnf_descriptor
1588 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1589 vnf_name
= vnf_descriptor
['vnf']['name']
1590 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1591 if "physical" in vnf_descriptor
['vnf']:
1592 del vnf_descriptor
['vnf']['physical']
1593 #print vnf_descriptor
1595 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1596 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1597 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1599 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1600 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1601 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1603 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1604 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1606 VNFCitem
["name"] = vnfc
['name']
1607 VNFCitem
["description"] = vnfc
.get("description", 'VM {} of the VNF {}'.format(vnfc
['name'],vnf_name
))
1609 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1612 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1613 myflavorDict
["description"] = VNFCitem
["description"]
1614 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1615 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1616 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1617 myflavorDict
["extended"] = {}
1619 devices
= vnfc
.get("devices")
1621 myflavorDict
["extended"]["devices"] = devices
1624 # 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
1625 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1627 # Previous code has been commented
1628 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1629 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1630 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1631 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1633 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1635 # print "Error creating flavor: unknown processor model. Rollback successful."
1636 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1638 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1639 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1641 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1642 myflavorDict
['extended']['numas'] = vnfc
['numas']
1646 # Step 6.2 New flavors are created in the VIM
1647 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1649 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1650 VNFCitem
["flavor_id"] = flavor_id
1651 VNFCDict
[vnfc
['name']] = VNFCitem
1653 logger
.debug("Creating new images in the VIM for each VNFC")
1654 # Step 6.3 New images are created in the VIM
1655 #For each VNFC, we must create the appropriate image.
1656 #This "for" loop might be integrated with the previous one
1657 #In case this integration is made, the VNFCDict might become a VNFClist.
1658 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1659 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1661 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1662 image_dict
['universal_name']=vnfc
.get('image name')
1663 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1664 image_dict
['location']=vnfc
.get('VNFC image')
1665 #image_dict['new_location']=vnfc.get('image location')
1666 image_dict
['checksum']=vnfc
.get('image checksum')
1667 image_metadata_dict
= vnfc
.get('image metadata', None)
1668 image_metadata_str
= None
1669 if image_metadata_dict
is not None:
1670 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1671 image_dict
['metadata']=image_metadata_str
1672 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1673 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1674 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1675 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1676 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1677 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1678 if vnfc
.get("boot-data"):
1679 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1681 # Step 7. Storing the VNF descriptor in the repository
1682 if "descriptor" not in vnf_descriptor
["vnf"]:
1683 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1685 # Step 8. Adding the VNF to the NFVO DB
1686 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1688 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1689 _
, message
= rollback(mydb
, vims
, rollback_list
)
1690 if isinstance(e
, db_base_Exception
):
1691 error_text
= "Exception at database"
1692 elif isinstance(e
, KeyError):
1693 error_text
= "KeyError exception "
1694 e
.http_code
= httperrors
.Internal_Server_Error
1696 error_text
= "Exception at VIM"
1697 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1698 #logger.error("start_scenario %s", error_text)
1699 raise NfvoException(error_text
, e
.http_code
)
1702 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1703 #check valid tenant_id
1704 check_tenant(mydb
, tenant_id
)
1707 if tenant_id
!= "any":
1708 where_or
["tenant_id"] = tenant_id
1709 where_or
["public"] = True
1710 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1712 vnf_id
= vnf
["uuid"]
1713 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1714 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.items() if k
in filter_keys
)
1715 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1716 data
={'vnf' : filtered_content
}
1718 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1719 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1721 WHERE
={'vnfs.uuid': vnf_id
} )
1722 if len(content
) != 0:
1723 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1724 # change boot_data into boot-data
1726 if vm
.get("boot_data"):
1727 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1730 data
['vnf']['VNFC'] = content
1731 #TODO: GET all the information from a VNFC and include it in the output.
1734 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1735 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1736 WHERE
={'vnfs.uuid': vnf_id
} )
1737 data
['vnf']['nets'] = content
1739 #GET ip-profile for each net
1740 for net
in data
['vnf']['nets']:
1741 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1742 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1743 WHERE
={'net_id': net
["uuid"]} )
1744 if len(ipprofiles
)==1:
1745 net
["ip_profile"] = ipprofiles
[0]
1746 elif len(ipprofiles
)>1:
1747 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
1750 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1752 #GET External Interfaces
1753 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1754 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1755 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1756 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1758 data
['vnf']['external-connections'] = content
1763 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1764 # Check tenant exist
1765 if tenant_id
!= "any":
1766 check_tenant(mydb
, tenant_id
)
1767 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1768 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1772 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1774 if tenant_id
!= "any":
1775 where_or
["tenant_id"] = tenant_id
1776 where_or
["public"] = True
1777 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1778 vnf_id
= vnf
["uuid"]
1780 # "Getting the list of flavors and tenants of the VNF"
1781 flavorList
= get_flavorlist(mydb
, vnf_id
)
1782 if len(flavorList
)==0:
1783 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1785 imageList
= get_imagelist(mydb
, vnf_id
)
1786 if len(imageList
)==0:
1787 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1789 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1791 raise NfvoException("vnf '{}' not found".format(vnf_id
), httperrors
.Not_Found
)
1794 for flavor
in flavorList
:
1795 #check if flavor is used by other vnf
1797 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1799 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1801 #flavor not used, must be deleted
1803 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1804 for flavor_vim
in c
:
1805 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1809 for vim
in vims
.values():
1810 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1816 myvim
.delete_flavor(flavor_vim
["vim_id"])
1817 except vimconn
.vimconnNotFoundException
:
1818 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1819 flavor_vim
["datacenter_vim_id"] )
1820 except vimconn
.vimconnException
as e
:
1821 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1822 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1823 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1824 flavor_vim
["datacenter_vim_id"]))
1825 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1826 mydb
.delete_row_by_id('flavors', flavor
)
1827 except db_base_Exception
as e
:
1828 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1829 undeletedItems
.append("flavor {}".format(flavor
))
1832 for image
in imageList
:
1834 #check if image is used by other vnf
1835 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1837 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1839 #image not used, must be deleted
1841 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1843 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1845 if image_vim
['created']=='false': #skip this image because not created by openmano
1847 myvim
=vims
[ image_vim
["datacenter_id"] ]
1849 myvim
.delete_image(image_vim
["vim_id"])
1850 except vimconn
.vimconnNotFoundException
as e
:
1851 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1852 except vimconn
.vimconnException
as e
:
1853 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1854 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1855 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1856 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1857 mydb
.delete_row_by_id('images', image
)
1858 except db_base_Exception
as e
:
1859 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1860 undeletedItems
.append("image {}".format(image
))
1862 return vnf_id
+ " " + vnf
["name"]
1864 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1867 @deprecated("Not used")
1868 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1869 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1873 return -httperrors
.Not_Found
, "datacenter '{}' not found".format(datacenter_name
)
1874 myvim
= next(iter(vims
.values()))
1875 result
,servers
= myvim
.get_hosts_info()
1877 return result
, servers
1878 topology
= {'name':myvim
['name'] , 'servers': servers
}
1879 return result
, topology
1882 def get_hosts(mydb
, nfvo_tenant_id
):
1883 vims
= get_vim(mydb
, nfvo_tenant_id
)
1885 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), httperrors
.Not_Found
)
1887 #print "nfvo.datacenter_action() error. Several datacenters found"
1888 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
1889 myvim
= next(iter(vims
.values()))
1891 hosts
= myvim
.get_hosts()
1892 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1894 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1896 server
={'name':host
['name'], 'vms':[]}
1897 for vm
in host
['instances']:
1898 #get internal name and model
1900 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1901 WHERE
={'vim_vm_id':vm
['id']} )
1903 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1905 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1907 except db_base_Exception
as e
:
1908 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1909 datacenter
['Datacenters'][0]['servers'].append(server
)
1910 #return -400, "en construccion"
1912 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1914 except vimconn
.vimconnException
as e
:
1915 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1918 @deprecated("Use new_nsd_v3")
1919 def new_scenario(mydb
, tenant_id
, topo
):
1921 # result, vims = get_vim(mydb, tenant_id)
1923 # return result, vims
1925 if tenant_id
!= "any":
1926 check_tenant(mydb
, tenant_id
)
1927 if "tenant_id" in topo
:
1928 if topo
["tenant_id"] != tenant_id
:
1929 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1930 httperrors
.Unauthorized
)
1934 #1.1: get VNFs and external_networks (other_nets).
1936 other_nets
={} #external_networks, bridge_networks and data_networkds
1937 nodes
= topo
['topology']['nodes']
1938 for k
in nodes
.keys():
1939 if nodes
[k
]['type'] == 'VNF':
1941 vnfs
[k
]['ifaces'] = {}
1942 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1943 other_nets
[k
] = nodes
[k
]
1944 other_nets
[k
]['external']=True
1945 elif nodes
[k
]['type'] == 'network':
1946 other_nets
[k
] = nodes
[k
]
1947 other_nets
[k
]['external']=False
1950 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1951 for name
,vnf
in vnfs
.items():
1952 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1954 error_pos
= "'topology':'nodes':'" + name
+ "'"
1956 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1957 where
['uuid'] = vnf
['vnf_id']
1958 if 'VNF model' in vnf
:
1959 error_text
+= " 'VNF model' " + vnf
['VNF model']
1960 where
['name'] = vnf
['VNF model']
1962 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, httperrors
.Bad_Request
)
1964 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1968 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
1970 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
1971 vnf
['uuid']=vnf_db
[0]['uuid']
1972 vnf
['description']=vnf_db
[0]['description']
1973 #get external interfaces
1974 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1975 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1976 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1977 for ext_iface
in ext_ifaces
:
1978 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1980 #1.4 get list of connections
1981 conections
= topo
['topology']['connections']
1982 conections_list
= []
1983 conections_list_name
= []
1984 for k
in conections
.keys():
1985 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1986 ifaces_list
= conections
[k
]['nodes'].items()
1987 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1989 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1990 for k2
in conection_pair_list
:
1993 con_type
= conections
[k
].get("type", "link")
1994 if con_type
!= "link":
1996 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), httperrors
.Bad_Request
)
1997 other_nets
[k
] = {'external': False}
1998 if conections
[k
].get("graph"):
1999 other_nets
[k
]["graph"] = conections
[k
]["graph"]
2000 ifaces_list
.append( (k
, None) )
2003 if con_type
== "external_network":
2004 other_nets
[k
]['external'] = True
2005 if conections
[k
].get("model"):
2006 other_nets
[k
]["model"] = conections
[k
]["model"]
2008 other_nets
[k
]["model"] = k
2009 if con_type
== "dataplane_net" or con_type
== "bridge_net":
2010 other_nets
[k
]["model"] = con_type
2012 conections_list_name
.append(k
)
2013 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)
2014 #print set(ifaces_list)
2015 #check valid VNF and iface names
2016 for iface
in ifaces_list
:
2017 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
2018 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
2019 str(k
), iface
[0]), httperrors
.Not_Found
)
2020 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
2021 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
2022 str(k
), iface
[0], iface
[1]), httperrors
.Not_Found
)
2024 #1.5 unify connections from the pair list to a consolidated list
2026 while index
< len(conections_list
):
2028 while index2
< len(conections_list
):
2029 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
2030 conections_list
[index
] |
= conections_list
[index2
]
2031 del conections_list
[index2
]
2032 del conections_list_name
[index2
]
2035 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
2037 #for k in conections_list:
2042 #1.6 Delete non external nets
2043 # for k in other_nets.keys():
2044 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
2045 # for con in conections_list:
2047 # for index in range(0,len(con)):
2048 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
2049 # for index in delete_indexes:
2052 #1.7: Check external_ports are present at database table datacenter_nets
2053 for k
,net
in other_nets
.items():
2054 error_pos
= "'topology':'nodes':'" + k
+ "'"
2055 if net
['external']==False:
2056 if 'name' not in net
:
2058 if 'model' not in net
:
2059 raise NfvoException("needed a 'model' at " + error_pos
, httperrors
.Bad_Request
)
2060 if net
['model']=='bridge_net':
2061 net
['type']='bridge';
2062 elif net
['model']=='dataplane_net':
2065 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, httperrors
.Not_Found
)
2067 #IF we do not want to check that external network exist at datacenter
2072 # if 'net_id' in net:
2073 # error_text += " 'net_id' " + net['net_id']
2074 # WHERE_['uuid'] = net['net_id']
2075 # if 'model' in net:
2076 # error_text += " 'model' " + net['model']
2077 # WHERE_['name'] = net['model']
2078 # if len(WHERE_) == 0:
2079 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2080 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2081 # FROM='datacenter_nets', WHERE=WHERE_ )
2083 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2085 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2086 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2088 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2089 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2090 # other_nets[k].update(net_db[0])
2093 net_nb
=0 #Number of nets
2094 for con
in conections_list
:
2095 #check if this is connected to a external net
2099 for index
in range(0,len(con
)):
2100 #check if this is connected to a external net
2101 for net_key
in other_nets
.keys():
2102 if con
[index
][0]==net_key
:
2103 if other_net_index
>=0:
2104 error_text
= "There is some interface connected both to net '{}' and net '{}'".format(
2105 con
[other_net_index
][0], net_key
)
2106 #print "nfvo.new_scenario " + error_text
2107 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2109 other_net_index
= index
2110 net_target
= net_key
2112 #print "other_net_index", other_net_index
2114 if other_net_index
>=0:
2115 del con
[other_net_index
]
2116 #IF we do not want to check that external network exist at datacenter
2117 if other_nets
[net_target
]['external'] :
2118 if "name" not in other_nets
[net_target
]:
2119 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
2120 if other_nets
[net_target
]["type"] == "external_network":
2121 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
2122 other_nets
[net_target
]["type"] = "data"
2124 other_nets
[net_target
]["type"] = "bridge"
2126 # if other_nets[net_target]['external'] :
2127 # 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
2128 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2129 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2130 # print "nfvo.new_scenario " + error_text
2131 # return -httperrors.Bad_Request, error_text
2134 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2137 net_type_bridge
=False
2139 net_target
= "__-__net"+str(net_nb
)
2140 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2141 'description':"net-{} in scenario {}".format(net_nb
,topo
['name']),
2144 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2145 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2146 if iface_type
=='mgmt' or iface_type
=='bridge':
2147 net_type_bridge
= True
2149 net_type_data
= True
2150 if net_type_bridge
and net_type_data
:
2151 error_text
= "Error connection interfaces of bridge type with data type. Firs node {}, iface {}".format(iface
[0], iface
[1])
2152 #print "nfvo.new_scenario " + error_text
2153 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2154 elif net_type_bridge
:
2157 type_
='data' if len(con
)>2 else 'ptp'
2158 net_list
[net_target
]['type'] = type_
2161 error_text
= "Error connection node {} : {} does not match any VNF or interface".format(iface
[0], iface
[1])
2162 #print "nfvo.new_scenario " + error_text
2164 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2166 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2167 #1.8.1 obtain management net
2168 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2169 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2170 #1.8.2 check all interfaces from all vnfs
2172 add_mgmt_net
= False
2173 for vnf
in vnfs
.values():
2174 for iface
in vnf
['ifaces'].values():
2175 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2176 #iface not connected
2177 iface
['net_key'] = 'mgmt'
2179 if add_mgmt_net
and 'mgmt' not in net_list
:
2180 net_list
['mgmt']=mgmt_net
[0]
2181 net_list
['mgmt']['external']=True
2182 net_list
['mgmt']['graph']={'visible':False}
2184 net_list
.update(other_nets
)
2186 #print 'net_list', net_list
2191 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2192 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2193 'tenant_id':tenant_id
, 'name':topo
['name'],
2194 'description':topo
.get('description',topo
['name']),
2195 'public': topo
.get('public', False)
2201 @deprecated("Use new_nsd_v3")
2202 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2203 """ This creates a new scenario for version 0.2 and 0.3"""
2204 scenario
= scenario_dict
["scenario"]
2205 if tenant_id
!= "any":
2206 check_tenant(mydb
, tenant_id
)
2207 if "tenant_id" in scenario
:
2208 if scenario
["tenant_id"] != tenant_id
:
2209 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2210 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2211 scenario
["tenant_id"], tenant_id
), httperrors
.Unauthorized
)
2215 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2216 for name
,vnf
in scenario
["vnfs"].items():
2217 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2219 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2221 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2222 where
['uuid'] = vnf
['vnf_id']
2223 if 'vnf_name' in vnf
:
2224 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2225 where
['name'] = vnf
['vnf_name']
2227 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, httperrors
.Bad_Request
)
2228 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2231 if len(vnf_db
) == 0:
2232 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
2233 elif len(vnf_db
) > 1:
2234 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
2235 vnf
['uuid'] = vnf_db
[0]['uuid']
2236 vnf
['description'] = vnf_db
[0]['description']
2238 # get external interfaces
2239 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2240 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2241 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2242 for ext_iface
in ext_ifaces
:
2243 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2244 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2246 # 2: Insert net_key and ip_address at every vnf interface
2247 for net_name
, net
in scenario
["networks"].items():
2248 net_type_bridge
= False
2249 net_type_data
= False
2250 for iface_dict
in net
["interfaces"]:
2251 if version
== "0.2":
2252 temp_dict
= iface_dict
2254 elif version
== "0.3":
2255 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2256 ip_address
= iface_dict
.get('ip_address', None)
2257 for vnf
, iface
in temp_dict
.items():
2258 if vnf
not in scenario
["vnfs"]:
2259 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2261 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2262 raise NfvoException(error_text
, httperrors
.Not_Found
)
2263 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2264 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2265 .format(net_name
, iface
)
2266 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2267 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2268 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2269 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2270 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2271 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2272 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2273 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2274 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2275 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2276 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2277 net_type_bridge
= True
2279 net_type_data
= True
2281 if net_type_bridge
and net_type_data
:
2282 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2284 # logger.debug("nfvo.new_scenario " + error_text)
2285 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2286 elif net_type_bridge
:
2289 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2291 if net
.get("implementation"): # for v0.3
2292 if type_
== "bridge" and net
["implementation"] == "underlay":
2293 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2294 "'network':'{}'".format(net_name
)
2295 # logger.debug(error_text)
2296 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2297 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2298 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2299 "'network':'{}'".format(net_name
)
2300 # logger.debug(error_text)
2301 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2302 net
.pop("implementation")
2303 if "type" in net
and version
== "0.3": # for v0.3
2304 if type_
== "data" and net
["type"] == "e-line":
2305 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2306 "'e-line' at 'network':'{}'".format(net_name
)
2307 # logger.debug(error_text)
2308 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2309 elif type_
== "ptp" and net
["type"] == "e-lan":
2313 net
['name'] = net_name
2314 net
['external'] = net
.get('external', False)
2316 # 3: insert at database
2317 scenario
["nets"] = scenario
["networks"]
2318 scenario
['tenant_id'] = tenant_id
2319 scenario_id
= mydb
.new_scenario(scenario
)
2323 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2325 Parses an OSM IM nsd_catalog and insert at DB
2328 :param nsd_descriptor:
2329 :return: The list of created NSD ids
2332 mynsd
= nsd_catalog
.nsd()
2334 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
, skip_unknown
=True)
2335 except Exception as e
:
2336 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), httperrors
.Bad_Request
)
2340 db_sce_interfaces
= []
2343 db_sce_rsp_hops
= []
2344 db_sce_classifiers
= []
2345 db_sce_classifier_matches
= []
2347 db_ip_profiles_index
= 0
2350 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.values():
2351 nsd
= nsd_yang
.get()
2354 scenario_uuid
= str(uuid4())
2355 uuid_list
.append(scenario_uuid
)
2356 nsd_uuid_list
.append(scenario_uuid
)
2358 "uuid": scenario_uuid
,
2359 "osm_id": get_str(nsd
, "id", 255),
2360 "name": get_str(nsd
, "name", 255),
2361 "description": get_str(nsd
, "description", 255),
2362 "tenant_id": tenant_id
,
2363 "vendor": get_str(nsd
, "vendor", 255),
2364 "short_name": get_str(nsd
, "short-name", 255),
2365 "descriptor": str(nsd_descriptor
)[:60000],
2367 db_scenarios
.append(db_scenario
)
2369 # table sce_vnfs (constituent-vnfd)
2370 vnf_index2scevnf_uuid
= {}
2371 vnf_index2vnf_uuid
= {}
2372 for vnf
in nsd
.get("constituent-vnfd").values():
2373 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2374 'tenant_id': tenant_id
})
2375 if not existing_vnf
:
2376 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2377 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2378 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2379 httperrors
.Bad_Request
)
2380 sce_vnf_uuid
= str(uuid4())
2381 uuid_list
.append(sce_vnf_uuid
)
2383 "uuid": sce_vnf_uuid
,
2384 "scenario_id": scenario_uuid
,
2385 # "name": get_str(vnf, "member-vnf-index", 255),
2386 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2387 "vnf_id": existing_vnf
[0]["uuid"],
2388 "member_vnf_index": str(vnf
["member-vnf-index"]),
2389 # TODO 'start-by-default': True
2391 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2392 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2393 db_sce_vnfs
.append(db_sce_vnf
)
2395 # table ip_profiles (ip-profiles)
2396 ip_profile_name2db_table_index
= {}
2397 for ip_profile
in nsd
.get("ip-profiles").values():
2399 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2400 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2401 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2402 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2403 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2404 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2407 for dns
in ip_profile
["ip-profile-params"]["dns-server"].values():
2408 dns_list
.append(str(dns
.get("address")))
2409 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2410 if ip_profile
["ip-profile-params"].get('security-group'):
2411 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2412 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2413 db_ip_profiles_index
+= 1
2414 db_ip_profiles
.append(db_ip_profile
)
2416 # table sce_nets (internal-vld)
2417 for vld
in nsd
.get("vld").values():
2418 sce_net_uuid
= str(uuid4())
2419 uuid_list
.append(sce_net_uuid
)
2421 "uuid": sce_net_uuid
,
2422 "name": get_str(vld
, "name", 255),
2423 "scenario_id": scenario_uuid
,
2425 "multipoint": not vld
.get("type") == "ELINE",
2426 "osm_id": get_str(vld
, "id", 255),
2428 "description": get_str(vld
, "description", 255),
2430 # guess type of network
2431 if vld
.get("mgmt-network"):
2432 db_sce_net
["type"] = "bridge"
2433 db_sce_net
["external"] = True
2434 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2435 db_sce_net
["type"] = "data"
2437 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2438 db_sce_net
["type"] = None
2439 db_sce_nets
.append(db_sce_net
)
2441 # ip-profile, link db_ip_profile with db_sce_net
2442 if vld
.get("ip-profile-ref"):
2443 ip_profile_name
= vld
.get("ip-profile-ref")
2444 if ip_profile_name
not in ip_profile_name2db_table_index
:
2445 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2446 " Reference to a non-existing 'ip_profiles'".format(
2447 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2448 httperrors
.Bad_Request
)
2449 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2450 elif vld
.get("vim-network-name"):
2451 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2453 # table sce_interfaces (vld:vnfd-connection-point-ref)
2454 for iface
in vld
.get("vnfd-connection-point-ref").values():
2455 # Check if there are VDUs in the descriptor
2456 vnf_index
= str(iface
['member-vnf-index-ref'])
2457 existing_vdus
= mydb
.get_rows(SELECT
=('vms.uuid'), FROM
="vms", WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
]})
2459 # check correct parameters
2460 if vnf_index
not in vnf_index2vnf_uuid
:
2461 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2462 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2463 "'nsd':'constituent-vnfd'".format(
2464 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2465 httperrors
.Bad_Request
)
2467 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2468 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2469 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2470 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2472 if not existing_ifaces
:
2473 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2474 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2475 "connection-point name at VNFD '{}'".format(
2476 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2477 str(iface
.get("vnfd-id-ref"))[:255]),
2478 httperrors
.Bad_Request
)
2479 interface_uuid
= existing_ifaces
[0]["uuid"]
2480 if existing_ifaces
[0]["iface_type"] == "data":
2481 db_sce_net
["type"] = "data"
2482 sce_interface_uuid
= str(uuid4())
2483 uuid_list
.append(sce_net_uuid
)
2484 iface_ip_address
= None
2485 if iface
.get("ip-address"):
2486 iface_ip_address
= str(iface
.get("ip-address"))
2487 db_sce_interface
= {
2488 "uuid": sce_interface_uuid
,
2489 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2490 "sce_net_id": sce_net_uuid
,
2491 "interface_id": interface_uuid
,
2492 "ip_address": iface_ip_address
,
2494 db_sce_interfaces
.append(db_sce_interface
)
2495 if not db_sce_net
["type"]:
2496 db_sce_net
["type"] = "bridge"
2498 # table sce_vnffgs (vnffgd)
2499 for vnffg
in nsd
.get("vnffgd").values():
2500 sce_vnffg_uuid
= str(uuid4())
2501 uuid_list
.append(sce_vnffg_uuid
)
2503 "uuid": sce_vnffg_uuid
,
2504 "name": get_str(vnffg
, "name", 255),
2505 "scenario_id": scenario_uuid
,
2506 "vendor": get_str(vnffg
, "vendor", 255),
2507 "description": get_str(vld
, "description", 255),
2509 db_sce_vnffgs
.append(db_sce_vnffg
)
2512 for rsp
in vnffg
.get("rsp").values():
2513 sce_rsp_uuid
= str(uuid4())
2514 uuid_list
.append(sce_rsp_uuid
)
2516 "uuid": sce_rsp_uuid
,
2517 "name": get_str(rsp
, "name", 255),
2518 "sce_vnffg_id": sce_vnffg_uuid
,
2519 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2521 db_sce_rsps
.append(db_sce_rsp
)
2522 for iface
in rsp
.get("vnfd-connection-point-ref").values():
2523 vnf_index
= str(iface
['member-vnf-index-ref'])
2524 if_order
= int(iface
['order'])
2525 # check correct parameters
2526 if vnf_index
not in vnf_index2vnf_uuid
:
2527 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2528 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2529 "'nsd':'constituent-vnfd'".format(
2530 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2531 httperrors
.Bad_Request
)
2533 ingress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2534 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2536 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2537 'external_name': get_str(iface
, "vnfd-ingress-connection-point-ref",
2539 if not ingress_existing_ifaces
:
2540 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2541 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2542 "connection-point name at VNFD '{}'".format(
2543 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-ingress-connection-point-ref"]),
2544 str(iface
.get("vnfd-id-ref"))[:255]), httperrors
.Bad_Request
)
2546 egress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2547 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2549 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2550 'external_name': get_str(iface
, "vnfd-egress-connection-point-ref",
2552 if not egress_existing_ifaces
:
2553 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2554 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2555 "connection-point name at VNFD '{}'".format(
2556 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-egress-connection-point-ref"]),
2557 str(iface
.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request
)
2559 ingress_interface_uuid
= ingress_existing_ifaces
[0]["uuid"]
2560 egress_interface_uuid
= egress_existing_ifaces
[0]["uuid"]
2561 sce_rsp_hop_uuid
= str(uuid4())
2562 uuid_list
.append(sce_rsp_hop_uuid
)
2564 "uuid": sce_rsp_hop_uuid
,
2565 "if_order": if_order
,
2566 "ingress_interface_id": ingress_interface_uuid
,
2567 "egress_interface_id": egress_interface_uuid
,
2568 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2569 "sce_rsp_id": sce_rsp_uuid
,
2571 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2573 # deal with classifiers
2574 for classifier
in vnffg
.get("classifier").values():
2575 sce_classifier_uuid
= str(uuid4())
2576 uuid_list
.append(sce_classifier_uuid
)
2579 vnf_index
= str(classifier
['member-vnf-index-ref'])
2580 if vnf_index
not in vnf_index2vnf_uuid
:
2581 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2582 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2583 "'nsd':'constituent-vnfd'".format(
2584 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2585 httperrors
.Bad_Request
)
2586 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2587 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2588 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2589 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2591 if not existing_ifaces
:
2592 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2593 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2594 "connection-point name at VNFD '{}'".format(
2595 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2596 str(iface
.get("vnfd-id-ref"))[:255]),
2597 httperrors
.Bad_Request
)
2598 interface_uuid
= existing_ifaces
[0]["uuid"]
2600 db_sce_classifier
= {
2601 "uuid": sce_classifier_uuid
,
2602 "name": get_str(classifier
, "name", 255),
2603 "sce_vnffg_id": sce_vnffg_uuid
,
2604 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2605 "interface_id": interface_uuid
,
2607 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2608 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2609 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2610 db_sce_classifiers
.append(db_sce_classifier
)
2612 for match
in classifier
.get("match-attributes").values():
2613 sce_classifier_match_uuid
= str(uuid4())
2614 uuid_list
.append(sce_classifier_match_uuid
)
2615 db_sce_classifier_match
= {
2616 "uuid": sce_classifier_match_uuid
,
2617 "ip_proto": get_str(match
, "ip-proto", 2),
2618 "source_ip": get_str(match
, "source-ip-address", 16),
2619 "destination_ip": get_str(match
, "destination-ip-address", 16),
2620 "source_port": get_str(match
, "source-port", 5),
2621 "destination_port": get_str(match
, "destination-port", 5),
2622 "sce_classifier_id": sce_classifier_uuid
,
2624 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2627 # remove unneeded id's in sce_rsps
2628 for rsp
in db_sce_rsps
:
2632 {"scenarios": db_scenarios
},
2633 {"sce_nets": db_sce_nets
},
2634 {"ip_profiles": db_ip_profiles
},
2635 {"sce_vnfs": db_sce_vnfs
},
2636 {"sce_interfaces": db_sce_interfaces
},
2637 {"sce_vnffgs": db_sce_vnffgs
},
2638 {"sce_rsps": db_sce_rsps
},
2639 {"sce_rsp_hops": db_sce_rsp_hops
},
2640 {"sce_classifiers": db_sce_classifiers
},
2641 {"sce_classifier_matches": db_sce_classifier_matches
},
2644 logger
.debug("new_nsd_v3 done: %s",
2645 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2646 mydb
.new_rows(db_tables
, uuid_list
)
2647 return nsd_uuid_list
2648 except NfvoException
:
2650 except Exception as e
:
2651 logger
.error("Exception {}".format(e
))
2652 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2655 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2656 data
["uuid"] = scenario_id
2657 data
["tenant_id"] = tenant_id
2658 c
= mydb
.edit_scenario( data
)
2662 @deprecated("Use create_instance")
2663 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2664 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2665 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2666 vims
= {datacenter_id
: myvim
}
2667 myvim_tenant
= myvim
['tenant_id']
2668 datacenter_name
= myvim
['name']
2672 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2673 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2674 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2675 scenarioDict
['datacenter_id'] = datacenter_id
2676 #print '================scenarioDict======================='
2677 #print json.dumps(scenarioDict, indent=4)
2678 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2680 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2681 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2683 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2684 auxNetDict
['scenario'] = {}
2686 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2687 for sce_net
in scenarioDict
['nets']:
2688 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2690 myNetName
= "{}.{}".format(instance_scenario_name
, sce_net
['name'])
2691 myNetName
= myNetName
[0:255] #limit length
2692 myNetType
= sce_net
['type']
2694 myNetDict
["name"] = myNetName
2695 myNetDict
["type"] = myNetType
2696 myNetDict
["tenant_id"] = myvim_tenant
2697 myNetIPProfile
= sce_net
.get('ip_profile', None)
2698 myProviderNetwork
= sce_net
.get('provider_network', None)
2700 #We should use the dictionary as input parameter for new_network
2702 if not sce_net
["external"]:
2703 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
, provider_network_profile
=myProviderNetwork
)
2704 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2705 sce_net
['vim_id'] = network_id
2706 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2707 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2708 sce_net
["created"] = True
2710 if sce_net
['vim_id'] == None:
2711 error_text
= "Error, datacenter '{}' does not have external network '{}'.".format(
2712 datacenter_name
, sce_net
['name'])
2713 _
, message
= rollback(mydb
, vims
, rollbackList
)
2714 logger
.error("nfvo.start_scenario: %s", error_text
)
2715 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2716 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2717 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2719 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2720 #For each vnf net, we create it and we add it to instanceNetlist.
2722 for sce_vnf
in scenarioDict
['vnfs']:
2723 for net
in sce_vnf
['nets']:
2724 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2726 myNetName
= "{}.{}".format(instance_scenario_name
,net
['name'])
2727 myNetName
= myNetName
[0:255] #limit length
2728 myNetType
= net
['type']
2730 myNetDict
["name"] = myNetName
2731 myNetDict
["type"] = myNetType
2732 myNetDict
["tenant_id"] = myvim_tenant
2733 myNetIPProfile
= net
.get('ip_profile', None)
2734 myProviderNetwork
= sce_net
.get('provider_network', None)
2737 #We should use the dictionary as input parameter for new_network
2738 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
, provider_network_profile
=myProviderNetwork
)
2739 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2740 net
['vim_id'] = network_id
2741 if sce_vnf
['uuid'] not in auxNetDict
:
2742 auxNetDict
[sce_vnf
['uuid']] = {}
2743 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2744 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2745 net
["created"] = True
2747 #print "auxNetDict:"
2748 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2750 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2751 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2753 for sce_vnf
in scenarioDict
['vnfs']:
2754 vnf_availability_zones
= []
2755 for vm
in sce_vnf
['vms']:
2756 vm_av
= vm
.get('availability_zone')
2757 if vm_av
and vm_av
not in vnf_availability_zones
:
2758 vnf_availability_zones
.append(vm_av
)
2760 # check if there is enough availability zones available at vim level.
2761 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2762 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2763 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
2765 for vm
in sce_vnf
['vms']:
2768 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2769 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2770 #myVMDict['description'] = vm['description']
2771 myVMDict
['description'] = myVMDict
['name'][0:99]
2773 myVMDict
['start'] = "no"
2774 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2775 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2777 #create image at vim in case it not exist
2778 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2779 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2780 vm
['vim_image_id'] = image_id
2782 #create flavor at vim in case it not exist
2783 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2784 if flavor_dict
['extended']!=None:
2785 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'], Loader
=yaml
.Loader
)
2786 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2787 vm
['vim_flavor_id'] = flavor_id
2790 myVMDict
['imageRef'] = vm
['vim_image_id']
2791 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2792 myVMDict
['networks'] = []
2793 for iface
in vm
['interfaces']:
2795 if iface
['type']=="data":
2796 netDict
['type'] = iface
['model']
2797 elif "model" in iface
and iface
["model"]!=None:
2798 netDict
['model']=iface
['model']
2799 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2800 #discover type of interface looking at flavor
2801 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2802 for flavor_iface
in numa
.get('interfaces',[]):
2803 if flavor_iface
.get('name') == iface
['internal_name']:
2804 if flavor_iface
['dedicated'] == 'yes':
2805 netDict
['type']="PF" #passthrough
2806 elif flavor_iface
['dedicated'] == 'no':
2807 netDict
['type']="VF" #siov
2808 elif flavor_iface
['dedicated'] == 'yes:sriov':
2809 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2810 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2812 netDict
["use"]=iface
['type']
2813 if netDict
["use"]=="data" and not netDict
.get("type"):
2814 #print "netDict", netDict
2815 #print "iface", iface
2816 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".format(
2817 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
2818 if flavor_dict
.get('extended')==None:
2819 raise NfvoException(e_text
+ "After database migration some information is not available. \
2820 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
2822 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
2823 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2824 netDict
["type"]="virtual"
2825 if "vpci" in iface
and iface
["vpci"] is not None:
2826 netDict
['vpci'] = iface
['vpci']
2827 if "mac" in iface
and iface
["mac"] is not None:
2828 netDict
['mac_address'] = iface
['mac']
2829 if "port-security" in iface
and iface
["port-security"] is not None:
2830 netDict
['port_security'] = iface
['port-security']
2831 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2832 netDict
['floating_ip'] = iface
['floating-ip']
2833 netDict
['name'] = iface
['internal_name']
2834 if iface
['net_id'] is None:
2835 for vnf_iface
in sce_vnf
["interfaces"]:
2838 if vnf_iface
['interface_id']==iface
['uuid']:
2839 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2842 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2843 #skip bridge ifaces not connected to any net
2844 #if 'net_id' not in netDict or netDict['net_id']==None:
2846 myVMDict
['networks'].append(netDict
)
2847 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2848 #print myVMDict['name']
2849 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2850 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2851 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2853 if 'availability_zone' in myVMDict
:
2854 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2858 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2859 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2860 availability_zone_index
=av_index
,
2861 availability_zone_list
=vnf_availability_zones
)
2862 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2863 vm
['vim_id'] = vm_id
2864 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2865 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2866 for net
in myVMDict
['networks']:
2868 for iface
in vm
['interfaces']:
2869 if net
["name"]==iface
["internal_name"]:
2870 iface
["vim_id"]=net
["vim_id"]
2873 logger
.debug("start scenario Deployment done")
2874 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2875 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2876 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2877 return mydb
.get_instance_scenario(instance_id
)
2879 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2880 _
, message
= rollback(mydb
, vims
, rollbackList
)
2881 if isinstance(e
, db_base_Exception
):
2882 error_text
= "Exception at database"
2884 error_text
= "Exception at VIM"
2885 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2886 #logger.error("start_scenario %s", error_text)
2887 raise NfvoException(error_text
, e
.http_code
)
2889 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2890 """ join the cloud config information into cloud_config_preserve.
2891 In case of conflict cloud_config_preserve preserves
2894 if not cloud_config_preserve
and not cloud_config
:
2897 new_cloud_config
= {"key-pairs":[], "users":[]}
2899 if cloud_config_preserve
:
2900 for key
in cloud_config_preserve
.get("key-pairs", () ):
2901 if key
not in new_cloud_config
["key-pairs"]:
2902 new_cloud_config
["key-pairs"].append(key
)
2904 for key
in cloud_config
.get("key-pairs", () ):
2905 if key
not in new_cloud_config
["key-pairs"]:
2906 new_cloud_config
["key-pairs"].append(key
)
2907 if not new_cloud_config
["key-pairs"]:
2908 del new_cloud_config
["key-pairs"]
2912 new_cloud_config
["users"] += cloud_config
.get("users", () )
2913 if cloud_config_preserve
:
2914 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2915 index_to_delete
= []
2916 users
= new_cloud_config
.get("users", [])
2917 for index0
in range(0,len(users
)):
2918 if index0
in index_to_delete
:
2920 for index1
in range(index0
+1,len(users
)):
2921 if index1
in index_to_delete
:
2923 if users
[index0
]["name"] == users
[index1
]["name"]:
2924 index_to_delete
.append(index1
)
2925 for key
in users
[index1
].get("key-pairs",()):
2926 if "key-pairs" not in users
[index0
]:
2927 users
[index0
]["key-pairs"] = [key
]
2928 elif key
not in users
[index0
]["key-pairs"]:
2929 users
[index0
]["key-pairs"].append(key
)
2930 index_to_delete
.sort(reverse
=True)
2931 for index
in index_to_delete
:
2933 if not new_cloud_config
["users"]:
2934 del new_cloud_config
["users"]
2937 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2938 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2939 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2940 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2943 new_cloud_config
["user-data"] = []
2944 if cloud_config
and cloud_config
.get("user-data"):
2945 if isinstance(cloud_config
["user-data"], list):
2946 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2948 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2949 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2950 if isinstance(cloud_config_preserve
["user-data"], list):
2951 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2953 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2954 if not new_cloud_config
["user-data"]:
2955 del new_cloud_config
["user-data"]
2958 new_cloud_config
["config-files"] = []
2959 if cloud_config
and cloud_config
.get("config-files") != None:
2960 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2961 if cloud_config_preserve
:
2962 for file in cloud_config_preserve
.get("config-files", ()):
2963 for index
in range(0, len(new_cloud_config
["config-files"])):
2964 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2965 new_cloud_config
["config-files"][index
] = file
2968 new_cloud_config
["config-files"].append(file)
2969 if not new_cloud_config
["config-files"]:
2970 del new_cloud_config
["config-files"]
2971 return new_cloud_config
2974 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2975 datacenter_id
= None
2976 datacenter_name
= None
2979 if datacenter_tenant_id
:
2980 thread_id
= datacenter_tenant_id
2981 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2983 where_
={"td.nfvo_tenant_id": tenant_id
}
2984 if datacenter_id_name
:
2985 if utils
.check_valid_uuid(datacenter_id_name
):
2986 datacenter_id
= datacenter_id_name
2987 where_
["dt.datacenter_id"] = datacenter_id
2989 datacenter_name
= datacenter_id_name
2990 where_
["d.name"] = datacenter_name
2991 if datacenter_tenant_id
:
2992 where_
["dt.uuid"] = datacenter_tenant_id
2993 datacenters
= mydb
.get_rows(
2994 SELECT
=("dt.uuid as datacenter_tenant_id, d.name as datacenter_name",),
2995 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2996 "join datacenters as d on d.uuid=dt.datacenter_id",
2998 if len(datacenters
) > 1:
2999 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
3001 thread_id
= datacenters
[0]["datacenter_tenant_id"]
3002 datacenter_name
= datacenters
[0]["datacenter_name"]
3003 thread
= vim_threads
["running"].get(thread_id
)
3005 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
)
3006 thread
= vim_thread(task_lock
, plugins
, thread_name
, None,
3009 vim_threads
["running"][thread_id
] = thread
3011 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3012 return thread_id
, thread
3013 except db_base_Exception
as e
:
3014 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
3017 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
3019 if utils
.check_valid_uuid(datacenter_id_name
):
3020 WHERE_dict
['d.uuid'] = datacenter_id_name
3022 WHERE_dict
['d.name'] = datacenter_id_name
3025 WHERE_dict
['nfvo_tenant_id'] = tenant_id
3026 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
3027 " dt on td.datacenter_tenant_id=dt.uuid"
3029 from_
= 'datacenters as d'
3030 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid, d.name as name",), WHERE
=WHERE_dict
)
3031 if len(vimaccounts
) == 0:
3032 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3033 elif len(vimaccounts
)>1:
3034 #print "nfvo.datacenter_action() error. Several datacenters found"
3035 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
3036 return vimaccounts
[0]["uuid"], vimaccounts
[0]["name"]
3039 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
3040 datacenter_id
= None
3041 datacenter_name
= None
3042 if datacenter_id_name
:
3043 if utils
.check_valid_uuid(datacenter_id_name
):
3044 datacenter_id
= datacenter_id_name
3046 datacenter_name
= datacenter_id_name
3047 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
3049 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3051 #print "nfvo.datacenter_action() error. Several datacenters found"
3052 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
3053 for vim_id
, vim_content
in vims
.items():
3054 return vim_id
, vim_content
3058 """Takes dict d and updates it with the values in dict u.
3059 It merges all depth levels"""
3060 for k
, v
in u
.items():
3061 if isinstance(v
, collections
.Mapping
):
3062 r
= update(d
.get(k
, {}), v
)
3069 def _get_wim(db
, wim_account_id
):
3070 # get wim from wim_account
3071 wim_accounts
= db
.get_rows(FROM
='wim_accounts', WHERE
={"uuid": wim_account_id
})
3072 if not wim_accounts
:
3073 raise NfvoException("Not found sdn id={}".format(wim_account_id
), http_code
=httperrors
.Not_Found
)
3074 return wim_accounts
[0]["wim_id"]
3077 def create_instance(mydb
, tenant_id
, instance_dict
):
3078 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3079 # logger.debug("Creating instance...")
3081 scenario
= instance_dict
["scenario"]
3083 # find main datacenter
3085 myvim_threads_id
= {}
3086 datacenter
= instance_dict
.get("datacenter")
3087 default_wim_account
= instance_dict
.get("wim_account")
3088 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3089 myvims
[default_datacenter_id
] = vim
3090 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
3091 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
3092 # myvim_tenant = myvim['tenant_id']
3095 # print "Checking that the scenario exists and getting the scenario dictionary"
3096 if isinstance(scenario
, str):
3097 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
3098 datacenter_id
=default_datacenter_id
)
3100 scenarioDict
= scenario
3101 scenarioDict
["uuid"] = None
3103 # logger.debug(">>>>>> Dictionaries before merging")
3104 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3105 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
3107 db_instance_vnfs
= []
3108 db_instance_vms
= []
3109 db_instance_interfaces
= []
3110 db_instance_sfis
= []
3111 db_instance_sfs
= []
3112 db_instance_classifications
= []
3113 db_instance_sfps
= []
3118 instance_name
= instance_dict
["name"]
3119 instance_uuid
= str(uuid4())
3120 uuid_list
.append(instance_uuid
)
3121 db_instance_scenario
= {
3122 "uuid": instance_uuid
,
3123 "name": instance_name
,
3124 "tenant_id": tenant_id
,
3125 "scenario_id": scenarioDict
['uuid'],
3126 "datacenter_id": default_datacenter_id
,
3127 # filled bellow 'datacenter_tenant_id'
3128 "description": instance_dict
.get("description"),
3130 if scenarioDict
.get("cloud-config"):
3131 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
3132 default_flow_style
=True, width
=256)
3133 instance_action_id
= get_task_id()
3134 db_instance_action
= {
3135 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3136 "tenant_id": tenant_id
,
3137 "instance_id": instance_uuid
,
3138 "description": "CREATE",
3141 # Auxiliary dictionaries from x to y
3142 sce_net2wim_instance
= {}
3143 sce_net2instance
= {}
3144 net2task_id
= {'scenario': {}}
3145 # Mapping between local networks and WIMs
3148 def ip_profile_IM2RO(ip_profile_im
):
3149 # translate from input format to database format
3151 if 'subnet-address' in ip_profile_im
:
3152 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
3153 if 'ip-version' in ip_profile_im
:
3154 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
3155 if 'gateway-address' in ip_profile_im
:
3156 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
3157 if 'dns-address' in ip_profile_im
:
3158 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
3159 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
3160 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
3161 if 'dhcp' in ip_profile_im
:
3162 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
3163 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
3164 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
3165 return ip_profile_ro
3167 # logger.debug("Creating instance from scenario-dict:\n%s",
3168 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3170 # 0 check correct parameters
3171 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).items():
3172 for scenario_net
in scenarioDict
['nets']:
3173 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3176 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3177 httperrors
.Bad_Request
)
3178 if "sites" not in net_instance_desc
:
3179 net_instance_desc
["sites"] = [ {} ]
3180 site_without_datacenter_field
= False
3181 for site
in net_instance_desc
["sites"]:
3182 if site
.get("datacenter"):
3183 site
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3184 if site
["datacenter"] not in myvims
:
3185 # Add this datacenter to myvims
3186 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3188 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3189 site
["datacenter"] = d
# change name to id
3191 if site_without_datacenter_field
:
3192 raise NfvoException("Found more than one entries without datacenter field at "
3193 "instance:networks:{}:sites".format(net_name
), httperrors
.Bad_Request
)
3194 site_without_datacenter_field
= True
3195 site
["datacenter"] = default_datacenter_id
# change name to id
3197 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).items():
3198 for scenario_vnf
in scenarioDict
['vnfs']:
3199 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3202 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), httperrors
.Bad_Request
)
3203 if "datacenter" in vnf_instance_desc
:
3204 # Add this datacenter to myvims
3205 vnf_instance_desc
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3206 if vnf_instance_desc
["datacenter"] not in myvims
:
3207 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3209 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3210 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3212 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).items():
3213 for scenario_net
in scenario_vnf
['nets']:
3214 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3217 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), httperrors
.Bad_Request
)
3218 if net_instance_desc
.get("vim-network-name"):
3219 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3220 if net_instance_desc
.get("vim-network-id"):
3221 scenario_net
["vim-network-id"] = net_instance_desc
["vim-network-id"]
3222 if net_instance_desc
.get("name"):
3223 scenario_net
["name"] = net_instance_desc
["name"]
3224 if 'ip-profile' in net_instance_desc
:
3225 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3226 if 'ip_profile' not in scenario_net
:
3227 scenario_net
['ip_profile'] = ipprofile_db
3229 update(scenario_net
['ip_profile'], ipprofile_db
)
3231 if 'provider-network' in net_instance_desc
:
3232 provider_network_db
= net_instance_desc
['provider-network']
3233 if 'provider-network' not in scenario_net
:
3234 scenario_net
['provider-network'] = provider_network_db
3236 update(scenario_net
['provider-network'], provider_network_db
)
3238 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).items():
3239 for scenario_vm
in scenario_vnf
['vms']:
3240 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3243 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3244 scenario_vm
["instance_parameters"] = vdu_instance_desc
3245 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).items():
3246 for scenario_interface
in scenario_vm
['interfaces']:
3247 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3248 scenario_interface
.update(iface_instance_desc
)
3251 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3253 # 0.1 parse cloud-config parameters
3254 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3256 # 0.2 merge instance information into scenario
3257 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3258 # However, this is not possible yet.
3259 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).items():
3260 for scenario_net
in scenarioDict
['nets']:
3261 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3262 if "wim_account" in net_instance_desc
and net_instance_desc
["wim_account"] is not None:
3263 scenario_net
["wim_account"] = net_instance_desc
["wim_account"]
3264 if 'ip-profile' in net_instance_desc
:
3265 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3266 if 'ip_profile' not in scenario_net
:
3267 scenario_net
['ip_profile'] = ipprofile_db
3269 update(scenario_net
['ip_profile'], ipprofile_db
)
3270 if 'provider-network' in net_instance_desc
:
3271 provider_network_db
= net_instance_desc
['provider-network']
3273 if 'provider-network' not in scenario_net
:
3274 scenario_net
['provider_network'] = provider_network_db
3276 update(scenario_net
['provider-network'], provider_network_db
)
3278 for interface
in net_instance_desc
.get('interfaces', ()):
3279 if 'ip_address' in interface
:
3280 for vnf
in scenarioDict
['vnfs']:
3281 if interface
['vnf'] == vnf
['name']:
3282 for vnf_interface
in vnf
['interfaces']:
3283 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3284 vnf_interface
['ip_address'] = interface
['ip_address']
3286 # logger.debug(">>>>>>>> Merged dictionary")
3287 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3288 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3291 # 1. Creating new nets (sce_nets) in the VIM"
3292 number_mgmt_networks
= 0
3293 db_instance_nets
= []
3294 db_instance_wim_nets
= []
3295 for sce_net
in scenarioDict
['nets']:
3297 sce_net_uuid
= sce_net
.get('uuid', sce_net
["name"])
3298 # get involved datacenters where this network need to be created
3299 involved_datacenters
= []
3300 for sce_vnf
in scenarioDict
.get("vnfs", ()):
3301 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3302 if vnf_datacenter
in involved_datacenters
:
3304 if sce_vnf
.get("interfaces"):
3305 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3306 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3307 involved_datacenters
.append(vnf_datacenter
)
3309 if not involved_datacenters
:
3310 involved_datacenters
.append(default_datacenter_id
)
3311 target_wim_account
= sce_net
.get("wim_account", default_wim_account
)
3314 # TODO: use this information during network creation
3315 wim_account_id
= wim_account_name
= None
3316 if len(involved_datacenters
) > 1 and 'uuid' in sce_net
:
3317 if target_wim_account
is None or target_wim_account
is True: # automatic selection of WIM
3318 # OBS: sce_net without uuid are used internally to VNFs
3319 # and the assumption is that VNFs will not be split among
3320 # different datacenters
3321 wim_account
= wim_engine
.find_suitable_wim_account(
3322 involved_datacenters
, tenant_id
)
3323 wim_account_id
= wim_account
['uuid']
3324 wim_account_name
= wim_account
['name']
3325 wim_usage
[sce_net
['uuid']] = wim_account_id
3326 elif isinstance(target_wim_account
, str): # manual selection of WIM
3327 wim_account
.persist
.get_wim_account_by(target_wim_account
, tenant_id
)
3328 wim_account_id
= wim_account
['uuid']
3329 wim_account_name
= wim_account
['name']
3330 wim_usage
[sce_net
['uuid']] = wim_account_id
3331 else: # not WIM usage
3332 wim_usage
[sce_net
['uuid']] = False
3336 if instance_dict
.get("networks"):
3337 if sce_net
.get("uuid") in instance_dict
["networks"]:
3338 descriptor_net
= instance_dict
["networks"][sce_net
["uuid"]]
3339 descriptor_net_name
= sce_net
["uuid"]
3340 elif sce_net
.get("osm_id") in instance_dict
["networks"]:
3341 descriptor_net
= instance_dict
["networks"][sce_net
["osm_id"]]
3342 descriptor_net_name
= sce_net
["osm_id"]
3343 elif sce_net
["name"] in instance_dict
["networks"]:
3344 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3345 descriptor_net_name
= sce_net
["name"]
3346 net_name
= descriptor_net
.get("vim-network-name")
3347 # add datacenters from instantiation parameters
3348 if descriptor_net
.get("sites"):
3349 for site
in descriptor_net
["sites"]:
3350 if site
.get("datacenter") and site
["datacenter"] not in involved_datacenters
:
3351 involved_datacenters
.append(site
["datacenter"])
3352 sce_net2instance
[sce_net_uuid
] = {}
3353 sce_net2wim_instance
[sce_net_uuid
] = {}
3354 net2task_id
['scenario'][sce_net_uuid
] = {}
3357 related_network
= None
3358 if descriptor_net
.get("use-network"):
3359 target_instance_nets
= mydb
.get_rows(
3361 FROM
="instance_nets",
3362 WHERE
={"instance_scenario_id": descriptor_net
["use-network"]["instance_scenario_id"],
3363 "osm_id": descriptor_net
["use-network"]["osm_id"]},
3365 if not target_instance_nets
:
3366 raise NfvoException(
3367 "Cannot find the target network at instance:networks[{}]:use-network".format(descriptor_net_name
),
3368 httperrors
.Bad_Request
)
3370 use_network
= target_instance_nets
[0]["related"]
3372 if sce_net
["external"]:
3373 number_mgmt_networks
+= 1
3375 for datacenter_id
in involved_datacenters
:
3377 netmap_create
= None
3378 if descriptor_net
.get("sites"):
3379 for site
in descriptor_net
["sites"]:
3380 if site
.get("datacenter") == datacenter_id
:
3381 netmap_use
= site
.get("netmap-use")
3382 netmap_create
= site
.get("netmap-create")
3385 vim
= myvims
[datacenter_id
]
3386 myvim_thread_id
= myvim_threads_id
[datacenter_id
]
3388 net_type
= sce_net
['type']
3390 lookfor_filter
= {'admin_state_up': True, 'status': 'ACTIVE'} # 'shared': True
3393 if sce_net
["external"]:
3394 net_name
= sce_net
["name"]
3396 net_name
= "{}-{}".format(instance_name
, sce_net
["name"])
3397 net_name
= net_name
[:255] # limit length
3399 if netmap_use
or netmap_create
:
3400 create_network
= False
3401 lookfor_network
= False
3403 lookfor_network
= True
3404 if utils
.check_valid_uuid(netmap_use
):
3405 lookfor_filter
["id"] = netmap_use
3407 lookfor_filter
["name"] = netmap_use
3409 create_network
= True
3410 net_vim_name
= net_name
3411 if isinstance(netmap_create
, str):
3412 net_vim_name
= netmap_create
3413 elif sce_net
.get("vim_network_name"):
3414 create_network
= False
3415 lookfor_network
= True
3416 lookfor_filter
["name"] = sce_net
.get("vim_network_name")
3417 elif sce_net
["external"]:
3418 if sce_net
.get('vim_id'):
3419 # there is a netmap at datacenter_nets database # TODO REVISE!!!!
3420 create_network
= False
3421 lookfor_network
= True
3422 lookfor_filter
["id"] = sce_net
['vim_id']
3423 elif vim
["config"].get("management_network_id") or vim
["config"].get("management_network_name"):
3424 if number_mgmt_networks
> 1:
3425 raise NfvoException("Found several VLD of type mgmt. "
3426 "You must concrete what vim-network must be use for each one",
3427 httperrors
.Bad_Request
)
3428 create_network
= False
3429 lookfor_network
= True
3430 if vim
["config"].get("management_network_id"):
3431 lookfor_filter
["id"] = vim
["config"]["management_network_id"]
3433 lookfor_filter
["name"] = vim
["config"]["management_network_name"]
3435 # There is not a netmap, look at datacenter for a net with this name and create if not found
3436 create_network
= True
3437 lookfor_network
= True
3438 lookfor_filter
["name"] = sce_net
["name"]
3439 net_vim_name
= sce_net
["name"]
3441 net_vim_name
= net_name
3442 create_network
= True
3443 lookfor_network
= False
3447 task_action
= "CREATE"
3448 task_extra
["params"] = (net_vim_name
, net_type
, sce_net
.get('ip_profile', None), False,
3449 sce_net
.get('provider_network', None), wim_account_name
)
3452 task_extra
["find"] = (lookfor_filter
,)
3453 elif lookfor_network
:
3454 task_action
= "FIND"
3455 task_extra
["params"] = (lookfor_filter
,)
3457 # fill database content
3458 net_uuid
= str(uuid4())
3459 uuid_list
.append(net_uuid
)
3460 sce_net2instance
[sce_net_uuid
][datacenter_id
] = net_uuid
3461 if not related_network
: # all db_instance_nets will have same related
3462 related_network
= use_network
or net_uuid
3464 sdn_controller
= vim
.config
.get('sdn-controller')
3465 sce_net2wim_instance
[sce_net_uuid
][datacenter_id
] = None
3466 if sdn_controller
and net_type
in ("data", "ptp"):
3467 wim_id
= _get_wim(mydb
, sdn_controller
)
3468 sdn_net_id
= str(uuid4())
3469 sce_net2wim_instance
[sce_net_uuid
][datacenter_id
] = sdn_net_id
3470 task_extra
["sdn_net_id"] = sdn_net_id
3471 db_instance_wim_nets
.append({
3473 "instance_scenario_id": instance_uuid
,
3474 "sce_net_id": sce_net
.get("uuid"),
3476 "wim_account_id": sdn_controller
,
3477 'status': 'BUILD', # if create_network else "ACTIVE"
3478 "related": related_network
,
3479 'multipoint': True if net_type
=="data" else False,
3480 "created": create_network
, # TODO py3
3483 task_wim_extra
= {"params": [net_type
, wim_account_name
]}
3485 "instance_action_id": instance_action_id
,
3486 "status": "SCHEDULED",
3487 "task_index": task_index
,
3488 # "datacenter_vim_id": myvim_thread_id,
3489 "wim_account_id": sdn_controller
,
3490 "action": task_action
,
3491 "item": "instance_wim_nets",
3492 "item_id": sdn_net_id
,
3493 "related": related_network
,
3494 "extra": yaml
.safe_dump(task_wim_extra
, default_flow_style
=True, width
=256)
3497 db_vim_actions
.append(db_vim_action
)
3500 "osm_id": sce_net
.get("osm_id") or sce_net
["name"],
3501 "related": related_network
,
3503 "vim_name": net_vim_name
,
3504 "instance_scenario_id": instance_uuid
,
3505 "sce_net_id": sce_net
.get("uuid"),
3506 "created": create_network
,
3507 'datacenter_id': datacenter_id
,
3508 'datacenter_tenant_id': myvim_thread_id
,
3509 'status': 'BUILD', # if create_network else "ACTIVE"
3510 'sdn_net_id': sdn_net_id
,
3512 db_instance_nets
.append(db_net
)
3514 "instance_action_id": instance_action_id
,
3515 "status": "SCHEDULED",
3516 "task_index": task_index
,
3517 "datacenter_vim_id": myvim_thread_id
,
3518 "action": task_action
,
3519 "item": "instance_nets",
3520 "item_id": net_uuid
,
3521 "related": related_network
,
3522 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3524 net2task_id
['scenario'][sce_net_uuid
][datacenter_id
] = task_index
3526 db_vim_actions
.append(db_vim_action
)
3528 if 'ip_profile' in sce_net
:
3530 'instance_net_id': net_uuid
,
3531 'ip_version': sce_net
['ip_profile']['ip_version'],
3532 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3533 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3534 'dns_address': sce_net
['ip_profile']['dns_address'],
3535 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3536 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3537 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3539 db_ip_profiles
.append(db_ip_profile
)
3543 "default_datacenter_id": default_datacenter_id
,
3544 "myvim_threads_id": myvim_threads_id
,
3545 "instance_uuid": instance_uuid
,
3546 "instance_name": instance_name
,
3547 "instance_action_id": instance_action_id
,
3549 "cloud_config": cloud_config
,
3550 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3551 "instance_parameters": instance_dict
,
3554 "task_index": task_index
,
3555 "uuid_list": uuid_list
,
3556 "db_instance_nets": db_instance_nets
,
3557 "db_instance_wim_nets": db_instance_wim_nets
,
3558 "db_vim_actions": db_vim_actions
,
3559 "db_ip_profiles": db_ip_profiles
,
3560 "db_instance_vnfs": db_instance_vnfs
,
3561 "db_instance_vms": db_instance_vms
,
3562 "db_instance_interfaces": db_instance_interfaces
,
3563 "net2task_id": net2task_id
,
3564 "sce_net2instance": sce_net2instance
,
3565 "sce_net2wim_instance": sce_net2wim_instance
,
3567 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3568 for sce_vnf
in scenarioDict
.get('vnfs', ()): # sce_vnf_list:
3569 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3570 task_index
= vnf_params_out
["task_index"]
3571 uuid_list
= vnf_params_out
["uuid_list"]
3574 # task_depends_on = []
3575 for vnffg
in scenarioDict
.get('vnffgs', ()):
3576 for rsp
in vnffg
['rsps']:
3578 for cp
in rsp
['connection_points']:
3579 count
= mydb
.get_rows(
3581 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3582 "on interfaces.uuid=h.ingress_interface_id",
3583 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3584 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3585 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3587 for instance_vm
in instance_vms
:
3588 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3590 dependencies
.append(action
['task_index'])
3591 # TODO: throw exception if count != len(instance_vms)
3592 # TODO: and action shouldn't ever be None
3594 for i
in range(count
):
3596 sfi_uuid
= str(uuid4())
3598 "ingress_interface_id": cp
["ingress_interface_id"],
3599 "egress_interface_id": cp
["egress_interface_id"]
3601 uuid_list
.append(sfi_uuid
)
3604 "related": sfi_uuid
,
3605 "instance_scenario_id": instance_uuid
,
3606 'sce_rsp_hop_id': cp
['uuid'],
3607 'datacenter_id': datacenter_id
,
3608 'datacenter_tenant_id': myvim_thread_id
,
3609 "vim_sfi_id": None, # vim thread will populate
3611 db_instance_sfis
.append(db_sfi
)
3613 "instance_action_id": instance_action_id
,
3614 "task_index": task_index
,
3615 "datacenter_vim_id": myvim_thread_id
,
3617 "status": "SCHEDULED",
3618 "item": "instance_sfis",
3619 "item_id": sfi_uuid
,
3620 "related": sfi_uuid
,
3621 "extra": yaml
.safe_dump({"params": extra_params
, "depends_on": [dependencies
[i
]]},
3622 default_flow_style
=True, width
=256)
3624 sfis_created
.append(task_index
)
3626 db_vim_actions
.append(db_vim_action
)
3628 sf_uuid
= str(uuid4())
3629 uuid_list
.append(sf_uuid
)
3633 "instance_scenario_id": instance_uuid
,
3634 'sce_rsp_hop_id': cp
['uuid'],
3635 'datacenter_id': datacenter_id
,
3636 'datacenter_tenant_id': myvim_thread_id
,
3637 "vim_sf_id": None, # vim thread will populate
3639 db_instance_sfs
.append(db_sf
)
3641 "instance_action_id": instance_action_id
,
3642 "task_index": task_index
,
3643 "datacenter_vim_id": myvim_thread_id
,
3645 "status": "SCHEDULED",
3646 "item": "instance_sfs",
3649 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3650 default_flow_style
=True, width
=256)
3652 sfs_created
.append(task_index
)
3654 db_vim_actions
.append(db_vim_action
)
3655 classifier
= rsp
['classifier']
3657 # TODO the following ~13 lines can be reused for the sfi case
3658 count
= mydb
.get_rows(
3659 SELECT
=('vms.count'),
3660 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3661 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3662 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3663 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3665 for instance_vm
in instance_vms
:
3666 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3668 dependencies
.append(action
['task_index'])
3669 # TODO: throw exception if count != len(instance_vms)
3670 # TODO: and action shouldn't ever be None
3671 classifications_created
= []
3672 for i
in range(count
):
3673 for match
in classifier
['matches']:
3674 # create classifications
3675 classification_uuid
= str(uuid4())
3676 uuid_list
.append(classification_uuid
)
3677 db_classification
= {
3678 "uuid": classification_uuid
,
3679 "related": classification_uuid
,
3680 "instance_scenario_id": instance_uuid
,
3681 'sce_classifier_match_id': match
['uuid'],
3682 'datacenter_id': datacenter_id
,
3683 'datacenter_tenant_id': myvim_thread_id
,
3684 "vim_classification_id": None, # vim thread will populate
3686 db_instance_classifications
.append(db_classification
)
3687 classification_params
= {
3688 "ip_proto": match
["ip_proto"],
3689 "source_ip": match
["source_ip"],
3690 "destination_ip": match
["destination_ip"],
3691 "source_port": match
["source_port"],
3692 "destination_port": match
["destination_port"],
3693 "logical_source_port": classifier
["interface_id"]
3696 "instance_action_id": instance_action_id
,
3697 "task_index": task_index
,
3698 "datacenter_vim_id": myvim_thread_id
,
3700 "status": "SCHEDULED",
3701 "item": "instance_classifications",
3702 "item_id": classification_uuid
,
3703 "related": classification_uuid
,
3704 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3705 default_flow_style
=True, width
=256)
3707 classifications_created
.append(task_index
)
3709 db_vim_actions
.append(db_vim_action
)
3712 sfp_uuid
= str(uuid4())
3713 uuid_list
.append(sfp_uuid
)
3716 "related": sfp_uuid
,
3717 "instance_scenario_id": instance_uuid
,
3718 'sce_rsp_id': rsp
['uuid'],
3719 'datacenter_id': datacenter_id
,
3720 'datacenter_tenant_id': myvim_thread_id
,
3721 "vim_sfp_id": None, # vim thread will populate
3723 db_instance_sfps
.append(db_sfp
)
3725 "instance_action_id": instance_action_id
,
3726 "task_index": task_index
,
3727 "datacenter_vim_id": myvim_thread_id
,
3729 "status": "SCHEDULED",
3730 "item": "instance_sfps",
3731 "item_id": sfp_uuid
,
3732 "related": sfp_uuid
,
3733 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3734 default_flow_style
=True, width
=256)
3737 db_vim_actions
.append(db_vim_action
)
3738 db_instance_action
["number_tasks"] = task_index
3741 logger
.debug('wim_usage:\n%s\n\n', pformat(wim_usage
))
3742 wan_links
= wim_engine
.derive_wan_links(wim_usage
, db_instance_nets
, tenant_id
)
3743 wim_actions
= wim_engine
.create_actions(wan_links
)
3744 wim_actions
, db_instance_action
= (
3745 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
3748 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3750 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3751 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3753 {"instance_scenarios": db_instance_scenario
},
3754 {"instance_vnfs": db_instance_vnfs
},
3755 {"instance_nets": db_instance_nets
},
3756 {"ip_profiles": db_ip_profiles
},
3757 {"instance_vms": db_instance_vms
},
3758 {"instance_interfaces": db_instance_interfaces
},
3759 {"instance_actions": db_instance_action
},
3760 {"instance_sfis": db_instance_sfis
},
3761 {"instance_sfs": db_instance_sfs
},
3762 {"instance_classifications": db_instance_classifications
},
3763 {"instance_sfps": db_instance_sfps
},
3764 {"instance_wim_nets": db_instance_wim_nets
+ wan_links
},
3765 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
3768 logger
.debug("create_instance done DB tables: %s",
3769 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3770 mydb
.new_rows(db_tables
, uuid_list
)
3771 for myvim_thread_id
in myvim_threads_id
.values():
3772 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3774 wim_engine
.dispatch(wim_actions
)
3776 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3777 returned_instance
["action_id"] = instance_action_id
3778 return returned_instance
3779 except (NfvoException
, vimconn
.vimconnException
, sdnconn
.SdnConnectorError
, db_base_Exception
) as e
:
3780 message
= rollback(mydb
, myvims
, rollbackList
)
3781 if isinstance(e
, db_base_Exception
):
3782 error_text
= "database Exception"
3783 elif isinstance(e
, vimconn
.vimconnException
):
3784 error_text
= "VIM Exception"
3785 elif isinstance(e
, sdnconn
.SdnConnectorError
):
3786 error_text
= "WIM Exception"
3788 error_text
= "Exception"
3789 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3790 # logger.error("create_instance: %s", error_text)
3792 raise NfvoException(error_text
, e
.http_code
)
3795 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3796 default_datacenter_id
= params
["default_datacenter_id"]
3797 myvim_threads_id
= params
["myvim_threads_id"]
3798 instance_uuid
= params
["instance_uuid"]
3799 instance_name
= params
["instance_name"]
3800 instance_action_id
= params
["instance_action_id"]
3801 myvims
= params
["myvims"]
3802 cloud_config
= params
["cloud_config"]
3803 RO_pub_key
= params
["RO_pub_key"]
3805 task_index
= params_out
["task_index"]
3806 uuid_list
= params_out
["uuid_list"]
3807 db_instance_nets
= params_out
["db_instance_nets"]
3808 db_instance_wim_nets
= params_out
["db_instance_wim_nets"]
3809 db_vim_actions
= params_out
["db_vim_actions"]
3810 db_ip_profiles
= params_out
["db_ip_profiles"]
3811 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3812 db_instance_vms
= params_out
["db_instance_vms"]
3813 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3814 net2task_id
= params_out
["net2task_id"]
3815 sce_net2instance
= params_out
["sce_net2instance"]
3816 sce_net2wim_instance
= params_out
["sce_net2wim_instance"]
3818 vnf_net2instance
= {}
3819 vnf_net2wim_instance
= {}
3821 # 2. Creating new nets (vnf internal nets) in the VIM"
3822 # For each vnf net, we create it and we add it to instanceNetlist.
3823 if sce_vnf
.get("datacenter"):
3824 vim
= myvims
[sce_vnf
["datacenter"]]
3825 datacenter_id
= sce_vnf
["datacenter"]
3826 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3828 vim
= myvims
[default_datacenter_id
]
3829 datacenter_id
= default_datacenter_id
3830 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3831 for net
in sce_vnf
['nets']:
3833 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3834 # net_name = descriptor_net.get("name")
3837 net_name
= "{}-{}".format(instance_name
, net
["name"])
3838 net_name
= net_name
[:255] # limit length
3839 net_type
= net
['type']
3841 if sce_vnf
['uuid'] not in vnf_net2instance
:
3842 vnf_net2instance
[sce_vnf
['uuid']] = {}
3843 if sce_vnf
['uuid'] not in net2task_id
:
3844 net2task_id
[sce_vnf
['uuid']] = {}
3846 # fill database content
3847 net_uuid
= str(uuid4())
3848 uuid_list
.append(net_uuid
)
3849 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3851 sdn_controller
= vim
.config
.get('sdn-controller')
3853 if sdn_controller
and net_type
in ("data", "ptp"):
3854 wim_id
= _get_wim(mydb
, sdn_controller
)
3855 sdn_net_id
= str(uuid4())
3856 db_instance_wim_nets
.append({
3858 "instance_scenario_id": instance_uuid
,
3860 "wim_account_id": sdn_controller
,
3861 'status': 'BUILD', # if create_network else "ACTIVE"
3862 "related": net_uuid
,
3863 'multipoint': True if net_type
== "data" else False,
3864 "created": True, # TODO py3
3867 vnf_net2wim_instance
[net_uuid
] = sdn_net_id
3871 "related": net_uuid
,
3873 "vim_name": net_name
,
3874 "instance_scenario_id": instance_uuid
,
3875 "net_id": net
["uuid"],
3877 'datacenter_id': datacenter_id
,
3878 'datacenter_tenant_id': myvim_thread_id
,
3879 'sdn_net_id': sdn_net_id
,
3881 db_instance_nets
.append(db_net
)
3884 if net
.get("vim-network-name"):
3885 lookfor_filter
["name"] = net
["vim-network-name"]
3886 if net
.get("vim-network-id"):
3887 lookfor_filter
["id"] = net
["vim-network-id"]
3889 task_action
= "FIND"
3890 task_extra
= {"params": (lookfor_filter
,)}
3892 task_action
= "CREATE"
3893 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3895 task_extra
["sdn_net_id"] = sdn_net_id
3898 task_wim_extra
= {"params": [net_type
, None]}
3900 "instance_action_id": instance_action_id
,
3901 "status": "SCHEDULED",
3902 "task_index": task_index
,
3903 # "datacenter_vim_id": myvim_thread_id,
3904 "wim_account_id": sdn_controller
,
3905 "action": task_action
,
3906 "item": "instance_wim_nets",
3907 "item_id": sdn_net_id
,
3908 "related": net_uuid
,
3909 "extra": yaml
.safe_dump(task_wim_extra
, default_flow_style
=True, width
=256)
3912 db_vim_actions
.append(db_vim_action
)
3914 "instance_action_id": instance_action_id
,
3915 "task_index": task_index
,
3916 "datacenter_vim_id": myvim_thread_id
,
3917 "status": "SCHEDULED",
3918 "action": task_action
,
3919 "item": "instance_nets",
3920 "item_id": net_uuid
,
3921 "related": net_uuid
,
3922 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3924 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3926 db_vim_actions
.append(db_vim_action
)
3928 if 'ip_profile' in net
:
3930 'instance_net_id': net_uuid
,
3931 'ip_version': net
['ip_profile']['ip_version'],
3932 'subnet_address': net
['ip_profile']['subnet_address'],
3933 'gateway_address': net
['ip_profile']['gateway_address'],
3934 'dns_address': net
['ip_profile']['dns_address'],
3935 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3936 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3937 'dhcp_count': net
['ip_profile']['dhcp_count'],
3939 db_ip_profiles
.append(db_ip_profile
)
3941 # print "vnf_net2instance:"
3942 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3944 # 3. Creating new vm instances in the VIM
3945 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3947 if sce_vnf
.get('mgmt_access'):
3948 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3949 vnf_availability_zones
= []
3950 for vm
in sce_vnf
.get('vms'):
3951 vm_av
= vm
.get('availability_zone')
3952 if vm_av
and vm_av
not in vnf_availability_zones
:
3953 vnf_availability_zones
.append(vm_av
)
3955 # check if there is enough availability zones available at vim level.
3956 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3957 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3958 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
3960 if sce_vnf
.get("datacenter"):
3961 vim
= myvims
[sce_vnf
["datacenter"]]
3962 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3963 datacenter_id
= sce_vnf
["datacenter"]
3965 vim
= myvims
[default_datacenter_id
]
3966 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3967 datacenter_id
= default_datacenter_id
3968 sce_vnf
["datacenter_id"] = datacenter_id
3971 vnf_uuid
= str(uuid4())
3972 uuid_list
.append(vnf_uuid
)
3975 'instance_scenario_id': instance_uuid
,
3976 'vnf_id': sce_vnf
['vnf_id'],
3977 'sce_vnf_id': sce_vnf
['uuid'],
3978 'datacenter_id': datacenter_id
,
3979 'datacenter_tenant_id': myvim_thread_id
,
3981 db_instance_vnfs
.append(db_instance_vnf
)
3983 for vm
in sce_vnf
['vms']:
3985 if vm
.get("pdu_type"):
3989 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3990 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3991 myVMDict
['description'] = myVMDict
['name'][0:99]
3993 # myVMDict['start'] = "no"
3994 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3995 myVMDict
['name'] = vm
["instance_parameters"].get("name")
3996 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
3997 # create image at vim in case it not exist
3998 image_uuid
= vm
['image_id']
3999 if vm
.get("image_list"):
4000 for alternative_image
in vm
["image_list"]:
4001 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
4002 image_uuid
= alternative_image
['image_id']
4004 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
4005 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
4006 vm
['vim_image_id'] = image_id
4008 # create flavor at vim in case it not exist
4009 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
4010 if flavor_dict
['extended'] != None:
4011 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'], Loader
=yaml
.Loader
)
4012 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
4014 # Obtain information for additional disks
4015 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
4016 WHERE
={'vim_id': flavor_id
})
4017 if not extended_flavor_dict
:
4018 raise NfvoException("flavor '{}' not found".format(flavor_id
), httperrors
.Not_Found
)
4020 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0], Loader=yaml.Loader)
4021 myVMDict
['disks'] = None
4022 extended_info
= extended_flavor_dict
[0]['extended']
4023 if extended_info
!= None:
4024 extended_flavor_dict_yaml
= yaml
.load(extended_info
, Loader
=yaml
.Loader
)
4025 if 'disks' in extended_flavor_dict_yaml
:
4026 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
4027 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
4028 for disk
in myVMDict
['disks']:
4029 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
4030 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
4032 vm
['vim_flavor_id'] = flavor_id
4033 myVMDict
['imageRef'] = vm
['vim_image_id']
4034 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
4035 myVMDict
['availability_zone'] = vm
.get('availability_zone')
4036 myVMDict
['networks'] = []
4037 task_depends_on
= []
4038 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
4039 is_management_vm
= False
4041 for iface
in vm
['interfaces']:
4043 if iface
['type'] == "data":
4044 netDict
['type'] = iface
['model']
4045 elif "model" in iface
and iface
["model"] != None:
4046 netDict
['model'] = iface
['model']
4047 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
4048 # is obtained from iterface table model
4049 # discover type of interface looking at flavor
4050 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
4051 for flavor_iface
in numa
.get('interfaces', []):
4052 if flavor_iface
.get('name') == iface
['internal_name']:
4053 if flavor_iface
['dedicated'] == 'yes':
4054 netDict
['type'] = "PF" # passthrough
4055 elif flavor_iface
['dedicated'] == 'no':
4056 netDict
['type'] = "VF" # siov
4057 elif flavor_iface
['dedicated'] == 'yes:sriov':
4058 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
4059 netDict
["mac_address"] = flavor_iface
.get("mac_address")
4061 netDict
["use"] = iface
['type']
4062 if netDict
["use"] == "data" and not netDict
.get("type"):
4063 # print "netDict", netDict
4064 # print "iface", iface
4065 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
4066 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
4067 if flavor_dict
.get('extended') == None:
4068 raise NfvoException(e_text
+ "After database migration some information is not available. \
4069 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
4071 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
4072 if netDict
["use"] == "mgmt":
4073 is_management_vm
= True
4074 netDict
["type"] = "virtual"
4075 if netDict
["use"] == "bridge":
4076 netDict
["type"] = "virtual"
4077 if iface
.get("vpci"):
4078 netDict
['vpci'] = iface
['vpci']
4079 if iface
.get("mac"):
4080 netDict
['mac_address'] = iface
['mac']
4081 if iface
.get("mac_address"):
4082 netDict
['mac_address'] = iface
['mac_address']
4083 if iface
.get("ip_address"):
4084 netDict
['ip_address'] = iface
['ip_address']
4085 if iface
.get("port-security") is not None:
4086 netDict
['port_security'] = iface
['port-security']
4087 if iface
.get("floating-ip") is not None:
4088 netDict
['floating_ip'] = iface
['floating-ip']
4089 netDict
['name'] = iface
['internal_name']
4090 if iface
['net_id'] is None:
4091 for vnf_iface
in sce_vnf
["interfaces"]:
4094 if vnf_iface
['interface_id'] == iface
['uuid']:
4095 netDict
['net_id'] = "TASK-{}".format(
4096 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
4097 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
4098 instance_wim_net_id
= sce_net2wim_instance
[vnf_iface
['sce_net_id']][datacenter_id
]
4099 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
4102 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
4103 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
4104 instance_wim_net_id
= vnf_net2wim_instance
.get(instance_net_id
)
4105 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
4106 # skip bridge ifaces not connected to any net
4107 if 'net_id' not in netDict
or netDict
['net_id'] == None:
4109 myVMDict
['networks'].append(netDict
)
4112 # 'instance_vm_id': instance_vm_uuid,
4113 "instance_net_id": instance_net_id
,
4114 "instance_wim_net_id": instance_wim_net_id
,
4115 'interface_id': iface
['uuid'],
4116 # 'vim_interface_id': ,
4117 'type': 'external' if iface
['external_name'] is not None else 'internal',
4118 'model': iface
['model'],
4119 'ip_address': iface
.get('ip_address'),
4120 'mac_address': iface
.get('mac'),
4121 'floating_ip': int(iface
.get('floating-ip', False)),
4122 'port_security': int(iface
.get('port-security', True))
4124 db_vm_ifaces
.append(db_vm_iface
)
4125 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
4126 # print myVMDict['name']
4127 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
4128 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
4129 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
4131 # We add the RO key to cloud_config if vnf will need ssh access
4132 cloud_config_vm
= cloud_config
4133 if is_management_vm
and params
["instance_parameters"].get("mgmt_keys"):
4134 cloud_config_vm
= unify_cloud_config({"key-pairs": params
["instance_parameters"]["mgmt_keys"]},
4137 if vm
.get("instance_parameters") and "mgmt_keys" in vm
["instance_parameters"]:
4138 if vm
["instance_parameters"]["mgmt_keys"]:
4139 cloud_config_vm
= unify_cloud_config({"key-pairs": vm
["instance_parameters"]["mgmt_keys"]},
4142 cloud_config_vm
= unify_cloud_config(cloud_config_vm
, {"key-pairs": [RO_pub_key
]})
4143 if vm
.get("boot_data"):
4144 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
4146 if myVMDict
.get('availability_zone'):
4147 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
4150 for vm_index
in range(0, vm
.get('count', 1)):
4151 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
4152 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
4153 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
4154 myVMDict
['disks'], av_index
, vnf_availability_zones
)
4155 # put interface uuid back to scenario[vnfs][vms[[interfaces]
4156 for net
in myVMDict
['networks']:
4158 for iface
in vm
['interfaces']:
4159 if net
["name"] == iface
["internal_name"]:
4160 iface
["vim_id"] = net
["vim_id"]
4162 vm_uuid
= str(uuid4())
4163 uuid_list
.append(vm_uuid
)
4167 'instance_vnf_id': vnf_uuid
,
4168 # TODO delete "vim_vm_id": vm_id,
4169 "vm_id": vm
["uuid"],
4170 "vim_name": vm_name
,
4173 db_instance_vms
.append(db_vm
)
4176 for db_vm_iface
in db_vm_ifaces
:
4177 iface_uuid
= str(uuid4())
4178 uuid_list
.append(iface_uuid
)
4179 db_vm_iface_instance
= {
4181 "instance_vm_id": vm_uuid
4183 db_vm_iface_instance
.update(db_vm_iface
)
4184 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
4185 ip
= db_vm_iface_instance
.get("ip_address")
4190 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4191 db_vm_iface_instance
["ip_address"] = ip
4193 db_vm_iface_instance
["ip_address"] = None
4194 db_instance_interfaces
.append(db_vm_iface_instance
)
4195 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
4199 "instance_action_id": instance_action_id
,
4200 "task_index": task_index
,
4201 "datacenter_vim_id": myvim_thread_id
,
4203 "status": "SCHEDULED",
4204 "item": "instance_vms",
4207 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
4208 default_flow_style
=True, width
=256)
4211 db_vim_actions
.append(db_vim_action
)
4212 params_out
["task_index"] = task_index
4213 params_out
["uuid_list"] = uuid_list
4216 def delete_instance(mydb
, tenant_id
, instance_id
):
4217 # print "Checking that the instance_id exists and getting the instance dictionary"
4218 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
4219 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4220 tenant_id
= instanceDict
["tenant_id"]
4223 # We need to retrieve the WIM Actions now, before the instance_scenario is
4224 # deleted. The reason for that is that: ON CASCADE rules will delete the
4225 # instance_wim_nets record in the database
4226 wim_actions
= wim_engine
.delete_actions(instance_scenario_id
=instance_id
)
4229 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4230 # 1. Delete from Database
4231 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
4233 # 2. delete from VIM
4237 vimthread_affected
= {}
4238 net2vm_dependencies
= {}
4241 instance_action_id
= get_task_id()
4243 db_instance_action
= {
4244 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4245 "tenant_id": tenant_id
,
4246 "instance_id": instance_id
,
4247 "description": "DELETE",
4248 # "number_tasks": 0 # filled bellow
4251 # 2.1 deleting VNFFGs
4252 for sfp
in instanceDict
.get('sfps', ()):
4253 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
4254 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4255 if datacenter_key
not in myvims
:
4257 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4258 except NfvoException
as e
:
4259 logger
.error(str(e
))
4261 myvim_threads
[datacenter_key
] = myvim_thread
4262 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
4263 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
4265 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
4266 myvims
[datacenter_key
] = None
4268 myvims
[datacenter_key
] = next(iter(vims
.values()))
4269 myvim
= myvims
[datacenter_key
]
4270 myvim_thread
= myvim_threads
[datacenter_key
]
4273 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
4275 extra
= {"params": (sfp
['vim_sfp_id'])}
4277 "instance_action_id": instance_action_id
,
4278 "task_index": task_index
,
4279 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
4281 "status": "SCHEDULED",
4282 "item": "instance_sfps",
4283 "item_id": sfp
["uuid"],
4284 "related": sfp
["related"],
4285 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4288 db_vim_actions
.append(db_vim_action
)
4290 for classification
in instanceDict
['classifications']:
4291 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4292 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4293 if datacenter_key
not in myvims
:
4295 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4296 except NfvoException
as e
:
4297 logger
.error(str(e
))
4299 myvim_threads
[datacenter_key
] = myvim_thread
4300 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4301 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4303 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"],
4304 classification
["datacenter_tenant_id"]))
4305 myvims
[datacenter_key
] = None
4307 myvims
[datacenter_key
] = next(iter(vims
.values()))
4308 myvim
= myvims
[datacenter_key
]
4309 myvim_thread
= myvim_threads
[datacenter_key
]
4312 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'],
4313 classification
["datacenter_id"])
4315 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4316 extra
= {"params": (classification
['vim_classification_id']), "depends_on": depends_on
}
4318 "instance_action_id": instance_action_id
,
4319 "task_index": task_index
,
4320 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4322 "status": "SCHEDULED",
4323 "item": "instance_classifications",
4324 "item_id": classification
["uuid"],
4325 "related": classification
["related"],
4326 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4329 db_vim_actions
.append(db_vim_action
)
4331 for sf
in instanceDict
.get('sfs', ()):
4332 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
4333 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4334 if datacenter_key
not in myvims
:
4336 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4337 except NfvoException
as e
:
4338 logger
.error(str(e
))
4340 myvim_threads
[datacenter_key
] = myvim_thread
4341 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
4342 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
4344 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
4345 myvims
[datacenter_key
] = None
4347 myvims
[datacenter_key
] = next(iter(vims
.values()))
4348 myvim
= myvims
[datacenter_key
]
4349 myvim_thread
= myvim_threads
[datacenter_key
]
4352 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
4354 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4355 extra
= {"params": (sf
['vim_sf_id']), "depends_on": depends_on
}
4357 "instance_action_id": instance_action_id
,
4358 "task_index": task_index
,
4359 "datacenter_vim_id": sf
["datacenter_tenant_id"],
4361 "status": "SCHEDULED",
4362 "item": "instance_sfs",
4363 "item_id": sf
["uuid"],
4364 "related": sf
["related"],
4365 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4368 db_vim_actions
.append(db_vim_action
)
4370 for sfi
in instanceDict
.get('sfis', ()):
4371 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4372 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4373 if datacenter_key
not in myvims
:
4375 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4376 except NfvoException
as e
:
4377 logger
.error(str(e
))
4379 myvim_threads
[datacenter_key
] = myvim_thread
4380 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4381 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4383 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4384 myvims
[datacenter_key
] = None
4386 myvims
[datacenter_key
] = next(iter(vims
.values()))
4387 myvim
= myvims
[datacenter_key
]
4388 myvim_thread
= myvim_threads
[datacenter_key
]
4391 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4393 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfs"]
4394 extra
= {"params": (sfi
['vim_sfi_id']), "depends_on": depends_on
}
4396 "instance_action_id": instance_action_id
,
4397 "task_index": task_index
,
4398 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4400 "status": "SCHEDULED",
4401 "item": "instance_sfis",
4402 "item_id": sfi
["uuid"],
4403 "related": sfi
["related"],
4404 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4407 db_vim_actions
.append(db_vim_action
)
4411 for sce_vnf
in instanceDict
.get('vnfs', ()):
4412 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4413 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
4414 if datacenter_key
not in myvims
:
4416 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4417 except NfvoException
as e
:
4418 logger
.error(str(e
))
4420 myvim_threads
[datacenter_key
] = myvim_thread
4421 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
4422 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
4424 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
4425 sce_vnf
["datacenter_tenant_id"]))
4426 myvims
[datacenter_key
] = None
4428 myvims
[datacenter_key
] = next(iter(vims
.values()))
4429 myvim
= myvims
[datacenter_key
]
4430 myvim_thread
= myvim_threads
[datacenter_key
]
4432 for vm
in sce_vnf
['vms']:
4434 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
4436 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4438 "instance_action_id": instance_action_id
,
4439 "task_index": task_index
,
4440 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
4442 "status": "SCHEDULED",
4443 "item": "instance_vms",
4444 "item_id": vm
["uuid"],
4445 "related": vm
["related"],
4446 "extra": yaml
.safe_dump({"params": vm
["interfaces"], "depends_on": sfi_dependencies
},
4447 default_flow_style
=True, width
=256)
4449 db_vim_actions
.append(db_vim_action
)
4450 for interface
in vm
["interfaces"]:
4451 if not interface
.get("instance_net_id"):
4453 if interface
["instance_net_id"] not in net2vm_dependencies
:
4454 net2vm_dependencies
[interface
["instance_net_id"]] = []
4455 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
4460 for net
in instanceDict
['nets']:
4461 vimthread_affected
[net
["datacenter_tenant_id"]] = None
4462 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
4463 if datacenter_key
not in myvims
:
4465 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, net
["datacenter_id"], net
["datacenter_tenant_id"])
4466 except NfvoException
as e
:
4467 logger
.error(str(e
))
4469 myvim_threads
[datacenter_key
] = myvim_thread
4470 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
4471 datacenter_tenant_id
=net
["datacenter_tenant_id"])
4473 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
4474 myvims
[datacenter_key
] = None
4476 myvims
[datacenter_key
] = next(iter(vims
.values()))
4477 myvim
= myvims
[datacenter_key
]
4478 myvim_thread
= myvim_threads
[datacenter_key
]
4481 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
4483 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
4484 if net2vm_dependencies
.get(net
["uuid"]):
4485 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
4486 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4487 if len(sfi_dependencies
) > 0:
4488 if "depends_on" in extra
:
4489 extra
["depends_on"] += sfi_dependencies
4491 extra
["depends_on"] = sfi_dependencies
4493 "instance_action_id": instance_action_id
,
4494 "task_index": task_index
,
4495 "datacenter_vim_id": net
["datacenter_tenant_id"],
4497 "status": "SCHEDULED",
4498 "item": "instance_nets",
4499 "item_id": net
["uuid"],
4500 "related": net
["related"],
4501 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4504 db_vim_actions
.append(db_vim_action
)
4505 for sdn_net
in instanceDict
['sdn_nets']:
4506 if not sdn_net
["sdn"]:
4510 "instance_action_id": instance_action_id
,
4511 "task_index": task_index
,
4512 "wim_account_id": sdn_net
["wim_account_id"],
4514 "status": "SCHEDULED",
4515 "item": "instance_wim_nets",
4516 "item_id": sdn_net
["uuid"],
4517 "related": sdn_net
["related"],
4518 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4521 db_vim_actions
.append(db_vim_action
)
4523 db_instance_action
["number_tasks"] = task_index
4526 wim_actions
, db_instance_action
= (
4527 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
4531 {"instance_actions": db_instance_action
},
4532 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
4535 logger
.debug("delete_instance done DB tables: %s",
4536 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4537 mydb
.new_rows(db_tables
, ())
4538 for myvim_thread_id
in vimthread_affected
.keys():
4539 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4541 wim_engine
.dispatch(wim_actions
)
4543 if len(error_msg
) > 0:
4544 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4545 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4547 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4549 def get_instance_id(mydb
, tenant_id
, instance_id
):
4551 #check valid tenant_id
4552 check_tenant(mydb
, tenant_id
)
4555 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4557 # for net in instance_dict["nets"]:
4558 # if net.get("sdn_net_id"):
4559 # net_sdn = ovim.show_network(net["sdn_net_id"])
4560 # net["sdn_info"] = {
4561 # "admin_state_up": net_sdn.get("admin_state_up"),
4562 # "flows": net_sdn.get("flows"),
4563 # "last_error": net_sdn.get("last_error"),
4564 # "ports": net_sdn.get("ports"),
4565 # "type": net_sdn.get("type"),
4566 # "status": net_sdn.get("status"),
4567 # "vlan": net_sdn.get("vlan"),
4569 return instance_dict
4571 @deprecated("Instance is automatically refreshed by vim_threads")
4572 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4573 '''Refreshes a scenario instance. It modifies instanceDict'''
4575 - 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
4578 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4579 # #print "nfvo.refresh_instance begins"
4580 # #print json.dumps(instanceDict, indent=4)
4582 # #print "Getting the VIM URL and the VIM tenant_id"
4585 # # 1. Getting VIM vm and net list
4586 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4589 # for sce_vnf in instanceDict['vnfs']:
4590 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4591 # if datacenter_key not in vm_list:
4592 # vm_list[datacenter_key] = []
4593 # if datacenter_key not in myvims:
4594 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4595 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4596 # if len(vims) == 0:
4597 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4598 # myvims[datacenter_key] = None
4600 # myvims[datacenter_key] = next(iter(vims.values()))
4601 # for vm in sce_vnf['vms']:
4602 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4603 # vms_notupdated.append(vm["uuid"])
4605 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4606 # nets_notupdated=[]
4608 # for net in instanceDict['nets']:
4609 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4610 # if datacenter_key not in net_list:
4611 # net_list[datacenter_key] = []
4612 # if datacenter_key not in myvims:
4613 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4614 # datacenter_tenant_id=net["datacenter_tenant_id"])
4615 # if len(vims) == 0:
4616 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4617 # myvims[datacenter_key] = None
4619 # myvims[datacenter_key] = next(iter(vims.values()))
4621 # net_list[datacenter_key].append(net['vim_net_id'])
4622 # nets_notupdated.append(net["uuid"])
4624 # # 1. Getting the status of all VMs
4626 # for datacenter_key in myvims:
4627 # if not vm_list.get(datacenter_key):
4631 # if not myvims[datacenter_key]:
4632 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4635 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4637 # except vimconn.vimconnException as e:
4638 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4639 # failed_message = str(e)
4641 # for vm in vm_list[datacenter_key]:
4642 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4644 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4645 # for sce_vnf in instanceDict['vnfs']:
4646 # for vm in sce_vnf['vms']:
4647 # vm_id = vm['vim_vm_id']
4648 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4649 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4650 # has_mgmt_iface = False
4651 # for iface in vm["interfaces"]:
4652 # if iface["type"]=="mgmt":
4653 # has_mgmt_iface = True
4654 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4655 # vm_dict[vm_id]['status'] = "ACTIVE"
4656 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4657 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4658 # 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'):
4659 # vm['status'] = vm_dict[vm_id]['status']
4660 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4661 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4662 # # 2.1. Update in openmano DB the VMs whose status changed
4664 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4665 # vms_notupdated.remove(vm["uuid"])
4667 # vms_updated.append(vm["uuid"])
4668 # except db_base_Exception as e:
4669 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4670 # # 2.2. Update in openmano DB the interface VMs
4671 # for interface in interfaces:
4672 # #translate from vim_net_id to instance_net_id
4673 # network_id_list=[]
4674 # for net in instanceDict['nets']:
4675 # if net["vim_net_id"] == interface["vim_net_id"]:
4676 # network_id_list.append(net["uuid"])
4677 # if not network_id_list:
4679 # del interface["vim_net_id"]
4681 # for network_id in network_id_list:
4682 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4683 # except db_base_Exception as e:
4684 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4686 # # 3. Getting the status of all nets
4688 # for datacenter_key in myvims:
4689 # if not net_list.get(datacenter_key):
4692 # failed_message = ""
4693 # if not myvims[datacenter_key]:
4694 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4697 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4699 # except vimconn.vimconnException as e:
4700 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4701 # failed_message = str(e)
4703 # for net in net_list[datacenter_key]:
4704 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4706 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4707 # # TODO: update nets inside a vnf
4708 # for net in instanceDict['nets']:
4709 # net_id = net['vim_net_id']
4710 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4711 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4712 # 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'):
4713 # net['status'] = net_dict[net_id]['status']
4714 # net['error_msg'] = net_dict[net_id].get('error_msg')
4715 # net['vim_info'] = net_dict[net_id].get('vim_info')
4716 # # 5.1. Update in openmano DB the nets whose status changed
4718 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4719 # nets_notupdated.remove(net["uuid"])
4721 # nets_updated.append(net["uuid"])
4722 # except db_base_Exception as e:
4723 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4725 # # Returns appropriate output
4726 # #print "nfvo.refresh_instance finishes"
4727 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4728 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4729 instance_id
= instanceDict
['uuid']
4730 # if len(vms_notupdated)+len(nets_notupdated)>0:
4731 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4732 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4734 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4736 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4737 #print "Checking that the instance_id exists and getting the instance dictionary"
4738 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4739 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4741 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4742 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4744 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), httperrors
.Not_Found
)
4745 myvim
= next(iter(vims
.values()))
4750 myvim_threads_id
= {}
4751 if action_dict
.get("vdu-scaling"):
4752 db_instance_vms
= []
4754 db_instance_interfaces
= []
4755 instance_action_id
= get_task_id()
4756 db_instance_action
= {
4757 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4758 "tenant_id": nfvo_tenant
,
4759 "instance_id": instance_id
,
4760 "description": "SCALE",
4762 vm_result
["instance_action_id"] = instance_action_id
4763 vm_result
["created"] = []
4764 vm_result
["deleted"] = []
4766 for vdu
in action_dict
["vdu-scaling"]:
4767 vdu_id
= vdu
.get("vdu-id")
4768 osm_vdu_id
= vdu
.get("osm_vdu_id")
4769 member_vnf_index
= vdu
.get("member-vnf-index")
4770 vdu_count
= vdu
.get("count", 1)
4772 target_vms
= mydb
.get_rows(
4773 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4774 WHERE
={"vms.uuid": vdu_id
},
4775 ORDER_BY
="vms.created_at"
4778 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), httperrors
.Not_Found
)
4780 if not osm_vdu_id
and not member_vnf_index
:
4781 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4782 target_vms
= mydb
.get_rows(
4783 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4784 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4785 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4786 " join vms on ivms.vm_id=vms.uuid",
4787 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
,
4788 "ivnfs.instance_scenario_id": instance_id
},
4789 ORDER_BY
="ivms.created_at"
4792 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), httperrors
.Not_Found
)
4793 vdu_id
= target_vms
[-1]["uuid"]
4794 target_vm
= target_vms
[-1]
4795 datacenter
= target_vm
["datacenter_id"]
4796 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4798 if vdu
["type"] == "delete":
4799 for index
in range(0, vdu_count
):
4800 target_vm
= target_vms
[-1-index
]
4801 vdu_id
= target_vm
["uuid"]
4803 vm_interfaces
= None
4804 for sce_vnf
in instanceDict
['vnfs']:
4805 for vm
in sce_vnf
['vms']:
4806 if vm
["uuid"] == vdu_id
:
4807 # TODO revise this should not be vm["uuid"] instance_vms["vm_id"]
4808 vm_interfaces
= vm
["interfaces"]
4812 "instance_action_id": instance_action_id
,
4813 "task_index": task_index
,
4814 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4816 "status": "SCHEDULED",
4817 "item": "instance_vms",
4819 "related": target_vm
["related"],
4820 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4821 default_flow_style
=True, width
=256)
4824 db_vim_actions
.append(db_vim_action
)
4825 vm_result
["deleted"].append(vdu_id
)
4826 # delete from database
4827 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4829 else: # vdu["type"] == "create":
4831 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4833 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
=where
)
4834 if not vim_action_to_clone
:
4835 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), httperrors
.Internal_Server_Error
)
4836 vim_action_to_clone
= vim_action_to_clone
[0]
4837 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4839 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4840 # TODO do the same for flavor and image when available
4841 task_depends_on
= []
4842 task_params
= extra
["params"]
4843 task_params_networks
= deepcopy(task_params
[5])
4844 for iface
in task_params
[5]:
4845 if iface
["net_id"].startswith("TASK-"):
4846 if "." not in iface
["net_id"]:
4847 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4848 iface
["net_id"][5:]))
4849 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4850 iface
["net_id"][5:])
4852 task_depends_on
.append(iface
["net_id"][5:])
4853 if "mac_address" in iface
:
4854 del iface
["mac_address"]
4856 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4857 for index
in range(0, vdu_count
):
4858 vm_uuid
= str(uuid4())
4859 vm_name
= target_vm
.get('vim_name')
4861 suffix
= vm_name
.rfind("-")
4862 vm_name
= vm_name
[:suffix
+1] + str(index
+ 1 + int(vm_name
[suffix
+1:]))
4868 'instance_vnf_id': target_vm
['instance_vnf_id'],
4869 'vm_id': target_vm
['vm_id'],
4870 'vim_name': vm_name
,
4872 db_instance_vms
.append(db_instance_vm
)
4874 for vm_iface
in vm_ifaces_to_clone
:
4875 iface_uuid
= str(uuid4())
4876 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4879 'instance_vm_id': vm_uuid
,
4880 "instance_net_id": vm_iface
["instance_net_id"],
4881 "instance_wim_net_id": vm_iface
["instance_wim_net_id"],
4882 'interface_id': vm_iface
['interface_id'],
4883 'type': vm_iface
['type'],
4884 'model': vm_iface
['model'],
4885 'floating_ip': vm_iface
['floating_ip'],
4886 'port_security': vm_iface
['port_security']
4888 db_instance_interfaces
.append(db_vm_iface
)
4889 task_params_copy
= deepcopy(task_params
)
4890 for iface
in task_params_copy
[5]:
4891 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4892 # increment ip_address
4893 if "ip_address" in iface
:
4894 ip
= iface
.get("ip_address")
4899 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4900 iface
["ip_address"] = ip
4902 iface
["ip_address"] = None
4904 task_params_copy
[0] = vm_name
4906 "instance_action_id": instance_action_id
,
4907 "task_index": task_index
,
4908 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4910 "status": "SCHEDULED",
4911 "item": "instance_vms",
4916 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4919 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4922 db_vim_actions
.append(db_vim_action
)
4923 vm_result
["created"].append(vm_uuid
)
4925 db_instance_action
["number_tasks"] = task_index
4927 {"instance_vms": db_instance_vms
},
4928 {"instance_interfaces": db_instance_interfaces
},
4929 {"instance_actions": db_instance_action
},
4931 # {"instance_sfis": db_instance_sfis},
4932 # {"instance_sfs": db_instance_sfs},
4933 # {"instance_classifications": db_instance_classifications},
4934 # {"instance_sfps": db_instance_sfps},
4935 {"vim_wim_actions": db_vim_actions
}
4937 logger
.debug("create_vdu done DB tables: %s",
4938 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4939 mydb
.new_rows(db_tables
, [])
4940 for myvim_thread
in myvim_threads_id
.values():
4941 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4945 input_vnfs
= action_dict
.pop("vnfs", [])
4946 input_vms
= action_dict
.pop("vms", [])
4947 action_over_all
= True if not input_vnfs
and not input_vms
else False
4948 for sce_vnf
in instanceDict
['vnfs']:
4949 for vm
in sce_vnf
['vms']:
4950 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4951 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4952 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
and \
4953 sce_vnf
['member_vnf_index'] + "-" + vm
['vdu_osm_id'] not in input_vms
: # TODO conside vm_count_index
4956 if "add_public_key" in action_dict
:
4957 if sce_vnf
.get('mgmt_access'):
4958 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'], Loader
=yaml
.Loader
)
4959 if not input_vms
and mgmt_access
.get("vdu-id") != vm
['vdu_osm_id']:
4961 default_user
= mgmt_access
.get("default-user")
4962 password
= mgmt_access
.get("password")
4963 if mgmt_access
.get(vm
['vdu_osm_id']):
4964 default_user
= mgmt_access
[vm
['vdu_osm_id']].get("default-user", default_user
)
4965 password
= mgmt_access
[vm
['vdu_osm_id']].get("password", password
)
4967 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4969 if 'ip_address' in vm
:
4970 mgmt_ip
= vm
['ip_address'].split(';')
4971 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4972 data
= myvim
.inject_user_key(mgmt_ip
[0], action_dict
.get('user', default_user
),
4973 action_dict
['add_public_key'],
4974 password
=password
, ro_key
=priv_RO_key
)
4975 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4976 "description": "Public key injected",
4980 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4981 httperrors
.Internal_Server_Error
)
4983 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4984 httperrors
.Internal_Server_Error
)
4986 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
4987 if "console" in action_dict
:
4988 if not global_config
["http_console_proxy"]:
4989 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4990 "description": "{protocol}//{ip}:{port}/{suffix}".format(
4991 protocol
=data
["protocol"],
4992 ip
= data
["server"],
4993 port
= data
["port"],
4994 suffix
= data
["suffix"]),
4998 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
4999 vm_result
[ vm
['uuid'] ] = {"vim_result": -httperrors
.Unauthorized
,
5000 "description": "this console is only reachable by local interface",
5005 #print "console data", data
5007 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
5008 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
5009 "description": "{protocol}//{ip}:{port}/{suffix}".format(
5010 protocol
=data
["protocol"],
5011 ip
= global_config
["http_console_host"],
5012 port
= console_thread
.port
,
5013 suffix
= data
["suffix"]),
5017 except NfvoException
as e
:
5018 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
5022 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
5024 except vimconn
.vimconnException
as e
:
5025 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
5028 if vm_ok
==0: #all goes wrong
5033 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
5035 if nfvo_tenant
and nfvo_tenant
!= "any":
5036 filter["tenant_id"] = nfvo_tenant
5037 if instance_id
and instance_id
!= "any":
5038 filter["instance_id"] = instance_id
5040 filter["uuid"] = action_id
5041 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
5044 raise NfvoException("Not found any action with this criteria", httperrors
.Not_Found
)
5045 vim_wim_actions
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
={"instance_action_id": action_id
})
5046 rows
[0]["vim_wim_actions"] = vim_wim_actions
5047 # for backward compatibility set vim_actions = vim_wim_actions
5048 rows
[0]["vim_actions"] = vim_wim_actions
5049 return {"actions": rows
}
5052 def create_or_use_console_proxy_thread(console_server
, console_port
):
5053 #look for a non-used port
5054 console_thread_key
= console_server
+ ":" + str(console_port
)
5055 if console_thread_key
in global_config
["console_thread"]:
5056 #global_config["console_thread"][console_thread_key].start_timeout()
5057 return global_config
["console_thread"][console_thread_key
]
5059 for port
in global_config
["console_port_iterator"]():
5060 #print "create_or_use_console_proxy_thread() port:", port
5061 if port
in global_config
["console_ports"]:
5064 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
5066 global_config
["console_thread"][console_thread_key
] = clithread
5067 global_config
["console_ports"][port
] = console_thread_key
5069 except cli
.ConsoleProxyExceptionPortUsed
as e
:
5070 #port used, try with onoher
5072 except cli
.ConsoleProxyException
as e
:
5073 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5074 raise NfvoException("Not found any free 'http_console_ports'", httperrors
.Conflict
)
5077 def check_tenant(mydb
, tenant_id
):
5078 '''check that tenant exists at database'''
5079 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
5081 raise NfvoException("tenant '{}' not found".format(tenant_id
), httperrors
.Not_Found
)
5084 def new_tenant(mydb
, tenant_dict
):
5086 tenant_uuid
= str(uuid4())
5087 tenant_dict
['uuid'] = tenant_uuid
5089 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
5090 tenant_dict
['RO_pub_key'] = pub_key
5091 tenant_dict
['encrypted_RO_priv_key'] = priv_key
5092 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
5093 except db_base_Exception
as e
:
5094 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
5097 def delete_tenant(mydb
, tenant
):
5098 #get nfvo_tenant info
5100 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
5101 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
5102 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
5105 def new_datacenter(mydb
, datacenter_descriptor
):
5106 sdn_port_mapping
= None
5107 if "config" in datacenter_descriptor
:
5108 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
5109 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
5111 # Check that datacenter-type is correct
5112 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
5113 # module_info = None
5116 plugin_name
= "rovim_" + datacenter_type
5117 if plugin_name
not in plugins
:
5118 _load_plugin(plugin_name
, type="vim")
5120 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
5121 if sdn_port_mapping
:
5123 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
5124 except Exception as e
:
5125 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
5127 return datacenter_id
5130 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
5131 # obtain data, check that only one exist
5132 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
5135 datacenter_id
= datacenter
['uuid']
5136 where
= {'uuid': datacenter
['uuid']}
5137 remove_port_mapping
= False
5138 new_sdn_port_mapping
= None
5139 if "config" in datacenter_descriptor
:
5140 if datacenter_descriptor
['config'] != None:
5142 new_config_dict
= datacenter_descriptor
["config"]
5143 if "sdn-port-mapping" in new_config_dict
:
5144 remove_port_mapping
= True
5145 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
5146 # delete null fields
5148 for k
in new_config_dict
:
5149 if new_config_dict
[k
] is None:
5151 if k
== 'sdn-controller':
5152 remove_port_mapping
= True
5154 config_text
= datacenter
.get("config")
5157 config_dict
= yaml
.load(config_text
, Loader
=yaml
.Loader
)
5158 config_dict
.update(new_config_dict
)
5159 # delete null fields
5162 except Exception as e
:
5163 raise NfvoException("Bad format at datacenter:config " + str(e
), httperrors
.Bad_Request
)
5165 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
5167 datacenter_descriptor
["config"] = None
5168 if remove_port_mapping
:
5170 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
5171 except ovimException
as e
:
5172 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
5174 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
5175 if new_sdn_port_mapping
:
5177 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
5178 except ovimException
as e
:
5180 mydb
.update_rows('datacenters', datacenter
, where
)
5181 raise NfvoException("Error adding datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
5182 return datacenter_id
5185 def delete_datacenter(mydb
, datacenter
):
5186 #get nfvo_tenant info
5187 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
5188 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
5190 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
5191 except ovimException
as e
:
5192 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
))
5193 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
5196 def create_vim_account(mydb
, nfvo_tenant
, datacenter_id
, name
=None, vim_id
=None, vim_tenant
=None, vim_tenant_name
=None,
5197 vim_username
=None, vim_password
=None, config
=None):
5198 # get datacenter info
5200 if not datacenter_id
:
5202 raise NfvoException("You must provide 'vim_id", http_code
=httperrors
.Bad_Request
)
5203 datacenter_id
= vim_id
5204 datacenter_id
, datacenter_name
= get_datacenter_uuid(mydb
, None, datacenter_id
)
5206 create_vim_tenant
= True if not vim_tenant
and not vim_tenant_name
else False
5208 # get nfvo_tenant info
5209 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
5210 if vim_tenant_name
==None:
5211 vim_tenant_name
=tenant_dict
['name']
5213 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
5214 # #check that this association does not exist before
5215 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5216 # if len(tenants_datacenters)>0:
5217 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
5219 vim_tenant_id_exist_atdb
=False
5220 if not create_vim_tenant
:
5221 where_
={"datacenter_id": datacenter_id
}
5222 if vim_tenant
!=None:
5223 where_
["vim_tenant_id"] = vim_tenant
5224 if vim_tenant_name
!=None:
5225 where_
["vim_tenant_name"] = vim_tenant_name
5226 #check if vim_tenant_id is already at database
5227 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
5228 if len(datacenter_tenants_dict
)>=1:
5229 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
5230 vim_tenant_id_exist_atdb
=True
5231 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5233 datacenter_tenants_dict
= {}
5234 #insert at table datacenter_tenants
5235 else: #if vim_tenant==None:
5236 #create tenant at VIM if not provided
5238 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter
, vim_user
=vim_username
,
5239 vim_passwd
=vim_password
)
5240 datacenter_name
= myvim
["name"]
5241 vim_tenant
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
5242 except vimconn
.vimconnException
as e
:
5243 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_id
, str(e
)), httperrors
.Internal_Server_Error
)
5244 datacenter_tenants_dict
= {}
5245 datacenter_tenants_dict
["created"]="true"
5247 #fill datacenter_tenants table
5248 if not vim_tenant_id_exist_atdb
:
5249 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant
5250 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
5251 datacenter_tenants_dict
["user"] = vim_username
5252 datacenter_tenants_dict
["passwd"] = vim_password
5253 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
5255 datacenter_tenants_dict
["name"] = name
5257 datacenter_tenants_dict
["name"] = datacenter_name
5259 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
5260 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
5261 datacenter_tenants_dict
["uuid"] = id_
5263 #fill tenants_datacenters table
5264 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
5265 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
5266 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
5269 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
)
5270 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, None, datacenter_tenant_id
, db
=db
)
5272 thread_id
= datacenter_tenants_dict
["uuid"]
5273 vim_threads
["running"][thread_id
] = new_thread
5275 except vimconn
.vimconnException
as e
:
5276 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5279 def edit_vim_account(mydb
, nfvo_tenant
, datacenter_tenant_id
, datacenter_id
=None, name
=None, vim_tenant
=None,
5280 vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
5282 # get vim_account; check is valid for this tenant
5283 from_
= "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5284 where_
= {"td.nfvo_tenant_id": nfvo_tenant
}
5285 if datacenter_tenant_id
:
5286 where_
["dt.uuid"] = datacenter_tenant_id
5288 where_
["dt.datacenter_id"] = datacenter_id
5289 vim_accounts
= mydb
.get_rows(SELECT
="dt.uuid as uuid, config", FROM
=from_
, WHERE
=where_
)
5290 if not vim_accounts
:
5291 raise NfvoException("vim_account not found for this tenant", http_code
=httperrors
.Not_Found
)
5292 elif len(vim_accounts
) > 1:
5293 raise NfvoException("found more than one vim_account for this tenant", http_code
=httperrors
.Conflict
)
5294 datacenter_tenant_id
= vim_accounts
[0]["uuid"]
5295 original_config
= vim_accounts
[0]["config"]
5299 original_config_dict
= yaml
.load(original_config
, Loader
=yaml
.Loader
)
5300 original_config_dict
.update(config
)
5301 update
["config"] = yaml
.safe_dump(original_config_dict
, default_flow_style
=True, width
=256)
5303 update_
['name'] = name
5305 update_
['vim_tenant_id'] = vim_tenant
5307 update_
['vim_tenant_name'] = vim_tenant_name
5309 update_
['user'] = vim_username
5311 update_
['passwd'] = vim_password
5313 mydb
.update_rows("datacenter_tenants", UPDATE
=update_
, WHERE
={"uuid": datacenter_tenant_id
})
5315 vim_threads
["running"][datacenter_tenant_id
].insert_task("reload")
5316 return datacenter_tenant_id
5318 def delete_vim_account(mydb
, tenant_id
, vim_account_id
, datacenter
=None):
5319 #get nfvo_tenant info
5320 if not tenant_id
or tenant_id
=="any":
5323 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
5324 tenant_uuid
= tenant_dict
['uuid']
5326 #check that this association exist before
5327 tenants_datacenter_dict
= {}
5329 datacenter_id
, _
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
5330 tenants_datacenter_dict
["datacenter_id"] = datacenter_id
5331 elif vim_account_id
:
5332 tenants_datacenter_dict
["datacenter_tenant_id"] = vim_account_id
5334 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
5335 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5336 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
5337 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), httperrors
.Not_Found
)
5339 #delete this association
5340 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5342 #get vim_tenant info and deletes
5344 for tenant_datacenter_item
in tenant_datacenter_list
:
5345 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5346 #try to delete vim:tenant
5348 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5349 if vim_tenant_dict
['created']=='true':
5350 #delete tenant at VIM if created by NFVO
5352 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5353 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
5354 except vimconn
.vimconnException
as e
:
5355 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
5356 logger
.warn(warning
)
5357 except db_base_Exception
as e
:
5358 logger
.error("Cannot delete datacenter_tenants " + str(e
))
5359 pass # the error will be caused because dependencies, vim_tenant can not be deleted
5360 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
5361 thread
= vim_threads
["running"].get(thread_id
)
5363 thread
.insert_task("exit")
5364 vim_threads
["deleting"][thread_id
] = thread
5365 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
5368 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
5370 #get datacenter info
5371 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5373 if 'check-connectivity' in action_dict
:
5375 myvim
.check_vim_connectivity()
5376 except vimconn
.vimconnException
as e
:
5377 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5378 raise NfvoException(str(e
), e
.http_code
)
5379 elif 'net-update' in action_dict
:
5381 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
5383 except vimconn
.vimconnException
as e
:
5384 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5385 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5386 #update nets Change from VIM format to NFVO format
5389 net_nfvo
={'datacenter_id': datacenter_id
}
5390 net_nfvo
['name'] = net
['name']
5391 #net_nfvo['description']= net['name']
5392 net_nfvo
['vim_net_id'] = net
['id']
5393 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5394 net_nfvo
['shared'] = net
['shared']
5395 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5396 net_list
.append(net_nfvo
)
5397 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
5398 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
5400 elif 'net-edit' in action_dict
:
5401 net
= action_dict
['net-edit'].pop('net')
5402 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5403 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
5404 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5406 elif 'net-delete' in action_dict
:
5407 net
= action_dict
['net-deelte'].get('net')
5408 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5409 result
= mydb
.delete_row(FROM
='datacenter_nets',
5410 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5414 raise NfvoException("Unknown action " + str(action_dict
), httperrors
.Bad_Request
)
5417 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
5418 #get datacenter info
5419 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5421 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
5422 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
5423 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
5427 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
5428 #get datacenter info
5429 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5432 action_dict
= action_dict
["netmap"]
5433 if 'vim_id' in action_dict
:
5434 filter_dict
["id"] = action_dict
['vim_id']
5435 if 'vim_name' in action_dict
:
5436 filter_dict
["name"] = action_dict
['vim_name']
5438 filter_dict
["shared"] = True
5441 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
5442 except vimconn
.vimconnException
as e
:
5443 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5444 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5445 if len(vim_nets
)>1 and action_dict
:
5446 raise NfvoException("more than two networks found, specify with vim_id", httperrors
.Conflict
)
5447 elif len(vim_nets
)==0: # and action_dict:
5448 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), httperrors
.Not_Found
)
5450 for net
in vim_nets
:
5451 net_nfvo
={'datacenter_id': datacenter_id
}
5452 if action_dict
and "name" in action_dict
:
5453 net_nfvo
['name'] = action_dict
['name']
5455 net_nfvo
['name'] = net
['name']
5456 #net_nfvo['description']= net['name']
5457 net_nfvo
['vim_net_id'] = net
['id']
5458 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5459 net_nfvo
['shared'] = net
['shared']
5460 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5462 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
5463 net_nfvo
["status"] = "OK"
5464 net_nfvo
["uuid"] = net_id
5465 except db_base_Exception
as e
:
5469 net_nfvo
["status"] = "FAIL: " + str(e
)
5470 net_list
.append(net_nfvo
)
5473 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
5474 # obtain all network data
5476 if utils
.check_valid_uuid(network_id
):
5477 filter_dict
= {"id": network_id
}
5479 filter_dict
= {"name": network_id
}
5481 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5482 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
5483 except vimconn
.vimconnException
as e
:
5484 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
5486 # ensure the network is defined
5487 if len(network
) == 0:
5488 raise NfvoException("Network {} is not present in the system".format(network_id
),
5489 httperrors
.Bad_Request
)
5491 # ensure there is only one network with the provided name
5492 if len(network
) > 1:
5493 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), httperrors
.Bad_Request
)
5495 # ensure it is a dataplane network
5496 if network
[0]['type'] != 'data':
5499 # ensure we use the id
5500 network_id
= network
[0]['id']
5502 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5503 # and with instance_scenario_id==NULL
5504 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5505 search_dict
= {'vim_net_id': network_id
}
5508 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5509 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5510 except db_base_Exception
as e
:
5511 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5512 network_id
) + str(e
), e
.http_code
)
5516 if net
['sdn_net_id'] != None:
5518 sdn_net_id
= net
['sdn_net_id']
5520 if sdn_net_counter
== 0:
5522 elif sdn_net_counter
== 1:
5525 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5526 network_id
), httperrors
.Internal_Server_Error
)
5528 def get_sdn_controller_id(mydb
, datacenter
):
5529 # Obtain sdn controller id
5530 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5534 return yaml
.load(config
, Loader
=yaml
.Loader
).get('sdn-controller')
5536 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5538 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5539 if not sdn_network_id
:
5540 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), httperrors
.Internal_Server_Error
)
5542 #Obtain sdn controller id
5543 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5544 if not controller_id
:
5545 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), httperrors
.Internal_Server_Error
)
5547 #Obtain sdn controller info
5548 sdn_controller
= ovim
.show_of_controller(controller_id
)
5551 'name': 'external_port',
5552 'net_id': sdn_network_id
,
5553 'ofc_id': controller_id
,
5554 'switch_dpid': sdn_controller
['dpid'],
5555 'switch_port': descriptor
['port']
5558 if 'vlan' in descriptor
:
5559 port_data
['vlan'] = descriptor
['vlan']
5560 if 'mac' in descriptor
:
5561 port_data
['mac'] = descriptor
['mac']
5563 result
= ovim
.new_port(port_data
)
5564 except ovimException
as e
:
5565 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5566 sdn_network_id
, network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5567 except db_base_Exception
as e
:
5568 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5569 network_id
) + str(e
), e
.http_code
)
5571 return 'Port uuid: '+ result
5573 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5575 filter = {'uuid': port_id
}
5577 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5578 if not sdn_network_id
:
5579 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5580 httperrors
.Internal_Server_Error
)
5581 #in case no port_id is specified only ports marked as 'external_port' will be detached
5582 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5585 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5586 except ovimException
as e
:
5587 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5588 httperrors
.Internal_Server_Error
)
5590 if len(port_list
) == 0:
5591 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5592 httperrors
.Bad_Request
)
5595 for port
in port_list
:
5597 port_uuid_list
.append(port
['uuid'])
5598 ovim
.delete_port(port
['uuid'])
5599 except ovimException
as e
:
5600 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5602 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5604 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5605 #get datacenter info
5606 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5609 if utils
.check_valid_uuid(name
):
5610 filter_dict
["id"] = name
5612 filter_dict
["name"] = name
5614 if item
=="networks":
5615 #filter_dict['tenant_id'] = myvim['tenant_id']
5616 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5618 if len(content
) == 0:
5619 raise NfvoException("Network {} is not present in the system. ".format(name
),
5620 httperrors
.Bad_Request
)
5622 #Update the networks with the attached ports
5624 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5625 if sdn_network_id
!= None:
5627 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5628 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5629 except ovimException
as e
:
5630 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5631 #Remove field name and if port name is external_port save it as 'type'
5632 for port
in port_list
:
5633 if port
['name'] == 'external_port':
5634 port
['type'] = "External"
5636 net
['sdn_network_id'] = sdn_network_id
5637 net
['sdn_attached_ports'] = port_list
5639 elif item
=="tenants":
5640 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5641 elif item
== "images":
5643 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5645 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5646 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5647 if name
and len(content
)==1:
5648 return {item
[:-1]: content
[0]}
5649 elif name
and len(content
)==0:
5650 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.items())),
5653 return {item
: content
}
5654 except vimconn
.vimconnException
as e
:
5655 print("vim_action Not possible to get_{}_list from VIM: {} ".format(item
, str(e
)))
5656 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5659 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5660 #get datacenter info
5661 if tenant_id
== "any":
5664 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5666 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5667 logger
.debug("vim_action_delete vim response: " + str(content
))
5668 items
= next(iter(content
.values()))
5669 if type(items
)==list and len(items
)==0:
5670 raise NfvoException("Not found " + item
, httperrors
.Not_Found
)
5671 elif type(items
)==list and len(items
)>1:
5672 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), httperrors
.Not_Found
)
5673 else: # it is a dict
5674 item_id
= items
["id"]
5675 item_name
= str(items
.get("name"))
5678 if item
=="networks":
5679 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5680 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5681 if sdn_network_id
!= None:
5682 #Delete any port attachment to this network
5684 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5685 except ovimException
as e
:
5686 raise NfvoException(
5687 "ovimException obtaining external ports for net {}. ".format(sdn_network_id
) + str(e
),
5688 httperrors
.Internal_Server_Error
)
5690 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5691 for port
in port_list
:
5692 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5694 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5696 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None,
5697 'sdn_net_id': sdn_network_id
,
5698 'vim_net_id': item_id
})
5699 except db_base_Exception
as e
:
5700 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: {}".format(
5701 item_id
, e
), e
.http_code
)
5703 #Delete the SDN network
5705 ovim
.delete_network(sdn_network_id
)
5706 except ovimException
as e
:
5707 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5708 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5709 httperrors
.Internal_Server_Error
)
5711 content
= myvim
.delete_network(item_id
)
5712 elif item
=="tenants":
5713 content
= myvim
.delete_tenant(item_id
)
5714 elif item
== "images":
5715 content
= myvim
.delete_image(item_id
)
5717 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5718 except vimconn
.vimconnException
as e
:
5719 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5720 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5722 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5725 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5726 #get datacenter info
5727 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5728 if tenant_id
== "any":
5730 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5732 if item
=="networks":
5733 net
= descriptor
["network"]
5734 net_name
= net
.pop("name")
5735 net_type
= net
.pop("type", "bridge")
5736 net_public
= net
.pop("shared", False)
5737 net_ipprofile
= net
.pop("ip_profile", None)
5738 net_vlan
= net
.pop("vlan", None)
5739 net_provider_network_profile
= None
5741 net_provider_network_profile
= {"segmentation-id": net_vlan
}
5742 content
, _
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, provider_network_profile
=net_provider_network_profile
) #, **net)
5744 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5745 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5746 #obtain datacenter_tenant_id
5747 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5748 FROM
='datacenter_tenants',
5749 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5752 sdn_network
['vlan'] = net_vlan
5753 sdn_network
['type'] = net_type
5754 sdn_network
['name'] = net_name
5755 sdn_network
['region'] = datacenter_tenant_id
5756 ovim_content
= ovim
.new_network(sdn_network
)
5757 except ovimException
as e
:
5758 logger
.error("ovimException creating SDN network={} ".format(
5759 sdn_network
) + str(e
), exc_info
=True)
5760 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5761 httperrors
.Internal_Server_Error
)
5763 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5764 # use instance_scenario_id=None to distinguish from real instaces of nets
5765 correspondence
= {'instance_scenario_id': None,
5766 'sdn_net_id': ovim_content
,
5767 'vim_net_id': content
,
5768 'datacenter_tenant_id': datacenter_tenant_id
5771 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5772 except db_base_Exception
as e
:
5773 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5774 correspondence
, e
), e
.http_code
)
5775 elif item
=="tenants":
5776 tenant
= descriptor
["tenant"]
5777 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5779 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5780 except vimconn
.vimconnException
as e
:
5781 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5783 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5785 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5787 wim_id
= ovim
.new_of_controller(sdn_controller
)
5789 # Load plugin if not previously loaded
5790 controller_type
= sdn_controller
.get("type")
5791 plugin_name
= "rosdn_" + controller_type
5792 if plugin_name
not in plugins
:
5793 _load_plugin(plugin_name
, type="sdn")
5795 thread_name
= get_non_used_vim_name(sdn_controller
['name'], wim_id
)
5796 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, wim_id
, None, db
=db
)
5799 vim_threads
["running"][thread_id
] = new_thread
5800 logger
.debug('New SDN controller created with uuid {}'.format(wim_id
))
5802 except ovimException
as e
:
5803 raise NfvoException(e
) from e
5805 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5806 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5807 msg
= 'SDN controller {} updated'.format(data
)
5808 vim_threads
["running"][controller_id
].insert_task("reload")
5812 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5813 if controller_id
== None:
5814 data
= ovim
.get_of_controllers()
5816 data
= ovim
.show_of_controller(controller_id
)
5818 msg
= 'SDN controller list:\n {}'.format(data
)
5822 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5823 select_
= ('uuid', 'config')
5824 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5825 for datacenter
in datacenters
:
5826 if datacenter
['config']:
5827 config
= yaml
.load(datacenter
['config'], Loader
=yaml
.Loader
)
5828 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5829 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), httperrors
.Conflict
)
5831 data
= ovim
.delete_of_controller(controller_id
)
5832 msg
= 'SDN controller {} deleted'.format(data
)
5836 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5837 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5838 if len(controller
) < 1:
5839 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), httperrors
.Not_Found
)
5842 sdn_controller_id
= yaml
.load(controller
[0]["config"], Loader
=yaml
.Loader
)["sdn-controller"]
5844 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), httperrors
.Bad_Request
)
5846 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5847 switch_dpid
= sdn_controller
["dpid"]
5850 for compute_node
in sdn_port_mapping
:
5851 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5853 element
["compute_node"] = compute_node
["compute_node"]
5854 if compute_node
["ports"]:
5855 for port
in compute_node
["ports"]:
5856 pci
= port
.get("pci")
5857 element
["switch_port"] = port
.get("switch_port")
5858 element
["switch_mac"] = port
.get("switch_mac")
5859 element
["switch_dpid"] = port
.get("switch_dpid")
5860 element
["switch_id"] = port
.get("switch_id")
5861 if not element
["switch_port"] and not element
["switch_mac"]:
5862 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors
.Bad_Request
)
5863 for pci_expanded
in utils
.expand_brackets(pci
):
5864 element
["pci"] = pci_expanded
5865 maps
.append(dict(element
))
5867 out
= ovim
.set_of_port_mapping(maps
, sdn_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, vim_id
=datacenter_id
)
5868 vim_threads
["running"][sdn_controller_id
].insert_task("reload")
5871 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5872 maps
= ovim
.get_of_port_mappings(db_filter
={"datacenter_id": datacenter_id
})
5875 "sdn-controller": None,
5876 "datacenter-id": datacenter_id
,
5878 "ports_mapping": list()
5881 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5882 if datacenter
['config']:
5883 config
= yaml
.load(datacenter
['config'], Loader
=yaml
.Loader
)
5884 if 'sdn-controller' in config
:
5885 controller_id
= config
['sdn-controller']
5886 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5887 result
["sdn-controller"] = controller_id
5888 result
["dpid"] = sdn_controller
["dpid"]
5890 if result
["sdn-controller"] == None:
5891 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), httperrors
.Bad_Request
)
5892 if result
["dpid"] == None:
5893 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5894 httperrors
.Internal_Server_Error
)
5899 ports_correspondence_dict
= dict()
5901 if result
["sdn-controller"] != link
["wim_id"]:
5902 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5903 if result
["dpid"] != link
["switch_dpid"]:
5904 raise NfvoException("The dpid specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5905 link_config
= link
["service_mapping_info"]
5907 element
["pci"] = link
.get("device_interface_id")
5908 if link
["switch_port"]:
5909 element
["switch_port"] = link
["switch_port"]
5910 if link_config
["switch_mac"]:
5911 element
["switch_mac"] = link_config
.get("switch_mac")
5913 if not link
.get("interface_id") in ports_correspondence_dict
:
5915 content
["compute_node"] = link
.get("interface_id")
5916 content
["ports"] = list()
5917 ports_correspondence_dict
[link
.get("interface_id")] = content
5919 ports_correspondence_dict
[link
["interface_id"]]["ports"].append(element
)
5921 for key
in sorted(ports_correspondence_dict
):
5922 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5926 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5927 return ovim
.clear_of_port_mapping(db_filter
={"datacenter_id":datacenter_id
})
5929 def create_RO_keypair(tenant_id
):
5931 Creates a public / private keys for a RO tenant and returns their values
5933 tenant_id: ID of the tenant
5935 public_key: Public key for the RO tenant
5936 private_key: Encrypted private key for RO tenant
5940 key
= RSA
.generate(bits
)
5942 public_key
= key
.publickey().exportKey('OpenSSH')
5943 if isinstance(public_key
, ValueError):
5944 raise NfvoException("Unable to create public key: {}".format(public_key
), httperrors
.Internal_Server_Error
)
5945 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5946 except (ValueError, NameError) as e
:
5947 raise NfvoException("Unable to create private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5948 if isinstance(public_key
, bytes
):
5949 public_key
= public_key
.decode(encoding
='UTF-8')
5950 if isinstance(private_key
, bytes
):
5951 private_key
= private_key
.decode(encoding
='UTF-8')
5952 return public_key
, private_key
5954 def decrypt_key (key
, tenant_id
):
5956 Decrypts an encrypted RSA key
5958 key: Private key to be decrypted
5959 tenant_id: ID of the tenant
5961 unencrypted_key: Unencrypted private key for RO tenant
5964 key
= RSA
.importKey(key
,tenant_id
)
5965 unencrypted_key
= key
.exportKey('PEM')
5966 if isinstance(unencrypted_key
, ValueError):
5967 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), httperrors
.Internal_Server_Error
)
5968 if isinstance(unencrypted_key
, bytes
):
5969 unencrypted_key
= unencrypted_key
.decode(encoding
='UTF-8')
5970 except ValueError as e
:
5971 raise NfvoException("Unable to decrypt the private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5972 return unencrypted_key