Search in sources :

Example 1 with RelationshipTypeId

use of org.eclipse.winery.common.ids.definitions.RelationshipTypeId in project winery by eclipse.

the class IGenericRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ComplianceRuleId id) {
    // We have to use a HashSet to ensure that no duplicate ids are added
    // E.g., there may be multiple relationship templates having the same type
    Collection<DefinitionsChildId> ids = new HashSet<>();
    TComplianceRule complianceRule = this.getElement(id);
    // TODO extend to required Structure
    if (complianceRule.getIdentifier() != null) {
        for (TEntityTemplate entityTemplate : complianceRule.getIdentifier().getNodeTemplateOrRelationshipTemplate()) {
            QName qname = entityTemplate.getType();
            if (entityTemplate instanceof TNodeTemplate) {
                ids.add(new NodeTypeId(qname));
                TNodeTemplate n = (TNodeTemplate) entityTemplate;
                // crawl through deployment artifacts
                TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
                if (deploymentArtifacts != null) {
                    List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
                    for (TDeploymentArtifact da : das) {
                        ids.add(new ArtifactTypeId(da.getArtifactType()));
                        if ((qname = da.getArtifactRef()) != null) {
                            ids.add(new ArtifactTemplateId(qname));
                        }
                    }
                }
                // crawl through reqs/caps
                TNodeTemplate.Requirements requirements = n.getRequirements();
                if (requirements != null) {
                    for (TRequirement req : requirements.getRequirement()) {
                        QName type = req.getType();
                        RequirementTypeId rtId = new RequirementTypeId(type);
                        ids.add(rtId);
                    }
                }
                TNodeTemplate.Capabilities capabilities = n.getCapabilities();
                if (capabilities != null) {
                    for (TCapability cap : capabilities.getCapability()) {
                        QName type = cap.getType();
                        CapabilityTypeId ctId = new CapabilityTypeId(type);
                        ids.add(ctId);
                    }
                }
                // crawl through policies
                org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
                if (policies != null) {
                    for (TPolicy pol : policies.getPolicy()) {
                        QName type = pol.getPolicyType();
                        PolicyTypeId ctId = new PolicyTypeId(type);
                        ids.add(ctId);
                    }
                }
            } else {
                assert (entityTemplate instanceof TRelationshipTemplate);
                ids.add(new RelationshipTypeId(qname));
            }
        }
    }
    return ids;
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TComplianceRule(org.eclipse.winery.model.tosca.TComplianceRule) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) QName(javax.xml.namespace.QName) TCapability(org.eclipse.winery.model.tosca.TCapability) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) TDeploymentArtifacts(org.eclipse.winery.model.tosca.TDeploymentArtifacts) PolicyTypeId(org.eclipse.winery.common.ids.definitions.PolicyTypeId) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) TPolicy(org.eclipse.winery.model.tosca.TPolicy) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId)

Example 2 with RelationshipTypeId

use of org.eclipse.winery.common.ids.definitions.RelationshipTypeId 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 3 with RelationshipTypeId

use of org.eclipse.winery.common.ids.definitions.RelationshipTypeId in project winery by eclipse.

the class IGenericRepository method getReferencedDefinitionsChildIds.

