Search in sources :

Example 1 with HasPolicies

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));
}
Also used : DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ArrayList(java.util.ArrayList) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) Test(org.junit.jupiter.api.Test)

Example 2 with HasPolicies

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);
    }
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) TopologyFragmentRefinement(org.eclipse.winery.model.adaptation.substitution.refinement.topologyrefinement.TopologyFragmentRefinement) RefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) List(java.util.List) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) Map(java.util.Map) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) PatternDetectionUtils.getEntityCorrespondence(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils.getEntityCorrespondence) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TPolicy(org.eclipse.winery.model.tosca.TPolicy) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Example 3 with HasPolicies

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;
}
Also used : Objects(java.util.Objects) TPolicy(org.eclipse.winery.model.tosca.TPolicy) List(java.util.List) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TPolicy(org.eclipse.winery.model.tosca.TPolicy) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies)

Example 4 with HasPolicies

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()));
            }
        }
    });
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) TopologyFragmentRefinement(org.eclipse.winery.model.adaptation.substitution.refinement.topologyrefinement.TopologyFragmentRefinement) RefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) List(java.util.List) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) Map(java.util.Map) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) ToscaBehaviorPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) PatternDetectionUtils.getEntityCorrespondence(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils.getEntityCorrespondence) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) TPolicy(org.eclipse.winery.model.tosca.TPolicy) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) ToscaEntity(org.eclipse.winery.topologygraph.model.ToscaEntity) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Aggregations

HasPolicies (org.eclipse.winery.model.tosca.HasPolicies)4 TPolicy (org.eclipse.winery.model.tosca.TPolicy)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)3 ToscaEntity (org.eclipse.winery.topologygraph.model.ToscaEntity)3 Map (java.util.Map)2 RefinementCandidate (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate)2 RefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser)2 TopologyFragmentRefinement (org.eclipse.winery.model.adaptation.substitution.refinement.topologyrefinement.TopologyFragmentRefinement)2 PatternRefinementModelId (org.eclipse.winery.model.ids.extensions.PatternRefinementModelId)2 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)2 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)2 OTPatternRefinementModel (org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)2 OTRefinementModel (org.eclipse.winery.model.tosca.extensions.OTRefinementModel)2 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)2 IToscaMatcher (org.eclipse.winery.topologygraph.matching.IToscaMatcher)2 PatternDetectionUtils (org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils)2 PatternDetectionUtils.getEntityCorrespondence (org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils.getEntityCorrespondence)2 ToscaBehaviorPatternMatcher (org.eclipse.winery.topologygraph.matching.patterndetection.ToscaBehaviorPatternMatcher)2