Example usage for org.springframework.boot.json YamlJsonParser YamlJsonParser

List of usage examples for org.springframework.boot.json YamlJsonParser YamlJsonParser

Introduction

In this page you can find the example usage for org.springframework.boot.json YamlJsonParser YamlJsonParser.

Prototype

YamlJsonParser

Source Link

Usage

From source file:org.openbaton.marketplace.core.VNFPackageManagement.java

public VNFPackageMetadata add(String fileName, byte[] pack, boolean imageLink)
        throws IOException, VimException, NotFoundException, SQLException, PluginException,
        AlreadyExistingException, PackageIntegrityException, FailedToUploadException {
    try {/* w w w .j  a v a 2s.  c om*/
        updateVims();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SDKException e) {
        e.printStackTrace();
    }

    VNFPackage vnfPackage = new VNFPackage();
    vnfPackage.setScripts(new HashSet<Script>());
    Map<String, Object> metadata = null;
    VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor = null;
    byte[] imageFile = null;
    NFVImage image = new NFVImage();
    ImageMetadata imageMetadata = new ImageMetadata();

    InputStream tarStream;
    ArchiveInputStream myTarFile;
    try {
        tarStream = new ByteArrayInputStream(pack);
        myTarFile = new ArchiveStreamFactory().createArchiveInputStream("tar", tarStream);
    } catch (ArchiveException e) {
        e.printStackTrace();
        throw new IOException();
    }
    TarArchiveEntry entry;
    Map<String, Object> imageDetails = new HashMap<>();
    while ((entry = (TarArchiveEntry) myTarFile.getNextEntry()) != null) {
        /* Get the name of the file */
        if (entry.isFile() && !entry.getName().startsWith("./._")) {
            log.debug("file inside tar: " + entry.getName());
            byte[] content = new byte[(int) entry.getSize()];
            myTarFile.read(content, 0, content.length);
            if (entry.getName().equals("Metadata.yaml")) {
                YamlJsonParser yaml = new YamlJsonParser();
                log.info(new String(content));
                metadata = yaml.parseMap(new String(content));
                //Get configuration for NFVImage
                String[] REQUIRED_PACKAGE_KEYS = new String[] { "name", "description", "provider", "image",
                        "shared" };
                for (String requiredKey : REQUIRED_PACKAGE_KEYS) {
                    if (!metadata.containsKey(requiredKey)) {
                        throw new PackageIntegrityException(
                                "Not found " + requiredKey + " of VNFPackage in Metadata.yaml");
                    }
                    if (metadata.get(requiredKey) == null) {
                        throw new PackageIntegrityException(
                                "Not defined " + requiredKey + " of VNFPackage in Metadata.yaml");
                    }
                }
                vnfPackage.setName((String) metadata.get("name"));

                if (vnfPackageMetadataRepository
                        .findByNameAndUsername(vnfPackage.getName(), userManagement.getCurrentUser())
                        .size() != 0) {
                    throw new AlreadyExistingException("Package with name " + vnfPackage.getName()
                            + " already exists, please " + "change the name");
                }

                if (metadata.containsKey("scripts-link")) {
                    vnfPackage.setScriptsLink((String) metadata.get("scripts-link"));
                }
                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 PackageIntegrityException(
                                    "Not found key: " + requiredKey + " of image in Metadata.yaml");
                        }
                        if (imageDetails.get(requiredKey) == null) {
                            throw new PackageIntegrityException(
                                    "Not defined value of key: " + requiredKey + " of image in Metadata.yaml");
                        }
                    }
                    imageMetadata.setUsername(userManagement.getCurrentUser());
                    imageMetadata.setUpload((String) imageDetails.get("option"));
                    if (imageDetails.containsKey("ids")) {
                        imageMetadata.setIds((List<String>) imageDetails.get("ids"));
                    } else {
                        imageMetadata.setIds(new ArrayList<String>());
                    }
                    if (imageDetails.containsKey("names")) {
                        imageMetadata.setNames((List<String>) imageDetails.get("names"));
                    } else {
                        imageMetadata.setNames(new ArrayList<String>());
                    }
                    if (imageDetails.containsKey("link")) {
                        imageMetadata.setLink((String) imageDetails.get("link"));
                    } else {
                        imageMetadata.setLink(null);
                    }

                    //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 PackageIntegrityException("Not found key: " + requiredKey
                                            + " of image-config in Metadata.yaml");
                                }
                                if (imageConfig.get(requiredKey) == null) {
                                    throw new PackageIntegrityException("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 PackageIntegrityException(
                                    "The image-config is not defined. Please define it to upload a new image");
                        }
                    }
                } else {
                    throw new PackageIntegrityException(
                            "The image details are not defined. Please define it to use the right image");
                }
            } else if (!entry.getName().startsWith("scripts/") && entry.getName().endsWith(".json")) {
                //this must be the vnfd
                //and has to be onboarded in the catalogue
                String json = new String(content);
                log.trace("Content of json is: " + json);
                try {
                    virtualNetworkFunctionDescriptor = mapper.fromJson(json,
                            VirtualNetworkFunctionDescriptor.class);
                    //remove the images
                    for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
                        vdu.setVm_image(new HashSet<String>());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                log.trace("Created VNFD: " + virtualNetworkFunctionDescriptor);
            } else if (entry.getName().endsWith(".img")) {
                //this must be the image
                //and has to be upladed to the RIGHT vim
                imageFile = content;
                log.debug("imageFile is: " + entry.getName());
                throw new VimException(
                        "Uploading an image file from the VNFPackage is not supported at this moment. Please use the image link"
                                + ".");
            } else if (entry.getName().startsWith("scripts/")) {
                Script script = new Script();
                script.setName(entry.getName().substring(8));
                script.setPayload(content);
                vnfPackage.getScripts().add(script);
            }
        }
    }
    if (metadata == null) {
        throw new PackageIntegrityException("Not found Metadata.yaml");
    }
    if (vnfPackage.getScriptsLink() != null) {
        if (vnfPackage.getScripts().size() > 0) {
            log.debug(
                    "VNFPackageManagement: Remove scripts got by scripts/ because the scripts-link is defined");
            vnfPackage.setScripts(new HashSet<Script>());
        }
    }
    List<String> vimInstances = new ArrayList<>();
    if (imageDetails.get("upload").equals("check")) {
        if (!imageLink) {
            if (vnfPackage.getImageLink() == null && imageFile == null) {
                throw new PackageIntegrityException(
                        "VNFPackageManagement: For option upload=check you must define an image. 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");
            }
        }
    }

    if (imageDetails.get("upload").equals("true")) {
        log.debug("VNFPackageManagement: Uploading a new Image");
        if (vnfPackage.getImageLink() == null && imageFile == null) {
            throw new PackageIntegrityException(
                    "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 (!imageDetails.containsKey("ids") && !imageDetails.containsKey("names")) {
            throw new PackageIntegrityException(
                    "VNFPackageManagement: Upload option 'false' or 'check' requires at least a list of ids or names to find "
                            + "the right image.");
        }
    }
    vnfPackage.setImage(image);
    myTarFile.close();
    vnfPackage = vnfPackageRepository.save(vnfPackage);
    virtualNetworkFunctionDescriptor.setVnfPackageLocation(vnfPackage.getId());

    VNFPackageMetadata vnfPackageMetadata = new VNFPackageMetadata();
    vnfPackageMetadata.setName(vnfPackage.getName());
    vnfPackageMetadata.setUsername(userManagement.getCurrentUser());
    vnfPackageMetadata.setVnfd(virtualNetworkFunctionDescriptor);
    vnfPackageMetadata.setVnfPackage(vnfPackage);
    vnfPackageMetadata.setNfvImage(image);
    vnfPackageMetadata.setImageMetadata(imageMetadata);
    vnfPackageMetadata.setVnfPackageFileName(fileName);
    vnfPackageMetadata.setVnfPackageFile(pack);
    String description = (String) metadata.get("description");
    if (description.length() > 100) {
        description = description.substring(0, 100);
    }
    vnfPackageMetadata.setDescription(description);
    vnfPackageMetadata.setProvider((String) metadata.get("provider"));
    vnfPackageMetadata.setRequirements((Map) metadata.get("requirements"));
    vnfPackageMetadata.setShared((boolean) metadata.get("shared"));
    vnfPackageMetadata.setMd5sum(DigestUtils.md5DigestAsHex(pack));
    try {
        this.dispatch(vnfPackageMetadata);
    } catch (FailedToUploadException e) {
        vnfPackageRepository.delete(vnfPackage.getId());
        throw e;
    }
    vnfPackageMetadataRepository.save(vnfPackageMetadata);

    //        vnfdRepository.save(virtualNetworkFunctionDescriptor);
    log.debug("Persisted " + vnfPackageMetadata);
    //        log.trace("Onboarded VNFPackage (" + virtualNetworkFunctionDescriptor.getVnfPackageLocation() + ")
    // successfully");

    return vnfPackageMetadata;
}

