Search in sources :

Example 1 with YTArtifactDefinition

use of org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition 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 2 with YTArtifactDefinition

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

the class YamlBuilder method buildArtifactDefinition.

@Nullable
public YTArtifactDefinition buildArtifactDefinition(Object object, Parameter<YTArtifactDefinition> parameter) {
    if (Objects.isNull(object)) {
        return null;
    }
    if (object instanceof String) {
        String file = stringValue(object);
        if (Objects.isNull(file)) {
            return null;
        }
        // TODO infer artifact type and mime type from file URI
        String type = file.substring(file.lastIndexOf("."), file.length());
        return new YTArtifactDefinition.Builder(buildQName(type), file).build();
    }
    if (!validate(YTArtifactDefinition.class, object, parameter)) {
        return null;
    }
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) object;
    String file;
    if (map.get(YamlSpecKeywords.FILE) instanceof String) {
        file = stringValue(map.get(YamlSpecKeywords.FILE));
    } else {
        file = null;
        assert false;
    }
    return new YTArtifactDefinition.Builder(buildQName(stringValue(map.get(YamlSpecKeywords.TYPE))), file).setRepository(stringValue(map.get(YamlSpecKeywords.REPOSITORY))).setDescription(buildDescription(map.get(YamlSpecKeywords.DESCRIPTION))).setDeployPath(stringValue(map.get(YamlSpecKeywords.DEPLOY_PATH))).setProperties(buildMap(map.get(YamlSpecKeywords.PROPERTIES), new Parameter<YTPropertyAssignment>().addContext(YamlSpecKeywords.PROPERTIES).setBuilderOO(this::buildPropertyAssignment))).build();
}
Also used : YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) YTMapObject(org.eclipse.winery.model.tosca.yaml.support.YTMapObject) YTListString(org.eclipse.winery.model.tosca.yaml.support.YTListString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) YTPropertyAssignment(org.eclipse.winery.model.tosca.yaml.YTPropertyAssignment) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with YTArtifactDefinition

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

the class FromCanonical method convertArtifactTemplate.

@Deprecated
public YTArtifactDefinition convertArtifactTemplate(TArtifactTemplate node) {
    if (Objects.isNull(node) || Objects.isNull(node.getType())) {
        return null;
    }
    List<String> files = new ArrayList<>();
    List<TArtifactReference> artifactReferences = node.getArtifactReferences();
    if (artifactReferences != null) {
        for (TArtifactReference artifactReference : artifactReferences) {
            files.add(artifactReference.getReference());
        }
    }
    return new YTArtifactDefinition.Builder(getQName(new ArtifactTypeId(node.getType()), node.getType().getNamespaceURI(), node.getType().getLocalPart()), files.size() > 0 ? files.get(0) : null).build();
}
Also used : YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ArrayList(java.util.ArrayList)

Example 4 with YTArtifactDefinition

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

the class YamlRepository method createNewCacheNodeTypeWithArtifact.

/**
 * Creates new cache node type and saves artifact until it's referenced
 *
 * @param ref              repository file reference
 * @param artifactTemplate new artifact template
 * @param artifact         yaml artifact
 * @param imports          imports
 * @return new yaml service template
 */
private YTServiceTemplate createNewCacheNodeTypeWithArtifact(RepositoryFileReference ref, TArtifactTemplate artifactTemplate, YTArtifactDefinition artifact, List<YTMapImportDefinition> imports) {
    YTServiceTemplate serviceTemplate = createEmptyCacheNodeType(((ArtifactTemplateId) ref.getParent()).getQName().getNamespaceURI());
    Map<String, YTArtifactDefinition> artifacts = new LinkedHashMap<>();
    artifacts.put(artifactTemplate.getIdFromIdOrNameField(), artifact);
    serviceTemplate.getNodeTypes().entrySet().iterator().next().getValue().setArtifacts(artifacts);
    serviceTemplate.setImports(imports);
    return serviceTemplate;
}
Also used : YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with YTArtifactDefinition

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

the class YamlRepository method deleteArtifact.

/**
 * Deletes artifacts from there referenced type
 *
 * @param id Artifact Template id
 */
private void deleteArtifact(ArtifactTemplateId id) {
    if (getNameOfTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("Cache")) {
        super.forceDelete(id);
    } else {
        Path targetPath = id2AbsolutePath(id);
        GenericId convertedId = convertGenericId(id);
        if (convertedId != null) {
            if (convertedId instanceof DefinitionsChildId) {
                String convertedFilename = BackendUtils.getFileNameOfDefinitions((DefinitionsChildId) convertedId);
                targetPath = targetPath.resolve(convertedFilename);
            }
        }
        if (Files.exists(targetPath)) {
            try {
                YTServiceTemplate nodeType = readServiceTemplate(targetPath);
                String targetArtifactName = getNameOfArtifactFromArtifactName(id.getQName().getLocalPart());
                if (getTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("nodetypes")) {
                    Map<String, YTArtifactDefinition> artifacts = nodeType.getNodeTypes().entrySet().iterator().next().getValue().getArtifacts();
                    nodeType.getNodeTypes().entrySet().iterator().next().setValue(removeImplementation(nodeType.getNodeTypes().entrySet().iterator().next().getValue(), targetArtifactName));
                    artifacts.remove(targetArtifactName);
                    nodeType.getNodeTypes().entrySet().iterator().next().getValue().setArtifacts(artifacts);
                } else {
                    nodeType.getRelationshipTypes().entrySet().iterator().next().setValue(removeRelationshipArtifact(nodeType.getRelationshipTypes().entrySet().iterator().next().getValue(), targetArtifactName));
                }
                YamlWriter writer = new YamlWriter();
                InputStream output = writer.writeToInputStream(nodeType);
                writeInputStreamToPath(targetPath, output);
            } catch (Exception e) {
                LOGGER.error("Error deleting file: {}", e.getMessage(), e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) GenericId(org.eclipse.winery.model.ids.GenericId) YTArtifactDefinition(org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) InputStream(java.io.InputStream) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) IOException(java.io.IOException) YamlWriter(org.eclipse.winery.repository.converter.writer.YamlWriter)

Aggregations

YTArtifactDefinition (org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition)6 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)3 YTServiceTemplate (org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)3 Path (java.nio.file.Path)2 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 QName (javax.xml.namespace.QName)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)1 GenericId (org.eclipse.winery.model.ids.GenericId)1 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)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