+def get_task_id():
+ global last_task_id
+ task_id = time()
+ if task_id <= last_task_id:
+ task_id = last_task_id + 0.000001
+ last_task_id = task_id
+ return "TASK.{:.6f}".format(task_id)
+
+
+def new_task(name, params, store=True, depends=None):
+ task_id = get_task_id()
+ task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
+ if depends:
+ task["depends"] = depends
+ if store:
+ task_dict[task_id] = task
+ return task
+
+
+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
+ name = datacenter_name[:16] + "." + tenant_name[:16]
+ if name not in vim_threads["names"]:
+ vim_threads["names"].append(name)
+ return name
+ name = datacenter_id + "-" + tenant_id
+ vim_threads["names"].append(name)
+ return name
+
+
+def start_service(mydb):
+ global db, global_config
+ db = nfvo_db.nfvo_db()
+ db.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
+ global ovim
+
+ # Initialize openvim for SDN control
+ # TODO: Avoid static configuration by adding new parameters to openmanod.cfg
+ # TODO: review ovim.py to delete not needed configuration
+ ovim_configuration = {
+ 'logger_name': 'openvim',
+ 'network_vlan_range_start': 1000,
+ 'network_vlan_range_end': 4096,
+ 'log_level_db': 'DEBUG',
+ 'db_name': 'mano_vim_db',
+ 'db_host': 'localhost',
+ 'db_user': 'vim',
+ 'db_passwd': 'vimpw',
+ 'database_version': '0.15',
+ 'bridge_ifaces': {},
+ 'mode': 'normal',
+ 'of_controller_nets_with_same_vlan': True,
+ 'network_type': 'bridge',
+ #TODO: log_level_of should not be needed. To be modified in ovim
+ 'log_level_of': 'DEBUG'
+ }
+ #ovim = Ovim.ovim(ovim_configuration)
+ #ovim.start_service()
+
+ 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'
+ select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
+ 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
+ 'user','passwd', 'dt.config as dt_config', 'nfvo_tenant_id')
+ try:
+ vims = mydb.get_rows(FROM=from_, SELECT=select_)
+ for vim in vims:
+ extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id')}
+ if vim["config"]:
+ extra.update(yaml.load(vim["config"]))
+ if vim.get('dt_config'):
+ extra.update(yaml.load(vim["dt_config"]))
+ if vim["type"] not in vimconn_imported:
+ module_info=None
+ try:
+ module = "vimconn_" + vim["type"]
+ module_info = imp.find_module(module)
+ vim_conn = imp.load_module(vim["type"], *module_info)
+ vimconn_imported[vim["type"]] = vim_conn
+ except (IOError, ImportError) as e:
+ if module_info and module_info[0]:
+ file.close(module_info[0])
+ raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
+ vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
+
+ thread_id = vim["datacenter_id"] + "." + vim['nfvo_tenant_id']
+ vim_persistent_info[thread_id] = {}
+ try:
+ #if not tenant:
+ # return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM %s" % ( vim["type"])
+ myvim = vimconn_imported[ vim["type"] ].vimconnector(
+ uuid=vim['datacenter_id'], name=vim['datacenter_name'],
+ tenant_id=vim['vim_tenant_id'], tenant_name=vim['vim_tenant_name'],
+ url=vim['vim_url'], url_admin=vim['vim_url_admin'],
+ user=vim['user'], passwd=vim['passwd'],
+ config=extra, persistent_info=vim_persistent_info[thread_id]
+ )
+ except Exception as e:
+ raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
+ thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], vim['vim_tenant_id'])
+ new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
+ vim.get('datacenter_tenant_id'), db=db, db_lock=db_lock, ovim=ovim)
+ new_thread.start()
+ vim_threads["running"][thread_id] = new_thread
+ except db_base_Exception as e:
+ raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
+
+def stop_service():
+ global ovim, global_config
+ if ovim:
+ ovim.stop_service()
+ for thread_id,thread in vim_threads["running"].items():
+ thread.insert_task(new_task("exit", None, store=False))
+ vim_threads["deleting"][thread_id] = thread
+ vim_threads["running"] = {}
+ if global_config and global_config.get("console_thread"):
+ for thread in global_config["console_thread"]:
+ thread.terminate = True
+
+