Search in sources :

Example 11 with DirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.

the class NodeTemplateResource method postYamlArtifactFile.

@Path("yamlartifacts/{artifactId}")
public GenericFileResource postYamlArtifactFile(@PathParam("artifactId") String id) {
    DirectoryId serviceTemplateYamlArtifactsDir = new GenericDirectoryId(getServiceTemplateResource().getId(), IdNames.FILES_DIRECTORY);
    DirectoryId nodeTemplateYamlArtifactsDir = new GenericDirectoryId(serviceTemplateYamlArtifactsDir, nodeTemplate.getId());
    DirectoryId yamlArtifactFilesDirectoryId = new GenericDirectoryId(nodeTemplateYamlArtifactsDir, id);
    return new GenericFileResource(yamlArtifactFilesDirectoryId);
}
Also used : GenericFileResource(org.eclipse.winery.repository.rest.resources._support.GenericFileResource) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) Path(javax.ws.rs.Path)

Example 12 with DirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.

the class YamlExporter method writeCsar.

/**
 * Writes a complete CSAR containing all necessary things reachable from the given service template
 *
 * @param entryId the id of the service template to export
 * @param out     the output stream to write to
 * @return the TOSCA meta file for the generated Csar
 */
@Override
public String writeCsar(DefinitionsChildId entryId, OutputStream out, Map<String, Object> exportConfiguration) throws IOException, RepositoryCorruptException {
    LOGGER.trace("Starting CSAR export with {}", entryId.toString());
    Map<CsarContentProperties, CsarEntry> refMap = new HashMap<>();
    YamlToscaExportUtil exporter = new YamlToscaExportUtil();
    ExportedState exportedState = new ExportedState();
    DefinitionsChildId currentId = entryId;
    Collection<DefinitionsChildId> referencedIds;
    // Process definitions and referenced files
    do {
        String definitionsPathInsideCSAR = getDefinitionsPathInsideCSAR(repository, currentId);
        CsarContentProperties definitionsFileProperties = new CsarContentProperties(definitionsPathInsideCSAR);
        if (!YamlRepository.ROOT_TYPE_QNAME.equals(currentId.getQName())) {
            referencedIds = exporter.processTOSCA(repository, currentId, definitionsFileProperties, refMap, exportConfiguration);
            // for each entryId add license and readme files (if they exist) to the refMap
            addLicenseAndReadmeFiles(currentId, refMap);
            exportedState.flagAsExported(currentId);
            exportedState.flagAsExportRequired(referencedIds);
        }
        currentId = exportedState.pop();
    } while (currentId != null);
    // Archive creation
    try (final ZipOutputStream zos = new ZipOutputStream(out)) {
        // write all referenced files
        for (Map.Entry<CsarContentProperties, CsarEntry> entry : refMap.entrySet()) {
            CsarContentProperties fileProperties = entry.getKey();
            CsarEntry ref = entry.getValue();
            LOGGER.trace("Creating {}", fileProperties.getPathInsideCsar());
            if (ref instanceof RepositoryRefBasedCsarEntry && ((RepositoryRefBasedCsarEntry) ref).getReference().getParent() instanceof DirectoryId) {
                addArtifactTemplateToZipFile(zos, (RepositoryRefBasedCsarEntry) ref, fileProperties);
            } else {
                addCsarEntryToArchive(zos, ref, fileProperties);
            }
        }
        // create manifest file and add it to archive
        return this.addManifest(repository, entryId, refMap, zos, exportConfiguration);
    }
}
Also used : RemoteRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RemoteRefBasedCsarEntry) DefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry) DocumentBasedCsarEntry(org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry) YAMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.yaml.export.entries.YAMLDefinitionsBasedCsarEntry) CsarEntry(org.eclipse.winery.repository.export.entries.CsarEntry) XMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry) RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) HashMap(java.util.HashMap) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) CsarContentProperties(org.eclipse.winery.repository.export.CsarContentProperties) RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ZipOutputStream(java.util.zip.ZipOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) ExportedState(org.eclipse.winery.repository.export.ExportedState)

Example 13 with DirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.

the class CsarImporter method adjustArtifactTemplate.

/**
 * Adjusts the given artifact template to conform with the repository format
 * <p>
 * We import the files given at the artifact references
 */
private void adjustArtifactTemplate(Path rootPath, TOSCAMetaFile tmf, ArtifactTemplateId atid, TArtifactTemplate ci, final List<String> errors) {
    List<TArtifactReference> refs = ci.getArtifactReferences();
    if (refs == null) {
        // no references stored - break
        return;
    }
    for (TArtifactReference ref : refs) {
        String reference = ref.getReference();
        // URLs are stored encoded -> undo the encoding
        reference = EncodingUtil.URLdecode(reference);
        URI refURI;
        try {
            refURI = new URI(reference);
        } catch (URISyntaxException e) {
            errors.add(String.format("Invalid URI %1$s", ref));
            continue;
        }
        if (refURI.isAbsolute()) {
            // We have to do nothing
            continue;
        }
        Path path = rootPath.resolve(reference);
        if (!Files.exists(path)) {
            errors.add(String.format("Reference %1$s not found", reference));
            return;
        }
        Set<Path> allFiles;
        if (Files.isRegularFile(path)) {
            allFiles = new HashSet<>();
            allFiles.add(path);
        } else {
            if (!Files.isDirectory(path)) {
                LOGGER.error("path {} is not a directory", path);
            }
            Path localRoot = rootPath.resolve(path);
            List<TArtifactReference.IncludeOrExclude> includeOrExclude = ref.getIncludeOrExclude();
            if (includeOrExclude.get(0) instanceof Exclude) {
                // Implicit semantics of an exclude listed first:
                // include all files and then exclude the files matched by the pattern
                allFiles = this.getAllFiles(localRoot);
            } else {
                // semantics if include listed as first:
                // same as listed at other places
                allFiles = new HashSet<>();
            }
            for (Object object : includeOrExclude) {
                if (object instanceof Include) {
                    this.handleInclude((Include) object, localRoot, allFiles);
                } else {
                    assert (object instanceof Exclude);
                    this.handleExclude((Exclude) object, localRoot, allFiles);
                }
            }
        }
        DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(atid);
        this.importAllFiles(rootPath, allFiles, fileDir, tmf, errors);
    }
    if (refs.isEmpty()) {
        // everything is imported and is a file stored locally
        // we don't need the references stored locally: they are generated on the fly when exporting
        ci.setArtifactReferences(null);
    }
}
Also used : Path(java.nio.file.Path) Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) Include(org.eclipse.winery.model.tosca.TArtifactReference.Include) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)

