use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId 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<>();
TServiceTemplate serviceTemplate = this.getElement(id);
// add included things to export queue
TBoundaryDefinitions boundaryDefs;
if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
List<TPolicy> policies = boundaryDefs.getPolicies();
if (policies != null) {
for (TPolicy policy : policies) {
PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
ids.add(policyTypeId);
PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
ids.add(policyTemplateId);
}
}
// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
}
final TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
if (topology != null) {
if (Objects.nonNull(topology.getPolicies())) {
topology.getPolicies().stream().filter(Objects::nonNull).forEach(p -> {
QName type = p.getPolicyType();
PolicyTypeId policyTypeIdId = new PolicyTypeId(type);
ids.add(policyTypeIdId);
});
}
for (TEntityTemplate entityTemplate : topology.getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// 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);
QName template = pol.getPolicyRef();
if (template != null) {
PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
ids.add(policyTemplateId);
}
}
}
// Crawl RequirementTypes and Capabilities for their references
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// TODO: this information is collected differently for YAML and XML modes
// crawl through deployment artifacts
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
if (da.getArtifactType() != null) {
// This is considered Nullable, because the test case ConsistencyCheckerTest#hasError
// expects an empty artifactType and thus it may be null.
ids.add(new ArtifactTypeId(da.getArtifactType()));
}
if (da.getArtifactRef() != null) {
ids.add(new ArtifactTemplateId(da.getArtifactRef()));
}
}
}
// Store all referenced artifact types
List<TArtifact> artifacts = n.getArtifacts();
if (Objects.nonNull(artifacts)) {
artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
}
TNodeType nodeType = this.getElement(new NodeTypeId(qname));
if (Objects.nonNull(nodeType.getInterfaceDefinitions())) {
nodeType.getInterfaceDefinitions().stream().filter(Objects::nonNull).forEach(iDef -> {
if (Objects.nonNull(iDef.getType())) {
ids.add(new InterfaceTypeId(iDef.getType()));
}
});
}
} 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 PatternDetectionUtilsTest method swapDetectorWithRefinement.
@Test
public void swapDetectorWithRefinement() {
TTopologyTemplate detector = new TTopologyTemplate();
TTopologyTemplate refinementStructure = new TTopologyTemplate();
TEntityTemplate detectorElement = new TNodeTemplate();
TEntityTemplate refinementElement = new TNodeTemplate();
OTRelationMapping relationMapping = new OTRelationMapping(new OTRelationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDirection(OTRelationDirection.INGOING).setRelationType("relationType").setValidSourceOrTarget(QName.valueOf("validSourceOrTarget")));
OTPermutationMapping permutationMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
OTAttributeMapping attributeMapping = new OTAttributeMapping(new OTAttributeMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setDetectorProperty("detectorProperty").setRefinementProperty("refinementProperty"));
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
OTDeploymentArtifactMapping deploymentArtifactMapping = new OTDeploymentArtifactMapping(new OTDeploymentArtifactMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement).setArtifactType(QName.valueOf("artifactType")));
OTBehaviorPatternMapping behaviorPatternMapping = new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("behaviorPattern").setProperty(new OTPropertyKV("key", "value")));
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinementStructure).setRelationMappings(Collections.singletonList(relationMapping)).setPermutationMappings(Collections.singletonList(permutationMapping)).setAttributeMappings(Collections.singletonList(attributeMapping)).setStayMappings(Collections.singletonList(stayMapping)).setDeploymentArtifactMappings(Collections.singletonList(deploymentArtifactMapping)).setBehaviorPatternMappings(Collections.singletonList(behaviorPatternMapping)));
PatternDetectionUtils.swapDetectorWithRefinement(prm);
assertEquals(prm.getDetector(), refinementStructure);
assertEquals(prm.getRefinementStructure(), detector);
assertEquals(relationMapping.getDetectorElement(), refinementElement);
assertEquals(relationMapping.getRefinementElement(), detectorElement);
assertEquals(relationMapping.getDirection(), OTRelationDirection.INGOING);
assertEquals(relationMapping.getRelationType(), QName.valueOf("relationType"));
assertEquals(relationMapping.getValidSourceOrTarget(), QName.valueOf("validSourceOrTarget"));
assertEquals(permutationMapping.getDetectorElement(), refinementElement);
assertEquals(permutationMapping.getRefinementElement(), detectorElement);
assertEquals(attributeMapping.getDetectorElement(), refinementElement);
assertEquals(attributeMapping.getRefinementElement(), detectorElement);
assertEquals(attributeMapping.getDetectorProperty(), "refinementProperty");
assertEquals(attributeMapping.getRefinementProperty(), "detectorProperty");
assertEquals(stayMapping.getDetectorElement(), refinementElement);
assertEquals(stayMapping.getRefinementElement(), detectorElement);
assertEquals(deploymentArtifactMapping.getDetectorElement(), refinementElement);
assertEquals(deploymentArtifactMapping.getRefinementElement(), detectorElement);
assertEquals(deploymentArtifactMapping.getArtifactType(), QName.valueOf("artifactType"));
assertEquals(behaviorPatternMapping.getDetectorElement(), refinementElement);
assertEquals(behaviorPatternMapping.getRefinementElement(), detectorElement);
assertEquals(behaviorPatternMapping.getBehaviorPattern(), "behaviorPattern");
assertEquals(behaviorPatternMapping.getProperty(), new OTPropertyKV("key", "value"));
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class ToscaPropertyMatcher method propertiesCompatible.
public boolean propertiesCompatible(ToscaEntity left, ToscaEntity right) {
// By convention, the left node is always the element to search in right.
TEntityTemplate detectorElement = left.getTemplate();
TEntityTemplate candidate = right.getTemplate();
// TODO the implementation (currently) works for KV properties only
Map<String, String> detectorProperties = ModelUtilities.getPropertiesKV(detectorElement);
Map<String, String> candidateProperties = ModelUtilities.getPropertiesKV(candidate);
if (detectorProperties == null || detectorProperties.isEmpty()) {
// detector and candidate are always compatible if detector specifies no properties
return true;
}
if (candidateProperties == null || candidateProperties.isEmpty()) {
// detector and candidate cannot be compatible if detector requires properties, but candidate has none
return false;
}
return detectorProperties.entrySet().stream().allMatch(entry -> {
String val = entry.getValue();
if (StringUtils.isEmpty(val)) {
// always match if detector value is empty
return true;
}
// Assumption: properties are simple KV Properties
String refProp = candidateProperties.get(entry.getKey());
if (StringUtils.isEmpty(refProp)) {
// cannot match if candidate value is empty but detector value is not
return false;
}
if (val.equalsIgnoreCase("*")) {
// if the detector defines a wildcard, the property must be set in the candidate
return StringUtils.isNotEmpty(refProp);
} else {
// if the detector defines a specific value, the candidate's property must match
if (matchPropertiesByRegex) {
return Pattern.matches(val, refProp);
} else {
return val.equalsIgnoreCase(refProp);
}
}
});
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class ToscaBehaviorPatternMatcher method propertiesCompatible.
@Override
public boolean propertiesCompatible(ToscaEntity left, ToscaEntity right) {
// By convention, the left node is always the element to search in right.
TEntityTemplate detectorElement = left.getTemplate();
TEntityTemplate candidateElement = right.getTemplate();
return propertiesCompatible(detectorElement, candidateElement);
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class ToscaComponentPatternMatcher method componentPatternsCompatible.
protected boolean componentPatternsCompatible(ToscaEntity left, ToscaEntity right) {
// By convention, the left node is always the element to search in right.
TEntityTemplate detectorElement = left.getTemplate();
TEntityTemplate candidateElement = right.getTemplate();
if (!(detectorElement instanceof TNodeTemplate) || !(candidateElement instanceof TNodeTemplate)) {
return false;
}
if (prm.getPermutationMappings() == null || prm.getPermutationMappings().isEmpty()) {
return false;
}
List<OTPermutationMapping> detectorMappings = prm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, prm) && mapping.getDetectorElement().getId().equals(detectorElement.getId())).collect(Collectors.toList());
if (detectorMappings.isEmpty()) {
return false;
}
TEntityTemplate refinementElement = detectorMappings.get(0).getRefinementElement();
return refinementModels.stream().map(OTPatternRefinementModel.class::cast).filter(otherPrm -> otherPrm.getPermutationMappings() != null && !otherPrm.getPermutationMappings().isEmpty()).anyMatch(otherPrm -> {
List<OTPermutationMapping> matchingDetectorMappings = otherPrm.getPermutationMappings().stream().filter(mapping -> isOneToOne(mapping, otherPrm) && super.isCompatible(mapping.getDetectorElement(), candidateElement) && mapping.getRefinementElement().getType().equals(refinementElement.getType())).collect(Collectors.toList());
if (!matchingDetectorMappings.isEmpty()) {
componentPatternCandidates.get(prm).add(new ComponentPatternCandidate((TNodeTemplate) detectorElement, (TNodeTemplate) refinementElement, otherPrm, (TNodeTemplate) matchingDetectorMappings.get(0).getDetectorElement()));
return true;
}
return false;
});
}
Aggregations