Search in sources :

Example 71 with DefinitionsChildId

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

the class YamlToscaExportUtil method processDefinitionsElement.

@Override
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.";
        LOGGER.error(error);
        throw new RepositoryCorruptException(error);
    }
    TDefinitions entryDefinitions = repository.getDefinitions(tcId);
    this.getPrepareForExport(repository, tcId, entryDefinitions);
    Collection<DefinitionsChildId> referencedDefinitionsChildIds = repository.getReferencedDefinitionsChildIds(tcId);
    if (!EXPORT_NORMATIVE_TYPES) {
        referencedDefinitionsChildIds.removeIf(id -> id.getQName().getNamespaceURI().startsWith("tosca"));
    }
    // adjust imports: add imports of definitions
    Collection<TImport> imports = new ArrayList<>();
    Map<String, QName> importDefinitions = new HashMap<>();
    for (DefinitionsChildId id : referencedDefinitionsChildIds) {
        this.addToImports(repository, id, imports);
        String defName = YamlExporter.getDefinitionsName(repository, id).concat(Constants.SUFFIX_TOSCA_DEFINITIONS);
        importDefinitions.put(defName, id.getQName());
    }
    entryDefinitions.getImport().addAll(imports);
    entryDefinitions.setImportDefinitions(importDefinitions);
    // END: Definitions modification
    YAMLDefinitionsBasedCsarEntry entry = new YAMLDefinitionsBasedCsarEntry(repository, entryDefinitions);
    // Custom Adjustments for Service Templates
    YamlExportAdjustmentsBuilder adjustmentsBuilder = new YamlExportAdjustmentsBuilder(entry);
    if (!EXPORT_NORMATIVE_TYPES) {
        adjustmentsBuilder.removeNormativeTypeImports();
    }
    entry = adjustmentsBuilder.setMetadataName(tcId).build();
    this.referencesToPathInCSARMap.put(definitionsFileProperties, entry);
    return referencedDefinitionsChildIds;
}
Also used : DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) TImport(org.eclipse.winery.model.tosca.TImport) ArrayList(java.util.ArrayList) YAMLDefinitionsBasedCsarEntry(org.eclipse.winery.repository.yaml.export.entries.YAMLDefinitionsBasedCsarEntry) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 72 with DefinitionsChildId

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

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

the class BackendUtilsTestWithGitBackedRepository method getVersionsOfOneDefinitionWithComponentThatDoesNotHaveAVersion.

@Test
public void getVersionsOfOneDefinitionWithComponentThatDoesNotHaveAVersion() throws Exception {
    this.setRevisionTo("origin/plain");
    DefinitionsChildId id = new RelationshipTypeId("http://plain.winery.opentosca.org/relationshiptypes", "RelationshipTypeWithoutProperties", false);
    List<WineryVersion> versions = WineryVersionUtils.getAllVersionsOfOneDefinition(id, repository);
    assertEquals(1, versions.size());
    assertEquals("", versions.get(0).toString());
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) WineryVersion(org.eclipse.winery.common.version.WineryVersion) Test(org.junit.jupiter.api.Test)

Example 74 with DefinitionsChildId

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

the class YamlCsarImporter method processDefinitionsImport.

@Override
protected Optional<ServiceTemplateId> processDefinitionsImport(TDefinitions defs, TOSCAMetaFile tmf, Path definitionsPath, List<String> errors, CsarImportOptions options) throws IOException {
    Optional<ServiceTemplateId> entryServiceTemplate = Optional.empty();
    String defaultNamespace = defs.getTargetNamespace();
    List<TExtensibleElements> componentInstanceList = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();
    for (final TExtensibleElements ci : componentInstanceList) {
        if (ci instanceof org.eclipse.winery.model.tosca.TServiceTemplate && Objects.isNull(((org.eclipse.winery.model.tosca.TServiceTemplate) ci).getTopologyTemplate())) {
            continue;
        }
        // Determine & ensure that element has the namespace
        String namespace = this.getNamespace(ci, defaultNamespace);
        this.setNamespace(ci, namespace);
        String id = ModelUtilities.getId(ci);
        final DefinitionsChildId wid = determineWineryId(ci, namespace, id);
        if (targetRepository.exists(wid)) {
            if (options.isOverwrite()) {
                targetRepository.forceDelete(wid);
                String msg = String.format("Deleted %1$s %2$s to enable replacement", ci.getClass().getName(), wid.getQName().toString());
                LOGGER.debug(msg);
            } else {
                String msg = String.format("Skipped %1$s %2$s, because it already exists", ci.getClass().getName(), wid.getQName().toString());
                LOGGER.debug(msg);
                // this is not displayed in the UI as we currently do not distinguish between pre-existing types and types created during the import.
                continue;
            }
        }
        // Create a fresh definitions object without the other data.
        final TDefinitions newDefs = BackendUtils.createWrapperDefinitions(wid, targetRepository);
        // add the current TExtensibleElements as the only content to it
        newDefs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(ci);
        // import license and readme files
        importLicenseAndReadme(definitionsPath.getParent().getParent(), wid, tmf, errors);
        importArtifacts(definitionsPath.getParent().getParent(), ci, wid, tmf, errors);
        if (ci instanceof TNodeType) {
            this.adjustNodeType(definitionsPath.getParent().getParent(), (TNodeType) ci, (NodeTypeId) wid, tmf, errors);
        } else if (ci instanceof TRelationshipType) {
            this.adjustRelationshipType(definitionsPath.getParent().getParent(), (TRelationshipType) ci, (RelationshipTypeId) wid, tmf, errors);
        }
        storeDefs(wid, newDefs);
    }
    List<TImport> imports = defs.getImport();
    this.importImports(definitionsPath.getParent(), tmf, imports, errors, options);
    return entryServiceTemplate;
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TImport(org.eclipse.winery.model.tosca.TImport) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)

Example 75 with DefinitionsChildId

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

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