Search in sources :

Example 31 with EdgeId

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

the class EntityViewController method unassignEntityViewFromEdge.

@ApiOperation(value = "Unassign entity view from edge (unassignEntityViewFromEdge)", notes = "Clears assignment of the entity view to the edge. " + EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive an 'unassign' command to remove entity view " + EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once 'unassign' command will be delivered to edge service, it's going to remove entity view locally.", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/entityView/{entityViewId}", method = RequestMethod.DELETE)
@ResponseBody
public EntityView unassignEntityViewFromEdge(@PathVariable(EDGE_ID) String strEdgeId, @PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    checkParameter(ENTITY_VIEW_ID, strEntityViewId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.READ);
        EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
        EntityView entityView = checkEntityViewId(entityViewId, Operation.READ);
        EntityView savedEntityView = checkNotNull(entityViewService.unassignEntityViewFromEdge(getTenantId(), entityViewId, edgeId));
        logEntityAction(entityViewId, entityView, entityView.getCustomerId(), ActionType.UNASSIGNED_FROM_EDGE, null, strEntityViewId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedEntityView.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE);
        return savedEntityView;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), null, null, ActionType.UNASSIGNED_FROM_EDGE, e, strEntityViewId, strEdgeId);
        throw handleException(e);
    }
}
Also used : EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) EntityView(org.thingsboard.server.common.data.EntityView) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) 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 32 with EdgeId

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

the class EntityViewController method deleteEntityView.

@ApiOperation(value = "Delete entity view (deleteEntityView)", notes = "Delete the EntityView object based on the provided entity view id. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/entityView/{entityViewId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteEntityView(@ApiParam(value = ENTITY_VIEW_ID_PARAM_DESCRIPTION) @PathVariable(ENTITY_VIEW_ID) String strEntityViewId) throws ThingsboardException {
    checkParameter(ENTITY_VIEW_ID, strEntityViewId);
    try {
        EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
        EntityView entityView = checkEntityViewId(entityViewId, Operation.DELETE);
        List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), entityViewId);
        entityViewService.deleteEntityView(getTenantId(), entityViewId);
        logEntityAction(entityViewId, entityView, entityView.getCustomerId(), ActionType.DELETED, null, strEntityViewId);
        sendDeleteNotificationMsg(getTenantId(), entityViewId, relatedEdgeIds);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), null, null, ActionType.DELETED, e, strEntityViewId);
        throw handleException(e);
    }
}
Also used : EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) EntityView(org.thingsboard.server.common.data.EntityView) EdgeId(org.thingsboard.server.common.data.id.EdgeId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) 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 33 with EdgeId

use of org.thingsboard.server.common.data.id.EdgeId 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 EdgeId

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

the class TenantActor method onComponentLifecycleMsg.

private void onComponentLifecycleMsg(ComponentLifecycleMsg msg) {
    if (msg.getEntityId().getEntityType().equals(EntityType.API_USAGE_STATE)) {
        ApiUsageState old = getApiUsageState();
        apiUsageState = new ApiUsageState(systemContext.getApiUsageStateService().getApiUsageState(tenantId));
        if (old.isReExecEnabled() && !apiUsageState.isReExecEnabled()) {
            log.info("[{}] Received API state update. Going to DISABLE Rule Engine execution.", tenantId);
            destroyRuleChains();
        } else if (!old.isReExecEnabled() && apiUsageState.isReExecEnabled()) {
            log.info("[{}] Received API state update. Going to ENABLE Rule Engine execution.", tenantId);
            initRuleChains();
        }
    } else if (msg.getEntityId().getEntityType() == EntityType.EDGE) {
        EdgeId edgeId = new EdgeId(msg.getEntityId().getId());
        EdgeRpcService edgeRpcService = systemContext.getEdgeRpcService();
        if (msg.getEvent() == ComponentLifecycleEvent.DELETED) {
            edgeRpcService.deleteEdge(tenantId, edgeId);
        } else {
            Edge edge = systemContext.getEdgeService().findEdgeById(tenantId, edgeId);
            if (msg.getEvent() == ComponentLifecycleEvent.UPDATED) {
                edgeRpcService.updateEdge(tenantId, edge);
            }
        }
    } else if (isRuleEngine) {
        TbActorRef target = getEntityActorRef(msg.getEntityId());
        if (target != null) {
            if (msg.getEntityId().getEntityType() == EntityType.RULE_CHAIN) {
                RuleChain ruleChain = systemContext.getRuleChainService().findRuleChainById(tenantId, new RuleChainId(msg.getEntityId().getId()));
                if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
                    visit(ruleChain, target);
                }
            }
            target.tellWithHighPriority(msg);
        } else {
            log.debug("[{}] Invalid component lifecycle msg: {}", tenantId, msg);
        }
    }
}
Also used : ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) EdgeId(org.thingsboard.server.common.data.id.EdgeId) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EdgeRpcService(org.thingsboard.server.service.edge.rpc.EdgeRpcService) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 35 with EdgeId

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

the class CustomerEdgeProcessor method processCustomerNotification.

public void processCustomerNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
    UUID uuid = new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB());
    CustomerId customerId = new CustomerId(EntityIdFactory.getByEdgeEventTypeAndUuid(type, uuid).getId());
    switch(actionType) {
        case UPDATED:
            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
            PageData<Edge> pageData;
            do {
                pageData = edgeService.findEdgesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                    for (Edge edge : pageData.getData()) {
                        saveEdgeEvent(tenantId, edge.getId(), type, actionType, customerId, null);
                    }
                    if (pageData.hasNext()) {
                        pageLink = pageLink.nextPageLink();
                    }
                }
            } while (pageData != null && pageData.hasNext());
            break;
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            saveEdgeEvent(tenantId, edgeId, type, actionType, customerId, null);
            break;
    }
}
Also used : EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID) Edge(org.thingsboard.server.common.data.edge.Edge)

Aggregations

EdgeId (org.thingsboard.server.common.data.id.EdgeId)57 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)33 ApiOperation (io.swagger.annotations.ApiOperation)32 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)32 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)32 Edge (org.thingsboard.server.common.data.edge.Edge)29 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)23 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)14 PageLink (org.thingsboard.server.common.data.page.PageLink)14 IOException (java.io.IOException)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)12 DeviceId (org.thingsboard.server.common.data.id.DeviceId)10 ArrayList (java.util.ArrayList)9 Customer (org.thingsboard.server.common.data.Customer)9 EdgeEventActionType (org.thingsboard.server.common.data.edge.EdgeEventActionType)9 List (java.util.List)8 UUID (java.util.UUID)7 Device (org.thingsboard.server.common.data.Device)7