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;
})));
}
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());
}
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"));
}
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
}
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()));
}
}
});
}
Aggregations