feature 1417 support of PDUs
[osm/RO.git] / osm_ro / nfvo.py
index e16c38f..cfd2b43 100644 (file)
@@ -943,6 +943,7 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
             cp_name2iface_uuid = {}
             cp_name2vm_uuid = {}
             cp_name2db_interface = {}
+            vdu_id2cp_name = {}  # stored only when one external connection point is presented at this VDU
 
             # table vms (vdus)
             vdu_id2uuid = {}
@@ -960,6 +961,7 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
                     "osm_id": vdu_id,
                     "name": get_str(vdu, "name", 255),
                     "description": get_str(vdu, "description", 255),
+                    "pdu_type": get_str(vdu, "pdu-type", 255),
                     "vnf_id": vnf_uuid,
                 }
                 vdu_id2uuid[db_vm["osm_id"]] = vm_uuid
@@ -1049,7 +1051,6 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
 
                 # table interfaces (internal/external interfaces)
                 flavor_epa_interfaces = []
-                vdu_id2cp_name = {}  # stored only when one external connection point is presented at this VDU
                 # for iface in chain(vdu.get("internal-interface").itervalues(), vdu.get("external-interface").itervalues()):
                 for iface in vdu.get("interface").itervalues():
                     flavor_epa_interface = {}
@@ -1072,7 +1073,7 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
 
                     if iface.get("virtual-interface").get("type") == "OM-MGMT":
                         db_interface["type"] = "mgmt"
-                    elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000"):
+                    elif iface.get("virtual-interface").get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
                         db_interface["type"] = "bridge"
                         db_interface["model"] = get_str(iface.get("virtual-interface"), "type", 12)
                     elif iface.get("virtual-interface").get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
@@ -1240,7 +1241,8 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
                                             "'member-vdus':'{vdu}'. Reference to a non-existing vdu".format(
                                                 vnf=vnfd_id, pg=pg_name, vdu=vdu_id),
                                             HTTP_Bad_Request)
-                    db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
+                    if vdu_id2db_table_index[vdu_id]:
+                        db_vms[vdu_id2db_table_index[vdu_id]]["availability_zone"] = pg_name
                     # TODO consider the case of isolation and not colocation
                     # if pg.get("strategy") == "ISOLATION":
                     
@@ -1256,20 +1258,22 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor):
                 mgmt_access["vm_id"] = vdu_id2uuid[vnfd["mgmt-interface"]["vdu-id"]]
                 # if only one cp is defined by this VDU, mark this interface as of type "mgmt"
                 if vdu_id2cp_name.get(mgmt_vdu_id):
-                    cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
+                    if cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]:
+                        cp_name2db_interface[vdu_id2cp_name[mgmt_vdu_id]]["type"] = "mgmt"
 
             if vnfd["mgmt-interface"].get("ip-address"):
                 mgmt_access["ip-address"] = str(vnfd["mgmt-interface"].get("ip-address"))
             if vnfd["mgmt-interface"].get("cp"):
                 if vnfd["mgmt-interface"]["cp"] not in cp_name2iface_uuid:
-                    raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp':'{cp}'. "
+                    raise NfvoException("Error. Invalid VNF descriptor at 'vnfd[{vnf}]':'mgmt-interface':'cp'['{cp}']. "
                                         "Reference to a non-existing connection-point".format(
                                             vnf=vnfd_id, cp=vnfd["mgmt-interface"]["cp"]),
                                         HTTP_Bad_Request)
                 mgmt_access["vm_id"] = cp_name2vm_uuid[vnfd["mgmt-interface"]["cp"]]
                 mgmt_access["interface_id"] = cp_name2iface_uuid[vnfd["mgmt-interface"]["cp"]]
                 # mark this interface as of type mgmt
-                cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
+                if cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]:
+                    cp_name2db_interface[vnfd["mgmt-interface"]["cp"]]["type"] = "mgmt"
 
             default_user = get_str(vnfd.get("vnf-configuration", {}).get("config-access", {}).get("ssh-access", {}),
                                     "default-user", 64)
@@ -1600,13 +1604,13 @@ def get_vnf_id(mydb, tenant_id, vnf_id):
             SELECT=('vms.uuid as uuid', 'vms.osm_id as osm_id', 'vms.name as name', 'vms.description as description',
                     'boot_data'),
             WHERE={'vnfs.uuid': vnf_id} )
-    if len(content)==0:
-        raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
+    if len(content)!=0:
+        #raise NfvoException("vnf '{}' not found".format(vnf_id), HTTP_Not_Found)
     # change boot_data into boot-data
-    for vm in content:
-        if vm.get("boot_data"):
-            vm["boot-data"] = yaml.safe_load(vm["boot_data"])
-            del vm["boot_data"]
+        for vm in content:
+            if vm.get("boot_data"):
+                vm["boot-data"] = yaml.safe_load(vm["boot_data"])
+                del vm["boot_data"]
 
     data['vnf']['VNFC'] = content
     #TODO: GET all the information from a VNFC and include it in the output.
