Search in sources :

Example 56 with TTopologyTemplate

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;
    }
}
Also used : TRequirement(org.eclipse.winery.model.tosca.TRequirement) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) List(java.util.List) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 57 with TTopologyTemplate

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();
}
Also used : HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Example 58 with TTopologyTemplate

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());
}
Also used : TRequirement(org.eclipse.winery.model.tosca.TRequirement) HashMap(java.util.HashMap) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with TTopologyTemplate

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));
}
Also used : MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TNodeType(org.eclipse.winery.model.tosca.TNodeType) MockNamespaceManager(org.eclipse.winery.topologygraph.matching.MockNamespaceManager) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) TPolicy(org.eclipse.winery.model.tosca.TPolicy) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) Test(org.junit.jupiter.api.Test)

Example 60 with TTopologyTemplate

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"))));
}
Also used : TPolicy(org.eclipse.winery.model.tosca.TPolicy) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) OTRelationMapping(org.eclipse.winery.model.tosca.extensions.OTRelationMapping) HasPolicies(org.eclipse.winery.model.tosca.HasPolicies) OTAttributeMapping(org.eclipse.winery.model.tosca.extensions.OTAttributeMapping) PatternDetectionUtils(org.eclipse.winery.topologygraph.matching.patterndetection.PatternDetectionUtils) GraphMapping(org.jgrapht.GraphMapping) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) ToscaEdge(org.eclipse.winery.topologygraph.model.ToscaEdge) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) ToscaComponentPatternMatcher(org.eclipse.winery.topologygraph.matching.patterndetection.ToscaComponentPatternMatcher) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ToscaIsomorphismMatcher(org.eclipse.winery.topologygraph.matching.ToscaIsomorphismMatcher) OTPermutationMapping(org.eclipse.winery.model.tosca.extensions.OTPermutationMapping) OTRefinementModel(org.eclipse.winery.model.tosca.extensions.OTRefinementModel) OTRelationDirection(org.eclipse.winery.model.tosca.extensions.OTRelationDirection) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OTAttributeMappingType(org.eclipse.winery.model.tosca.extensions.OTAttributeMappingType) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) Iterator(java.util.Iterator) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ToscaNode(org.eclipse.winery.topologygraph.model.ToscaNode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) 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) Collections(java.util.Collections) ToscaTransformer(org.eclipse.winery.topologygraph.transformation.ToscaTransformer) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DefaultRefinementChooser(org.eclipse.winery.model.adaptation.substitution.refinement.DefaultRefinementChooser) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) OTStayMapping(org.eclipse.winery.model.tosca.extensions.OTStayMapping) TPolicy(org.eclipse.winery.model.tosca.TPolicy) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel) Test(org.junit.jupiter.api.Test)

Aggregations

TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)158 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)112 ArrayList (java.util.ArrayList)85 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)62 List (java.util.List)59 QName (javax.xml.namespace.QName)55 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)55 Test (org.junit.jupiter.api.Test)51 Map (java.util.Map)48 HashMap (java.util.HashMap)47 LinkedHashMap (java.util.LinkedHashMap)42 TNodeType (org.eclipse.winery.model.tosca.TNodeType)42 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)38 IRepository (org.eclipse.winery.repository.backend.IRepository)38 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)36 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)35 Objects (java.util.Objects)34 IOException (java.io.IOException)32 HashSet (java.util.HashSet)32 Collectors (java.util.stream.Collectors)32