use of org.eclipse.winery.model.tosca.HasPolicies in project winery by eclipse.
the class ComponentPatternDetectionTest method refineTopologyWithBehaviorPatterns.
@Test
public void refineTopologyWithBehaviorPatterns() {
TPolicy behaviorPattern1 = new TPolicy(new TPolicy.Builder(QName.valueOf("behaviorPattern1")).setName("behaviorPattern1"));
TPolicy behaviorPattern2 = new TPolicy(new TPolicy.Builder(QName.valueOf("behaviorPattern2")).setName("behaviorPattern2"));
List<TPolicy> topologyPolicies = new ArrayList<>();
topologyPolicies.add(behaviorPattern1);
((HasPolicies) topology.getNodeTemplateOrRelationshipTemplate("database")).setPolicies(topologyPolicies);
List<TPolicy> detectorPolicies = new ArrayList<>();
detectorPolicies.add(behaviorPattern1);
((HasPolicies) prm2.getDetector().getNodeTemplateOrRelationshipTemplate("database-rs2")).setPolicies(detectorPolicies);
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(behaviorPattern1);
refinementPolicies.add(behaviorPattern2);
((HasPolicies) prm2.getRefinementStructure().getNodeTemplateOrRelationshipTemplate("relationalDb-d2")).setPolicies(refinementPolicies);
ComponentPatternDetection detection = new ComponentPatternDetection(new DefaultRefinementChooser(), Arrays.asList(prm2, prm1));
detection.refineTopology(topology);
// behaviorPattern2 must have been removed as it cannot be guaranteed to be implemented
HasPolicies db = ((HasPolicies) topology.getNodeTemplateOrRelationshipTemplate("relationalDb-d2"));
assertEquals(db.getPolicies().size(), 1);
assertTrue(db.getPolicies().contains(behaviorPattern1));
}
use of org.eclipse.winery.model.tosca.HasPolicies in project winery by eclipse.
the class BehaviorPatternDetection method addCompatibleBehaviorPatterns.
private void addCompatibleBehaviorPatterns(TEntityTemplate refinementElement, RefinementCandidate refinement) {
OTPatternRefinementModel prm = (OTPatternRefinementModel) refinement.getRefinementModel();
TEntityTemplate detectorElement = prm.getStayMappings().stream().filter(stayMapping -> stayMapping.getRefinementElement().getId().equals(refinementElement.getId())).findFirst().get().getDetectorElement();
ToscaEntity detectorEntity = refinement.getDetectorGraph().getVertexOrEdge(detectorElement.getId()).get();
TEntityTemplate stayingElement = getEntityCorrespondence(detectorEntity, refinement.getGraphMapping());
List<TPolicy> refinementPolicies = ((HasPolicies) refinementElement).getPolicies();
List<TPolicy> stayingPolicies = ((HasPolicies) stayingElement).getPolicies();
if (refinementPolicies != null) {
if (stayingPolicies != null) {
// avoid duplicates
refinementPolicies.forEach(refinementPolicy -> {
boolean policyExists = stayingPolicies.stream().anyMatch(stayingPolicy -> stayingPolicy.getPolicyType().equals(refinementPolicy.getPolicyType()));
if (!policyExists) {
stayingPolicies.add(refinementPolicy);
}
});
} else {
((HasPolicies) stayingElement).setPolicies(new ArrayList<>(refinementPolicies));
}
removeIncompatibleBehaviorPatterns(refinementElement, stayingElement, refinement);
}
}
use of org.eclipse.winery.model.tosca.HasPolicies in project winery by eclipse.
the class ToscaPrmPropertyMatcher method characterizingPatternsCompatible.
public boolean characterizingPatternsCompatible(TEntityTemplate detectorEntityElement, TEntityTemplate candidateEntityElement) {
// if the detector has no patterns attached but the candidate has --> it's a match
boolean characterizingPatternsCompatible = true;
if (detectorEntityElement instanceof HasPolicies && candidateEntityElement instanceof HasPolicies) {
HasPolicies detectorElement = (HasPolicies) detectorEntityElement;
HasPolicies candidate = (HasPolicies) candidateEntityElement;
if (Objects.nonNull(detectorElement.getPolicies()) && Objects.nonNull(candidate.getPolicies())) {
List<TPolicy> candidatePolicies = candidate.getPolicies();
characterizingPatternsCompatible = detectorElement.getPolicies().stream().allMatch(detectorPolicy -> {
if (this.namespaceManager.isPatternNamespace(detectorPolicy.getPolicyType().getNamespaceURI())) {
return candidatePolicies.stream().anyMatch(candidatePolicy -> {
boolean typeEquals = candidatePolicy.getPolicyType().equals(detectorPolicy.getPolicyType());
if (typeEquals && Objects.nonNull(detectorPolicy.getPolicyRef())) {
return Objects.nonNull(candidatePolicy.getPolicyRef()) && candidatePolicy.getPolicyRef().equals(detectorPolicy.getPolicyRef());
}
return typeEquals;
});
}
return true;
});
} else if (Objects.nonNull(detectorElement.getPolicies())) {
// only if there are patterns attached
characterizingPatternsCompatible = detectorElement.getPolicies().stream().noneMatch(detectorPolicy -> this.namespaceManager.isPatternNamespace(detectorPolicy.getPolicyType().getNamespaceURI()));
}
}
return characterizingPatternsCompatible;
}
use of org.eclipse.winery.model.tosca.HasPolicies in project winery by eclipse.
the class BehaviorPatternDetection method removeIncompatibleBehaviorPatterns.
private void removeIncompatibleBehaviorPatterns(TEntityTemplate refinementElement, TEntityTemplate addedElement, RefinementCandidate refinement) {
OTPatternRefinementModel prm = (OTPatternRefinementModel) refinement.getRefinementModel();
List<TPolicy> addedElementPolicies = ((HasPolicies) addedElement).getPolicies();
prm.getBehaviorPatternMappings().stream().filter(bpm -> bpm.getRefinementElement().getId().equals(refinementElement.getId())).forEach(bpm -> {
ToscaEntity detectorElement = refinement.getDetectorGraph().getVertexOrEdge(bpm.getDetectorElement().getId()).get();
TEntityTemplate candidateElement = getEntityCorrespondence(detectorElement, refinement.getGraphMapping());
if (ModelUtilities.hasKvProperties(detectorElement.getTemplate()) && ModelUtilities.hasKvProperties(candidateElement)) {
String detectorValue = ModelUtilities.getPropertiesKV(detectorElement.getTemplate()).get(bpm.getProperty().getKey());
String candidateValue = ModelUtilities.getPropertiesKV(candidateElement).get(bpm.getProperty().getKey());
boolean propsNotCompatible = (detectorValue != null && !detectorValue.isEmpty()) && !detectorValue.equalsIgnoreCase(candidateValue) && (!detectorValue.equals("*") || (candidateValue == null || candidateValue.isEmpty()));
if (propsNotCompatible) {
TPolicy behaviorPattern = ((HasPolicies) refinementElement).getPolicies().stream().filter(policy -> bpm.getBehaviorPattern().equals(policy.getName())).findFirst().get();
addedElementPolicies.removeIf(policy -> policy.getPolicyType().equals(behaviorPattern.getPolicyType()));
}
}
});
}
Aggregations