Search in sources :

Example 6 with PolicyTemplateId

use of org.eclipse.winery.model.ids.definitions.PolicyTemplateId in project winery by eclipse.

the class ThreatModelingUtilsWithGitBackendRepositoryTest method checkThreatCatalog.

@Test
public void checkThreatCatalog() throws Exception {
    this.setRevisionTo("origin/plain");
    ThreatModelingUtils tmu = new ThreatModelingUtils(repository);
    assertTrue(tmu.getThreatCatalogue().isEmpty());
    PolicyTemplateId threatAID = new PolicyTemplateId("http://example.com", "MyThreat", false);
    TPolicyTemplate threatA = new TPolicyTemplate.Builder("myThreat", QName.valueOf(ThreatModelingConstants.THREAT_POLICY_ID)).setName("MyThreat").build();
    repository.setElement(threatAID, threatA);
    tmu = new ThreatModelingUtils(repository);
    assertFalse(tmu.getThreatCatalogue().isEmpty());
    assertEquals("MyThreat", tmu.getThreatCatalogue().get(0).getTemplateName());
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) Test(org.junit.jupiter.api.Test)

Example 7 with PolicyTemplateId

use of org.eclipse.winery.model.ids.definitions.PolicyTemplateId in project winery by eclipse.

the class IRepository 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) {
        List<TPolicy> policies = boundaryDefs.getPolicies();
        if (policies != null) {
            for (TPolicy policy : policies) {
                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)
    }
    final TTopologyTemplate topology = serviceTemplate.getTopologyTemplate();
    if (topology != null) {
        if (Objects.nonNull(topology.getPolicies())) {
            topology.getPolicies().stream().filter(Objects::nonNull).forEach(p -> {
                QName type = p.getPolicyType();
                PolicyTypeId policyTypeIdId = new PolicyTypeId(type);
                ids.add(policyTypeIdId);
            });
        }
        for (TEntityTemplate entityTemplate : topology.getNodeTemplateOrRelationshipTemplate()) {
            QName qname = entityTemplate.getType();
            if (entityTemplate instanceof TNodeTemplate) {
                ids.add(new NodeTypeId(qname));
                TNodeTemplate n = (TNodeTemplate) entityTemplate;
                // crawl through policies
                List<TPolicy> policies = n.getPolicies();
                if (policies != null) {
                    for (TPolicy pol : policies) {
                        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);
                        }
                    }
                }
                // Crawl RequirementTypes and Capabilities for their references
                getReferencedRequirementTypeIds(ids, n);
                getCapabilitiesReferences(ids, n);
                // TODO: this information is collected differently for YAML and XML modes
                // crawl through deployment artifacts
                List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
                if (deploymentArtifacts != null) {
                    for (TDeploymentArtifact da : deploymentArtifacts) {
                        if (da.getArtifactType() != null) {
                            // This is considered Nullable, because the test case ConsistencyCheckerTest#hasError
                            // expects an empty artifactType and thus it may be null.
                            ids.add(new ArtifactTypeId(da.getArtifactType()));
                        }
                        if (da.getArtifactRef() != null) {
                            ids.add(new ArtifactTemplateId(da.getArtifactRef()));
                        }
                    }
                }
                // Store all referenced artifact types
                List<TArtifact> artifacts = n.getArtifacts();
                if (Objects.nonNull(artifacts)) {
                    artifacts.forEach(a -> ids.add(new ArtifactTypeId(a.getType())));
                }
                TNodeType nodeType = this.getElement(new NodeTypeId(qname));
                if (Objects.nonNull(nodeType.getInterfaceDefinitions())) {
                    nodeType.getInterfaceDefinitions().stream().filter(Objects::nonNull).forEach(iDef -> {
                        if (Objects.nonNull(iDef.getType())) {
                            ids.add(new InterfaceTypeId(iDef.getType()));
                        }
                    });
                }
            } else {
                assert (entityTemplate instanceof TRelationshipTemplate);
                ids.add(new RelationshipTypeId(qname));
            }
        }
    }
    return ids;
}
Also used : RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) TArtifact(org.eclipse.winery.model.tosca.TArtifact) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TPolicy(org.eclipse.winery.model.tosca.TPolicy) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) HashSet(java.util.HashSet) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 8 with PolicyTemplateId

use of org.eclipse.winery.model.ids.definitions.PolicyTemplateId in project winery by eclipse.

the class VersionSupport method getDefinitionInTheGivenVersion.

public static DefinitionsChildId getDefinitionInTheGivenVersion(DefinitionsChildId childId, WineryVersion otherVersion) {
    if (childId.getVersion().compareTo(otherVersion) == 0) {
        return childId;
    }
    String localPart = childId.getNameWithoutVersion() + (otherVersion.toString().length() > 0 ? WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + otherVersion.toString() : "");
    QName qName = new QName(childId.getNamespace().getDecoded(), localPart);
    if (childId instanceof RelationshipTypeImplementationId) {
        return new RelationshipTypeImplementationId(qName);
    } else if (childId instanceof NodeTypeImplementationId) {
        return new NodeTypeImplementationId(qName);
    } else if (childId instanceof RequirementTypeId) {
        return new RequirementTypeId(qName);
    } else if (childId instanceof NodeTypeId) {
        return new NodeTypeId(qName);
    } else if (childId instanceof RelationshipTypeId) {
        return new RelationshipTypeId(qName);
    } else if (childId instanceof CapabilityTypeId) {
        return new CapabilityTypeId(qName);
    } else if (childId instanceof ArtifactTypeId) {
        return new ArtifactTypeId(qName);
    } else if (childId instanceof PolicyTypeId) {
        return new PolicyTypeId(qName);
    } else if (childId instanceof PolicyTemplateId) {
        return new PolicyTemplateId(qName);
    } else if (childId instanceof ServiceTemplateId) {
        return new ServiceTemplateId(qName);
    } else if (childId instanceof ArtifactTemplateId) {
        return new ArtifactTemplateId(qName);
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
}
Also used : NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId)

