Search in sources :

Example 31 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method newChainCanBeStarted.

@Test
public void newChainCanBeStarted() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset));
    node.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals(customerId, originatorCaptor.getValue());
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 32 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainController method setEdgeTemplateRootRuleChain.

@ApiOperation(value = "Set Edge Template Root Rule Chain (setEdgeTemplateRootRuleChain)", notes = "Makes the rule chain to be root rule chain for any new edge that will be created. " + "Does not update root rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}/edgeTemplateRoot", method = RequestMethod.POST)
@ResponseBody
public RuleChain setEdgeTemplateRootRuleChain(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.WRITE);
        ruleChainService.setEdgeTemplateRootRuleChain(getTenantId(), ruleChainId);
        return ruleChain;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, strRuleChainId);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 33 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainController method deleteRuleChain.

@ApiOperation(value = "Delete rule chain (deleteRuleChain)", notes = "Deletes the rule chain. Referencing non-existing rule chain Id will cause an error. " + "Referencing rule chain that is used in the device profiles will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteRuleChain(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.DELETE);
        List<RuleNode> referencingRuleNodes = ruleChainService.getReferencingRuleChainNodes(getTenantId(), ruleChainId);
        Set<RuleChainId> referencingRuleChainIds = referencingRuleNodes.stream().map(RuleNode::getRuleChainId).collect(Collectors.toSet());
        List<EdgeId> relatedEdgeIds = null;
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            relatedEdgeIds = findRelatedEdgeIds(getTenantId(), ruleChainId);
        }
        ruleChainService.deleteRuleChainById(getTenantId(), ruleChainId);
        referencingRuleChainIds.remove(ruleChain.getId());
        if (RuleChainType.CORE.equals(ruleChain.getType())) {
            referencingRuleChainIds.forEach(referencingRuleChainId -> tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), referencingRuleChainId, ComponentLifecycleEvent.UPDATED));
            tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.DELETED);
        }
        logEntityAction(ruleChainId, ruleChain, null, ActionType.DELETED, null, strRuleChainId);
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            sendDeleteNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), relatedEdgeIds);
        }
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.DELETED, e, strRuleChainId);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainController method getRuleChainOutputLabelsUsage.

@ApiOperation(value = "Get output labels usage (getRuleChainOutputLabelsUsage)", notes = "Fetch the list of rule chains and the relation types (labels) they use to process output of the current rule chain based on the provided Rule Chain Id. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}/output/labels/usage", method = RequestMethod.GET)
@ResponseBody
public List<RuleChainOutputLabelsUsage> getRuleChainOutputLabelsUsage(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        checkRuleChain(ruleChainId, Operation.READ);
        return tbRuleChainService.getOutputLabelUsage(getCurrentUser().getTenantId(), ruleChainId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 35 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainController method setAutoAssignToEdgeRuleChain.

@ApiOperation(value = "Set Auto Assign To Edge Rule Chain (setAutoAssignToEdgeRuleChain)", notes = "Makes the rule chain to be automatically assigned for any new edge that will be created. " + "Does not assign this rule chain for already created edges. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}/autoAssignToEdge", method = RequestMethod.POST)
@ResponseBody
public RuleChain setAutoAssignToEdgeRuleChain(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.WRITE);
        ruleChainService.setAutoAssignToEdgeRuleChain(getTenantId(), ruleChainId);
        return ruleChain;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, strRuleChainId);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)60 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)22 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)20 Test (org.junit.Test)17 TbMsg (org.thingsboard.server.common.msg.TbMsg)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)12 EntityId (org.thingsboard.server.common.data.id.EntityId)12 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 TenantId (org.thingsboard.server.common.data.id.TenantId)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 IOException (java.io.IOException)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6