use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.
the class YamlRepository method addImports.
/**
* Adds new import to existing imports
*
* @param oldImports existing imports
* @param newImport new import
* @return edited imports
*/
private List<YTMapImportDefinition> addImports(List<YTMapImportDefinition> oldImports, List<YTMapImportDefinition> newImport) {
if (newImport.isEmpty()) {
return oldImports;
}
if (newImport.get(0).isEmpty()) {
return oldImports;
}
Map.Entry<String, YTImportDefinition> targetImport = newImport.get(0).entrySet().iterator().next();
for (YTMapImportDefinition tMapImportDefinition : oldImports) {
for (Map.Entry<String, YTImportDefinition> tImportDefinitionEntry : tMapImportDefinition.entrySet()) {
if (tImportDefinitionEntry.getKey().equalsIgnoreCase(targetImport.getKey())) {
if (tImportDefinitionEntry.getValue().equals(targetImport.getValue())) {
return oldImports;
}
}
}
}
oldImports.get(0).put(targetImport.getKey(), targetImport.getValue());
return oldImports;
}
use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition 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.support.YTMapImportDefinition in project winery by eclipse.
the class FromCanonical method addNewImports.
private List<YTMapImportDefinition> addNewImports(List<YTMapImportDefinition> imports) {
List<YTMapImportDefinition> newImportsList = convertImports();
if (newImportsList.isEmpty()) {
return imports;
}
if (imports.isEmpty()) {
return newImportsList;
}
YTMapImportDefinition newImports = newImportsList.get(0);
YTMapImportDefinition existingImports = imports.get(0);
for (Map.Entry<String, YTImportDefinition> newImport : newImports.entrySet()) {
boolean found = false;
for (Map.Entry<String, YTImportDefinition> existingImport : existingImports.entrySet()) {
if (newImport.getKey().equalsIgnoreCase(existingImport.getKey()) && newImport.getValue().equals(existingImport.getValue())) {
found = true;
break;
}
}
if (!found) {
existingImports.put(newImport.getKey(), newImport.getValue());
}
}
imports.set(0, newImports);
return imports;
}
use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.
the class SchemaVisitor method visit.
@Override
public Result visit(YTServiceTemplate node, Parameter parameter) {
Map<String, YTDataType> tmpDataTypes = this.data_types;
this.data_types = node.getDataTypes();
for (YTMapImportDefinition map : node.getImports()) {
for (Map.Entry<String, YTImportDefinition> entry : map.entrySet()) {
addImport(entry.getValue().getNamespaceUri(), new AbstractMap.SimpleEntry<>(entry));
}
}
// init localDataTypesList
node.getDataTypes().forEach((key, value) -> this.localDatatypeNames.add(key));
// Default: parameter.datatype is not set -> visit all datatype nodes
QName type = parameter.getDatatype();
if (type == null) {
for (Map.Entry<String, YTDataType> entry : node.getDataTypes().entrySet()) {
if (entry.getValue() != null) {
entry.getValue().accept(this, parameter.copy().addContext("datatypes", entry.getKey()));
}
}
visitChildren(node, parameter);
} else // Optimized: parameter.datatype is set and defined in this service template
if (type.getNamespaceURI().equals(parameter.getNamespace()) && this.localDatatypeNames.contains(type.getLocalPart())) {
node.getDataTypes().get(parameter.getDatatype().getLocalPart()).accept(this, parameter.copy().addContext("datatypes", parameter.getDatatype().getLocalPart()));
}
if (parameter.getBuildSchema()) {
for (Map.Entry<String, SchemaBuilder> entry : schemaBuilders.entrySet()) {
Document document = entry.getValue().build();
WriterUtils.saveType(document, parameter.getPath(), parameter.getNamespace(), entry.getValue().getNamespace());
}
}
this.data_types = tmpDataTypes;
return null;
}
use of org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition in project winery by eclipse.
the class YamlBuilder method buildMapImportDefinition.
@Nullable
public YTMapImportDefinition buildMapImportDefinition(Object object, Parameter<YTMapImportDefinition> parameter) {
YTMapImportDefinition mapImportDefinition = new YTMapImportDefinition();
mapImportDefinition.put(stringValue(parameter.getValue()), buildImportDefinition(object, new Parameter<>(parameter.getContext())));
return mapImportDefinition;
}
Aggregations