use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IGenericRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ComplianceRuleId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
TComplianceRule complianceRule = this.getElement(id);
// TODO extend to required Structure
if (complianceRule.getIdentifier() != null) {
for (TEntityTemplate entityTemplate : complianceRule.getIdentifier().getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through deployment artifacts
TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
for (TDeploymentArtifact da : das) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
// crawl through reqs/caps
TNodeTemplate.Requirements requirements = n.getRequirements();
if (requirements != null) {
for (TRequirement req : requirements.getRequirement()) {
QName type = req.getType();
RequirementTypeId rtId = new RequirementTypeId(type);
ids.add(rtId);
}
}
TNodeTemplate.Capabilities capabilities = n.getCapabilities();
if (capabilities != null) {
for (TCapability cap : capabilities.getCapability()) {
QName type = cap.getType();
CapabilityTypeId ctId = new CapabilityTypeId(type);
ids.add(ctId);
}
}
// crawl through policies
org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies.getPolicy()) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class TopologyTemplateResource method computeDi.
@POST
@Path("kvcomparison")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public PropertyDiffList computeDi(UpdateInfo updateInfo) {
List<String> resolvedProperties = new ArrayList<>();
List<String> removedProperties = new ArrayList<>();
List<String> newProperties = new ArrayList<>();
Optional<TEntityTemplate> foundTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate().stream().filter(template -> template.getId().equals(updateInfo.getNodeTemplateId())).filter(template -> template.getProperties() != null).findFirst();
if (foundTemplate.isPresent() && Objects.nonNull(foundTemplate.get().getProperties()) && Objects.nonNull(ModelUtilities.getPropertiesKV(foundTemplate.get()))) {
HashMap<String, String> oldKvs = ModelUtilities.getPropertiesKV(foundTemplate.get());
QName qNameType = QName.valueOf(updateInfo.getNewComponentType());
IRepository repository = RepositoryFactory.getRepository();
TEntityType newNodeTypeVersion = repository.getElement(new NodeTypeId(qNameType));
if (Objects.nonNull(newNodeTypeVersion) && Objects.nonNull(newNodeTypeVersion.getWinerysPropertiesDefinition())) {
List<PropertyDefinitionKV> newKvs = newNodeTypeVersion.getWinerysPropertiesDefinition().getPropertyDefinitions();
resolvedProperties = newKvs.stream().map(PropertyDefinitionKV::getKey).filter(keys -> oldKvs.keySet().stream().map(String::toLowerCase).collect(Collectors.toList()).contains(keys.toLowerCase())).collect(Collectors.toList());
removedProperties = oldKvs.keySet().stream().filter(keys -> !newKvs.stream().map(newProp -> newProp.getKey().toLowerCase()).collect(Collectors.toList()).contains(keys.toLowerCase())).collect(Collectors.toList());
newProperties = newKvs.stream().map(PropertyDefinitionKV::getKey).filter(keys -> !oldKvs.keySet().stream().map(String::toLowerCase).collect(Collectors.toList()).contains(keys.toLowerCase())).collect(Collectors.toList());
}
}
return new PropertyDiffList(resolvedProperties, removedProperties, newProperties);
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class BackendUtils method mergeTopologyTemplateAinTopologyTemplateB.
/**
* Merges two Topology Templates and returns the mapping between the topology elements from the original Topology
* Template and their respective clones inside the merged topology. Hereby, the staying elements must not be
* merged.
*
* @param topologyTemplateA the topology to merged into <code>topologyTemplateB</code>
* @param topologyTemplateB the target topology in which <dode>topologyTemplateA</dode> should be merged in
* @param stayingElementIds the TEntityTemplates that must not be merged from A to B.
* @return A mapping between the ids in the <code>topologyTemplateA</code> and their corresponding ids in
* <code>topologyTemplateB</code>
*/
public static Map<String, String> mergeTopologyTemplateAinTopologyTemplateB(TTopologyTemplate topologyTemplateA, TTopologyTemplate topologyTemplateB, List<String> stayingElementIds) {
Objects.requireNonNull(topologyTemplateA);
Objects.requireNonNull(topologyTemplateB);
TTopologyTemplate topologyTemplateToBeMerged = new TTopologyTemplate.Builder().build();
Map<String, String> idMapping = new HashMap<>();
Optional<Integer> shiftLeft = topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).max(Comparator.comparingInt(n -> ModelUtilities.getLeft(n).orElse(0))).map(n -> ModelUtilities.getLeft(n).orElse(0));
if (Objects.nonNull(stayingElementIds)) {
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().forEach(entity -> {
if (!stayingElementIds.contains(entity.getId())) {
if (entity instanceof TNodeTemplate) {
topologyTemplateToBeMerged.addNodeTemplate((TNodeTemplate) entity);
} else if (entity instanceof TRelationshipTemplate) {
topologyTemplateToBeMerged.addRelationshipTemplate((TRelationshipTemplate) entity);
}
}
});
} else {
topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateA.getNodeTemplateOrRelationshipTemplate());
}
if (shiftLeft.isPresent()) {
ModelUtilities.collectIdsOfExistingTopologyElements(topologyTemplateB, idMapping);
// patch ids of reqs change them if required
topologyTemplateToBeMerged.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().forEach(oldReq -> {
TRequirement req = SerializationUtils.clone(oldReq);
ModelUtilities.generateNewIdOfTemplate(req, idMapping);
topologyTemplateToBeMerged.getRelationshipTemplates().stream().filter(rt -> rt.getSourceElement().getRef() instanceof TRequirement).forEach(rt -> {
TRequirement sourceElement = (TRequirement) rt.getSourceElement().getRef();
if (sourceElement.getId().equalsIgnoreCase(oldReq.getId())) {
sourceElement.setId(req.getId());
}
});
}));
// patch ids of caps change them if required
topologyTemplateToBeMerged.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().forEach(oldCap -> {
TCapability cap = SerializationUtils.clone(oldCap);
ModelUtilities.generateNewIdOfTemplate(cap, idMapping);
topologyTemplateToBeMerged.getRelationshipTemplates().stream().filter(rt -> rt.getTargetElement().getRef() instanceof TCapability).forEach(rt -> {
TCapability targetElement = (TCapability) rt.getTargetElement().getRef();
if (targetElement.getId().equalsIgnoreCase(oldCap.getId())) {
targetElement.setId(cap.getId());
}
});
}));
ArrayList<TRelationshipTemplate> newRelationships = new ArrayList<>();
// patch the ids of templates and add them
topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate().forEach(element -> {
TEntityTemplate rtOrNt = SerializationUtils.clone(element);
ModelUtilities.generateNewIdOfTemplate(rtOrNt, idMapping);
if (rtOrNt instanceof TNodeTemplate) {
int newLeft = ModelUtilities.getLeft((TNodeTemplate) rtOrNt).orElse(0) + shiftLeft.get();
((TNodeTemplate) rtOrNt).setX(Integer.toString(newLeft));
} else if (rtOrNt instanceof TRelationshipTemplate) {
newRelationships.add((TRelationshipTemplate) rtOrNt);
}
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(rtOrNt);
});
// update references to the new elements
newRelationships.forEach(rel -> {
RelationshipSourceOrTarget source = rel.getSourceElement().getRef();
RelationshipSourceOrTarget target = rel.getTargetElement().getRef();
if (source instanceof TNodeTemplate && (stayingElementIds == null || stayingElementIds.stream().noneMatch(elementId -> elementId.equals(source.getId())))) {
TNodeTemplate newSource = topologyTemplateB.getNodeTemplate(idMapping.get(source.getId()));
rel.setSourceNodeTemplate(newSource);
}
if (target instanceof TNodeTemplate && (stayingElementIds == null || stayingElementIds.stream().noneMatch(elementId -> elementId.equals(target.getId())))) {
TNodeTemplate newTarget = topologyTemplateB.getNodeTemplate(idMapping.get(target.getId()));
rel.setTargetNodeTemplate(newTarget);
}
});
} else {
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate());
}
return idMapping;
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class ConsistencyChecker method checkPropertiesValidation.
private void checkPropertiesValidation(DefinitionsChildId id) {
if (!(id instanceof EntityTemplateId)) {
return;
}
TEntityTemplate entityTemplate;
try {
// TEntityTemplate is abstract. IRepository does not offer getElement for abstract ids
// Therefore, we have to use the detour through getDefinitions
entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
} catch (ClassCastException e) {
LOGGER.error("Something wrong in the consistency between Ids and the TOSCA data model. See http://eclipse.github.io/winery/dev/id-system.html for more information on the ID system.");
printAndAddError(id, "Critical error at analysis: " + e.getMessage());
return;
}
if (Objects.isNull(entityTemplate.getType())) {
// no printing necessary; type consistency is checked at other places
return;
}
TEntityType entityType;
try {
entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during getting type for template {}", entityTemplate.getId(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
}
TEntityTemplate.Properties definedProps = entityTemplate.getProperties();
if (requiresProperties(entityType) && definedProps == null) {
printAndAddError(id, "Properties required, but no properties defined");
return;
} else if (!requiresProperties(entityType) && definedProps != null) {
printAndAddError(id, "No properties required by type, but properties were defined on template");
return;
} else if (definedProps == null) {
// no properties required and none defined
return;
}
if (definedProps instanceof TEntityTemplate.XmlProperties) {
// check defined properties any against the xml schema
@Nullable final Object any = ((TEntityTemplate.XmlProperties) definedProps).getAny();
if (any == null) {
printAndAddError(id, "Properties required, but no XmlProperties were empty (any case)");
return;
}
TEntityType.PropertiesDefinition def = entityType.getProperties();
if (def == null) {
printAndAddError(id, "XmlProperties were given, but no XmlPropertiesDefinition was specified");
return;
}
if (def instanceof TEntityType.XmlElementDefinition) {
final QName element = ((TEntityType.XmlElementDefinition) def).getElement();
final Map<String, RepositoryFileReference> mapFromLocalNameToXSD = configuration.getRepository().getXsdImportManager().getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
final RepositoryFileReference repositoryFileReference = mapFromLocalNameToXSD.get(element.getLocalPart());
if (repositoryFileReference == null) {
printAndAddError(id, "No Xml Schema definition found for " + element);
return;
}
validate(repositoryFileReference, any, id);
}
} else if (definedProps instanceof TEntityTemplate.WineryKVProperties) {
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
Map<String, String> kvProperties = ((TEntityTemplate.WineryKVProperties) definedProps).getKVProperties();
if (kvProperties.isEmpty()) {
printAndAddError(id, "Properties required, but no properties set (kvproperties case)");
return;
}
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitions()) {
String key = propertyDefinitionKV.getKey();
if (kvProperties.get(key) == null) {
printAndAddError(id, "Property " + key + " required, but not set.");
} else {
// removeNamespaceProperties the key from the map to enable checking below whether a property is defined which not requried by the property definition
kvProperties.remove(key);
}
}
// If any key is left, this is a key not defined at the schema
for (Object o : kvProperties.keySet()) {
printAndAddError(id, "Property " + o + " set, but not defined at schema.");
}
} else if (definedProps instanceof TEntityTemplate.YamlProperties) {
// FIXME todo
LOGGER.debug("YAML Properties checking is not yet implemented!");
}
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ComplianceRuleId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
OTComplianceRule complianceRule = this.getElement(id);
// TODO extend to required Structure
if (complianceRule.getIdentifier() != null) {
for (TEntityTemplate entityTemplate : complianceRule.getIdentifier().getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through deployment artifacts
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// crawl through policies
List<TPolicy> policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
Aggregations