Search in sources :

Example 76 with DefinitionsChildId

use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId 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 77 with DefinitionsChildId

use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.

the class ToscaExportUtil method processDefinitionsElement.

/**
 * Writes the Definitions belonging to the given definition children to the output stream
 *
 * @return a collection of DefinitionsChildIds referenced by the given component
 * @throws RepositoryCorruptException if tcId does not exist
 */
protected Collection<DefinitionsChildId> processDefinitionsElement(IRepository repository, DefinitionsChildId tcId, CsarContentProperties definitionsFileProperties) throws RepositoryCorruptException, IOException {
    if (!repository.exists(tcId)) {
        String error = "Component instance " + tcId.toReadableString() + " does not exist.";
        ToscaExportUtil.LOGGER.error(error);
        throw new RepositoryCorruptException(error);
    }
    this.getPrepareForExport(repository, tcId);
    // this doesn't need to be exhaustive, because this method is only called for a full export.
    // as such all references are traversed externally
    Collection<DefinitionsChildId> references = repository.getReferencedDefinitionsChildIds(tcId);
    TDefinitions entryDefinitions = specifyImports(repository, tcId, references);
    this.referencesToPathInCSARMap.put(definitionsFileProperties, new DefinitionsBasedCsarEntry(entryDefinitions, repository));
    return references;
}
Also used : DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) XMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.XMLDefinitionsBasedCsarEntry) DefinitionsBasedCsarEntry(org.eclipse.winery.repository.export.entries.DefinitionsBasedCsarEntry) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Aggregations

DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)77 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)29 Test (org.junit.jupiter.api.Test)21 QName (javax.xml.namespace.QName)19 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)16 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)16 HashMap (java.util.HashMap)15 IRepository (org.eclipse.winery.repository.backend.IRepository)14 TNodeType (org.eclipse.winery.model.tosca.TNodeType)13 Path (java.nio.file.Path)12 Map (java.util.Map)12 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)12 TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 InputStream (java.io.InputStream)10 HashSet (java.util.HashSet)10 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)10