Search in sources :

Example 11 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class TopologyFragmentRefinement method applyDeploymentArtifactMapping.

private boolean applyDeploymentArtifactMapping(RefinementCandidate refinement, TNodeTemplate detectorNode, TNodeTemplate matchingNode, TTopologyTemplate topology, Map<String, String> idMapping) {
    List<OTDeploymentArtifactMapping> deploymentArtifactMappings = ((OTTopologyFragmentRefinementModel) refinement.getRefinementModel()).getDeploymentArtifactMappings();
    return matchingNode.getDeploymentArtifacts() == null || matchingNode.getDeploymentArtifacts().isEmpty() || (deploymentArtifactMappings != null && matchingNode.getDeploymentArtifacts().stream().filter(deploymentArtifact -> deploymentArtifact.getArtifactType() != null).allMatch(deploymentArtifact -> deploymentArtifactMappings.stream().filter(mapping -> mapping.getDetectorElement().getId().equals(detectorNode.getId())).anyMatch(mapping -> {
        if (ModelUtilities.isOfType(mapping.getArtifactType(), deploymentArtifact.getArtifactType(), this.artifactTypes)) {
            if (idMapping != null) {
                TNodeTemplate addedNode = topology.getNodeTemplate(idMapping.get(mapping.getRefinementElement().getId()));
                if (addedNode != null) {
                    List<TDeploymentArtifact> existingDAs = addedNode.getDeploymentArtifacts();
                    if (existingDAs == null) {
                        existingDAs = new ArrayList<>();
                        addedNode.setDeploymentArtifacts(existingDAs);
                    } else if (existingDAs.stream().anyMatch(da -> da.getName().equals(deploymentArtifact.getName()))) {
                        deploymentArtifact.setName(deploymentArtifact.getName() + UUID.randomUUID());
                    }
                    existingDAs.add(deploymentArtifact);
                } else {
                    LOGGER.error("Error while adding Deployment Artifacts! Node was not added to the topology!");
                }
            }
            return true;
        }
        return false;
    })));
}
Also used : OTDeploymentArtifactMapping(org.eclipse.winery.model.tosca.extensions.OTDeploymentArtifactMapping) OTAttributeMapping(org.eclipse.winery.model.tosca.extensions.OTAttributeMapping) TopologyFragmentRefinementModelId(org.eclipse.winery.model.ids.extensions.TopologyFragmentRefinementModelId) GraphMapping(org.jgrapht.GraphMapping) LoggerFactory(org.slf4j.LoggerFactory) RefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) IToscaMatcher(org.eclipse.winery.topologygraph.matching.IToscaMatcher) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTTopologyFragmentRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Map(java.util.Map) ToscaPrmPropertyMatcher(org.eclipse.winery.topologygraph.matching.ToscaPrmPropertyMatcher) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTAttributeMappingType(org.eclipse.winery.model.tosca.extensions.OTAttributeMappingType) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) Logger(org.slf4j.Logger) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) RefinementUtils.redirectRelation(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementUtils.redirectRelation) AbstractRefinement(org.eclipse.winery.model.adaptation.substitution.refinement.AbstractRefinement) UUID(java.util.UUID) RefinementId(org.eclipse.winery.model.ids.extensions.RefinementId) Collectors(java.util.stream.Collectors) RefinementUtils(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementUtils) HasId(org.eclipse.winery.model.tosca.HasId) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) QName(javax.xml.namespace.QName) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) OTDeploymentArtifactMapping(org.eclipse.winery.model.tosca.extensions.OTDeploymentArtifactMapping) OTTopologyFragmentRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 12 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class PatternRefinementTestWithGitBackedRepository method refineTopologyWithPatternsAnnotatedAsPolicies.

@Test
void refineTopologyWithPatternsAnnotatedAsPolicies() throws Exception {
    this.setRevisionTo("origin/plain");
    List<RefinementCandidate> myCandidates = new ArrayList<>();
    PatternRefinement refinement = new PatternRefinement((candidates, refinementServiceTemplate, currentTopology) -> {
        myCandidates.addAll(candidates);
        return null;
    });
    ServiceTemplateId serviceTemplateId = refinement.refineServiceTemplate(new ServiceTemplateId("http://winery.opentosca.org/test/servicetemplates", "NodeTemplateAnnotedWithPattern_w1-wip2", false));
    assertEquals(1, myCandidates.size());
    assertEquals("PolicyAnnotatedNtWithTwoKvProps_w1-wip1", myCandidates.get(0).getRefinementModel().getName());
}
Also used : RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Test(org.junit.jupiter.api.Test)

