Search in sources :

Example 86 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class MinHostsTest method test5PonyPastureProviderPony2FieldProvider.

@Test
public void test5PonyPastureProviderPony2FieldProvider() throws Exception {
    ServiceTemplateId id = new ServiceTemplateId("http://www.winery.opentosca.org/test/targetallocation/servicetemplates", "TargetAllocationMinHostsTest5_-w1-wip1", false);
    TopologyWrapper topology = new TopologyWrapper(repository.getElement(id).getTopologyTemplate());
    ModelUtilities.setTargetLabel(topology.getTopology().getNodeTemplate("shetland_pony"), "PastureProvider");
    ModelUtilities.setTargetLabel(topology.getTopology().getNodeTemplate("shetland_pony_2"), "FieldProvider");
    Criteria minHosts = new MinHosts(null, Integer.MAX_VALUE);
    List<TopologyWrapper> topologies = minHosts.allocate(topology);
    assertEquals(2, topologies.size());
    TTopologyTemplate allocated1 = topologies.get(0).getTopology();
    assertEquals(6, allocated1.getNodeTemplates().size());
    assertEquals(3, allocated1.getRelationshipTemplates().size());
    TTopologyTemplate allocated2 = topologies.get(1).getTopology();
    assertEquals(6, allocated2.getNodeTemplates().size());
    assertEquals(3, allocated2.getRelationshipTemplates().size());
    assertEquals("PastureProvider".toLowerCase(), ModelUtilities.getTargetLabel(allocated1.getNodeTemplate("shetland_pony")).get());
    assertEquals("FieldProvider".toLowerCase(), ModelUtilities.getTargetLabel(allocated1.getNodeTemplate("shetland_pony_2")).get());
    assertEquals(2, allocated1.getNodeTemplates().stream().map(ModelUtilities::getTargetLabel).collect(Collectors.toSet()).size());
    assertEquals("PastureProvider".toLowerCase(), ModelUtilities.getTargetLabel(allocated2.getNodeTemplate("shetland_pony")).get());
    assertEquals("FieldProvider".toLowerCase(), ModelUtilities.getTargetLabel(allocated2.getNodeTemplate("shetland_pony_2")).get());
    assertEquals(2, allocated2.getNodeTemplates().stream().map(ModelUtilities::getTargetLabel).collect(Collectors.toSet()).size());
    // one topology has shetland_pony_3 on pastureprovider, one on fieldprovider
    TNodeTemplate pony3Allocated1 = allocated1.getNodeTemplate("shetland_pony_3");
    TNodeTemplate pony3Allocated2 = allocated2.getNodeTemplate("shetland_pony_3");
    boolean pony3Provider = (ModelUtilities.getTargetLabel(pony3Allocated1).get().equalsIgnoreCase("PastureProvider") && ModelUtilities.getTargetLabel(pony3Allocated2).get().equalsIgnoreCase("FieldProvider")) || (ModelUtilities.getTargetLabel(pony3Allocated1).get().equalsIgnoreCase("FieldProvider") && ModelUtilities.getTargetLabel(pony3Allocated2).get().equalsIgnoreCase("PastureProvider"));
    assertTrue(pony3Provider);
}
Also used : TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TopologyWrapper(org.eclipse.winery.repository.targetallocation.util.TopologyWrapper) Criteria(org.eclipse.winery.repository.targetallocation.Criteria) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 87 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class TopologyMappingsWrapperTest method removeAllNTs.

@Test
public void removeAllNTs() {
    TNodeTemplate one = new TNodeTemplate("id1");
    TNodeTemplate two = new TNodeTemplate("id2");
    TNodeTemplate three = new TNodeTemplate("id3");
    TTopologyTemplate topologyTemplate = new TTopologyTemplate.Builder().addNodeTemplate(one).addNodeTemplate(two).addNodeTemplate(three).build();
    assertEquals(3, topologyTemplate.getNodeTemplates().size());
    TopologyWrapper topology = new TopologyWrapper(topologyTemplate);
    topology.removeAllNTs(Arrays.asList(one, two));
    assertEquals(1, topology.getNodeTemplates().size());
}
Also used : TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 88 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class TopologyMappingsWrapperTest method removeNT.

