+ if vdu_id:
+ 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_vms:
+ raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), httperrors.Not_Found)
+ else:
+ if not osm_vdu_id and not member_vnf_index:
+ raise NfvoException("Invalid input vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'")
+ 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"\
+ " join vms on ivms.vm_id=vms.uuid",
+ WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index,
+ "ivnfs.instance_scenario_id": instance_id},
+ ORDER_BY="ivms.created_at"
+ )
+ 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), httperrors.Not_Found)
+ 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":
+ 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:
+ # TODO revise this should not be vm["uuid"] instance_vms["vm_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": vdu_id,
+ "related": target_vm["related"],
+ "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 = {}
+ where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"}
+
+ vim_action_to_clone = mydb.get_rows(FROM="vim_wim_actions", WHERE=where)
+ if not vim_action_to_clone:
+ raise NfvoException("Cannot find the vim_action at database with {}".format(where), httperrors.Internal_Server_Error)
+ vim_action_to_clone = vim_action_to_clone[0]
+ extra = yaml.safe_load(vim_action_to_clone["extra"])
+
+ # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y
+ # TODO do the same for flavor and image when available
+ task_depends_on = []
+ task_params = extra["params"]
+ task_params_networks = deepcopy(task_params[5])
+ for iface in task_params[5]:
+ if iface["net_id"].startswith("TASK-"):
+ if "." not in iface["net_id"]:
+ task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"],
+ iface["net_id"][5:]))
+ iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"],
+ iface["net_id"][5:])
+ else:
+ task_depends_on.append(iface["net_id"][5:])
+ if "mac_address" in iface:
+ del iface["mac_address"]
+
+ vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]})
+ for index in range(0, vdu_count):
+ vm_uuid = str(uuid4())
+ vm_name = target_vm.get('vim_name')
+ try:
+ suffix = vm_name.rfind("-")
+ vm_name = vm_name[:suffix+1] + str(index + 1 + int(vm_name[suffix+1:]))
+ except Exception:
+ pass
+ db_instance_vm = {
+ "uuid": vm_uuid,
+ 'related': vm_uuid,
+ 'instance_vnf_id': target_vm['instance_vnf_id'],
+ 'vm_id': target_vm['vm_id'],
+ 'vim_name': vm_name,
+ }
+ db_instance_vms.append(db_instance_vm)
+
+ for vm_iface in vm_ifaces_to_clone:
+ iface_uuid = str(uuid4())
+ iface2iface[vm_iface["uuid"]] = iface_uuid
+ db_vm_iface = {
+ "uuid": iface_uuid,
+ 'instance_vm_id': vm_uuid,
+ "instance_net_id": vm_iface["instance_net_id"],
+ 'interface_id': vm_iface['interface_id'],
+ 'type': vm_iface['type'],
+ 'floating_ip': vm_iface['floating_ip'],
+ 'port_security': vm_iface['port_security']
+ }
+ db_instance_interfaces.append(db_vm_iface)
+ task_params_copy = deepcopy(task_params)
+ for iface in task_params_copy[5]:
+ iface["uuid"] = iface2iface[iface["uuid"]]
+ # increment ip_address
+ if "ip_address" in iface:
+ ip = iface.get("ip_address")
+ i = ip.rfind(".")
+ if i > 0:
+ try:
+ i += 1
+ ip = ip[i:] + str(int(ip[:i]) + 1)
+ iface["ip_address"] = ip
+ except:
+ iface["ip_address"] = None
+ if vm_name:
+ task_params_copy[0] = vm_name
+ db_vim_action = {
+ "instance_action_id": instance_action_id,
+ "task_index": task_index,
+ "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"],
+ "action": "CREATE",
+ "status": "SCHEDULED",
+ "item": "instance_vms",
+ "item_id": vm_uuid,
+ "related": vm_uuid,
+ # ALF
+ # ALF
+ # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd.
+ # ALF
+ # ALF
+ "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256)
+ }
+ task_index += 1
+ db_vim_actions.append(db_vim_action)
+ vm_result["created"].append(vm_uuid)
+
+ db_instance_action["number_tasks"] = task_index
+ db_tables = [
+ {"instance_vms": db_instance_vms},
+ {"instance_interfaces": db_instance_interfaces},
+ {"instance_actions": db_instance_action},
+ # TODO revise sfps
+ # {"instance_sfis": db_instance_sfis},
+ # {"instance_sfs": db_instance_sfs},
+ # {"instance_classifications": db_instance_classifications},
+ # {"instance_sfps": db_instance_sfps},
+ {"vim_wim_actions": db_vim_actions}
+ ]
+ logger.debug("create_vdu done DB tables: %s",
+ yaml.safe_dump(db_tables, indent=4, default_flow_style=False))
+ mydb.new_rows(db_tables, [])
+ for myvim_thread in myvim_threads_id.values():
+ vim_threads["running"][myvim_thread].insert_task(db_vim_actions)
+
+ return vm_result