+
+def set_res_cache(res_cache, res_type, key, val):
+ if res_type not in res_cache:
+ res_cache[res_type] = {}
+ if key in res_cache[res_type]:
+ raise VimException("Duplicate key(%s) of %s" % (key, res_type), ERR_CODE)
+ res_cache[res_type][key] = val
+
+
+def get_res_id(res_cache, res_type, key):
+ if res_type not in res_cache:
+ raise VimException("%s not found in cache" % res_type, ERR_CODE)
+ if key not in res_cache[res_type]:
+ raise VimException("%s(%s) not found in cache" % (res_type, key), ERR_CODE)
+ return res_cache[res_type][key]
+
+
+def action_vm(action_type, server, vimId, tenantId):
+ param = {}
+ if action_type == ACTION_TYPE.START:
+ param = {
+ "os-start": None,
+ }
+ elif action_type == ACTION_TYPE.STOP:
+ param = {
+ "os-stop": None,
+ }
+ elif action_type == ACTION_TYPE.REBOOT:
+ param = {
+ "reboot": {}
+ }
+ if server["status"] == "ACTIVE":
+ param["reboot"]["type"] = "SOFT"
+ else:
+ param["reboot"]["type"] = "HARD"
+ res_id = server["id"]
+ logger.debug("%s,%s,%s,%s", vimId, tenantId, res_id, param)
+ api.action_vm(vimId, tenantId, res_id, param)
+
+
+# TODO Have to check if the resources should be started and stopped in some order.
+def operate_vim_res(data, changeStateTo, stopType, gracefulStopTimeout, do_notify_op):
+ for res in ignore_case_get(data, "vm"):
+ try:
+ if changeStateTo == "STARTED":
+ action_vm(ACTION_TYPE.START, res, res["vim_id"], res["tenant_id"])
+ do_notify_op("ACTIVE", res["id"])
+ elif changeStateTo == "STOPPED":
+ if stopType == "GRACEFUL":
+ if gracefulStopTimeout > 60:
+ gracefulStopTimeout = 60
+ time.sleep(gracefulStopTimeout)
+ action_vm(ACTION_TYPE.STOP, res, res["vim_id"], res["tenant_id"])
+ do_notify_op("INACTIVE", res["id"])
+ except VimException as e:
+ logger.error("Failed to Operate %s(%s)", RES_VM, res["res_id"])
+ logger.error("%s:%s", e.http_code, e.message)
+ raise NFLCMException("Failed to Operate %s(%s)", RES_VM, res["res_id"])
+
+
+def heal_vim_res(vdus, vnfd_info, do_notify, data, vim_cache, res_cache):
+ try:
+ vimid = data["vimid"]
+ tenant = data["tenant"]
+ actionType = data["action"]
+ resid = ''
+ if actionType == HEAL_ACTION_TYPE.START:
+ resid = vdus[0]["vdu_id"]
+ create_vm(vim_cache, res_cache, vnfd_info, vdus[0], do_notify, RES_VM)
+ elif actionType == HEAL_ACTION_TYPE.RESTART:
+ resid = vdus[0].resourceid
+ logger.debug("Start restart vm(%s)", resid)
+ vm_info = api.get_vm(vimid, tenant, vdus[0].resourceid)
+ logger.debug("vminfo=%s", vm_info)
+ action_vm(ACTION_TYPE.REBOOT, vm_info, vimid, tenant)
+ except VimException as e:
+ logger.error("Failed to Heal %s(%s)", RES_VM, resid)
+ logger.error("%s:%s", e.http_code, e.message)
+ raise NFLCMException("Failed to Heal %s(%s)" % (RES_VM, resid))
+
+
+def create_vim_res(data, do_notify, vim_cache={}, res_cache={}):