Search in sources :

Example 16 with NodeTypeId

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

the class BackendUtilsTestWithGitBackedRepository method detectChangesInANodeTemplate.

@Test
public void detectChangesInANodeTemplate() throws Exception {
    this.setRevisionTo("origin/plain");
    NodeTypeId newVersion = new NodeTypeId("http://plain.winery.opentosca.org/nodetypes", "NodeTypeWithThreeReqCapPairsCoveringAllReqCapVariants_w1-wip1", false);
    WineryVersion oldVersion = new WineryVersion("", 0, 0);
    ToscaDiff toscaDiff = BackendUtils.compare(newVersion, oldVersion, repository);
    ToscaDiff properties = toscaDiff.getChildrenMap().get("winerysPropertiesDefinition");
    assertEquals(VersionState.CHANGED, toscaDiff.getState());
    assertEquals(VersionState.ADDED, properties.getState());
}
Also used : ToscaDiff(org.eclipse.winery.model.version.ToscaDiff) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) WineryVersion(org.eclipse.winery.common.version.WineryVersion) Test(org.junit.jupiter.api.Test)

Example 17 with NodeTypeId

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

the class ToscaGraphIsomorphismTest method testServiceTemplateComplianceRuleChecker.

@Test
public void testServiceTemplateComplianceRuleChecker() throws Exception {
    this.setRevisionTo("origin/plain");
    HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
    TServiceTemplate tServiceTemplate = createTServiceTemplate("ServiceTemplateTestId", TEST_TARGET_NAMESPACE);
    ComplianceRuleId crId1 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test1"));
    ComplianceRuleId crId2 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test2"));
    ComplianceRuleId crId3 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test3"));
    // create NodeType
    NodeTypeId nodeTypeIdA = createNodeTypeId("IDA");
    NodeTypeId nodeTypeIdB = createNodeTypeId("IDB");
    NodeTypeId nodeTypeIdC = createNodeTypeId("IDC");
    NodeTypeId nodeTypeIdD = createNodeTypeId("IDD");
    TNodeType nodeTypeA = createTNodeType(nodeTypeIdA);
    allEntities.put(nodeTypeIdA, nodeTypeA);
    TNodeType nodeTypeB = createTNodeType(nodeTypeIdB);
    setDerivedFrom(nodeTypeIdA, nodeTypeB);
    allEntities.put(nodeTypeIdB, nodeTypeB);
    TNodeType nodeTypeC = createTNodeType(nodeTypeIdC);
    setDerivedFrom(nodeTypeIdA, nodeTypeC);
    allEntities.put(nodeTypeIdC, nodeTypeC);
    TNodeType nodeTypeD = createTNodeType(nodeTypeIdD);
    allEntities.put(nodeTypeIdD, nodeTypeD);
    TNodeTemplate ruleOneIdentifier = createTNodeTemplate("R1_I", nodeTypeIdA);
    TNodeTemplate ruleOneRequiredStructure = createTNodeTemplate("R1_RS", nodeTypeIdB);
    TNodeTemplate ruleTwoIdentifier = createTNodeTemplate("R2_I", nodeTypeIdA);
    TNodeTemplate ruleTwoRequiredStructure = createTNodeTemplate("R2_RS", nodeTypeIdC);
    TNodeTemplate ruleThreeIdentifier = createTNodeTemplate("R3_I", nodeTypeIdA);
    TNodeTemplate ruleThreeRequiredStructure = createTNodeTemplate("R3_RS", nodeTypeIdD);
    TNodeTemplate serviceTemplateTopology = createTNodeTemplate("ST", nodeTypeIdB);
    // satisfied
    OTComplianceRule ruleOne = createTComplianceRule(crId1);
    ruleOne.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleOneIdentifier), newArrayList()));
    ruleOne.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleOneRequiredStructure), newArrayList()));
    allEntities.put(crId1, ruleOne);
    // unsatisfied
    OTComplianceRule ruleTwo = createTComplianceRule(crId2);
    ruleTwo.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleTwoIdentifier), newArrayList()));
    ruleTwo.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleTwoRequiredStructure), newArrayList()));
    allEntities.put(crId2, ruleTwo);
    // invalid rule
    OTComplianceRule ruleThree = createTComplianceRule(crId3);
    ruleThree.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleThreeIdentifier), newArrayList()));
    ruleThree.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleThreeRequiredStructure), newArrayList()));
    allEntities.put(crId3, ruleThree);
    tServiceTemplate.setTopologyTemplate(createTTopologyTemplate(Arrays.asList(serviceTemplateTopology), newArrayList()));
    persist(allEntities);
    ServiceTemplateComplianceRuleRuleChecker checker = new ServiceTemplateComplianceRuleRuleChecker(tServiceTemplate);
    assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().count());
    assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().filter(id -> id.getQName().getLocalPart().matches("test1|test2|test3")).count());
    ServiceTemplateCheckingResult serviceTemplateCheckingResult = checker.checkComplianceRules();
    assertEquals(1, serviceTemplateCheckingResult.getSatisfied().size());
    assertEquals(1, serviceTemplateCheckingResult.getUnsatisfied().size());
    assertEquals(1, serviceTemplateCheckingResult.getException().size());
    JAXBContext jaxbContext = null;
    try {
        jaxbContext = JAXBContext.newInstance(ServiceTemplateCheckingResult.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(serviceTemplateCheckingResult, sw);
        System.out.println(sw.toString());
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}
Also used : ServiceTemplateCheckingResult(org.eclipse.winery.compliance.checking.ServiceTemplateCheckingResult) Marshaller(javax.xml.bind.Marshaller) ServiceTemplateComplianceRuleRuleChecker(org.eclipse.winery.compliance.checking.ServiceTemplateComplianceRuleRuleChecker) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) JAXBContext(javax.xml.bind.JAXBContext) ToscaModelHelper.createTNodeType(org.eclipse.winery.compliance.ToscaModelHelper.createTNodeType) TNodeType(org.eclipse.winery.model.tosca.TNodeType) ComplianceRuleId(org.eclipse.winery.model.ids.extensions.ComplianceRuleId) StringWriter(java.io.StringWriter) ToscaModelHelper.createNodeTypeId(org.eclipse.winery.compliance.ToscaModelHelper.createNodeTypeId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ToscaModelHelper.createTNodeTemplate(org.eclipse.winery.compliance.ToscaModelHelper.createTNodeTemplate) OTComplianceRule(org.eclipse.winery.model.tosca.extensions.OTComplianceRule) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) ToscaModelHelper.createTServiceTemplate(org.eclipse.winery.compliance.ToscaModelHelper.createTServiceTemplate) Test(org.junit.jupiter.api.Test)

