@Test @SuppressWarnings(value = { "static-access"}) public void deleteVM() { DeploymentDAO deploymentDAO = mock(DeploymentDAO.class); Deployment deployment = new Deployment(); deployment.setOvf(threeTierWebAppOvfString); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); when(deploymentDAO.getById(11)).thenReturn(deployment); VM vm = new VM(); vm.setOvfId("haproxy"); vm.setProviderVmId("provider-id"); VMDAO vmDAO = mock(VMDAO.class); when(vmDAO.getById(33)).thenReturn(vm); List<VM> vms = new ArrayList<VM>(); vms.add(vm); vms.add(new VM()); when(vmDAO.getVMsWithOVfIdForDeploymentNotDeleted("haproxy", 11)).thenReturn(vms); VmManagerClient vmMaClient = mock(VmManagerClient.class); PaaSEnergyModeller em = mock(PaaSEnergyModeller.class); VMRest vmRest = new VMRest(); vmRest.deploymentDAO = deploymentDAO; vmRest.vmDAO = vmDAO; vmRest.vmManagerClient = vmMaClient; vmRest.energyModeller = em; Response response = vmRest.deleteVM("", "11", "33"); assertEquals(204, response.getStatus()); assertEquals("", (String) response.getEntity()); verify(em, times(1)).writeDeploymentRequest(null, "", "0", "0", "provider-id", "DELETED"); verify(vmMaClient).deleteVM("provider-id"); ArgumentCaptor<VM> vmCaptor = ArgumentCaptor.forClass(VM.class); verify(vmDAO, times(1)).update(vmCaptor.capture()); assertEquals(vm.getOvfId(), vmCaptor.getValue().getOvfId()); assertEquals(vm.getProviderVmId(), vmCaptor.getValue().getProviderVmId()); }
@DELETE @Path("{vm_id}") public Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId) { int deploymentIdInt = 0; try { deploymentIdInt = Integer.parseInt(deploymentId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid deploymentID number!!!"); } Deployment deployment = deploymentDAO.getById(deploymentIdInt); if(deployment == null) { return buildResponse(Status.BAD_REQUEST, "No deployment by that ID in the DB!!!"); } if(!deployment.getStatus().equals(APPLICATION_STATUS_DEPLOYED)) { return buildResponse(Status.BAD_REQUEST, "No Active deployment!!!"); } int vmIdInt = 0; try { vmIdInt = Integer.parseInt(vmId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid vmId number!!!"); } VM vm = vmDAO.getById(vmIdInt); if(vm == null) { return buildResponse(Status.BAD_REQUEST, "No VM by that Id in the database!!!"); } VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(OVFUtils.getVirtualSystemForOvfId(deployment.getOvf(), vm.getOvfId())); List<VM> vms = vmDAO.getVMsWithOVfIdForDeploymentNotDeleted(vm.getOvfId(), deploymentIdInt); if(vms.size() <= vmLimits.getLowerNumberOfVMs()) { return buildResponse(Status.BAD_REQUEST, vm.getOvfId() + " number of VMs already at its minimum!!!"); } logger.info("DELETING VM: " + vm.getProviderVmId()); vmManagerClient.deleteVM(vm.getProviderVmId()); vm.setStatus(STATE_VM_DELETED); vmDAO.update(vm); getEnergyModeller().writeDeploymentRequest(vm.getProviderId(), applicationName, "" + deployment.getId(), "" + vm.getId(), vm.getProviderVmId(), vm.getStatus()); AmqpProducer.sendVMDeletedMessage(applicationName, deployment, vm); return buildResponse(Status.NO_CONTENT, ""); }
VMRest extends AbstractRest { @DELETE @Path("{vm_id}") public Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId) { int deploymentIdInt = 0; try { deploymentIdInt = Integer.parseInt(deploymentId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid deploymentID number!!!"); } Deployment deployment = deploymentDAO.getById(deploymentIdInt); if(deployment == null) { return buildResponse(Status.BAD_REQUEST, "No deployment by that ID in the DB!!!"); } if(!deployment.getStatus().equals(APPLICATION_STATUS_DEPLOYED)) { return buildResponse(Status.BAD_REQUEST, "No Active deployment!!!"); } int vmIdInt = 0; try { vmIdInt = Integer.parseInt(vmId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid vmId number!!!"); } VM vm = vmDAO.getById(vmIdInt); if(vm == null) { return buildResponse(Status.BAD_REQUEST, "No VM by that Id in the database!!!"); } VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(OVFUtils.getVirtualSystemForOvfId(deployment.getOvf(), vm.getOvfId())); List<VM> vms = vmDAO.getVMsWithOVfIdForDeploymentNotDeleted(vm.getOvfId(), deploymentIdInt); if(vms.size() <= vmLimits.getLowerNumberOfVMs()) { return buildResponse(Status.BAD_REQUEST, vm.getOvfId() + " number of VMs already at its minimum!!!"); } logger.info("DELETING VM: " + vm.getProviderVmId()); vmManagerClient.deleteVM(vm.getProviderVmId()); vm.setStatus(STATE_VM_DELETED); vmDAO.update(vm); getEnergyModeller().writeDeploymentRequest(vm.getProviderId(), applicationName, "" + deployment.getId(), "" + vm.getId(), vm.getProviderVmId(), vm.getStatus()); AmqpProducer.sendVMDeletedMessage(applicationName, deployment, vm); return buildResponse(Status.NO_CONTENT, ""); } }
VMRest extends AbstractRest { @DELETE @Path("{vm_id}") public Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId) { int deploymentIdInt = 0; try { deploymentIdInt = Integer.parseInt(deploymentId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid deploymentID number!!!"); } Deployment deployment = deploymentDAO.getById(deploymentIdInt); if(deployment == null) { return buildResponse(Status.BAD_REQUEST, "No deployment by that ID in the DB!!!"); } if(!deployment.getStatus().equals(APPLICATION_STATUS_DEPLOYED)) { return buildResponse(Status.BAD_REQUEST, "No Active deployment!!!"); } int vmIdInt = 0; try { vmIdInt = Integer.parseInt(vmId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid vmId number!!!"); } VM vm = vmDAO.getById(vmIdInt); if(vm == null) { return buildResponse(Status.BAD_REQUEST, "No VM by that Id in the database!!!"); } VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(OVFUtils.getVirtualSystemForOvfId(deployment.getOvf(), vm.getOvfId())); List<VM> vms = vmDAO.getVMsWithOVfIdForDeploymentNotDeleted(vm.getOvfId(), deploymentIdInt); if(vms.size() <= vmLimits.getLowerNumberOfVMs()) { return buildResponse(Status.BAD_REQUEST, vm.getOvfId() + " number of VMs already at its minimum!!!"); } logger.info("DELETING VM: " + vm.getProviderVmId()); vmManagerClient.deleteVM(vm.getProviderVmId()); vm.setStatus(STATE_VM_DELETED); vmDAO.update(vm); getEnergyModeller().writeDeploymentRequest(vm.getProviderId(), applicationName, "" + deployment.getId(), "" + vm.getId(), vm.getProviderVmId(), vm.getStatus()); AmqpProducer.sendVMDeletedMessage(applicationName, deployment, vm); return buildResponse(Status.NO_CONTENT, ""); }  }
VMRest extends AbstractRest { @DELETE @Path("{vm_id}") public Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId) { int deploymentIdInt = 0; try { deploymentIdInt = Integer.parseInt(deploymentId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid deploymentID number!!!"); } Deployment deployment = deploymentDAO.getById(deploymentIdInt); if(deployment == null) { return buildResponse(Status.BAD_REQUEST, "No deployment by that ID in the DB!!!"); } if(!deployment.getStatus().equals(APPLICATION_STATUS_DEPLOYED)) { return buildResponse(Status.BAD_REQUEST, "No Active deployment!!!"); } int vmIdInt = 0; try { vmIdInt = Integer.parseInt(vmId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid vmId number!!!"); } VM vm = vmDAO.getById(vmIdInt); if(vm == null) { return buildResponse(Status.BAD_REQUEST, "No VM by that Id in the database!!!"); } VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(OVFUtils.getVirtualSystemForOvfId(deployment.getOvf(), vm.getOvfId())); List<VM> vms = vmDAO.getVMsWithOVfIdForDeploymentNotDeleted(vm.getOvfId(), deploymentIdInt); if(vms.size() <= vmLimits.getLowerNumberOfVMs()) { return buildResponse(Status.BAD_REQUEST, vm.getOvfId() + " number of VMs already at its minimum!!!"); } logger.info("DELETING VM: " + vm.getProviderVmId()); vmManagerClient.deleteVM(vm.getProviderVmId()); vm.setStatus(STATE_VM_DELETED); vmDAO.update(vm); getEnergyModeller().writeDeploymentRequest(vm.getProviderId(), applicationName, "" + deployment.getId(), "" + vm.getId(), vm.getProviderVmId(), vm.getStatus()); AmqpProducer.sendVMDeletedMessage(applicationName, deployment, vm); return buildResponse(Status.NO_CONTENT, ""); }  @GET @Produces(MediaType.APPLICATION_XML) Response getVMs(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId); @POST @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) Response postVM(@PathParam("application_name") String applicationName, 
						   @PathParam("deployment_id") String deploymentId,
						   @DefaultValue("false") @QueryParam("force") boolean force,
						   String payload); @DELETE @Path("{vm_id}") Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId); @GET @Path("{vm_id}") @Produces(MediaType.APPLICATION_XML) Response getVM(@PathParam("application_name") String applicationName,  @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId); @GET @Path("{vm_id}/energy-estimation") @Produces(MediaType.APPLICATION_XML) Response getEnergyEstimation(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @DefaultValue("0") @QueryParam("duration") long duration); @GET @Path("{vm_id}/events/{event_id}/energy-estimation") @Produces(MediaType.APPLICATION_XML) Response getEnergyEstimationForAnEvent(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("duration") long duration); @GET @Path("{vm_id}/power-estimation") @Produces(MediaType.APPLICATION_XML) Response getPowerEstimation(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId); @GET @Path("{vm_id}/events/{event_id}/power-estimation") @Produces(MediaType.APPLICATION_XML) Response getPowerEstimationForAnEvent(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId); @GET @Path("{vm_id}/events/{event_id}/cost-estimation") @Produces(MediaType.APPLICATION_XML) Response getCostEstimation(@PathParam("application_name") String applicationName, 
			                          @PathParam("deployment_id") String deploymentId,
			                          @PathParam("vm_id") String vmId,
			                          @PathParam("event_id") String eventId); @GET @Path("{vm_id}/events/{event_id}/energy-consumption") @Produces(MediaType.APPLICATION_XML) Response getEventEnergyConsumption(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("startTime") long startTime,
			                             @DefaultValue("0") @QueryParam("endTime") long endTime); @GET @Path("{vm_id}/events/{event_id}/power-consumption") @Produces(MediaType.APPLICATION_XML) Response getEventPowerConsumption(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @QueryParam("startTime") long startTime,
			                             @QueryParam("endTime") long endTime,
			                             @Context UriInfo uriInfo); @GET @Path("{vm_id}/energy-consumption") @Produces(MediaType.APPLICATION_XML) Response getEnergyConsumption(@PathParam("application_name") String applicationName, 
										@PathParam("deployment_id") String deploymentId,
										@PathParam("vm_id") String vmId,
										@DefaultValue("0") @QueryParam("startTime") long startTime,
										@DefaultValue("0") @QueryParam("endTime") long endTime); @GET @Path("{vm_id}/power-consumption") @Produces(MediaType.APPLICATION_XML) Response getPowerConsumption(@PathParam("application_name") String applicationName, 
										@PathParam("deployment_id") String deploymentId,
										@PathParam("vm_id") String vmId,
										@DefaultValue("0") @QueryParam("startTime") long startTime,
										@DefaultValue("0") @QueryParam("endTime") long endTime,
										@Context UriInfo uriInfo); @GET @Path("{vm_id}/events/{event_id}/energy-sample") @Produces(MediaType.APPLICATION_XML) Response getEnergySample(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("startTime") long startTime,
			                             @DefaultValue("0") @QueryParam("endTime") long endTime,
			                             @DefaultValue("0") @QueryParam("interval") long interval); }
