Search in sources :

Example 1 with TOSCAMetaFile

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile 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 TOSCAMetaFile

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile 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 TOSCAMetaFile

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile 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)

Example 4 with TOSCAMetaFile

use of org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile 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 options the set of options applicable while importing a CSAR
 * @param fileMap Contains all files which were extracted from the CSAR and have to be validated using the
 *                accountability layer
 */
private ImportMetaInformation importFromDir(final Path path, CsarImportOptions options, Map<String, File> fileMap) throws IOException, AccountabilityException, ExecutionException, InterruptedException {
    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 TOSCAMetaFile tmf = parseTOSCAMetaFile(toscaMetaPath);
    parseCsarContents(path, tmf, importMetaInformation, options, fileMap);
    if (importMetaInformation.errors.isEmpty()) {
        importNamespacePrefixes(path);
    }
    return importMetaInformation;
}
Also used : Path(java.nio.file.Path) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)

Example 5 with TOSCAMetaFile

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

the class YamlCsarImporter method importArtifacts.

private void importArtifacts(Path rootPath, TExtensibleElements ci, DefinitionsChildId wid, TOSCAMetaFile tmf, final List<String> errors) {
    if (ci instanceof TServiceTemplate) {
        TServiceTemplate st = (TServiceTemplate) ci;
        if (st.getTopologyTemplate() != null) {
            st.getTopologyTemplate().getNodeTemplates().forEach(node -> {
                if (Objects.nonNull(node.getArtifacts()) && !node.getArtifacts().isEmpty()) {
                    node.getArtifacts().stream().map(this::fixForwardSlash).filter(a -> this.isImportable(rootPath, a)).forEach(a -> {
                        DirectoryId stFilesDir = new GenericDirectoryId(wid, IdNames.FILES_DIRECTORY);
                        DirectoryId ntFilesDir = new GenericDirectoryId(stFilesDir, node.getId());
                        DirectoryId artifactDir = new GenericDirectoryId(ntFilesDir, a.getName());
                        importArtifact(rootPath, a, artifactDir, tmf, errors);
                        fixArtifactRefName(rootPath, a);
                    });
                }
            });
        }
    } else if (ci instanceof TNodeType) {
        TNodeType nt = (TNodeType) ci;
        fixOperationImplFileRef(nt);
        if (Objects.nonNull(nt.getArtifacts()) && !nt.getArtifacts().isEmpty()) {
            nt.getArtifacts().stream().map(this::fixForwardSlash).filter(a -> this.isImportable(rootPath, a)).forEach(a -> {
                DirectoryId typeFilesDir = new GenericDirectoryId(wid, IdNames.FILES_DIRECTORY);
                DirectoryId artifactDir = new GenericDirectoryId(typeFilesDir, a.getName());
                importArtifact(rootPath, a, artifactDir, tmf, errors);
                fixArtifactRefName(rootPath, a);
            });
        }
    }
}
Also used : TArtifact(org.eclipse.winery.model.tosca.TArtifact) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) LoggerFactory(org.slf4j.LoggerFactory) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet) IdNames(org.eclipse.winery.model.ids.IdNames) YamlRepository(org.eclipse.winery.repository.yaml.YamlRepository) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Map(java.util.Map) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) Util(org.eclipse.winery.repository.common.Util) Path(java.nio.file.Path) TImport(org.eclipse.winery.model.tosca.TImport) Filename(org.eclipse.winery.repository.backend.constants.Filename) VisualAppearanceId(org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId) YamlTOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.YamlTOSCAMetaFileParser) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Logger(org.slf4j.Logger) Files(java.nio.file.Files) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) FromCanonical(org.eclipse.winery.repository.yaml.converter.FromCanonical) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) Objects(java.util.Objects) YamlWriter(org.eclipse.winery.repository.converter.writer.YamlWriter) List(java.util.List) ToCanonical(org.eclipse.winery.repository.yaml.converter.ToCanonical) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) GitBasedRepository(org.eclipse.winery.repository.backend.filebased.GitBasedRepository) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) Namespaces(org.eclipse.winery.model.converter.support.Namespaces) IRepository(org.eclipse.winery.repository.backend.IRepository) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) Optional(java.util.Optional) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Aggregations

TOSCAMetaFile (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)5 IOException (java.io.IOException)3 TOSCAMetaFileParser (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser)3 Path (java.nio.file.Path)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Files (java.nio.file.Files)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)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 Namespaces (org.eclipse.winery.model.converter.support.Namespaces)1