@Test public void vnfLifecycleOperationGrantingTest() throws VimException { VirtualNetworkFunctionRecord vnfr = createVirtualNetworkFunctionRecord(); boolean granted; when(vimBroker.getLeftQuota(any(VimInstance.class))).thenReturn(createMaxQuota()); granted = vnfLifecycleOperationGranting.grantLifecycleOperation(vnfr); Assert.assertTrue(granted); when(vimBroker.getLeftQuota(any(VimInstance.class))).thenReturn(createMinQuota()); granted = vnfLifecycleOperationGranting.grantLifecycleOperation(vnfr); Assert.assertFalse(granted); }
private NFVImage getImage( VNFPackage vnfPackage, VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor, String projectId) throws NotFoundException, PluginException, VimException, IncompatibleVNFPackage { Map<String, Object> metadata; NFVImage image = new NFVImage(); Map<String, Object> imageDetails = new HashMap<>(); List<String> vimInstances = new ArrayList<>(); byte[] imageFile = null; YamlJsonParser yaml = new YamlJsonParser(); metadata = yaml.parseMap(new String(this.vnfMetadata.toByteArray())); // Get configuration for NFVImage String[] REQUIRED_PACKAGE_KEYS = new String[] {"name", "image", "vim_types"}; for (String requiredKey : REQUIRED_PACKAGE_KEYS) { if (!metadata.containsKey(requiredKey)) { throw new NotFoundException("Not found " + requiredKey + " of VNFPackage in Metadata.yaml"); } if (metadata.get(requiredKey) == null) { throw new NullPointerException( "Not defined " + requiredKey + " of VNFPackage in Metadata.yaml"); } } vnfPackage.setName((String) metadata.get("name")); if (metadata.containsKey("nfvo_version")) { String nfvo_version = (String) metadata.get("nfvo_version"); String actualNfvoVersion = getNfvoVersion(); if (nfvo_version.equals(actualNfvoVersion)) { vnfPackage.setNfvo_version(nfvo_version); } else { throw new IncompatibleVNFPackage( "The NFVO Version: " + nfvo_version + " specified in the Metadata" + " is not compatible with the this NFVOs version: " + actualNfvoVersion); } } if (metadata.containsKey("scripts-link")) vnfPackage.setScriptsLink((String) metadata.get("scripts-link")); if (metadata.containsKey("vim_types")) { List<String> vimTypes = (List<String>) metadata.get("vim_types"); vnfPackage.setVimTypes(vimTypes); } if (metadata.containsKey("image")) { imageDetails = (Map<String, Object>) metadata.get("image"); String[] REQUIRED_IMAGE_DETAILS = new String[] {"upload"}; log.debug("image: " + imageDetails); for (String requiredKey : REQUIRED_IMAGE_DETAILS) { if (!imageDetails.containsKey(requiredKey)) { throw new NotFoundException( "Not found key: " + requiredKey + "of image in Metadata.yaml"); } if (imageDetails.get(requiredKey) == null) { throw new NullPointerException( "Not defined value of key: " + requiredKey + " of image in Metadata.yaml"); } } // If upload==true -> create a new Image if (imageDetails.get("upload").equals("true") || imageDetails.get("upload").equals("check")) { vnfPackage.setImageLink((String) imageDetails.get("link")); if (metadata.containsKey("image-config")) { log.debug("image-config: " + metadata.get("image-config")); Map<String, Object> imageConfig = (Map<String, Object>) metadata.get("image-config"); // Check if all required keys are available String[] REQUIRED_IMAGE_CONFIG = new String[] { "name", "diskFormat", "containerFormat", "minCPU", "minDisk", "minRam", "isPublic" }; for (String requiredKey : REQUIRED_IMAGE_CONFIG) { if (!imageConfig.containsKey(requiredKey)) { throw new NotFoundException( "Not found key: " + requiredKey + " of image-config in Metadata.yaml"); } if (imageConfig.get(requiredKey) == null) { throw new NullPointerException( "Not defined value of key: " + requiredKey + " of image-config in Metadata.yaml"); } } image.setName((String) imageConfig.get("name")); image.setDiskFormat(((String) imageConfig.get("diskFormat")).toUpperCase()); image.setContainerFormat(((String) imageConfig.get("containerFormat")).toUpperCase()); image.setMinCPU(Integer.toString((Integer) imageConfig.get("minCPU"))); image.setMinDiskSpace((Integer) imageConfig.get("minDisk")); image.setMinRam((Integer) imageConfig.get("minRam")); image.setIsPublic( Boolean.parseBoolean(Integer.toString((Integer) imageConfig.get("minRam")))); } else { throw new NotFoundException( "The image-config is not defined. Please define it to upload a new image"); } } } else { throw new NotFoundException( "The image details are not defined. Please define it to use the right image"); } nsdUtils.fetchVimInstances(virtualNetworkFunctionDescriptor, projectId); if (imageDetails.get("upload").equals("true")) { log.debug("VNFPackageManagement: Uploading a new Image"); if (vnfPackage.getImageLink() == null && imageFile == null) { throw new NotFoundException( "VNFPackageManagement: Neither the image link is defined nor the image file is available. Please define at least one if you want to upload a new image"); } else if (vnfPackage.getImageLink() != null) { log.debug("VNFPackageManagement: Uploading a new Image by using the image link"); for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) { if (vdu.getVimInstanceName() != null) { for (String vimName : vdu.getVimInstanceName()) { VimInstance vimInstance = null; for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) { if (vimName.equals(vi.getName())) vimInstance = vi; } if (!vimInstances.contains( vimInstance.getId())) { // check if we didn't already upload it Vim vim = vimBroker.getVim(vimInstance.getType()); log.debug( "VNFPackageManagement: Uploading a new Image to VimInstance " + vimInstance.getName()); image = vim.add(vimInstance, image, vnfPackage.getImageLink()); if (vdu.getVm_image() == null) vdu.setVm_image(new HashSet<String>()); vdu.getVm_image().add(image.getExtId()); vimInstances.add(vimInstance.getId()); } } } } } else if (imageFile != null) { log.debug("VNFPackageManagement: Uploading a new Image by using the image file"); for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) { if (vdu.getVimInstanceName() != null) { for (String vimName : vdu.getVimInstanceName()) { VimInstance vimInstance = null; for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) { if (vimName.equals(vi.getName())) vimInstance = vi; } if (!vimInstances.contains( vimInstance.getId())) { // check if we didn't already upload it Vim vim = vimBroker.getVim(vimInstance.getType()); log.debug( "VNFPackageManagement: Uploading a new Image to VimInstance " + vimInstance.getName()); image = vim.add(vimInstance, image, imageFile); if (vdu.getVm_image() == null) vdu.setVm_image(new HashSet<String>()); vdu.getVm_image().add(image.getExtId()); vimInstances.add(vimInstance.getId()); } } } } } } else { if (!imageDetails.containsKey("ids") && !imageDetails.containsKey("names")) { throw new NotFoundException( "VNFPackageManagement: Upload option 'false' or 'check' requires at least a list of ids or names to find " + " the right image."); } for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) { if (vdu.getVimInstanceName() != null) { if (vdu.getVimInstanceName().size() != 0) { for (String vimName : vdu.getVimInstanceName()) { VimInstance vimInstance = null; for (VimInstance vi : vimInstanceRepository.findByProjectId(projectId)) { if (vimName.equals(vi.getName())) { vimInstance = vi; } } if (vimInstance == null) { throw new NotFoundException( "Vim Instance with name " + vimName + " was not found in project: " + projectId); } boolean found = false; // First, check for image ids if (imageDetails.containsKey("ids")) { for (NFVImage nfvImage : vimInstance.getImages()) { if (((List) imageDetails.get("ids")).contains(nfvImage.getExtId())) { if (!found) { vdu.getVm_image().add(nfvImage.getExtId()); found = true; } else { throw new NotFoundException( "VNFPackageManagement: Multiple images found with the defined list of IDs. Do not know " + "which one to choose"); } } } } // If no one was found, check for the names if (!found) { if (imageDetails.containsKey("names")) { for (NFVImage nfvImage : vimInstance.getImages()) { if (((List) imageDetails.get("names")).contains(nfvImage.getName())) { if (!found) { vdu.getVm_image().add(nfvImage.getExtId()); found = true; } else { throw new NotFoundException( "VNFPackageManagement: Multiple images found with the same name. Do not know which one to" + " choose. To avoid this, define the id"); } } } } } // if no image was found with the defined ids or names, the image doesn't exist if (!found) { if (imageDetails.get("upload").equals("check")) { if (vnfPackage.getImageLink() == null && imageFile == null) { throw new NotFoundException( "VNFPackageManagement: Neither the image link is defined nor the image file is available. " + "Please define at least one if you want to upload a new image"); } else if (vnfPackage.getImageLink() != null) { log.debug( "VNFPackageManagement: Uploading a new Image by using the image link"); if (!vimInstances.contains( vimInstance.getId())) { // check if we didn't already upload it Vim vim = vimBroker.getVim(vimInstance.getType()); log.debug( "VNFPackageManagement: Uploading a new Image to VimInstance " + vimInstance.getName()); image = vim.add(vimInstance, image, vnfPackage.getImageLink()); if (vdu.getVm_image() == null) { vdu.setVm_image(new HashSet<String>()); } vdu.getVm_image().add(image.getExtId()); vimInstances.add(vimInstance.getId()); } } else if (imageFile != null) { log.debug( "VNFPackageManagement: Uploading a new Image by using the image file"); if (!vimInstances.contains( vimInstance.getId())) { // check if we didn't already upload it Vim vim = vimBroker.getVim(vimInstance.getType()); log.debug( "VNFPackageManagement: Uploading a new Image to VimInstance " + vimInstance.getName()); image = vim.add(vimInstance, image, imageFile); if (vdu.getVm_image() == null) { vdu.setVm_image(new HashSet<String>()); } vimInstances.add(vimInstance.getId()); vdu.getVm_image().add(image.getExtId()); } } } else { throw new NotFoundException( "VNFPackageManagement: Neither the defined ids nor the names were found. Use upload option " + "'check' to get sure that the image will be available"); } } else { log.debug("VNFPackageManagement: Found image"); } } } else { // vimInstanceName is not defined, just put the name into the vdu List names = (List) imageDetails.get("names"); log.debug("Adding names: " + names); vdu.getVm_image().addAll(names); } } else { // vimInstanceName is not defined, just put the name into the vdu List names = (List) imageDetails.get("names"); log.debug("Adding names: " + names); vdu.getVm_image().addAll(names); } } } return image; }