@@ -3119,6 +3123,8 @@ def create_instance(mydb, tenant_id, instance_dict):
                         if sce_vnf_ifaces.get("sce_net_id") == sce_net["uuid"]:
                             involved_datacenters.append(vnf_datacenter)
                             break
+            if not involved_datacenters:
+                involved_datacenters.append(default_datacenter_id)
 
             descriptor_net = {}
             if instance_dict.get("networks") and instance_dict["networks"].get(sce_net["name"]):
@@ -3144,6 +3150,7 @@ def create_instance(mydb, tenant_id, instance_dict):
                 myvim_thread_id = myvim_threads_id[datacenter_id]
 
                 net_type = sce_net['type']
+                net_vim_name = None
                 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'}  # 'shared': True
 
                 if not net_name:
@@ -3216,6 +3223,7 @@ def create_instance(mydb, tenant_id, instance_dict):
                 db_net = {
                     "uuid": net_uuid,
                     'vim_net_id': None,
+                    "vim_name": net_vim_name,
                     "instance_scenario_id": instance_uuid,
                     "sce_net_id": sce_net["uuid"],
                     "created": create_network,
@@ -3261,6 +3269,7 @@ def create_instance(mydb, tenant_id, instance_dict):
             "myvims": myvims,
             "cloud_config": cloud_config,
             "RO_pub_key": tenant[0].get('RO_pub_key'),
+            "instance_parameters": instance_dict,
         }
         vnf_params_out = {
             "task_index": task_index,
@@ -3275,7 +3284,7 @@ def create_instance(mydb, tenant_id, instance_dict):
             "sce_net2instance": sce_net2instance,
         }
         # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name'])
-        for sce_vnf in scenarioDict['vnfs']:  # sce_vnf_list:
+        for sce_vnf in scenarioDict.get('vnfs'):  # sce_vnf_list:
             instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList)
         task_index = vnf_params_out["task_index"]
         uuid_list = vnf_params_out["uuid_list"]
@@ -3528,6 +3537,7 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
         db_net = {
             "uuid": net_uuid,
             'vim_net_id': None,
+            "vim_name": net_name,
             "instance_scenario_id": instance_uuid,
             "net_id": net["uuid"],
             "created": True,
@@ -3579,7 +3589,7 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
     if sce_vnf.get('mgmt_access'):
         ssh_access = sce_vnf['mgmt_access'].get('config-access', {}).get('ssh-access')
     vnf_availability_zones = []
-    for vm in sce_vnf['vms']:
+    for vm in sce_vnf.get('vms'):
         vm_av = vm.get('availability_zone')
         if vm_av and vm_av not in vnf_availability_zones:
             vnf_availability_zones.append(vm_av)
@@ -3613,6 +3623,10 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
     db_instance_vnfs.append(db_instance_vnf)
 
     for vm in sce_vnf['vms']:
+        # skip PDUs
+        if vm.get("pdu_type"):
+            continue
+
         myVMDict = {}
         sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name']
         myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64])
@@ -3664,6 +3678,7 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
         myVMDict['networks'] = []
         task_depends_on = []
         # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
+        is_management_vm = False
         db_vm_ifaces = []
         for iface in vm['interfaces']:
             netDict = {}
@@ -3696,7 +3711,10 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
                             Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
                 else:
                     raise NfvoException(e_text, HTTP_Internal_Server_Error)
-            if netDict["use"] == "mgmt" or netDict["use"] == "bridge":
+            if netDict["use"] == "mgmt":
+                is_management_vm = True
+                netDict["type"] = "virtual"
+            if netDict["use"] == "bridge":
                 netDict["type"] = "virtual"
             if iface.get("vpci"):
                 netDict['vpci'] = iface['vpci']
@@ -3750,9 +3768,16 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList):
 
         # We add the RO key to cloud_config if vnf will need ssh access
         cloud_config_vm = cloud_config
-        if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
-            RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
-            cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
+        if is_management_vm and params["instance_parameters"].get("mgmt_keys"):
+            cloud_config_vm = unify_cloud_config({"key-pairs": params["instance_parameters"]["mgmt_keys"]},
+                                                  cloud_config_vm)
+
+        if vm.get("instance_parameters") and vm["instance_parameters"].get("mgmt_keys"):
+            cloud_config_vm = unify_cloud_config({"key-pairs": vm["instance_parameters"]["mgmt_keys"]},
+                                                 cloud_config_vm)
+        # if ssh_access and ssh_access['required'] and ssh_access['default-user'] and tenant[0].get('RO_pub_key'):
+        #     RO_key = {"key-pairs": [tenant[0]['RO_pub_key']]}
+        #     cloud_config_vm = unify_cloud_config(cloud_config_vm, RO_key)
         if vm.get("boot_data"):
             cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config_vm)
 
@@ -4007,7 +4032,7 @@ def delete_instance(mydb, tenant_id, instance_id):
 
     # 2.2 deleting VMs
     # vm_fail_list=[]