@Test
public void removeNT() {
    TNodeTemplate one = new TNodeTemplate("id1");
    TNodeTemplate two = new TNodeTemplate("id2");
    TNodeTemplate three = new TNodeTemplate("id3");
    TTopologyTemplate topologyTemplate = new TTopologyTemplate.Builder().addNodeTemplate(one).addNodeTemplate(two).addNodeTemplate(three).build();
    assertEquals(3, topologyTemplate.getNodeTemplates().size());
    TopologyWrapper topology = new TopologyWrapper(topologyTemplate);
    topology.removeNT(one);
    assertEquals(2, topology.getNodeTemplates().size());
}
Also used : TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) Test(org.junit.jupiter.api.Test)

Example 89 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class TOSCAGraphIsomorphismTest method testTOSCAComplianceRuleChecker.

@Test
public void testTOSCAComplianceRuleChecker() throws IOException, ComplianceCheckingException {
    HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
    TOSCAModelPropertiesBuilder bldr = new TOSCAModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "MyProperties");
    bldr.addProperty("key1", "value1");
    bldr.addProperty("key2", "value2");
    // create NodeTypes A B
    String idNodeTypeA = "idA";
    TNodeType nodeTypeA = TOSCAModelHelper.createTNodeType(idNodeTypeA, TEST_TARGET_NAMESPACE);
    String idNodeTypeB = "idB";
    TNodeType nodeTypeB = TOSCAModelHelper.createTNodeType(idNodeTypeB, TEST_TARGET_NAMESPACE);
    String idNodeTypeC = "idC";
    TNodeType nodeTypeC = TOSCAModelHelper.createTNodeType(idNodeTypeC, TEST_TARGET_NAMESPACE);
    NodeTypeId idA = TOSCAModelHelper.createNodeTypeId(idNodeTypeA);
    NodeTypeId idB = TOSCAModelHelper.createNodeTypeId(idNodeTypeB);
    NodeTypeId idC = TOSCAModelHelper.createNodeTypeId(idNodeTypeC);
    allEntities.put(idA, nodeTypeA);
    allEntities.put(idB, nodeTypeB);
    allEntities.put(idC, nodeTypeC);
    // createRelationshipTypes
    String relTypeIdAString = "adRelA";
    RelationshipTypeId relTypeIdA = new RelationshipTypeId(new QName(TEST_TARGET_NAMESPACE, relTypeIdAString));
    TRelationshipType relTypeA = TOSCAModelHelper.createTRelationshipType(relTypeIdAString, TEST_TARGET_NAMESPACE);
    allEntities.put(relTypeIdA, relTypeA);
    // createNodeTemplates
    TNodeTemplate nodeTemplate1 = TOSCAModelHelper.createTNodeTemplate("01");
    nodeTemplate1.setType(idA.getQName());
    TNodeTemplate nodeTemplate2 = TOSCAModelHelper.createTNodeTemplate("02");
    nodeTemplate2.setType(idB.getQName());
    TNodeTemplate nodeTemplate3 = TOSCAModelHelper.createTNodeTemplate("03");
    nodeTemplate3.setType(idA.getQName());
    nodeTemplate3.setProperties(bldr.build());
    TNodeTemplate nodeTemplate4 = TOSCAModelHelper.createTNodeTemplate("04");
    nodeTemplate4.setType(idB.getQName());
    TNodeTemplate nodeTemplate5 = TOSCAModelHelper.createTNodeTemplate("05");
    nodeTemplate5.setType(idA.getQName());
    TNodeTemplate nodeTemplate6 = TOSCAModelHelper.createTNodeTemplate("06");
    nodeTemplate6.setType(idB.getQName());
    TNodeTemplate nodeTemplate7 = TOSCAModelHelper.createTNodeTemplate("07");
    nodeTemplate7.setType(idA.getQName());
    nodeTemplate7.setProperties(bldr.build());
    TNodeTemplate nodeTemplate8 = TOSCAModelHelper.createTNodeTemplate("08");
    nodeTemplate8.setType(idB.getQName());
    TNodeTemplate nodeTemplate9 = TOSCAModelHelper.createTNodeTemplate("CompletelyUnrelated");
    nodeTemplate9.setType(idC.getQName());
    // create RelationshipTemplates
    TRelationshipTemplate relTemplate1 = TOSCAModelHelper.createTRelationshipTemplate("1");
    relTemplate1.setSourceNodeTemplate(nodeTemplate1);
    relTemplate1.setTargetNodeTemplate(nodeTemplate2);
    relTemplate1.setType(relTypeIdA.getQName());
    TRelationshipTemplate relTemplate2 = TOSCAModelHelper.createTRelationshipTemplate("2");
    relTemplate2.setSourceNodeTemplate(nodeTemplate3);
    relTemplate2.setTargetNodeTemplate(nodeTemplate4);
    relTemplate2.setType(relTypeIdA.getQName());
    TRelationshipTemplate relTemplate3 = TOSCAModelHelper.createTRelationshipTemplate("3");
    relTemplate3.setSourceNodeTemplate(nodeTemplate5);
    relTemplate3.setTargetNodeTemplate(nodeTemplate6);
    relTemplate3.setType(relTypeIdA.getQName());
    TRelationshipTemplate relTemplate4 = TOSCAModelHelper.createTRelationshipTemplate("4");
    relTemplate4.setSourceNodeTemplate(nodeTemplate7);
    relTemplate4.setTargetNodeTemplate(nodeTemplate8);
    relTemplate4.setType(relTypeIdA.getQName());
    // create TopologyTemplates
    List<TNodeTemplate> nodeTemplates = new ArrayList<>();
    List<TRelationshipTemplate> relationshipTemplates = new ArrayList<>();
    // create identifier
    nodeTemplates.add(nodeTemplate1);
    nodeTemplates.add(nodeTemplate2);
    relationshipTemplates.add(relTemplate1);
    TTopologyTemplate identifier = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
    identifier.setNodeTemplates(nodeTemplates);
    identifier.setRelationshipTemplates(relationshipTemplates);
    nodeTemplates.clear();
    relationshipTemplates.clear();
    // create required structure
    nodeTemplates.add(nodeTemplate3);
    nodeTemplates.add(nodeTemplate4);
    relationshipTemplates.add(relTemplate2);
    TTopologyTemplate requiredStructure = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
    requiredStructure.setNodeTemplates(nodeTemplates);
    requiredStructure.setRelationshipTemplates(relationshipTemplates);
    nodeTemplates.clear();
    relationshipTemplates.clear();
    // create topologyToSearchIn
    nodeTemplates.add(nodeTemplate5);
    nodeTemplates.add(nodeTemplate6);
    nodeTemplates.add(nodeTemplate7);
    nodeTemplates.add(nodeTemplate8);
    relationshipTemplates.add(relTemplate3);
    relationshipTemplates.add(relTemplate4);
    TTopologyTemplate topologyTemplateToSearchIn = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
    topologyTemplateToSearchIn.setNodeTemplates(nodeTemplates);
    topologyTemplateToSearchIn.setRelationshipTemplates(relationshipTemplates);
    nodeTemplates.clear();
    relationshipTemplates.clear();
    // create unrelated topology
    nodeTemplates.add(nodeTemplate9);
    TTopologyTemplate unrelatedTopology = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
    unrelatedTopology.setNodeTemplates(nodeTemplates);
    unrelatedTopology.setRelationshipTemplates(relationshipTemplates);
    nodeTemplates.clear();
    relationshipTemplates.clear();
    persist(allEntities);
    // **************** //
    // Test starts here //
    // **************** //
    ComplianceRuleChecker checker;
    // test null topologyTemplateToCheck
    checker = new ComplianceRuleChecker(null, null, null);
    ComplianceCheckingException expected = null;
    try {
        checker.checkComplianceRule();
    } catch (ComplianceCheckingException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertTrue(StringUtils.equals(ComplianceCheckingException.NO_TEMPLATE_TO_CHECK, expected.getMessage()));
    checker.setToCheckTemplate(topologyTemplateToSearchIn);
    // test empty rule
    expected = null;
    try {
        checker.checkComplianceRule();
    } catch (ComplianceCheckingException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertTrue(StringUtils.equals(ComplianceCheckingException.EMPTY_COMPLIANCE_RULE, expected.getMessage()));
    // test Whitelist
    checker.setRequiredStructureTemplate(requiredStructure);
    expected = null;
    try {
        checker.checkComplianceRule();
    } catch (ComplianceCheckingException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertTrue(StringUtils.equals(ComplianceCheckingException.WHITELISTING_NOT_YET_IMPLEMENTED, expected.getMessage()));
    // test blacklist
    checker.setRequiredStructureTemplate(null);
    checker.setIdentifierTemplate(identifier);
    List<GraphMapping> blacklistResult = checker.checkComplianceRule();
    assertEquals(2, blacklistResult.size());
    // test completeRule
    // invalid Rule: identifier and required structure have no mapping
    checker.setIdentifierTemplate(unrelatedTopology);
    checker.setRequiredStructureTemplate(requiredStructure);
    expected = null;
    try {
        checker.checkComplianceRule();
    } catch (ComplianceCheckingException e) {
        expected = e;
    }
    assertNotNull(expected);
    assertTrue(StringUtils.equals(ComplianceCheckingException.IDENTIFIER_NOT_IN_REQUIREDSTRUCTURE, expected.getMessage()));
    // valid Rule: identifier in required structure
    // finds one violation
    checker.setIdentifierTemplate(identifier);
    checker.setRequiredStructureTemplate(requiredStructure);
    List<GraphMapping> violatingMappings = checker.checkComplianceRule();
    assertEquals(1, violatingMappings.size());
    // check the mapping.
    // must contain nodesTemplates 5 & 6 and relTemplate3
    TOSCANode identifierNode1 = checker.getIdentifierGraph().getNode("01");
    TOSCANode identifierNode2 = checker.getIdentifierGraph().getNode("02");
    TOSCAEdge identifierEdge = checker.getIdentifierGraph().getEdge(identifierNode1, identifierNode2);
    GraphMapping violatingMapping = violatingMappings.stream().findFirst().get();
    assertNotNull(violatingMapping);
    Map<TOSCANode, TOSCANode> resultMap = checker.getSubGraphMappingAsMap(violatingMapping, checker.getIdentifierGraph());
    assertTrue(resultMap.get(identifierNode1).getId().matches("05"));
    assertTrue(resultMap.get(identifierNode2).getId().matches("06"));
    assertTrue(((TOSCAEdge) violatingMapping.getEdgeCorrespondence(identifierEdge, false)).getId().matches("3"));
    // finds no violation
    checker.setRequiredStructureTemplate(identifier);
    assertEquals(0, checker.checkComplianceRule().size());
    TComplianceRule rule = new TComplianceRule();
    rule.setName("test");
    rule.setTargetNamespace(TEST_TARGET_NAMESPACE);
    rule.setIdentifier(identifier);
    rule.setRequiredStructure(requiredStructure);
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) HashMap(java.util.HashMap) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) TOSCAEdge(org.eclipse.winery.compliance.model.TOSCAEdge) TComplianceRule(org.eclipse.winery.model.tosca.TComplianceRule) TOSCAModelHelper.createTComplianceRule(org.eclipse.winery.compliance.TOSCAModelHelper.createTComplianceRule) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TOSCAModelHelper.createTTopologyTemplate(org.eclipse.winery.compliance.TOSCAModelHelper.createTTopologyTemplate) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TOSCANode(org.eclipse.winery.compliance.model.TOSCANode) TOSCAModelHelper.createTNodeTemplate(org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ComplianceRuleChecker(org.eclipse.winery.compliance.checking.ComplianceRuleChecker) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) QName(javax.xml.namespace.QName) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) TOSCAModelHelper.createTNodeType(org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeType) TNodeType(org.eclipse.winery.model.tosca.TNodeType) ComplianceCheckingException(org.eclipse.winery.compliance.checking.ComplianceCheckingException) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TOSCAModelHelper.createNodeTypeId(org.eclipse.winery.compliance.TOSCAModelHelper.createNodeTypeId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId) GraphMapping(org.jgrapht.GraphMapping) Test(org.junit.Test)

