1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
27 __author__
="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
28 __date__
="$16-sep-2014 22:05:01$"
34 from random
import choice
as random_choice
35 from osm_ro
import utils
36 from osm_ro
.utils
import deprecated
37 from osm_ro
.vim_thread
import vim_thread
38 import osm_ro
.console_proxy_thread
as cli
39 from osm_ro
import vimconn
, vim_dummy
43 from uuid
import uuid4
44 from osm_ro
.db_base
import db_base_Exception
46 from osm_ro
import nfvo_db
47 from threading
import Lock
49 from osm_ro
.sdn
import Sdn
, SdnException
as ovimException
51 from Crypto
.PublicKey
import RSA
53 import osm_im
.vnfd
as vnfd_catalog
54 import osm_im
.nsd
as nsd_catalog
55 from pyangbind
.lib
.serialise
import pybindJSONDecoder
56 from copy
import deepcopy
57 from pkg_resources
import iter_entry_points
61 from .wim
import sdnconn
62 from .wim
.wimconn_dummy
import DummyConnector
63 from .wim
.failing_connector
import FailingConnector
64 from .http_tools
import errors
as httperrors
65 from .wim
.engine
import WimEngine
66 from .wim
.persistence
import WimPersistence
67 from copy
import deepcopy
68 from pprint
import pformat
75 global sdnconn_imported
78 global default_volume_size
79 default_volume_size
= '5' #size in GB
84 plugins
= {} # dictionary with VIM type as key, loaded module as value
85 vim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-VIMs
86 vim_persistent_info
= {}
88 sdnconn_imported
= {} # dictionary with WIM type as key, loaded module as value
89 wim_threads
= {"running":{}, "deleting": {}, "names": []} # threads running for attached-WIMs
90 wim_persistent_info
= {}
93 logger
= logging
.getLogger('openmano.nfvo')
101 class NfvoException(httperrors
.HttpMappedError
):
102 """Common Class for NFVO errors"""
104 def _load_plugin(name
, type="vim"):
105 # type can be vim or sdn
108 for v
in iter_entry_points('osm_ro{}.plugins'.format(type), name
):
109 plugins
[name
] = v
.load()
110 except Exception as e
:
111 logger
.critical("Cannot load osm_{}: {}".format(name
, e
))
113 plugins
[name
] = FailingConnector("Cannot load osm_{}: {}".format(name
, e
))
114 if name
and name
not in plugins
:
115 error_text
= "Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been" \
116 " registered".format(t
=type, n
=name
)
117 logger
.critical(error_text
)
118 plugins
[name
] = FailingConnector(error_text
)
119 # raise NfvoException("Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has not been registered".
120 # format(t=type, n=name), httperrors.Bad_Request)
125 if task_id
<= last_task_id
:
126 task_id
= last_task_id
+ 0.000001
127 last_task_id
= task_id
128 return "ACTION-{:.6f}".format(task_id
)
129 # return (t.strftime("%Y%m%dT%H%M%S.{}%Z", t.localtime(task_id))).format(int((task_id % 1)*1e6))
132 def new_task(name
, params
, depends
=None):
134 task_id
= get_task_id()
135 task
= {"status": "enqueued", "id": task_id
, "name": name
, "params": params
}
137 task
["depends"] = depends
142 return True if id[:5] == "TASK-" else False
144 def get_process_id():
146 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
147 will provide a random one
150 # Try getting docker id. If fails, get pid
152 with
open("/proc/self/cgroup", "r") as f
:
153 text_id_
= f
.readline()
154 _
, _
, text_id
= text_id_
.rpartition("/")
155 text_id
= text_id
.replace("\n", "")[:12]
161 return "".join(random_choice("0123456789abcdef") for _
in range(12))
163 def get_non_used_vim_name(datacenter_name
, datacenter_id
):
164 return "{}:{}:{}".format(
165 worker_id
[:12], datacenter_id
.replace("-", "")[:32], datacenter_name
[:16]
169 def get_non_used_wim_name(wim_name
, wim_id
, tenant_name
, tenant_id
):
171 if name
not in wim_threads
["names"]:
172 wim_threads
["names"].append(name
)
174 name
= wim_name
[:16] + "." + tenant_name
[:16]
175 if name
not in wim_threads
["names"]:
176 wim_threads
["names"].append(name
)
178 name
= wim_id
+ "-" + tenant_id
179 wim_threads
["names"].append(name
)
183 def start_service(mydb
, persistence
=None, wim
=None):
184 global db
, global_config
, plugins
, ovim
, worker_id
185 db
= nfvo_db
.nfvo_db(lock
=db_lock
)
187 db
.connect(global_config
['db_host'], global_config
['db_user'], global_config
['db_passwd'], global_config
['db_name'])
189 persistence
= persistence
or WimPersistence(db
)
192 worker_id
= get_process_id()
193 if "rosdn_dummy" not in plugins
:
194 plugins
["rosdn_dummy"] = DummyConnector
195 if "rovim_dummy" not in plugins
:
196 plugins
["rovim_dummy"] = vim_dummy
197 # starts ovim library
198 ovim
= Sdn(db
, plugins
)
201 wim_engine
= wim
or WimEngine(persistence
, plugins
)
202 wim_engine
.ovim
= ovim
206 #delete old unneeded vim_wim_actions
210 from_
= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\
211 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
212 select_
= ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin',
213 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id',
214 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id',
215 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
216 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
)
218 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
219 'datacenter_id': vim
.get('datacenter_id')}
221 extra
.update(yaml
.load(vim
["config"], Loader
=yaml
.Loader
))
222 if vim
.get('dt_config'):
223 extra
.update(yaml
.load(vim
["dt_config"], Loader
=yaml
.Loader
))
224 plugin_name
= "rovim_" + vim
["type"]
225 if plugin_name
not in plugins
:
226 _load_plugin(plugin_name
, type="vim")
228 thread_id
= vim
['datacenter_tenant_id']
229 vim_persistent_info
[thread_id
] = {}
232 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
233 myvim
= plugins
[plugin_name
].vimconnector(
234 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
235 tenant_id
=vim
['vim_tenant_id'], tenant_name
=vim
['vim_tenant_name'],
236 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
237 user
=vim
['user'], passwd
=vim
['passwd'],
238 config
=extra
, persistent_info
=vim_persistent_info
[thread_id
]
240 except vimconn
.vimconnException
as e
:
242 logger
.error("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
243 vim
['datacenter_id'], e
))
244 except Exception as e
:
245 logger
.critical("Cannot launch thread for VIM {} '{}': {}".format(vim
['datacenter_name'],
246 vim
['datacenter_id'], e
))
247 # raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e),
248 # httperrors.Internal_Server_Error)
249 thread_name
= get_non_used_vim_name(vim
['datacenter_name'], vim
['datacenter_id'])
250 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, None,
251 vim
['datacenter_tenant_id'], db
=db
)
253 vim_threads
["running"][thread_id
] = new_thread
254 wims
= mydb
.get_rows(FROM
="wim_accounts join wims on wim_accounts.wim_id=wims.uuid",
255 WHERE
={"sdn": "true"},
256 SELECT
=("wim_accounts.uuid as uuid", "type", "wim_accounts.name as name"))
258 plugin_name
= "rosdn_" + wim
["type"]
259 if plugin_name
not in plugins
:
260 _load_plugin(plugin_name
, type="sdn")
262 thread_id
= wim
['uuid']
263 thread_name
= get_non_used_vim_name(wim
['name'], wim
['uuid'])
264 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, wim
['uuid'], None, db
=db
)
266 vim_threads
["running"][thread_id
] = new_thread
267 wim_engine
.start_threads()
268 except db_base_Exception
as e
:
269 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
270 except ovimException
as e
:
272 if message
[:22] == "DATABASE wrong version":
273 message
= "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\
274 "at host {dbhost}".format(
275 msg
=message
[22:-3], dbname
=global_config
["db_ovim_name"],
276 dbuser
=global_config
["db_ovim_user"], dbpass
=global_config
["db_ovim_passwd"],
277 ver
=message
[-3:-1], dbhost
=global_config
["db_ovim_host"])
278 raise NfvoException(message
, httperrors
.Bad_Request
)
282 global ovim
, global_config
285 for thread_id
, thread
in vim_threads
["running"].items():
286 thread
.insert_task("exit")
287 vim_threads
["deleting"][thread_id
] = thread
288 vim_threads
["running"] = {}
291 wim_engine
.stop_threads()
293 if global_config
and global_config
.get("console_thread"):
294 for thread
in global_config
["console_thread"]:
295 thread
.terminate
= True
298 return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config
["version"],
299 global_config
["version_date"] ))
303 Clean unused or old entries at database to avoid unlimited growing
304 :param mydb: database connector
307 # get and delete unused vim_wim_actions: all elements deleted, one week before, instance not present
308 now
= t
.time()-3600*24*7
309 instance_action_id
= None
312 actions_to_delete
= mydb
.get_rows(
313 SELECT
=("item", "item_id", "instance_action_id"),
314 FROM
="vim_wim_actions as va join instance_actions as ia on va.instance_action_id=ia.uuid "
315 "left join instance_scenarios as i on ia.instance_id=i.uuid",
316 WHERE
={"va.action": "DELETE", "va.modified_at<": now
, "i.uuid": None,
317 "va.status": ("DONE", "SUPERSEDED")},
320 for to_delete
in actions_to_delete
:
321 mydb
.delete_row(FROM
="vim_wim_actions", WHERE
=to_delete
)
322 if instance_action_id
!= to_delete
["instance_action_id"]:
323 instance_action_id
= to_delete
["instance_action_id"]
324 mydb
.delete_row(FROM
="instance_actions", WHERE
={"uuid": instance_action_id
})
325 nb_deleted
+= len(actions_to_delete
)
326 if len(actions_to_delete
) < 100:
329 mydb
.update_rows("vim_wim_actions", UPDATE
={"worker": None}, WHERE
={"worker<>": None})
332 logger
.debug("Removed {} unused vim_wim_actions".format(nb_deleted
))
335 def get_flavorlist(mydb
, vnf_id
, nfvo_tenant
=None):
337 return result, content:
338 <0, error_text upon error
339 nb_records, flavor_list on success
342 WHERE_dict
['vnf_id'] = vnf_id
343 if nfvo_tenant
is not None:
344 WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
346 #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
347 #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
348 flavors
= mydb
.get_rows(FROM
='vms join flavors on vms.flavor_id=flavors.uuid',SELECT
=('flavor_id',),WHERE
=WHERE_dict
)
349 #print "get_flavor_list result:", result
350 #print "get_flavor_list content:", content
352 for flavor
in flavors
:
353 flavorList
.append(flavor
['flavor_id'])
357 def get_imagelist(mydb
, vnf_id
, nfvo_tenant
=None):
359 Get used images of all vms belonging to this VNFD
360 :param mydb: database conector
361 :param vnf_id: vnfd uuid
362 :param nfvo_tenant: tenant, not used
363 :return: The list of image uuid used
366 vms
= mydb
.get_rows(SELECT
=('image_id','image_list'), FROM
='vms', WHERE
={'vnf_id': vnf_id
})
368 if vm
["image_id"] and vm
["image_id"] not in image_list
:
369 image_list
.append(vm
["image_id"])
371 vm_image_list
= yaml
.load(vm
["image_list"], Loader
=yaml
.Loader
)
372 for image_dict
in vm_image_list
:
373 if image_dict
["image_id"] not in image_list
:
374 image_list
.append(image_dict
["image_id"])
378 def get_vim(mydb
, nfvo_tenant
=None, datacenter_id
=None, datacenter_name
=None, datacenter_tenant_id
=None,
379 vim_tenant
=None, vim_tenant_name
=None, vim_user
=None, vim_passwd
=None, ignore_errors
=False):
380 '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
381 return dictionary with {datacenter_id: vim_class, ... }. vim_class contain:
382 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
383 raise exception upon error
387 if nfvo_tenant
is not None: WHERE_dict
['nfvo_tenant_id'] = nfvo_tenant
388 if datacenter_id
is not None: WHERE_dict
['d.uuid'] = datacenter_id
389 if datacenter_tenant_id
is not None: WHERE_dict
['datacenter_tenant_id'] = datacenter_tenant_id
390 if datacenter_name
is not None: WHERE_dict
['d.name'] = datacenter_name
391 if vim_tenant
is not None: WHERE_dict
['dt.vim_tenant_id'] = vim_tenant
392 if vim_tenant_name
is not None: WHERE_dict
['vim_tenant_name'] = vim_tenant_name
393 if nfvo_tenant
or vim_tenant
or vim_tenant_name
or datacenter_tenant_id
:
394 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'
395 select_
= ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
396 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
397 'user','passwd', 'dt.config as dt_config')
399 from_
= 'datacenters as d'
400 select_
= ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
402 vims
= mydb
.get_rows(FROM
=from_
, SELECT
=select_
, WHERE
=WHERE_dict
)
405 extra
={'datacenter_tenant_id': vim
.get('datacenter_tenant_id'),
406 'datacenter_id': vim
.get('datacenter_id'),
407 '_vim_type_internal': vim
.get('type')}
409 extra
.update(yaml
.load(vim
["config"], Loader
=yaml
.Loader
))
410 if vim
.get('dt_config'):
411 extra
.update(yaml
.load(vim
["dt_config"], Loader
=yaml
.Loader
))
412 plugin_name
= "rovim_" + vim
["type"]
413 if plugin_name
not in plugins
:
415 _load_plugin(plugin_name
, type="vim")
416 except NfvoException
as e
:
418 logger
.error("{}".format(e
))
423 if 'datacenter_tenant_id' in vim
:
424 thread_id
= vim
["datacenter_tenant_id"]
425 if thread_id
not in vim_persistent_info
:
426 vim_persistent_info
[thread_id
] = {}
427 persistent_info
= vim_persistent_info
[thread_id
]
431 # return -httperrors.Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
432 vim_dict
[vim
['datacenter_id']] = plugins
[plugin_name
].vimconnector(
433 uuid
=vim
['datacenter_id'], name
=vim
['datacenter_name'],
434 tenant_id
=vim
.get('vim_tenant_id',vim_tenant
),
435 tenant_name
=vim
.get('vim_tenant_name',vim_tenant_name
),
436 url
=vim
['vim_url'], url_admin
=vim
['vim_url_admin'],
437 user
=vim
.get('user',vim_user
), passwd
=vim
.get('passwd',vim_passwd
),
438 config
=extra
, persistent_info
=persistent_info
440 except Exception as e
:
442 logger
.error("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)))
444 http_code
= httperrors
.Internal_Server_Error
445 if isinstance(e
, vimconn
.vimconnException
):
446 http_code
= e
.http_code
447 raise NfvoException("Error at VIM {}; {}: {}".format(vim
["type"], type(e
).__name
__, str(e
)), http_code
)
449 except db_base_Exception
as e
:
450 raise NfvoException(str(e
) + " at nfvo.get_vim", e
.http_code
)
453 def rollback(mydb
, vims
, rollback_list
):
455 #delete things by reverse order
456 for i
in range(len(rollback_list
)-1, -1, -1):
457 item
= rollback_list
[i
]
458 if item
["where"]=="vim":
459 if item
["vim_id"] not in vims
:
461 if is_task_id(item
["uuid"]):
463 vim
= vims
[item
["vim_id"]]
465 if item
["what"]=="image":
466 vim
.delete_image(item
["uuid"])
467 mydb
.delete_row(FROM
="datacenters_images", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
468 elif item
["what"]=="flavor":
469 vim
.delete_flavor(item
["uuid"])
470 mydb
.delete_row(FROM
="datacenters_flavors", WHERE
={"datacenter_vim_id": vim
["id"], "vim_id":item
["uuid"]})
471 elif item
["what"]=="network":
472 vim
.delete_network(item
["uuid"])
473 elif item
["what"]=="vm":
474 vim
.delete_vminstance(item
["uuid"])
475 except vimconn
.vimconnException
as e
:
476 logger
.error("Error in rollback. Not possible to delete VIM %s '%s'. Message: %s", item
['what'], item
["uuid"], str(e
))
477 undeleted_items
.append("{} {} from VIM {}".format(item
['what'], item
["uuid"], vim
["name"]))
478 except db_base_Exception
as e
:
479 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB.datacenters Message: %s", item
['what'], item
["uuid"], str(e
))
483 if item
["what"]=="image":
484 mydb
.delete_row(FROM
="images", WHERE
={"uuid": item
["uuid"]})
485 elif item
["what"]=="flavor":
486 mydb
.delete_row(FROM
="flavors", WHERE
={"uuid": item
["uuid"]})
487 except db_base_Exception
as e
:
488 logger
.error("Error in rollback. Not possible to delete %s '%s' from DB. Message: %s", item
['what'], item
["uuid"], str(e
))
489 undeleted_items
.append("{} '{}'".format(item
['what'], item
["uuid"]))
490 if len(undeleted_items
)==0:
491 return True," Rollback successful."
493 return False," Rollback fails to delete: " + str(undeleted_items
)
496 def check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1):
498 #create a dictionary with vnfc-name: vnfc:interface-list key:values pairs
500 for vnfc
in vnf_descriptor
["vnf"]["VNFC"]:
502 #dataplane interfaces
503 for numa
in vnfc
.get("numas",() ):
504 for interface
in numa
.get("interfaces",()):
505 if interface
["name"] in name_dict
:
507 "Error at vnf:VNFC[name:'{}']:numas:interfaces:name, interface name '{}' already used in this VNFC".format(
508 vnfc
["name"], interface
["name"]),
509 httperrors
.Bad_Request
)
510 name_dict
[ interface
["name"] ] = "underlay"
512 for interface
in vnfc
.get("bridge-ifaces",() ):
513 if interface
["name"] in name_dict
:
515 "Error at vnf:VNFC[name:'{}']:bridge-ifaces:name, interface name '{}' already used in this VNFC".format(
516 vnfc
["name"], interface
["name"]),
517 httperrors
.Bad_Request
)
518 name_dict
[ interface
["name"] ] = "overlay"
519 vnfc_interfaces
[ vnfc
["name"] ] = name_dict
520 # check bood-data info
521 # if "boot-data" in vnfc:
522 # # check that user-data is incompatible with users and config-files
523 # if (vnfc["boot-data"].get("users") or vnfc["boot-data"].get("config-files")) and vnfc["boot-data"].get("user-data"):
524 # raise NfvoException(
525 # "Error at vnf:VNFC:boot-data, fields 'users' and 'config-files' are not compatible with 'user-data'",
526 # httperrors.Bad_Request)
528 #check if the info in external_connections matches with the one in the vnfcs
530 for external_connection
in vnf_descriptor
["vnf"].get("external-connections",() ):
531 if external_connection
["name"] in name_list
:
533 "Error at vnf:external-connections:name, value '{}' already used as an external-connection".format(
534 external_connection
["name"]),
535 httperrors
.Bad_Request
)
536 name_list
.append(external_connection
["name"])
537 if external_connection
["VNFC"] not in vnfc_interfaces
:
539 "Error at vnf:external-connections[name:'{}']:VNFC, value '{}' does not match any VNFC".format(
540 external_connection
["name"], external_connection
["VNFC"]),
541 httperrors
.Bad_Request
)
543 if external_connection
["local_iface_name"] not in vnfc_interfaces
[ external_connection
["VNFC"] ]:
545 "Error at vnf:external-connections[name:'{}']:local_iface_name, value '{}' does not match any interface of this VNFC".format(
546 external_connection
["name"],
547 external_connection
["local_iface_name"]),
548 httperrors
.Bad_Request
)
550 #check if the info in internal_connections matches with the one in the vnfcs
552 for internal_connection
in vnf_descriptor
["vnf"].get("internal-connections",() ):
553 if internal_connection
["name"] in name_list
:
555 "Error at vnf:internal-connections:name, value '{}' already used as an internal-connection".format(
556 internal_connection
["name"]),
557 httperrors
.Bad_Request
)
558 name_list
.append(internal_connection
["name"])
559 #We should check that internal-connections of type "ptp" have only 2 elements
561 if len(internal_connection
["elements"])>2 and (internal_connection
.get("type") == "ptp" or internal_connection
.get("type") == "e-line"):
563 "Error at 'vnf:internal-connections[name:'{}']:elements', size must be 2 for a '{}' type. Consider change it to '{}' type".format(
564 internal_connection
["name"],
565 'ptp' if vnf_descriptor_version
==1 else 'e-line',
566 'data' if vnf_descriptor_version
==1 else "e-lan"),
567 httperrors
.Bad_Request
)
568 for port
in internal_connection
["elements"]:
570 iface
= port
["local_iface_name"]
571 if vnf
not in vnfc_interfaces
:
573 "Error at vnf:internal-connections[name:'{}']:elements[]:VNFC, value '{}' does not match any VNFC".format(
574 internal_connection
["name"], vnf
),
575 httperrors
.Bad_Request
)
576 if iface
not in vnfc_interfaces
[ vnf
]:
578 "Error at vnf:internal-connections[name:'{}']:elements[]:local_iface_name, value '{}' does not match any interface of this VNFC".format(
579 internal_connection
["name"], iface
),
580 httperrors
.Bad_Request
)
581 return -httperrors
.Bad_Request
,
582 if vnf_descriptor_version
==1 and "type" not in internal_connection
:
583 if vnfc_interfaces
[vnf
][iface
] == "overlay":
584 internal_connection
["type"] = "bridge"
586 internal_connection
["type"] = "data"
587 if vnf_descriptor_version
==2 and "implementation" not in internal_connection
:
588 if vnfc_interfaces
[vnf
][iface
] == "overlay":
589 internal_connection
["implementation"] = "overlay"
591 internal_connection
["implementation"] = "underlay"
592 if (internal_connection
.get("type") == "data" or internal_connection
.get("type") == "ptp" or \
593 internal_connection
.get("implementation") == "underlay") and vnfc_interfaces
[vnf
][iface
] == "overlay":
595 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
596 internal_connection
["name"],
597 iface
, 'bridge' if vnf_descriptor_version
==1 else 'overlay',
598 'data' if vnf_descriptor_version
==1 else 'underlay'),
599 httperrors
.Bad_Request
)
600 if (internal_connection
.get("type") == "bridge" or internal_connection
.get("implementation") == "overlay") and \
601 vnfc_interfaces
[vnf
][iface
] == "underlay":
603 "Error at vnf:internal-connections[name:'{}']:elements[]:{}, interface of type {} connected to an {} network".format(
604 internal_connection
["name"], iface
,
605 'data' if vnf_descriptor_version
==1 else 'underlay',
606 'bridge' if vnf_descriptor_version
==1 else 'overlay'),
607 httperrors
.Bad_Request
)
610 def create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=None):
612 if only_create_at_vim
:
613 image_mano_id
= image_dict
['uuid']
614 if return_on_error
== None:
615 return_on_error
= True
617 if image_dict
['location']:
618 images
= mydb
.get_rows(FROM
="images", WHERE
={'location':image_dict
['location'], 'metadata':image_dict
['metadata']})
620 images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name':image_dict
['universal_name'], 'checksum':image_dict
['checksum']})
622 image_mano_id
= images
[0]['uuid']
624 #create image in MANO DB
625 temp_image_dict
={'name':image_dict
['name'], 'description':image_dict
.get('description',None),
626 'location':image_dict
['location'], 'metadata':image_dict
.get('metadata',None),
627 'universal_name':image_dict
['universal_name'] , 'checksum':image_dict
['checksum']
629 #temp_image_dict['location'] = image_dict.get('new_location') if image_dict['location'] is None
630 image_mano_id
= mydb
.new_row('images', temp_image_dict
, add_uuid
=True)
631 rollback_list
.append({"where":"mano", "what":"image","uuid":image_mano_id
})
632 #create image at every vim
633 for vim_id
,vim
in vims
.items():
634 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
635 image_created
="false"
637 image_db
= mydb
.get_rows(FROM
="datacenters_images",
638 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'image_id': image_mano_id
})
639 #look at VIM if this image exist
641 if image_dict
['location'] is not None:
642 image_vim_id
= vim
.get_image_id_from_path(image_dict
['location'])
645 filter_dict
['name'] = image_dict
['universal_name']
646 if image_dict
.get('checksum') != None:
647 filter_dict
['checksum'] = image_dict
['checksum']
648 #logger.debug('>>>>>>>> Filter dict: %s', str(filter_dict))
649 vim_images
= vim
.get_image_list(filter_dict
)
650 #logger.debug('>>>>>>>> VIM images: %s', str(vim_images))
651 if len(vim_images
) > 1:
652 raise vimconn
.vimconnException("More than one candidate VIM image found for filter: {}".format(str(filter_dict
)), httperrors
.Conflict
)
653 elif len(vim_images
) == 0:
654 raise vimconn
.vimconnNotFoundException("Image not found at VIM with filter: '{}'".format(str(filter_dict
)))
656 #logger.debug('>>>>>>>> VIM image 0: %s', str(vim_images[0]))
657 image_vim_id
= vim_images
[0]['id']
659 except vimconn
.vimconnNotFoundException
as e
:
660 #Create the image in VIM only if image_dict['location'] or image_dict['new_location'] is not None
662 #image_dict['location']=image_dict.get('new_location') if image_dict['location'] is None
663 if image_dict
['location']:
664 image_vim_id
= vim
.new_image(image_dict
)
665 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"image","uuid":image_vim_id
})
668 #If we reach this point, then the image has image name, and optionally checksum, and could not be found
669 raise vimconn
.vimconnException(str(e
))
670 except vimconn
.vimconnException
as e
:
672 logger
.error("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
675 logger
.warn("Error creating image at VIM '%s': %s", vim
["name"], str(e
))
677 except vimconn
.vimconnException
as e
:
679 logger
.error("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
681 logger
.warn("Error contacting VIM to know if the image exists at VIM: %s", str(e
))
684 #if we reach here, the image has been created or existed
686 #add new vim_id at datacenters_images
687 mydb
.new_row('datacenters_images', {'datacenter_vim_id': datacenter_vim_id
,
688 'image_id':image_mano_id
,
689 'vim_id': image_vim_id
,
690 'created':image_created
})
691 elif image_db
[0]["vim_id"]!=image_vim_id
:
692 #modify existing vim_id at datacenters_images
693 mydb
.update_rows('datacenters_images', UPDATE
={'vim_id':image_vim_id
}, WHERE
={'datacenter_vim_id':vim_id
, 'image_id':image_mano_id
})
695 return image_vim_id
if only_create_at_vim
else image_mano_id
698 def create_or_use_flavor(mydb
, vims
, flavor_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
= None):
699 temp_flavor_dict
= {'disk':flavor_dict
.get('disk',0),
700 'ram':flavor_dict
.get('ram'),
701 'vcpus':flavor_dict
.get('vcpus'),
703 if 'extended' in flavor_dict
and flavor_dict
['extended']==None:
704 del flavor_dict
['extended']
705 if 'extended' in flavor_dict
:
706 temp_flavor_dict
['extended']=yaml
.safe_dump(flavor_dict
['extended'],default_flow_style
=True,width
=256)
708 #look if flavor exist
709 if only_create_at_vim
:
710 flavor_mano_id
= flavor_dict
['uuid']
711 if return_on_error
== None:
712 return_on_error
= True
714 flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
716 flavor_mano_id
= flavors
[0]['uuid']
719 #create one by one the images of aditional disks
720 dev_image_list
=[] #list of images
721 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None:
723 for device
in flavor_dict
['extended'].get('devices',[]):
724 if "image" not in device
and "image name" not in device
:
727 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
728 image_dict
['universal_name']=device
.get('image name')
729 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
730 image_dict
['location']=device
.get('image')
731 #image_dict['new_location']=vnfc.get('image location')
732 image_dict
['checksum']=device
.get('image checksum')
733 image_metadata_dict
= device
.get('image metadata', None)
734 image_metadata_str
= None
735 if image_metadata_dict
!= None:
736 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
737 image_dict
['metadata']=image_metadata_str
738 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
739 #print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
740 dev_image_list
.append(image_id
)
742 temp_flavor_dict
['name'] = flavor_dict
['name']
743 temp_flavor_dict
['description'] = flavor_dict
.get('description',None)
744 content
= mydb
.new_row('flavors', temp_flavor_dict
, add_uuid
=True)
745 flavor_mano_id
= content
746 rollback_list
.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id
})
747 #create flavor at every vim
748 if 'uuid' in flavor_dict
:
749 del flavor_dict
['uuid']
751 for vim_id
,vim
in vims
.items():
752 datacenter_vim_id
= vim
["config"]["datacenter_tenant_id"]
753 flavor_created
="false"
755 flavor_db
= mydb
.get_rows(FROM
="datacenters_flavors",
756 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
757 #look at VIM if this flavor exist SKIPPED
758 #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
760 # print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
764 # Create the flavor in VIM
765 # Translate images at devices from MANO id to VIM id
767 if 'extended' in flavor_dict
and flavor_dict
['extended']!=None and "devices" in flavor_dict
['extended']:
768 # make a copy of original devices
771 for device
in flavor_dict
["extended"].get("devices",[]):
774 devices_original
.append(dev
)
775 if 'image' in device
:
777 if 'image metadata' in device
:
778 del device
['image metadata']
779 if 'image checksum' in device
:
780 del device
['image checksum']
782 for index
in range(0,len(devices_original
)) :
783 device
=devices_original
[index
]
784 if "image" not in device
and "image name" not in device
:
785 # if 'size' in device:
786 disk_list
.append({'size': device
.get('size', default_volume_size
), 'name': device
.get('name')})
789 image_dict
['name']=device
.get('image name',flavor_dict
['name']+str(dev_nb
)+"-img")
790 image_dict
['universal_name']=device
.get('image name')
791 image_dict
['description']=flavor_dict
['name']+str(dev_nb
)+"-img"
792 image_dict
['location']=device
.get('image')
793 # image_dict['new_location']=device.get('image location')
794 image_dict
['checksum']=device
.get('image checksum')
795 image_metadata_dict
= device
.get('image metadata', None)
796 image_metadata_str
= None
797 if image_metadata_dict
!= None:
798 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
799 image_dict
['metadata']=image_metadata_str
800 image_mano_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=False, return_on_error
=return_on_error
)
801 image_dict
["uuid"]=image_mano_id
802 image_vim_id
=create_or_use_image(mydb
, vims
, image_dict
, rollback_list
, only_create_at_vim
=True, return_on_error
=return_on_error
)
804 #save disk information (image must be based on and size
805 disk_list
.append({'image_id': image_vim_id
, 'size': device
.get('size', default_volume_size
)})
807 flavor_dict
["extended"]["devices"][index
]['imageRef']=image_vim_id
810 #check that this vim_id exist in VIM, if not create
811 flavor_vim_id
=flavor_db
[0]["vim_id"]
813 vim
.get_flavor(flavor_vim_id
)
814 continue #flavor exist
815 except vimconn
.vimconnException
:
817 #create flavor at vim
818 logger
.debug("nfvo.create_or_use_flavor() adding flavor to VIM %s", vim
["name"])
821 flavor_vim_id
=vim
.get_flavor_id_from_data(flavor_dict
)
822 flavor_created
="false"
823 except vimconn
.vimconnException
as e
:
826 if not flavor_vim_id
:
827 flavor_vim_id
= vim
.new_flavor(flavor_dict
)
828 rollback_list
.append({"where":"vim", "vim_id": vim_id
, "what":"flavor","uuid":flavor_vim_id
})
829 flavor_created
="true"
830 except vimconn
.vimconnException
as e
:
832 logger
.error("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
834 logger
.warn("Error creating flavor at VIM %s: %s.", vim
["name"], str(e
))
837 #if reach here the flavor has been create or exist
838 if len(flavor_db
)==0:
839 #add new vim_id at datacenters_flavors
840 extended_devices_yaml
= None
841 if len(disk_list
) > 0:
842 extended_devices
= dict()
843 extended_devices
['disks'] = disk_list
844 extended_devices_yaml
= yaml
.safe_dump(extended_devices
,default_flow_style
=True,width
=256)
845 mydb
.new_row('datacenters_flavors',
846 {'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
, 'vim_id': flavor_vim_id
,
847 'created': flavor_created
, 'extended': extended_devices_yaml
})
848 elif flavor_db
[0]["vim_id"]!=flavor_vim_id
:
849 #modify existing vim_id at datacenters_flavors
850 mydb
.update_rows('datacenters_flavors', UPDATE
={'vim_id':flavor_vim_id
},
851 WHERE
={'datacenter_vim_id': datacenter_vim_id
, 'flavor_id': flavor_mano_id
})
853 return flavor_vim_id
if only_create_at_vim
else flavor_mano_id
856 def get_str(obj
, field
, length
):
858 Obtain the str value,
863 value
= obj
.get(field
)
864 if value
is not None:
865 value
= str(value
)[:length
]
868 def _lookfor_or_create_image(db_image
, mydb
, descriptor
):
870 fill image content at db_image dictionary. Check if the image with this image and checksum exist
871 :param db_image: dictionary to insert data
872 :param mydb: database connector
873 :param descriptor: yang descriptor
874 :return: uuid if the image exist at DB, or None if a new image must be created with the data filled at db_image
877 db_image
["name"] = get_str(descriptor
, "image", 255)
878 db_image
["checksum"] = get_str(descriptor
, "image-checksum", 32)
879 if not db_image
["checksum"]: # Ensure that if empty string, None is stored
880 db_image
["checksum"] = None
881 if db_image
["name"].startswith("/"):
882 db_image
["location"] = db_image
["name"]
883 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'location': db_image
["location"]})
885 db_image
["universal_name"] = db_image
["name"]
886 existing_images
= mydb
.get_rows(FROM
="images", WHERE
={'universal_name': db_image
['universal_name'],
887 'checksum': db_image
['checksum']})
889 return existing_images
[0]["uuid"]
891 image_uuid
= str(uuid4())
892 db_image
["uuid"] = image_uuid
895 def get_resource_allocation_params(quota_descriptor
):
897 read the quota_descriptor from vnfd and fetch the resource allocation properties from the descriptor object
898 :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
899 :return: quota params for limit, reserve, shares from the descriptor object
902 if quota_descriptor
.get("limit"):
903 quota
["limit"] = int(quota_descriptor
["limit"])
904 if quota_descriptor
.get("reserve"):
905 quota
["reserve"] = int(quota_descriptor
["reserve"])
906 if quota_descriptor
.get("shares"):
907 quota
["shares"] = int(quota_descriptor
["shares"])
910 def new_vnfd_v3(mydb
, tenant_id
, vnf_descriptor
):
912 Parses an OSM IM vnfd_catalog and insert at DB
915 :param vnf_descriptor:
916 :return: The list of cretated vnf ids
919 myvnfd
= vnfd_catalog
.vnfd()
921 pybindJSONDecoder
.load_ietf_json(vnf_descriptor
, None, None, obj
=myvnfd
, path_helper
=True,
923 except Exception as e
:
924 raise NfvoException("Error. Invalid VNF descriptor format " + str(e
), httperrors
.Bad_Request
)
932 db_ip_profiles_index
= 0
936 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd:vnfd-catalog")
937 if not vnfd_catalog_descriptor
:
938 vnfd_catalog_descriptor
= vnf_descriptor
.get("vnfd-catalog")
939 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd")
940 if not vnfd_descriptor_list
:
941 vnfd_descriptor_list
= vnfd_catalog_descriptor
.get("vnfd:vnfd")
942 for vnfd_yang
in myvnfd
.vnfd_catalog
.vnfd
.values():
943 vnfd
= vnfd_yang
.get()
946 vnf_uuid
= str(uuid4())
947 uuid_list
.append(vnf_uuid
)
948 vnfd_uuid_list
.append(vnf_uuid
)
949 vnfd_id
= get_str(vnfd
, "id", 255)
953 "name": get_str(vnfd
, "name", 255),
954 "description": get_str(vnfd
, "description", 255),
955 "tenant_id": tenant_id
,
956 "vendor": get_str(vnfd
, "vendor", 255),
957 "short_name": get_str(vnfd
, "short-name", 255),
958 "descriptor": str(vnf_descriptor
)[:60000]
961 for vnfd_descriptor
in vnfd_descriptor_list
:
962 if vnfd_descriptor
["id"] == str(vnfd
["id"]):
965 # table ip_profiles (ip-profiles)
966 ip_profile_name2db_table_index
= {}
967 for ip_profile
in vnfd
.get("ip-profiles").values():
969 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
970 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
971 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
972 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
973 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
974 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
977 for dns
in ip_profile
["ip-profile-params"]["dns-server"].values():
978 dns_list
.append(str(dns
.get("address")))
979 db_ip_profile
["dns_address"] = ";".join(dns_list
)
980 if ip_profile
["ip-profile-params"].get('security-group'):
981 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
982 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
983 db_ip_profiles_index
+= 1
984 db_ip_profiles
.append(db_ip_profile
)
986 # table nets (internal-vld)
987 net_id2uuid
= {} # for mapping interface with network
988 net_id2index
= {} # for mapping interface with network
989 for vld
in vnfd
.get("internal-vld").values():
990 net_uuid
= str(uuid4())
991 uuid_list
.append(net_uuid
)
993 "name": get_str(vld
, "name", 255),
996 "description": get_str(vld
, "description", 255),
997 "osm_id": get_str(vld
, "id", 255),
998 "type": "bridge", # TODO adjust depending on connection point type
1000 net_id2uuid
[vld
.get("id")] = net_uuid
1001 net_id2index
[vld
.get("id")] = len(db_nets
)
1002 db_nets
.append(db_net
)
1003 # ip-profile, link db_ip_profile with db_sce_net
1004 if vld
.get("ip-profile-ref"):
1005 ip_profile_name
= vld
.get("ip-profile-ref")
1006 if ip_profile_name
not in ip_profile_name2db_table_index
:
1007 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vld[{}]':'ip-profile-ref':"
1008 "'{}'. Reference to a non-existing 'ip_profiles'".format(
1009 str(vnfd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
1010 httperrors
.Bad_Request
)
1011 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["net_id"] = net_uuid
1012 else: #check no ip-address has been defined
1013 for icp
in vld
.get("internal-connection-point").values():
1014 if icp
.get("ip-address"):
1015 raise NfvoException("Error at 'vnfd[{}]':'vld[{}]':'internal-connection-point[{}]' "
1016 "contains an ip-address but no ip-profile has been defined at VLD".format(
1017 str(vnfd
["id"]), str(vld
["id"]), str(icp
["id"])),
1018 httperrors
.Bad_Request
)
1020 # connection points vaiable declaration
1021 cp_name2iface_uuid
= {}
1023 cp_name2vm_uuid
= {}
1024 cp_name2db_interface
= {}
1025 vdu_id2cp_name
= {} # stored only when one external connection point is presented at this VDU
1029 vdu_id2db_table_index
= {}
1031 for vdu
in vnfd
.get("vdu").values():
1033 for vdu_descriptor
in vnfd_descriptor
["vdu"]:
1034 if vdu_descriptor
["id"] == str(vdu
["id"]):
1036 vm_uuid
= str(uuid4())
1037 uuid_list
.append(vm_uuid
)
1038 vdu_id
= get_str(vdu
, "id", 255)
1042 "name": get_str(vdu
, "name", 255),
1043 "description": get_str(vdu
, "description", 255),
1044 "pdu_type": get_str(vdu
, "pdu-type", 255),
1047 vdu_id2uuid
[db_vm
["osm_id"]] = vm_uuid
1048 vdu_id2db_table_index
[db_vm
["osm_id"]] = db_vms_index
1049 if vdu
.get("count"):
1050 db_vm
["count"] = int(vdu
["count"])
1053 image_present
= False
1054 if vdu
.get("image"):
1055 image_present
= True
1057 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, vdu
)
1059 image_uuid
= db_image
["uuid"]
1060 db_images
.append(db_image
)
1061 db_vm
["image_id"] = image_uuid
1062 if vdu
.get("alternative-images"):
1063 vm_alternative_images
= []
1064 for alt_image
in vdu
.get("alternative-images").values():
1066 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, alt_image
)
1068 image_uuid
= db_image
["uuid"]
1069 db_images
.append(db_image
)
1070 vm_alternative_images
.append({
1071 "image_id": image_uuid
,
1072 "vim_type": str(alt_image
["vim-type"]),
1073 # "universal_name": str(alt_image["image"]),
1074 # "checksum": str(alt_image["image-checksum"]) if alt_image.get("image-checksum") else None
1077 db_vm
["image_list"] = yaml
.safe_dump(vm_alternative_images
, default_flow_style
=True, width
=256)
1081 if vdu
.get("volumes"):
1082 for volume_key
in vdu
["volumes"]:
1083 volume
= vdu
["volumes"][volume_key
]
1084 if not image_present
:
1085 # Convert the first volume to vnfc.image
1086 image_present
= True
1088 image_uuid
= _lookfor_or_create_image(db_image
, mydb
, volume
)
1090 image_uuid
= db_image
["uuid"]
1091 db_images
.append(db_image
)
1092 db_vm
["image_id"] = image_uuid
1094 # Add Openmano devices
1095 device
= {"name": str(volume
.get("name"))}
1096 device
["type"] = str(volume
.get("device-type"))
1097 if volume
.get("size"):
1098 device
["size"] = int(volume
["size"])
1099 if volume
.get("image"):
1100 device
["image name"] = str(volume
["image"])
1101 if volume
.get("image-checksum"):
1102 device
["image checksum"] = str(volume
["image-checksum"])
1104 devices
.append(device
)
1106 if not db_vm
.get("image_id"):
1107 if not db_vm
["pdu_type"]:
1108 raise NfvoException("Not defined image for VDU")
1109 # create a fake image
1113 if vdu
.get("cloud-init"):
1114 boot_data
["user-data"] = str(vdu
["cloud-init"])
1115 elif vdu
.get("cloud-init-file"):
1116 # TODO Where this file content is present???
1117 # boot_data["user-data"] = vnfd_yang.files[vdu["cloud-init-file"]]
1118 boot_data
["user-data"] = str(vdu
["cloud-init-file"])
1120 if vdu
.get("supplemental-boot-data"):
1121 if vdu
["supplemental-boot-data"].get('boot-data-drive'):
1122 boot_data
['boot-data-drive'] = True
1123 if vdu
["supplemental-boot-data"].get('config-file'):
1124 om_cfgfile_list
= list()
1125 for custom_config_file
in vdu
["supplemental-boot-data"]['config-file'].values():
1126 # TODO Where this file content is present???
1127 cfg_source
= str(custom_config_file
["source"])
1128 om_cfgfile_list
.append({"dest": custom_config_file
["dest"],
1129 "content": cfg_source
})
1130 boot_data
['config-files'] = om_cfgfile_list
1132 db_vm
["boot_data"] = yaml
.safe_dump(boot_data
, default_flow_style
=True, width
=256)
1134 db_vms
.append(db_vm
)
1137 # table interfaces (internal/external interfaces)
1138 flavor_epa_interfaces
= []
1139 # for iface in chain(vdu.get("internal-interface").values(), vdu.get("external-interface").values()):
1140 for iface
in vdu
.get("interface").values():
1141 flavor_epa_interface
= {}
1142 iface_uuid
= str(uuid4())
1143 uuid_list
.append(iface_uuid
)
1146 "internal_name": get_str(iface
, "name", 255),
1149 flavor_epa_interface
["name"] = db_interface
["internal_name"]
1150 if iface
.get("virtual-interface").get("vpci"):
1151 db_interface
["vpci"] = get_str(iface
.get("virtual-interface"), "vpci", 12)
1152 flavor_epa_interface
["vpci"] = db_interface
["vpci"]
1154 if iface
.get("virtual-interface").get("bandwidth"):
1155 bps
= int(iface
.get("virtual-interface").get("bandwidth"))
1156 db_interface
["bw"] = int(math
.ceil(bps
/ 1000000.0))
1157 flavor_epa_interface
["bandwidth"] = "{} Mbps".format(db_interface
["bw"])
1159 if iface
.get("virtual-interface").get("type") == "OM-MGMT":
1160 db_interface
["type"] = "mgmt"
1161 elif iface
.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
1162 db_interface
["type"] = "bridge"
1163 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1164 elif iface
.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
1165 db_interface
["type"] = "data"
1166 db_interface
["model"] = get_str(iface
.get("virtual-interface"), "type", 12)
1167 flavor_epa_interface
["dedicated"] = "no" if iface
["virtual-interface"]["type"] == "SR-IOV" \
1169 flavor_epa_interfaces
.append(flavor_epa_interface
)
1171 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{}]':'vdu[{}]':'interface':'virtual"
1172 "-interface':'type':'{}'. Interface type is not supported".format(
1173 vnfd_id
, vdu_id
, iface
.get("virtual-interface").get("type")),
1174 httperrors
.Bad_Request
)
1176 if iface
.get("mgmt-interface"):
1177 db_interface
["type"] = "mgmt"
1179 if iface
.get("external-connection-point-ref"):
1181 cp
= vnfd
.get("connection-point")[iface
.get("external-connection-point-ref")]
1182 db_interface
["external_name"] = get_str(cp
, "name", 255)
1183 cp_name2iface_uuid
[db_interface
["external_name"]] = iface_uuid
1184 cp_name2vdu_id
[db_interface
["external_name"]] = vdu_id
1185 cp_name2vm_uuid
[db_interface
["external_name"]] = vm_uuid
1186 cp_name2db_interface
[db_interface
["external_name"]] = db_interface
1187 for cp_descriptor
in vnfd_descriptor
["connection-point"]:
1188 if cp_descriptor
["name"] == db_interface
["external_name"]:
1193 if vdu_id
in vdu_id2cp_name
:
1194 vdu_id2cp_name
[vdu_id
] = None # more than two connection point for this VDU
1196 vdu_id2cp_name
[vdu_id
] = db_interface
["external_name"]
1199 if str(cp_descriptor
.get("port-security-enabled")).lower() == "false":
1200 db_interface
["port_security"] = 0
1201 elif str(cp_descriptor
.get("port-security-enabled")).lower() == "true":
1202 db_interface
["port_security"] = 1
1204 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1205 "'interface[{iface}]':'vnfd-connection-point-ref':'{cp}' is not present"
1206 " at connection-point".format(
1207 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1208 cp
=iface
.get("vnfd-connection-point-ref")),
1209 httperrors
.Bad_Request
)
1210 elif iface
.get("internal-connection-point-ref"):
1212 for icp_descriptor
in vdu_descriptor
["internal-connection-point"]:
1213 if icp_descriptor
["id"] == str(iface
.get("internal-connection-point-ref")):
1216 raise KeyError("does not exist at vdu:internal-connection-point")
1219 for vld
in vnfd
.get("internal-vld").values():
1220 for cp
in vld
.get("internal-connection-point").values():
1221 if cp
.get("id-ref") == iface
.get("internal-connection-point-ref"):
1223 raise KeyError("is referenced by more than one 'internal-vld'")
1227 raise KeyError("is not referenced by any 'internal-vld'")
1229 # set network type as data
1230 if iface
.get("virtual-interface") and iface
["virtual-interface"].get("type") in \
1231 ("SR-IOV", "PCI-PASSTHROUGH"):
1232 db_nets
[net_id2index
[icp_vld
.get("id")]]["type"] = "data"
1233 db_interface
["net_id"] = net_id2uuid
[icp_vld
.get("id")]
1234 if str(icp_descriptor
.get("port-security-enabled")).lower() == "false":
1235 db_interface
["port_security"] = 0
1236 elif str(icp_descriptor
.get("port-security-enabled")).lower() == "true":
1237 db_interface
["port_security"] = 1
1238 if icp
.get("ip-address"):
1239 if not icp_vld
.get("ip-profile-ref"):
1241 db_interface
["ip_address"] = str(icp
.get("ip-address"))
1242 except KeyError as e
:
1243 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'vdu[{vdu}]':"
1244 "'interface[{iface}]':'internal-connection-point-ref':'{cp}'"
1246 vnf
=vnfd_id
, vdu
=vdu_id
, iface
=iface
["name"],
1247 cp
=iface
.get("internal-connection-point-ref"), msg
=str(e
)),
1248 httperrors
.Bad_Request
)
1249 if iface
.get("position"):
1250 db_interface
["created_at"] = int(iface
.get("position")) * 50
1251 if iface
.get("mac-address"):
1252 db_interface
["mac"] = str(iface
.get("mac-address"))
1253 db_interfaces
.append(db_interface
)
1257 "name": get_str(vdu
, "name", 250) + "-flv",
1258 "vcpus": int(vdu
["vm-flavor"].get("vcpu-count", 1)),
1259 "ram": int(vdu
["vm-flavor"].get("memory-mb", 1)),
1260 "disk": int(vdu
["vm-flavor"].get("storage-gb", 0)),
1262 # TODO revise the case of several numa-node-policy node
1266 extended
["devices"] = devices
1267 if flavor_epa_interfaces
:
1268 numa
["interfaces"] = flavor_epa_interfaces
1269 if vdu
.get("guest-epa"): # TODO or dedicated_int:
1270 epa_vcpu_set
= False
1271 if vdu
["guest-epa"].get("numa-node-policy"): # TODO or dedicated_int:
1272 numa_node_policy
= vdu
["guest-epa"].get("numa-node-policy")
1273 if numa_node_policy
.get("node"):
1274 numa_node
= next(iter(numa_node_policy
["node"].values()))
1275 if numa_node
.get("num-cores"):
1276 numa
["cores"] = numa_node
["num-cores"]
1278 if numa_node
.get("paired-threads"):
1279 if numa_node
["paired-threads"].get("num-paired-threads"):
1280 numa
["paired-threads"] = int(numa_node
["paired-threads"]["num-paired-threads"])
1282 if len(numa_node
["paired-threads"].get("paired-thread-ids")):
1283 numa
["paired-threads-id"] = []
1284 for pair
in numa_node
["paired-threads"]["paired-thread-ids"].values():
1285 numa
["paired-threads-id"].append(
1286 (str(pair
["thread-a"]), str(pair
["thread-b"]))
1288 if numa_node
.get("num-threads"):
1289 numa
["threads"] = int(numa_node
["num-threads"])
1291 if numa_node
.get("memory-mb"):
1292 numa
["memory"] = max(int(numa_node
["memory-mb"] / 1024), 1)
1293 if vdu
["guest-epa"].get("mempage-size"):
1294 if vdu
["guest-epa"]["mempage-size"] != "SMALL":
1295 numa
["memory"] = max(int(db_flavor
["ram"] / 1024), 1)
1296 if vdu
["guest-epa"].get("cpu-pinning-policy") and not epa_vcpu_set
:
1297 if vdu
["guest-epa"]["cpu-pinning-policy"] == "DEDICATED":
1298 if vdu
["guest-epa"].get("cpu-thread-pinning-policy") and \
1299 vdu
["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
1300 numa
["cores"] = max(db_flavor
["vcpus"], 1)
1302 numa
["threads"] = max(db_flavor
["vcpus"], 1)
1304 if vdu
["guest-epa"].get("cpu-quota") and not epa_vcpu_set
:
1305 cpuquota
= get_resource_allocation_params(vdu
["guest-epa"].get("cpu-quota"))
1307 extended
["cpu-quota"] = cpuquota
1308 if vdu
["guest-epa"].get("mem-quota"):
1309 vduquota
= get_resource_allocation_params(vdu
["guest-epa"].get("mem-quota"))
1311 extended
["mem-quota"] = vduquota
1312 if vdu
["guest-epa"].get("disk-io-quota"):
1313 diskioquota
= get_resource_allocation_params(vdu
["guest-epa"].get("disk-io-quota"))
1315 extended
["disk-io-quota"] = diskioquota
1316 if vdu
["guest-epa"].get("vif-quota"):
1317 vifquota
= get_resource_allocation_params(vdu
["guest-epa"].get("vif-quota"))
1319 extended
["vif-quota"] = vifquota
1321 extended
["numas"] = [numa
]
1323 extended_text
= yaml
.safe_dump(extended
, default_flow_style
=True, width
=256)
1324 db_flavor
["extended"] = extended_text
1325 # look if flavor exist
1326 temp_flavor_dict
= {'disk': db_flavor
.get('disk', 0),
1327 'ram': db_flavor
.get('ram'),
1328 'vcpus': db_flavor
.get('vcpus'),
1329 'extended': db_flavor
.get('extended')
1331 existing_flavors
= mydb
.get_rows(FROM
="flavors", WHERE
=temp_flavor_dict
)
1332 if existing_flavors
:
1333 flavor_uuid
= existing_flavors
[0]["uuid"]
1335 flavor_uuid
= str(uuid4())
1336 uuid_list
.append(flavor_uuid
)
1337 db_flavor
["uuid"] = flavor_uuid
1338 db_flavors
.append(db_flavor
)
1339 db_vm
["flavor_id"] = flavor_uuid
1341 # VNF affinity and antiaffinity
1342 for pg
in vnfd
.get("placement-groups").values():
1343 pg_name
= get_str(pg
, "name", 255)
1344 for vdu
in pg
.get("member-vdus").values():
1345 vdu_id
= get_str(vdu
, "member-vdu-ref", 255)
1346 if vdu_id
not in vdu_id2db_table_index
:
1347 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'placement-groups[{pg}]':"
1348 "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
1349 vnf
=vnfd_id
, pg
=pg_name
, vdu
=vdu_id
),
1350 httperrors
.Bad_Request
)
1351 db_vms
[vdu_id2db_table_index
[vdu_id
]]["availability_zone"] = pg_name
1352 # TODO consider the case of isolation and not colocation
1353 # if pg.get("strategy") == "ISOLATION":
1355 # VNF mgmt configuration
1356 if vnfd
["mgmt-interface"].get("vdu-id"):
1357 mgmt_vdu_id
= get_str(vnfd
["mgmt-interface"], "vdu-id", 255)
1358 if mgmt_vdu_id
not in vdu_id2uuid
:
1359 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'vdu-id':"
1360 "'{vdu}'. Reference to a non-existing vdu".format(
1361 vnf
=vnfd_id
, vdu
=mgmt_vdu_id
),
1362 httperrors
.Bad_Request
)
1363 mgmt_access
["vm_id"] = vdu_id2uuid
[mgmt_vdu_id
]
1364 mgmt_access
["vdu-id"] = mgmt_vdu_id
1365 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
1366 if vdu_id2cp_name
.get(mgmt_vdu_id
):
1367 if cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]:
1368 cp_name2db_interface
[vdu_id2cp_name
[mgmt_vdu_id
]]["type"] = "mgmt"
1370 if vnfd
["mgmt-interface"].get("ip-address"):
1371 mgmt_access
["ip-address"] = str(vnfd
["mgmt-interface"].get("ip-address"))
1372 if vnfd
["mgmt-interface"].get("cp") and vnfd
.get("vdu"):
1373 if vnfd
["mgmt-interface"]["cp"] not in cp_name2iface_uuid
:
1374 raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
1375 "Reference to a non-existing connection-point".format(
1376 vnf
=vnfd_id
, cp
=vnfd
["mgmt-interface"]["cp"]),
1377 httperrors
.Bad_Request
)
1378 mgmt_access
["vm_id"] = cp_name2vm_uuid
[vnfd
["mgmt-interface"]["cp"]]
1379 mgmt_access
["interface_id"] = cp_name2iface_uuid
[vnfd
["mgmt-interface"]["cp"]]
1380 mgmt_access
["vdu-id"] = cp_name2vdu_id
[vnfd
["mgmt-interface"]["cp"]]
1381 # mark this interface as of type mgmt
1382 if cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]:
1383 cp_name2db_interface
[vnfd
["mgmt-interface"]["cp"]]["type"] = "mgmt"
1385 default_user
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1388 mgmt_access
["default_user"] = default_user
1390 required
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
1393 mgmt_access
["required"] = required
1395 password_
= get_str(vnfd
.get("vnf-configuration", {}).get("config-access", {}),
1398 mgmt_access
["password"] = password_
1401 db_vnf
["mgmt_access"] = yaml
.safe_dump(mgmt_access
, default_flow_style
=True, width
=256)
1403 db_vnfs
.append(db_vnf
)
1407 {"images": db_images
},
1408 {"flavors": db_flavors
},
1409 {"ip_profiles": db_ip_profiles
},
1411 {"interfaces": db_interfaces
},
1414 logger
.debug("create_vnf Deployment done vnfDict: %s",
1415 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
1416 mydb
.new_rows(db_tables
, uuid_list
)
1417 return vnfd_uuid_list
1418 except NfvoException
:
1420 except Exception as e
:
1421 logger
.error("Exception {}".format(e
))
1422 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
1425 @deprecated("Use new_vnfd_v3")
1426 def new_vnf(mydb
, tenant_id
, vnf_descriptor
):
1427 global global_config
1429 # Step 1. Check the VNF descriptor
1430 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=1)
1431 # Step 2. Check tenant exist
1433 if tenant_id
!= "any":
1434 check_tenant(mydb
, tenant_id
)
1435 if "tenant_id" in vnf_descriptor
["vnf"]:
1436 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1437 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1438 httperrors
.Unauthorized
)
1440 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1441 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1442 if global_config
["auto_push_VNF_to_VIMs"]:
1443 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1445 # Step 4. Review the descriptor and add missing fields
1446 #print vnf_descriptor
1447 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1448 vnf_name
= vnf_descriptor
['vnf']['name']
1449 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1450 if "physical" in vnf_descriptor
['vnf']:
1451 del vnf_descriptor
['vnf']['physical']
1452 #print vnf_descriptor
1454 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1455 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1456 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1458 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1459 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1460 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1462 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1463 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1465 VNFCitem
["name"] = vnfc
['name']
1466 VNFCitem
["availability_zone"] = vnfc
.get('availability_zone')
1467 VNFCitem
["description"] = vnfc
.get("description", 'VM {} of the VNF {}'.format(vnfc
['name'],vnf_name
))
1469 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1472 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1473 myflavorDict
["description"] = VNFCitem
["description"]
1474 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1475 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1476 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1477 myflavorDict
["extended"] = {}
1479 devices
= vnfc
.get("devices")
1481 myflavorDict
["extended"]["devices"] = devices
1484 # 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
1485 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1487 # Previous code has been commented
1488 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1489 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1490 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1491 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1493 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1495 # print "Error creating flavor: unknown processor model. Rollback successful."
1496 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1498 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1499 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1501 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1502 myflavorDict
['extended']['numas'] = vnfc
['numas']
1506 # Step 6.2 New flavors are created in the VIM
1507 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1509 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1510 VNFCitem
["flavor_id"] = flavor_id
1511 VNFCDict
[vnfc
['name']] = VNFCitem
1513 logger
.debug("Creating new images in the VIM for each VNFC")
1514 # Step 6.3 New images are created in the VIM
1515 #For each VNFC, we must create the appropriate image.
1516 #This "for" loop might be integrated with the previous one
1517 #In case this integration is made, the VNFCDict might become a VNFClist.
1518 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1519 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1521 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1522 image_dict
['universal_name']=vnfc
.get('image name')
1523 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1524 image_dict
['location']=vnfc
.get('VNFC image')
1525 #image_dict['new_location']=vnfc.get('image location')
1526 image_dict
['checksum']=vnfc
.get('image checksum')
1527 image_metadata_dict
= vnfc
.get('image metadata', None)
1528 image_metadata_str
= None
1529 if image_metadata_dict
is not None:
1530 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1531 image_dict
['metadata']=image_metadata_str
1532 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1533 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1534 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1535 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1536 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1537 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1538 if vnfc
.get("boot-data"):
1539 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1542 # Step 7. Storing the VNF descriptor in the repository
1543 if "descriptor" not in vnf_descriptor
["vnf"]:
1544 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1546 # Step 8. Adding the VNF to the NFVO DB
1547 vnf_id
= mydb
.new_vnf_as_a_whole(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1549 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1550 _
, message
= rollback(mydb
, vims
, rollback_list
)
1551 if isinstance(e
, db_base_Exception
):
1552 error_text
= "Exception at database"
1553 elif isinstance(e
, KeyError):
1554 error_text
= "KeyError exception "
1555 e
.http_code
= httperrors
.Internal_Server_Error
1557 error_text
= "Exception at VIM"
1558 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1559 #logger.error("start_scenario %s", error_text)
1560 raise NfvoException(error_text
, e
.http_code
)
1563 @deprecated("Use new_vnfd_v3")
1564 def new_vnf_v02(mydb
, tenant_id
, vnf_descriptor
):
1565 global global_config
1567 # Step 1. Check the VNF descriptor
1568 check_vnf_descriptor(vnf_descriptor
, vnf_descriptor_version
=2)
1569 # Step 2. Check tenant exist
1571 if tenant_id
!= "any":
1572 check_tenant(mydb
, tenant_id
)
1573 if "tenant_id" in vnf_descriptor
["vnf"]:
1574 if vnf_descriptor
["vnf"]["tenant_id"] != tenant_id
:
1575 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor
["vnf"]["tenant_id"], tenant_id
),
1576 httperrors
.Unauthorized
)
1578 vnf_descriptor
['vnf']['tenant_id'] = tenant_id
1579 # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
1580 if global_config
["auto_push_VNF_to_VIMs"]:
1581 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1583 # Step 4. Review the descriptor and add missing fields
1584 #print vnf_descriptor
1585 #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
1586 vnf_name
= vnf_descriptor
['vnf']['name']
1587 vnf_descriptor
['vnf']['description'] = vnf_descriptor
['vnf'].get("description", vnf_name
)
1588 if "physical" in vnf_descriptor
['vnf']:
1589 del vnf_descriptor
['vnf']['physical']
1590 #print vnf_descriptor
1592 # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
1593 logger
.debug('BEGIN creation of VNF "%s"' % vnf_name
)
1594 logger
.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name
,len(vnf_descriptor
['vnf']['VNFC'])))
1596 #For each VNFC, we add it to the VNFCDict and we create a flavor.
1597 VNFCDict
= {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
1598 rollback_list
= [] # It will contain the new images created in mano. It is used for rollback
1600 logger
.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
1601 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1603 VNFCitem
["name"] = vnfc
['name']
1604 VNFCitem
["description"] = vnfc
.get("description", 'VM {} of the VNF {}'.format(vnfc
['name'],vnf_name
))
1606 #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
1609 myflavorDict
["name"] = vnfc
['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
1610 myflavorDict
["description"] = VNFCitem
["description"]
1611 myflavorDict
["ram"] = vnfc
.get("ram", 0)
1612 myflavorDict
["vcpus"] = vnfc
.get("vcpus", 0)
1613 myflavorDict
["disk"] = vnfc
.get("disk", 0)
1614 myflavorDict
["extended"] = {}
1616 devices
= vnfc
.get("devices")
1618 myflavorDict
["extended"]["devices"] = devices
1621 # 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
1622 # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
1624 # Previous code has been commented
1625 #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
1626 # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
1627 #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
1628 # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
1630 # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
1632 # print "Error creating flavor: unknown processor model. Rollback successful."
1633 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
1635 # return -httperrors.Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
1636 myflavorDict
['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
1638 if 'numas' in vnfc
and len(vnfc
['numas'])>0:
1639 myflavorDict
['extended']['numas'] = vnfc
['numas']
1643 # Step 6.2 New flavors are created in the VIM
1644 flavor_id
= create_or_use_flavor(mydb
, vims
, myflavorDict
, rollback_list
)
1646 #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
1647 VNFCitem
["flavor_id"] = flavor_id
1648 VNFCDict
[vnfc
['name']] = VNFCitem
1650 logger
.debug("Creating new images in the VIM for each VNFC")
1651 # Step 6.3 New images are created in the VIM
1652 #For each VNFC, we must create the appropriate image.
1653 #This "for" loop might be integrated with the previous one
1654 #In case this integration is made, the VNFCDict might become a VNFClist.
1655 for vnfc
in vnf_descriptor
['vnf']['VNFC']:
1656 #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
1658 image_dict
['name']=vnfc
.get('image name',vnf_name
+"-"+vnfc
['name']+"-img")
1659 image_dict
['universal_name']=vnfc
.get('image name')
1660 image_dict
['description']=vnfc
.get('image name', VNFCDict
[vnfc
['name']]['description'])
1661 image_dict
['location']=vnfc
.get('VNFC image')
1662 #image_dict['new_location']=vnfc.get('image location')
1663 image_dict
['checksum']=vnfc
.get('image checksum')
1664 image_metadata_dict
= vnfc
.get('image metadata', None)
1665 image_metadata_str
= None
1666 if image_metadata_dict
is not None:
1667 image_metadata_str
= yaml
.safe_dump(image_metadata_dict
,default_flow_style
=True,width
=256)
1668 image_dict
['metadata']=image_metadata_str
1669 #print "create_or_use_image", mydb, vims, image_dict, rollback_list
1670 image_id
= create_or_use_image(mydb
, vims
, image_dict
, rollback_list
)
1671 #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
1672 VNFCDict
[vnfc
['name']]["image_id"] = image_id
1673 VNFCDict
[vnfc
['name']]["image_path"] = vnfc
.get('VNFC image')
1674 VNFCDict
[vnfc
['name']]["count"] = vnfc
.get('count', 1)
1675 if vnfc
.get("boot-data"):
1676 VNFCDict
[vnfc
['name']]["boot_data"] = yaml
.safe_dump(vnfc
["boot-data"], default_flow_style
=True, width
=256)
1678 # Step 7. Storing the VNF descriptor in the repository
1679 if "descriptor" not in vnf_descriptor
["vnf"]:
1680 vnf_descriptor
["vnf"]["descriptor"] = yaml
.safe_dump(vnf_descriptor
, indent
=4, explicit_start
=True, default_flow_style
=False)
1682 # Step 8. Adding the VNF to the NFVO DB
1683 vnf_id
= mydb
.new_vnf_as_a_whole2(tenant_id
,vnf_name
,vnf_descriptor
,VNFCDict
)
1685 except (db_base_Exception
, vimconn
.vimconnException
, KeyError) as e
:
1686 _
, message
= rollback(mydb
, vims
, rollback_list
)
1687 if isinstance(e
, db_base_Exception
):
1688 error_text
= "Exception at database"
1689 elif isinstance(e
, KeyError):
1690 error_text
= "KeyError exception "
1691 e
.http_code
= httperrors
.Internal_Server_Error
1693 error_text
= "Exception at VIM"
1694 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
1695 #logger.error("start_scenario %s", error_text)
1696 raise NfvoException(error_text
, e
.http_code
)
1699 def get_vnf_id(mydb
, tenant_id
, vnf_id
):
1700 #check valid tenant_id
1701 check_tenant(mydb
, tenant_id
)
1704 if tenant_id
!= "any":
1705 where_or
["tenant_id"] = tenant_id
1706 where_or
["public"] = True
1707 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1709 vnf_id
= vnf
["uuid"]
1710 filter_keys
= ('uuid', 'name', 'description', 'public', "tenant_id", "osm_id", "created_at")
1711 filtered_content
= dict( (k
,v
) for k
,v
in vnf
.items() if k
in filter_keys
)
1712 #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
1713 data
={'vnf' : filtered_content
}
1715 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id',
1716 SELECT
=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
1718 WHERE
={'vnfs.uuid': vnf_id
} )
1719 if len(content
) != 0:
1720 #raise NfvoException("vnf '{}' not found".format(vnf_id), httperrors.Not_Found)
1721 # change boot_data into boot-data
1723 if vm
.get("boot_data"):
1724 vm
["boot-data"] = yaml
.safe_load(vm
["boot_data"])
1727 data
['vnf']['VNFC'] = content
1728 #TODO: GET all the information from a VNFC and include it in the output.
1731 content
= mydb
.get_rows(FROM
='vnfs join nets on vnfs.uuid=nets.vnf_id',
1732 SELECT
=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
1733 WHERE
={'vnfs.uuid': vnf_id
} )
1734 data
['vnf']['nets'] = content
1736 #GET ip-profile for each net
1737 for net
in data
['vnf']['nets']:
1738 ipprofiles
= mydb
.get_rows(FROM
='ip_profiles',
1739 SELECT
=('ip_version','subnet_address','gateway_address','dns_address','dhcp_enabled','dhcp_start_address','dhcp_count'),
1740 WHERE
={'net_id': net
["uuid"]} )
1741 if len(ipprofiles
)==1:
1742 net
["ip_profile"] = ipprofiles
[0]
1743 elif len(ipprofiles
)>1:
1744 raise NfvoException("More than one ip-profile found with this criteria: net_id='{}'".format(net
['uuid']), httperrors
.Bad_Request
)
1747 #TODO: For each net, GET its elements and relevant info per element (VNFC, iface, ip_address) and include them in the output.
1749 #GET External Interfaces
1750 content
= mydb
.get_rows(FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
1751 SELECT
=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
1752 'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
1753 WHERE
={'vnfs.uuid': vnf_id
, 'interfaces.external_name<>': None} )
1755 data
['vnf']['external-connections'] = content
1760 def delete_vnf(mydb
,tenant_id
,vnf_id
,datacenter
=None,vim_tenant
=None):
1761 # Check tenant exist
1762 if tenant_id
!= "any":
1763 check_tenant(mydb
, tenant_id
)
1764 # Get the URL of the VIM from the nfvo_tenant and the datacenter
1765 vims
= get_vim(mydb
, tenant_id
, ignore_errors
=True)
1769 # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
1771 if tenant_id
!= "any":
1772 where_or
["tenant_id"] = tenant_id
1773 where_or
["public"] = True
1774 vnf
= mydb
.get_table_by_uuid_name('vnfs', vnf_id
, "VNF", WHERE_OR
=where_or
, WHERE_AND_OR
="AND")
1775 vnf_id
= vnf
["uuid"]
1777 # "Getting the list of flavors and tenants of the VNF"
1778 flavorList
= get_flavorlist(mydb
, vnf_id
)
1779 if len(flavorList
)==0:
1780 logger
.warn("delete_vnf error. No flavors found for the VNF id '%s'", vnf_id
)
1782 imageList
= get_imagelist(mydb
, vnf_id
)
1783 if len(imageList
)==0:
1784 logger
.warn( "delete_vnf error. No images found for the VNF id '%s'", vnf_id
)
1786 deleted
= mydb
.delete_row_by_id('vnfs', vnf_id
)
1788 raise NfvoException("vnf '{}' not found".format(vnf_id
), httperrors
.Not_Found
)
1791 for flavor
in flavorList
:
1792 #check if flavor is used by other vnf
1794 c
= mydb
.get_rows(FROM
='vms', WHERE
={'flavor_id':flavor
} )
1796 logger
.debug("Flavor '%s' not deleted because it is being used by another VNF", flavor
)
1798 #flavor not used, must be deleted
1800 c
= mydb
.get_rows(FROM
='datacenters_flavors', WHERE
={'flavor_id': flavor
})
1801 for flavor_vim
in c
:
1802 if not flavor_vim
['created']: # skip this flavor because not created by openmano
1806 for vim
in vims
.values():
1807 if vim
["config"]["datacenter_tenant_id"] == flavor_vim
["datacenter_vim_id"]:
1813 myvim
.delete_flavor(flavor_vim
["vim_id"])
1814 except vimconn
.vimconnNotFoundException
:
1815 logger
.warn("VIM flavor %s not exist at datacenter %s", flavor_vim
["vim_id"],
1816 flavor_vim
["datacenter_vim_id"] )
1817 except vimconn
.vimconnException
as e
:
1818 logger
.error("Not possible to delete VIM flavor %s from datacenter %s: %s %s",
1819 flavor_vim
["vim_id"], flavor_vim
["datacenter_vim_id"], type(e
).__name
__, str(e
))
1820 undeletedItems
.append("flavor {} from VIM {}".format(flavor_vim
["vim_id"],
1821 flavor_vim
["datacenter_vim_id"]))
1822 # delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
1823 mydb
.delete_row_by_id('flavors', flavor
)
1824 except db_base_Exception
as e
:
1825 logger
.error("delete_vnf_error. Not possible to get flavor details and delete '%s'. %s", flavor
, str(e
))
1826 undeletedItems
.append("flavor {}".format(flavor
))
1829 for image
in imageList
:
1831 #check if image is used by other vnf
1832 c
= mydb
.get_rows(FROM
='vms', WHERE
=[{'image_id': image
}, {'image_list LIKE ': '%' + image
+ '%'}])
1834 logger
.debug("Image '%s' not deleted because it is being used by another VNF", image
)
1836 #image not used, must be deleted
1838 c
= mydb
.get_rows(FROM
='datacenters_images', WHERE
={'image_id':image
})
1840 if image_vim
["datacenter_vim_id"] not in vims
: # TODO change to datacenter_tenant_id
1842 if image_vim
['created']=='false': #skip this image because not created by openmano
1844 myvim
=vims
[ image_vim
["datacenter_id"] ]
1846 myvim
.delete_image(image_vim
["vim_id"])
1847 except vimconn
.vimconnNotFoundException
as e
:
1848 logger
.warn("VIM image %s not exist at datacenter %s", image_vim
["vim_id"], image_vim
["datacenter_id"] )
1849 except vimconn
.vimconnException
as e
:
1850 logger
.error("Not possible to delete VIM image %s from datacenter %s: %s %s",
1851 image_vim
["vim_id"], image_vim
["datacenter_id"], type(e
).__name
__, str(e
))
1852 undeletedItems
.append("image {} from VIM {}".format(image_vim
["vim_id"], image_vim
["datacenter_id"] ))
1853 #delete image from Database, using table images and with cascade foreign key also at datacenters_images
1854 mydb
.delete_row_by_id('images', image
)
1855 except db_base_Exception
as e
:
1856 logger
.error("delete_vnf_error. Not possible to get image details and delete '%s'. %s", image
, str(e
))
1857 undeletedItems
.append("image {}".format(image
))
1859 return vnf_id
+ " " + vnf
["name"]
1861 # return "delete_vnf. Undeleted: %s" %(undeletedItems)
1864 @deprecated("Not used")
1865 def get_hosts_info(mydb
, nfvo_tenant_id
, datacenter_name
=None):
1866 result
, vims
= get_vim(mydb
, nfvo_tenant_id
, None, datacenter_name
)
1870 return -httperrors
.Not_Found
, "datacenter '{}' not found".format(datacenter_name
)
1871 myvim
= next(iter(vims
.values()))
1872 result
,servers
= myvim
.get_hosts_info()
1874 return result
, servers
1875 topology
= {'name':myvim
['name'] , 'servers': servers
}
1876 return result
, topology
1879 def get_hosts(mydb
, nfvo_tenant_id
):
1880 vims
= get_vim(mydb
, nfvo_tenant_id
)
1882 raise NfvoException("No datacenter found for tenant '{}'".format(str(nfvo_tenant_id
)), httperrors
.Not_Found
)
1884 #print "nfvo.datacenter_action() error. Several datacenters found"
1885 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
1886 myvim
= next(iter(vims
.values()))
1888 hosts
= myvim
.get_hosts()
1889 logger
.debug('VIM hosts response: '+ yaml
.safe_dump(hosts
, indent
=4, default_flow_style
=False))
1891 datacenter
= {'Datacenters': [ {'name':myvim
['name'],'servers':[]} ] }
1893 server
={'name':host
['name'], 'vms':[]}
1894 for vm
in host
['instances']:
1895 #get internal name and model
1897 c
= mydb
.get_rows(SELECT
=('name',), FROM
='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
1898 WHERE
={'vim_vm_id':vm
['id']} )
1900 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' not found at tidnfvo".format(vm
['id']))
1902 server
['vms'].append( {'name':vm
['name'] , 'model':c
[0]['name']} )
1904 except db_base_Exception
as e
:
1905 logger
.warn("nfvo.get_hosts virtual machine at VIM '{}' error {}".format(vm
['id'], str(e
)))
1906 datacenter
['Datacenters'][0]['servers'].append(server
)
1907 #return -400, "en construccion"
1909 #print 'datacenters '+ json.dumps(datacenter, indent=4)
1911 except vimconn
.vimconnException
as e
:
1912 raise NfvoException("Not possible to get_host_list from VIM: {}".format(str(e
)), e
.http_code
)
1915 @deprecated("Use new_nsd_v3")
1916 def new_scenario(mydb
, tenant_id
, topo
):
1918 # result, vims = get_vim(mydb, tenant_id)
1920 # return result, vims
1922 if tenant_id
!= "any":
1923 check_tenant(mydb
, tenant_id
)
1924 if "tenant_id" in topo
:
1925 if topo
["tenant_id"] != tenant_id
:
1926 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(topo
["tenant_id"], tenant_id
),
1927 httperrors
.Unauthorized
)
1931 #1.1: get VNFs and external_networks (other_nets).
1933 other_nets
={} #external_networks, bridge_networks and data_networkds
1934 nodes
= topo
['topology']['nodes']
1935 for k
in nodes
.keys():
1936 if nodes
[k
]['type'] == 'VNF':
1938 vnfs
[k
]['ifaces'] = {}
1939 elif nodes
[k
]['type'] == 'other_network' or nodes
[k
]['type'] == 'external_network':
1940 other_nets
[k
] = nodes
[k
]
1941 other_nets
[k
]['external']=True
1942 elif nodes
[k
]['type'] == 'network':
1943 other_nets
[k
] = nodes
[k
]
1944 other_nets
[k
]['external']=False
1947 #1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
1948 for name
,vnf
in vnfs
.items():
1949 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
1951 error_pos
= "'topology':'nodes':'" + name
+ "'"
1953 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
1954 where
['uuid'] = vnf
['vnf_id']
1955 if 'VNF model' in vnf
:
1956 error_text
+= " 'VNF model' " + vnf
['VNF model']
1957 where
['name'] = vnf
['VNF model']
1959 raise NfvoException("Descriptor need a 'vnf_id' or 'VNF model' field at " + error_pos
, httperrors
.Bad_Request
)
1961 vnf_db
= mydb
.get_rows(SELECT
=('uuid','name','description'),
1965 raise NfvoException("unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
1967 raise NfvoException("more than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
1968 vnf
['uuid']=vnf_db
[0]['uuid']
1969 vnf
['description']=vnf_db
[0]['description']
1970 #get external interfaces
1971 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
1972 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
1973 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
1974 for ext_iface
in ext_ifaces
:
1975 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type':ext_iface
['type']}
1977 #1.4 get list of connections
1978 conections
= topo
['topology']['connections']
1979 conections_list
= []
1980 conections_list_name
= []
1981 for k
in conections
.keys():
1982 if type(conections
[k
]['nodes'])==dict: #dict with node:iface pairs
1983 ifaces_list
= conections
[k
]['nodes'].items()
1984 elif type(conections
[k
]['nodes'])==list: #list with dictionary
1986 conection_pair_list
= map(lambda x
: x
.items(), conections
[k
]['nodes'] )
1987 for k2
in conection_pair_list
:
1990 con_type
= conections
[k
].get("type", "link")
1991 if con_type
!= "link":
1993 raise NfvoException("Format error. Reapeted network name at 'topology':'connections':'{}'".format(str(k
)), httperrors
.Bad_Request
)
1994 other_nets
[k
] = {'external': False}
1995 if conections
[k
].get("graph"):
1996 other_nets
[k
]["graph"] = conections
[k
]["graph"]
1997 ifaces_list
.append( (k
, None) )
2000 if con_type
== "external_network":
2001 other_nets
[k
]['external'] = True
2002 if conections
[k
].get("model"):
2003 other_nets
[k
]["model"] = conections
[k
]["model"]
2005 other_nets
[k
]["model"] = k
2006 if con_type
== "dataplane_net" or con_type
== "bridge_net":
2007 other_nets
[k
]["model"] = con_type
2009 conections_list_name
.append(k
)
2010 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)
2011 #print set(ifaces_list)
2012 #check valid VNF and iface names
2013 for iface
in ifaces_list
:
2014 if iface
[0] not in vnfs
and iface
[0] not in other_nets
:
2015 raise NfvoException("format error. Invalid VNF name at 'topology':'connections':'{}':'nodes':'{}'".format(
2016 str(k
), iface
[0]), httperrors
.Not_Found
)
2017 if iface
[0] in vnfs
and iface
[1] not in vnfs
[ iface
[0] ]['ifaces']:
2018 raise NfvoException("format error. Invalid interface name at 'topology':'connections':'{}':'nodes':'{}':'{}'".format(
2019 str(k
), iface
[0], iface
[1]), httperrors
.Not_Found
)
2021 #1.5 unify connections from the pair list to a consolidated list
2023 while index
< len(conections_list
):
2025 while index2
< len(conections_list
):
2026 if len(conections_list
[index
] & conections_list
[index2
])>0: #common interface, join nets
2027 conections_list
[index
] |
= conections_list
[index2
]
2028 del conections_list
[index2
]
2029 del conections_list_name
[index2
]
2032 conections_list
[index
] = list(conections_list
[index
]) # from set to list again
2034 #for k in conections_list:
2039 #1.6 Delete non external nets
2040 # for k in other_nets.keys():
2041 # if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
2042 # for con in conections_list:
2044 # for index in range(0,len(con)):
2045 # if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
2046 # for index in delete_indexes:
2049 #1.7: Check external_ports are present at database table datacenter_nets
2050 for k
,net
in other_nets
.items():
2051 error_pos
= "'topology':'nodes':'" + k
+ "'"
2052 if net
['external']==False:
2053 if 'name' not in net
:
2055 if 'model' not in net
:
2056 raise NfvoException("needed a 'model' at " + error_pos
, httperrors
.Bad_Request
)
2057 if net
['model']=='bridge_net':
2058 net
['type']='bridge';
2059 elif net
['model']=='dataplane_net':
2062 raise NfvoException("unknown 'model' '"+ net
['model'] +"' at " + error_pos
, httperrors
.Not_Found
)
2064 #IF we do not want to check that external network exist at datacenter
2069 # if 'net_id' in net:
2070 # error_text += " 'net_id' " + net['net_id']
2071 # WHERE_['uuid'] = net['net_id']
2072 # if 'model' in net:
2073 # error_text += " 'model' " + net['model']
2074 # WHERE_['name'] = net['model']
2075 # if len(WHERE_) == 0:
2076 # return -httperrors.Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
2077 # r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
2078 # FROM='datacenter_nets', WHERE=WHERE_ )
2080 # print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
2082 # print "nfvo.new_scenario Error" +error_text+ " is not present at database"
2083 # return -httperrors.Bad_Request, "unknown " +error_text+ " at " + error_pos
2085 # print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database"
2086 # return -httperrors.Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'"
2087 # other_nets[k].update(net_db[0])
2090 net_nb
=0 #Number of nets
2091 for con
in conections_list
:
2092 #check if this is connected to a external net
2096 for index
in range(0,len(con
)):
2097 #check if this is connected to a external net
2098 for net_key
in other_nets
.keys():
2099 if con
[index
][0]==net_key
:
2100 if other_net_index
>=0:
2101 error_text
= "There is some interface connected both to net '{}' and net '{}'".format(
2102 con
[other_net_index
][0], net_key
)
2103 #print "nfvo.new_scenario " + error_text
2104 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2106 other_net_index
= index
2107 net_target
= net_key
2109 #print "other_net_index", other_net_index
2111 if other_net_index
>=0:
2112 del con
[other_net_index
]
2113 #IF we do not want to check that external network exist at datacenter
2114 if other_nets
[net_target
]['external'] :
2115 if "name" not in other_nets
[net_target
]:
2116 other_nets
[net_target
]['name'] = other_nets
[net_target
]['model']
2117 if other_nets
[net_target
]["type"] == "external_network":
2118 if vnfs
[ con
[0][0] ]['ifaces'][ con
[0][1] ]["type"] == "data":
2119 other_nets
[net_target
]["type"] = "data"
2121 other_nets
[net_target
]["type"] = "bridge"
2123 # if other_nets[net_target]['external'] :
2124 # 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
2125 # if type_=='data' and other_nets[net_target]['type']=="ptp":
2126 # error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
2127 # print "nfvo.new_scenario " + error_text
2128 # return -httperrors.Bad_Request, error_text
2131 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2134 net_type_bridge
=False
2136 net_target
= "__-__net"+str(net_nb
)
2137 net_list
[net_target
] = {'name': conections_list_name
[net_nb
], #"net-"+str(net_nb),
2138 'description':"net-{} in scenario {}".format(net_nb
,topo
['name']),
2141 vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['net_key'] = net_target
2142 iface_type
= vnfs
[ iface
[0] ]['ifaces'][ iface
[1] ]['type']
2143 if iface_type
=='mgmt' or iface_type
=='bridge':
2144 net_type_bridge
= True
2146 net_type_data
= True
2147 if net_type_bridge
and net_type_data
:
2148 error_text
= "Error connection interfaces of bridge type with data type. Firs node {}, iface {}".format(iface
[0], iface
[1])
2149 #print "nfvo.new_scenario " + error_text
2150 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2151 elif net_type_bridge
:
2154 type_
='data' if len(con
)>2 else 'ptp'
2155 net_list
[net_target
]['type'] = type_
2158 error_text
= "Error connection node {} : {} does not match any VNF or interface".format(iface
[0], iface
[1])
2159 #print "nfvo.new_scenario " + error_text
2161 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2163 #1.8: Connect to management net all not already connected interfaces of type 'mgmt'
2164 #1.8.1 obtain management net
2165 mgmt_net
= mydb
.get_rows(SELECT
=('uuid','name','description','type','shared'),
2166 FROM
='datacenter_nets', WHERE
={'name':'mgmt'} )
2167 #1.8.2 check all interfaces from all vnfs
2169 add_mgmt_net
= False
2170 for vnf
in vnfs
.values():
2171 for iface
in vnf
['ifaces'].values():
2172 if iface
['type']=='mgmt' and 'net_key' not in iface
:
2173 #iface not connected
2174 iface
['net_key'] = 'mgmt'
2176 if add_mgmt_net
and 'mgmt' not in net_list
:
2177 net_list
['mgmt']=mgmt_net
[0]
2178 net_list
['mgmt']['external']=True
2179 net_list
['mgmt']['graph']={'visible':False}
2181 net_list
.update(other_nets
)
2183 #print 'net_list', net_list
2188 #2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
2189 c
= mydb
.new_scenario( { 'vnfs':vnfs
, 'nets':net_list
,
2190 'tenant_id':tenant_id
, 'name':topo
['name'],
2191 'description':topo
.get('description',topo
['name']),
2192 'public': topo
.get('public', False)
2198 @deprecated("Use new_nsd_v3")
2199 def new_scenario_v02(mydb
, tenant_id
, scenario_dict
, version
):
2200 """ This creates a new scenario for version 0.2 and 0.3"""
2201 scenario
= scenario_dict
["scenario"]
2202 if tenant_id
!= "any":
2203 check_tenant(mydb
, tenant_id
)
2204 if "tenant_id" in scenario
:
2205 if scenario
["tenant_id"] != tenant_id
:
2206 # print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
2207 raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
2208 scenario
["tenant_id"], tenant_id
), httperrors
.Unauthorized
)
2212 # 1: Check that VNF are present at database table vnfs and update content into scenario dict
2213 for name
,vnf
in scenario
["vnfs"].items():
2214 where
= {"OR": {"tenant_id": tenant_id
, 'public': "true"}}
2216 error_pos
= "'scenario':'vnfs':'" + name
+ "'"
2218 error_text
+= " 'vnf_id' " + vnf
['vnf_id']
2219 where
['uuid'] = vnf
['vnf_id']
2220 if 'vnf_name' in vnf
:
2221 error_text
+= " 'vnf_name' " + vnf
['vnf_name']
2222 where
['name'] = vnf
['vnf_name']
2224 raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos
, httperrors
.Bad_Request
)
2225 vnf_db
= mydb
.get_rows(SELECT
=('uuid', 'name', 'description'),
2228 if len(vnf_db
) == 0:
2229 raise NfvoException("Unknown" + error_text
+ " at " + error_pos
, httperrors
.Not_Found
)
2230 elif len(vnf_db
) > 1:
2231 raise NfvoException("More than one" + error_text
+ " at " + error_pos
+ " Concrete with 'vnf_id'", httperrors
.Conflict
)
2232 vnf
['uuid'] = vnf_db
[0]['uuid']
2233 vnf
['description'] = vnf_db
[0]['description']
2235 # get external interfaces
2236 ext_ifaces
= mydb
.get_rows(SELECT
=('external_name as name', 'i.uuid as iface_uuid', 'i.type as type'),
2237 FROM
='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
2238 WHERE
={'vnfs.uuid':vnf
['uuid'], 'external_name<>': None} )
2239 for ext_iface
in ext_ifaces
:
2240 vnf
['ifaces'][ ext_iface
['name'] ] = {'uuid':ext_iface
['iface_uuid'], 'type': ext_iface
['type']}
2241 # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
2243 # 2: Insert net_key and ip_address at every vnf interface
2244 for net_name
, net
in scenario
["networks"].items():
2245 net_type_bridge
= False
2246 net_type_data
= False
2247 for iface_dict
in net
["interfaces"]:
2248 if version
== "0.2":
2249 temp_dict
= iface_dict
2251 elif version
== "0.3":
2252 temp_dict
= {iface_dict
["vnf"] : iface_dict
["vnf_interface"]}
2253 ip_address
= iface_dict
.get('ip_address', None)
2254 for vnf
, iface
in temp_dict
.items():
2255 if vnf
not in scenario
["vnfs"]:
2256 error_text
= "Error at 'networks':'{}':'interfaces' VNF '{}' not match any VNF at 'vnfs'".format(
2258 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2259 raise NfvoException(error_text
, httperrors
.Not_Found
)
2260 if iface
not in scenario
["vnfs"][vnf
]['ifaces']:
2261 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface not match any VNF interface"\
2262 .format(net_name
, iface
)
2263 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2264 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2265 if "net_key" in scenario
["vnfs"][vnf
]['ifaces'][iface
]:
2266 error_text
= "Error at 'networks':'{}':'interfaces':'{}' interface already connected at network"\
2267 "'{}'".format(net_name
, iface
,scenario
["vnfs"][vnf
]['ifaces'][iface
]['net_key'])
2268 # logger.debug("nfvo.new_scenario_v02 " + error_text)
2269 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2270 scenario
["vnfs"][vnf
]['ifaces'][ iface
]['net_key'] = net_name
2271 scenario
["vnfs"][vnf
]['ifaces'][iface
]['ip_address'] = ip_address
2272 iface_type
= scenario
["vnfs"][vnf
]['ifaces'][iface
]['type']
2273 if iface_type
== 'mgmt' or iface_type
== 'bridge':
2274 net_type_bridge
= True
2276 net_type_data
= True
2278 if net_type_bridge
and net_type_data
:
2279 error_text
= "Error connection interfaces of 'bridge' type and 'data' type at 'networks':'{}':'interfaces'"\
2281 # logger.debug("nfvo.new_scenario " + error_text)
2282 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2283 elif net_type_bridge
:
2286 type_
= 'data' if len(net
["interfaces"]) > 2 else 'ptp'
2288 if net
.get("implementation"): # for v0.3
2289 if type_
== "bridge" and net
["implementation"] == "underlay":
2290 error_text
= "Error connecting interfaces of data type to a network declared as 'underlay' at "\
2291 "'network':'{}'".format(net_name
)
2292 # logger.debug(error_text)
2293 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2294 elif type_
!= "bridge" and net
["implementation"] == "overlay":
2295 error_text
= "Error connecting interfaces of data type to a network declared as 'overlay' at "\
2296 "'network':'{}'".format(net_name
)
2297 # logger.debug(error_text)
2298 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2299 net
.pop("implementation")
2300 if "type" in net
and version
== "0.3": # for v0.3
2301 if type_
== "data" and net
["type"] == "e-line":
2302 error_text
= "Error connecting more than 2 interfaces of data type to a network declared as type "\
2303 "'e-line' at 'network':'{}'".format(net_name
)
2304 # logger.debug(error_text)
2305 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2306 elif type_
== "ptp" and net
["type"] == "e-lan":
2310 net
['name'] = net_name
2311 net
['external'] = net
.get('external', False)
2313 # 3: insert at database
2314 scenario
["nets"] = scenario
["networks"]
2315 scenario
['tenant_id'] = tenant_id
2316 scenario_id
= mydb
.new_scenario(scenario
)
2320 def new_nsd_v3(mydb
, tenant_id
, nsd_descriptor
):
2322 Parses an OSM IM nsd_catalog and insert at DB
2325 :param nsd_descriptor:
2326 :return: The list of created NSD ids
2329 mynsd
= nsd_catalog
.nsd()
2331 pybindJSONDecoder
.load_ietf_json(nsd_descriptor
, None, None, obj
=mynsd
, skip_unknown
=True)
2332 except Exception as e
:
2333 raise NfvoException("Error. Invalid NS descriptor format: " + str(e
), httperrors
.Bad_Request
)
2337 db_sce_interfaces
= []
2340 db_sce_rsp_hops
= []
2341 db_sce_classifiers
= []
2342 db_sce_classifier_matches
= []
2344 db_ip_profiles_index
= 0
2347 for nsd_yang
in mynsd
.nsd_catalog
.nsd
.values():
2348 nsd
= nsd_yang
.get()
2351 scenario_uuid
= str(uuid4())
2352 uuid_list
.append(scenario_uuid
)
2353 nsd_uuid_list
.append(scenario_uuid
)
2355 "uuid": scenario_uuid
,
2356 "osm_id": get_str(nsd
, "id", 255),
2357 "name": get_str(nsd
, "name", 255),
2358 "description": get_str(nsd
, "description", 255),
2359 "tenant_id": tenant_id
,
2360 "vendor": get_str(nsd
, "vendor", 255),
2361 "short_name": get_str(nsd
, "short-name", 255),
2362 "descriptor": str(nsd_descriptor
)[:60000],
2364 db_scenarios
.append(db_scenario
)
2366 # table sce_vnfs (constituent-vnfd)
2367 vnf_index2scevnf_uuid
= {}
2368 vnf_index2vnf_uuid
= {}
2369 for vnf
in nsd
.get("constituent-vnfd").values():
2370 existing_vnf
= mydb
.get_rows(FROM
="vnfs", WHERE
={'osm_id': str(vnf
["vnfd-id-ref"])[:255],
2371 'tenant_id': tenant_id
})
2372 if not existing_vnf
:
2373 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'constituent-vnfd':'vnfd-id-ref':"
2374 "'{}'. Reference to a non-existing VNFD in the catalog".format(
2375 str(nsd
["id"]), str(vnf
["vnfd-id-ref"])[:255]),
2376 httperrors
.Bad_Request
)
2377 sce_vnf_uuid
= str(uuid4())
2378 uuid_list
.append(sce_vnf_uuid
)
2380 "uuid": sce_vnf_uuid
,
2381 "scenario_id": scenario_uuid
,
2382 # "name": get_str(vnf, "member-vnf-index", 255),
2383 "name": existing_vnf
[0]["name"][:200] + "." + get_str(vnf
, "member-vnf-index", 50),
2384 "vnf_id": existing_vnf
[0]["uuid"],
2385 "member_vnf_index": str(vnf
["member-vnf-index"]),
2386 # TODO 'start-by-default': True
2388 vnf_index2scevnf_uuid
[str(vnf
['member-vnf-index'])] = sce_vnf_uuid
2389 vnf_index2vnf_uuid
[str(vnf
['member-vnf-index'])] = existing_vnf
[0]["uuid"]
2390 db_sce_vnfs
.append(db_sce_vnf
)
2392 # table ip_profiles (ip-profiles)
2393 ip_profile_name2db_table_index
= {}
2394 for ip_profile
in nsd
.get("ip-profiles").values():
2396 "ip_version": str(ip_profile
["ip-profile-params"].get("ip-version", "ipv4")),
2397 "subnet_address": str(ip_profile
["ip-profile-params"].get("subnet-address")),
2398 "gateway_address": str(ip_profile
["ip-profile-params"].get("gateway-address")),
2399 "dhcp_enabled": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("enabled", True)),
2400 "dhcp_start_address": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("start-address")),
2401 "dhcp_count": str(ip_profile
["ip-profile-params"]["dhcp-params"].get("count")),
2404 for dns
in ip_profile
["ip-profile-params"]["dns-server"].values():
2405 dns_list
.append(str(dns
.get("address")))
2406 db_ip_profile
["dns_address"] = ";".join(dns_list
)
2407 if ip_profile
["ip-profile-params"].get('security-group'):
2408 db_ip_profile
["security_group"] = ip_profile
["ip-profile-params"]['security-group']
2409 ip_profile_name2db_table_index
[str(ip_profile
["name"])] = db_ip_profiles_index
2410 db_ip_profiles_index
+= 1
2411 db_ip_profiles
.append(db_ip_profile
)
2413 # table sce_nets (internal-vld)
2414 for vld
in nsd
.get("vld").values():
2415 sce_net_uuid
= str(uuid4())
2416 uuid_list
.append(sce_net_uuid
)
2418 "uuid": sce_net_uuid
,
2419 "name": get_str(vld
, "name", 255),
2420 "scenario_id": scenario_uuid
,
2422 "multipoint": not vld
.get("type") == "ELINE",
2423 "osm_id": get_str(vld
, "id", 255),
2425 "description": get_str(vld
, "description", 255),
2427 # guess type of network
2428 if vld
.get("mgmt-network"):
2429 db_sce_net
["type"] = "bridge"
2430 db_sce_net
["external"] = True
2431 elif vld
.get("provider-network").get("overlay-type") == "VLAN":
2432 db_sce_net
["type"] = "data"
2434 # later on it will be fixed to bridge or data depending on the type of interfaces attached to it
2435 db_sce_net
["type"] = None
2436 db_sce_nets
.append(db_sce_net
)
2438 # ip-profile, link db_ip_profile with db_sce_net
2439 if vld
.get("ip-profile-ref"):
2440 ip_profile_name
= vld
.get("ip-profile-ref")
2441 if ip_profile_name
not in ip_profile_name2db_table_index
:
2442 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'ip-profile-ref':'{}'."
2443 " Reference to a non-existing 'ip_profiles'".format(
2444 str(nsd
["id"]), str(vld
["id"]), str(vld
["ip-profile-ref"])),
2445 httperrors
.Bad_Request
)
2446 db_ip_profiles
[ip_profile_name2db_table_index
[ip_profile_name
]]["sce_net_id"] = sce_net_uuid
2447 elif vld
.get("vim-network-name"):
2448 db_sce_net
["vim_network_name"] = get_str(vld
, "vim-network-name", 255)
2450 # table sce_interfaces (vld:vnfd-connection-point-ref)
2451 for iface
in vld
.get("vnfd-connection-point-ref").values():
2452 # Check if there are VDUs in the descriptor
2453 vnf_index
= str(iface
['member-vnf-index-ref'])
2454 existing_vdus
= mydb
.get_rows(SELECT
=('vms.uuid'), FROM
="vms", WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
]})
2456 # check correct parameters
2457 if vnf_index
not in vnf_index2vnf_uuid
:
2458 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2459 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2460 "'nsd':'constituent-vnfd'".format(
2461 str(nsd
["id"]), str(vld
["id"]), str(iface
["member-vnf-index-ref"])),
2462 httperrors
.Bad_Request
)
2464 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid', 'i.type as iface_type'),
2465 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2466 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2467 'external_name': get_str(iface
, "vnfd-connection-point-ref",
2469 if not existing_ifaces
:
2470 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'vld[{}]':'vnfd-connection-point"
2471 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2472 "connection-point name at VNFD '{}'".format(
2473 str(nsd
["id"]), str(vld
["id"]), str(iface
["vnfd-connection-point-ref"]),
2474 str(iface
.get("vnfd-id-ref"))[:255]),
2475 httperrors
.Bad_Request
)
2476 interface_uuid
= existing_ifaces
[0]["uuid"]
2477 if existing_ifaces
[0]["iface_type"] == "data":
2478 db_sce_net
["type"] = "data"
2479 sce_interface_uuid
= str(uuid4())
2480 uuid_list
.append(sce_net_uuid
)
2481 iface_ip_address
= None
2482 if iface
.get("ip-address"):
2483 iface_ip_address
= str(iface
.get("ip-address"))
2484 db_sce_interface
= {
2485 "uuid": sce_interface_uuid
,
2486 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2487 "sce_net_id": sce_net_uuid
,
2488 "interface_id": interface_uuid
,
2489 "ip_address": iface_ip_address
,
2491 db_sce_interfaces
.append(db_sce_interface
)
2492 if not db_sce_net
["type"]:
2493 db_sce_net
["type"] = "bridge"
2495 # table sce_vnffgs (vnffgd)
2496 for vnffg
in nsd
.get("vnffgd").values():
2497 sce_vnffg_uuid
= str(uuid4())
2498 uuid_list
.append(sce_vnffg_uuid
)
2500 "uuid": sce_vnffg_uuid
,
2501 "name": get_str(vnffg
, "name", 255),
2502 "scenario_id": scenario_uuid
,
2503 "vendor": get_str(vnffg
, "vendor", 255),
2504 "description": get_str(vld
, "description", 255),
2506 db_sce_vnffgs
.append(db_sce_vnffg
)
2509 for rsp
in vnffg
.get("rsp").values():
2510 sce_rsp_uuid
= str(uuid4())
2511 uuid_list
.append(sce_rsp_uuid
)
2513 "uuid": sce_rsp_uuid
,
2514 "name": get_str(rsp
, "name", 255),
2515 "sce_vnffg_id": sce_vnffg_uuid
,
2516 "id": get_str(rsp
, "id", 255), # only useful to link with classifiers; will be removed later in the code
2518 db_sce_rsps
.append(db_sce_rsp
)
2519 for iface
in rsp
.get("vnfd-connection-point-ref").values():
2520 vnf_index
= str(iface
['member-vnf-index-ref'])
2521 if_order
= int(iface
['order'])
2522 # check correct parameters
2523 if vnf_index
not in vnf_index2vnf_uuid
:
2524 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2525 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2526 "'nsd':'constituent-vnfd'".format(
2527 str(nsd
["id"]), str(rsp
["id"]), str(iface
["member-vnf-index-ref"])),
2528 httperrors
.Bad_Request
)
2530 ingress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2531 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2533 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2534 'external_name': get_str(iface
, "vnfd-ingress-connection-point-ref",
2536 if not ingress_existing_ifaces
:
2537 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2538 "-ref':'vnfd-ingress-connection-point-ref':'{}'. Reference to a non-existing "
2539 "connection-point name at VNFD '{}'".format(
2540 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-ingress-connection-point-ref"]),
2541 str(iface
.get("vnfd-id-ref"))[:255]), httperrors
.Bad_Request
)
2543 egress_existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2544 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2546 'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2547 'external_name': get_str(iface
, "vnfd-egress-connection-point-ref",
2549 if not egress_existing_ifaces
:
2550 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2551 "-ref':'vnfd-egress-connection-point-ref':'{}'. Reference to a non-existing "
2552 "connection-point name at VNFD '{}'".format(
2553 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-egress-connection-point-ref"]),
2554 str(iface
.get("vnfd-id-ref"))[:255]), HTTP_Bad_Request
)
2556 ingress_interface_uuid
= ingress_existing_ifaces
[0]["uuid"]
2557 egress_interface_uuid
= egress_existing_ifaces
[0]["uuid"]
2558 sce_rsp_hop_uuid
= str(uuid4())
2559 uuid_list
.append(sce_rsp_hop_uuid
)
2561 "uuid": sce_rsp_hop_uuid
,
2562 "if_order": if_order
,
2563 "ingress_interface_id": ingress_interface_uuid
,
2564 "egress_interface_id": egress_interface_uuid
,
2565 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2566 "sce_rsp_id": sce_rsp_uuid
,
2568 db_sce_rsp_hops
.append(db_sce_rsp_hop
)
2570 # deal with classifiers
2571 for classifier
in vnffg
.get("classifier").values():
2572 sce_classifier_uuid
= str(uuid4())
2573 uuid_list
.append(sce_classifier_uuid
)
2576 vnf_index
= str(classifier
['member-vnf-index-ref'])
2577 if vnf_index
not in vnf_index2vnf_uuid
:
2578 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'classifier[{}]':'vnfd-connection-point"
2579 "-ref':'member-vnf-index-ref':'{}'. Reference to a non-existing index at "
2580 "'nsd':'constituent-vnfd'".format(
2581 str(nsd
["id"]), str(classifier
["id"]), str(classifier
["member-vnf-index-ref"])),
2582 httperrors
.Bad_Request
)
2583 existing_ifaces
= mydb
.get_rows(SELECT
=('i.uuid as uuid',),
2584 FROM
="interfaces as i join vms on i.vm_id=vms.uuid",
2585 WHERE
={'vnf_id': vnf_index2vnf_uuid
[vnf_index
],
2586 'external_name': get_str(classifier
, "vnfd-connection-point-ref",
2588 if not existing_ifaces
:
2589 raise NfvoException("Error. Invalid NS descriptor at 'nsd[{}]':'rsp[{}]':'vnfd-connection-point"
2590 "-ref':'vnfd-connection-point-ref':'{}'. Reference to a non-existing "
2591 "connection-point name at VNFD '{}'".format(
2592 str(nsd
["id"]), str(rsp
["id"]), str(iface
["vnfd-connection-point-ref"]),
2593 str(iface
.get("vnfd-id-ref"))[:255]),
2594 httperrors
.Bad_Request
)
2595 interface_uuid
= existing_ifaces
[0]["uuid"]
2597 db_sce_classifier
= {
2598 "uuid": sce_classifier_uuid
,
2599 "name": get_str(classifier
, "name", 255),
2600 "sce_vnffg_id": sce_vnffg_uuid
,
2601 "sce_vnf_id": vnf_index2scevnf_uuid
[vnf_index
],
2602 "interface_id": interface_uuid
,
2604 rsp_id
= get_str(classifier
, "rsp-id-ref", 255)
2605 rsp
= next((item
for item
in db_sce_rsps
if item
["id"] == rsp_id
), None)
2606 db_sce_classifier
["sce_rsp_id"] = rsp
["uuid"]
2607 db_sce_classifiers
.append(db_sce_classifier
)
2609 for match
in classifier
.get("match-attributes").values():
2610 sce_classifier_match_uuid
= str(uuid4())
2611 uuid_list
.append(sce_classifier_match_uuid
)
2612 db_sce_classifier_match
= {
2613 "uuid": sce_classifier_match_uuid
,
2614 "ip_proto": get_str(match
, "ip-proto", 2),
2615 "source_ip": get_str(match
, "source-ip-address", 16),
2616 "destination_ip": get_str(match
, "destination-ip-address", 16),
2617 "source_port": get_str(match
, "source-port", 5),
2618 "destination_port": get_str(match
, "destination-port", 5),
2619 "sce_classifier_id": sce_classifier_uuid
,
2621 db_sce_classifier_matches
.append(db_sce_classifier_match
)
2624 # remove unneeded id's in sce_rsps
2625 for rsp
in db_sce_rsps
:
2629 {"scenarios": db_scenarios
},
2630 {"sce_nets": db_sce_nets
},
2631 {"ip_profiles": db_ip_profiles
},
2632 {"sce_vnfs": db_sce_vnfs
},
2633 {"sce_interfaces": db_sce_interfaces
},
2634 {"sce_vnffgs": db_sce_vnffgs
},
2635 {"sce_rsps": db_sce_rsps
},
2636 {"sce_rsp_hops": db_sce_rsp_hops
},
2637 {"sce_classifiers": db_sce_classifiers
},
2638 {"sce_classifier_matches": db_sce_classifier_matches
},
2641 logger
.debug("new_nsd_v3 done: %s",
2642 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
2643 mydb
.new_rows(db_tables
, uuid_list
)
2644 return nsd_uuid_list
2645 except NfvoException
:
2647 except Exception as e
:
2648 logger
.error("Exception {}".format(e
))
2649 raise # NfvoException("Exception {}".format(e), httperrors.Bad_Request)
2652 def edit_scenario(mydb
, tenant_id
, scenario_id
, data
):
2653 data
["uuid"] = scenario_id
2654 data
["tenant_id"] = tenant_id
2655 c
= mydb
.edit_scenario( data
)
2659 @deprecated("Use create_instance")
2660 def start_scenario(mydb
, tenant_id
, scenario_id
, instance_scenario_name
, instance_scenario_description
, datacenter
=None,vim_tenant
=None, startvms
=True):
2661 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
2662 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
, vim_tenant
=vim_tenant
)
2663 vims
= {datacenter_id
: myvim
}
2664 myvim_tenant
= myvim
['tenant_id']
2665 datacenter_name
= myvim
['name']
2669 #print "Checking that the scenario_id exists and getting the scenario dictionary"
2670 scenarioDict
= mydb
.get_scenario(scenario_id
, tenant_id
, datacenter_id
=datacenter_id
)
2671 scenarioDict
['datacenter2tenant'] = { datacenter_id
: myvim
['config']['datacenter_tenant_id'] }
2672 scenarioDict
['datacenter_id'] = datacenter_id
2673 #print '================scenarioDict======================='
2674 #print json.dumps(scenarioDict, indent=4)
2675 #print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
2677 logger
.debug("start_scenario Scenario %s: consisting of %d VNF(s)", scenarioDict
['name'],len(scenarioDict
['vnfs']))
2678 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2680 auxNetDict
= {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
2681 auxNetDict
['scenario'] = {}
2683 logger
.debug("start_scenario 1. Creating new nets (sce_nets) in the VIM")
2684 for sce_net
in scenarioDict
['nets']:
2685 #print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
2687 myNetName
= "{}.{}".format(instance_scenario_name
, sce_net
['name'])
2688 myNetName
= myNetName
[0:255] #limit length
2689 myNetType
= sce_net
['type']
2691 myNetDict
["name"] = myNetName
2692 myNetDict
["type"] = myNetType
2693 myNetDict
["tenant_id"] = myvim_tenant
2694 myNetIPProfile
= sce_net
.get('ip_profile', None)
2695 myProviderNetwork
= sce_net
.get('provider_network', None)
2697 #We should use the dictionary as input parameter for new_network
2699 if not sce_net
["external"]:
2700 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
, provider_network_profile
=myProviderNetwork
)
2701 #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
2702 sce_net
['vim_id'] = network_id
2703 auxNetDict
['scenario'][sce_net
['uuid']] = network_id
2704 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2705 sce_net
["created"] = True
2707 if sce_net
['vim_id'] == None:
2708 error_text
= "Error, datacenter '{}' does not have external network '{}'.".format(
2709 datacenter_name
, sce_net
['name'])
2710 _
, message
= rollback(mydb
, vims
, rollbackList
)
2711 logger
.error("nfvo.start_scenario: %s", error_text
)
2712 raise NfvoException(error_text
, httperrors
.Bad_Request
)
2713 logger
.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict
['name'],sce_net
['vim_id'])
2714 auxNetDict
['scenario'][sce_net
['uuid']] = sce_net
['vim_id']
2716 logger
.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
2717 #For each vnf net, we create it and we add it to instanceNetlist.
2719 for sce_vnf
in scenarioDict
['vnfs']:
2720 for net
in sce_vnf
['nets']:
2721 #print "Net name: %s. Description: %s" % (net["name"], net["description"])
2723 myNetName
= "{}.{}".format(instance_scenario_name
,net
['name'])
2724 myNetName
= myNetName
[0:255] #limit length
2725 myNetType
= net
['type']
2727 myNetDict
["name"] = myNetName
2728 myNetDict
["type"] = myNetType
2729 myNetDict
["tenant_id"] = myvim_tenant
2730 myNetIPProfile
= net
.get('ip_profile', None)
2731 myProviderNetwork
= sce_net
.get('provider_network', None)
2734 #We should use the dictionary as input parameter for new_network
2735 network_id
, _
= myvim
.new_network(myNetName
, myNetType
, myNetIPProfile
, provider_network_profile
=myProviderNetwork
)
2736 #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
2737 net
['vim_id'] = network_id
2738 if sce_vnf
['uuid'] not in auxNetDict
:
2739 auxNetDict
[sce_vnf
['uuid']] = {}
2740 auxNetDict
[sce_vnf
['uuid']][net
['uuid']] = network_id
2741 rollbackList
.append({'what':'network','where':'vim','vim_id':datacenter_id
,'uuid':network_id
})
2742 net
["created"] = True
2744 #print "auxNetDict:"
2745 #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
2747 logger
.debug("start_scenario 3. Creating new vm instances in the VIM")
2748 #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
2750 for sce_vnf
in scenarioDict
['vnfs']:
2751 vnf_availability_zones
= []
2752 for vm
in sce_vnf
['vms']:
2753 vm_av
= vm
.get('availability_zone')
2754 if vm_av
and vm_av
not in vnf_availability_zones
:
2755 vnf_availability_zones
.append(vm_av
)
2757 # check if there is enough availability zones available at vim level.
2758 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
2759 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
2760 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
2762 for vm
in sce_vnf
['vms']:
2765 #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
2766 myVMDict
['name'] = "{}.{}.{}".format(instance_scenario_name
,sce_vnf
['name'],chr(96+i
))
2767 #myVMDict['description'] = vm['description']
2768 myVMDict
['description'] = myVMDict
['name'][0:99]
2770 myVMDict
['start'] = "no"
2771 myVMDict
['name'] = myVMDict
['name'][0:255] #limit name length
2772 #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
2774 #create image at vim in case it not exist
2775 image_dict
= mydb
.get_table_by_uuid_name("images", vm
['image_id'])
2776 image_id
= create_or_use_image(mydb
, vims
, image_dict
, [], True)
2777 vm
['vim_image_id'] = image_id
2779 #create flavor at vim in case it not exist
2780 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
2781 if flavor_dict
['extended']!=None:
2782 flavor_dict
['extended']= yaml
.load(flavor_dict
['extended'], Loader
=yaml
.Loader
)
2783 flavor_id
= create_or_use_flavor(mydb
, vims
, flavor_dict
, [], True)
2784 vm
['vim_flavor_id'] = flavor_id
2787 myVMDict
['imageRef'] = vm
['vim_image_id']
2788 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
2789 myVMDict
['networks'] = []
2790 for iface
in vm
['interfaces']:
2792 if iface
['type']=="data":
2793 netDict
['type'] = iface
['model']
2794 elif "model" in iface
and iface
["model"]!=None:
2795 netDict
['model']=iface
['model']
2796 #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
2797 #discover type of interface looking at flavor
2798 for numa
in flavor_dict
.get('extended',{}).get('numas',[]):
2799 for flavor_iface
in numa
.get('interfaces',[]):
2800 if flavor_iface
.get('name') == iface
['internal_name']:
2801 if flavor_iface
['dedicated'] == 'yes':
2802 netDict
['type']="PF" #passthrough
2803 elif flavor_iface
['dedicated'] == 'no':
2804 netDict
['type']="VF" #siov
2805 elif flavor_iface
['dedicated'] == 'yes:sriov':
2806 netDict
['type']="VFnotShared" #sriov but only one sriov on the PF
2807 netDict
["mac_address"] = flavor_iface
.get("mac_address")
2809 netDict
["use"]=iface
['type']
2810 if netDict
["use"]=="data" and not netDict
.get("type"):
2811 #print "netDict", netDict
2812 #print "iface", iface
2813 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".format(
2814 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
2815 if flavor_dict
.get('extended')==None:
2816 raise NfvoException(e_text
+ "After database migration some information is not available. \
2817 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
2819 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
2820 if netDict
["use"]=="mgmt" or netDict
["use"]=="bridge":
2821 netDict
["type"]="virtual"
2822 if "vpci" in iface
and iface
["vpci"] is not None:
2823 netDict
['vpci'] = iface
['vpci']
2824 if "mac" in iface
and iface
["mac"] is not None:
2825 netDict
['mac_address'] = iface
['mac']
2826 if "port-security" in iface
and iface
["port-security"] is not None:
2827 netDict
['port_security'] = iface
['port-security']
2828 if "floating-ip" in iface
and iface
["floating-ip"] is not None:
2829 netDict
['floating_ip'] = iface
['floating-ip']
2830 netDict
['name'] = iface
['internal_name']
2831 if iface
['net_id'] is None:
2832 for vnf_iface
in sce_vnf
["interfaces"]:
2835 if vnf_iface
['interface_id']==iface
['uuid']:
2836 netDict
['net_id'] = auxNetDict
['scenario'][ vnf_iface
['sce_net_id'] ]
2839 netDict
['net_id'] = auxNetDict
[ sce_vnf
['uuid'] ][ iface
['net_id'] ]
2840 #skip bridge ifaces not connected to any net
2841 #if 'net_id' not in netDict or netDict['net_id']==None:
2843 myVMDict
['networks'].append(netDict
)
2844 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2845 #print myVMDict['name']
2846 #print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
2847 #print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
2848 #print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
2850 if 'availability_zone' in myVMDict
:
2851 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
2855 vm_id
, _
= myvim
.new_vminstance(myVMDict
['name'], myVMDict
['description'], myVMDict
.get('start', None),
2856 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'],
2857 availability_zone_index
=av_index
,
2858 availability_zone_list
=vnf_availability_zones
)
2859 #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
2860 vm
['vim_id'] = vm_id
2861 rollbackList
.append({'what':'vm','where':'vim','vim_id':datacenter_id
,'uuid':vm_id
})
2862 #put interface uuid back to scenario[vnfs][vms[[interfaces]
2863 for net
in myVMDict
['networks']:
2865 for iface
in vm
['interfaces']:
2866 if net
["name"]==iface
["internal_name"]:
2867 iface
["vim_id"]=net
["vim_id"]
2870 logger
.debug("start scenario Deployment done")
2871 #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
2872 #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
2873 instance_id
= mydb
.new_instance_scenario_as_a_whole(tenant_id
,instance_scenario_name
, instance_scenario_description
, scenarioDict
)
2874 return mydb
.get_instance_scenario(instance_id
)
2876 except (db_base_Exception
, vimconn
.vimconnException
) as e
:
2877 _
, message
= rollback(mydb
, vims
, rollbackList
)
2878 if isinstance(e
, db_base_Exception
):
2879 error_text
= "Exception at database"
2881 error_text
= "Exception at VIM"
2882 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
2883 #logger.error("start_scenario %s", error_text)
2884 raise NfvoException(error_text
, e
.http_code
)
2886 def unify_cloud_config(cloud_config_preserve
, cloud_config
):
2887 """ join the cloud config information into cloud_config_preserve.
2888 In case of conflict cloud_config_preserve preserves
2891 if not cloud_config_preserve
and not cloud_config
:
2894 new_cloud_config
= {"key-pairs":[], "users":[]}
2896 if cloud_config_preserve
:
2897 for key
in cloud_config_preserve
.get("key-pairs", () ):
2898 if key
not in new_cloud_config
["key-pairs"]:
2899 new_cloud_config
["key-pairs"].append(key
)
2901 for key
in cloud_config
.get("key-pairs", () ):
2902 if key
not in new_cloud_config
["key-pairs"]:
2903 new_cloud_config
["key-pairs"].append(key
)
2904 if not new_cloud_config
["key-pairs"]:
2905 del new_cloud_config
["key-pairs"]
2909 new_cloud_config
["users"] += cloud_config
.get("users", () )
2910 if cloud_config_preserve
:
2911 new_cloud_config
["users"] += cloud_config_preserve
.get("users", () )
2912 index_to_delete
= []
2913 users
= new_cloud_config
.get("users", [])
2914 for index0
in range(0,len(users
)):
2915 if index0
in index_to_delete
:
2917 for index1
in range(index0
+1,len(users
)):
2918 if index1
in index_to_delete
:
2920 if users
[index0
]["name"] == users
[index1
]["name"]:
2921 index_to_delete
.append(index1
)
2922 for key
in users
[index1
].get("key-pairs",()):
2923 if "key-pairs" not in users
[index0
]:
2924 users
[index0
]["key-pairs"] = [key
]
2925 elif key
not in users
[index0
]["key-pairs"]:
2926 users
[index0
]["key-pairs"].append(key
)
2927 index_to_delete
.sort(reverse
=True)
2928 for index
in index_to_delete
:
2930 if not new_cloud_config
["users"]:
2931 del new_cloud_config
["users"]
2934 if cloud_config
and cloud_config
.get("boot-data-drive") != None:
2935 new_cloud_config
["boot-data-drive"] = cloud_config
["boot-data-drive"]
2936 if cloud_config_preserve
and cloud_config_preserve
.get("boot-data-drive") != None:
2937 new_cloud_config
["boot-data-drive"] = cloud_config_preserve
["boot-data-drive"]
2940 new_cloud_config
["user-data"] = []
2941 if cloud_config
and cloud_config
.get("user-data"):
2942 if isinstance(cloud_config
["user-data"], list):
2943 new_cloud_config
["user-data"] += cloud_config
["user-data"]
2945 new_cloud_config
["user-data"].append(cloud_config
["user-data"])
2946 if cloud_config_preserve
and cloud_config_preserve
.get("user-data"):
2947 if isinstance(cloud_config_preserve
["user-data"], list):
2948 new_cloud_config
["user-data"] += cloud_config_preserve
["user-data"]
2950 new_cloud_config
["user-data"].append(cloud_config_preserve
["user-data"])
2951 if not new_cloud_config
["user-data"]:
2952 del new_cloud_config
["user-data"]
2955 new_cloud_config
["config-files"] = []
2956 if cloud_config
and cloud_config
.get("config-files") != None:
2957 new_cloud_config
["config-files"] += cloud_config
["config-files"]
2958 if cloud_config_preserve
:
2959 for file in cloud_config_preserve
.get("config-files", ()):
2960 for index
in range(0, len(new_cloud_config
["config-files"])):
2961 if new_cloud_config
["config-files"][index
]["dest"] == file["dest"]:
2962 new_cloud_config
["config-files"][index
] = file
2965 new_cloud_config
["config-files"].append(file)
2966 if not new_cloud_config
["config-files"]:
2967 del new_cloud_config
["config-files"]
2968 return new_cloud_config
2971 def get_vim_thread(mydb
, tenant_id
, datacenter_id_name
=None, datacenter_tenant_id
=None):
2972 datacenter_id
= None
2973 datacenter_name
= None
2976 if datacenter_tenant_id
:
2977 thread_id
= datacenter_tenant_id
2978 thread
= vim_threads
["running"].get(datacenter_tenant_id
)
2980 where_
={"td.nfvo_tenant_id": tenant_id
}
2981 if datacenter_id_name
:
2982 if utils
.check_valid_uuid(datacenter_id_name
):
2983 datacenter_id
= datacenter_id_name
2984 where_
["dt.datacenter_id"] = datacenter_id
2986 datacenter_name
= datacenter_id_name
2987 where_
["d.name"] = datacenter_name
2988 if datacenter_tenant_id
:
2989 where_
["dt.uuid"] = datacenter_tenant_id
2990 datacenters
= mydb
.get_rows(
2991 SELECT
=("dt.uuid as datacenter_tenant_id, d.name as datacenter_name",),
2992 FROM
="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
2993 "join datacenters as d on d.uuid=dt.datacenter_id",
2995 if len(datacenters
) > 1:
2996 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
2998 thread_id
= datacenters
[0]["datacenter_tenant_id"]
2999 datacenter_name
= datacenters
[0]["datacenter_name"]
3000 thread
= vim_threads
["running"].get(thread_id
)
3002 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
)
3003 thread
= vim_thread(task_lock
, plugins
, thread_name
, None,
3006 vim_threads
["running"][thread_id
] = thread
3008 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3009 return thread_id
, thread
3010 except db_base_Exception
as e
:
3011 raise NfvoException("{} {}".format(type(e
).__name
__ , str(e
)), e
.http_code
)
3014 def get_datacenter_uuid(mydb
, tenant_id
, datacenter_id_name
):
3016 if utils
.check_valid_uuid(datacenter_id_name
):
3017 WHERE_dict
['d.uuid'] = datacenter_id_name
3019 WHERE_dict
['d.name'] = datacenter_id_name
3022 WHERE_dict
['nfvo_tenant_id'] = tenant_id
3023 from_
= "tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as" \
3024 " dt on td.datacenter_tenant_id=dt.uuid"
3026 from_
= 'datacenters as d'
3027 vimaccounts
= mydb
.get_rows(FROM
=from_
, SELECT
=("d.uuid as uuid, d.name as name",), WHERE
=WHERE_dict
)
3028 if len(vimaccounts
) == 0:
3029 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3030 elif len(vimaccounts
)>1:
3031 #print "nfvo.datacenter_action() error. Several datacenters found"
3032 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
3033 return vimaccounts
[0]["uuid"], vimaccounts
[0]["name"]
3036 def get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter_id_name
=None, **extra_filter
):
3037 datacenter_id
= None
3038 datacenter_name
= None
3039 if datacenter_id_name
:
3040 if utils
.check_valid_uuid(datacenter_id_name
):
3041 datacenter_id
= datacenter_id_name
3043 datacenter_name
= datacenter_id_name
3044 vims
= get_vim(mydb
, tenant_id
, datacenter_id
, datacenter_name
, **extra_filter
)
3046 raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name
)), httperrors
.Not_Found
)
3048 #print "nfvo.datacenter_action() error. Several datacenters found"
3049 raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors
.Conflict
)
3050 for vim_id
, vim_content
in vims
.items():
3051 return vim_id
, vim_content
3055 """Takes dict d and updates it with the values in dict u.
3056 It merges all depth levels"""
3057 for k
, v
in u
.items():
3058 if isinstance(v
, collections
.Mapping
):
3059 r
= update(d
.get(k
, {}), v
)
3066 def _get_wim(db
, wim_account_id
):
3067 # get wim from wim_account
3068 wim_accounts
= db
.get_rows(FROM
='wim_accounts', WHERE
={"uuid": wim_account_id
})
3069 if not wim_accounts
:
3070 raise NfvoException("Not found sdn id={}".format(wim_account_id
), http_code
=httperrors
.Not_Found
)
3071 return wim_accounts
[0]["wim_id"]
3074 def create_instance(mydb
, tenant_id
, instance_dict
):
3075 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
3076 # logger.debug("Creating instance...")
3078 scenario
= instance_dict
["scenario"]
3080 # find main datacenter
3082 myvim_threads_id
= {}
3083 datacenter
= instance_dict
.get("datacenter")
3084 default_wim_account
= instance_dict
.get("wim_account")
3085 default_datacenter_id
, vim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
3086 myvims
[default_datacenter_id
] = vim
3087 myvim_threads_id
[default_datacenter_id
], _
= get_vim_thread(mydb
, tenant_id
, default_datacenter_id
)
3088 tenant
= mydb
.get_rows_by_id('nfvo_tenants', tenant_id
)
3089 # myvim_tenant = myvim['tenant_id']
3092 # print "Checking that the scenario exists and getting the scenario dictionary"
3093 if isinstance(scenario
, str):
3094 scenarioDict
= mydb
.get_scenario(scenario
, tenant_id
, datacenter_vim_id
=myvim_threads_id
[default_datacenter_id
],
3095 datacenter_id
=default_datacenter_id
)
3097 scenarioDict
= scenario
3098 scenarioDict
["uuid"] = None
3100 # logger.debug(">>>>>> Dictionaries before merging")
3101 # logger.debug(">>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
3102 # logger.debug(">>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
3104 db_instance_vnfs
= []
3105 db_instance_vms
= []
3106 db_instance_interfaces
= []
3107 db_instance_sfis
= []
3108 db_instance_sfs
= []
3109 db_instance_classifications
= []
3110 db_instance_sfps
= []
3115 instance_name
= instance_dict
["name"]
3116 instance_uuid
= str(uuid4())
3117 uuid_list
.append(instance_uuid
)
3118 db_instance_scenario
= {
3119 "uuid": instance_uuid
,
3120 "name": instance_name
,
3121 "tenant_id": tenant_id
,
3122 "scenario_id": scenarioDict
['uuid'],
3123 "datacenter_id": default_datacenter_id
,
3124 # filled bellow 'datacenter_tenant_id'
3125 "description": instance_dict
.get("description"),
3127 if scenarioDict
.get("cloud-config"):
3128 db_instance_scenario
["cloud_config"] = yaml
.safe_dump(scenarioDict
["cloud-config"],
3129 default_flow_style
=True, width
=256)
3130 instance_action_id
= get_task_id()
3131 db_instance_action
= {
3132 "uuid": instance_action_id
, # same uuid for the instance and the action on create
3133 "tenant_id": tenant_id
,
3134 "instance_id": instance_uuid
,
3135 "description": "CREATE",
3138 # Auxiliary dictionaries from x to y
3139 sce_net2wim_instance
= {}
3140 sce_net2instance
= {}
3141 net2task_id
= {'scenario': {}}
3142 # Mapping between local networks and WIMs
3145 def ip_profile_IM2RO(ip_profile_im
):
3146 # translate from input format to database format
3148 if 'subnet-address' in ip_profile_im
:
3149 ip_profile_ro
['subnet_address'] = ip_profile_im
['subnet-address']
3150 if 'ip-version' in ip_profile_im
:
3151 ip_profile_ro
['ip_version'] = ip_profile_im
['ip-version']
3152 if 'gateway-address' in ip_profile_im
:
3153 ip_profile_ro
['gateway_address'] = ip_profile_im
['gateway-address']
3154 if 'dns-address' in ip_profile_im
:
3155 ip_profile_ro
['dns_address'] = ip_profile_im
['dns-address']
3156 if isinstance(ip_profile_ro
['dns_address'], (list, tuple)):
3157 ip_profile_ro
['dns_address'] = ";".join(ip_profile_ro
['dns_address'])
3158 if 'dhcp' in ip_profile_im
:
3159 ip_profile_ro
['dhcp_start_address'] = ip_profile_im
['dhcp'].get('start-address')
3160 ip_profile_ro
['dhcp_enabled'] = ip_profile_im
['dhcp'].get('enabled', True)
3161 ip_profile_ro
['dhcp_count'] = ip_profile_im
['dhcp'].get('count')
3162 return ip_profile_ro
3164 # logger.debug("Creating instance from scenario-dict:\n%s",
3165 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3167 # 0 check correct parameters
3168 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).items():
3169 for scenario_net
in scenarioDict
['nets']:
3170 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3173 raise NfvoException("Invalid scenario network name or id '{}' at instance:networks".format(net_name
),
3174 httperrors
.Bad_Request
)
3175 if "sites" not in net_instance_desc
:
3176 net_instance_desc
["sites"] = [ {} ]
3177 site_without_datacenter_field
= False
3178 for site
in net_instance_desc
["sites"]:
3179 if site
.get("datacenter"):
3180 site
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, site
["datacenter"])
3181 if site
["datacenter"] not in myvims
:
3182 # Add this datacenter to myvims
3183 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, site
["datacenter"])
3185 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, site
["datacenter"])
3186 site
["datacenter"] = d
# change name to id
3188 if site_without_datacenter_field
:
3189 raise NfvoException("Found more than one entries without datacenter field at "
3190 "instance:networks:{}:sites".format(net_name
), httperrors
.Bad_Request
)
3191 site_without_datacenter_field
= True
3192 site
["datacenter"] = default_datacenter_id
# change name to id
3194 for vnf_name
, vnf_instance_desc
in instance_dict
.get("vnfs",{}).items():
3195 for scenario_vnf
in scenarioDict
['vnfs']:
3196 if vnf_name
== scenario_vnf
['member_vnf_index'] or vnf_name
== scenario_vnf
['uuid'] or vnf_name
== scenario_vnf
['name']:
3199 raise NfvoException("Invalid vnf name '{}' at instance:vnfs".format(vnf_name
), httperrors
.Bad_Request
)
3200 if "datacenter" in vnf_instance_desc
:
3201 # Add this datacenter to myvims
3202 vnf_instance_desc
["datacenter"], _
= get_datacenter_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3203 if vnf_instance_desc
["datacenter"] not in myvims
:
3204 d
, v
= get_datacenter_by_name_uuid(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3206 myvim_threads_id
[d
], _
= get_vim_thread(mydb
, tenant_id
, vnf_instance_desc
["datacenter"])
3207 scenario_vnf
["datacenter"] = vnf_instance_desc
["datacenter"]
3209 for net_id
, net_instance_desc
in vnf_instance_desc
.get("networks", {}).items():
3210 for scenario_net
in scenario_vnf
['nets']:
3211 if net_id
== scenario_net
['osm_id'] or net_id
== scenario_net
['uuid'] or net_id
== scenario_net
["name"]:
3214 raise NfvoException("Invalid net id or name '{}' at instance:vnfs:networks".format(net_id
), httperrors
.Bad_Request
)
3215 if net_instance_desc
.get("vim-network-name"):
3216 scenario_net
["vim-network-name"] = net_instance_desc
["vim-network-name"]
3217 if net_instance_desc
.get("vim-network-id"):
3218 scenario_net
["vim-network-id"] = net_instance_desc
["vim-network-id"]
3219 if net_instance_desc
.get("name"):
3220 scenario_net
["name"] = net_instance_desc
["name"]
3221 if 'ip-profile' in net_instance_desc
:
3222 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3223 if 'ip_profile' not in scenario_net
:
3224 scenario_net
['ip_profile'] = ipprofile_db
3226 update(scenario_net
['ip_profile'], ipprofile_db
)
3228 if net_instance_desc
.get('provider-network'):
3229 provider_network_db
= net_instance_desc
['provider-network']
3230 if 'provider_network' not in scenario_net
:
3231 scenario_net
['provider_network'] = provider_network_db
3233 update(scenario_net
['provider_network'], provider_network_db
)
3235 for vdu_id
, vdu_instance_desc
in vnf_instance_desc
.get("vdus", {}).items():
3236 for scenario_vm
in scenario_vnf
['vms']:
3237 if vdu_id
== scenario_vm
['osm_id'] or vdu_id
== scenario_vm
["name"]:
3240 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3241 scenario_vm
["instance_parameters"] = vdu_instance_desc
3242 for iface_id
, iface_instance_desc
in vdu_instance_desc
.get("interfaces", {}).items():
3243 for scenario_interface
in scenario_vm
['interfaces']:
3244 if iface_id
== scenario_interface
['internal_name'] or iface_id
== scenario_interface
["external_name"]:
3245 scenario_interface
.update(iface_instance_desc
)
3248 raise NfvoException("Invalid vdu id or name '{}' at instance:vnfs:vdus".format(vdu_id
), httperrors
.Bad_Request
)
3250 # 0.1 parse cloud-config parameters
3251 cloud_config
= unify_cloud_config(instance_dict
.get("cloud-config"), scenarioDict
.get("cloud-config"))
3253 # 0.2 merge instance information into scenario
3254 # Ideally, the operation should be as simple as: update(scenarioDict,instance_dict)
3255 # However, this is not possible yet.
3256 for net_name
, net_instance_desc
in instance_dict
.get("networks", {}).items():
3257 for scenario_net
in scenarioDict
['nets']:
3258 if net_name
== scenario_net
.get("name") or net_name
== scenario_net
.get("osm_id") or net_name
== scenario_net
.get("uuid"):
3259 if "wim_account" in net_instance_desc
and net_instance_desc
["wim_account"] is not None:
3260 scenario_net
["wim_account"] = net_instance_desc
["wim_account"]
3261 if 'ip-profile' in net_instance_desc
:
3262 ipprofile_db
= ip_profile_IM2RO(net_instance_desc
['ip-profile'])
3263 if 'ip_profile' not in scenario_net
:
3264 scenario_net
['ip_profile'] = ipprofile_db
3266 update(scenario_net
['ip_profile'], ipprofile_db
)
3267 if 'provider-network' in net_instance_desc
:
3268 provider_network_db
= net_instance_desc
['provider-network']
3270 if 'provider-network' not in scenario_net
:
3271 scenario_net
['provider_network'] = provider_network_db
3273 update(scenario_net
['provider-network'], provider_network_db
)
3275 for interface
in net_instance_desc
.get('interfaces', ()):
3276 if 'ip_address' in interface
:
3277 for vnf
in scenarioDict
['vnfs']:
3278 if interface
['vnf'] == vnf
['name']:
3279 for vnf_interface
in vnf
['interfaces']:
3280 if interface
['vnf_interface'] == vnf_interface
['external_name']:
3281 vnf_interface
['ip_address'] = interface
['ip_address']
3283 # logger.debug(">>>>>>>> Merged dictionary")
3284 # logger.debug("Creating instance scenario-dict MERGED:\n%s",
3285 # yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False))
3288 # 1. Creating new nets (sce_nets) in the VIM"
3289 number_mgmt_networks
= 0
3290 db_instance_nets
= []
3291 db_instance_wim_nets
= []
3292 for sce_net
in scenarioDict
['nets']:
3294 sce_net_uuid
= sce_net
.get('uuid', sce_net
["name"])
3295 # get involved datacenters where this network need to be created
3296 involved_datacenters
= []
3297 for sce_vnf
in scenarioDict
.get("vnfs", ()):
3298 vnf_datacenter
= sce_vnf
.get("datacenter", default_datacenter_id
)
3299 if vnf_datacenter
in involved_datacenters
:
3301 if sce_vnf
.get("interfaces"):
3302 for sce_vnf_ifaces
in sce_vnf
["interfaces"]:
3303 if sce_vnf_ifaces
.get("sce_net_id") == sce_net
["uuid"]:
3304 involved_datacenters
.append(vnf_datacenter
)
3306 if not involved_datacenters
:
3307 involved_datacenters
.append(default_datacenter_id
)
3308 target_wim_account
= sce_net
.get("wim_account", default_wim_account
)
3311 if instance_dict
.get("networks"):
3312 if sce_net
.get("uuid") in instance_dict
["networks"]:
3313 descriptor_net
= instance_dict
["networks"][sce_net
["uuid"]]
3314 descriptor_net_name
= sce_net
["uuid"]
3315 elif sce_net
.get("osm_id") in instance_dict
["networks"]:
3316 descriptor_net
= instance_dict
["networks"][sce_net
["osm_id"]]
3317 descriptor_net_name
= sce_net
["osm_id"]
3318 elif sce_net
["name"] in instance_dict
["networks"]:
3319 descriptor_net
= instance_dict
["networks"][sce_net
["name"]]
3320 descriptor_net_name
= sce_net
["name"]
3321 net_name
= descriptor_net
.get("vim-network-name")
3322 # add datacenters from instantiation parameters
3323 if descriptor_net
.get("sites"):
3324 for site
in descriptor_net
["sites"]:
3325 if site
.get("datacenter") and site
["datacenter"] not in involved_datacenters
:
3326 involved_datacenters
.append(site
["datacenter"])
3327 sce_net2instance
[sce_net_uuid
] = {}
3328 sce_net2wim_instance
[sce_net_uuid
] = {}
3329 net2task_id
['scenario'][sce_net_uuid
] = {}
3332 related_network
= None
3333 if descriptor_net
.get("use-network"):
3334 target_instance_nets
= mydb
.get_rows(
3336 FROM
="instance_nets",
3337 WHERE
={"instance_scenario_id": descriptor_net
["use-network"]["instance_scenario_id"],
3338 "osm_id": descriptor_net
["use-network"]["osm_id"]},
3340 if not target_instance_nets
:
3341 raise NfvoException(
3342 "Cannot find the target network at instance:networks[{}]:use-network".format(
3343 descriptor_net_name
), httperrors
.Bad_Request
)
3345 use_network
= target_instance_nets
[0]["related"]
3347 if sce_net
["external"]:
3348 number_mgmt_networks
+= 1
3351 # TODO: use this information during network creation
3352 wim_account_id
= wim_account_name
= None
3353 if len(involved_datacenters
) > 1 and 'uuid' in sce_net
:
3354 urls
= [myvims
[v
].url
for v
in involved_datacenters
]
3355 if len(set(urls
)) < 2:
3356 wim_usage
[sce_net
['uuid']] = False
3357 elif target_wim_account
is None or target_wim_account
is True: # automatic selection of WIM
3358 # OBS: sce_net without uuid are used internally to VNFs
3359 # and the assumption is that VNFs will not be split among
3360 # different datacenters
3361 wim_account
= wim_engine
.find_suitable_wim_account(
3362 involved_datacenters
, tenant_id
)
3363 wim_account_id
= wim_account
['uuid']
3364 wim_account_name
= wim_account
['name']
3365 wim_usage
[sce_net
['uuid']] = wim_account_id
3366 elif isinstance(target_wim_account
, str): # manual selection of WIM
3367 wim_account
.persist
.get_wim_account_by(target_wim_account
, tenant_id
)
3368 wim_account_id
= wim_account
['uuid']
3369 wim_account_name
= wim_account
['name']
3370 wim_usage
[sce_net
['uuid']] = wim_account_id
3371 else: # not WIM usage
3372 wim_usage
[sce_net
['uuid']] = False
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
3484 task_wim_extra
= {"params": [net_type
, wim_account_name
]}
3485 # add sdn interfaces
3486 if sce_net
.get('provider_network') and sce_net
['provider_network'].get("sdn-ports"):
3487 task_wim_extra
["sdn-ports"] = sce_net
['provider_network'].get("sdn-ports")
3489 "instance_action_id": instance_action_id
,
3490 "status": "SCHEDULED",
3491 "task_index": task_index
,
3492 # "datacenter_vim_id": myvim_thread_id,
3493 "wim_account_id": sdn_controller
,
3494 "action": task_action
,
3495 "item": "instance_wim_nets",
3496 "item_id": sdn_net_id
,
3497 "related": related_network
,
3498 "extra": yaml
.safe_dump(task_wim_extra
, default_flow_style
=True, width
=256)
3501 db_vim_actions
.append(db_vim_action
)
3504 "osm_id": sce_net
.get("osm_id") or sce_net
["name"],
3505 "related": related_network
,
3507 "vim_name": net_vim_name
,
3508 "instance_scenario_id": instance_uuid
,
3509 "sce_net_id": sce_net
.get("uuid"),
3510 "created": create_network
,
3511 'datacenter_id': datacenter_id
,
3512 'datacenter_tenant_id': myvim_thread_id
,
3513 'status': 'BUILD', # if create_network else "ACTIVE"
3514 'sdn_net_id': sdn_net_id
,
3516 db_instance_nets
.append(db_net
)
3518 "instance_action_id": instance_action_id
,
3519 "status": "SCHEDULED",
3520 "task_index": task_index
,
3521 "datacenter_vim_id": myvim_thread_id
,
3522 "action": task_action
,
3523 "item": "instance_nets",
3524 "item_id": net_uuid
,
3525 "related": related_network
,
3526 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3528 net2task_id
['scenario'][sce_net_uuid
][datacenter_id
] = task_index
3530 db_vim_actions
.append(db_vim_action
)
3532 if 'ip_profile' in sce_net
:
3534 'instance_net_id': net_uuid
,
3535 'ip_version': sce_net
['ip_profile']['ip_version'],
3536 'subnet_address': sce_net
['ip_profile']['subnet_address'],
3537 'gateway_address': sce_net
['ip_profile']['gateway_address'],
3538 'dns_address': sce_net
['ip_profile']['dns_address'],
3539 'dhcp_enabled': sce_net
['ip_profile']['dhcp_enabled'],
3540 'dhcp_start_address': sce_net
['ip_profile']['dhcp_start_address'],
3541 'dhcp_count': sce_net
['ip_profile']['dhcp_count'],
3543 db_ip_profiles
.append(db_ip_profile
)
3547 "default_datacenter_id": default_datacenter_id
,
3548 "myvim_threads_id": myvim_threads_id
,
3549 "instance_uuid": instance_uuid
,
3550 "instance_name": instance_name
,
3551 "instance_action_id": instance_action_id
,
3553 "cloud_config": cloud_config
,
3554 "RO_pub_key": tenant
[0].get('RO_pub_key'),
3555 "instance_parameters": instance_dict
,
3558 "task_index": task_index
,
3559 "uuid_list": uuid_list
,
3560 "db_instance_nets": db_instance_nets
,
3561 "db_instance_wim_nets": db_instance_wim_nets
,
3562 "db_vim_actions": db_vim_actions
,
3563 "db_ip_profiles": db_ip_profiles
,
3564 "db_instance_vnfs": db_instance_vnfs
,
3565 "db_instance_vms": db_instance_vms
,
3566 "db_instance_interfaces": db_instance_interfaces
,
3567 "net2task_id": net2task_id
,
3568 "sce_net2instance": sce_net2instance
,
3569 "sce_net2wim_instance": sce_net2wim_instance
,
3571 # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
3572 for sce_vnf
in scenarioDict
.get('vnfs', ()): # sce_vnf_list:
3573 instantiate_vnf(mydb
, sce_vnf
, vnf_params
, vnf_params_out
, rollbackList
)
3574 task_index
= vnf_params_out
["task_index"]
3575 uuid_list
= vnf_params_out
["uuid_list"]
3578 # task_depends_on = []
3579 for vnffg
in scenarioDict
.get('vnffgs', ()):
3580 for rsp
in vnffg
['rsps']:
3582 for cp
in rsp
['connection_points']:
3583 count
= mydb
.get_rows(
3585 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_rsp_hops as h "
3586 "on interfaces.uuid=h.ingress_interface_id",
3587 WHERE
={'h.uuid': cp
['uuid']})[0]['count']
3588 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == cp
['sce_vnf_id']), None)
3589 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3591 for instance_vm
in instance_vms
:
3592 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3594 dependencies
.append(action
['task_index'])
3595 # TODO: throw exception if count != len(instance_vms)
3596 # TODO: and action shouldn't ever be None
3598 for i
in range(count
):
3600 sfi_uuid
= str(uuid4())
3602 "ingress_interface_id": cp
["ingress_interface_id"],
3603 "egress_interface_id": cp
["egress_interface_id"]
3605 uuid_list
.append(sfi_uuid
)
3608 "related": sfi_uuid
,
3609 "instance_scenario_id": instance_uuid
,
3610 'sce_rsp_hop_id': cp
['uuid'],
3611 'datacenter_id': datacenter_id
,
3612 'datacenter_tenant_id': myvim_thread_id
,
3613 "vim_sfi_id": None, # vim thread will populate
3615 db_instance_sfis
.append(db_sfi
)
3617 "instance_action_id": instance_action_id
,
3618 "task_index": task_index
,
3619 "datacenter_vim_id": myvim_thread_id
,
3621 "status": "SCHEDULED",
3622 "item": "instance_sfis",
3623 "item_id": sfi_uuid
,
3624 "related": sfi_uuid
,
3625 "extra": yaml
.safe_dump({"params": extra_params
, "depends_on": [dependencies
[i
]]},
3626 default_flow_style
=True, width
=256)
3628 sfis_created
.append(task_index
)
3630 db_vim_actions
.append(db_vim_action
)
3632 sf_uuid
= str(uuid4())
3633 uuid_list
.append(sf_uuid
)
3637 "instance_scenario_id": instance_uuid
,
3638 'sce_rsp_hop_id': cp
['uuid'],
3639 'datacenter_id': datacenter_id
,
3640 'datacenter_tenant_id': myvim_thread_id
,
3641 "vim_sf_id": None, # vim thread will populate
3643 db_instance_sfs
.append(db_sf
)
3645 "instance_action_id": instance_action_id
,
3646 "task_index": task_index
,
3647 "datacenter_vim_id": myvim_thread_id
,
3649 "status": "SCHEDULED",
3650 "item": "instance_sfs",
3653 "extra": yaml
.safe_dump({"params": "", "depends_on": sfis_created
},
3654 default_flow_style
=True, width
=256)
3656 sfs_created
.append(task_index
)
3658 db_vim_actions
.append(db_vim_action
)
3659 classifier
= rsp
['classifier']
3661 # TODO the following ~13 lines can be reused for the sfi case
3662 count
= mydb
.get_rows(
3663 SELECT
=('vms.count'),
3664 FROM
="vms join interfaces on vms.uuid=interfaces.vm_id join sce_classifiers as c on interfaces.uuid=c.interface_id",
3665 WHERE
={'c.uuid': classifier
['uuid']})[0]['count']
3666 instance_vnf
= next((item
for item
in db_instance_vnfs
if item
['sce_vnf_id'] == classifier
['sce_vnf_id']), None)
3667 instance_vms
= [item
for item
in db_instance_vms
if item
['instance_vnf_id'] == instance_vnf
['uuid']]
3669 for instance_vm
in instance_vms
:
3670 action
= next((item
for item
in db_vim_actions
if item
['item_id'] == instance_vm
['uuid']), None)
3672 dependencies
.append(action
['task_index'])
3673 # TODO: throw exception if count != len(instance_vms)
3674 # TODO: and action shouldn't ever be None
3675 classifications_created
= []
3676 for i
in range(count
):
3677 for match
in classifier
['matches']:
3678 # create classifications
3679 classification_uuid
= str(uuid4())
3680 uuid_list
.append(classification_uuid
)
3681 db_classification
= {
3682 "uuid": classification_uuid
,
3683 "related": classification_uuid
,
3684 "instance_scenario_id": instance_uuid
,
3685 'sce_classifier_match_id': match
['uuid'],
3686 'datacenter_id': datacenter_id
,
3687 'datacenter_tenant_id': myvim_thread_id
,
3688 "vim_classification_id": None, # vim thread will populate
3690 db_instance_classifications
.append(db_classification
)
3691 classification_params
= {
3692 "ip_proto": match
["ip_proto"],
3693 "source_ip": match
["source_ip"],
3694 "destination_ip": match
["destination_ip"],
3695 "source_port": match
["source_port"],
3696 "destination_port": match
["destination_port"],
3697 "logical_source_port": classifier
["interface_id"]
3700 "instance_action_id": instance_action_id
,
3701 "task_index": task_index
,
3702 "datacenter_vim_id": myvim_thread_id
,
3704 "status": "SCHEDULED",
3705 "item": "instance_classifications",
3706 "item_id": classification_uuid
,
3707 "related": classification_uuid
,
3708 "extra": yaml
.safe_dump({"params": classification_params
, "depends_on": [dependencies
[i
]]},
3709 default_flow_style
=True, width
=256)
3711 classifications_created
.append(task_index
)
3713 db_vim_actions
.append(db_vim_action
)
3716 sfp_uuid
= str(uuid4())
3717 uuid_list
.append(sfp_uuid
)
3720 "related": sfp_uuid
,
3721 "instance_scenario_id": instance_uuid
,
3722 'sce_rsp_id': rsp
['uuid'],
3723 'datacenter_id': datacenter_id
,
3724 'datacenter_tenant_id': myvim_thread_id
,
3725 "vim_sfp_id": None, # vim thread will populate
3727 db_instance_sfps
.append(db_sfp
)
3729 "instance_action_id": instance_action_id
,
3730 "task_index": task_index
,
3731 "datacenter_vim_id": myvim_thread_id
,
3733 "status": "SCHEDULED",
3734 "item": "instance_sfps",
3735 "item_id": sfp_uuid
,
3736 "related": sfp_uuid
,
3737 "extra": yaml
.safe_dump({"params": "", "depends_on": sfs_created
+ classifications_created
},
3738 default_flow_style
=True, width
=256)
3741 db_vim_actions
.append(db_vim_action
)
3742 db_instance_action
["number_tasks"] = task_index
3745 logger
.debug('wim_usage:\n%s\n\n', pformat(wim_usage
))
3746 wan_links
= wim_engine
.derive_wan_links(wim_usage
, db_instance_nets
, tenant_id
)
3747 wim_actions
= wim_engine
.create_actions(wan_links
)
3748 wim_actions
, db_instance_action
= (
3749 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
3752 scenarioDict
["datacenter2tenant"] = myvim_threads_id
3754 db_instance_scenario
['datacenter_tenant_id'] = myvim_threads_id
[default_datacenter_id
]
3755 db_instance_scenario
['datacenter_id'] = default_datacenter_id
3757 {"instance_scenarios": db_instance_scenario
},
3758 {"instance_vnfs": db_instance_vnfs
},
3759 {"instance_nets": db_instance_nets
},
3760 {"ip_profiles": db_ip_profiles
},
3761 {"instance_vms": db_instance_vms
},
3762 {"instance_interfaces": db_instance_interfaces
},
3763 {"instance_actions": db_instance_action
},
3764 {"instance_sfis": db_instance_sfis
},
3765 {"instance_sfs": db_instance_sfs
},
3766 {"instance_classifications": db_instance_classifications
},
3767 {"instance_sfps": db_instance_sfps
},
3768 {"instance_wim_nets": db_instance_wim_nets
+ wan_links
},
3769 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
3772 logger
.debug("create_instance done DB tables: %s",
3773 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False) )
3774 mydb
.new_rows(db_tables
, uuid_list
)
3775 for myvim_thread_id
in myvim_threads_id
.values():
3776 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
3778 wim_engine
.dispatch(wim_actions
)
3780 returned_instance
= mydb
.get_instance_scenario(instance_uuid
)
3781 returned_instance
["action_id"] = instance_action_id
3782 return returned_instance
3783 except (NfvoException
, vimconn
.vimconnException
, sdnconn
.SdnConnectorError
, db_base_Exception
) as e
:
3784 message
= rollback(mydb
, myvims
, rollbackList
)
3785 if isinstance(e
, db_base_Exception
):
3786 error_text
= "database Exception"
3787 elif isinstance(e
, vimconn
.vimconnException
):
3788 error_text
= "VIM Exception"
3789 elif isinstance(e
, sdnconn
.SdnConnectorError
):
3790 error_text
= "WIM Exception"
3792 error_text
= "Exception"
3793 error_text
+= " {} {}. {}".format(type(e
).__name
__, str(e
), message
)
3794 # logger.error("create_instance: %s", error_text)
3796 raise NfvoException(error_text
, e
.http_code
)
3799 def instantiate_vnf(mydb
, sce_vnf
, params
, params_out
, rollbackList
):
3800 default_datacenter_id
= params
["default_datacenter_id"]
3801 myvim_threads_id
= params
["myvim_threads_id"]
3802 instance_uuid
= params
["instance_uuid"]
3803 instance_name
= params
["instance_name"]
3804 instance_action_id
= params
["instance_action_id"]
3805 myvims
= params
["myvims"]
3806 cloud_config
= params
["cloud_config"]
3807 RO_pub_key
= params
["RO_pub_key"]
3809 task_index
= params_out
["task_index"]
3810 uuid_list
= params_out
["uuid_list"]
3811 db_instance_nets
= params_out
["db_instance_nets"]
3812 db_instance_wim_nets
= params_out
["db_instance_wim_nets"]
3813 db_vim_actions
= params_out
["db_vim_actions"]
3814 db_ip_profiles
= params_out
["db_ip_profiles"]
3815 db_instance_vnfs
= params_out
["db_instance_vnfs"]
3816 db_instance_vms
= params_out
["db_instance_vms"]
3817 db_instance_interfaces
= params_out
["db_instance_interfaces"]
3818 net2task_id
= params_out
["net2task_id"]
3819 sce_net2instance
= params_out
["sce_net2instance"]
3820 sce_net2wim_instance
= params_out
["sce_net2wim_instance"]
3822 vnf_net2instance
= {}
3823 vnf_net2wim_instance
= {}
3825 # 2. Creating new nets (vnf internal nets) in the VIM"
3826 # For each vnf net, we create it and we add it to instanceNetlist.
3827 if sce_vnf
.get("datacenter"):
3828 vim
= myvims
[sce_vnf
["datacenter"]]
3829 datacenter_id
= sce_vnf
["datacenter"]
3830 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3832 vim
= myvims
[default_datacenter_id
]
3833 datacenter_id
= default_datacenter_id
3834 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3835 for net
in sce_vnf
['nets']:
3837 # descriptor_net = instance_dict.get("vnfs", {}).get(sce_vnf["name"], {})
3838 # net_name = descriptor_net.get("name")
3841 net_name
= "{}-{}".format(instance_name
, net
["name"])
3842 net_name
= net_name
[:255] # limit length
3843 net_type
= net
['type']
3845 if sce_vnf
['uuid'] not in vnf_net2instance
:
3846 vnf_net2instance
[sce_vnf
['uuid']] = {}
3847 if sce_vnf
['uuid'] not in net2task_id
:
3848 net2task_id
[sce_vnf
['uuid']] = {}
3850 # fill database content
3851 net_uuid
= str(uuid4())
3852 uuid_list
.append(net_uuid
)
3853 vnf_net2instance
[sce_vnf
['uuid']][net
['uuid']] = net_uuid
3855 sdn_controller
= vim
.config
.get('sdn-controller')
3857 if sdn_controller
and net_type
in ("data", "ptp"):
3858 wim_id
= _get_wim(mydb
, sdn_controller
)
3859 sdn_net_id
= str(uuid4())
3860 db_instance_wim_nets
.append({
3862 "instance_scenario_id": instance_uuid
,
3864 "wim_account_id": sdn_controller
,
3865 'status': 'BUILD', # if create_network else "ACTIVE"
3866 "related": net_uuid
,
3867 'multipoint': True if net_type
== "data" else False,
3868 "created": True, # TODO py3
3871 vnf_net2wim_instance
[net_uuid
] = sdn_net_id
3875 "related": net_uuid
,
3877 "vim_name": net_name
,
3878 "instance_scenario_id": instance_uuid
,
3879 "net_id": net
["uuid"],
3881 'datacenter_id': datacenter_id
,
3882 'datacenter_tenant_id': myvim_thread_id
,
3883 'sdn_net_id': sdn_net_id
,
3885 db_instance_nets
.append(db_net
)
3888 if net
.get("vim-network-name"):
3889 lookfor_filter
["name"] = net
["vim-network-name"]
3890 if net
.get("vim-network-id"):
3891 lookfor_filter
["id"] = net
["vim-network-id"]
3893 task_action
= "FIND"
3894 task_extra
= {"params": (lookfor_filter
,)}
3896 task_action
= "CREATE"
3897 task_extra
= {"params": (net_name
, net_type
, net
.get('ip_profile', None))}
3899 task_extra
["sdn_net_id"] = sdn_net_id
3902 task_wim_extra
= {"params": [net_type
, None]}
3904 "instance_action_id": instance_action_id
,
3905 "status": "SCHEDULED",
3906 "task_index": task_index
,
3907 # "datacenter_vim_id": myvim_thread_id,
3908 "wim_account_id": sdn_controller
,
3909 "action": task_action
,
3910 "item": "instance_wim_nets",
3911 "item_id": sdn_net_id
,
3912 "related": net_uuid
,
3913 "extra": yaml
.safe_dump(task_wim_extra
, default_flow_style
=True, width
=256)
3916 db_vim_actions
.append(db_vim_action
)
3918 "instance_action_id": instance_action_id
,
3919 "task_index": task_index
,
3920 "datacenter_vim_id": myvim_thread_id
,
3921 "status": "SCHEDULED",
3922 "action": task_action
,
3923 "item": "instance_nets",
3924 "item_id": net_uuid
,
3925 "related": net_uuid
,
3926 "extra": yaml
.safe_dump(task_extra
, default_flow_style
=True, width
=256)
3928 net2task_id
[sce_vnf
['uuid']][net
['uuid']] = task_index
3930 db_vim_actions
.append(db_vim_action
)
3932 if 'ip_profile' in net
:
3934 'instance_net_id': net_uuid
,
3935 'ip_version': net
['ip_profile']['ip_version'],
3936 'subnet_address': net
['ip_profile']['subnet_address'],
3937 'gateway_address': net
['ip_profile']['gateway_address'],
3938 'dns_address': net
['ip_profile']['dns_address'],
3939 'dhcp_enabled': net
['ip_profile']['dhcp_enabled'],
3940 'dhcp_start_address': net
['ip_profile']['dhcp_start_address'],
3941 'dhcp_count': net
['ip_profile']['dhcp_count'],
3943 db_ip_profiles
.append(db_ip_profile
)
3945 # print "vnf_net2instance:"
3946 # print yaml.safe_dump(vnf_net2instance, indent=4, default_flow_style=False)
3948 # 3. Creating new vm instances in the VIM
3949 # myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
3951 if sce_vnf
.get('mgmt_access'):
3952 ssh_access
= sce_vnf
['mgmt_access'].get('config-access', {}).get('ssh-access')
3953 vnf_availability_zones
= []
3954 for vm
in sce_vnf
.get('vms'):
3955 vm_av
= vm
.get('availability_zone')
3956 if vm_av
and vm_av
not in vnf_availability_zones
:
3957 vnf_availability_zones
.append(vm_av
)
3959 # check if there is enough availability zones available at vim level.
3960 if myvims
[datacenter_id
].availability_zone
and vnf_availability_zones
:
3961 if len(vnf_availability_zones
) > len(myvims
[datacenter_id
].availability_zone
):
3962 raise NfvoException('No enough availability zones at VIM for this deployment', httperrors
.Bad_Request
)
3964 if sce_vnf
.get("datacenter"):
3965 vim
= myvims
[sce_vnf
["datacenter"]]
3966 myvim_thread_id
= myvim_threads_id
[sce_vnf
["datacenter"]]
3967 datacenter_id
= sce_vnf
["datacenter"]
3969 vim
= myvims
[default_datacenter_id
]
3970 myvim_thread_id
= myvim_threads_id
[default_datacenter_id
]
3971 datacenter_id
= default_datacenter_id
3972 sce_vnf
["datacenter_id"] = datacenter_id
3975 vnf_uuid
= str(uuid4())
3976 uuid_list
.append(vnf_uuid
)
3979 'instance_scenario_id': instance_uuid
,
3980 'vnf_id': sce_vnf
['vnf_id'],
3981 'sce_vnf_id': sce_vnf
['uuid'],
3982 'datacenter_id': datacenter_id
,
3983 'datacenter_tenant_id': myvim_thread_id
,
3985 db_instance_vnfs
.append(db_instance_vnf
)
3987 for vm
in sce_vnf
['vms']:
3989 if vm
.get("pdu_type"):
3993 sce_vnf_name
= sce_vnf
['member_vnf_index'] if sce_vnf
['member_vnf_index'] else sce_vnf
['name']
3994 myVMDict
['name'] = "{}-{}-{}".format(instance_name
[:64], sce_vnf_name
[:64], vm
["name"][:64])
3995 myVMDict
['description'] = myVMDict
['name'][0:99]
3997 # myVMDict['start'] = "no"
3998 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("name"):
3999 myVMDict
['name'] = vm
["instance_parameters"].get("name")
4000 myVMDict
['name'] = myVMDict
['name'][0:255] # limit name length
4001 # create image at vim in case it not exist
4002 image_uuid
= vm
['image_id']
4003 if vm
.get("image_list"):
4004 for alternative_image
in vm
["image_list"]:
4005 if alternative_image
["vim_type"] == vim
["config"]["_vim_type_internal"]:
4006 image_uuid
= alternative_image
['image_id']
4008 image_dict
= mydb
.get_table_by_uuid_name("images", image_uuid
)
4009 image_id
= create_or_use_image(mydb
, {datacenter_id
: vim
}, image_dict
, [], True)
4010 vm
['vim_image_id'] = image_id
4012 # create flavor at vim in case it not exist
4013 flavor_dict
= mydb
.get_table_by_uuid_name("flavors", vm
['flavor_id'])
4014 if flavor_dict
['extended'] != None:
4015 flavor_dict
['extended'] = yaml
.load(flavor_dict
['extended'], Loader
=yaml
.Loader
)
4016 flavor_id
= create_or_use_flavor(mydb
, {datacenter_id
: vim
}, flavor_dict
, rollbackList
, True)
4018 # Obtain information for additional disks
4019 extended_flavor_dict
= mydb
.get_rows(FROM
='datacenters_flavors', SELECT
=('extended',),
4020 WHERE
={'vim_id': flavor_id
})
4021 if not extended_flavor_dict
:
4022 raise NfvoException("flavor '{}' not found".format(flavor_id
), httperrors
.Not_Found
)
4024 # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0], Loader=yaml.Loader)
4025 myVMDict
['disks'] = None
4026 extended_info
= extended_flavor_dict
[0]['extended']
4027 if extended_info
!= None:
4028 extended_flavor_dict_yaml
= yaml
.load(extended_info
, Loader
=yaml
.Loader
)
4029 if 'disks' in extended_flavor_dict_yaml
:
4030 myVMDict
['disks'] = extended_flavor_dict_yaml
['disks']
4031 if vm
.get("instance_parameters") and vm
["instance_parameters"].get("devices"):
4032 for disk
in myVMDict
['disks']:
4033 if disk
.get("name") in vm
["instance_parameters"]["devices"]:
4034 disk
.update(vm
["instance_parameters"]["devices"][disk
.get("name")])
4036 vm
['vim_flavor_id'] = flavor_id
4037 myVMDict
['imageRef'] = vm
['vim_image_id']
4038 myVMDict
['flavorRef'] = vm
['vim_flavor_id']
4039 myVMDict
['availability_zone'] = vm
.get('availability_zone')
4040 myVMDict
['networks'] = []
4041 task_depends_on
= []
4042 # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
4043 is_management_vm
= False
4045 for iface
in vm
['interfaces']:
4047 if iface
['type'] == "data":
4048 netDict
['type'] = iface
['model']
4049 elif "model" in iface
and iface
["model"] != None:
4050 netDict
['model'] = iface
['model']
4051 # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
4052 # is obtained from iterface table model
4053 # discover type of interface looking at flavor
4054 for numa
in flavor_dict
.get('extended', {}).get('numas', []):
4055 for flavor_iface
in numa
.get('interfaces', []):
4056 if flavor_iface
.get('name') == iface
['internal_name']:
4057 if flavor_iface
['dedicated'] == 'yes':
4058 netDict
['type'] = "PF" # passthrough
4059 elif flavor_iface
['dedicated'] == 'no':
4060 netDict
['type'] = "VF" # siov
4061 elif flavor_iface
['dedicated'] == 'yes:sriov':
4062 netDict
['type'] = "VFnotShared" # sriov but only one sriov on the PF
4063 netDict
["mac_address"] = flavor_iface
.get("mac_address")
4065 netDict
["use"] = iface
['type']
4066 if netDict
["use"] == "data" and not netDict
.get("type"):
4067 # print "netDict", netDict
4068 # print "iface", iface
4069 e_text
= "Cannot determine the interface type PF or VF of VNF '{}' VM '{}' iface '{}'".fromat(
4070 sce_vnf
['name'], vm
['name'], iface
['internal_name'])
4071 if flavor_dict
.get('extended') == None:
4072 raise NfvoException(e_text
+ "After database migration some information is not available. \
4073 Try to delete and create the scenarios and VNFs again", httperrors
.Conflict
)
4075 raise NfvoException(e_text
, httperrors
.Internal_Server_Error
)
4076 if netDict
["use"] == "mgmt":
4077 is_management_vm
= True
4078 netDict
["type"] = "virtual"
4079 if netDict
["use"] == "bridge":
4080 netDict
["type"] = "virtual"
4081 if iface
.get("vpci"):
4082 netDict
['vpci'] = iface
['vpci']
4083 if iface
.get("mac"):
4084 netDict
['mac_address'] = iface
['mac']
4085 if iface
.get("mac_address"):
4086 netDict
['mac_address'] = iface
['mac_address']
4087 if iface
.get("ip_address"):
4088 netDict
['ip_address'] = iface
['ip_address']
4089 if iface
.get("port-security") is not None:
4090 netDict
['port_security'] = iface
['port-security']
4091 if iface
.get("floating-ip") is not None:
4092 netDict
['floating_ip'] = iface
['floating-ip']
4093 netDict
['name'] = iface
['internal_name']
4094 if iface
['net_id'] is None:
4095 for vnf_iface
in sce_vnf
["interfaces"]:
4098 if vnf_iface
['interface_id'] == iface
['uuid']:
4099 netDict
['net_id'] = "TASK-{}".format(
4100 net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
4101 instance_net_id
= sce_net2instance
[vnf_iface
['sce_net_id']][datacenter_id
]
4102 instance_wim_net_id
= sce_net2wim_instance
[vnf_iface
['sce_net_id']][datacenter_id
]
4103 task_depends_on
.append(net2task_id
['scenario'][vnf_iface
['sce_net_id']][datacenter_id
])
4106 netDict
['net_id'] = "TASK-{}".format(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
4107 instance_net_id
= vnf_net2instance
[sce_vnf
['uuid']][iface
['net_id']]
4108 instance_wim_net_id
= vnf_net2wim_instance
.get(instance_net_id
)
4109 task_depends_on
.append(net2task_id
[sce_vnf
['uuid']][iface
['net_id']])
4110 # skip bridge ifaces not connected to any net
4111 if 'net_id' not in netDict
or netDict
['net_id'] == None:
4113 myVMDict
['networks'].append(netDict
)
4116 # 'instance_vm_id': instance_vm_uuid,
4117 "instance_net_id": instance_net_id
,
4118 "instance_wim_net_id": instance_wim_net_id
,
4119 'interface_id': iface
['uuid'],
4120 # 'vim_interface_id': ,
4121 'type': 'external' if iface
['external_name'] is not None else 'internal',
4122 'model': iface
['model'],
4123 'ip_address': iface
.get('ip_address'),
4124 'mac_address': iface
.get('mac'),
4125 'floating_ip': int(iface
.get('floating-ip', False)),
4126 'port_security': int(iface
.get('port-security', True))
4128 db_vm_ifaces
.append(db_vm_iface
)
4129 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
4130 # print myVMDict['name']
4131 # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
4132 # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
4133 # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
4135 # We add the RO key to cloud_config if vnf will need ssh access
4136 cloud_config_vm
= cloud_config
4137 if is_management_vm
and params
["instance_parameters"].get("mgmt_keys"):
4138 cloud_config_vm
= unify_cloud_config({"key-pairs": params
["instance_parameters"]["mgmt_keys"]},
4141 if vm
.get("instance_parameters") and "mgmt_keys" in vm
["instance_parameters"]:
4142 if vm
["instance_parameters"]["mgmt_keys"]:
4143 cloud_config_vm
= unify_cloud_config({"key-pairs": vm
["instance_parameters"]["mgmt_keys"]},
4146 cloud_config_vm
= unify_cloud_config(cloud_config_vm
, {"key-pairs": [RO_pub_key
]})
4147 if vm
.get("boot_data"):
4148 cloud_config_vm
= unify_cloud_config(vm
["boot_data"], cloud_config_vm
)
4150 if myVMDict
.get('availability_zone'):
4151 av_index
= vnf_availability_zones
.index(myVMDict
['availability_zone'])
4154 for vm_index
in range(0, vm
.get('count', 1)):
4155 vm_name
= myVMDict
['name'] + "-" + str(vm_index
+1)
4156 task_params
= (vm_name
, myVMDict
['description'], myVMDict
.get('start', None),
4157 myVMDict
['imageRef'], myVMDict
['flavorRef'], myVMDict
['networks'], cloud_config_vm
,
4158 myVMDict
['disks'], av_index
, vnf_availability_zones
)
4159 # put interface uuid back to scenario[vnfs][vms[[interfaces]
4160 for net
in myVMDict
['networks']:
4162 for iface
in vm
['interfaces']:
4163 if net
["name"] == iface
["internal_name"]:
4164 iface
["vim_id"] = net
["vim_id"]
4166 vm_uuid
= str(uuid4())
4167 uuid_list
.append(vm_uuid
)
4171 'instance_vnf_id': vnf_uuid
,
4172 # TODO delete "vim_vm_id": vm_id,
4173 "vm_id": vm
["uuid"],
4174 "vim_name": vm_name
,
4177 db_instance_vms
.append(db_vm
)
4180 for db_vm_iface
in db_vm_ifaces
:
4181 iface_uuid
= str(uuid4())
4182 uuid_list
.append(iface_uuid
)
4183 db_vm_iface_instance
= {
4185 "instance_vm_id": vm_uuid
4187 db_vm_iface_instance
.update(db_vm_iface
)
4188 if db_vm_iface_instance
.get("ip_address"): # increment ip_address
4189 ip
= db_vm_iface_instance
.get("ip_address")
4194 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4195 db_vm_iface_instance
["ip_address"] = ip
4197 db_vm_iface_instance
["ip_address"] = None
4198 db_instance_interfaces
.append(db_vm_iface_instance
)
4199 myVMDict
['networks'][iface_index
]["uuid"] = iface_uuid
4203 "instance_action_id": instance_action_id
,
4204 "task_index": task_index
,
4205 "datacenter_vim_id": myvim_thread_id
,
4207 "status": "SCHEDULED",
4208 "item": "instance_vms",
4211 "extra": yaml
.safe_dump({"params": task_params
, "depends_on": task_depends_on
},
4212 default_flow_style
=True, width
=256)
4215 db_vim_actions
.append(db_vim_action
)
4216 params_out
["task_index"] = task_index
4217 params_out
["uuid_list"] = uuid_list
4220 def delete_instance(mydb
, tenant_id
, instance_id
):
4221 # print "Checking that the instance_id exists and getting the instance dictionary"
4222 instanceDict
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
4223 # print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4224 tenant_id
= instanceDict
["tenant_id"]
4227 # We need to retrieve the WIM Actions now, before the instance_scenario is
4228 # deleted. The reason for that is that: ON CASCADE rules will delete the
4229 # instance_wim_nets record in the database
4230 wim_actions
= wim_engine
.delete_actions(instance_scenario_id
=instance_id
)
4233 # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4234 # 1. Delete from Database
4235 message
= mydb
.delete_instance_scenario(instance_id
, tenant_id
)
4237 # 2. delete from VIM
4241 vimthread_affected
= {}
4242 net2vm_dependencies
= {}
4245 instance_action_id
= get_task_id()
4247 db_instance_action
= {
4248 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4249 "tenant_id": tenant_id
,
4250 "instance_id": instance_id
,
4251 "description": "DELETE",
4252 # "number_tasks": 0 # filled bellow
4255 # 2.1 deleting VNFFGs
4256 for sfp
in instanceDict
.get('sfps', ()):
4257 vimthread_affected
[sfp
["datacenter_tenant_id"]] = None
4258 datacenter_key
= (sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4259 if datacenter_key
not in myvims
:
4261 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfp
["datacenter_id"], sfp
["datacenter_tenant_id"])
4262 except NfvoException
as e
:
4263 logger
.error(str(e
))
4265 myvim_threads
[datacenter_key
] = myvim_thread
4266 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfp
["datacenter_id"],
4267 datacenter_tenant_id
=sfp
["datacenter_tenant_id"])
4269 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfp
["datacenter_id"], sfp
["datacenter_tenant_id"]))
4270 myvims
[datacenter_key
] = None
4272 myvims
[datacenter_key
] = next(iter(vims
.values()))
4273 myvim
= myvims
[datacenter_key
]
4274 myvim_thread
= myvim_threads
[datacenter_key
]
4277 error_msg
+= "\n vim_sfp_id={} cannot be deleted because datacenter={} not found".format(sfp
['vim_sfp_id'], sfp
["datacenter_id"])
4279 extra
= {"params": (sfp
['vim_sfp_id'])}
4281 "instance_action_id": instance_action_id
,
4282 "task_index": task_index
,
4283 "datacenter_vim_id": sfp
["datacenter_tenant_id"],
4285 "status": "SCHEDULED",
4286 "item": "instance_sfps",
4287 "item_id": sfp
["uuid"],
4288 "related": sfp
["related"],
4289 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4292 db_vim_actions
.append(db_vim_action
)
4294 for classification
in instanceDict
['classifications']:
4295 vimthread_affected
[classification
["datacenter_tenant_id"]] = None
4296 datacenter_key
= (classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4297 if datacenter_key
not in myvims
:
4299 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, classification
["datacenter_id"], classification
["datacenter_tenant_id"])
4300 except NfvoException
as e
:
4301 logger
.error(str(e
))
4303 myvim_threads
[datacenter_key
] = myvim_thread
4304 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=classification
["datacenter_id"],
4305 datacenter_tenant_id
=classification
["datacenter_tenant_id"])
4307 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(classification
["datacenter_id"],
4308 classification
["datacenter_tenant_id"]))
4309 myvims
[datacenter_key
] = None
4311 myvims
[datacenter_key
] = next(iter(vims
.values()))
4312 myvim
= myvims
[datacenter_key
]
4313 myvim_thread
= myvim_threads
[datacenter_key
]
4316 error_msg
+= "\n vim_classification_id={} cannot be deleted because datacenter={} not found".format(classification
['vim_classification_id'],
4317 classification
["datacenter_id"])
4319 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4320 extra
= {"params": (classification
['vim_classification_id']), "depends_on": depends_on
}
4322 "instance_action_id": instance_action_id
,
4323 "task_index": task_index
,
4324 "datacenter_vim_id": classification
["datacenter_tenant_id"],
4326 "status": "SCHEDULED",
4327 "item": "instance_classifications",
4328 "item_id": classification
["uuid"],
4329 "related": classification
["related"],
4330 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4333 db_vim_actions
.append(db_vim_action
)
4335 for sf
in instanceDict
.get('sfs', ()):
4336 vimthread_affected
[sf
["datacenter_tenant_id"]] = None
4337 datacenter_key
= (sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4338 if datacenter_key
not in myvims
:
4340 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sf
["datacenter_id"], sf
["datacenter_tenant_id"])
4341 except NfvoException
as e
:
4342 logger
.error(str(e
))
4344 myvim_threads
[datacenter_key
] = myvim_thread
4345 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sf
["datacenter_id"],
4346 datacenter_tenant_id
=sf
["datacenter_tenant_id"])
4348 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sf
["datacenter_id"], sf
["datacenter_tenant_id"]))
4349 myvims
[datacenter_key
] = None
4351 myvims
[datacenter_key
] = next(iter(vims
.values()))
4352 myvim
= myvims
[datacenter_key
]
4353 myvim_thread
= myvim_threads
[datacenter_key
]
4356 error_msg
+= "\n vim_sf_id={} cannot be deleted because datacenter={} not found".format(sf
['vim_sf_id'], sf
["datacenter_id"])
4358 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfps"]
4359 extra
= {"params": (sf
['vim_sf_id']), "depends_on": depends_on
}
4361 "instance_action_id": instance_action_id
,
4362 "task_index": task_index
,
4363 "datacenter_vim_id": sf
["datacenter_tenant_id"],
4365 "status": "SCHEDULED",
4366 "item": "instance_sfs",
4367 "item_id": sf
["uuid"],
4368 "related": sf
["related"],
4369 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4372 db_vim_actions
.append(db_vim_action
)
4374 for sfi
in instanceDict
.get('sfis', ()):
4375 vimthread_affected
[sfi
["datacenter_tenant_id"]] = None
4376 datacenter_key
= (sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4377 if datacenter_key
not in myvims
:
4379 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sfi
["datacenter_id"], sfi
["datacenter_tenant_id"])
4380 except NfvoException
as e
:
4381 logger
.error(str(e
))
4383 myvim_threads
[datacenter_key
] = myvim_thread
4384 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sfi
["datacenter_id"],
4385 datacenter_tenant_id
=sfi
["datacenter_tenant_id"])
4387 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sfi
["datacenter_id"], sfi
["datacenter_tenant_id"]))
4388 myvims
[datacenter_key
] = None
4390 myvims
[datacenter_key
] = next(iter(vims
.values()))
4391 myvim
= myvims
[datacenter_key
]
4392 myvim_thread
= myvim_threads
[datacenter_key
]
4395 error_msg
+= "\n vim_sfi_id={} cannot be deleted because datacenter={} not found".format(sfi
['vim_sfi_id'], sfi
["datacenter_id"])
4397 depends_on
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfs"]
4398 extra
= {"params": (sfi
['vim_sfi_id']), "depends_on": depends_on
}
4400 "instance_action_id": instance_action_id
,
4401 "task_index": task_index
,
4402 "datacenter_vim_id": sfi
["datacenter_tenant_id"],
4404 "status": "SCHEDULED",
4405 "item": "instance_sfis",
4406 "item_id": sfi
["uuid"],
4407 "related": sfi
["related"],
4408 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4411 db_vim_actions
.append(db_vim_action
)
4415 for sce_vnf
in instanceDict
.get('vnfs', ()):
4416 datacenter_key
= (sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4417 vimthread_affected
[sce_vnf
["datacenter_tenant_id"]] = None
4418 if datacenter_key
not in myvims
:
4420 _
, myvim_thread
= get_vim_thread(mydb
, tenant_id
, sce_vnf
["datacenter_id"], sce_vnf
["datacenter_tenant_id"])
4421 except NfvoException
as e
:
4422 logger
.error(str(e
))
4424 myvim_threads
[datacenter_key
] = myvim_thread
4425 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=sce_vnf
["datacenter_id"],
4426 datacenter_tenant_id
=sce_vnf
["datacenter_tenant_id"])
4428 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf
["datacenter_id"],
4429 sce_vnf
["datacenter_tenant_id"]))
4430 myvims
[datacenter_key
] = None
4432 myvims
[datacenter_key
] = next(iter(vims
.values()))
4433 myvim
= myvims
[datacenter_key
]
4434 myvim_thread
= myvim_threads
[datacenter_key
]
4436 for vm
in sce_vnf
['vms']:
4438 error_msg
+= "\n VM id={} cannot be deleted because datacenter={} not found".format(vm
['vim_vm_id'], sce_vnf
["datacenter_id"])
4440 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4442 "instance_action_id": instance_action_id
,
4443 "task_index": task_index
,
4444 "datacenter_vim_id": sce_vnf
["datacenter_tenant_id"],
4446 "status": "SCHEDULED",
4447 "item": "instance_vms",
4448 "item_id": vm
["uuid"],
4449 "related": vm
["related"],
4450 "extra": yaml
.safe_dump({"params": vm
["interfaces"], "depends_on": sfi_dependencies
},
4451 default_flow_style
=True, width
=256)
4453 db_vim_actions
.append(db_vim_action
)
4454 for interface
in vm
["interfaces"]:
4455 if not interface
.get("instance_net_id"):
4457 if interface
["instance_net_id"] not in net2vm_dependencies
:
4458 net2vm_dependencies
[interface
["instance_net_id"]] = []
4459 net2vm_dependencies
[interface
["instance_net_id"]].append(task_index
)
4464 for net
in instanceDict
['nets']:
4465 vimthread_affected
[net
["datacenter_tenant_id"]] = None
4466 datacenter_key
= (net
["datacenter_id"], net
["datacenter_tenant_id"])
4467 if datacenter_key
not in myvims
:
4469 _
,myvim_thread
= get_vim_thread(mydb
, tenant_id
, net
["datacenter_id"], net
["datacenter_tenant_id"])
4470 except NfvoException
as e
:
4471 logger
.error(str(e
))
4473 myvim_threads
[datacenter_key
] = myvim_thread
4474 vims
= get_vim(mydb
, tenant_id
, datacenter_id
=net
["datacenter_id"],
4475 datacenter_tenant_id
=net
["datacenter_tenant_id"])
4477 logger
.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net
["datacenter_id"], net
["datacenter_tenant_id"]))
4478 myvims
[datacenter_key
] = None
4480 myvims
[datacenter_key
] = next(iter(vims
.values()))
4481 myvim
= myvims
[datacenter_key
]
4482 myvim_thread
= myvim_threads
[datacenter_key
]
4485 error_msg
+= "\n Net VIM_id={} cannot be deleted because datacenter={} not found".format(net
['vim_net_id'], net
["datacenter_id"])
4487 extra
= {"params": (net
['vim_net_id'], net
['sdn_net_id'])}
4488 if net2vm_dependencies
.get(net
["uuid"]):
4489 extra
["depends_on"] = net2vm_dependencies
[net
["uuid"]]
4490 sfi_dependencies
= [action
["task_index"] for action
in db_vim_actions
if action
["item"] == "instance_sfis"]
4491 if len(sfi_dependencies
) > 0:
4492 if "depends_on" in extra
:
4493 extra
["depends_on"] += sfi_dependencies
4495 extra
["depends_on"] = sfi_dependencies
4497 "instance_action_id": instance_action_id
,
4498 "task_index": task_index
,
4499 "datacenter_vim_id": net
["datacenter_tenant_id"],
4501 "status": "SCHEDULED",
4502 "item": "instance_nets",
4503 "item_id": net
["uuid"],
4504 "related": net
["related"],
4505 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4508 db_vim_actions
.append(db_vim_action
)
4509 for sdn_net
in instanceDict
['sdn_nets']:
4510 if not sdn_net
["sdn"]:
4514 "instance_action_id": instance_action_id
,
4515 "task_index": task_index
,
4516 "wim_account_id": sdn_net
["wim_account_id"],
4518 "status": "SCHEDULED",
4519 "item": "instance_wim_nets",
4520 "item_id": sdn_net
["uuid"],
4521 "related": sdn_net
["related"],
4522 "extra": yaml
.safe_dump(extra
, default_flow_style
=True, width
=256)
4525 db_vim_actions
.append(db_vim_action
)
4527 db_instance_action
["number_tasks"] = task_index
4530 wim_actions
, db_instance_action
= (
4531 wim_engine
.incorporate_actions(wim_actions
, db_instance_action
))
4535 {"instance_actions": db_instance_action
},
4536 {"vim_wim_actions": db_vim_actions
+ wim_actions
}
4539 logger
.debug("delete_instance done DB tables: %s",
4540 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4541 mydb
.new_rows(db_tables
, ())
4542 for myvim_thread_id
in vimthread_affected
.keys():
4543 vim_threads
["running"][myvim_thread_id
].insert_task(db_vim_actions
)
4545 wim_engine
.dispatch(wim_actions
)
4547 if len(error_msg
) > 0:
4548 return 'action_id={} instance {} deleted but some elements could not be deleted, or already deleted '\
4549 '(error: 404) from VIM: {}'.format(instance_action_id
, message
, error_msg
)
4551 return "action_id={} instance {} deleted".format(instance_action_id
, message
)
4553 def get_instance_id(mydb
, tenant_id
, instance_id
):
4555 #check valid tenant_id
4556 check_tenant(mydb
, tenant_id
)
4559 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
4561 # for net in instance_dict["nets"]:
4562 # if net.get("sdn_net_id"):
4563 # net_sdn = ovim.show_network(net["sdn_net_id"])
4564 # net["sdn_info"] = {
4565 # "admin_state_up": net_sdn.get("admin_state_up"),
4566 # "flows": net_sdn.get("flows"),
4567 # "last_error": net_sdn.get("last_error"),
4568 # "ports": net_sdn.get("ports"),
4569 # "type": net_sdn.get("type"),
4570 # "status": net_sdn.get("status"),
4571 # "vlan": net_sdn.get("vlan"),
4573 return instance_dict
4575 @deprecated("Instance is automatically refreshed by vim_threads")
4576 def refresh_instance(mydb
, nfvo_tenant
, instanceDict
, datacenter
=None, vim_tenant
=None):
4577 '''Refreshes a scenario instance. It modifies instanceDict'''
4579 - 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
4582 # # Assumption: nfvo_tenant and instance_id were checked before entering into this function
4583 # #print "nfvo.refresh_instance begins"
4584 # #print json.dumps(instanceDict, indent=4)
4586 # #print "Getting the VIM URL and the VIM tenant_id"
4589 # # 1. Getting VIM vm and net list
4590 # vms_updated = [] #List of VM instance uuids in openmano that were updated
4593 # for sce_vnf in instanceDict['vnfs']:
4594 # datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
4595 # if datacenter_key not in vm_list:
4596 # vm_list[datacenter_key] = []
4597 # if datacenter_key not in myvims:
4598 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
4599 # datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
4600 # if len(vims) == 0:
4601 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
4602 # myvims[datacenter_key] = None
4604 # myvims[datacenter_key] = next(iter(vims.values()))
4605 # for vm in sce_vnf['vms']:
4606 # vm_list[datacenter_key].append(vm['vim_vm_id'])
4607 # vms_notupdated.append(vm["uuid"])
4609 # nets_updated = [] #List of VM instance uuids in openmano that were updated
4610 # nets_notupdated=[]
4612 # for net in instanceDict['nets']:
4613 # datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
4614 # if datacenter_key not in net_list:
4615 # net_list[datacenter_key] = []
4616 # if datacenter_key not in myvims:
4617 # vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
4618 # datacenter_tenant_id=net["datacenter_tenant_id"])
4619 # if len(vims) == 0:
4620 # logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
4621 # myvims[datacenter_key] = None
4623 # myvims[datacenter_key] = next(iter(vims.values()))
4625 # net_list[datacenter_key].append(net['vim_net_id'])
4626 # nets_notupdated.append(net["uuid"])
4628 # # 1. Getting the status of all VMs
4630 # for datacenter_key in myvims:
4631 # if not vm_list.get(datacenter_key):
4635 # if not myvims[datacenter_key]:
4636 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4639 # vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
4641 # except vimconn.vimconnException as e:
4642 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4643 # failed_message = str(e)
4645 # for vm in vm_list[datacenter_key]:
4646 # vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4648 # # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
4649 # for sce_vnf in instanceDict['vnfs']:
4650 # for vm in sce_vnf['vms']:
4651 # vm_id = vm['vim_vm_id']
4652 # interfaces = vm_dict[vm_id].pop('interfaces', [])
4653 # #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
4654 # has_mgmt_iface = False
4655 # for iface in vm["interfaces"]:
4656 # if iface["type"]=="mgmt":
4657 # has_mgmt_iface = True
4658 # if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
4659 # vm_dict[vm_id]['status'] = "ACTIVE"
4660 # if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
4661 # vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
4662 # 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'):
4663 # vm['status'] = vm_dict[vm_id]['status']
4664 # vm['error_msg'] = vm_dict[vm_id].get('error_msg')
4665 # vm['vim_info'] = vm_dict[vm_id].get('vim_info')
4666 # # 2.1. Update in openmano DB the VMs whose status changed
4668 # updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
4669 # vms_notupdated.remove(vm["uuid"])
4671 # vms_updated.append(vm["uuid"])
4672 # except db_base_Exception as e:
4673 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4674 # # 2.2. Update in openmano DB the interface VMs
4675 # for interface in interfaces:
4676 # #translate from vim_net_id to instance_net_id
4677 # network_id_list=[]
4678 # for net in instanceDict['nets']:
4679 # if net["vim_net_id"] == interface["vim_net_id"]:
4680 # network_id_list.append(net["uuid"])
4681 # if not network_id_list:
4683 # del interface["vim_net_id"]
4685 # for network_id in network_id_list:
4686 # mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
4687 # except db_base_Exception as e:
4688 # logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
4690 # # 3. Getting the status of all nets
4692 # for datacenter_key in myvims:
4693 # if not net_list.get(datacenter_key):
4696 # failed_message = ""
4697 # if not myvims[datacenter_key]:
4698 # failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
4701 # net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
4703 # except vimconn.vimconnException as e:
4704 # logger.error("VIM exception %s %s", type(e).__name__, str(e))
4705 # failed_message = str(e)
4707 # for net in net_list[datacenter_key]:
4708 # net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
4710 # # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
4711 # # TODO: update nets inside a vnf
4712 # for net in instanceDict['nets']:
4713 # net_id = net['vim_net_id']
4714 # if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
4715 # net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
4716 # 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'):
4717 # net['status'] = net_dict[net_id]['status']
4718 # net['error_msg'] = net_dict[net_id].get('error_msg')
4719 # net['vim_info'] = net_dict[net_id].get('vim_info')
4720 # # 5.1. Update in openmano DB the nets whose status changed
4722 # updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
4723 # nets_notupdated.remove(net["uuid"])
4725 # nets_updated.append(net["uuid"])
4726 # except db_base_Exception as e:
4727 # logger.error("nfvo.refresh_instance error database update: %s", str(e))
4729 # # Returns appropriate output
4730 # #print "nfvo.refresh_instance finishes"
4731 # logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
4732 # str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
4733 instance_id
= instanceDict
['uuid']
4734 # if len(vms_notupdated)+len(nets_notupdated)>0:
4735 # error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
4736 # return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
4738 return 0, 'Scenario instance ' + instance_id
+ ' refreshed.'
4740 def instance_action(mydb
,nfvo_tenant
,instance_id
, action_dict
):
4741 #print "Checking that the instance_id exists and getting the instance dictionary"
4742 instanceDict
= mydb
.get_instance_scenario(instance_id
, nfvo_tenant
)
4743 #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
4745 #print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
4746 vims
= get_vim(mydb
, nfvo_tenant
, instanceDict
['datacenter_id'])
4748 raise NfvoException("datacenter '{}' not found".format(str(instanceDict
['datacenter_id'])), httperrors
.Not_Found
)
4749 myvim
= next(iter(vims
.values()))
4754 myvim_threads_id
= {}
4755 if action_dict
.get("vdu-scaling"):
4756 db_instance_vms
= []
4758 db_instance_interfaces
= []
4759 instance_action_id
= get_task_id()
4760 db_instance_action
= {
4761 "uuid": instance_action_id
, # same uuid for the instance and the action on create
4762 "tenant_id": nfvo_tenant
,
4763 "instance_id": instance_id
,
4764 "description": "SCALE",
4766 vm_result
["instance_action_id"] = instance_action_id
4767 vm_result
["created"] = []
4768 vm_result
["deleted"] = []
4770 for vdu
in action_dict
["vdu-scaling"]:
4771 vdu_id
= vdu
.get("vdu-id")
4772 osm_vdu_id
= vdu
.get("osm_vdu_id")
4773 member_vnf_index
= vdu
.get("member-vnf-index")
4774 vdu_count
= vdu
.get("count", 1)
4776 target_vms
= mydb
.get_rows(
4777 FROM
="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
4778 WHERE
={"vms.uuid": vdu_id
},
4779 ORDER_BY
="vms.created_at"
4782 raise NfvoException("Cannot find the vdu with id {}".format(vdu_id
), httperrors
.Not_Found
)
4784 if not osm_vdu_id
and not member_vnf_index
:
4785 raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
4786 target_vms
= mydb
.get_rows(
4787 # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
4788 FROM
="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
4789 " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
4790 " join vms on ivms.vm_id=vms.uuid",
4791 WHERE
={"vms.osm_id": osm_vdu_id
, "svnfs.member_vnf_index": member_vnf_index
,
4792 "ivnfs.instance_scenario_id": instance_id
},
4793 ORDER_BY
="ivms.created_at"
4796 raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id
, member_vnf_index
), httperrors
.Not_Found
)
4797 vdu_id
= target_vms
[-1]["uuid"]
4798 target_vm
= target_vms
[-1]
4799 datacenter
= target_vm
["datacenter_id"]
4800 myvim_threads_id
[datacenter
], _
= get_vim_thread(mydb
, nfvo_tenant
, datacenter
)
4802 if vdu
["type"] == "delete":
4803 for index
in range(0, vdu_count
):
4804 target_vm
= target_vms
[-1-index
]
4805 vdu_id
= target_vm
["uuid"]
4807 vm_interfaces
= None
4808 for sce_vnf
in instanceDict
['vnfs']:
4809 for vm
in sce_vnf
['vms']:
4810 if vm
["uuid"] == vdu_id
:
4811 # TODO revise this should not be vm["uuid"] instance_vms["vm_id"]
4812 vm_interfaces
= vm
["interfaces"]
4816 "instance_action_id": instance_action_id
,
4817 "task_index": task_index
,
4818 "datacenter_vim_id": target_vm
["datacenter_tenant_id"],
4820 "status": "SCHEDULED",
4821 "item": "instance_vms",
4823 "related": target_vm
["related"],
4824 "extra": yaml
.safe_dump({"params": vm_interfaces
},
4825 default_flow_style
=True, width
=256)
4827 # get affected instance_interfaces (deleted on cascade) to check if a wim_network must be updated
4828 deleted_interfaces
= mydb
.get_rows(
4829 SELECT
=("instance_wim_net_id", ),
4830 FROM
="instance_interfaces",
4831 WHERE
={"instance_vm_id": vdu_id
, "instance_wim_net_id<>": None},
4833 for deleted_interface
in deleted_interfaces
:
4834 db_vim_actions
.append({"TO-UPDATE": {}, "WHERE": {
4835 "item": "instance_wim_nets", "item_id": deleted_interface
["instance_wim_net_id"]}})
4838 db_vim_actions
.append(db_vim_action
)
4839 vm_result
["deleted"].append(vdu_id
)
4840 # delete from database
4841 db_instance_vms
.append({"TO-DELETE": vdu_id
})
4843 else: # vdu["type"] == "create":
4845 where
= {"item": "instance_vms", "item_id": target_vm
["uuid"], "action": "CREATE"}
4847 vim_action_to_clone
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
=where
)
4848 if not vim_action_to_clone
:
4849 raise NfvoException("Cannot find the vim_action at database with {}".format(where
), httperrors
.Internal_Server_Error
)
4850 vim_action_to_clone
= vim_action_to_clone
[0]
4851 extra
= yaml
.safe_load(vim_action_to_clone
["extra"])
4853 # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
4854 # TODO do the same for flavor and image when available
4855 task_depends_on
= []
4856 task_params
= extra
["params"]
4857 task_params_networks
= deepcopy(task_params
[5])
4858 for iface
in task_params
[5]:
4859 if iface
["net_id"].startswith("TASK-"):
4860 if "." not in iface
["net_id"]:
4861 task_depends_on
.append("{}.{}".format(vim_action_to_clone
["instance_action_id"],
4862 iface
["net_id"][5:]))
4863 iface
["net_id"] = "TASK-{}.{}".format(vim_action_to_clone
["instance_action_id"],
4864 iface
["net_id"][5:])
4866 task_depends_on
.append(iface
["net_id"][5:])
4867 if "mac_address" in iface
:
4868 del iface
["mac_address"]
4870 vm_ifaces_to_clone
= mydb
.get_rows(FROM
="instance_interfaces", WHERE
={"instance_vm_id": target_vm
["uuid"]})
4871 for index
in range(0, vdu_count
):
4872 vm_uuid
= str(uuid4())
4873 vm_name
= target_vm
.get('vim_name')
4875 suffix
= vm_name
.rfind("-")
4876 vm_name
= vm_name
[:suffix
+1] + str(index
+ 1 + int(vm_name
[suffix
+1:]))
4882 'instance_vnf_id': target_vm
['instance_vnf_id'],
4883 'vm_id': target_vm
['vm_id'],
4884 'vim_name': vm_name
,
4886 db_instance_vms
.append(db_instance_vm
)
4888 for vm_iface
in vm_ifaces_to_clone
:
4889 iface_uuid
= str(uuid4())
4890 iface2iface
[vm_iface
["uuid"]] = iface_uuid
4893 'instance_vm_id': vm_uuid
,
4894 "instance_net_id": vm_iface
["instance_net_id"],
4895 "instance_wim_net_id": vm_iface
["instance_wim_net_id"],
4896 'interface_id': vm_iface
['interface_id'],
4897 'type': vm_iface
['type'],
4898 'model': vm_iface
['model'],
4899 'floating_ip': vm_iface
['floating_ip'],
4900 'port_security': vm_iface
['port_security']
4902 db_instance_interfaces
.append(db_vm_iface
)
4903 if db_vm_iface
["instance_wim_net_id"]:
4904 db_vim_actions
.append({"TO-UPDATE": {}, "WHERE": {
4905 "item": "instance_wim_nets", "item_id": db_vm_iface
["instance_wim_net_id"]}})
4906 task_params_copy
= deepcopy(task_params
)
4907 for iface
in task_params_copy
[5]:
4908 iface
["uuid"] = iface2iface
[iface
["uuid"]]
4909 # increment ip_address
4910 if iface
.get("ip_address"):
4912 ip
= iface
["ip_address"]
4916 ip
= ip
[i
:] + str(int(ip
[:i
]) + 1)
4917 iface
["ip_address"] = ip
4919 iface
["ip_address"] = None
4921 task_params_copy
[0] = vm_name
4923 "instance_action_id": instance_action_id
,
4924 "task_index": task_index
,
4925 "datacenter_vim_id": vim_action_to_clone
["datacenter_vim_id"],
4927 "status": "SCHEDULED",
4928 "item": "instance_vms",
4933 # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
4936 "extra": yaml
.safe_dump({"params": task_params_copy
, "depends_on": task_depends_on
}, default_flow_style
=True, width
=256)
4939 db_vim_actions
.append(db_vim_action
)
4940 vm_result
["created"].append(vm_uuid
)
4942 db_instance_action
["number_tasks"] = task_index
4944 {"instance_vms": db_instance_vms
},
4945 {"instance_interfaces": db_instance_interfaces
},
4946 {"instance_actions": db_instance_action
},
4948 # {"instance_sfis": db_instance_sfis},
4949 # {"instance_sfs": db_instance_sfs},
4950 # {"instance_classifications": db_instance_classifications},
4951 # {"instance_sfps": db_instance_sfps},
4952 {"vim_wim_actions": db_vim_actions
}
4954 logger
.debug("create_vdu done DB tables: %s",
4955 yaml
.safe_dump(db_tables
, indent
=4, default_flow_style
=False))
4956 mydb
.new_rows(db_tables
, [])
4957 for myvim_thread
in myvim_threads_id
.values():
4958 vim_threads
["running"][myvim_thread
].insert_task(db_vim_actions
)
4962 input_vnfs
= action_dict
.pop("vnfs", [])
4963 input_vms
= action_dict
.pop("vms", [])
4964 action_over_all
= True if not input_vnfs
and not input_vms
else False
4965 for sce_vnf
in instanceDict
['vnfs']:
4966 for vm
in sce_vnf
['vms']:
4967 if not action_over_all
and sce_vnf
['uuid'] not in input_vnfs
and sce_vnf
['vnf_name'] not in input_vnfs
and \
4968 sce_vnf
['member_vnf_index'] not in input_vnfs
and \
4969 vm
['uuid'] not in input_vms
and vm
['name'] not in input_vms
and \
4970 sce_vnf
['member_vnf_index'] + "-" + vm
['vdu_osm_id'] not in input_vms
: # TODO conside vm_count_index
4973 if "add_public_key" in action_dict
:
4974 if sce_vnf
.get('mgmt_access'):
4975 mgmt_access
= yaml
.load(sce_vnf
['mgmt_access'], Loader
=yaml
.Loader
)
4976 if not input_vms
and mgmt_access
.get("vdu-id") != vm
['vdu_osm_id']:
4978 default_user
= mgmt_access
.get("default-user")
4979 password
= mgmt_access
.get("password")
4980 if mgmt_access
.get(vm
['vdu_osm_id']):
4981 default_user
= mgmt_access
[vm
['vdu_osm_id']].get("default-user", default_user
)
4982 password
= mgmt_access
[vm
['vdu_osm_id']].get("password", password
)
4984 tenant
= mydb
.get_rows_by_id('nfvo_tenants', nfvo_tenant
)
4986 if 'ip_address' in vm
:
4987 mgmt_ip
= vm
['ip_address'].split(';')
4988 priv_RO_key
= decrypt_key(tenant
[0]['encrypted_RO_priv_key'], tenant
[0]['uuid'])
4989 data
= myvim
.inject_user_key(mgmt_ip
[0], action_dict
.get('user', default_user
),
4990 action_dict
['add_public_key'],
4991 password
=password
, ro_key
=priv_RO_key
)
4992 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
4993 "description": "Public key injected",
4997 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
4998 httperrors
.Internal_Server_Error
)
5000 raise NfvoException("Unable to inject ssh key in vm: {} - Aborting".format(vm
['uuid']),
5001 httperrors
.Internal_Server_Error
)
5003 data
= myvim
.action_vminstance(vm
['vim_vm_id'], action_dict
)
5004 if "console" in action_dict
:
5005 if not global_config
["http_console_proxy"]:
5006 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
5007 "description": "{protocol}//{ip}:{port}/{suffix}".format(
5008 protocol
=data
["protocol"],
5009 ip
= data
["server"],
5010 port
= data
["port"],
5011 suffix
= data
["suffix"]),
5015 elif data
["server"]=="127.0.0.1" or data
["server"]=="localhost":
5016 vm_result
[ vm
['uuid'] ] = {"vim_result": -httperrors
.Unauthorized
,
5017 "description": "this console is only reachable by local interface",
5022 #print "console data", data
5024 console_thread
= create_or_use_console_proxy_thread(data
["server"], data
["port"])
5025 vm_result
[ vm
['uuid'] ] = {"vim_result": 200,
5026 "description": "{protocol}//{ip}:{port}/{suffix}".format(
5027 protocol
=data
["protocol"],
5028 ip
= global_config
["http_console_host"],
5029 port
= console_thread
.port
,
5030 suffix
= data
["suffix"]),
5034 except NfvoException
as e
:
5035 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
5039 vm_result
[ vm
['uuid'] ] = {"vim_result": 200, "description": "ok", "name":vm
['name']}
5041 except vimconn
.vimconnException
as e
:
5042 vm_result
[ vm
['uuid'] ] = {"vim_result": e
.http_code
, "name":vm
['name'], "description": str(e
)}
5045 if vm_ok
==0: #all goes wrong
5050 def instance_action_get(mydb
, nfvo_tenant
, instance_id
, action_id
):
5052 if nfvo_tenant
and nfvo_tenant
!= "any":
5053 filter["tenant_id"] = nfvo_tenant
5054 if instance_id
and instance_id
!= "any":
5055 filter["instance_id"] = instance_id
5057 filter["uuid"] = action_id
5058 rows
= mydb
.get_rows(FROM
="instance_actions", WHERE
=filter)
5061 raise NfvoException("Not found any action with this criteria", httperrors
.Not_Found
)
5062 vim_wim_actions
= mydb
.get_rows(FROM
="vim_wim_actions", WHERE
={"instance_action_id": action_id
})
5063 rows
[0]["vim_wim_actions"] = vim_wim_actions
5064 # for backward compatibility set vim_actions = vim_wim_actions
5065 rows
[0]["vim_actions"] = vim_wim_actions
5066 return {"actions": rows
}
5069 def create_or_use_console_proxy_thread(console_server
, console_port
):
5070 #look for a non-used port
5071 console_thread_key
= console_server
+ ":" + str(console_port
)
5072 if console_thread_key
in global_config
["console_thread"]:
5073 #global_config["console_thread"][console_thread_key].start_timeout()
5074 return global_config
["console_thread"][console_thread_key
]
5076 for port
in global_config
["console_port_iterator"]():
5077 #print "create_or_use_console_proxy_thread() port:", port
5078 if port
in global_config
["console_ports"]:
5081 clithread
= cli
.ConsoleProxyThread(global_config
['http_host'], port
, console_server
, console_port
)
5083 global_config
["console_thread"][console_thread_key
] = clithread
5084 global_config
["console_ports"][port
] = console_thread_key
5086 except cli
.ConsoleProxyExceptionPortUsed
as e
:
5087 #port used, try with onoher
5089 except cli
.ConsoleProxyException
as e
:
5090 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5091 raise NfvoException("Not found any free 'http_console_ports'", httperrors
.Conflict
)
5094 def check_tenant(mydb
, tenant_id
):
5095 '''check that tenant exists at database'''
5096 tenant
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=('uuid',), WHERE
={'uuid': tenant_id
})
5098 raise NfvoException("tenant '{}' not found".format(tenant_id
), httperrors
.Not_Found
)
5101 def new_tenant(mydb
, tenant_dict
):
5103 tenant_uuid
= str(uuid4())
5104 tenant_dict
['uuid'] = tenant_uuid
5106 pub_key
, priv_key
= create_RO_keypair(tenant_uuid
)
5107 tenant_dict
['RO_pub_key'] = pub_key
5108 tenant_dict
['encrypted_RO_priv_key'] = priv_key
5109 mydb
.new_row("nfvo_tenants", tenant_dict
, confidential_data
=True)
5110 except db_base_Exception
as e
:
5111 raise NfvoException("Error creating the new tenant: {} ".format(tenant_dict
['name']) + str(e
), e
.http_code
)
5114 def delete_tenant(mydb
, tenant
):
5115 #get nfvo_tenant info
5117 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant
, 'tenant')
5118 mydb
.delete_row_by_id("nfvo_tenants", tenant_dict
['uuid'])
5119 return tenant_dict
['uuid'] + " " + tenant_dict
["name"]
5122 def new_datacenter(mydb
, datacenter_descriptor
):
5123 sdn_port_mapping
= None
5124 if "config" in datacenter_descriptor
:
5125 sdn_port_mapping
= datacenter_descriptor
["config"].pop("sdn-port-mapping", None)
5126 datacenter_descriptor
["config"] = yaml
.safe_dump(datacenter_descriptor
["config"], default_flow_style
=True,
5128 # Check that datacenter-type is correct
5129 datacenter_type
= datacenter_descriptor
.get("type", "openvim");
5130 # module_info = None
5132 for url_field
in ('vim_url', 'vim_url_admin'):
5133 # It is common that users copy and paste the URL from the VIM website
5134 # (example OpenStack), therefore a common mistake is to include blank
5135 # characters at the end of the URL. Let's remove it and just in case,
5136 # lets remove trailing slash as well.
5137 url
= datacenter_descriptor
.get(url_field
)
5139 datacenter_descriptor
[url_field
] = url
.strip(string
.whitespace
+ '/')
5142 plugin_name
= "rovim_" + datacenter_type
5143 if plugin_name
not in plugins
:
5144 _load_plugin(plugin_name
, type="vim")
5146 datacenter_id
= mydb
.new_row("datacenters", datacenter_descriptor
, add_uuid
=True, confidential_data
=True)
5147 if sdn_port_mapping
:
5149 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, sdn_port_mapping
)
5150 except Exception as e
:
5151 mydb
.delete_row_by_id("datacenters", datacenter_id
) # Rollback
5153 return datacenter_id
5156 def edit_datacenter(mydb
, datacenter_id_name
, datacenter_descriptor
):
5157 # obtain data, check that only one exist
5158 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id_name
)
5161 datacenter_id
= datacenter
['uuid']
5162 where
= {'uuid': datacenter
['uuid']}
5163 remove_port_mapping
= False
5164 new_sdn_port_mapping
= None
5165 if "config" in datacenter_descriptor
:
5166 if datacenter_descriptor
['config'] != None:
5168 new_config_dict
= datacenter_descriptor
["config"]
5169 if "sdn-port-mapping" in new_config_dict
:
5170 remove_port_mapping
= True
5171 new_sdn_port_mapping
= new_config_dict
.pop("sdn-port-mapping")
5172 # delete null fields
5174 for k
in new_config_dict
:
5175 if new_config_dict
[k
] is None:
5177 if k
== 'sdn-controller':
5178 remove_port_mapping
= True
5180 config_text
= datacenter
.get("config")
5183 config_dict
= yaml
.load(config_text
, Loader
=yaml
.Loader
)
5184 config_dict
.update(new_config_dict
)
5185 # delete null fields
5188 except Exception as e
:
5189 raise NfvoException("Bad format at datacenter:config " + str(e
), httperrors
.Bad_Request
)
5191 datacenter_descriptor
["config"] = yaml
.safe_dump(config_dict
, default_flow_style
=True, width
=256)
5193 datacenter_descriptor
["config"] = None
5194 if remove_port_mapping
:
5196 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_id
)
5197 except ovimException
as e
:
5198 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
5200 mydb
.update_rows('datacenters', datacenter_descriptor
, where
)
5201 if new_sdn_port_mapping
:
5203 datacenter_sdn_port_mapping_set(mydb
, None, datacenter_id
, new_sdn_port_mapping
)
5204 except ovimException
as e
:
5206 mydb
.update_rows('datacenters', datacenter
, where
)
5207 raise NfvoException("Error adding datacenter-port-mapping " + str(e
), httperrors
.Conflict
)
5208 return datacenter_id
5211 def delete_datacenter(mydb
, datacenter
):
5212 #get nfvo_tenant info
5213 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter
, 'datacenter')
5214 mydb
.delete_row_by_id("datacenters", datacenter_dict
['uuid'])
5216 datacenter_sdn_port_mapping_delete(mydb
, None, datacenter_dict
['uuid'])
5217 except ovimException
as e
:
5218 raise NfvoException("Error deleting datacenter-port-mapping " + str(e
))
5219 return datacenter_dict
['uuid'] + " " + datacenter_dict
['name']
5222 def create_vim_account(mydb
, nfvo_tenant
, datacenter_id
, name
=None, vim_id
=None, vim_tenant
=None, vim_tenant_name
=None,
5223 vim_username
=None, vim_password
=None, config
=None):
5224 # get datacenter info
5226 if not datacenter_id
:
5228 raise NfvoException("You must provide 'vim_id", http_code
=httperrors
.Bad_Request
)
5229 datacenter_id
= vim_id
5230 datacenter_id
, datacenter_name
= get_datacenter_uuid(mydb
, None, datacenter_id
)
5232 create_vim_tenant
= True if not vim_tenant
and not vim_tenant_name
else False
5234 # get nfvo_tenant info
5235 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant
)
5236 if vim_tenant_name
==None:
5237 vim_tenant_name
=tenant_dict
['name']
5239 tenants_datacenter_dict
={"nfvo_tenant_id":tenant_dict
['uuid'], "datacenter_id":datacenter_id
}
5240 # #check that this association does not exist before
5241 # tenants_datacenters = mydb.get_rows(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
5242 # if len(tenants_datacenters)>0:
5243 # raise NfvoException("datacenter '{}' and tenant'{}' are already attached".format(datacenter_id, tenant_dict['uuid']), httperrors.Conflict)
5245 vim_tenant_id_exist_atdb
=False
5246 if not create_vim_tenant
:
5247 where_
={"datacenter_id": datacenter_id
}
5248 if vim_tenant
!=None:
5249 where_
["vim_tenant_id"] = vim_tenant
5250 if vim_tenant_name
!=None:
5251 where_
["vim_tenant_name"] = vim_tenant_name
5252 #check if vim_tenant_id is already at database
5253 datacenter_tenants_dict
= mydb
.get_rows(FROM
='datacenter_tenants', WHERE
=where_
)
5254 if len(datacenter_tenants_dict
)>=1:
5255 datacenter_tenants_dict
= datacenter_tenants_dict
[0]
5256 vim_tenant_id_exist_atdb
=True
5257 #TODO check if a field has changed and edit entry at datacenter_tenants at DB
5259 datacenter_tenants_dict
= {}
5260 #insert at table datacenter_tenants
5261 else: #if vim_tenant==None:
5262 #create tenant at VIM if not provided
5264 _
, myvim
= get_datacenter_by_name_uuid(mydb
, None, datacenter_id
, vim_user
=vim_username
,
5265 vim_passwd
=vim_password
)
5266 datacenter_name
= myvim
["name"]
5267 vim_tenant
= myvim
.new_tenant(vim_tenant_name
, "created by openmano for datacenter "+datacenter_name
)
5268 except vimconn
.vimconnException
as e
:
5269 raise NfvoException("Not possible to create vim_tenant {} at VIM: {}".format(vim_tenant_name
, e
),
5270 httperrors
.Internal_Server_Error
)
5271 datacenter_tenants_dict
= {}
5272 datacenter_tenants_dict
["created"]="true"
5274 #fill datacenter_tenants table
5275 if not vim_tenant_id_exist_atdb
:
5276 datacenter_tenants_dict
["vim_tenant_id"] = vim_tenant
5277 datacenter_tenants_dict
["vim_tenant_name"] = vim_tenant_name
5278 datacenter_tenants_dict
["user"] = vim_username
5279 datacenter_tenants_dict
["passwd"] = vim_password
5280 datacenter_tenants_dict
["datacenter_id"] = datacenter_id
5282 datacenter_tenants_dict
["name"] = name
5284 datacenter_tenants_dict
["name"] = datacenter_name
5286 datacenter_tenants_dict
["config"] = yaml
.safe_dump(config
, default_flow_style
=True, width
=256)
5287 id_
= mydb
.new_row('datacenter_tenants', datacenter_tenants_dict
, add_uuid
=True, confidential_data
=True)
5288 datacenter_tenants_dict
["uuid"] = id_
5290 #fill tenants_datacenters table
5291 datacenter_tenant_id
= datacenter_tenants_dict
["uuid"]
5292 tenants_datacenter_dict
["datacenter_tenant_id"] = datacenter_tenant_id
5293 mydb
.new_row('tenants_datacenters', tenants_datacenter_dict
)
5296 thread_name
= get_non_used_vim_name(datacenter_name
, datacenter_id
)
5297 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, None, datacenter_tenant_id
, db
=db
)
5299 thread_id
= datacenter_tenants_dict
["uuid"]
5300 vim_threads
["running"][thread_id
] = new_thread
5302 except vimconn
.vimconnException
as e
:
5303 raise NfvoException(str(e
), httperrors
.Bad_Request
)
5306 def edit_vim_account(mydb
, nfvo_tenant
, datacenter_tenant_id
, datacenter_id
=None, name
=None, vim_tenant
=None,
5307 vim_tenant_name
=None, vim_username
=None, vim_password
=None, config
=None):
5309 # get vim_account; check is valid for this tenant
5310 from_
= "datacenter_tenants as dt JOIN tenants_datacenters as td ON dt.uuid=td.datacenter_tenant_id"
5311 where_
= {"td.nfvo_tenant_id": nfvo_tenant
}
5312 if datacenter_tenant_id
:
5313 where_
["dt.uuid"] = datacenter_tenant_id
5315 where_
["dt.datacenter_id"] = datacenter_id
5316 vim_accounts
= mydb
.get_rows(SELECT
="dt.uuid as uuid, config", FROM
=from_
, WHERE
=where_
)
5317 if not vim_accounts
:
5318 raise NfvoException("vim_account not found for this tenant", http_code
=httperrors
.Not_Found
)
5319 elif len(vim_accounts
) > 1:
5320 raise NfvoException("found more than one vim_account for this tenant", http_code
=httperrors
.Conflict
)
5321 datacenter_tenant_id
= vim_accounts
[0]["uuid"]
5322 original_config
= vim_accounts
[0]["config"]
5326 original_config_dict
= yaml
.load(original_config
, Loader
=yaml
.Loader
)
5327 original_config_dict
.update(config
)
5328 update_
["config"] = yaml
.safe_dump(original_config_dict
, default_flow_style
=True, width
=256)
5330 update_
['name'] = name
5332 update_
['vim_tenant_id'] = vim_tenant
5334 update_
['vim_tenant_name'] = vim_tenant_name
5336 update_
['user'] = vim_username
5338 update_
['passwd'] = vim_password
5340 mydb
.update_rows("datacenter_tenants", UPDATE
=update_
, WHERE
={"uuid": datacenter_tenant_id
})
5342 vim_threads
["running"][datacenter_tenant_id
].insert_task("reload")
5343 return datacenter_tenant_id
5345 def delete_vim_account(mydb
, tenant_id
, vim_account_id
, datacenter
=None):
5346 #get nfvo_tenant info
5347 if not tenant_id
or tenant_id
=="any":
5350 tenant_dict
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
5351 tenant_uuid
= tenant_dict
['uuid']
5353 #check that this association exist before
5354 tenants_datacenter_dict
= {}
5356 datacenter_id
, _
= get_datacenter_uuid(mydb
, tenant_uuid
, datacenter
)
5357 tenants_datacenter_dict
["datacenter_id"] = datacenter_id
5358 elif vim_account_id
:
5359 tenants_datacenter_dict
["datacenter_tenant_id"] = vim_account_id
5361 tenants_datacenter_dict
["nfvo_tenant_id"] = tenant_uuid
5362 tenant_datacenter_list
= mydb
.get_rows(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5363 if len(tenant_datacenter_list
)==0 and tenant_uuid
:
5364 raise NfvoException("datacenter '{}' and tenant '{}' are not attached".format(datacenter_id
, tenant_dict
['uuid']), httperrors
.Not_Found
)
5366 #delete this association
5367 mydb
.delete_row(FROM
='tenants_datacenters', WHERE
=tenants_datacenter_dict
)
5369 #get vim_tenant info and deletes
5371 for tenant_datacenter_item
in tenant_datacenter_list
:
5372 vim_tenant_dict
= mydb
.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5373 #try to delete vim:tenant
5375 mydb
.delete_row_by_id('datacenter_tenants', tenant_datacenter_item
['datacenter_tenant_id'])
5376 if vim_tenant_dict
['created']=='true':
5377 #delete tenant at VIM if created by NFVO
5379 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5380 myvim
.delete_tenant(vim_tenant_dict
['vim_tenant_id'])
5381 except vimconn
.vimconnException
as e
:
5382 warning
= "Not possible to delete vim_tenant_id {} from VIM: {} ".format(vim_tenant_dict
['vim_tenant_id'], str(e
))
5383 logger
.warn(warning
)
5384 except db_base_Exception
as e
:
5385 logger
.error("Cannot delete datacenter_tenants " + str(e
))
5386 pass # the error will be caused because dependencies, vim_tenant can not be deleted
5387 thread_id
= tenant_datacenter_item
["datacenter_tenant_id"]
5388 thread
= vim_threads
["running"].get(thread_id
)
5390 thread
.insert_task("exit")
5391 vim_threads
["deleting"][thread_id
] = thread
5392 return "datacenter {} detached. {}".format(datacenter_id
, warning
)
5395 def datacenter_action(mydb
, tenant_id
, datacenter
, action_dict
):
5397 #get datacenter info
5398 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5400 if 'check-connectivity' in action_dict
:
5402 myvim
.check_vim_connectivity()
5403 except vimconn
.vimconnException
as e
:
5404 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5405 raise NfvoException(str(e
), e
.http_code
)
5406 elif 'net-update' in action_dict
:
5408 nets
= myvim
.get_network_list(filter_dict
={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
5410 except vimconn
.vimconnException
as e
:
5411 #logger.error("nfvo.datacenter_action() Not possible to get_network_list from VIM: %s ", str(e))
5412 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5413 #update nets Change from VIM format to NFVO format
5416 net_nfvo
={'datacenter_id': datacenter_id
}
5417 net_nfvo
['name'] = net
['name']
5418 #net_nfvo['description']= net['name']
5419 net_nfvo
['vim_net_id'] = net
['id']
5420 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5421 net_nfvo
['shared'] = net
['shared']
5422 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5423 net_list
.append(net_nfvo
)
5424 inserted
, deleted
= mydb
.update_datacenter_nets(datacenter_id
, net_list
)
5425 logger
.info("Inserted %d nets, deleted %d old nets", inserted
, deleted
)
5427 elif 'net-edit' in action_dict
:
5428 net
= action_dict
['net-edit'].pop('net')
5429 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5430 result
= mydb
.update_rows('datacenter_nets', action_dict
['net-edit'],
5431 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5433 elif 'net-delete' in action_dict
:
5434 net
= action_dict
['net-deelte'].get('net')
5435 what
= 'vim_net_id' if utils
.check_valid_uuid(net
) else 'name'
5436 result
= mydb
.delete_row(FROM
='datacenter_nets',
5437 WHERE
={'datacenter_id':datacenter_id
, what
: net
})
5441 raise NfvoException("Unknown action " + str(action_dict
), httperrors
.Bad_Request
)
5444 def datacenter_edit_netmap(mydb
, tenant_id
, datacenter
, netmap
, action_dict
):
5445 #get datacenter info
5446 datacenter_id
, _
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5448 what
= 'uuid' if utils
.check_valid_uuid(netmap
) else 'name'
5449 result
= mydb
.update_rows('datacenter_nets', action_dict
['netmap'],
5450 WHERE
={'datacenter_id':datacenter_id
, what
: netmap
})
5454 def datacenter_new_netmap(mydb
, tenant_id
, datacenter
, action_dict
=None):
5455 #get datacenter info
5456 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5459 action_dict
= action_dict
["netmap"]
5460 if 'vim_id' in action_dict
:
5461 filter_dict
["id"] = action_dict
['vim_id']
5462 if 'vim_name' in action_dict
:
5463 filter_dict
["name"] = action_dict
['vim_name']
5465 filter_dict
["shared"] = True
5468 vim_nets
= myvim
.get_network_list(filter_dict
=filter_dict
)
5469 except vimconn
.vimconnException
as e
:
5470 #logger.error("nfvo.datacenter_new_netmap() Not possible to get_network_list from VIM: %s ", str(e))
5471 raise NfvoException(str(e
), httperrors
.Internal_Server_Error
)
5472 if len(vim_nets
)>1 and action_dict
:
5473 raise NfvoException("more than two networks found, specify with vim_id", httperrors
.Conflict
)
5474 elif len(vim_nets
)==0: # and action_dict:
5475 raise NfvoException("Not found a network at VIM with " + str(filter_dict
), httperrors
.Not_Found
)
5477 for net
in vim_nets
:
5478 net_nfvo
={'datacenter_id': datacenter_id
}
5479 if action_dict
and "name" in action_dict
:
5480 net_nfvo
['name'] = action_dict
['name']
5482 net_nfvo
['name'] = net
['name']
5483 #net_nfvo['description']= net['name']
5484 net_nfvo
['vim_net_id'] = net
['id']
5485 net_nfvo
['type'] = net
['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man') to ('bridge','data','ptp')
5486 net_nfvo
['shared'] = net
['shared']
5487 net_nfvo
['multipoint'] = False if net
['type']=='ptp' else True
5489 net_id
= mydb
.new_row("datacenter_nets", net_nfvo
, add_uuid
=True)
5490 net_nfvo
["status"] = "OK"
5491 net_nfvo
["uuid"] = net_id
5492 except db_base_Exception
as e
:
5496 net_nfvo
["status"] = "FAIL: " + str(e
)
5497 net_list
.append(net_nfvo
)
5500 def get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
):
5501 # obtain all network data
5503 if utils
.check_valid_uuid(network_id
):
5504 filter_dict
= {"id": network_id
}
5506 filter_dict
= {"name": network_id
}
5508 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5509 network
= myvim
.get_network_list(filter_dict
=filter_dict
)
5510 except vimconn
.vimconnException
as e
:
5511 raise NfvoException("Not possible to get_sdn_net_id from VIM: {}".format(str(e
)), e
.http_code
)
5513 # ensure the network is defined
5514 if len(network
) == 0:
5515 raise NfvoException("Network {} is not present in the system".format(network_id
),
5516 httperrors
.Bad_Request
)
5518 # ensure there is only one network with the provided name
5519 if len(network
) > 1:
5520 raise NfvoException("Multiple networks present in vim identified by {}".format(network_id
), httperrors
.Bad_Request
)
5522 # ensure it is a dataplane network
5523 if network
[0]['type'] != 'data':
5526 # ensure we use the id
5527 network_id
= network
[0]['id']
5529 # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id
5530 # and with instance_scenario_id==NULL
5531 #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None}
5532 search_dict
= {'vim_net_id': network_id
}
5535 #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id']
5536 result
= mydb
.get_rows(SELECT
=('sdn_net_id',), FROM
='instance_nets', WHERE
=search_dict
)
5537 except db_base_Exception
as e
:
5538 raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format(
5539 network_id
) + str(e
), e
.http_code
)
5543 if net
['sdn_net_id'] != None:
5545 sdn_net_id
= net
['sdn_net_id']
5547 if sdn_net_counter
== 0:
5549 elif sdn_net_counter
== 1:
5552 raise NfvoException("More than one SDN network is associated to vim network {}".format(
5553 network_id
), httperrors
.Internal_Server_Error
)
5555 def get_sdn_controller_id(mydb
, datacenter
):
5556 # Obtain sdn controller id
5557 config
= mydb
.get_rows(SELECT
=('config',), FROM
='datacenters', WHERE
={'uuid': datacenter
})[0].get('config', '{}')
5561 return yaml
.load(config
, Loader
=yaml
.Loader
).get('sdn-controller')
5563 def vim_net_sdn_attach(mydb
, tenant_id
, datacenter
, network_id
, descriptor
):
5565 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5566 if not sdn_network_id
:
5567 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
), httperrors
.Internal_Server_Error
)
5569 #Obtain sdn controller id
5570 controller_id
= get_sdn_controller_id(mydb
, datacenter
)
5571 if not controller_id
:
5572 raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter
), httperrors
.Internal_Server_Error
)
5574 #Obtain sdn controller info
5575 sdn_controller
= ovim
.show_of_controller(controller_id
)
5578 'name': 'external_port',
5579 'net_id': sdn_network_id
,
5580 'ofc_id': controller_id
,
5581 'switch_dpid': sdn_controller
['dpid'],
5582 'switch_port': descriptor
['port']
5585 if 'vlan' in descriptor
:
5586 port_data
['vlan'] = descriptor
['vlan']
5587 if 'mac' in descriptor
:
5588 port_data
['mac'] = descriptor
['mac']
5590 result
= ovim
.new_port(port_data
)
5591 except ovimException
as e
:
5592 raise NfvoException("ovimException attaching SDN network {} to vim network {}".format(
5593 sdn_network_id
, network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5594 except db_base_Exception
as e
:
5595 raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format(
5596 network_id
) + str(e
), e
.http_code
)
5598 return 'Port uuid: '+ result
5600 def vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, network_id
, port_id
=None):
5602 filter = {'uuid': port_id
}
5604 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, network_id
)
5605 if not sdn_network_id
:
5606 raise NfvoException("No SDN network is associated to vim-network {}".format(network_id
),
5607 httperrors
.Internal_Server_Error
)
5608 #in case no port_id is specified only ports marked as 'external_port' will be detached
5609 filter = {'name': 'external_port', 'net_id': sdn_network_id
}
5612 port_list
= ovim
.get_ports(columns
={'uuid'}, filter=filter)
5613 except ovimException
as e
:
5614 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
),
5615 httperrors
.Internal_Server_Error
)
5617 if len(port_list
) == 0:
5618 raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id
),
5619 httperrors
.Bad_Request
)
5622 for port
in port_list
:
5624 port_uuid_list
.append(port
['uuid'])
5625 ovim
.delete_port(port
['uuid'])
5626 except ovimException
as e
:
5627 raise NfvoException("ovimException deleting port {} for net {}. ".format(port
['uuid'], network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5629 return 'Detached ports uuid: {}'.format(','.join(port_uuid_list
))
5631 def vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
):
5632 #get datacenter info
5633 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5636 if utils
.check_valid_uuid(name
):
5637 filter_dict
["id"] = name
5639 filter_dict
["name"] = name
5641 if item
=="networks":
5642 #filter_dict['tenant_id'] = myvim['tenant_id']
5643 content
= myvim
.get_network_list(filter_dict
=filter_dict
)
5645 if len(content
) == 0:
5646 raise NfvoException("Network {} is not present in the system. ".format(name
),
5647 httperrors
.Bad_Request
)
5649 #Update the networks with the attached ports
5651 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, net
['id'])
5652 if sdn_network_id
!= None:
5654 #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id})
5655 port_list
= ovim
.get_ports(columns
={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id
})
5656 except ovimException
as e
:
5657 raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id
) + str(e
), httperrors
.Internal_Server_Error
)
5658 #Remove field name and if port name is external_port save it as 'type'
5659 for port
in port_list
:
5660 if port
['name'] == 'external_port':
5661 port
['type'] = "External"
5663 net
['sdn_network_id'] = sdn_network_id
5664 net
['sdn_attached_ports'] = port_list
5666 elif item
=="tenants":
5667 content
= myvim
.get_tenant_list(filter_dict
=filter_dict
)
5668 elif item
== "images":
5670 content
= myvim
.get_image_list(filter_dict
=filter_dict
)
5672 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5673 logger
.debug("vim_action response %s", content
) #update nets Change from VIM format to NFVO format
5674 if name
and len(content
)==1:
5675 return {item
[:-1]: content
[0]}
5676 elif name
and len(content
)==0:
5677 raise NfvoException("No {} found with ".format(item
[:-1]) + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), filter_dict
.items())),
5680 return {item
: content
}
5681 except vimconn
.vimconnException
as e
:
5682 print("vim_action Not possible to get_{}_list from VIM: {} ".format(item
, str(e
)))
5683 raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item
, str(e
)), e
.http_code
)
5686 def vim_action_delete(mydb
, tenant_id
, datacenter
, item
, name
):
5687 #get datacenter info
5688 if tenant_id
== "any":
5691 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5693 content
= vim_action_get(mydb
, tenant_id
, datacenter
, item
, name
)
5694 logger
.debug("vim_action_delete vim response: " + str(content
))
5695 items
= next(iter(content
.values()))
5696 if type(items
)==list and len(items
)==0:
5697 raise NfvoException("Not found " + item
, httperrors
.Not_Found
)
5698 elif type(items
)==list and len(items
)>1:
5699 raise NfvoException("Found more than one {} with this name. Use uuid.".format(item
), httperrors
.Not_Found
)
5700 else: # it is a dict
5701 item_id
= items
["id"]
5702 item_name
= str(items
.get("name"))
5705 if item
=="networks":
5706 # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it
5707 sdn_network_id
= get_sdn_net_id(mydb
, tenant_id
, datacenter
, item_id
)
5708 if sdn_network_id
!= None:
5709 #Delete any port attachment to this network
5711 port_list
= ovim
.get_ports(columns
={'uuid'}, filter={'net_id': sdn_network_id
})
5712 except ovimException
as e
:
5713 raise NfvoException(
5714 "ovimException obtaining external ports for net {}. ".format(sdn_network_id
) + str(e
),
5715 httperrors
.Internal_Server_Error
)
5717 # By calling one by one all ports to be detached we ensure that not only the external_ports get detached
5718 for port
in port_list
:
5719 vim_net_sdn_detach(mydb
, tenant_id
, datacenter
, item_id
, port
['uuid'])
5721 #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id
5723 mydb
.delete_row(FROM
='instance_nets', WHERE
={'instance_scenario_id': None,
5724 'sdn_net_id': sdn_network_id
,
5725 'vim_net_id': item_id
})
5726 except db_base_Exception
as e
:
5727 raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: {}".format(
5728 item_id
, e
), e
.http_code
)
5730 #Delete the SDN network
5732 ovim
.delete_network(sdn_network_id
)
5733 except ovimException
as e
:
5734 logger
.error("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
), exc_info
=True)
5735 raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id
) + str(e
),
5736 httperrors
.Internal_Server_Error
)
5738 content
= myvim
.delete_network(item_id
)
5739 elif item
=="tenants":
5740 content
= myvim
.delete_tenant(item_id
)
5741 elif item
== "images":
5742 content
= myvim
.delete_image(item_id
)
5744 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5745 except vimconn
.vimconnException
as e
:
5746 #logger.error( "vim_action Not possible to delete_{} {}from VIM: {} ".format(item, name, str(e)))
5747 raise NfvoException("Not possible to delete_{} {} from VIM: {}".format(item
, name
, str(e
)), e
.http_code
)
5749 return "{} {} {} deleted".format(item
[:-1], item_id
,item_name
)
5752 def vim_action_create(mydb
, tenant_id
, datacenter
, item
, descriptor
):
5753 #get datacenter info
5754 logger
.debug("vim_action_create descriptor %s", str(descriptor
))
5755 if tenant_id
== "any":
5757 datacenter_id
, myvim
= get_datacenter_by_name_uuid(mydb
, tenant_id
, datacenter
)
5759 if item
=="networks":
5760 net
= descriptor
["network"]
5761 net_name
= net
.pop("name")
5762 net_type
= net
.pop("type", "bridge")
5763 net_public
= net
.pop("shared", False)
5764 net_ipprofile
= net
.pop("ip_profile", None)
5765 net_vlan
= net
.pop("vlan", None)
5766 net_provider_network_profile
= None
5768 net_provider_network_profile
= {"segmentation-id": net_vlan
}
5769 content
, _
= myvim
.new_network(net_name
, net_type
, net_ipprofile
, shared
=net_public
, provider_network_profile
=net_provider_network_profile
) #, **net)
5771 #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network
5772 if get_sdn_controller_id(mydb
, datacenter
) != None and (net_type
== 'data' or net_type
== 'ptp'):
5773 #obtain datacenter_tenant_id
5774 datacenter_tenant_id
= mydb
.get_rows(SELECT
=('uuid',),
5775 FROM
='datacenter_tenants',
5776 WHERE
={'datacenter_id': datacenter
})[0]['uuid']
5779 sdn_network
['vlan'] = net_vlan
5780 sdn_network
['type'] = net_type
5781 sdn_network
['name'] = net_name
5782 sdn_network
['region'] = datacenter_tenant_id
5783 ovim_content
= ovim
.new_network(sdn_network
)
5784 except ovimException
as e
:
5785 logger
.error("ovimException creating SDN network={} ".format(
5786 sdn_network
) + str(e
), exc_info
=True)
5787 raise NfvoException("ovimException creating SDN network={} ".format(sdn_network
) + str(e
),
5788 httperrors
.Internal_Server_Error
)
5790 # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id
5791 # use instance_scenario_id=None to distinguish from real instaces of nets
5792 correspondence
= {'instance_scenario_id': None,
5793 'sdn_net_id': ovim_content
,
5794 'vim_net_id': content
,
5795 'datacenter_tenant_id': datacenter_tenant_id
5798 mydb
.new_row('instance_nets', correspondence
, add_uuid
=True)
5799 except db_base_Exception
as e
:
5800 raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: {}".format(
5801 correspondence
, e
), e
.http_code
)
5802 elif item
=="tenants":
5803 tenant
= descriptor
["tenant"]
5804 content
= myvim
.new_tenant(tenant
["name"], tenant
.get("description"))
5806 raise NfvoException(item
+ "?", httperrors
.Method_Not_Allowed
)
5807 except vimconn
.vimconnException
as e
:
5808 raise NfvoException("Not possible to create {} at VIM: {}".format(item
, str(e
)), e
.http_code
)
5810 return vim_action_get(mydb
, tenant_id
, datacenter
, item
, content
)
5812 def sdn_controller_create(mydb
, tenant_id
, sdn_controller
):
5814 wim_id
= ovim
.new_of_controller(sdn_controller
)
5816 # Load plugin if not previously loaded
5817 controller_type
= sdn_controller
.get("type")
5818 plugin_name
= "rosdn_" + controller_type
5819 if plugin_name
not in plugins
:
5820 _load_plugin(plugin_name
, type="sdn")
5822 thread_name
= get_non_used_vim_name(sdn_controller
['name'], wim_id
)
5823 new_thread
= vim_thread(task_lock
, plugins
, thread_name
, wim_id
, None, db
=db
)
5826 vim_threads
["running"][thread_id
] = new_thread
5827 logger
.debug('New SDN controller created with uuid {}'.format(wim_id
))
5829 except ovimException
as e
:
5830 raise NfvoException(e
) from e
5832 def sdn_controller_update(mydb
, tenant_id
, controller_id
, sdn_controller
):
5833 data
= ovim
.edit_of_controller(controller_id
, sdn_controller
)
5834 msg
= 'SDN controller {} updated'.format(data
)
5835 vim_threads
["running"][controller_id
].insert_task("reload")
5839 def sdn_controller_list(mydb
, tenant_id
, controller_id
=None):
5840 if controller_id
== None:
5841 data
= ovim
.get_of_controllers()
5843 data
= ovim
.show_of_controller(controller_id
)
5845 msg
= 'SDN controller list:\n {}'.format(data
)
5849 def sdn_controller_delete(mydb
, tenant_id
, controller_id
):
5850 select_
= ('uuid', 'config')
5851 datacenters
= mydb
.get_rows(FROM
='datacenters', SELECT
=select_
)
5852 for datacenter
in datacenters
:
5853 if datacenter
['config']:
5854 config
= yaml
.load(datacenter
['config'], Loader
=yaml
.Loader
)
5855 if 'sdn-controller' in config
and config
['sdn-controller'] == controller_id
:
5856 raise NfvoException("SDN controller {} is in use by datacenter {}".format(controller_id
, datacenter
['uuid']), httperrors
.Conflict
)
5858 data
= ovim
.delete_of_controller(controller_id
)
5859 msg
= 'SDN controller {} deleted'.format(data
)
5863 def datacenter_sdn_port_mapping_set(mydb
, tenant_id
, datacenter_id
, sdn_port_mapping
):
5864 controller
= mydb
.get_rows(FROM
="datacenters", SELECT
=("config",), WHERE
={"uuid":datacenter_id
})
5865 if len(controller
) < 1:
5866 raise NfvoException("Datacenter {} not present in the database".format(datacenter_id
), httperrors
.Not_Found
)
5869 sdn_controller_id
= yaml
.load(controller
[0]["config"], Loader
=yaml
.Loader
)["sdn-controller"]
5871 raise NfvoException("The datacenter {} has not an SDN controller associated".format(datacenter_id
), httperrors
.Bad_Request
)
5873 sdn_controller
= ovim
.show_of_controller(sdn_controller_id
)
5874 switch_dpid
= sdn_controller
["dpid"]
5877 for compute_node
in sdn_port_mapping
:
5878 #element = {"ofc_id": sdn_controller_id, "region": datacenter_id, "switch_dpid": switch_dpid}
5880 element
["compute_node"] = compute_node
["compute_node"]
5881 if compute_node
["ports"]:
5882 for port
in compute_node
["ports"]:
5883 pci
= port
.get("pci")
5884 element
["switch_port"] = port
.get("switch_port")
5885 element
["switch_mac"] = port
.get("switch_mac")
5886 element
["switch_dpid"] = port
.get("switch_dpid")
5887 element
["switch_id"] = port
.get("switch_id")
5888 if not element
["switch_port"] and not element
["switch_mac"]:
5889 raise NfvoException ("The mapping must contain 'switch_port' or 'switch_mac'", httperrors
.Bad_Request
)
5890 for pci_expanded
in utils
.expand_brackets(pci
):
5891 element
["pci"] = pci_expanded
5892 maps
.append(dict(element
))
5894 out
= ovim
.set_of_port_mapping(maps
, sdn_id
=sdn_controller_id
, switch_dpid
=switch_dpid
, vim_id
=datacenter_id
)
5895 vim_threads
["running"][sdn_controller_id
].insert_task("reload")
5898 def datacenter_sdn_port_mapping_list(mydb
, tenant_id
, datacenter_id
):
5899 maps
= ovim
.get_of_port_mappings(db_filter
={"datacenter_id": datacenter_id
})
5902 "sdn-controller": None,
5903 "datacenter-id": datacenter_id
,
5905 "ports_mapping": list()
5908 datacenter
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
)
5909 if datacenter
['config']:
5910 config
= yaml
.load(datacenter
['config'], Loader
=yaml
.Loader
)
5911 if 'sdn-controller' in config
:
5912 controller_id
= config
['sdn-controller']
5913 sdn_controller
= sdn_controller_list(mydb
, tenant_id
, controller_id
)
5914 result
["sdn-controller"] = controller_id
5915 result
["dpid"] = sdn_controller
["dpid"]
5917 if result
["sdn-controller"] == None:
5918 raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id
), httperrors
.Bad_Request
)
5919 if result
["dpid"] == None:
5920 raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result
["sdn-controller"]),
5921 httperrors
.Internal_Server_Error
)
5926 ports_correspondence_dict
= dict()
5928 if result
["sdn-controller"] != link
["wim_id"]:
5929 raise NfvoException("The sdn-controller specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5930 if result
["dpid"] != link
["switch_dpid"]:
5931 raise NfvoException("The dpid specified for different port mappings differ", httperrors
.Internal_Server_Error
)
5932 link_config
= link
["service_mapping_info"]
5934 element
["pci"] = link
.get("device_interface_id")
5935 if link
["switch_port"]:
5936 element
["switch_port"] = link
["switch_port"]
5937 if link_config
["switch_mac"]:
5938 element
["switch_mac"] = link_config
.get("switch_mac")
5940 if not link
.get("interface_id") in ports_correspondence_dict
:
5942 content
["compute_node"] = link
.get("interface_id")
5943 content
["ports"] = list()
5944 ports_correspondence_dict
[link
.get("interface_id")] = content
5946 ports_correspondence_dict
[link
["interface_id"]]["ports"].append(element
)
5948 for key
in sorted(ports_correspondence_dict
):
5949 result
["ports_mapping"].append(ports_correspondence_dict
[key
])
5953 def datacenter_sdn_port_mapping_delete(mydb
, tenant_id
, datacenter_id
):
5954 return ovim
.clear_of_port_mapping(db_filter
={"datacenter_id":datacenter_id
})
5956 def create_RO_keypair(tenant_id
):
5958 Creates a public / private keys for a RO tenant and returns their values
5960 tenant_id: ID of the tenant
5962 public_key: Public key for the RO tenant
5963 private_key: Encrypted private key for RO tenant
5967 key
= RSA
.generate(bits
)
5969 public_key
= key
.publickey().exportKey('OpenSSH')
5970 if isinstance(public_key
, ValueError):
5971 raise NfvoException("Unable to create public key: {}".format(public_key
), httperrors
.Internal_Server_Error
)
5972 private_key
= key
.exportKey(passphrase
=tenant_id
, pkcs
=8)
5973 except (ValueError, NameError) as e
:
5974 raise NfvoException("Unable to create private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5975 if isinstance(public_key
, bytes
):
5976 public_key
= public_key
.decode(encoding
='UTF-8')
5977 if isinstance(private_key
, bytes
):
5978 private_key
= private_key
.decode(encoding
='UTF-8')
5979 return public_key
, private_key
5981 def decrypt_key (key
, tenant_id
):
5983 Decrypts an encrypted RSA key
5985 key: Private key to be decrypted
5986 tenant_id: ID of the tenant
5988 unencrypted_key: Unencrypted private key for RO tenant
5991 key
= RSA
.importKey(key
,tenant_id
)
5992 unencrypted_key
= key
.exportKey('PEM')
5993 if isinstance(unencrypted_key
, ValueError):
5994 raise NfvoException("Unable to decrypt the private key: {}".format(unencrypted_key
), httperrors
.Internal_Server_Error
)
5995 if isinstance(unencrypted_key
, bytes
):
5996 unencrypted_key
= unencrypted_key
.decode(encoding
='UTF-8')
5997 except ValueError as e
:
5998 raise NfvoException("Unable to decrypt the private key: {}".format(e
), httperrors
.Internal_Server_Error
)
5999 return unencrypted_key