use of org.eclipse.winery.model.tosca.extensions.OTStayMapping in project winery by eclipse.
the class ToscaComponentPatternMatcherTest method isCompatible.
@Test
public void isCompatible() {
// same type
TNodeTemplate refinementTemplate = new TNodeTemplate(new TNodeTemplate.Builder("d", new QName("type1")));
ToscaNode refinement = new ToscaNode();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.setNodeTemplate(refinementTemplate);
TNodeTemplate candidateTemplate = new TNodeTemplate(new TNodeTemplate.Builder("c", new QName("type1")));
ToscaNode candidate = new ToscaNode();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidate.setNodeTemplate(candidateTemplate);
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setRefinementStructure(new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplate(refinementTemplate))));
// needs to be swapped manually as only prms retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
NamespaceManager namespaceManager = new MockNamespaceManager() {
@Override
public boolean isPatternNamespace(String namespace) {
return namespace.equals("patternNs");
}
};
ToscaComponentPatternMatcher matcher = new ToscaComponentPatternMatcher(prm, namespaceManager, new ArrayList<>(), new HashMap<>());
assertTrue(matcher.isCompatible(refinement, candidate));
// different type
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
assertFalse(matcher.isCompatible(refinement, candidate));
// share supertype
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// stay mapping exists
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(null).setRefinementElement(refinementTemplate));
List<OTStayMapping> stayMappings = new ArrayList<>();
stayMappings.add((OTStayMapping) PatternDetectionUtils.swapDetectorWithRefinement(stayMapping));
prm.setStayMappings(stayMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
assertNotNull(prm.getStayMappings());
prm.getStayMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// component pattern mapping exists
List<OTPermutationMapping> componentPatternMappings = new ArrayList<>();
OTPermutationMapping componentPatternMapping = new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(new TNodeTemplate.Builder("detector", QName.valueOf("{ns}type")).build()).setRefinementElement(refinementTemplate));
componentPatternMappings.add((OTPermutationMapping) PatternDetectionUtils.swapDetectorWithRefinement(componentPatternMapping));
prm.setPermutationMappings(componentPatternMappings);
assertFalse(matcher.isCompatible(refinement, candidate));
prm.getPermutationMappings().clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// different behavior patterns
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
refinementTemplate.setPolicies(refinementPolicies);
List<TPolicy> candidatePolicies = new ArrayList<>();
candidateTemplate.setPolicies(candidatePolicies);
// detector has behavior pattern, candidate doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.add(new TPolicy.Builder(QName.valueOf("{patternNs}type1")).build());
// detector and candidate have same behavior pattern
assertTrue(matcher.isCompatible(refinement, candidate));
refinementPolicies.clear();
// candidate has behavior pattern, detector doesn't
assertFalse(matcher.isCompatible(refinement, candidate));
candidatePolicies.clear();
assertTrue(matcher.isCompatible(refinement, candidate));
// detector supertype of candidate
refinementTemplate.setType(new QName("super"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("type"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// candidate supertype of detector
refinementTemplate.setType(new QName("type"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
candidateTemplate.setType(new QName("super"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super")));
assertTrue(matcher.isCompatible(refinement, candidate));
// different supertypes
refinementTemplate.setType(new QName("type1"));
refinement.getTypes().clear();
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("type1")));
refinement.addTEntityType(new TNodeType(new TNodeType.Builder("super1")));
candidateTemplate.setType(new QName("type2"));
candidate.getTypes().clear();
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("type2")));
candidate.addTEntityType(new TNodeType(new TNodeType.Builder("super2")));
assertFalse(matcher.isCompatible(refinement, candidate));
}
use of org.eclipse.winery.model.tosca.extensions.OTStayMapping in project winery by eclipse.
the class ComponentPatternDetectionTest method testRemoveBehaviorPatterns.
@Test
public void testRemoveBehaviorPatterns() {
// region: detector
TNodeTemplate detectorElement = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate detectorElement2 = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement2", QName.valueOf("{ns}pattern1")).setX("1").setY("1"));
List<TPolicy> detectorPolicies = new ArrayList<>();
detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}one")).setName("one")));
detectorPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}two")).setName("two")));
detectorElement.setPolicies(detectorPolicies);
TTopologyTemplate detector = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(detectorElement, detectorElement2)));
// endregion
// region: refinement
TNodeTemplate refinementElement = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate refinementElement2 = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement2", QName.valueOf("{ns}type2")).setX("1").setY("1"));
List<TPolicy> refinementPolicies = new ArrayList<>();
refinementPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}one")).setName("one")));
refinementElement.setPolicies(refinementPolicies);
TTopologyTemplate refinement = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(refinementElement, refinementElement2)));
// endregion
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement));
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinement).setStayMappings(Arrays.asList(stayMapping)));
PatternDetectionUtils.swapDetectorWithRefinement(prm);
TNodeTemplate nodeTemplate = new TNodeTemplate(new TNodeTemplate.Builder("nodeTemplate", QName.valueOf("{ns}type1")).setX("1").setY("1"));
TNodeTemplate nodeTemplate2 = new TNodeTemplate(new TNodeTemplate.Builder("nodeTemplate2", QName.valueOf("{ns}type2")).setX("1").setY("1"));
nodeTemplate.setPolicies(new ArrayList<>(refinementPolicies));
TTopologyTemplate topology = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(nodeTemplate, nodeTemplate2)));
ComponentPatternDetection detection = new ComponentPatternDetection(new DefaultRefinementChooser(), Arrays.asList(prm));
detection.refineTopology(topology);
assertEquals(topology.getNodeTemplates().size(), 2);
Set<QName> types = topology.getNodeTemplates().stream().map(TNodeTemplate::getType).collect(Collectors.toSet());
assertTrue(types.contains(QName.valueOf("{ns}type1")));
assertTrue(types.contains(QName.valueOf("{ns}pattern1")));
TNodeTemplate nodeTemplate1 = topology.getNodeTemplate("nodeTemplate");
assertNotNull(nodeTemplate1);
assertNotNull(nodeTemplate1.getPolicies());
List<TPolicy> policies = nodeTemplate1.getPolicies();
assertEquals(policies.size(), 1);
assertTrue(policies.stream().anyMatch(policy -> policy.getPolicyType().equals(QName.valueOf("{patternNs}one"))));
}
use of org.eclipse.winery.model.tosca.extensions.OTStayMapping in project winery by eclipse.
the class ComponentPatternDetectionTest method prm1.
private OTPatternRefinementModel prm1() {
// region: detector
TNodeTemplate processorD = new TNodeTemplate(new TNodeTemplate.Builder("processor-d1", QName.valueOf("{ns}java-8-app")).setX("1").setY("1"));
LinkedHashMap<String, String> processorDProps = new LinkedHashMap<>();
processorDProps.put("port", "8081");
ModelUtilities.setPropertiesKV(processorD, processorDProps);
TNodeTemplate executionEnv = new TNodeTemplate(new TNodeTemplate.Builder("executionEnv-d1", QName.valueOf("{ns}execution-env")).setX("1").setY("1"));
TNodeTemplate privateCloud = new TNodeTemplate(new TNodeTemplate.Builder("privateCloud-d1", QName.valueOf("{ns}private-cloud")).setX("1").setY("1"));
TRelationshipTemplate processorDToExecutionEnv = new TRelationshipTemplate(new TRelationshipTemplate.Builder("processorD-executionEnv-d1", QName.valueOf("{ns}hostedOn"), processorD, executionEnv));
TRelationshipTemplate executionEnvToPrivateCloud = new TRelationshipTemplate(new TRelationshipTemplate.Builder("executionEnv-privateCloud-d1", QName.valueOf("{ns}hostedOn"), executionEnv, privateCloud));
TTopologyTemplate detector = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(processorD, executionEnv, privateCloud)).addRelationshipTemplates(Arrays.asList(processorDToExecutionEnv, executionEnvToPrivateCloud)));
// endregion
// region: refinement
TNodeTemplate processorRs = new TNodeTemplate(new TNodeTemplate.Builder("processor-rs1", QName.valueOf("{ns}java-8-app")).setX("1").setY("1"));
TNodeTemplate webserver = new TNodeTemplate(new TNodeTemplate.Builder("webserver-rs1", QName.valueOf("{ns}tomcat-9")).setX("1").setY("1"));
TNodeTemplate vm = new TNodeTemplate(new TNodeTemplate.Builder("ubuntu-2004-rs1", QName.valueOf("{ns}vm")).setX("1").setY("1"));
TNodeTemplate openstack = new TNodeTemplate(new TNodeTemplate.Builder("openstack-rs1", QName.valueOf("{ns}openstack-victoria")).setX("1").setY("1"));
LinkedHashMap<String, String> processorRsProps = new LinkedHashMap<>();
processorRsProps.put("port", "8081");
ModelUtilities.setPropertiesKV(processorRs, processorRsProps);
LinkedHashMap<String, String> webserverProps = new LinkedHashMap<>();
webserverProps.put("jmxPort", "9091");
ModelUtilities.setPropertiesKV(webserver, webserverProps);
LinkedHashMap<String, String> vmProps = new LinkedHashMap<>();
vmProps.put("ram", "128GB");
ModelUtilities.setPropertiesKV(vm, vmProps);
LinkedHashMap<String, String> openstackProps = new LinkedHashMap<>();
openstackProps.put("api", "openstack.uni-stuttgart");
ModelUtilities.setPropertiesKV(openstack, openstackProps);
TRelationshipTemplate processorToWebserver = new TRelationshipTemplate(new TRelationshipTemplate.Builder("processor-webserver-rs1", QName.valueOf("{ns}hostedOn"), processorRs, webserver));
TRelationshipTemplate webserverToVm = new TRelationshipTemplate(new TRelationshipTemplate.Builder("webserver-vm-rs1", QName.valueOf("{ns}hostedOn"), webserver, vm));
TRelationshipTemplate vmToOpenstack = new TRelationshipTemplate(new TRelationshipTemplate.Builder("vm-openstack-rs1", QName.valueOf("{ns}hostedOn"), vm, openstack));
TTopologyTemplate refinement = new TTopologyTemplate(new TTopologyTemplate.Builder().addNodeTemplates(Arrays.asList(processorRs, webserver, vm, openstack)).addRelationshipTemplates(Arrays.asList(processorToWebserver, webserverToVm, vmToOpenstack)));
// endregion
OTStayMapping stayMapping = new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(processorD).setRefinementElement(processorRs));
List<OTPermutationMapping> permutationMappings = new ArrayList<>();
permutationMappings.add(new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(executionEnv).setRefinementElement(webserver)));
permutationMappings.add(new OTPermutationMapping(new OTPermutationMapping.Builder().setDetectorElement(privateCloud).setRefinementElement(openstack)));
List<OTAttributeMapping> attributeMappings = new ArrayList<>();
attributeMappings.add(new OTAttributeMapping(new OTAttributeMapping.Builder().setDetectorElement(executionEnv).setDetectorProperty("port").setRefinementElement(webserver).setRefinementProperty("jmxPort").setType(OTAttributeMappingType.SELECTIVE)));
attributeMappings.add(new OTAttributeMapping(new OTAttributeMapping.Builder().setDetectorElement(privateCloud).setDetectorProperty("interface").setRefinementElement(openstack).setRefinementProperty("api").setType(OTAttributeMappingType.SELECTIVE)));
List<OTRelationMapping> relationMappings = new ArrayList<>();
relationMappings.add(new OTRelationMapping(new OTRelationMapping.Builder().setDetectorElement(privateCloud).setRefinementElement(vm).setRelationType("{ns}hostedOn").setValidSourceOrTarget(null).setDirection(OTRelationDirection.INGOING)));
return new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinement).setStayMappings(Arrays.asList(stayMapping)).setPermutationMappings(permutationMappings).setAttributeMappings(attributeMappings).setRelationMappings(relationMappings));
}
use of org.eclipse.winery.model.tosca.extensions.OTStayMapping in project winery by eclipse.
the class BehaviorPatternDetectionTest method addCompatibleBehaviorPatterns.
@Test
public void addCompatibleBehaviorPatterns() {
List<TPolicy> behaviorPatterns = new ArrayList<>();
behaviorPatterns.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{patternNs}oneProp")).setName("oneProp")));
TNodeTemplate detectorElement = new TNodeTemplate(new TNodeTemplate.Builder("detectorElement", QName.valueOf("{ns}patternType")).setPolicies(behaviorPatterns).setX("1").setY("1"));
TNodeTemplate refinementElement = new TNodeTemplate(new TNodeTemplate.Builder("refinementElement", QName.valueOf("{ns}concreteType")).setX("1").setY("1"));
LinkedHashMap<String, String> refinementProps = new LinkedHashMap<>();
refinementProps.put("oneProp", "true");
ModelUtilities.setPropertiesKV(refinementElement, refinementProps);
List<TPolicy> stayingPolicies = new ArrayList<>();
stayingPolicies.add(new TPolicy(new TPolicy.Builder(QName.valueOf("{ns}normalPolicy")).setName("normalPolicy")));
TNodeTemplate stayingElement = new TNodeTemplate(new TNodeTemplate.Builder("stayingElement", QName.valueOf("{ns}concreteType")).setPolicies(stayingPolicies).setX("1").setY("1"));
LinkedHashMap<String, String> stayingProps = new LinkedHashMap<>();
stayingProps.put("oneProp", "false");
ModelUtilities.setPropertiesKV(stayingElement, stayingProps);
List<OTStayMapping> stayMappings = Collections.singletonList(new OTStayMapping(new OTStayMapping.Builder().setDetectorElement(detectorElement).setRefinementElement(refinementElement)));
List<OTBehaviorPatternMapping> behaviorPatternMappings = Collections.singletonList(new OTBehaviorPatternMapping(new OTBehaviorPatternMapping.Builder("behaviorPatternMap0").setDetectorElement(detectorElement).setRefinementElement(refinementElement).setBehaviorPattern("oneProp").setProperty(new OTPropertyKV("oneProp", "true"))));
TTopologyTemplate detector = new TTopologyTemplate();
detector.addNodeTemplate(detectorElement);
TTopologyTemplate refinementStructure = new TTopologyTemplate();
refinementStructure.addNodeTemplate(refinementElement);
OTPatternRefinementModel prm = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder().setDetector(detector).setRefinementStructure(refinementStructure).setStayMappings(stayMappings).setBehaviorPatternMappings(behaviorPatternMappings));
// needs to be swapped manually as only PRMs retrieved from repo are swapped automatically
PatternDetectionUtils.swapDetectorWithRefinement(prm);
TTopologyTemplate topology = new TTopologyTemplate();
topology.addNodeTemplate(stayingElement);
ToscaGraph topologyGraph = ToscaTransformer.createTOSCAGraph(topology);
ToscaGraph detectorGraph = ToscaTransformer.createTOSCAGraph(prm.getDetector());
IToscaMatcher matcher = new ToscaBehaviorPatternMatcher(prm, namespaceManager());
ToscaIsomorphismMatcher isomorphismMatcher = new ToscaIsomorphismMatcher();
Iterator<GraphMapping<ToscaNode, ToscaEdge>> matches = isomorphismMatcher.findMatches(detectorGraph, topologyGraph, matcher);
RefinementCandidate refinementCandidate = new RefinementCandidate(prm, matches.next(), detectorGraph, 1);
// 'normalPolicy' is not removed because not behavior pattern, 'oneProp' not present behavior pattern mapping doesn't match
BehaviorPatternDetection behaviorPatternDetection = new BehaviorPatternDetection(new DefaultRefinementChooser());
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
assertEquals(topology.getNodeTemplates().get(0).getId(), "stayingElement");
List<TPolicy> policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 1);
assertEquals(policies.get(0).getName(), "normalPolicy");
// 'normalPolicy' is not removed because not behavior pattern, 'oneProp' present behavior pattern mapping matches
stayingProps.put("oneProp", "true");
topology.getNodeTemplateOrRelationshipTemplate().clear();
topology.addNodeTemplate(stayingElement);
behaviorPatternDetection.applyRefinement(refinementCandidate, topology);
assertEquals(topology.getNodeTemplateOrRelationshipTemplate().size(), 1);
assertEquals(topology.getNodeTemplates().get(0).getId(), "stayingElement");
policies = topology.getNodeTemplates().get(0).getPolicies();
assertEquals(policies.size(), 2);
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("normalPolicy")));
assertTrue(policies.stream().anyMatch(policy -> policy.getName().equals("oneProp")));
}
use of org.eclipse.winery.model.tosca.extensions.OTStayMapping in project winery by eclipse.
the class PermutationHelper method generatePrmWithStayMapping.
static OTPatternRefinementModel generatePrmWithStayMapping() {
/*
######## ########
# (1) #----stay-------> # (11) #
######## ########
| (2) |
| (2) \/
\/ ########
########---------------> # (13) #
# (2) #--------+ ########
######## | | (2)
| | \/
| (2 +------> ########
\/ # (15) #
######## ########
# (3) #--------+ | (2)
######## | \/
| ########
+------> # (16) #
########
*/
TTopologyTemplate detector = generateDetectorWithThreePatterns();
// region refinement structure
TNodeTemplate refinementNode_11 = new TNodeTemplate();
refinementNode_11.setType("{http://ex.org}nodeType_11");
refinementNode_11.setId("11");
TNodeTemplate refinementNode_13 = new TNodeTemplate();
refinementNode_13.setType("{http://ex.org}nodeType_13");
refinementNode_13.setId("13");
TNodeTemplate refinementNode_15 = new TNodeTemplate();
refinementNode_15.setType("{http://ex.org}nodeType_15");
refinementNode_15.setId("15");
TNodeTemplate refinementNode_16 = new TNodeTemplate();
refinementNode_16.setType("{http://ex.org}nodeType_16");
refinementNode_16.setId("16");
TRelationshipTemplate node11_hostedOn_node13 = ModelUtilities.createRelationshipTemplate(refinementNode_11, refinementNode_13, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node13_hostedOn_node15 = ModelUtilities.createRelationshipTemplate(refinementNode_13, refinementNode_15, QName.valueOf("{http://ex.org}relType_hostedOn"));
TRelationshipTemplate node15_hostedOn_node16 = ModelUtilities.createRelationshipTemplate(refinementNode_15, refinementNode_16, QName.valueOf("{http://ex.org}relType_hostedOn"));
TTopologyTemplate refinementStructure = new TTopologyTemplate.Builder().addNodeTemplate(refinementNode_11).addNodeTemplate(refinementNode_13).addNodeTemplate(refinementNode_15).addNodeTemplate(refinementNode_16).addRelationshipTemplate(node11_hostedOn_node13).addRelationshipTemplate(node13_hostedOn_node15).addRelationshipTemplate(node15_hostedOn_node16).build();
// endregion
// region mappings
OTStayMapping pattern1_to_node11 = new OTStayMapping.Builder("sm-1--11").setDetectorElement(detector.getNodeTemplate("1")).setRefinementElement(refinementNode_11).build();
OTRelationMapping pattern2_to_node13 = new OTRelationMapping.Builder("pm-2--13").setRelationType("{http://ex.org}relType_hostedOn").setDirection(OTRelationDirection.INGOING).setDetectorElement(detector.getNodeTemplate("2")).setRefinementElement(refinementNode_13).build();
OTPermutationMapping pattern2_to_node15 = new OTPermutationMapping.Builder("pm-2--15").setDetectorElement(detector.getNodeTemplate("2")).setRefinementElement(refinementNode_15).build();
OTRelationMapping pattern3_to_node16 = new OTRelationMapping.Builder("pm-3--16").setRelationType(QName.valueOf("{http://ex.org}relType_connectsTo")).setDirection(OTRelationDirection.INGOING).setDetectorElement(detector.getNodeTemplate("3")).setRefinementElement(refinementNode_16).build();
// endregion
// region prm
ArrayList<OTPermutationMapping> mappings = new ArrayList<>();
mappings.add(pattern2_to_node15);
return new OTPatternRefinementModel.Builder().setName("PrmWithStaying").setTargetNamespace("http://ex.org").setDetector(detector).setRefinementStructure(refinementStructure).setRelationMappings(Arrays.asList(pattern2_to_node13, pattern3_to_node16)).setStayMappings(Collections.singletonList(pattern1_to_node11)).setPermutationMappings(mappings).build();
}
Aggregations