Search in sources :

Example 1 with PolicyTemplateId

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

the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.

public static TDefinitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
    final TDefinitions definitions = createWrapperDefinitions(id, repository);
    HasIdInIdOrNameField element;
    if (id instanceof RelationshipTypeImplementationId) {
        element = new TRelationshipTypeImplementation();
    } else if (id instanceof NodeTypeImplementationId) {
        element = new TNodeTypeImplementation();
    } else if (id instanceof RequirementTypeId) {
        element = new TRequirementType();
    } else if (id instanceof NodeTypeId) {
        element = new TNodeType();
    } else if (id instanceof RelationshipTypeId) {
        element = new TRelationshipType();
    } else if (id instanceof CapabilityTypeId) {
        element = new TCapabilityType();
    } else if (id instanceof DataTypeId) {
        element = new TDataType();
    } else if (id instanceof ArtifactTypeId) {
        element = new TArtifactType();
    } else if (id instanceof PolicyTypeId) {
        element = new TPolicyType();
    } else if (id instanceof PolicyTemplateId) {
        element = new TPolicyTemplate();
    } else if (id instanceof ServiceTemplateId) {
        element = new TServiceTemplate();
    } else if (id instanceof ArtifactTemplateId) {
        element = new TArtifactTemplate();
    } else if (id instanceof ComplianceRuleId) {
        element = new OTComplianceRule(new OTComplianceRule.Builder(id.getXmlId().getDecoded()));
    } else if (id instanceof PatternRefinementModelId) {
        element = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
    } else if (id instanceof TopologyFragmentRefinementModelId) {
        element = new OTTopologyFragmentRefinementModel(new OTPatternRefinementModel.Builder());
    } else if (id instanceof TestRefinementModelId) {
        element = new OTTestRefinementModel(new OTTestRefinementModel.Builder());
    } else if (id instanceof InterfaceTypeId) {
        element = new TInterfaceType();
    } else if (id instanceof XSDImportId) {
        // TImport has no id; thus directly generating it without setting an id
        TImport tImport = new TImport();
        definitions.setElement(tImport);
        return definitions;
    } else {
        throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
    }
    copyIdToFields(element, id);
    definitions.setElement((TExtensibleElements) element);
    return definitions;
}
Also used : OTTopologyFragmentRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTopologyFragmentRefinementModel) ArtifactTypeId(org.eclipse.winery.model.ids.definitions.ArtifactTypeId) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TestRefinementModelId(org.eclipse.winery.model.ids.extensions.TestRefinementModelId) TImport(org.eclipse.winery.model.tosca.TImport) TCapabilityType(org.eclipse.winery.model.tosca.TCapabilityType) TDataType(org.eclipse.winery.model.tosca.TDataType) OTTestRefinementModel(org.eclipse.winery.model.tosca.extensions.OTTestRefinementModel) TRequirementType(org.eclipse.winery.model.tosca.TRequirementType) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) HasIdInIdOrNameField(org.eclipse.winery.model.tosca.HasIdInIdOrNameField) ComplianceRuleId(org.eclipse.winery.model.ids.extensions.ComplianceRuleId) InterfaceTypeId(org.eclipse.winery.model.ids.definitions.InterfaceTypeId) TArtifactType(org.eclipse.winery.model.tosca.TArtifactType) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) TopologyFragmentRefinementModelId(org.eclipse.winery.model.ids.extensions.TopologyFragmentRefinementModelId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CapabilityTypeId(org.eclipse.winery.model.ids.definitions.CapabilityTypeId) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) TNodeType(org.eclipse.winery.model.tosca.TNodeType) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PatternRefinementModelId(org.eclipse.winery.model.ids.extensions.PatternRefinementModelId) PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) RequirementTypeId(org.eclipse.winery.model.ids.definitions.RequirementTypeId) DataTypeId(org.eclipse.winery.model.ids.definitions.DataTypeId) TInterfaceType(org.eclipse.winery.model.tosca.TInterfaceType) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) OTComplianceRule(org.eclipse.winery.model.tosca.extensions.OTComplianceRule) OTPatternRefinementModel(org.eclipse.winery.model.tosca.extensions.OTPatternRefinementModel)

Example 2 with PolicyTemplateId

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

the class BackendUtilsTestWithGitBackedRepository method initializePropertiesDoesNothingInTheCaseOfXmlElementProperties.

@Test
public void initializePropertiesDoesNothingInTheCaseOfXmlElementProperties() throws Exception {
    this.setRevisionTo("origin/plain");
    PolicyTemplateId policyTemplateId = new PolicyTemplateId("http://www.example.org", "policytemplate", false);
    // create prepared policy template
    final TDefinitions definitions = BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(repository, policyTemplateId);
    final TPolicyTemplate policyTemplate = (TPolicyTemplate) definitions.getElement();
    QName policyTypeQName = new QName("http://plain.winery.opentosca.org/policytypes", "PolicyTypeWithXmlElementProperty");
    policyTemplate.setType(policyTypeQName);
    BackendUtils.initializeProperties(repository, policyTemplate);
    assertNull(policyTemplate.getProperties());
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) QName(javax.xml.namespace.QName) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Test(org.junit.jupiter.api.Test)

Example 3 with PolicyTemplateId

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

the class BackendUtilsTestWithGitBackedRepository method initializePropertiesGeneratesCorrectKvProperties.

