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