Search in sources :

Example 1 with YTMapImportDefinition

use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.

the class YamlRepository method addImports.

/**
 * Adds new import to existing imports
 *
 * @param oldImports existing imports
 * @param newImport  new import
 * @return edited imports
 */
private List<YTMapImportDefinition> addImports(List<YTMapImportDefinition> oldImports, List<YTMapImportDefinition> newImport) {
    if (newImport.isEmpty()) {
        return oldImports;
    }
    if (newImport.get(0).isEmpty()) {
        return oldImports;
    }
    Map.Entry<String, YTImportDefinition> targetImport = newImport.get(0).entrySet().iterator().next();
    for (YTMapImportDefinition tMapImportDefinition : oldImports) {
        for (Map.Entry<String, YTImportDefinition> tImportDefinitionEntry : tMapImportDefinition.entrySet()) {
            if (tImportDefinitionEntry.getKey().equalsIgnoreCase(targetImport.getKey())) {
                if (tImportDefinitionEntry.getValue().equals(targetImport.getValue())) {
                    return oldImports;
                }
            }
        }
    }
    oldImports.get(0).put(targetImport.getKey(), targetImport.getValue());
    return oldImports;
}
Also used : YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) YTImportDefinition(org.eclipse.winery.model.tosca.yaml.YTImportDefinition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with YTMapImportDefinition

use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.

the class YamlRepository method convertToYamlModel.

private YTServiceTemplate convertToYamlModel(RepositoryFileReference existing, TDefinitions definitions) throws IOException, MultiException {
    FromCanonical converter = new FromCanonical(this);
    YTServiceTemplate serviceTemplate;
    if (existing.getParent() instanceof NodeTypeImplementationId) {
        serviceTemplate = readServiceTemplate(existing);
        serviceTemplate = converter.convertNodeTypeImplementation(serviceTemplate, definitions.getNodeTypeImplementations().get(0));
    } else if (existing.getParent() instanceof RelationshipTypeImplementationId) {
        serviceTemplate = readServiceTemplate(existing);
        serviceTemplate = converter.convertRelationshipTypeImplementation(serviceTemplate, definitions.getRelationshipTypeImplementations().get(0));
    } else if (existing.getParent() instanceof NodeTypeId) {
        serviceTemplate = converter.convert(definitions);
        if (exists(existing)) {
            YTServiceTemplate oldServiceTemplate = readServiceTemplate(existing);
            serviceTemplate = replaceOldWithNewData(serviceTemplate, oldServiceTemplate);
        }
    } else if (existing.getParent() instanceof RelationshipTypeId) {
        serviceTemplate = converter.convert(definitions);
        if (exists(existing)) {
            YTServiceTemplate oldServiceTemplate = readServiceTemplate(existing);
            serviceTemplate = replaceOldRelationshipTypeWithNewData(serviceTemplate, oldServiceTemplate);
        }
    } else if (existing.getParent() instanceof ArtifactTemplateId) {
        ArtifactTemplateId id = (ArtifactTemplateId) existing.getParent();
        TArtifactTemplate artifactTemplate = definitions.getArtifactTemplates().get(0);
        YTArtifactDefinition artifact = converter.convertArtifactTemplate(artifactTemplate);
        List<YTMapImportDefinition> imports = converter.convertImports();
        Path targetPath = ref2AbsolutePath(existing);
        if (Files.exists(targetPath)) {
            serviceTemplate = readServiceTemplate(targetPath);
            if (serviceTemplate == null) {
                serviceTemplate = createNewCacheNodeTypeWithArtifact(existing, artifactTemplate, artifact, imports);
            } else if (getTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("nodetypes")) {
                YTNodeType nodeType = serviceTemplate.getNodeTypes().entrySet().iterator().next().getValue();
                Map<String, YTArtifactDefinition> artifacts = nodeType.getArtifacts();
                if (artifacts.containsKey(artifactTemplate.getIdFromIdOrNameField())) {
                    artifacts.replace(artifactTemplate.getIdFromIdOrNameField(), artifact);
                } else {
                    artifacts.put(artifactTemplate.getIdFromIdOrNameField(), artifact);
                }
            } else if (existing.getParent() instanceof PolicyTypeId || existing.getParent() instanceof CapabilityTypeId) {
                // we simply take the new definition as is
                serviceTemplate = converter.convert(definitions);
            } else {
                serviceTemplate = converter.convert(definitions);
                if (exists(existing)) {
                    YTServiceTemplate existingServiceTemplate = readServiceTemplate(existing);
                    serviceTemplate = replaceTopologyTemplate(serviceTemplate, existingServiceTemplate);
                }
            }
        } else {
            serviceTemplate = createNewCacheNodeTypeWithArtifact(existing, artifactTemplate, artifact, imports);
        }
    } else {
        serviceTemplate = converter.convert(definitions);
    }
    return serviceTemplate;
}
Also used : Path(java.nio.file.Path) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) FromCanonical(org.eclipse.winery.repository.yaml.converter.FromCanonical) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) YTNodeType(org.eclipse.winery.model.tosca.yaml.YTNodeType)