Example 9 with PolicyTemplateId

use of org.eclipse.winery.model.ids.definitions.PolicyTemplateId in project winery by eclipse.

the class ThreatModeling method findAbstractMitigationsForPresentThreats.

/**
 * find abstract SVNF group Node Types for appropriate mitigations based on the present threats of the current
 * Service Template
 */
private void findAbstractMitigationsForPresentThreats() {
    List<TServiceTemplate> mitigationCandidates = getServiceTemplateSubstitutionCandidates();
    mitigationCandidates.forEach((TServiceTemplate template) -> {
        QName substitutableNodeType = template.getSubstitutableNodeType();
        QName abstractMitigationType;
        try {
            abstractMitigationType = utils.findFirstAbstractType(substitutableNodeType);
        } catch (Exception e) {
            return;
        }
        List<TPolicy> mitigationPolicies = utils.getBoundaryDefinitionMitigationPolicies(template);
        mitigationPolicies.forEach((TPolicy mitigation) -> {
            PolicyTemplateId mitigationTemplateId = new PolicyTemplateId(mitigation.getPolicyRef());
            TPolicyTemplate mitigationTemplate = repository.getElement(mitigationTemplateId);
            if (Objects.nonNull(mitigationTemplate.getProperties())) {
                LinkedHashMap<String, String> properties = ModelUtilities.getPropertiesKV(mitigationTemplate);
                // FIXME Assumption that we're dealing with simple KV Properties
                String threatReferenceString = (String) properties.get("ThreatReference");
                QName threatReference = QName.valueOf(threatReferenceString);
                // check if the threat, the mitigation references is present.
                if (Objects.nonNull(abstractThreatMitigations.get(threatReference))) {
                    // add abstract SVNF to list of effective mitigations to present threat
                    Threat threat = abstractThreatMitigations.get(threatReference);
                    threat.addMitigations(abstractMitigationType);
                    abstractThreatMitigations.put(threatReference, threat);
                }
            }
        });
    });
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) QName(javax.xml.namespace.QName) TPolicy(org.eclipse.winery.model.tosca.TPolicy) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 10 with PolicyTemplateId

use of org.eclipse.winery.model.ids.definitions.PolicyTemplateId in project winery by eclipse.

the class ThreatModeling method getPresentThreats.

/**
 * Collect all threats present in the current Service Template
 */
private void getPresentThreats() {
    nodeTemplates.forEach((TNodeTemplate nt) -> {
        if (nt.getPolicies() != null) {
            nt.getPolicies().forEach((TPolicy policy) -> {
                String policyTypeName = policy.getPolicyType().toString();
                if (policyTypeName.equals(ThreatModelingConstants.THREAT_POLICY_ID)) {
                    QName threatQName = policy.getPolicyRef();
                    PolicyTemplateId threatId = new PolicyTemplateId(threatQName);
                    TPolicyTemplate threatTemplate = repository.getElement(threatId);
                    Threat threat = new Threat();
                    // set target of current threat.
                    threat.addTarget(nt.getName(), nt.getTypeAsQName());
                    if (Objects.nonNull(threatTemplate.getProperties())) {
                        threat.setProperties(ModelUtilities.getPropertiesKV(threatTemplate));
                    }
                    threat.setTemplateName(threatQName.getLocalPart());
                    threat.setNamespace(threatQName.getNamespaceURI());
                    abstractThreatMitigations.putIfAbsent(threatQName, threat);
                }
            });
        }
    });
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) QName(javax.xml.namespace.QName) TPolicy(org.eclipse.winery.model.tosca.TPolicy) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate)

Aggregations

PolicyTemplateId (org.eclipse.winery.model.ids.definitions.PolicyTemplateId)12 TPolicyTemplate (org.eclipse.winery.model.tosca.TPolicyTemplate)8 QName (javax.xml.namespace.QName)7 Test (org.junit.jupiter.api.Test)5 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)4 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)4 ArtifactTypeId (org.eclipse.winery.model.ids.definitions.ArtifactTypeId)3 NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)3 PolicyTypeId (org.eclipse.winery.model.ids.definitions.PolicyTypeId)3 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)3 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)3 TPolicy (org.eclipse.winery.model.tosca.TPolicy)3 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)3 LinkedHashMap (java.util.LinkedHashMap)2 CapabilityTypeId (org.eclipse.winery.model.ids.definitions.CapabilityTypeId)2 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)2 InterfaceTypeId (org.eclipse.winery.model.ids.definitions.InterfaceTypeId)2 NodeTypeImplementationId (org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId)2 RelationshipTypeImplementationId (org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId)2 RequirementTypeId (org.eclipse.winery.model.ids.definitions.RequirementTypeId)2