-    for sce_vnf in instanceDict['vnfs']:
+    for sce_vnf in instanceDict.get('vnfs', ()):
         datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
         vimthread_affected[sce_vnf["datacenter_tenant_id"]] = None
         if datacenter_key not in myvims:
@@ -4060,7 +4085,7 @@ def delete_instance(mydb, tenant_id, instance_id):
         datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
         if datacenter_key not in myvims:
             try:
-                _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
+                _,myvim_thread = get_vim_thread(mydb, tenant_id, net["datacenter_id"], net["datacenter_tenant_id"])
             except NfvoException as e:
                 logger.error(str(e))
                 myvim_thread = None
@@ -4331,6 +4356,8 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
             "description": "SCALE",
         }
         vm_result["instance_action_id"] = instance_action_id
+        vm_result["created"] = []
+        vm_result["deleted"] = []
         task_index = 0
         for vdu in action_dict["vdu-scaling"]:
             vdu_id = vdu.get("vdu-id")
@@ -4338,17 +4365,17 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
             member_vnf_index = vdu.get("member-vnf-index")
             vdu_count = vdu.get("count", 1)
             if vdu_id:
-                target_vm = mydb.get_rows(
+                target_vms = mydb.get_rows(
                     FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid",
                     WHERE={"vms.uuid": vdu_id},
                     ORDER_BY="vms.created_at"
                 )
-                if not target_vm:
+                if not target_vms:
                     raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), HTTP_Not_Found)
             else:
                 if not osm_vdu_id and not member_vnf_index:
                     raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
-                target_vm = mydb.get_rows(
+                target_vms = mydb.get_rows(
                     # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"),
                     FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\
                          " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\
@@ -4356,38 +4383,41 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
                     WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index},
                     ORDER_BY="ivms.created_at"
                 )
-                if not target_vm:
+                if not target_vms:
                     raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), HTTP_Not_Found)
-                vdu_id = target_vm[-1]["uuid"]
-            vm_result[vdu_id] = {"created": [], "deleted": [], "description": "scheduled"}
-            target_vm = target_vm[-1]
+                vdu_id = target_vms[-1]["uuid"]
+            target_vm = target_vms[-1]
             datacenter = target_vm["datacenter_id"]
             myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter)
+
             if vdu["type"] == "delete":
-                # look for nm
-                vm_interfaces = None
-                for sce_vnf in instanceDict['vnfs']:
-                    for vm in sce_vnf['vms']:
-                        if vm["uuid"] == vdu_id:
-                            vm_interfaces = vm["interfaces"]
-                            break
+                for index in range(0, vdu_count):
+                    target_vm = target_vms[-1-index]
+                    vdu_id = target_vm["uuid"]
+                    # look for nm
+                    vm_interfaces = None
+                    for sce_vnf in instanceDict['vnfs']:
+                        for vm in sce_vnf['vms']:
+                            if vm["uuid"] == vdu_id:
+                                vm_interfaces = vm["interfaces"]
+                                break
 
-                db_vim_action = {
-                    "instance_action_id": instance_action_id,
-                    "task_index": task_index,
-                    "datacenter_vim_id": target_vm["datacenter_tenant_id"],
-                    "action": "DELETE",
-                    "status": "SCHEDULED",
-                    "item": "instance_vms",
-                    "item_id": target_vm["uuid"],
-                    "extra": yaml.safe_dump({"params": vm_interfaces},
-                                            default_flow_style=True, width=256)
-                }
-                task_index += 1
-                db_vim_actions.append(db_vim_action)
-                vm_result[vdu_id]["deleted"].append(vdu_id)
-                # delete from database
-                db_instance_vms.append({"TO-DELETE": vdu_id})
+                    db_vim_action = {
+                        "instance_action_id": instance_action_id,
+                        "task_index": task_index,
+                        "datacenter_vim_id": target_vm["datacenter_tenant_id"],
+                        "action": "DELETE",
+                        "status": "SCHEDULED",
+                        "item": "instance_vms",
+                        "item_id": vdu_id,
+                        "extra": yaml.safe_dump({"params": vm_interfaces},
+                                                default_flow_style=True, width=256)
+                    }
+                    task_index += 1
+                    db_vim_actions.append(db_vim_action)
+                    vm_result["deleted"].append(vdu_id)
+                    # delete from database
+                    db_instance_vms.append({"TO-DELETE": vdu_id})
 
             else:  # vdu["type"] == "create":
                 iface2iface = {}
@@ -4422,7 +4452,7 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
                     vm_name = target_vm.get('vim_name')
                     try:
                         suffix = vm_name.rfind("-")
-                        vm_name = vm_name[:suffix+1] + str(1 + int(vm_name[suffix+1:]))
+                        vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
                     except Exception:
                         pass
                     db_instance_vm = {
@@ -4479,7 +4509,7 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
                     }
                     task_index += 1
                     db_vim_actions.append(db_vim_action)
-                    vm_result[vdu_id]["created"].append(vm_uuid)
+                    vm_result["created"].append(vm_uuid)
 
         db_instance_action["number_tasks"] = task_index
         db_tables = [