Example 3 with YTMapImportDefinition

use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.

the class FromCanonical method addNewImports.

private List<YTMapImportDefinition> addNewImports(List<YTMapImportDefinition> imports) {
    List<YTMapImportDefinition> newImportsList = convertImports();
    if (newImportsList.isEmpty()) {
        return imports;
    }
    if (imports.isEmpty()) {
        return newImportsList;
    }
    YTMapImportDefinition newImports = newImportsList.get(0);
    YTMapImportDefinition existingImports = imports.get(0);
    for (Map.Entry<String, YTImportDefinition> newImport : newImports.entrySet()) {
        boolean found = false;
        for (Map.Entry<String, YTImportDefinition> existingImport : existingImports.entrySet()) {
            if (newImport.getKey().equalsIgnoreCase(existingImport.getKey()) && newImport.getValue().equals(existingImport.getValue())) {
                found = true;
                break;
            }
        }
        if (!found) {
            existingImports.put(newImport.getKey(), newImport.getValue());
        }
    }
    imports.set(0, newImports);
    return imports;
}
Also used : YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) YTImportDefinition(org.eclipse.winery.model.tosca.yaml.YTImportDefinition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashBiMap(org.eclipse.collections.impl.bimap.mutable.HashBiMap) HashMap(java.util.HashMap)

Example 4 with YTMapImportDefinition

use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(YTServiceTemplate node, Parameter parameter) {
    Map<String, YTDataType> tmpDataTypes = this.data_types;
    this.data_types = node.getDataTypes();
    for (YTMapImportDefinition map : node.getImports()) {
        for (Map.Entry<String, YTImportDefinition> entry : map.entrySet()) {
            addImport(entry.getValue().getNamespaceUri(), new AbstractMap.SimpleEntry<>(entry));
        }
    }
    // init localDataTypesList
    node.getDataTypes().forEach((key, value) -> this.localDatatypeNames.add(key));
    // Default: parameter.datatype is not set -> visit all datatype nodes
    QName type = parameter.getDatatype();
    if (type == null) {
        for (Map.Entry<String, YTDataType> entry : node.getDataTypes().entrySet()) {
            if (entry.getValue() != null) {
                entry.getValue().accept(this, parameter.copy().addContext("datatypes", entry.getKey()));
            }
        }
        visitChildren(node, parameter);
    } else // Optimized: parameter.datatype is set and defined in this service template
    if (type.getNamespaceURI().equals(parameter.getNamespace()) && this.localDatatypeNames.contains(type.getLocalPart())) {
        node.getDataTypes().get(parameter.getDatatype().getLocalPart()).accept(this, parameter.copy().addContext("datatypes", parameter.getDatatype().getLocalPart()));
    }
    if (parameter.getBuildSchema()) {
        for (Map.Entry<String, SchemaBuilder> entry : schemaBuilders.entrySet()) {
            Document document = entry.getValue().build();
            WriterUtils.saveType(document, parameter.getPath(), parameter.getNamespace(), entry.getValue().getNamespace());
        }
    }
    this.data_types = tmpDataTypes;
    return null;
}
Also used : YTDataType(org.eclipse.winery.model.tosca.yaml.YTDataType) QName(javax.xml.namespace.QName) YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) Document(org.w3c.dom.Document) AbstractMap(java.util.AbstractMap) YTImportDefinition(org.eclipse.winery.model.tosca.yaml.YTImportDefinition) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 5 with YTMapImportDefinition

use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.

the class YamlBuilder method buildMapImportDefinition.

@Nullable
public YTMapImportDefinition buildMapImportDefinition(Object object, Parameter<YTMapImportDefinition> parameter) {
    YTMapImportDefinition mapImportDefinition = new YTMapImportDefinition();
    mapImportDefinition.put(stringValue(parameter.getValue()), buildImportDefinition(object, new Parameter<>(parameter.getContext())));
    return mapImportDefinition;
}
Also used : YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

YTMapImportDefinition (org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition)9 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 YTImportDefinition (org.eclipse.winery.model.tosca.yaml.YTImportDefinition)6 YTServiceTemplate (org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)3 AbstractMap (java.util.AbstractMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashBiMap (org.eclipse.collections.impl.bimap.mutable.HashBiMap)2 Path (java.nio.file.Path)1 QName (javax.xml.namespace.QName)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)1 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)1 CapabilityTypeId (org.eclipse.winery.model.ids.definitions.CapabilityTypeId)1 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)1 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)1 NodeTypeImplementationId (org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId)1 PolicyTypeId (org.eclipse.winery.model.ids.definitions.PolicyTypeId)1