Example 14 with DirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId 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)

Example 15 with DirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId in project winery by eclipse.

the class CsarExporter method writeCsar.

/**
 * Writes a complete CSAR containing all necessary things reachable from the given service template
 *
 * @param entryId the id of the service template to export
 * @param out     the output stream to write to
 * @return the TOSCA meta file for the generated Csar
 */
public String writeCsar(DefinitionsChildId entryId, OutputStream out, Map<String, Object> exportConfiguration) throws IOException, RepositoryCorruptException, InterruptedException, AccountabilityException, ExecutionException {
    CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());
    Map<CsarContentProperties, CsarEntry> refMap = new HashMap<>();
    ToscaExportUtil exporter = new ToscaExportUtil();
    ExportedState exportedState = new ExportedState();
    DefinitionsChildId currentId = entryId;
    Collection<DefinitionsChildId> referencedIds;
    if (entryId.isSelfContained()) {
        exportConfiguration.put(CsarExportConfiguration.INCLUDE_HASHES.name(), true);
    }
    // Process definitions and referenced files
    do {
        String definitionsPathInsideCSAR = CsarExporter.getDefinitionsPathInsideCSAR(repository, currentId);
        CsarContentProperties definitionsFileProperties = new CsarContentProperties(definitionsPathInsideCSAR);
        referencedIds = exporter.processTOSCA(repository, currentId, definitionsFileProperties, refMap, exportConfiguration);
        // for each entryId add license and readme files (if they exist) to the refMap
        addLicenseAndReadmeFiles(currentId, refMap);
        exportedState.flagAsExported(currentId);
        exportedState.flagAsExportRequired(referencedIds);
        currentId = exportedState.pop();
    } while (currentId != null);
    // if we export a ServiceTemplate, data for the self-service portal might exist
    if (entryId instanceof ServiceTemplateId) {
        ServiceTemplateId serviceTemplateId = (ServiceTemplateId) entryId;
        this.addSelfServiceMetaData(serviceTemplateId, refMap);
        this.addSelfServiceFiles(serviceTemplateId, refMap);
    }
    this.addNamespacePrefixes(refMap);
    // Calculate hashes for referenced files if necessary
    if (exportConfiguration.containsKey(CsarExportConfiguration.INCLUDE_HASHES.name())) {
        LOGGER.trace("Calculating checksum for {} files.", refMap.size());
        calculateFileHashes(refMap);
    }
    // Store referenced files in immutable file storage if necessary
    if (exportConfiguration.containsKey(CsarExportConfiguration.STORE_IMMUTABLY.name())) {
        try {
            LOGGER.trace("Storing {} files in the immutable file storage", refMap.size());
            immutablyStoreRefFiles(refMap);
        } catch (InterruptedException | ExecutionException | AccountabilityException e) {
            LOGGER.error("Failed to store files in immutable storage. Reason: {}", e.getMessage());
            throw e;
        }
    }
    // Archive creation
    try (final ZipOutputStream zos = new ZipOutputStream(out)) {
        // write all referenced files
        for (Map.Entry<CsarContentProperties, CsarEntry> entry : refMap.entrySet()) {
            CsarContentProperties fileProperties = entry.getKey();
            CsarEntry ref = entry.getValue();
            CsarExporter.LOGGER.trace("Creating {}", fileProperties.getPathInsideCsar());
            if (ref instanceof RepositoryRefBasedCsarEntry && ((RepositoryRefBasedCsarEntry) ref).getReference().getParent() instanceof DirectoryId) {
                addArtifactTemplateToZipFile(zos, (RepositoryRefBasedCsarEntry) ref, fileProperties);
            } else {
                addCsarEntryToArchive(zos, ref, fileProperties);
            }
        }
        // create manifest file and add it to archive
        return this.addManifest(entryId, refMap, zos, exportConfiguration);
    }
}
Also used : DocumentBasedCsarEntry(org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry) CsarEntry(org.eclipse.winery.repository.export.entries.CsarEntry) XMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry) RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) DefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry) HashMap(java.util.HashMap) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) AccountabilityException(org.eclipse.winery.accountability.exceptions.AccountabilityException) ServiceTemplateSelfServiceFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ZipOutputStream(java.util.zip.ZipOutputStream) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)16 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)7 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)4 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)4 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)4 GenericDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId)4 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 Map (java.util.Map)3 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)3 TArtifactReference (org.eclipse.winery.model.tosca.TArtifactReference)3 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)2 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)2 BackendUtils (org.eclipse.winery.repository.backend.BackendUtils)2