Example 13 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class PatternRefinementTestWithGitBackedRepository method refineTopologyWithPatternsAnnotatedAsPoliciesInMultipleInstances.

@Test
void refineTopologyWithPatternsAnnotatedAsPoliciesInMultipleInstances() throws Exception {
    this.setRevisionTo("origin/plain");
    List<RefinementCandidate> myCandidates = new ArrayList<>();
    PatternRefinement refinement = new PatternRefinement((candidates, refinementServiceTemplate, currentTopology) -> {
        myCandidates.addAll(candidates);
        return null;
    });
    // This ST contains two similar sub-graphs where one only defines a policy type whereas the second one defines a specific template as well.
    ServiceTemplateId serviceTemplateId = refinement.refineServiceTemplate(new ServiceTemplateId("http://winery.opentosca.org/test/servicetemplates", "NodeTemplateAnnotedWithPattern_w1-wip3", false));
    assertEquals(3, myCandidates.size());
    long countForRefinementModelsWithPolicyTypeOnly = myCandidates.stream().filter(refinementCandidate -> refinementCandidate.getRefinementModel().getName().equals("PolicyAnnotatedNtWithTwoKvProps_w1-wip1")).count();
    assertEquals(2, countForRefinementModelsWithPolicyTypeOnly);
    List<RefinementCandidate> moreSpecificPrm = myCandidates.stream().filter(refinementCandidate -> refinementCandidate.getRefinementModel().getName().equals("PolicyAnnotatedNtWithTwoKvProps_w1-wip2")).collect(Collectors.toList());
    assertEquals(1, moreSpecificPrm.size());
    ArrayList<String> nodeIdsToBeReplaced = moreSpecificPrm.get(0).getNodeIdsToBeReplaced();
    assertEquals(2, nodeIdsToBeReplaced.size());
    assertTrue(nodeIdsToBeReplaced.contains("NodeTypeWithTwoKVProperties_3"));
    assertTrue(nodeIdsToBeReplaced.contains("Infrastructure-As-A-Service_w1_3"));
}
Also used : Test(org.junit.jupiter.api.Test) List(java.util.List) TestWithGitBackedRepository(org.eclipse.winery.repository.TestWithGitBackedRepository) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collectors(java.util.stream.Collectors) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) Test(org.junit.jupiter.api.Test)

Example 14 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate in project winery by eclipse.

the class TestRefinementTest method applyRefinementTest.