Example 18 with NodeTypeId

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

the class ToscaGraphIsomorphismTest method testTOSCAComplianceRuleMatcher.

@Test
public void testTOSCAComplianceRuleMatcher() throws IOException {
    // TNodeTemplateMatching
    // by TNodeType
    HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
    NodeTypeId nodeTypeIdAbstractA = createNodeTypeId("AbstractA");
    allEntities.put(nodeTypeIdAbstractA, createTNodeType(nodeTypeIdAbstractA));
    NodeTypeId nodeTypeIdAbstractB = createNodeTypeId("AbstractB");
    allEntities.put(nodeTypeIdAbstractB, createTNodeType(nodeTypeIdAbstractB));
    NodeTypeId nodeTypeIdActualA = createNodeTypeId("ActualA");
    TNodeType actualA = createTNodeType(nodeTypeIdActualA);
    setDerivedFrom(nodeTypeIdAbstractA, actualA);
    allEntities.put(nodeTypeIdActualA, actualA);
    NodeTypeId nodeTypeIdActualB = createNodeTypeId("ActualB");
    TNodeType actualB = createTNodeType(nodeTypeIdActualB);
    setDerivedFrom(nodeTypeIdAbstractB, actualB);
    allEntities.put(nodeTypeIdActualB, actualB);
    NodeTypeId nodeTypeIdActualB2 = createNodeTypeId("ActualB2");
    TNodeType actualB2 = createTNodeType(nodeTypeIdActualB2);
    setDerivedFrom(nodeTypeIdAbstractB, actualB2);
    allEntities.put(nodeTypeIdActualB2, actualB2);
    persist(allEntities);
    TNodeTemplate nodeTemplate1 = createTNodeTemplate("01", nodeTypeIdAbstractA);
    TNodeTemplate nodeTemplate2 = createTNodeTemplate("02", nodeTypeIdAbstractB);
    TNodeTemplate nodeTemplate3 = createTNodeTemplate("03", nodeTypeIdActualA);
    TNodeTemplate nodeTemplate4 = createTNodeTemplate("04", nodeTypeIdActualB);
    TNodeTemplate nodeTemplate5 = createTNodeTemplate("05", nodeTypeIdActualB);
    TNodeTemplate nodeTemplate6 = createTNodeTemplate("06", nodeTypeIdActualB2);
    ToscaComplianceRuleMatcher matcher = new ToscaComplianceRuleMatcher();
    // same type
    assertTrue(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate1), createTOSCANodeOnlyTypes(nodeTemplate1)));
    // "01" is supertype of "03" -> match
    assertTrue(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate1), createTOSCANodeOnlyTypes(nodeTemplate3)));
    // "03" is subtype of "01" -> match
    assertTrue(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate3), createTOSCANodeOnlyTypes(nodeTemplate1)));
    // different types
    assertFalse(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate4), createTOSCANodeOnlyTypes(nodeTemplate3)));
    // different types
    assertFalse(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate3), createTOSCANodeOnlyTypes(nodeTemplate2)));
    // same type
    assertTrue(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate5), createTOSCANodeOnlyTypes(nodeTemplate4)));
    // different types derived from same type
    assertFalse(matcher.isTEntityTypesCompatible(createTOSCANodeOnlyTypes(nodeTemplate5), createTOSCANodeOnlyTypes(nodeTemplate6)));
    // by TEntityTemplate.Properties
    ToscaModelPropertiesBuilder bldrLeft = new ToscaModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "TestProperties");
    ToscaModelPropertiesBuilder bldrRight = new ToscaModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "TestProperties");
    // test by values
    // test simple matching
    bldrLeft.addProperty("A", "Something");
    bldrRight.addProperty("A", "Something");
    assertTrue(matcher.isPropertiesCompatible(createTOSCANodeOnlyProperties(bldrLeft), createTOSCANodeOnlyProperties(bldrRight)));
    bldrRight.addProperty("A", "SomeOtherThing");
    assertFalse(matcher.isPropertiesCompatible(createTOSCANodeOnlyProperties(bldrLeft), createTOSCANodeOnlyProperties(bldrRight)));
    // testRegex
    bldrLeft.addProperty("A", ".*");
    assertTrue(matcher.isPropertiesCompatible(createTOSCANodeOnlyProperties(bldrLeft), createTOSCANodeOnlyProperties(bldrRight)));
    // test by property set
    // left subset of right
    bldrLeft.addProperty("B", ".*");
    bldrRight.addProperty("B", ".*");
    bldrRight.addProperty("C", ".*");
    assertTrue(matcher.isPropertiesCompatible(createTOSCANodeOnlyProperties(bldrLeft), createTOSCANodeOnlyProperties(bldrRight)));
    // other way round
    assertFalse(matcher.isPropertiesCompatible(createTOSCANodeOnlyProperties(bldrRight), createTOSCANodeOnlyProperties(bldrLeft)));
}
Also used : DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) HashMap(java.util.HashMap) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) ToscaModelHelper.createNodeTypeId(org.eclipse.winery.compliance.ToscaModelHelper.createNodeTypeId) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) ToscaComplianceRuleMatcher(org.eclipse.winery.compliance.checking.ToscaComplianceRuleMatcher) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ToscaModelHelper.createTNodeTemplate(org.eclipse.winery.compliance.ToscaModelHelper.createTNodeTemplate) ToscaModelHelper.createTNodeType(org.eclipse.winery.compliance.ToscaModelHelper.createTNodeType) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Test(org.junit.jupiter.api.Test)

