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;
}
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());
}
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);
}
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!";
}
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"));
}
Aggregations