Search in sources :

Example 6 with RepositoryRefBasedCsarEntry

use of org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry 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 7 with RepositoryRefBasedCsarEntry

use of org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry 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)

Example 8 with RepositoryRefBasedCsarEntry

use of org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry in project winery by eclipse.

the class CsarExporter method addLicenseAndReadmeFiles.

protected void addLicenseAndReadmeFiles(DefinitionsChildId entryId, Map<CsarContentProperties, CsarEntry> refMap) {
    final RepositoryFileReference licenseRef = new RepositoryFileReference(entryId, Constants.LICENSE_FILE_NAME);
    if (repository.exists(licenseRef)) {
        refMap.put(new CsarContentProperties(BackendUtils.getPathInsideRepo(licenseRef)), new RepositoryRefBasedCsarEntry(repository, licenseRef));
    }
    final RepositoryFileReference readmeRef = new RepositoryFileReference(entryId, Constants.README_FILE_NAME);
    if (repository.exists(readmeRef)) {
        refMap.put(new CsarContentProperties(BackendUtils.getPathInsideRepo(readmeRef)), new RepositoryRefBasedCsarEntry(repository, readmeRef));
    }
}
Also used : RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 9 with RepositoryRefBasedCsarEntry

use of org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry in project winery by eclipse.

the class CsarExporter method putRefIntoRefMap.

private void putRefIntoRefMap(String targetDir, Map<CsarContentProperties, CsarEntry> refMap, GenericId id, String fileName) {
    RepositoryFileReference ref = new RepositoryFileReference(id, fileName);
    if (repository.exists(ref)) {
        CsarContentProperties csarContentProperties = new CsarContentProperties(targetDir + fileName);
        refMap.put(csarContentProperties, new RepositoryRefBasedCsarEntry(repository, ref));
    } else {
        CsarExporter.LOGGER.error("Data corrupt: pointing to non-existent file " + ref);
    }
}
Also used : RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 10 with RepositoryRefBasedCsarEntry

use of org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry in project winery by eclipse.

the class CsarExporter method addNamespacePrefixes.

/**
 * Writes the configured mapping namespace prefix -> namespace to the archive
 * <p>
 * This is kind of a quick hack. TODO: during the import, the prefixes should be extracted using JAXB and stored in
 * the NamespacesResource
 */
private void addNamespacePrefixes(Map<CsarContentProperties, CsarEntry> refMap) {
    // ensure that the namespaces are saved as json
    SortedSet<RepositoryFileReference> references = repository.getContainedFiles(new NamespacesId());
    references.forEach(repositoryFileReference -> {
        if (repositoryFileReference.getFileName().toLowerCase().endsWith(Constants.SUFFIX_JSON)) {
            CsarContentProperties csarContentProperties = new CsarContentProperties(CsarExporter.PATH_TO_NAMESPACES_JSON);
            refMap.put(csarContentProperties, new RepositoryRefBasedCsarEntry(repository, repositoryFileReference));
        }
    });
}
Also used : RepositoryRefBasedCsarEntry(org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) NamespacesId(org.eclipse.winery.model.ids.admin.NamespacesId)

Aggregations

RepositoryRefBasedCsarEntry (org.eclipse.winery.repository.export.entries.RepositoryRefBasedCsarEntry)10 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)4 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)4 CsarEntry (org.eclipse.winery.repository.export.entries.CsarEntry)4 DefinitionsBasedCsarEntry (org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry)4 DocumentBasedCsarEntry (org.eclipse.winery.repository.export.entries.DocumentBasedCsarEntry)4 XMLDefinitionsBasedCsarEntry (org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry)4 IOException (java.io.IOException)3 ServiceTemplateSelfServiceFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Collection (java.util.Collection)2 ZipEntry (java.util.zip.ZipEntry)2 IOUtils (org.apache.commons.io.IOUtils)2 Constants (org.eclipse.winery.common.Constants)2 Environments (org.eclipse.winery.common.configuration.Environments)2