use of org.eclipse.winery.yaml.common.writer.yaml.Writer 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<>();
}
Aggregations