VMRest extends AbstractRest { @DELETE @Path("{vm_id}") public Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId) { int deploymentIdInt = 0; try { deploymentIdInt = Integer.parseInt(deploymentId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid deploymentID number!!!"); } Deployment deployment = deploymentDAO.getById(deploymentIdInt); if(deployment == null) { return buildResponse(Status.BAD_REQUEST, "No deployment by that ID in the DB!!!"); } if(!deployment.getStatus().equals(APPLICATION_STATUS_DEPLOYED)) { return buildResponse(Status.BAD_REQUEST, "No Active deployment!!!"); } int vmIdInt = 0; try { vmIdInt = Integer.parseInt(vmId); } catch(NumberFormatException ex) { return buildResponse(Status.BAD_REQUEST, "Invalid vmId number!!!"); } VM vm = vmDAO.getById(vmIdInt); if(vm == null) { return buildResponse(Status.BAD_REQUEST, "No VM by that Id in the database!!!"); } VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(OVFUtils.getVirtualSystemForOvfId(deployment.getOvf(), vm.getOvfId())); List<VM> vms = vmDAO.getVMsWithOVfIdForDeploymentNotDeleted(vm.getOvfId(), deploymentIdInt); if(vms.size() <= vmLimits.getLowerNumberOfVMs()) { return buildResponse(Status.BAD_REQUEST, vm.getOvfId() + " number of VMs already at its minimum!!!"); } logger.info("DELETING VM: " + vm.getProviderVmId()); vmManagerClient.deleteVM(vm.getProviderVmId()); vm.setStatus(STATE_VM_DELETED); vmDAO.update(vm); getEnergyModeller().writeDeploymentRequest(vm.getProviderId(), applicationName, "" + deployment.getId(), "" + vm.getId(), vm.getProviderVmId(), vm.getStatus()); AmqpProducer.sendVMDeletedMessage(applicationName, deployment, vm); return buildResponse(Status.NO_CONTENT, ""); }  @GET @Produces(MediaType.APPLICATION_XML) Response getVMs(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId); @POST @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) Response postVM(@PathParam("application_name") String applicationName, 
						   @PathParam("deployment_id") String deploymentId,
						   @DefaultValue("false") @QueryParam("force") boolean force,
						   String payload); @DELETE @Path("{vm_id}") Response deleteVM(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId); @GET @Path("{vm_id}") @Produces(MediaType.APPLICATION_XML) Response getVM(@PathParam("application_name") String applicationName,  @PathParam("deployment_id") String deploymentId, @PathParam("vm_id") String vmId); @GET @Path("{vm_id}/energy-estimation") @Produces(MediaType.APPLICATION_XML) Response getEnergyEstimation(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @DefaultValue("0") @QueryParam("duration") long duration); @GET @Path("{vm_id}/events/{event_id}/energy-estimation") @Produces(MediaType.APPLICATION_XML) Response getEnergyEstimationForAnEvent(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("duration") long duration); @GET @Path("{vm_id}/power-estimation") @Produces(MediaType.APPLICATION_XML) Response getPowerEstimation(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId); @GET @Path("{vm_id}/events/{event_id}/power-estimation") @Produces(MediaType.APPLICATION_XML) Response getPowerEstimationForAnEvent(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId); @GET @Path("{vm_id}/events/{event_id}/cost-estimation") @Produces(MediaType.APPLICATION_XML) Response getCostEstimation(@PathParam("application_name") String applicationName, 
			                          @PathParam("deployment_id") String deploymentId,
			                          @PathParam("vm_id") String vmId,
			                          @PathParam("event_id") String eventId); @GET @Path("{vm_id}/events/{event_id}/energy-consumption") @Produces(MediaType.APPLICATION_XML) Response getEventEnergyConsumption(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("startTime") long startTime,
			                             @DefaultValue("0") @QueryParam("endTime") long endTime); @GET @Path("{vm_id}/events/{event_id}/power-consumption") @Produces(MediaType.APPLICATION_XML) Response getEventPowerConsumption(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @QueryParam("startTime") long startTime,
			                             @QueryParam("endTime") long endTime,
			                             @Context UriInfo uriInfo); @GET @Path("{vm_id}/energy-consumption") @Produces(MediaType.APPLICATION_XML) Response getEnergyConsumption(@PathParam("application_name") String applicationName, 
										@PathParam("deployment_id") String deploymentId,
										@PathParam("vm_id") String vmId,
										@DefaultValue("0") @QueryParam("startTime") long startTime,
										@DefaultValue("0") @QueryParam("endTime") long endTime); @GET @Path("{vm_id}/power-consumption") @Produces(MediaType.APPLICATION_XML) Response getPowerConsumption(@PathParam("application_name") String applicationName, 
										@PathParam("deployment_id") String deploymentId,
										@PathParam("vm_id") String vmId,
										@DefaultValue("0") @QueryParam("startTime") long startTime,
										@DefaultValue("0") @QueryParam("endTime") long endTime,
										@Context UriInfo uriInfo); @GET @Path("{vm_id}/events/{event_id}/energy-sample") @Produces(MediaType.APPLICATION_XML) Response getEnergySample(@PathParam("application_name") String applicationName, 
			                             @PathParam("deployment_id") String deploymentId,
			                             @PathParam("vm_id") String vmId,
			                             @PathParam("event_id") String eventId,
			                             @DefaultValue("0") @QueryParam("startTime") long startTime,
			                             @DefaultValue("0") @QueryParam("endTime") long endTime,
			                             @DefaultValue("0") @QueryParam("interval") long interval);  }