use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class TargetLabelAssignment method isMatchableAllPredecessorsHaveTargetLabel.
private boolean isMatchableAllPredecessorsHaveTargetLabel(TNodeTemplate nodeTemplate) {
List<TNodeTemplate> predecessors = original.getHostedOnPredecessors(nodeTemplate);
Map<String, List<TNodeTemplate>> targetLabelGroups = groupByTargetLabel(predecessors);
// check each group if its matchable here
Set<TNodeTemplate> matchingFound = new HashSet<>();
for (Map.Entry<String, List<TNodeTemplate>> entry : targetLabelGroups.entrySet()) {
List<TRequirement> reqs = original.getRequirements(entry.getValue());
List<TTopologyTemplate> fragments = ProviderRepository.INSTANCE.getPaaSFragments(entry.getKey(), reqs);
if (!fragments.isEmpty()) {
matchingFound.addAll(entry.getValue());
}
}
Set<TNodeTemplate> noMatchingFound = new HashSet<>(predecessors);
noMatchingFound.removeAll(matchingFound);
if (noMatchingFound.isEmpty()) {
// all groups of target labels can be matched here
original.removeNotNeededSuccessors(nodeTemplate);
return true;
} else {
// at least one target label group can't be matched here
Set<String> targetLabelsToPropagate = new HashSet<>();
for (TNodeTemplate ntNoMatchFound : noMatchingFound) {
targetLabelsToPropagate.addAll(possibleTargetLabels.get(ntNoMatchFound));
}
possibleTargetLabels.get(nodeTemplate).addAll(targetLabelsToPropagate);
return false;
}
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class AllocationUtils method deepCopy.
/**
* Still no complete copy of all TOSCA constructs.
*/
public static TTopologyTemplate deepCopy(TTopologyTemplate topologyTemplate, boolean changeNames) {
TTopologyTemplate.Builder clone = new TTopologyTemplate.Builder();
Map<String, TNodeTemplate> clonedNTsByIds = new HashMap<>();
for (TNodeTemplate nodeTemplate : topologyTemplate.getNodeTemplates()) {
TNodeTemplate clonedNT = clone(nodeTemplate, changeNames);
clone.addNodeTemplate(clonedNT);
clonedNTsByIds.put(clonedNT.getId(), clonedNT);
}
for (TRelationshipTemplate relationshipTemplate : topologyTemplate.getRelationshipTemplates()) {
TRelationshipTemplate clonedRT = clone(relationshipTemplate, changeNames);
// source
if (relationshipTemplate.getSourceElement().getRef() instanceof TNodeTemplate) {
clonedRT.setSourceNodeTemplate(clonedNTsByIds.get(relationshipTemplate.getSourceElement().getRef().getId()));
} else {
TRelationshipTemplate.SourceOrTargetElement source = new TRelationshipTemplate.SourceOrTargetElement();
source.setRef(relationshipTemplate.getSourceElement().getRef());
clonedRT.setSourceElement(source);
}
// target
if (relationshipTemplate.getTargetElement().getRef() instanceof TNodeTemplate) {
clonedRT.setTargetNodeTemplate(clonedNTsByIds.get(relationshipTemplate.getTargetElement().getRef().getId()));
} else {
TRelationshipTemplate.SourceOrTargetElement target = new TRelationshipTemplate.SourceOrTargetElement();
target.setRef(relationshipTemplate.getTargetElement().getRef());
clonedRT.setTargetElement(target);
}
clone.addRelationshipTemplate(clonedRT);
}
return clone.build();
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class ProviderRepository method getTopologyFragments.
/**
* Get all fragments which fulfill all specified requirements from the given provider.
*
* @return the matching fragments if any exists, empty list else
*/
public List<TTopologyTemplate> getTopologyFragments(String targetLocation, List<TRequirement> requirements) {
if (targetLocation == null || requirements == null || requirements.isEmpty()) {
return new ArrayList<>();
}
Map<TRequirement, List<TTopologyTemplate>> fragmentsForRequirement = new HashMap<>();
List<TRequirement> mergedReqs = mergeByType(requirements);
for (TRequirement requirement : mergedReqs) {
List<TTopologyTemplate> fragments = getAllTopologyFragmentsForLocationAndOfferingCapability(targetLocation, requirement);
// all requirements have to have at least one possible fragment
if (fragments.isEmpty()) {
return new ArrayList<>();
} else {
fragmentsForRequirement.put(requirement, getAllTopologyFragmentsForLocationAndOfferingCapability(targetLocation, requirement));
}
}
return getIntersection(fragmentsForRequirement.values());
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate 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.TTopologyTemplate 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"))));
}
Aggregations