default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId id) {
    // We have to use a HashSet to ensure that no duplicate ids are added
    // E.g., there may be multiple relationship templates having the same type
    Collection<DefinitionsChildId> ids = new HashSet<>();
    TServiceTemplate serviceTemplate = this.getElement(id);
    // add included things to export queue
    TBoundaryDefinitions boundaryDefs;
    if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
        TBoundaryDefinitions.Policies policies = boundaryDefs.getPolicies();
        if (policies != null) {
            for (TPolicy policy : policies.getPolicy()) {
                PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
                ids.add(policyTypeId);
                PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
                ids.add(policyTemplateId);
            }
        }
    // reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
    }
    if (serviceTemplate.getTopologyTemplate() != null) {
        for (TEntityTemplate entityTemplate : serviceTemplate.getTopologyTemplate().getNodeTemplateOrRelationshipTemplate()) {
            QName qname = entityTemplate.getType();
            if (entityTemplate instanceof TNodeTemplate) {
                ids.add(new NodeTypeId(qname));
                TNodeTemplate n = (TNodeTemplate) entityTemplate;
                // crawl through deployment artifacts
                TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
                if (deploymentArtifacts != null) {
                    List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
                    for (TDeploymentArtifact da : das) {
                        ids.add(new ArtifactTypeId(da.getArtifactType()));
                        if ((qname = da.getArtifactRef()) != null) {
                            ids.add(new ArtifactTemplateId(qname));
                        }
                    }
                }
                // crawl through reqs/caps
                TNodeTemplate.Requirements requirements = n.getRequirements();
                if (requirements != null) {
                    for (TRequirement req : requirements.getRequirement()) {
                        QName type = req.getType();
                        RequirementTypeId rtId = new RequirementTypeId(type);
                        ids.add(rtId);
                    }
                }
                TNodeTemplate.Capabilities capabilities = n.getCapabilities();
                if (capabilities != null) {
                    for (TCapability cap : capabilities.getCapability()) {
                        QName type = cap.getType();
                        CapabilityTypeId ctId = new CapabilityTypeId(type);
                        ids.add(ctId);
                    }
                }
                // crawl through policies
                org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
                if (policies != null) {
                    for (TPolicy pol : policies.getPolicy()) {
                        QName type = pol.getPolicyType();
                        PolicyTypeId ctId = new PolicyTypeId(type);
                        ids.add(ctId);
                        QName template = pol.getPolicyRef();
                        if (template != null) {
                            PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
                            ids.add(policyTemplateId);
                        }
                    }
                }
            } else {
                assert (entityTemplate instanceof TRelationshipTemplate);
                ids.add(new RelationshipTypeId(qname));
            }
        }
    }
    return ids;
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) TRequirement(org.eclipse.winery.model.tosca.TRequirement) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) RelationshipTypeId(org.eclipse.winery.common.ids.definitions.RelationshipTypeId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) CapabilityTypeId(org.eclipse.winery.common.ids.definitions.CapabilityTypeId) QName(javax.xml.namespace.QName) TCapability(org.eclipse.winery.model.tosca.TCapability) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) TDeploymentArtifacts(org.eclipse.winery.model.tosca.TDeploymentArtifacts) PolicyTypeId(org.eclipse.winery.common.ids.definitions.PolicyTypeId) RequirementTypeId(org.eclipse.winery.common.ids.definitions.RequirementTypeId) TPolicy(org.eclipse.winery.model.tosca.TPolicy) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId)

Aggregations

QName (javax.xml.namespace.QName)3 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)3 NodeTypeId (org.eclipse.winery.common.ids.definitions.NodeTypeId)3 RelationshipTypeId (org.eclipse.winery.common.ids.definitions.RelationshipTypeId)3 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)3 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)3 HashSet (java.util.HashSet)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2 ArtifactTypeId (org.eclipse.winery.common.ids.definitions.ArtifactTypeId)2 CapabilityTypeId (org.eclipse.winery.common.ids.definitions.CapabilityTypeId)2 PolicyTypeId (org.eclipse.winery.common.ids.definitions.PolicyTypeId)2 RequirementTypeId (org.eclipse.winery.common.ids.definitions.RequirementTypeId)2 TCapability (org.eclipse.winery.model.tosca.TCapability)2 TComplianceRule (org.eclipse.winery.model.tosca.TComplianceRule)2 TDeploymentArtifact (org.eclipse.winery.model.tosca.TDeploymentArtifact)2 TDeploymentArtifacts (org.eclipse.winery.model.tosca.TDeploymentArtifacts)2 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)2 TPolicy (org.eclipse.winery.model.tosca.TPolicy)2 TRequirement (org.eclipse.winery.model.tosca.TRequirement)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1