diff options
Diffstat (limited to 'share/common')
-rw-r--r-- | share/common/msapi/helper.py | 63 | ||||
-rw-r--r-- | share/common/utils/aai_cache.py | 11 | ||||
-rw-r--r-- | share/common/utils/restcall.py | 11 |
3 files changed, 47 insertions, 38 deletions
diff --git a/share/common/msapi/helper.py b/share/common/msapi/helper.py index b2ff1d6b..69b91953 100644 --- a/share/common/msapi/helper.py +++ b/share/common/msapi/helper.py @@ -13,12 +13,10 @@ import json import logging # import re import uuid - import threading import datetime import time - -import traceback +#import traceback # from common.exceptions import VimDriverNewtonException from common.utils import restcall @@ -75,7 +73,7 @@ class MultiCloudAAIHelper(object): ''' def __init__(self, multicloud_prefix, aai_base_url): - logger.debug("MultiCloudAAIHelper __init__ traceback: %s" % traceback.format_exc()) + # logger.debug("MultiCloudAAIHelper __init__ traceback: %s" % traceback.format_exc()) self.proxy_prefix = multicloud_prefix self.aai_base_url = aai_base_url self._logger = logger @@ -145,18 +143,18 @@ class MultiCloudAAIHelper(object): retcode, content, status_code = \ restcall.req_to_aai(resource_url, "PUT", content=resource_info) - self._logger.debug( - ("_update_resoure,vimid:%(cloud_owner)s" - "_%(cloud_region_id)s req_to_aai: %(resoure_id)s, " - "return %(retcode)s, %(content)s, %(status_code)s") - % { - "cloud_owner": cloud_owner, - "cloud_region_id": cloud_region_id, - "resoure_id": resoure_id, - "retcode": retcode, - "content": content, - "status_code": status_code, - }) + # self._logger.debug( + # ("_update_resoure,vimid:%(cloud_owner)s" + # "_%(cloud_region_id)s req_to_aai: %(resoure_id)s, " + # "return %(retcode)s, %(content)s, %(status_code)s") + # % { + # "cloud_owner": cloud_owner, + # "cloud_region_id": cloud_region_id, + # "resoure_id": resoure_id, + # "retcode": retcode, + # "content": content, + # "status_code": status_code, + # }) return retcode, content # unknown cloud owner,region_id return ( @@ -197,17 +195,17 @@ class MultiCloudThreadHelper(object): # } # format of backlog: # {"<id value of backlog item>": <backlog item>, ...} + self.name = name or "default" self.backlog = {} # expired backlog items self.expired_backlog = {} self.lock = threading.Lock() self.state_ = 0 # 0: stopped, 1: started - self.cache_prefix = "bi_"+name+"_" - self.cache_expired_prefix = "biex_"+name+"_" + self.cache_prefix = "bi_"+self.name+"_" + self.cache_expired_prefix = "biex_"+self.name+"_" self.thread = MultiCloudThreadHelper.HelperThread(self) - self.thread.start() - + # self.thread.start() def state(self): return self.state_ @@ -217,7 +215,7 @@ class MultiCloudThreadHelper(object): if 0 == self.state_: self.state_ = 1 # self.thread = MultiCloudThreadHelper.HelperThread(self) - # self.thread.start() + self.thread.start() else: pass self.lock.release() @@ -227,9 +225,10 @@ class MultiCloudThreadHelper(object): def add(self, backlog_item): cache_for_query = None - if not hasattr(backlog_item, "worker"): + if not backlog_item.get("worker", None): + logger.warn("Fail to add backlog item: %s" % backlog_item) return None - if not hasattr(backlog_item, "id"): + if not backlog_item.get("id", None): backlog_item["id"] = str(uuid.uuid1()) else: cache_for_query = { @@ -237,7 +236,7 @@ class MultiCloudThreadHelper(object): "status": backlog_item.get("status", None) } - if not hasattr(backlog_item, "repeat"): + if not backlog_item.get("repeat", None): backlog_item["repeat"] = 0 backlog_item["timestamp"] = 0 @@ -248,8 +247,9 @@ class MultiCloudThreadHelper(object): json.dumps(cache_for_query), 3600 * 24) self.expired_backlog.pop(backlog_item["id"], None) - self.backlog.update(backlog_item["id"], backlog_item) + self.backlog[backlog_item["id"]] = backlog_item # self.lock.release() + logger.debug("Add backlog item: %s" % backlog_item) return len(self.backlog) def get(self, backlog_id): @@ -305,17 +305,19 @@ class MultiCloudThreadHelper(object): self.duration = 0 self.owner = owner # debug: dump the callstack to determine the callstack, hence the lcm - logger.debug("HelperThread __init__ : %s" % traceback.format_exc()) + # logger.debug("HelperThread __init__ : %s" % traceback.format_exc()) def run(self): - logger.debug("Start processing backlogs") + logger.debug("Thread %s starts processing backlogs" % self.owner.name) nexttimer = 0 while self.owner.state_ == 1: # and self.owner.count() > 0: if nexttimer > 1000000: # sleep in case of interval > 1 second time.sleep(nexttimer // 1000000) nexttimer = 30*1000000 # initial interval in us to be updated:30 seconds - for backlog_id, item in self.owner.backlog: + # logger.debug("self.owner.backlog: %s, len: %s" % (self.owner.name, len(self.owner.backlog))) + for backlog_id, item in self.owner.backlog.items(): + # logger.debug("evaluate backlog item: %s" % item) # check interval for repeatable backlog item now = MultiCloudThreadHelper.get_epoch_now_usecond() repeat_interval = item.get("repeat", 0) @@ -331,10 +333,11 @@ class MultiCloudThreadHelper(object): # not time to run this backlog item yet continue + # logger.debug("process backlog item: %s" % backlog_id) worker = item.get("worker", None) payload = item.get("payload", None) try: - item["status"] = worker(payload) or 0 + item["status"] = worker(*payload) or 0 except Exception as e: item["status"] = e.message cache_item_for_query = { @@ -364,6 +367,6 @@ class MultiCloudThreadHelper(object): # while True: # logger.debug("thread sleep for 5 seconds") # time.sleep(5) # wait forever, testonly - logger.debug("stop processing backlogs") + logger.debug("Thread %s stops processing backlogs" % self.owner.name) self.owner.state_ = 0 # end of processing diff --git a/share/common/utils/aai_cache.py b/share/common/utils/aai_cache.py index 53298bb8..41506aca 100644 --- a/share/common/utils/aai_cache.py +++ b/share/common/utils/aai_cache.py @@ -26,12 +26,14 @@ def flush_cache_by_url(resource_url): def get_cache_by_url(resource_url): try: - if (filter_cache_by_url(resource_url)): + if filter_cache_by_url(resource_url): value = cache.get("AAI_" + resource_url) + # logger.debug("Find cache the resource: %s, %s" %( resource_url, value)) return json.loads(value) if value else None else: return None - except: + except Exception as e: + logger.error("get_cache_by_url exception: %s" % e.message) return None @@ -40,9 +42,10 @@ def set_cache_by_url(resource_url, resource_in_json): # filter out unmanaged AAI resource if filter_cache_by_url(resource_url): # cache the resource for 24 hours - logger.debug("Cache the resource: "+ resource_url) + # logger.debug("Cache the resource: "+ resource_url) cache.set("AAI_" + resource_url, json.dumps(resource_in_json), 3600 * 24) - except: + except Exception as e: + logger.error("get_cache_by_url exception: %s" % e.message) pass def filter_cache_by_url(resource_url): diff --git a/share/common/utils/restcall.py b/share/common/utils/restcall.py index eb4cb008..464dd65f 100644 --- a/share/common/utils/restcall.py +++ b/share/common/utils/restcall.py @@ -65,9 +65,10 @@ def _call_req(base_url, user, passwd, auth_type, headers['Authorization'] = 'Basic ' + \ base64.b64encode(tmpauthsource).decode('utf-8') - logger.info("Making rest call with uri,method, header = %s, %s, %s" % (full_url, method.upper(), headers)) + logger.info("Making rest call with method, uri, header = %s, %s, %s" % + (method.upper(), full_url, headers)) if content: - logger.debug("with content = %s" % (content)) + logger.debug("with content = %s" % content) ca_certs = None for retry_times in range(MAX_RETRY_TIME): @@ -138,8 +139,9 @@ def req_to_aai(resource, method, content='', appid=settings.MULTICLOUD_APP_ID, n # hook to flush cache if method.upper() in ["PUT", "POST", "PATCH", "DELETE"]: aai_cache.flush_cache_by_url(resource) - elif method.upper in ["GET"] and not nocache: + elif method.upper() in ["GET"] and not nocache: content = aai_cache.get_cache_by_url(resource) + # logger.debug("cached resource: %s, %s" % (resource, content)) if content: return content @@ -148,7 +150,8 @@ def req_to_aai(resource, method, content='', appid=settings.MULTICLOUD_APP_ID, n resource, method, content=json.dumps(content), extra_headers=headers) if method.upper() in ["GET"] and ret == 0 and not nocache: - aai_cache.set_cache_by_url(resource, [ret, resp_body, resp_status]) + # aai_cache.set_cache_by_url(resource, [ret, resp_body, resp_status]) + aai_cache.set_cache_by_url(resource, (ret, resp_body, resp_status)) return [ret, resp_body, resp_status] |