Search in sources :

Example 6 with MultiException

use of org.eclipse.winery.yaml.common.exception.MultiException in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(TDataType node, Parameter parameter) {
    SchemaBuilder builder;
    if (this.schemaBuilders.containsKey(parameter.getNamespace())) {
        builder = this.schemaBuilders.get(parameter.getNamespace());
    } else {
        builder = new SchemaBuilder(parameter.getNamespace());
        this.schemaBuilders.put(parameter.getNamespace(), builder);
    }
    Map<String, QName> buildPlan = new LinkedHashMap<>();
    for (Map.Entry<String, TPropertyDefinition> entry : node.getProperties().entrySet()) {
        QName type = entry.getValue().getType();
        buildPlan.put(entry.getKey(), entry.getValue().getType());
        // Add default YAML types
        if (type.getNamespaceURI().equals(Namespaces.YAML_NS)) {
            builder.addElements(entry.getKey(), entry.getValue());
        } else // if parameter.datatype is not defined and property type is defined in this schema -> add
        if (parameter.getDatatype() == null && type.getNamespaceURI().equals(parameter.getNamespace()) && this.localDatatypeNames.contains(type.getLocalPart())) {
            builder.addElements(entry.getKey(), entry.getValue());
        } else // if parameter.datatype is defined and property type is not in this service template
        if (!type.getNamespaceURI().equals(parameter.getNamespace()) || !this.localDatatypeNames.contains(type.getLocalPart())) {
            for (Map.Entry<String, TImportDefinition> importDefinition : imports.get(type.getNamespaceURI())) {
                try {
                    Reader reader = Reader.getReader();
                    TServiceTemplate serviceTemplate = reader.parse(importDefinition.getValue(), parameter.getPath(), importDefinition.getValue().getNamespaceUri());
                    visit(serviceTemplate, new Parameter(parameter.getPath(), type.getNamespaceURI()).setDatatype(type).setBuildSchema(false));
                    // TODO getAbsoluteFilePath
                    builder.addImports(importDefinition.getValue().getNamespaceUri(), getRelativeFileName(importDefinition.getValue().getNamespaceUri()));
                    builder.addElements(entry.getKey(), entry.getValue());
                } catch (MultiException e) {
                    setException(e);
                }
            }
        } else // if parameter.datatype is defined and property type is in this service template but not read
        if (!(this.schemaTypes.containsKey(parameter.getNamespace()) && this.schemaTypes.get(parameter.getNamespace()).contains(type.getLocalPart()))) {
            this.data_types.get(type.getLocalPart()).accept(this, parameter.copy().addContext(type.getLocalPart()).setDatatype(type));
            builder.addElements(entry.getKey(), entry.getValue());
        }
    }
    this.addSchemaTypes(parameter.getNamespace(), parameter.getKey());
    this.propertyDefinition.put(new QName(parameter.getNamespace(), parameter.getKey()), buildPlan);
    if (parameter.getBuildComplexType()) {
        builder.buildComplexType(parameter.getKey(), false);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) SchemaBuilder(org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder) Parameter(org.eclipse.winery.yaml.converter.yaml.visitors.support.Parameter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException)

Example 7 with MultiException

use of org.eclipse.winery.yaml.common.exception.MultiException 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 8 with MultiException

use of org.eclipse.winery.yaml.common.exception.MultiException in project winery by eclipse.

the class Converter method convertX2Y.

public InputStream convertX2Y(InputStream csar) {
    Path filePath = Utils.unzipFile(csar);
    Path fileOutPath = filePath.resolve("tmp");
    try {
        TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
        TOSCAMetaFile metaFile = parser.parse(filePath.resolve("TOSCA-Metadata").resolve("TOSCA.meta"));
        org.eclipse.winery.yaml.common.reader.xml.Reader reader = new org.eclipse.winery.yaml.common.reader.xml.Reader();
        try {
            String fileName = metaFile.getEntryDefinitions();
            Definitions definitions = reader.parse(filePath, Paths.get(fileName));
            this.convertX2Y(definitions, fileOutPath);
        } catch (MultiException e) {
            LOGGER.error("Convert TOSCA XML to TOSCA YAML error", e);
        }
        return Utils.zipPath(fileOutPath);
    } catch (Exception e) {
        LOGGER.error("Error", e);
        throw new AssertionError();
    }
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) IOException(java.io.IOException) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)

Example 9 with MultiException

use of org.eclipse.winery.yaml.common.exception.MultiException 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)

Example 10 with MultiException

use of org.eclipse.winery.yaml.common.exception.MultiException in project winery by eclipse.

the class Y2XConverter method convert.

/**
 * Converts TOSCA YAML TImportDefinitions and returns list of TOSCA XML TImports
 */
private TImport convert(TImportDefinition node, String name) {
    Reader reader = Reader.getReader();
    String namespace = node.getNamespaceUri() == null ? this.namespace : node.getNamespaceUri();
    try {
        org.eclipse.winery.model.tosca.yaml.TServiceTemplate serviceTemplate = reader.readImportDefinition(node, path, namespace);
        Converter converter = new Converter(this.repository);
        Definitions definitions = converter.convertY2X(serviceTemplate, getFileNameFromFile(node.getFile()), namespace, path, outPath);
        WriterUtils.saveDefinitions(definitions, outPath, namespace, name);
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setLocation(WriterUtils.getDefinitionsLocation(namespace, name));
        builder.setNamespace(namespace);
        return builder.build();
    } catch (MultiException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AssignmentBuilder(org.eclipse.winery.yaml.converter.yaml.support.AssignmentBuilder) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) TypeConverter(org.eclipse.winery.yaml.converter.yaml.support.TypeConverter) Converter(org.eclipse.winery.yaml.converter.Converter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) org.eclipse.winery.model.tosca.yaml(org.eclipse.winery.model.tosca.yaml)

Aggregations

MultiException (org.eclipse.winery.yaml.common.exception.MultiException)12 Reader (org.eclipse.winery.yaml.common.reader.yaml.Reader)8 TServiceTemplate (org.eclipse.winery.model.tosca.yaml.TServiceTemplate)6 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 File (java.io.File)3 Path (java.nio.file.Path)3 Objects (java.util.Objects)3 TOSCAMetaFile (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile)3 TOSCAMetaFileParser (org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser)3 Definitions (org.eclipse.winery.model.tosca.Definitions)3 RepositoryFactory (org.eclipse.winery.repository.backend.RepositoryFactory)3 WineryRepositoryException (org.eclipse.winery.repository.exceptions.WineryRepositoryException)3 StandardCharsets (java.nio.charset.StandardCharsets)2 java.nio.file (java.nio.file)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Optional (java.util.Optional)2 QName (javax.xml.namespace.QName)2 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)2