+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 = datatacenter_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):
+ 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)
+
+ 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
+ )
+ 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, thread_name)
+ new_thread.start()
+ thread_id = vim["datacenter_id"] + "-" + vim['nfvo_tenant_id']
+ 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():
+ for thread_id,thread in vim_threads["running"].items():
+ thread.insert_task("exit")
+ vim_threads["deleting"][thread_id] = thread
+ vim_threads["running"]={}
+