X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FRO.git;a=blobdiff_plain;f=RO%2Fosm_ro%2Fnfvo.py;h=96e0a7a0483de69a4d91fc72275aecfbfb264b83;hp=32d739a3bec676573cc144ea4ef8602e246691db;hb=08cfe043bcce0c20728b09865f192f9c61d864e7;hpb=16894de5c4aa19e571f4d18ef9c90bf60596dc19 diff --git a/RO/osm_ro/nfvo.py b/RO/osm_ro/nfvo.py index 32d739a3..96e0a7a0 100644 --- a/RO/osm_ro/nfvo.py +++ b/RO/osm_ro/nfvo.py @@ -30,6 +30,7 @@ __date__ ="$16-sep-2014 22:05:01$" # import imp import json import yaml +from random import choice as random_choice from osm_ro import utils from osm_ro.utils import deprecated from osm_ro.vim_thread import vim_thread @@ -63,7 +64,7 @@ from pkg_resources import iter_entry_points # WIM from .wim import sdnconn -from .wim.wimconn_fake import FakeConnector +from .wim.wimconn_dummy import DummyConnector from .wim.failing_connector import FailingConnector from .http_tools import errors as httperrors from .wim.engine import WimEngine @@ -100,6 +101,7 @@ last_task_id = 0.0 db = None db_lock = Lock() +worker_id = None class NfvoException(httperrors.HttpMappedError): """Common Class for NFVO errors""" @@ -144,20 +146,29 @@ def new_task(name, params, depends=None): def is_task_id(id): return True if id[:5] == "TASK-" else False - -def get_non_used_vim_name(datacenter_name, datacenter_id, tenant_name, tenant_id): - name = datacenter_name[:16] - if name not in vim_threads["names"]: - vim_threads["names"].append(name) - return name - if tenant_name: - name = datacenter_name[:16] + "." + tenant_name[:16] - if name not in vim_threads["names"]: - vim_threads["names"].append(name) - return name - name = datacenter_id - vim_threads["names"].append(name) - return name +def get_process_id(): + """ + Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it + will provide a random one + :return: Obtained ID + """ + # Try getting docker id. If fails, get pid + try: + with open("/proc/self/cgroup", "r") as f: + text_id_ = f.readline() + _, _, text_id = text_id_.rpartition("/") + text_id = text_id.replace("\n", "")[:12] + if text_id: + return text_id + except Exception: + pass + # Return a random id + return "".join(random_choice("0123456789abcdef") for _ in range(12)) + +def get_non_used_vim_name(datacenter_name, datacenter_id): + return "{}:{}:{}".format( + worker_id[:12], datacenter_id.replace("-", "")[:32], datacenter_name[:16] + ) # -- Move def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id): @@ -175,7 +186,7 @@ def get_non_used_wim_name(wim_name, wim_id, tenant_name, tenant_id): def start_service(mydb, persistence=None, wim=None): - global db, global_config, plugins, ovim + global db, global_config, plugins, ovim, worker_id db = nfvo_db.nfvo_db(lock=db_lock) mydb.lock = db_lock db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name']) @@ -183,8 +194,9 @@ def start_service(mydb, persistence=None, wim=None): persistence = persistence or WimPersistence(db) try: - if "rosdn_fake" not in plugins: - plugins["rosdn_fake"] = FakeConnector + worker_id = get_process_id() + if "rosdn_dummy" not in plugins: + plugins["rosdn_dummy"] = DummyConnector # starts ovim library ovim = Sdn(db, plugins) @@ -237,8 +249,7 @@ def start_service(mydb, persistence=None, wim=None): vim['datacenter_id'], e)) # raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), # httperrors.Internal_Server_Error) - thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['datacenter_id'], vim['vim_tenant_name'], - vim['vim_tenant_id']) + thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['datacenter_id']) new_thread = vim_thread(task_lock, plugins, thread_name, None, vim['datacenter_tenant_id'], db=db) new_thread.start() @@ -252,7 +263,7 @@ def start_service(mydb, persistence=None, wim=None): _load_plugin(plugin_name, type="sdn") thread_id = wim['uuid'] - thread_name = get_non_used_vim_name(wim['name'], wim['uuid'], wim['uuid'], None) + thread_name = get_non_used_vim_name(wim['name'], wim['uuid']) new_thread = vim_thread(task_lock, plugins, thread_name, wim['uuid'], None, db=db) new_thread.start() vim_threads["running"][thread_id] = new_thread @@ -2433,7 +2444,6 @@ def new_nsd_v3(mydb, tenant_id, nsd_descriptor): elif vld.get("vim-network-name"): db_sce_net["vim_network_name"] = get_str(vld, "vim-network-name", 255) - # table sce_interfaces (vld:vnfd-connection-point-ref) for iface in vld.get("vnfd-connection-point-ref").values(): # Check if there are VDUs in the descriptor @@ -2447,7 +2457,7 @@ def new_nsd_v3(mydb, tenant_id, nsd_descriptor): "'nsd':'constituent-vnfd'".format( str(nsd["id"]), str(vld["id"]), str(iface["member-vnf-index-ref"])), httperrors.Bad_Request) - + existing_ifaces = mydb.get_rows(SELECT=('i.uuid as uuid', 'i.type as iface_type'), FROM="interfaces as i join vms on i.vm_id=vms.uuid", WHERE={'vnf_id': vnf_index2vnf_uuid[vnf_index], @@ -2474,7 +2484,7 @@ def new_nsd_v3(mydb, tenant_id, nsd_descriptor): "sce_net_id": sce_net_uuid, "interface_id": interface_uuid, "ip_address": iface_ip_address, - } + } db_sce_interfaces.append(db_sce_interface) if not db_sce_net["type"]: db_sce_net["type"] = "bridge" @@ -2963,7 +2973,7 @@ def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_i if datacenter_tenant_id: thread_id = datacenter_tenant_id thread = vim_threads["running"].get(datacenter_tenant_id) - else: + if not thread: where_={"td.nfvo_tenant_id": tenant_id} if datacenter_id_name: if utils.check_valid_uuid(datacenter_id_name): @@ -2975,7 +2985,7 @@ def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_i if datacenter_tenant_id: where_["dt.uuid"] = datacenter_tenant_id datacenters = mydb.get_rows( - SELECT=("dt.uuid as datacenter_tenant_id",), + SELECT=("dt.uuid as datacenter_tenant_id, d.name as datacenter_name",), FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id " "join datacenters as d on d.uuid=dt.datacenter_id", WHERE=where_) @@ -2983,7 +2993,14 @@ def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_i raise NfvoException("More than one datacenters found, try to identify with uuid", httperrors.Conflict) elif datacenters: thread_id = datacenters[0]["datacenter_tenant_id"] + datacenter_name = datacenters[0]["datacenter_name"] thread = vim_threads["running"].get(thread_id) + if not thread: + thread_name = get_non_used_vim_name(datacenter_name, datacenter_id) + thread = vim_thread(task_lock, plugins, thread_name, None, + thread_id, db=mydb) + thread.start() + vim_threads["running"][thread_id] = thread if not thread: raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), httperrors.Not_Found) return thread_id, thread @@ -4076,6 +4093,7 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): else: netDict['net_id'] = "TASK-{}".format(net2task_id[sce_vnf['uuid']][iface['net_id']]) instance_net_id = vnf_net2instance[sce_vnf['uuid']][iface['net_id']] + instance_wim_net_id = None task_depends_on.append(net2task_id[sce_vnf['uuid']][iface['net_id']]) # skip bridge ifaces not connected to any net if 'net_id' not in netDict or netDict['net_id'] == None: @@ -5238,7 +5256,7 @@ def create_vim_account(mydb, nfvo_tenant, datacenter_id, name=None, vim_id=None, mydb.new_row('tenants_datacenters', tenants_datacenter_dict) # create thread - thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid']) + thread_name = get_non_used_vim_name(datacenter_name, datacenter_id) new_thread = vim_thread(task_lock, plugins, thread_name, None, datacenter_tenant_id, db=db) new_thread.start() thread_id = datacenter_tenants_dict["uuid"] @@ -5755,15 +5773,24 @@ def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): return vim_action_get(mydb, tenant_id, datacenter, item, content) def sdn_controller_create(mydb, tenant_id, sdn_controller): - wim_id = ovim.new_of_controller(sdn_controller) - - thread_name = get_non_used_vim_name(sdn_controller['name'], wim_id, wim_id, None) - new_thread = vim_thread(task_lock, plugins, thread_name, wim_id, None, db=db) - new_thread.start() - thread_id = wim_id - vim_threads["running"][thread_id] = new_thread - logger.debug('New SDN controller created with uuid {}'.format(wim_id)) - return wim_id + try: + wim_id = ovim.new_of_controller(sdn_controller) + + # Load plugin if not previously loaded + controller_type = sdn_controller.get("type") + plugin_name = "rosdn_" + controller_type + if plugin_name not in plugins: + _load_plugin(plugin_name, type="sdn") + + thread_name = get_non_used_vim_name(sdn_controller['name'], wim_id) + new_thread = vim_thread(task_lock, plugins, thread_name, wim_id, None, db=db) + new_thread.start() + thread_id = wim_id + vim_threads["running"][thread_id] = new_thread + logger.debug('New SDN controller created with uuid {}'.format(wim_id)) + return wim_id + except ovimException as e: + raise NfvoException(e) from e def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): data = ovim.edit_of_controller(controller_id, sdn_controller)