Search in sources :

Example 1 with TOSCAMetaFileParser

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser in project winery by eclipse.

the class CsarImporter method importFromDir.

/**
 * Import an extracted CSAR from a directory
 *
 * @param path            the root path of an extracted CSAR file
 * @param overwrite       if true: contents of the repo are overwritten
 * @param asyncWPDParsing true if WPD should be parsed asynchronously to speed up the import. Required, because
 *                        JUnit terminates the used ExecutorService
 */
public ImportMetaInformation importFromDir(final Path path, final boolean overwrite, final boolean asyncWPDParsing) throws IOException {
    final ImportMetaInformation importMetaInformation = new ImportMetaInformation();
    Path toscaMetaPath = path.resolve("TOSCA-Metadata/TOSCA.meta");
    if (!Files.exists(toscaMetaPath)) {
        importMetaInformation.errors.add("TOSCA.meta does not exist");
        return importMetaInformation;
    }
    final TOSCAMetaFileParser tmfp = new TOSCAMetaFileParser();
    final TOSCAMetaFile tmf = tmfp.parse(toscaMetaPath);
    if (tmf.getEntryDefinitions() != null) {
        // we obey the entry definitions and "just" import that
        // imported definitions are added recursively
        Path defsPath = path.resolve(tmf.getEntryDefinitions());
        importMetaInformation.entryServiceTemplate = this.importDefinitions(tmf, defsPath, importMetaInformation.errors, overwrite, asyncWPDParsing);
        this.importSelfServiceMetaData(tmf, path, defsPath, importMetaInformation.errors);
    } else {
        // no explicit entry definitions found
        // we import all available definitions
        // The specification says (cos01, Section 16.1, line 2935) that all definitions are contained in the "Definitions" directory
        // The alternative is to go through all entries in the TOSCA Meta File, but there is no guarantee that this list is complete
        Path definitionsDir = path.resolve("Definitions");
        if (!Files.exists(definitionsDir)) {
            importMetaInformation.errors.add("No entry definitions defined and Definitions directory does not exist.");
            return importMetaInformation;
        }
        final List<IOException> exceptions = new ArrayList<>();
        Files.walkFileTree(definitionsDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                if (dir.endsWith("Definitions")) {
                    return FileVisitResult.CONTINUE;
                } else {
                    return FileVisitResult.SKIP_SUBTREE;
                }
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                try {
                    CsarImporter.this.importDefinitions(tmf, file, importMetaInformation.errors, overwrite, asyncWPDParsing);
                } catch (IOException e) {
                    exceptions.add(e);
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }
        });
        if (!exceptions.isEmpty()) {
            // we rethrow the exception
            throw exceptions.get(0);
        }
    }
    this.importNamespacePrefixes(path);
    return importMetaInformation;
}
Also used : IOException(java.io.IOException) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with TOSCAMetaFileParser

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser in project winery by eclipse.

the class AccountabilityManagerImpl method fillFilesOfModel.

private void fillFilesOfModel(ModelProvenanceElement model) {
    // 1. parse element.state as ManifestContent
    RecoveringManifestParser genericParser = new RecoveringManifestParser();
    ManifestContents manifestContents = genericParser.parse(model.getFingerprint());
    // 2. parse the ManifestContent as a TOSCAMetaFile
    TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
    TOSCAMetaFile toscaMetaFile = parser.parse(manifestContents, genericParser.getProblems().size());
    // 3. retrieve files from meta file
    Objects.requireNonNull(toscaMetaFile);
    List<FileProvenanceElement> result = toscaMetaFile.getFileBlocks().stream().map(fileSection -> {
        FileProvenanceElement fileElement = new FileProvenanceElement(model);
        fileElement.setFileHash(fileSection.get(TOSCAMetaFileAttributes.HASH));
        fileElement.setAddressInImmutableStorage(fileSection.get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
        fileElement.setFileName(fileSection.get(TOSCAMetaFileAttributes.NAME));
        return fileElement;
    }).sorted(Comparator.comparing(FileProvenanceElement::getFileName)).collect(Collectors.toList());
    model.setFiles(result);
}
Also used : ManifestContents(org.eclipse.virgo.util.parser.manifest.ManifestContents) RecoveringManifestParser(org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) FileProvenanceElement(org.eclipse.winery.accountability.model.FileProvenanceElement)

Example 3 with TOSCAMetaFileParser

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser in project winery by eclipse.

the class Converter method convertX2Y.

public InputStream convertX2Y(InputStream csar) {
    Path filePath = Utils.unzipFile(csar);
    Path fileOutPath = filePath.resolve("tmp");
    try {
        TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
        TOSCAMetaFile metaFile = parser.parse(filePath.resolve("TOSCA-Metadata").resolve("TOSCA.meta"));
        org.eclipse.winery.yaml.common.reader.xml.Reader reader = new org.eclipse.winery.yaml.common.reader.xml.Reader();
        try {
            String fileName = metaFile.getEntryDefinitions();
            Definitions definitions = reader.parse(filePath, Paths.get(fileName));
            this.convertX2Y(definitions, fileOutPath);
        } catch (MultiException e) {
            LOGGER.error("Convert TOSCA XML to TOSCA YAML error", e);
        }
        return Utils.zipPath(fileOutPath);
    } catch (Exception e) {
        LOGGER.error("Error", e);
        throw new AssertionError();
    }
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) IOException(java.io.IOException) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)

Aggregations

TOSCAMetaFile (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)3 TOSCAMetaFileParser (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser)3 IOException (java.io.IOException)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ManifestContents (org.eclipse.virgo.util.parser.manifest.ManifestContents)1 RecoveringManifestParser (org.eclipse.virgo.util.parser.manifest.RecoveringManifestParser)1 FileProvenanceElement (org.eclipse.winery.accountability.model.FileProvenanceElement)1 Definitions (org.eclipse.winery.model.tosca.Definitions)1 WineryRepositoryException (org.eclipse.winery.repository.exceptions.WineryRepositoryException)1 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)1 Reader (org.eclipse.winery.yaml.common.reader.yaml.Reader)1