Fix vnf heal logic
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / vimapi / adaptor.py
index 4415362..bffdcbe 100644 (file)
@@ -16,6 +16,7 @@ import logging
 import time
 import json
 import os
+import base64
 
 from lcm.pub.utils.values import ignore_case_get, set_opt_val
 from lcm.pub.msapi.aai import get_flavor_info
@@ -87,6 +88,7 @@ def action_vm(action_type, server, vimId, tenantId):
         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)
 
 
@@ -115,15 +117,20 @@ def heal_vim_res(vdus, vnfd_info, do_notify, data, vim_cache, res_cache):
         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, vdus[0]["vdu_id"])
+        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, vdus[0]["vdu_id"])
+        raise NFLCMException("Failed to Heal %s(%s)" % (RES_VM, resid))
 
 
 def create_vim_res(data, do_notify, vim_cache={}, res_cache={}):
@@ -251,7 +258,7 @@ def create_port(vim_cache, res_cache, data, port, do_notify, res_type):
         subnet_id = get_res_id(res_cache, RES_SUBNET, port["vl_id"])
     param = {
         "networkId": network_id,
-        "name": port["properties"].get("name", "undefined")
+        "name": port["properties"].get("name", "")
     }
     set_opt_val(param, "subnetId", subnet_id)
     set_opt_val(param, "macAddress", ignore_case_get(port["properties"], "mac_address"))
@@ -400,8 +407,9 @@ def create_vm(vim_cache, res_cache, data, vm, do_notify, res_type):
             "volumeId": get_res_id(res_cache, RES_VOLUME, vol_id)
         })
 
+    user_data = base64.encodestring(ignore_case_get(vm["properties"], "user_data"))
     set_opt_val(param, "availabilityZone", ignore_case_get(location_info, "availability_zone"))
-    set_opt_val(param, "userdata", ignore_case_get(vm["properties"], "user_data"))
+    set_opt_val(param, "userdata", user_data)
     set_opt_val(param, "metadata", ignore_case_get(vm["properties"], "meta_data"))
     set_opt_val(param, "securityGroups", "")   # TODO List of names of security group
     set_opt_val(param, "serverGroup", "")      # TODO the ServerGroup for anti-affinity and affinity