Search in sources :

Example 1 with YTServiceTemplate

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

the class YamlRepository method convertToServiceTemplate.

/**
 * Reads xml definition input stream converts it to yaml service template and writes it to input stream
 *
 * @param ref         Repository File Reference
 * @param inputStream Input Stream
 * @return yaml service template input stream
 */
@Deprecated
private InputStream convertToServiceTemplate(RepositoryFileReference ref, InputStream inputStream, MediaType mediaType) {
    if (!mediaType.equals(MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS)) {
        // do not modify content that's not a TOSCA_DEFINITIONS file
        return inputStream;
    }
    // ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        // IOUtils.copy(inputStream, outputStream);
        // Definitions definitions = readInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
        TDefinitions definitions = (TDefinitions) JAXBSupport.createUnmarshaller().unmarshal(inputStream);
        YTServiceTemplate serviceTemplate = convertToYamlModel(ref, definitions);
        YamlWriter writer = new YamlWriter();
        return writer.writeToInputStream(serviceTemplate);
    } catch (Exception e) {
        LOGGER.error("Error converting service template. Reason: {}", e.getMessage(), e);
    }
    return null;
}
Also used : YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) 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)

Example 2 with YTServiceTemplate

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

the class YamlRepository method putDefinition.

@Override
public void putDefinition(RepositoryFileReference ref, TDefinitions content) {
    try {
        YTServiceTemplate yaml = convertToYamlModel(ref, content);
        YamlWriter writer = new YamlWriter();
        writer.write(yaml, ref2AbsolutePath(ref));
    } catch (Exception e) {
        LOGGER.error("Error converting service template. Reason: {}", e.getMessage(), e);
    }
}
Also used : 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)

Example 3 with YTServiceTemplate

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

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

the class ImportVisitor method visit.

@Override
public Result visit(YTServiceTemplate node, Parameter parameter) {
    YamlReader reader = new YamlReader();
    if (!this.namespace.equals(Namespaces.TOSCA_YAML_NS)) {
        Set<String> typeDefinitions = new HashSet<>(Arrays.asList(Defaults.TOSCA_NORMATIVE_TYPES, Defaults.TOSCA_NONNORMATIVE_TYPES));
        String tmpNamespace = this.namespace;
        this.namespace = Namespaces.TOSCA_YAML_NS;
        Path tmpDir = Utils.getTmpDir(Paths.get("types"));
        for (String typeDefinition : typeDefinitions) {
            try {
                Path outFilePath = tmpDir.resolve(typeDefinition);
                InputStream inputStream = this.getClass().getResourceAsStream(// Do not use File.separator here (https://stackoverflow.com/a/41677152/8235252)
                "/".concat(typeDefinition));
                Files.copy(inputStream, outFilePath, StandardCopyOption.REPLACE_EXISTING);
                YTServiceTemplate serviceTemplate = reader.parseSkipTest(outFilePath, Namespaces.TOSCA_YAML_NS);
                if (Objects.nonNull(serviceTemplate)) {
                    serviceTemplate.accept(this, new Parameter());
                }
            } catch (MultiException e) {
                setException(e);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.namespace = tmpNamespace;
    }
    super.visit(node, parameter);
    return null;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) Parameter(org.eclipse.winery.repository.converter.validator.support.Parameter) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) HashSet(java.util.HashSet)

Example 5 with YTServiceTemplate

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

the class YamlReaderTest method testPropertyFunctionReading.

@Test
public void testPropertyFunctionReading() throws Exception {
    YamlReader reader = new YamlReader();
    InputStream is = getClass().getClassLoader().getResourceAsStream("yaml/property_functions.yml");
    YTServiceTemplate template = reader.parse(is);
    Assertions.assertNotNull(template);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) AbstractConverterTest(org.eclipse.winery.repository.converter.AbstractConverterTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

YTServiceTemplate (org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)19 YamlReader (org.eclipse.winery.repository.converter.reader.YamlReader)10 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)9 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 AbstractConverterTest (org.eclipse.winery.repository.converter.AbstractConverterTest)5 Test (org.junit.jupiter.api.Test)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Path (java.nio.file.Path)4 LinkedHashMap (java.util.LinkedHashMap)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Map (java.util.Map)3 JAXBException (javax.xml.bind.JAXBException)3 YTArtifactDefinition (org.eclipse.winery.model.tosca.yaml.YTArtifactDefinition)3 YTTopologyTemplateDefinition (org.eclipse.winery.model.tosca.yaml.YTTopologyTemplateDefinition)3 YTMapImportDefinition (org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition)3 YamlWriter (org.eclipse.winery.repository.converter.writer.YamlWriter)3 AbstractMap (java.util.AbstractMap)2 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)2