@Test public void invalidVMManager() throws Exception { Deployment deployment = new Deployment(); deployment.setId(22); deployment.setOvf(threeTierWebAppOvfString); AmqpMessageReceiver receiver = new AmqpMessageReceiver(Configuration.amqpAddress, Configuration.amqpUsername, Configuration.amqpPassword, "APPLICATION.>", true); AmqpListListener listener = new AmqpListListener(); receiver.setMessageConsumer(listener); Configuration.providerRegistryEndpoint = "http: PRClient prClient = mock(PRClient.class); when(prClient.getVMMClient(-1)).thenReturn(null); DeploymentDAO deploymentDAO = mock(DeploymentDAO.class); when(deploymentDAO.getById(22)).thenReturn(deployment); DeploymentEventService deploymentEventService = mock(DeploymentEventService.class); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setDeploymentId(22); deploymentEvent.setApplicationName("pepito"); deploymentEvent.setDeploymentStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED); DeployEventHandler deploymentEventHandler = new DeployEventHandler(); deploymentEventHandler.deploymentDAO = deploymentDAO; deploymentEventHandler.deploymentEventService = deploymentEventService; deploymentEventHandler.prClient = prClient; deploymentEventHandler.deployDeployment(Event.wrap(deploymentEvent)); Thread.sleep(4000l); ArgumentCaptor<Deployment> deploymentCaptor = ArgumentCaptor.forClass(Deployment.class); verify(deploymentDAO, times(1)).update(deploymentCaptor.capture()); assertEquals(Dictionary.APPLICATION_STATUS_ERROR, deploymentCaptor.getValue().getStatus()); assertEquals(1, listener.getTextMessages().size()); assertEquals("APPLICATION.pepito.DEPLOYMENT.22.ERROR", listener.getTextMessages().get(0).getJMSDestination().toString()); assertEquals("pepito", ModelConverter.jsonToApplicationManagerMessage(listener.getTextMessages().get(0).getText()).getApplicationId()); assertEquals("22", ModelConverter.jsonToApplicationManagerMessage(listener.getTextMessages().get(0).getText()).getDeploymentId()); assertEquals("ERROR", ModelConverter.jsonToApplicationManagerMessage(listener.getTextMessages().get(0).getText()).getStatus()); receiver.close(); }
@Selector(value="topic.deployment.status", reactor="@rootReactor") public void deployDeployment(Event<DeploymentEvent> event) { DeploymentEvent deploymentEvent = event.getData(); if(deploymentEvent.getDeploymentStatus().equals(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED)) { VmManagerClient vmManagerClient = prClient.getVMMClient(deploymentEvent.getProviderId()); Deployment deployment = deploymentDAO.getById(deploymentEvent.getDeploymentId()); if(vmManagerClient != null) { logger.info("Creating a new thread to deploy in infrastructure the deployment: " + deploymentEvent.getDeploymentId()); logger.info("Connecting to the VMM: " + vmManagerClient.getURL()); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYING); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentDeployingMessage(deploymentEvent.getApplicationName(), deployment); try { OvfDefinition ovfDocument = OVFUtils.getOvfDefinition(deployment.getOvf()); String applicationName = OVFUtils.getApplicationName(deployment.getOvf()); VirtualSystemCollection vsc = ovfDocument.getVirtualSystemCollection(); for(int i = 0; i < vsc.getVirtualSystemArray().length; i++) { VirtualSystem virtualSystem = vsc.getVirtualSystemAtIndex(i); String ovfID = virtualSystem.getId(); logger.info(" Starting to deploy Virtual System: " + virtualSystem.getName()); VirtualHardwareSection virtualHardwareSection = virtualSystem.getVirtualHardwareSection(); String diskId = OVFUtils.getDiskId(virtualHardwareSection); String fileId = OVFUtils.getFileId(diskId, ovfDocument.getDiskSection().getDiskArray()); String urlImg = OVFUtils.getUrlImg(ovfDocument, fileId); Image image = null; if(OVFUtils.usesACacheImage(virtualSystem)) { logger.info("This virtual system uses a cache demo image"); image = imageDAO.getDemoCacheImage(fileId, urlImg); if(image == null) { logger.info("The image was not cached, we need to uplaod first"); image = ImageUploader.uploadImage(urlImg, fileId, true, applicationName, vmManagerClient, applicationDAO, imageDAO); } } else { image = ImageUploader.uploadImage(urlImg, fileId, false, applicationName, vmManagerClient, applicationDAO, imageDAO); } String ovfVirtualSystemID = virtualSystem.getId(); VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(virtualSystem); long minNumberVMs = vmLimits.getLowerNumberOfVMs(); long maxNumberVMs = vmLimits.getUpperNumberOfVMs(); int priceSchema = OVFUtils.getPriceSchema(virtualSystem.getProductSectionAtIndex(0)); boolean publicIP = false; try { publicIP = virtualSystem.getProductSectionAtIndex(0).isAssociatePublicIp(); } catch(NullPointerException ex) {} String vmName = virtualSystem.getName(); int cpus = virtualSystem.getVirtualHardwareSection().getNumberOfVirtualCPUs(); int ramMb = virtualSystem.getVirtualHardwareSection().getMemorySize(); String isoPath = OVFUtils.getIsoPathFromVm(virtualSystem.getVirtualHardwareSection(), ovfDocument); logger.info("ISO patth: " + isoPath); int capacity = OVFUtils.getCapacity(ovfDocument, diskId); image = imageDAO.getById(image.getId()); for(int j = 0; j < minNumberVMs; j++) { logger.info(" OVF-ID: " + ovfVirtualSystemID + " #VMs: " + minNumberVMs + " Name: " + vmName + " CPU: " + cpus + " RAM: " + ramMb + " Disk capacity: " + capacity + " ISO Path: " + isoPath + " PUBLIC IP: " + publicIP); int suffixInt = j + 1; String suffix = "_" + suffixInt; String iso = ""; if(isoPath != null) iso = isoPath + suffix ; Vm virtMachine = new Vm(vmName + suffix, image.getProviderImageId(), cpus, ramMb, capacity, 0, iso , ovfDocument.getVirtualSystemCollection().getId(), ovfID, "", publicIP ); logger.debug("virtMachine: " + virtMachine); List<Vm> vms = new ArrayList<Vm>(); vms.add(virtMachine); List<String> vmIds = vmManagerClient.deployVMs(vms); logger.debug("Id: " + vmIds.get(0)); for(String id : vmIds) { VmDeployed vmDeployed = vmManagerClient.getVM(id); VM vmToDB = new VM(); vmToDB.setIp(vmDeployed.getIpAddress()); vmToDB.setOvfId(ovfVirtualSystemID); vmToDB.setStatus(vmDeployed.getState()); vmToDB.setProviderVmId(id); vmToDB.setNumberVMsMax(maxNumberVMs); vmToDB.setNumberVMsMin(minNumberVMs); if(vmLimits.getMinNumberCPUs() > 0) { vmToDB.setCpuMin(vmLimits.getMinNumberCPUs()); } else { vmToDB.setCpuMin(cpus); } vmToDB.setCpuActual(cpus); if(vmLimits.getMaxNumberCPUs() > 0 ) { vmToDB.setCpuMax(vmLimits.getMaxNumberCPUs()); } else { vmToDB.setCpuMax(cpus); } vmToDB.setDiskMin(capacity); vmToDB.setDiskActual(capacity); vmToDB.setDiskMax(capacity); vmToDB.setRamMin(ramMb); vmToDB.setRamActual(ramMb); vmToDB.setRamMax(ramMb); vmToDB.setSwapMax(0); vmToDB.setSwapActual(0); vmToDB.setSwapMin(0); if(priceSchema != -1) { vmToDB.setPriceSchema(new Long(priceSchema)); } vmToDB.setProviderId("" + deploymentEvent.getProviderId()); vmDAO.save(vmToDB); vmToDB.addImage(image); vmDAO.update(vmToDB); deployment.addVM(vmToDB); deploymentDAO.update(deployment); AmqpProducer.sendVMDeployedMessage(applicationName, deployment, vmToDB); } } } deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); try { logger.info("Initializing the deployment into the PriceModeller"); PriceModellerBean.initializeDeployment(deployment); } catch(NullPointerException ex) { logger.warn("PriceModeller object does not exist!!!"); } deploymentDAO.update(deployment); } catch(OvfRuntimeException ex) { logger.info("Error parsing OVF file: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } catch (Exception ex){ logger.info("Error triying to deploy new VMs: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } deploymentEvent.setDeploymentStatus(deployment.getStatus()); AmqpProducer.sendDeploymentDeployedMessage(deploymentEvent.getApplicationName(), deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); } else { logger.info("Error, not available VMM Client"); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } } }
DeployEventHandler { @Selector(value="topic.deployment.status", reactor="@rootReactor") public void deployDeployment(Event<DeploymentEvent> event) { DeploymentEvent deploymentEvent = event.getData(); if(deploymentEvent.getDeploymentStatus().equals(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED)) { VmManagerClient vmManagerClient = prClient.getVMMClient(deploymentEvent.getProviderId()); Deployment deployment = deploymentDAO.getById(deploymentEvent.getDeploymentId()); if(vmManagerClient != null) { logger.info("Creating a new thread to deploy in infrastructure the deployment: " + deploymentEvent.getDeploymentId()); logger.info("Connecting to the VMM: " + vmManagerClient.getURL()); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYING); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentDeployingMessage(deploymentEvent.getApplicationName(), deployment); try { OvfDefinition ovfDocument = OVFUtils.getOvfDefinition(deployment.getOvf()); String applicationName = OVFUtils.getApplicationName(deployment.getOvf()); VirtualSystemCollection vsc = ovfDocument.getVirtualSystemCollection(); for(int i = 0; i < vsc.getVirtualSystemArray().length; i++) { VirtualSystem virtualSystem = vsc.getVirtualSystemAtIndex(i); String ovfID = virtualSystem.getId(); logger.info(" Starting to deploy Virtual System: " + virtualSystem.getName()); VirtualHardwareSection virtualHardwareSection = virtualSystem.getVirtualHardwareSection(); String diskId = OVFUtils.getDiskId(virtualHardwareSection); String fileId = OVFUtils.getFileId(diskId, ovfDocument.getDiskSection().getDiskArray()); String urlImg = OVFUtils.getUrlImg(ovfDocument, fileId); Image image = null; if(OVFUtils.usesACacheImage(virtualSystem)) { logger.info("This virtual system uses a cache demo image"); image = imageDAO.getDemoCacheImage(fileId, urlImg); if(image == null) { logger.info("The image was not cached, we need to uplaod first"); image = ImageUploader.uploadImage(urlImg, fileId, true, applicationName, vmManagerClient, applicationDAO, imageDAO); } } else { image = ImageUploader.uploadImage(urlImg, fileId, false, applicationName, vmManagerClient, applicationDAO, imageDAO); } String ovfVirtualSystemID = virtualSystem.getId(); VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(virtualSystem); long minNumberVMs = vmLimits.getLowerNumberOfVMs(); long maxNumberVMs = vmLimits.getUpperNumberOfVMs(); int priceSchema = OVFUtils.getPriceSchema(virtualSystem.getProductSectionAtIndex(0)); boolean publicIP = false; try { publicIP = virtualSystem.getProductSectionAtIndex(0).isAssociatePublicIp(); } catch(NullPointerException ex) {} String vmName = virtualSystem.getName(); int cpus = virtualSystem.getVirtualHardwareSection().getNumberOfVirtualCPUs(); int ramMb = virtualSystem.getVirtualHardwareSection().getMemorySize(); String isoPath = OVFUtils.getIsoPathFromVm(virtualSystem.getVirtualHardwareSection(), ovfDocument); logger.info("ISO patth: " + isoPath); int capacity = OVFUtils.getCapacity(ovfDocument, diskId); image = imageDAO.getById(image.getId()); for(int j = 0; j < minNumberVMs; j++) { logger.info(" OVF-ID: " + ovfVirtualSystemID + " #VMs: " + minNumberVMs + " Name: " + vmName + " CPU: " + cpus + " RAM: " + ramMb + " Disk capacity: " + capacity + " ISO Path: " + isoPath + " PUBLIC IP: " + publicIP); int suffixInt = j + 1; String suffix = "_" + suffixInt; String iso = ""; if(isoPath != null) iso = isoPath + suffix ; Vm virtMachine = new Vm(vmName + suffix, image.getProviderImageId(), cpus, ramMb, capacity, 0, iso , ovfDocument.getVirtualSystemCollection().getId(), ovfID, "", publicIP ); logger.debug("virtMachine: " + virtMachine); List<Vm> vms = new ArrayList<Vm>(); vms.add(virtMachine); List<String> vmIds = vmManagerClient.deployVMs(vms); logger.debug("Id: " + vmIds.get(0)); for(String id : vmIds) { VmDeployed vmDeployed = vmManagerClient.getVM(id); VM vmToDB = new VM(); vmToDB.setIp(vmDeployed.getIpAddress()); vmToDB.setOvfId(ovfVirtualSystemID); vmToDB.setStatus(vmDeployed.getState()); vmToDB.setProviderVmId(id); vmToDB.setNumberVMsMax(maxNumberVMs); vmToDB.setNumberVMsMin(minNumberVMs); if(vmLimits.getMinNumberCPUs() > 0) { vmToDB.setCpuMin(vmLimits.getMinNumberCPUs()); } else { vmToDB.setCpuMin(cpus); } vmToDB.setCpuActual(cpus); if(vmLimits.getMaxNumberCPUs() > 0 ) { vmToDB.setCpuMax(vmLimits.getMaxNumberCPUs()); } else { vmToDB.setCpuMax(cpus); } vmToDB.setDiskMin(capacity); vmToDB.setDiskActual(capacity); vmToDB.setDiskMax(capacity); vmToDB.setRamMin(ramMb); vmToDB.setRamActual(ramMb); vmToDB.setRamMax(ramMb); vmToDB.setSwapMax(0); vmToDB.setSwapActual(0); vmToDB.setSwapMin(0); if(priceSchema != -1) { vmToDB.setPriceSchema(new Long(priceSchema)); } vmToDB.setProviderId("" + deploymentEvent.getProviderId()); vmDAO.save(vmToDB); vmToDB.addImage(image); vmDAO.update(vmToDB); deployment.addVM(vmToDB); deploymentDAO.update(deployment); AmqpProducer.sendVMDeployedMessage(applicationName, deployment, vmToDB); } } } deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); try { logger.info("Initializing the deployment into the PriceModeller"); PriceModellerBean.initializeDeployment(deployment); } catch(NullPointerException ex) { logger.warn("PriceModeller object does not exist!!!"); } deploymentDAO.update(deployment); } catch(OvfRuntimeException ex) { logger.info("Error parsing OVF file: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } catch (Exception ex){ logger.info("Error triying to deploy new VMs: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } deploymentEvent.setDeploymentStatus(deployment.getStatus()); AmqpProducer.sendDeploymentDeployedMessage(deploymentEvent.getApplicationName(), deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); } else { logger.info("Error, not available VMM Client"); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } } } }
DeployEventHandler { @Selector(value="topic.deployment.status", reactor="@rootReactor") public void deployDeployment(Event<DeploymentEvent> event) { DeploymentEvent deploymentEvent = event.getData(); if(deploymentEvent.getDeploymentStatus().equals(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED)) { VmManagerClient vmManagerClient = prClient.getVMMClient(deploymentEvent.getProviderId()); Deployment deployment = deploymentDAO.getById(deploymentEvent.getDeploymentId()); if(vmManagerClient != null) { logger.info("Creating a new thread to deploy in infrastructure the deployment: " + deploymentEvent.getDeploymentId()); logger.info("Connecting to the VMM: " + vmManagerClient.getURL()); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYING); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentDeployingMessage(deploymentEvent.getApplicationName(), deployment); try { OvfDefinition ovfDocument = OVFUtils.getOvfDefinition(deployment.getOvf()); String applicationName = OVFUtils.getApplicationName(deployment.getOvf()); VirtualSystemCollection vsc = ovfDocument.getVirtualSystemCollection(); for(int i = 0; i < vsc.getVirtualSystemArray().length; i++) { VirtualSystem virtualSystem = vsc.getVirtualSystemAtIndex(i); String ovfID = virtualSystem.getId(); logger.info(" Starting to deploy Virtual System: " + virtualSystem.getName()); VirtualHardwareSection virtualHardwareSection = virtualSystem.getVirtualHardwareSection(); String diskId = OVFUtils.getDiskId(virtualHardwareSection); String fileId = OVFUtils.getFileId(diskId, ovfDocument.getDiskSection().getDiskArray()); String urlImg = OVFUtils.getUrlImg(ovfDocument, fileId); Image image = null; if(OVFUtils.usesACacheImage(virtualSystem)) { logger.info("This virtual system uses a cache demo image"); image = imageDAO.getDemoCacheImage(fileId, urlImg); if(image == null) { logger.info("The image was not cached, we need to uplaod first"); image = ImageUploader.uploadImage(urlImg, fileId, true, applicationName, vmManagerClient, applicationDAO, imageDAO); } } else { image = ImageUploader.uploadImage(urlImg, fileId, false, applicationName, vmManagerClient, applicationDAO, imageDAO); } String ovfVirtualSystemID = virtualSystem.getId(); VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(virtualSystem); long minNumberVMs = vmLimits.getLowerNumberOfVMs(); long maxNumberVMs = vmLimits.getUpperNumberOfVMs(); int priceSchema = OVFUtils.getPriceSchema(virtualSystem.getProductSectionAtIndex(0)); boolean publicIP = false; try { publicIP = virtualSystem.getProductSectionAtIndex(0).isAssociatePublicIp(); } catch(NullPointerException ex) {} String vmName = virtualSystem.getName(); int cpus = virtualSystem.getVirtualHardwareSection().getNumberOfVirtualCPUs(); int ramMb = virtualSystem.getVirtualHardwareSection().getMemorySize(); String isoPath = OVFUtils.getIsoPathFromVm(virtualSystem.getVirtualHardwareSection(), ovfDocument); logger.info("ISO patth: " + isoPath); int capacity = OVFUtils.getCapacity(ovfDocument, diskId); image = imageDAO.getById(image.getId()); for(int j = 0; j < minNumberVMs; j++) { logger.info(" OVF-ID: " + ovfVirtualSystemID + " #VMs: " + minNumberVMs + " Name: " + vmName + " CPU: " + cpus + " RAM: " + ramMb + " Disk capacity: " + capacity + " ISO Path: " + isoPath + " PUBLIC IP: " + publicIP); int suffixInt = j + 1; String suffix = "_" + suffixInt; String iso = ""; if(isoPath != null) iso = isoPath + suffix ; Vm virtMachine = new Vm(vmName + suffix, image.getProviderImageId(), cpus, ramMb, capacity, 0, iso , ovfDocument.getVirtualSystemCollection().getId(), ovfID, "", publicIP ); logger.debug("virtMachine: " + virtMachine); List<Vm> vms = new ArrayList<Vm>(); vms.add(virtMachine); List<String> vmIds = vmManagerClient.deployVMs(vms); logger.debug("Id: " + vmIds.get(0)); for(String id : vmIds) { VmDeployed vmDeployed = vmManagerClient.getVM(id); VM vmToDB = new VM(); vmToDB.setIp(vmDeployed.getIpAddress()); vmToDB.setOvfId(ovfVirtualSystemID); vmToDB.setStatus(vmDeployed.getState()); vmToDB.setProviderVmId(id); vmToDB.setNumberVMsMax(maxNumberVMs); vmToDB.setNumberVMsMin(minNumberVMs); if(vmLimits.getMinNumberCPUs() > 0) { vmToDB.setCpuMin(vmLimits.getMinNumberCPUs()); } else { vmToDB.setCpuMin(cpus); } vmToDB.setCpuActual(cpus); if(vmLimits.getMaxNumberCPUs() > 0 ) { vmToDB.setCpuMax(vmLimits.getMaxNumberCPUs()); } else { vmToDB.setCpuMax(cpus); } vmToDB.setDiskMin(capacity); vmToDB.setDiskActual(capacity); vmToDB.setDiskMax(capacity); vmToDB.setRamMin(ramMb); vmToDB.setRamActual(ramMb); vmToDB.setRamMax(ramMb); vmToDB.setSwapMax(0); vmToDB.setSwapActual(0); vmToDB.setSwapMin(0); if(priceSchema != -1) { vmToDB.setPriceSchema(new Long(priceSchema)); } vmToDB.setProviderId("" + deploymentEvent.getProviderId()); vmDAO.save(vmToDB); vmToDB.addImage(image); vmDAO.update(vmToDB); deployment.addVM(vmToDB); deploymentDAO.update(deployment); AmqpProducer.sendVMDeployedMessage(applicationName, deployment, vmToDB); } } } deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); try { logger.info("Initializing the deployment into the PriceModeller"); PriceModellerBean.initializeDeployment(deployment); } catch(NullPointerException ex) { logger.warn("PriceModeller object does not exist!!!"); } deploymentDAO.update(deployment); } catch(OvfRuntimeException ex) { logger.info("Error parsing OVF file: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } catch (Exception ex){ logger.info("Error triying to deploy new VMs: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } deploymentEvent.setDeploymentStatus(deployment.getStatus()); AmqpProducer.sendDeploymentDeployedMessage(deploymentEvent.getApplicationName(), deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); } else { logger.info("Error, not available VMM Client"); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } } }  }
DeployEventHandler { @Selector(value="topic.deployment.status", reactor="@rootReactor") public void deployDeployment(Event<DeploymentEvent> event) { DeploymentEvent deploymentEvent = event.getData(); if(deploymentEvent.getDeploymentStatus().equals(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED)) { VmManagerClient vmManagerClient = prClient.getVMMClient(deploymentEvent.getProviderId()); Deployment deployment = deploymentDAO.getById(deploymentEvent.getDeploymentId()); if(vmManagerClient != null) { logger.info("Creating a new thread to deploy in infrastructure the deployment: " + deploymentEvent.getDeploymentId()); logger.info("Connecting to the VMM: " + vmManagerClient.getURL()); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYING); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentDeployingMessage(deploymentEvent.getApplicationName(), deployment); try { OvfDefinition ovfDocument = OVFUtils.getOvfDefinition(deployment.getOvf()); String applicationName = OVFUtils.getApplicationName(deployment.getOvf()); VirtualSystemCollection vsc = ovfDocument.getVirtualSystemCollection(); for(int i = 0; i < vsc.getVirtualSystemArray().length; i++) { VirtualSystem virtualSystem = vsc.getVirtualSystemAtIndex(i); String ovfID = virtualSystem.getId(); logger.info(" Starting to deploy Virtual System: " + virtualSystem.getName()); VirtualHardwareSection virtualHardwareSection = virtualSystem.getVirtualHardwareSection(); String diskId = OVFUtils.getDiskId(virtualHardwareSection); String fileId = OVFUtils.getFileId(diskId, ovfDocument.getDiskSection().getDiskArray()); String urlImg = OVFUtils.getUrlImg(ovfDocument, fileId); Image image = null; if(OVFUtils.usesACacheImage(virtualSystem)) { logger.info("This virtual system uses a cache demo image"); image = imageDAO.getDemoCacheImage(fileId, urlImg); if(image == null) { logger.info("The image was not cached, we need to uplaod first"); image = ImageUploader.uploadImage(urlImg, fileId, true, applicationName, vmManagerClient, applicationDAO, imageDAO); } } else { image = ImageUploader.uploadImage(urlImg, fileId, false, applicationName, vmManagerClient, applicationDAO, imageDAO); } String ovfVirtualSystemID = virtualSystem.getId(); VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(virtualSystem); long minNumberVMs = vmLimits.getLowerNumberOfVMs(); long maxNumberVMs = vmLimits.getUpperNumberOfVMs(); int priceSchema = OVFUtils.getPriceSchema(virtualSystem.getProductSectionAtIndex(0)); boolean publicIP = false; try { publicIP = virtualSystem.getProductSectionAtIndex(0).isAssociatePublicIp(); } catch(NullPointerException ex) {} String vmName = virtualSystem.getName(); int cpus = virtualSystem.getVirtualHardwareSection().getNumberOfVirtualCPUs(); int ramMb = virtualSystem.getVirtualHardwareSection().getMemorySize(); String isoPath = OVFUtils.getIsoPathFromVm(virtualSystem.getVirtualHardwareSection(), ovfDocument); logger.info("ISO patth: " + isoPath); int capacity = OVFUtils.getCapacity(ovfDocument, diskId); image = imageDAO.getById(image.getId()); for(int j = 0; j < minNumberVMs; j++) { logger.info(" OVF-ID: " + ovfVirtualSystemID + " #VMs: " + minNumberVMs + " Name: " + vmName + " CPU: " + cpus + " RAM: " + ramMb + " Disk capacity: " + capacity + " ISO Path: " + isoPath + " PUBLIC IP: " + publicIP); int suffixInt = j + 1; String suffix = "_" + suffixInt; String iso = ""; if(isoPath != null) iso = isoPath + suffix ; Vm virtMachine = new Vm(vmName + suffix, image.getProviderImageId(), cpus, ramMb, capacity, 0, iso , ovfDocument.getVirtualSystemCollection().getId(), ovfID, "", publicIP ); logger.debug("virtMachine: " + virtMachine); List<Vm> vms = new ArrayList<Vm>(); vms.add(virtMachine); List<String> vmIds = vmManagerClient.deployVMs(vms); logger.debug("Id: " + vmIds.get(0)); for(String id : vmIds) { VmDeployed vmDeployed = vmManagerClient.getVM(id); VM vmToDB = new VM(); vmToDB.setIp(vmDeployed.getIpAddress()); vmToDB.setOvfId(ovfVirtualSystemID); vmToDB.setStatus(vmDeployed.getState()); vmToDB.setProviderVmId(id); vmToDB.setNumberVMsMax(maxNumberVMs); vmToDB.setNumberVMsMin(minNumberVMs); if(vmLimits.getMinNumberCPUs() > 0) { vmToDB.setCpuMin(vmLimits.getMinNumberCPUs()); } else { vmToDB.setCpuMin(cpus); } vmToDB.setCpuActual(cpus); if(vmLimits.getMaxNumberCPUs() > 0 ) { vmToDB.setCpuMax(vmLimits.getMaxNumberCPUs()); } else { vmToDB.setCpuMax(cpus); } vmToDB.setDiskMin(capacity); vmToDB.setDiskActual(capacity); vmToDB.setDiskMax(capacity); vmToDB.setRamMin(ramMb); vmToDB.setRamActual(ramMb); vmToDB.setRamMax(ramMb); vmToDB.setSwapMax(0); vmToDB.setSwapActual(0); vmToDB.setSwapMin(0); if(priceSchema != -1) { vmToDB.setPriceSchema(new Long(priceSchema)); } vmToDB.setProviderId("" + deploymentEvent.getProviderId()); vmDAO.save(vmToDB); vmToDB.addImage(image); vmDAO.update(vmToDB); deployment.addVM(vmToDB); deploymentDAO.update(deployment); AmqpProducer.sendVMDeployedMessage(applicationName, deployment, vmToDB); } } } deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); try { logger.info("Initializing the deployment into the PriceModeller"); PriceModellerBean.initializeDeployment(deployment); } catch(NullPointerException ex) { logger.warn("PriceModeller object does not exist!!!"); } deploymentDAO.update(deployment); } catch(OvfRuntimeException ex) { logger.info("Error parsing OVF file: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } catch (Exception ex){ logger.info("Error triying to deploy new VMs: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } deploymentEvent.setDeploymentStatus(deployment.getStatus()); AmqpProducer.sendDeploymentDeployedMessage(deploymentEvent.getApplicationName(), deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); } else { logger.info("Error, not available VMM Client"); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } } }  @Selector(value="topic.deployment.status", reactor="@rootReactor") void deployDeployment(Event<DeploymentEvent> event); }
DeployEventHandler { @Selector(value="topic.deployment.status", reactor="@rootReactor") public void deployDeployment(Event<DeploymentEvent> event) { DeploymentEvent deploymentEvent = event.getData(); if(deploymentEvent.getDeploymentStatus().equals(Dictionary.APPLICATION_STATUS_CONTEXTUALIZED)) { VmManagerClient vmManagerClient = prClient.getVMMClient(deploymentEvent.getProviderId()); Deployment deployment = deploymentDAO.getById(deploymentEvent.getDeploymentId()); if(vmManagerClient != null) { logger.info("Creating a new thread to deploy in infrastructure the deployment: " + deploymentEvent.getDeploymentId()); logger.info("Connecting to the VMM: " + vmManagerClient.getURL()); deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYING); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentDeployingMessage(deploymentEvent.getApplicationName(), deployment); try { OvfDefinition ovfDocument = OVFUtils.getOvfDefinition(deployment.getOvf()); String applicationName = OVFUtils.getApplicationName(deployment.getOvf()); VirtualSystemCollection vsc = ovfDocument.getVirtualSystemCollection(); for(int i = 0; i < vsc.getVirtualSystemArray().length; i++) { VirtualSystem virtualSystem = vsc.getVirtualSystemAtIndex(i); String ovfID = virtualSystem.getId(); logger.info(" Starting to deploy Virtual System: " + virtualSystem.getName()); VirtualHardwareSection virtualHardwareSection = virtualSystem.getVirtualHardwareSection(); String diskId = OVFUtils.getDiskId(virtualHardwareSection); String fileId = OVFUtils.getFileId(diskId, ovfDocument.getDiskSection().getDiskArray()); String urlImg = OVFUtils.getUrlImg(ovfDocument, fileId); Image image = null; if(OVFUtils.usesACacheImage(virtualSystem)) { logger.info("This virtual system uses a cache demo image"); image = imageDAO.getDemoCacheImage(fileId, urlImg); if(image == null) { logger.info("The image was not cached, we need to uplaod first"); image = ImageUploader.uploadImage(urlImg, fileId, true, applicationName, vmManagerClient, applicationDAO, imageDAO); } } else { image = ImageUploader.uploadImage(urlImg, fileId, false, applicationName, vmManagerClient, applicationDAO, imageDAO); } String ovfVirtualSystemID = virtualSystem.getId(); VMLimits vmLimits = OVFUtils.getUpperAndLowerVMlimits(virtualSystem); long minNumberVMs = vmLimits.getLowerNumberOfVMs(); long maxNumberVMs = vmLimits.getUpperNumberOfVMs(); int priceSchema = OVFUtils.getPriceSchema(virtualSystem.getProductSectionAtIndex(0)); boolean publicIP = false; try { publicIP = virtualSystem.getProductSectionAtIndex(0).isAssociatePublicIp(); } catch(NullPointerException ex) {} String vmName = virtualSystem.getName(); int cpus = virtualSystem.getVirtualHardwareSection().getNumberOfVirtualCPUs(); int ramMb = virtualSystem.getVirtualHardwareSection().getMemorySize(); String isoPath = OVFUtils.getIsoPathFromVm(virtualSystem.getVirtualHardwareSection(), ovfDocument); logger.info("ISO patth: " + isoPath); int capacity = OVFUtils.getCapacity(ovfDocument, diskId); image = imageDAO.getById(image.getId()); for(int j = 0; j < minNumberVMs; j++) { logger.info(" OVF-ID: " + ovfVirtualSystemID + " #VMs: " + minNumberVMs + " Name: " + vmName + " CPU: " + cpus + " RAM: " + ramMb + " Disk capacity: " + capacity + " ISO Path: " + isoPath + " PUBLIC IP: " + publicIP); int suffixInt = j + 1; String suffix = "_" + suffixInt; String iso = ""; if(isoPath != null) iso = isoPath + suffix ; Vm virtMachine = new Vm(vmName + suffix, image.getProviderImageId(), cpus, ramMb, capacity, 0, iso , ovfDocument.getVirtualSystemCollection().getId(), ovfID, "", publicIP ); logger.debug("virtMachine: " + virtMachine); List<Vm> vms = new ArrayList<Vm>(); vms.add(virtMachine); List<String> vmIds = vmManagerClient.deployVMs(vms); logger.debug("Id: " + vmIds.get(0)); for(String id : vmIds) { VmDeployed vmDeployed = vmManagerClient.getVM(id); VM vmToDB = new VM(); vmToDB.setIp(vmDeployed.getIpAddress()); vmToDB.setOvfId(ovfVirtualSystemID); vmToDB.setStatus(vmDeployed.getState()); vmToDB.setProviderVmId(id); vmToDB.setNumberVMsMax(maxNumberVMs); vmToDB.setNumberVMsMin(minNumberVMs); if(vmLimits.getMinNumberCPUs() > 0) { vmToDB.setCpuMin(vmLimits.getMinNumberCPUs()); } else { vmToDB.setCpuMin(cpus); } vmToDB.setCpuActual(cpus); if(vmLimits.getMaxNumberCPUs() > 0 ) { vmToDB.setCpuMax(vmLimits.getMaxNumberCPUs()); } else { vmToDB.setCpuMax(cpus); } vmToDB.setDiskMin(capacity); vmToDB.setDiskActual(capacity); vmToDB.setDiskMax(capacity); vmToDB.setRamMin(ramMb); vmToDB.setRamActual(ramMb); vmToDB.setRamMax(ramMb); vmToDB.setSwapMax(0); vmToDB.setSwapActual(0); vmToDB.setSwapMin(0); if(priceSchema != -1) { vmToDB.setPriceSchema(new Long(priceSchema)); } vmToDB.setProviderId("" + deploymentEvent.getProviderId()); vmDAO.save(vmToDB); vmToDB.addImage(image); vmDAO.update(vmToDB); deployment.addVM(vmToDB); deploymentDAO.update(deployment); AmqpProducer.sendVMDeployedMessage(applicationName, deployment, vmToDB); } } } deployment.setStatus(Dictionary.APPLICATION_STATUS_DEPLOYED); try { logger.info("Initializing the deployment into the PriceModeller"); PriceModellerBean.initializeDeployment(deployment); } catch(NullPointerException ex) { logger.warn("PriceModeller object does not exist!!!"); } deploymentDAO.update(deployment); } catch(OvfRuntimeException ex) { logger.info("Error parsing OVF file: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } catch (Exception ex){ logger.info("Error triying to deploy new VMs: " + ex.getMessage()); ex.printStackTrace(); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } deploymentEvent.setDeploymentStatus(deployment.getStatus()); AmqpProducer.sendDeploymentDeployedMessage(deploymentEvent.getApplicationName(), deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); } else { logger.info("Error, not available VMM Client"); deployment.setStatus(Dictionary.APPLICATION_STATUS_ERROR); deploymentDAO.update(deployment); AmqpProducer.sendDeploymentErrorMessage(deploymentEvent.getApplicationName(), deployment); } } }  @Selector(value="topic.deployment.status", reactor="@rootReactor") void deployDeployment(Event<DeploymentEvent> event);  }