Example 90 with TTopologyTemplate

use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.

the class IGenericRepository method getReferenceCount.

default int getReferenceCount(ArtifactTemplateId id) {
    // We do not use a database, therefore, we have to go through all possibilities pointing to the artifact template
    // DAs and IAs point to an artifact template
    // DAs are contained in Node Type Implementations and Node Templates
    // IAs are contained in Node Type Implementations and Relationship Type Implementations
    int count = 0;
    Collection<TDeploymentArtifact> allDAs = new HashSet<>();
    Collection<TImplementationArtifact> allIAs = new HashSet<>();
    // handle Node Type Implementation, which contains DAs and IAs
    SortedSet<NodeTypeImplementationId> nodeTypeImplementations = this.getAllDefinitionsChildIds(NodeTypeImplementationId.class);
    for (NodeTypeImplementationId ntiId : nodeTypeImplementations) {
        final TNodeTypeImplementation nodeTypeImplementation = this.getElement(ntiId);
        TDeploymentArtifacts deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
        if (deploymentArtifacts != null) {
            allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
        }
        TImplementationArtifacts implementationArtifacts = nodeTypeImplementation.getImplementationArtifacts();
        if (implementationArtifacts != null) {
            allIAs.addAll(implementationArtifacts.getImplementationArtifact());
        }
    }
    // check all Relationshiptype Implementations for IAs
    SortedSet<RelationshipTypeImplementationId> relationshipTypeImplementations = this.getAllDefinitionsChildIds(RelationshipTypeImplementationId.class);
    for (RelationshipTypeImplementationId rtiId : relationshipTypeImplementations) {
        TImplementationArtifacts implementationArtifacts = this.getElement(rtiId).getImplementationArtifacts();
        if (implementationArtifacts != null) {
            allIAs.addAll(implementationArtifacts.getImplementationArtifact());
        }
    }
    // check all node templates for DAs
    SortedSet<ServiceTemplateId> serviceTemplates = this.getAllDefinitionsChildIds(ServiceTemplateId.class);
    for (ServiceTemplateId sid : serviceTemplates) {
        TTopologyTemplate topologyTemplate = this.getElement(sid).getTopologyTemplate();
        if (topologyTemplate != null) {
            List<TEntityTemplate> nodeTemplateOrRelationshipTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
            for (TEntityTemplate template : nodeTemplateOrRelationshipTemplate) {
                if (template instanceof TNodeTemplate) {
                    TNodeTemplate nodeTemplate = (TNodeTemplate) template;
                    TDeploymentArtifacts deploymentArtifacts = nodeTemplate.getDeploymentArtifacts();
                    if (deploymentArtifacts != null) {
                        allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
                    }
                }
            }
        }
    }
    // now we have all DAs and IAs
    QName ourQName = id.getQName();
    // check DAs for artifact templates
    for (TDeploymentArtifact da : allDAs) {
        QName artifactRef = da.getArtifactRef();
        if (ourQName.equals(artifactRef)) {
            count++;
        }
    }
    // check IAs for artifact templates
    for (TImplementationArtifact ia : allIAs) {
        QName artifactRef = ia.getArtifactRef();
        if (ourQName.equals(artifactRef)) {
            count++;
        }
    }
    return count;
}
Also used : NodeTypeImplementationId(org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId) TImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifact) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) QName(javax.xml.namespace.QName) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) RelationshipTypeImplementationId(org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) TImplementationArtifacts(org.eclipse.winery.model.tosca.TImplementationArtifacts) TDeploymentArtifacts(org.eclipse.winery.model.tosca.TDeploymentArtifacts) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet)

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