Search in sources :

Example 6 with TServiceTemplate

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

the class Datatypes method testDataTypesRecursive.

@Test
public void testDataTypesRecursive() throws Exception {
    String name = "data_types-recursive";
    String namespace = "http://www.example.com/DataTypesRecursive";
    TServiceTemplate serviceTemplate = readServiceTemplate(name, namespace);
    Definitions definitions = convert(serviceTemplate, name, namespace);
    writeXml(definitions, name, namespace);
    Assert.assertNotNull(definitions);
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) Test(org.junit.Test)

Example 7 with TServiceTemplate

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

the class Datatypes method testDataTypesWithImport.

@Test
public void testDataTypesWithImport() throws Exception {
    String name = "data_types-with_import";
    String namespace = "http://www.example.com/DataTypesWithImportTest";
    TServiceTemplate serviceTemplate = readServiceTemplate(name, namespace);
    Definitions definitions = convert(serviceTemplate, name, namespace);
    writeXml(definitions, name, namespace);
    Assert.assertNotNull(definitions);
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) Test(org.junit.Test)

Example 8 with TServiceTemplate

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

the class ImportVisitor method visit.

@Override
public Result visit(TImportDefinition node, Parameter parameter) {
    Reader reader = Reader.getReader();
    String importNamespace = node.getNamespaceUri() == null ? this.namespace : node.getNamespaceUri();
    try {
        TServiceTemplate serviceTemplate = reader.parse(node, path, importNamespace);
        if (serviceTemplate != null) {
            String tmpNamespace = this.namespace;
            this.namespace = importNamespace;
            this.visit(serviceTemplate, new Parameter());
            this.namespace = tmpNamespace;
        }
        super.visit(node, parameter);
    } catch (MultiException e) {
        this.setException(e);
    }
    return null;
}
Also used : Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) Parameter(org.eclipse.winery.yaml.common.validator.support.Parameter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate)

Example 9 with TServiceTemplate

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

the class YamlWriterTest method roundTripSingleTest.

@DisplayName("Test read and write single round trip")
@ParameterizedTest(name = "{index} name=''{0}''")
@ValueSource(strings = { "example_16-topology_templates-1_1" })
public void roundTripSingleTest(String file) throws Exception {
    Path fileName = getYamlFile(file);
    TServiceTemplate serviceTemplate = this.getYamlServiceTemplate(fileName);
    writeYamlServiceTemplate(serviceTemplate, temporaryFolder.resolve(fileName));
    TServiceTemplate out = this.getYamlServiceTemplate(fileName, temporaryFolder);
    Assertions.assertEquals(serviceTemplate, out);
}
Also used : Path(java.nio.file.Path) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) ValueSource(org.junit.jupiter.params.provider.ValueSource) DisplayName(org.junit.jupiter.api.DisplayName) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with TServiceTemplate

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

the class X2YConverter method convert.

/**
 * Converts TOSCA XML Definitions to TOSCA YAML ServiceTemplates
 */
@NonNull
public Map<Path, TServiceTemplate> convert(Definitions node, Path outPath, QName name) throws MultiException {
    if (Objects.isNull(node))
        return new LinkedHashMap<>();
    QName tmpName = name;
    LOGGER.debug("Convert TServiceTemplate: {}", node.getIdFromIdOrNameField());
    TServiceTemplate.Builder builder = new TServiceTemplate.Builder(Defaults.TOSCA_DEFINITIONS_VERSION).setDescription(convertDocumentation(node.getDocumentation())).setArtifactTypes(convert(node.getArtifactTypes())).setCapabilityTypes(convert(node.getCapabilityTypes())).setRelationshipTypes(convert(node.getRelationshipTypes())).setNodeTypes(convert(node.getNodeTypes())).setPolicyTypes(convert(node.getPolicyTypes()));
    if (node.getServiceTemplates().size() == 1) {
        builder.setTopologyTemplate(convert(node.getServiceTemplates().get(0)));
        if (Objects.nonNull(node.getServiceTemplates().get(0).getName())) {
            tmpName = new QName(node.getServiceTemplates().get(0).getName());
        }
        builder.addMetadata("targetNamespace", node.getTargetNamespace());
    }
    MultiException exception = importDefinitions.entrySet().stream().map((entry) -> {
        Path path = this.path.resolve(entry.getKey().getGroup()).resolve(entry.getKey().getNamespace().getEncoded());
        try {
            Map<Path, TServiceTemplate> map = new X2YConverter(repository, this.path).convert(entry.getValue(), path, entry.getKey().getQName());
            Optional.ofNullable(map).orElse(new LinkedHashMap<>()).forEach((Path key, TServiceTemplate value) -> builder.addImports(key.toFile().getName(), new TImportDefinition.Builder(this.path.relativize(key).toString()).setNamespaceUri(entry.getValue().getTargetNamespace()).setNamespacePrefix(getNamespacePrefix(entry.getValue().getTargetNamespace())).build()));
        } catch (MultiException e) {
            return e;
        }
        return null;
    }).filter(Objects::nonNull).reduce(new MultiException(), (a, b) -> a.add(b.getException()));
    if (exception.hasException()) {
        throw exception;
    }
    TServiceTemplate serviceTemplate = builder.build();
    Path filePath = outPath.resolve(tmpName.getLocalPart().concat(".yml"));
    try {
        Files.createDirectories(filePath.getParent());
        Writer writer = new Writer();
        writer.write(serviceTemplate, filePath);
        return Collections.singletonMap(filePath, serviceTemplate);
    } catch (IOException e) {
        LOGGER.error("Could not create file path {}", filePath.getParent());
    }
    return new LinkedHashMap<>();
}
Also used : Path(java.nio.file.Path) QName(javax.xml.namespace.QName) IOException(java.io.IOException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) Writer(org.eclipse.winery.yaml.common.writer.yaml.Writer) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) NonNull(org.eclipse.jdt.annotation.NonNull)

Aggregations

TServiceTemplate (org.eclipse.winery.model.tosca.yaml.TServiceTemplate)11 Definitions (org.eclipse.winery.model.tosca.Definitions)5 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)5 Path (java.nio.file.Path)4 Test (org.junit.Test)4 Reader (org.eclipse.winery.yaml.common.reader.yaml.Reader)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Parameter (org.eclipse.winery.yaml.common.validator.support.Parameter)2 DisplayName (org.junit.jupiter.api.DisplayName)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 File (java.io.File)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.nio.file (java.nio.file)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 QName (javax.xml.namespace.QName)1