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;
}
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;
}
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();
}
}
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<>();
}
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;
}
Aggregations