@Test
public void initializePropertiesGeneratesCorrectKvProperties() throws Exception {
    this.setRevisionTo("origin/plain");
    PolicyTemplateId policyTemplateId = new PolicyTemplateId("http://www.example.org", "policytemplate", false);
    // create prepared policy template
    final TDefinitions definitions = BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(repository, policyTemplateId);
    final TPolicyTemplate policyTemplate = (TPolicyTemplate) definitions.getElement();
    QName policyTypeQName = new QName("http://plain.winery.opentosca.org/policytypes", "PolicyTypeWithTwoKvProperties");
    policyTemplate.setType(policyTypeQName);
    BackendUtils.initializeProperties(repository, policyTemplate);
    assertNotNull(policyTemplate.getProperties());
    LinkedHashMap<String, String> kvProperties = ModelUtilities.getPropertiesKV(policyTemplate);
    LinkedHashMap<String, String> expectedPropertyKVS = new LinkedHashMap<>();
    expectedPropertyKVS.put("key1", "");
    expectedPropertyKVS.put("key2", "");
    assertEquals(expectedPropertyKVS, kvProperties);
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) QName(javax.xml.namespace.QName) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 4 with PolicyTemplateId

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

the class ThreatModelingUtils method createThreatAndMitigationTemplates.

/**
 * Creates matching threat mitigations policy template pairs.
 *
 * @return status string passed to user
 * @throws IOException when persistence fails
 */
public String createThreatAndMitigationTemplates(ThreatCreationApiData data) throws IOException {
    /* check prerequisites for implicit setup */
    if (!checkPrerequisites()) {
        try {
            setupThreatModelingTypes();
        } catch (Exception e) {
            return "Error while setting up threat modeling types";
        }
    }
    /* Create Threat */
    TPolicyTemplate threat = new TPolicyTemplate();
    QName threatTypeQName = QName.valueOf(ThreatModelingConstants.THREAT_POLICY_ID);
    String threatName = Util.makeNCName(data.getName());
    PolicyTemplateId threatID = BackendUtils.getDefinitionsChildId(PolicyTemplateId.class, ThreatModelingConstants.THREATMODELING_NAMESPACE, threatName, false);
    threat.setId(threatName);
    threat.setName(threatName);
    threat.setType(threatTypeQName);
    TPolicyTemplate.WineryKVProperties threatProps = new TPolicyTemplate.WineryKVProperties();
    LinkedHashMap<String, String> propMap = new LinkedHashMap<>();
    propMap.put(ThreatModelingProperties.description.toString(), data.getDescription());
    propMap.put(ThreatModelingProperties.strideClassification.toString(), data.getStride());
    propMap.put(ThreatModelingProperties.severity.toString(), data.getSeverity());
    threatProps.setKVProperties(propMap);
    threat.setProperties(threatProps);
    TDefinitions threatDefinitions = BackendUtils.createWrapperDefinitions(threatID, repository);
    threatDefinitions.setElement(threat);
    try {
        BackendUtils.persist(repository, threatID, threatDefinitions);
    } catch (IOException i) {
        LOGGER.debug("Could not save new threat", i);
        return "Could not save new threat";
    }
    TPolicyTemplate mitigation = new TPolicyTemplate();
    QName mitigationTypeQName = QName.valueOf(ThreatModelingConstants.MITIGATION_POLICY_ID);
    String mitigationName = "MITIGATE_".concat(threatName);
    PolicyTemplateId mitigationID = BackendUtils.getDefinitionsChildId(PolicyTemplateId.class, ThreatModelingConstants.THREATMODELING_NAMESPACE, mitigationName, false);
    mitigation.setId(mitigationName);
    mitigation.setName(mitigationName);
    mitigation.setType(mitigationTypeQName);
    TPolicyTemplate.WineryKVProperties mitigationProps = new TPolicyTemplate.WineryKVProperties();
    LinkedHashMap<String, String> mitigationPropMap = new LinkedHashMap<>();
    mitigationPropMap.put(ThreatModelingProperties.ThreatReference.toString(), threatID.getQName().toString());
    mitigationProps.setKVProperties(mitigationPropMap);
    mitigation.setProperties(mitigationProps);
    TDefinitions mitigationDefinitions = BackendUtils.createWrapperDefinitions(threatID, repository);
    mitigationDefinitions.setElement(mitigation);
    try {
        BackendUtils.persist(repository, mitigationID, mitigationDefinitions);
    } catch (IOException i) {
        LOGGER.debug("Could not save new threat", i);
        return "Could not save new mitigation";
    }
    return "Threat created!";
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) QName(javax.xml.namespace.QName) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with PolicyTemplateId

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

the class ThreatModelingUtilsWithGitBackendRepositoryTest method checkThreatMitigationPairCreation.

@Test
public void checkThreatMitigationPairCreation() throws Exception {
    this.setRevisionTo("origin/plain");
    ThreatModelingUtils tmu = new ThreatModelingUtils(repository);
    PolicyTemplateId threat = new PolicyTemplateId(ThreatModelingConstants.THREATMODELING_NAMESPACE, "MyThreat", false);
    PolicyTemplateId mitigation = new PolicyTemplateId(ThreatModelingConstants.THREATMODELING_NAMESPACE, "MITIGATE_MyThreat", false);
    ThreatCreationApiData newThreat = new ThreatCreationApiData();
    newThreat.setName("MyThreat");
    newThreat.setDescription("My Description");
    newThreat.setStride("Spoofing");
    newThreat.setSeverity("Low");
    tmu.createThreatAndMitigationTemplates(newThreat);
    assertTrue(repository.exists(threat));
    assertTrue(repository.exists(mitigation));
    TPolicyTemplate mitigationPolicy = repository.getElement(mitigation);
    assertEquals(threat.getQName().toString(), ModelUtilities.getPropertiesKV(mitigationPolicy).get("ThreatReference"));
}
Also used : TPolicyTemplate(org.eclipse.winery.model.tosca.TPolicyTemplate) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) Test(org.junit.jupiter.api.Test)

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