Add port of SR-IOV NIC support
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / vimapi / adaptor.py
index 70f33dc..bbc9109 100644 (file)
 import logging
 import time
 import ast
+import json
 
 from lcm.pub.utils.values import ignore_case_get, set_opt_val
 from lcm.pub.msapi.aai import get_flavor_info
 from . import api
 from .exceptions import VimException
+from lcm.nf.const import ACTION_TYPE
 
 logger = logging.getLogger(__name__)
 
@@ -65,6 +67,50 @@ def get_res_id(res_cache, res_type, key):
     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"]
+    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"])
+                # TODO check if the we should poll getvm to get the status or the action_vm api
+                # successful return should suffice to mark vm as Active/Inactive
+                do_notify_op("INACTIVE", res["id"])
+        except VimException as e:
+            # TODO Have to update database appropriately on failure
+            logger.error("Failed to Heal %s(%s)", RES_VM, res["res_id"])
+            logger.error("%s:%s", e.http_code, e.message)
+
+
 def create_vim_res(data, do_notify):
     vim_cache, res_cache = {}, {}
     for vol in ignore_case_get(data, "volume_storages"):
@@ -182,7 +228,7 @@ def create_port(vim_cache, res_cache, data, port, do_notify, res_type):
                 vdu["cps"].append(port["cp_id"])
             break
     if not location_info:
-        err_msg = "vdu_id(%s) for cp(%s) is not defined"
+        err_msg = "vdu_id(%s) for cp(%s) is not defined."
         raise VimException(err_msg % (port_ref_vdu_id, port["cp_id"]), ERR_CODE)
     network_id = ignore_case_get(port, "networkId")
     subnet_id = ignore_case_get(port, "subnetId")
@@ -197,11 +243,20 @@ def create_port(vim_cache, res_cache, data, port, do_notify, res_type):
     set_opt_val(param, "macAddress", ignore_case_get(port["properties"], "mac_address"))
     ip_address = []
     for one_protocol_data in port["properties"]["protocol_data"]:
-        l3_address_data = one_protocol_data["address_data"]["l3_address_data"]
+        l3_address_data = one_protocol_data["address_data"]["l3_address_data"]  # l3 is not 13
         fixed_ip_address = ignore_case_get(l3_address_data, "fixed_ip_address")
         ip_address.extend(fixed_ip_address)
+    for one_virtual_network_interface in port["properties"]["virtual_network_interface_requirements"]:
+        interfaceTypeString = one_virtual_network_interface["network_interface_requirements"]["interfaceType"]
+        interfaceType = json.loads(interfaceTypeString)["configuration-value"]
+        vnic_type = ignore_case_get(port["properties"], "vnic_type")
+        if vnic_type == "":
+            if interfaceType == "SR-IOV":
+                set_opt_val(param, "vnicType", "direct")
+        else:
+            set_opt_val(param, "vnicType", vnic_type)
+
     set_opt_val(param, "ip", ",".join(ip_address))
-    set_opt_val(param, "vnicType", ignore_case_get(port["properties"], "vnic_type"))
     set_opt_val(param, "securityGroups", "")   # TODO
     vim_id, tenant_name = location_info["vimid"], location_info["tenant"]
     tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name)
@@ -266,7 +321,7 @@ def create_flavor(vim_cache, res_cache, data, flavor, do_notify, res_type):
     vdu_memory_requirements = ignore_case_get(virtual_memory, "vdu_memory_requirements")
     if "memoryPageSize" in vdu_memory_requirements:
         memory_page_size = int(vdu_memory_requirements["memoryPageSize"].replace('MB', '').strip())
-        flavor_extra_specs = ("hw:mem_page_size=%sMB" % memory_page_size)
+        flavor_extra_specs = {"hw": memory_page_size, }  # TODO
         logger.debug("flavor_extra_specs:%s" % flavor_extra_specs)
 
     # FIXME: search aai flavor