Search in sources :

Example 6 with TExtensibleElements

use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.

the class TOSCAGraphIsomorphismTest method testServiceTemplateComplianceRuleChecker.

@Test
public void testServiceTemplateComplianceRuleChecker() throws IOException {
    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
    TComplianceRule ruleOne = createTComplianceRule(crId1);
    ruleOne.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleOneIdentifier), newArrayList()));
    ruleOne.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleOneRequiredStructure), newArrayList()));
    allEntities.put(crId1, ruleOne);
    // unsatisfied
    TComplianceRule ruleTwo = createTComplianceRule(crId2);
    ruleTwo.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleTwoIdentifier), newArrayList()));
    ruleTwo.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleTwoRequiredStructure), newArrayList()));
    allEntities.put(crId2, ruleTwo);
    // invalid rule
    TComplianceRule 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.common.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.common.ids.definitions.ComplianceRuleId) StringWriter(java.io.StringWriter) TComplianceRule(org.eclipse.winery.model.tosca.TComplianceRule) TOSCAModelHelper.createTComplianceRule(org.eclipse.winery.compliance.TOSCAModelHelper.createTComplianceRule) TOSCAModelHelper.createNodeTypeId(org.eclipse.winery.compliance.TOSCAModelHelper.createNodeTypeId) NodeTypeId(org.eclipse.winery.common.ids.definitions.NodeTypeId) TOSCAModelHelper.createTNodeTemplate(org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeTemplate) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) TOSCAModelHelper.createTServiceTemplate(org.eclipse.winery.compliance.TOSCAModelHelper.createTServiceTemplate) Test(org.junit.Test)

Example 7 with TExtensibleElements

use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.

the class WineryRepositoryClient method getDefinitions.

@Override
public Definitions getDefinitions(DefinitionsChildId id) {
    for (WebResource wr : this.repositoryResources) {
        String path = Util.getUrlPath(id);
        Definitions definitions = WineryRepositoryClient.getDefinitions(wr.path(path));
        if (definitions == null) {
            // in case of an error, just try the next one
            continue;
        }
        TExtensibleElements element = definitions.getElement();
        if (element instanceof TEntityType) {
            this.cache((TEntityType) element, id.getQName());
            return definitions;
        }
    }
    return new Definitions();
}
Also used : TEntityType(org.eclipse.winery.model.tosca.TEntityType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) Definitions(org.eclipse.winery.model.tosca.Definitions) WebResource(com.sun.jersey.api.client.WebResource) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements)

Example 8 with TExtensibleElements

use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.

the class AbstractComponentsWithTypeReferenceResource method onJsonPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
    // only check for type parameter as namespace and name are checked in super.onPost
    if (StringUtils.isEmpty(jsonData.type)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    ResourceCreationResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
    if (!creationResult.isSuccess()) {
        return creationResult.getResponse();
    }
    if (creationResult.getStatus().equals(Status.CREATED)) {
        final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
        final IRepository repository = RepositoryFactory.getRepository();
        final Definitions definitions = repository.getDefinitions(id);
        final TExtensibleElements element = definitions.getElement();
        ((HasType) element).setType(jsonData.type);
        if (id instanceof EntityTemplateId) {
            BackendUtils.initializeProperties(repository, (TEntityTemplate) element);
        }
        try {
            BackendUtils.persist(id, definitions);
        } catch (IOException e) {
            throw new WebApplicationException(e);
        }
    }
    return creationResult.getResponse();
}
Also used : EntityTemplateId(org.eclipse.winery.common.ids.definitions.EntityTemplateId) WebApplicationException(javax.ws.rs.WebApplicationException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) Definitions(org.eclipse.winery.model.tosca.Definitions) HasType(org.eclipse.winery.model.tosca.HasType) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) IOException(java.io.IOException) IRepository(org.eclipse.winery.repository.backend.IRepository) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

TExtensibleElements (org.eclipse.winery.model.tosca.TExtensibleElements)8 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)5 HashMap (java.util.HashMap)4 NodeTypeId (org.eclipse.winery.common.ids.definitions.NodeTypeId)4 TOSCAModelHelper.createNodeTypeId (org.eclipse.winery.compliance.TOSCAModelHelper.createNodeTypeId)4 TOSCAModelHelper.createTNodeTemplate (org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeTemplate)4 TOSCAModelHelper.createTNodeType (org.eclipse.winery.compliance.TOSCAModelHelper.createTNodeType)4 TNodeTemplate (org.eclipse.winery.model.tosca.TNodeTemplate)4 TNodeType (org.eclipse.winery.model.tosca.TNodeType)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 JAXBException (javax.xml.bind.JAXBException)3 QName (javax.xml.namespace.QName)3 TOSCAModelHelper.createTComplianceRule (org.eclipse.winery.compliance.TOSCAModelHelper.createTComplianceRule)3 Definitions (org.eclipse.winery.model.tosca.Definitions)3 TComplianceRule (org.eclipse.winery.model.tosca.TComplianceRule)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 JAXBContext (javax.xml.bind.JAXBContext)2