From source file:org.openbaton.nfvo.core.api.VNFPackageManagement.java

@Override
public VirtualNetworkFunctionDescriptor onboard(byte[] pack, String projectId)
        throws IOException, VimException, NotFoundException, PluginException, IncompatibleVNFPackage,
        AlreadyExistingException, NetworkServiceIntegrityException {
    log.info("Onboarding VNF Package...");
    VNFPackage vnfPackage = new VNFPackage();
    vnfPackage.setScripts(new HashSet<Script>());
    Map<String, Object> metadata = null;
    VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor = null;
    byte[] imageFile = null;
    NFVImage image = new NFVImage();

    InputStream tarStream;//  w  w  w.j a v a 2 s.com
    ArchiveInputStream myTarFile;
    try {
        tarStream = new ByteArrayInputStream(pack);
        myTarFile = new ArchiveStreamFactory().createArchiveInputStream("tar", tarStream);
    } catch (ArchiveException e) {
        e.printStackTrace();
        throw new IOException();
    }
    TarArchiveEntry entry;
    Map<String, Object> imageDetails = new HashMap<>();
    while ((entry = (TarArchiveEntry) myTarFile.getNextEntry()) != null) {
        /* Get the name of the file */
        if (entry.isFile() && !entry.getName().startsWith("./._")) {
            log.debug("file inside tar: " + entry.getName());
            byte[] content = new byte[(int) entry.getSize()];
            myTarFile.read(content, 0, content.length);
            if (entry.getName().equals("Metadata.yaml")) {
                YamlJsonParser yaml = new YamlJsonParser();
                metadata = yaml.parseMap(new String(content));
                imageDetails = handleMetadata(metadata, vnfPackage, imageDetails, image);

            } else if (!entry.getName().startsWith("scripts/") && entry.getName().endsWith(".json")) {
                //this must be the vnfd
                //and has to be onboarded in the catalogue
                String json = new String(content);
                log.trace("Content of json is: " + json);
                try {
                    virtualNetworkFunctionDescriptor = mapper.fromJson(json,
                            VirtualNetworkFunctionDescriptor.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                int i = 1;
                for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
                    if (vdu.getName() == null) {
                        vdu.setName(virtualNetworkFunctionDescriptor.getName() + "-" + i);
                        i++;
                    }
                }
                for (VirtualDeploymentUnit vdu : virtualNetworkFunctionDescriptor.getVdu()) {
                    log.debug("vdu name: " + vdu.getName());
                }
                log.debug("Created VNFD: " + virtualNetworkFunctionDescriptor.getName());
                log.trace("Created VNFD: " + virtualNetworkFunctionDescriptor);
                nsdUtils.fetchVimInstances(virtualNetworkFunctionDescriptor, projectId);
            } else if (entry.getName().endsWith(".img")) {
                //this must be the image
                //and has to be upladed to the RIGHT vim
                imageFile = content;
                log.debug("imageFile is: " + entry.getName());
                throw new VimException(
                        "Uploading an image file from the VNFPackage is not supported at this moment. Please use the image link"
                                + ".");
            } else if (entry.getName().startsWith("scripts/")) {
                Script script = new Script();
                script.setName(entry.getName().substring(8));
                script.setPayload(content);
                vnfPackage.getScripts().add(script);
            }
        }
    }

    handleImage(vnfPackage, imageFile, virtualNetworkFunctionDescriptor, metadata, image, imageDetails,
            projectId);

    vnfPackage.setImage(image);
    myTarFile.close();
    virtualNetworkFunctionDescriptor.setProjectId(projectId);
    vnfPackage.setProjectId(projectId);
    for (VirtualNetworkFunctionDescriptor vnfd : vnfdRepository.findByProjectId(projectId)) {
        if (vnfd.getVendor().equals(virtualNetworkFunctionDescriptor.getVendor())
                && vnfd.getName().equals(virtualNetworkFunctionDescriptor.getName())
                && vnfd.getVersion().equals(virtualNetworkFunctionDescriptor.getVersion())) {
            throw new AlreadyExistingException("A VNF with this vendor, name and version is already existing");
        }
    }

    nsdUtils.checkIntegrity(virtualNetworkFunctionDescriptor);

    vnfPackageRepository.save(vnfPackage);
    virtualNetworkFunctionDescriptor.setVnfPackageLocation(vnfPackage.getId());
    virtualNetworkFunctionDescriptor = vnfdRepository.save(virtualNetworkFunctionDescriptor);
    log.trace("Persisted " + virtualNetworkFunctionDescriptor);
    log.trace("Onboarded VNFPackage (" + virtualNetworkFunctionDescriptor.getVnfPackageLocation()
            + ") successfully");
    return virtualNetworkFunctionDescriptor;
}

From source file:org.openbaton.tosca.parser.CSARParser.java

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<>();
    byte[] imageFile = null;

    YamlJsonParser yaml = new YamlJsonParser();
    metadata = yaml.parseMap(new String(this.vnfMetadata.toByteArray()));
    //Get configuration for NFVImage
    imageDetails = vnfPackageManagement.handleMetadata(metadata, vnfPackage, imageDetails, image);

    vnfPackageManagement.handleImage(vnfPackage, imageFile, virtualNetworkFunctionDescriptor, metadata, image,
            imageDetails, projectId);/*from   w w  w .  ja v a 2 s . c o  m*/

    return image;
}