@Test
public void applyRefinementTest() {
    // region *** topology ***
    TNodeTemplate tomcat = new TNodeTemplate();
    tomcat.setId("tomcat");
    tomcat.setType("{ns}tomcat");
    TNodeTemplate webShop = new TNodeTemplate();
    webShop.setId("webShop");
    webShop.setType("{ns}webShop");
    TNodeTemplate database = new TNodeTemplate();
    database.setId("database");
    database.setType("{ns}database");
    TRelationshipTemplate webShopOnTomcat = new TRelationshipTemplate();
    webShopOnTomcat.setSourceNodeTemplate(webShop);
    webShopOnTomcat.setTargetNodeTemplate(tomcat);
    webShopOnTomcat.setType("{ns}hostedOn");
    TRelationshipTemplate webShopToDatabase = new TRelationshipTemplate();
    webShopToDatabase.setTargetNodeTemplate(database);
    webShopToDatabase.setSourceNodeTemplate(webShop);
    webShopToDatabase.setType("{ns}connectsTo");
    TTopologyTemplate topologyTemplate = new TTopologyTemplate.Builder().addNodeTemplate(tomcat).addNodeTemplate(webShop).addNodeTemplate(database).addRelationshipTemplate(webShopOnTomcat).addRelationshipTemplate(webShopToDatabase).build();
    // endregion
    // region *** refinement model ***
    TNodeTemplate mySqlConnectorTest = new TNodeTemplate();
    mySqlConnectorTest.setId("sqlConnectorTest");
    mySqlConnectorTest.setType("{ns}sqlConnectorTest");
    TTopologyTemplate refinementTopology = new TTopologyTemplate.Builder().addNodeTemplate(mySqlConnectorTest).build();
    OTRelationMapping testHostedOn = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.OUTGOING).setRelationType(QName.valueOf("{ns}hostedOn")).setRefinementElement(mySqlConnectorTest).setDetectorElement(tomcat));
    OTRelationMapping testConnectsTo = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.OUTGOING).setRelationType(QName.valueOf("{ns}connectsTo")).setRefinementElement(mySqlConnectorTest).setDetectorElement(database));
    OTRelationMapping testIngoingRelationTest = new OTRelationMapping(new OTRelationMapping.Builder().setDirection(OTRelationDirection.INGOING).setRelationType(QName.valueOf("{ns}ingoingTest")).setRefinementElement(mySqlConnectorTest).setDetectorElement(webShop));
    List<OTRelationMapping> relationMappings = new ArrayList<>();
    relationMappings.add(testHostedOn);
    relationMappings.add(testConnectsTo);
    relationMappings.add(testIngoingRelationTest);
    OTTestRefinementModel testRefinementModel = new OTTestRefinementModel(new OTTestRefinementModel.Builder().setTestFragment(refinementTopology).setDetector(topologyTemplate).setRelationMappings(relationMappings));
    // endregion
    ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topologyTemplate);
    ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(testRefinementModel.getDetector());
    ToscaIsomorphismMatcher matcher = new ToscaIsomorphismMatcher();
    Iterator<GraphMapping<ToscaNode, ToscaEdge>> mappings = matcher.findMatches(detectorGraph, topologyGraph, new ToscaTypeMatcher());
    GraphMapping<ToscaNode, ToscaEdge> mapping = mappings.next();
    RefinementCandidate refinementCandidate = new RefinementCandidate(testRefinementModel, mapping, detectorGraph, 1);
    TestRefinement testRefinement = new TestRefinement();
    testRefinement.applyRefinement(refinementCandidate, topologyTemplate);
    // region *** assertions ***
    assertEquals(4, topologyTemplate.getNodeTemplates().size());
    assertEquals(5, topologyTemplate.getRelationshipTemplates().size());
    TRelationshipTemplate mimicTestHostedOnTomcat = topologyTemplate.getRelationshipTemplate("mimicTest-hostedOn");
    assertNotNull(mimicTestHostedOnTomcat);
    assertEquals("tomcat", mimicTestHostedOnTomcat.getTargetElement().getRef().getId());
    assertEquals("sqlConnectorTest", mimicTestHostedOnTomcat.getSourceElement().getRef().getId());
    TRelationshipTemplate mimicTestConnectsToDatabase = topologyTemplate.getRelationshipTemplate("mimicTest-connectsTo");
    assertNotNull(mimicTestConnectsToDatabase);
    assertEquals("database", mimicTestConnectsToDatabase.getTargetElement().getRef().getId());
    assertEquals("sqlConnectorTest", mimicTestConnectsToDatabase.getSourceElement().getRef().getId());
    TRelationshipTemplate mimicTestTestIngoingConnection = topologyTemplate.getRelationshipTemplate("mimicTest-ingoingTest");
    assertNotNull(mimicTestTestIngoingConnection);
    assertEquals("webShop", mimicTestTestIngoingConnection.getSourceElement().getRef().getId());
    assertEquals("sqlConnectorTest", mimicTestTestIngoingConnection.getTargetElement().getRef().getId());
// endregion
}
Also used : ToscaGraph(org.eclipse.winery.topologygraph.model.ToscaGraph) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) RefinementCandidate(org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate) ArrayList(java.util.ArrayList) OTTestRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTestRefinementModel) OTRelationMapping(org.eclipse.winery.model.tosca.extensions.OTRelationMapping) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ToscaTypeMatcher(org.eclipse.winery.topologygraph.matching.ToscaTypeMatcher) GraphMapping(org.jgrapht.GraphMapping) Test(org.junit.jupiter.api.Test)

Example 15 with RefinementCandidate

use of org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate 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

RefinementCandidate (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementCandidate)15 ArrayList (java.util.ArrayList)10 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)9 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)8 List (java.util.List)7 Map (java.util.Map)7 IToscaMatcher (org.eclipse.winery.topologygraph.matching.IToscaMatcher)7 ToscaNode (org.eclipse.winery.topologygraph.model.ToscaNode)7 Test (org.junit.jupiter.api.Test)7 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)6 ToscaEdge (org.eclipse.winery.topologygraph.model.ToscaEdge)6 ToscaGraph (org.eclipse.winery.topologygraph.model.ToscaGraph)6 DefaultRefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser)5 RefinementChooser (org.eclipse.winery.model.adaptation.substitution.refinement.RefinementChooser)5 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)5 OTRefinementModel (org.eclipse.winery.model.tosca.extensions.OTRefinementModel)5 OTStayMapping (org.eclipse.winery.model.tosca.extensions.OTStayMapping)5 LinkedHashMap (java.util.LinkedHashMap)4 QName (javax.xml.namespace.QName)4 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)4