Example 19 with NodeTypeId

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

the class ModelUtilitiesTest method instantiateNodeTemplate.

// endregion
@Test
public void instantiateNodeTemplate() throws Exception {
    this.setRevisionTo("origin/plain");
    IRepository repository = RepositoryFactory.getRepository();
    TNodeType nodeType = repository.getElement(new NodeTypeId(QName.valueOf("{http://opentosca.org/add/management/to/instances/nodetypes}Ubuntu_16.04-w1")));
    TNodeTemplate nodeTemplate = ModelUtilities.instantiateNodeTemplate(nodeType);
    assertNotNull(nodeTemplate);
    assertNotNull(nodeTemplate.getProperties());
    assertEquals(8, ((TEntityTemplate.WineryKVProperties) nodeTemplate.getProperties()).getKVProperties().size());
}
Also used : NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IRepository(org.eclipse.winery.repository.backend.IRepository) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Test(org.junit.jupiter.api.Test)

Example 20 with NodeTypeId

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

the class ThreatModelingUtils method setupThreatModelingTypes.

/**
 * create all Policy Types and Node Types required for threat modeling
 *
 * @throws Exception if setup was already done
 */
public void setupThreatModelingTypes() throws Exception {
    if (checkPrerequisites()) {
        throw new Exception("Threat modeling already set up.");
    }
    TPolicyType threat = new TPolicyType();
    threat.setId(ThreatModelingConstants.THREAT_POLICY_NAME);
    threat.setName(ThreatModelingConstants.THREAT_POLICY_NAME);
    threat.setAbstract(false);
    threat.setFinal(false);
    threat.setTargetNamespace(ThreatModelingConstants.THREATMODELING_NAMESPACE);
    threat.setProperties(null);
    WinerysPropertiesDefinition threatProps = new WinerysPropertiesDefinition();
    threatProps.setElementName("properties");
    threatProps.setNamespace(ThreatModelingConstants.THREATMODELING_NAMESPACE.concat("/propertiesdefinition/winery"));
    List<PropertyDefinitionKV> threatPropList = new ArrayList<>(Arrays.asList(new PropertyDefinitionKV(ThreatModelingProperties.description.toString(), "xsd:string"), new PropertyDefinitionKV(ThreatModelingProperties.strideClassification.toString(), "xsd:string"), new PropertyDefinitionKV(ThreatModelingProperties.severity.toString(), "xsd:string")));
    threatProps.setPropertyDefinitions(threatPropList);
    ModelUtilities.replaceWinerysPropertiesDefinition(threat, threatProps);
    PolicyTypeId threatID = BackendUtils.getDefinitionsChildId(PolicyTypeId.class, ThreatModelingConstants.THREATMODELING_NAMESPACE, ThreatModelingConstants.THREAT_POLICY_NAME, false);
    TDefinitions threatDefinitions = BackendUtils.createWrapperDefinitions(threatID, repository);
    threatDefinitions.setElement(threat);
    TPolicyType mitigation = new TPolicyType();
    mitigation.setId(ThreatModelingConstants.MITIGATION_POLICY_NAME);
    mitigation.setName(ThreatModelingConstants.MITIGATION_POLICY_NAME);
    mitigation.setAbstract(false);
    mitigation.setFinal(false);
    mitigation.setTargetNamespace(ThreatModelingConstants.THREATMODELING_NAMESPACE);
    mitigation.setProperties(null);
    WinerysPropertiesDefinition mitigationProps = new WinerysPropertiesDefinition();
    List<PropertyDefinitionKV> mitigationPropList = new ArrayList<>();
    mitigationProps.setElementName("properties");
    mitigationProps.setNamespace(ThreatModelingConstants.THREATMODELING_NAMESPACE.concat("/propertiesdefinition/winery"));
    mitigationPropList.add(new PropertyDefinitionKV(ThreatModelingProperties.ThreatReference.toString(), "xsd:string"));
    mitigationProps.setPropertyDefinitions(mitigationPropList);
    ModelUtilities.replaceWinerysPropertiesDefinition(mitigation, mitigationProps);
    PolicyTypeId mitigationID = BackendUtils.getDefinitionsChildId(PolicyTypeId.class, ThreatModelingConstants.THREATMODELING_NAMESPACE, ThreatModelingConstants.MITIGATION_POLICY_NAME, false);
    TDefinitions mitigationDefinitions = BackendUtils.createWrapperDefinitions(mitigationID, repository);
    mitigationDefinitions.setElement(mitigation);
    TNodeType svnf = new TNodeType.Builder("S-VNF-w1_wip1").setTargetNamespace(ThreatModelingConstants.SECURITY_NAMESPACE).setAbstract(true).build();
    NodeTypeId svnfID = new NodeTypeId(QName.valueOf(ThreatModelingConstants.SVNF_NODE_TYPE));
    TDefinitions svnfDefinitions = BackendUtils.createWrapperDefinitions(svnfID, repository);
    svnfDefinitions.setElement(svnf);
    try {
        BackendUtils.persist(repository, threatID, threatDefinitions);
        BackendUtils.persist(repository, mitigationID, mitigationDefinitions);
        BackendUtils.persist(repository, svnfID, svnfDefinitions);
    } catch (IOException i) {
        LOGGER.debug("Could not set up threat modeling", i);
    }
}
Also used : PolicyTypeId(org.eclipse.winery.model.ids.definitions.PolicyTypeId) PropertyDefinitionKV(org.eclipse.winery.model.tosca.extensions.kvproperties.PropertyDefinitionKV) TPolicyType(org.eclipse.winery.model.tosca.TPolicyType) WinerysPropertiesDefinition(org.eclipse.winery.model.tosca.extensions.kvproperties.WinerysPropertiesDefinition) ArrayList(java.util.ArrayList) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) IOException(java.io.IOException) TNodeType(org.eclipse.winery.model.tosca.TNodeType)

Aggregations

NodeTypeId (org.eclipse.winery.model.ids.definitions.NodeTypeId)67 TNodeType (org.eclipse.winery.model.tosca.TNodeType)36 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)28 Test (org.junit.jupiter.api.Test)27 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)23 QName (javax.xml.namespace.QName)21 IRepository (org.eclipse.winery.repository.backend.IRepository)20 RelationshipTypeId (org.eclipse.winery.model.ids.definitions.RelationshipTypeId)15 TTopologyTemplate (org.eclipse.winery.model.tosca.TTopologyTemplate)15 WineryVersion (org.eclipse.winery.common.version.WineryVersion)14 Response (javax.ws.rs.core.Response)13 IOException (java.io.IOException)12 LinkedHashMap (java.util.LinkedHashMap)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)11 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)11 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)10 TRelationshipTemplate (org.eclipse.winery.model.tosca.TRelationshipTemplate)10 ArrayList (java.util.ArrayList)9 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)9 TEntityTemplate (org.eclipse.winery.model.tosca.TEntityTemplate)9