Search in sources :

Example 16 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeController method unassignEdgeFromCustomer.

@ApiOperation(value = "Unassign edge from customer (unassignEdgeFromCustomer)", notes = "Clears assignment of the edge to customer. Customer will not be able to query edge afterwards." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/edge/{edgeId}", method = RequestMethod.DELETE)
@ResponseBody
public Edge unassignEdgeFromCustomer(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable(EDGE_ID) String strEdgeId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.UNASSIGN_FROM_CUSTOMER);
        if (edge.getCustomerId() == null || edge.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            throw new IncorrectParameterException("Edge isn't assigned to any customer!");
        }
        Customer customer = checkCustomerId(edge.getCustomerId(), Operation.READ);
        Edge savedEdge = checkNotNull(edgeService.unassignEdgeFromCustomer(getCurrentUser().getTenantId(), edgeId));
        tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, ComponentLifecycleEvent.UPDATED);
        logEntityAction(edgeId, edge, edge.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, strEdgeId, customer.getId().toString(), customer.getName());
        sendEntityAssignToCustomerNotificationMsg(savedEdge.getTenantId(), savedEdge.getId(), customer.getId(), EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER);
        return savedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.UNASSIGNED_FROM_CUSTOMER, e, strEdgeId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) EdgeId(org.thingsboard.server.common.data.id.EdgeId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) 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 17 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeController method setEdgeRootRuleChain.

@ApiOperation(value = "Set root rule chain for provided edge (setEdgeRootRuleChain)", notes = "Change root rule chain of the edge to the new provided rule chain. \n" + "This operation will send a notification to update root rule chain on remote edge service." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/{ruleChainId}/root", method = RequestMethod.POST)
@ResponseBody
public Edge setEdgeRootRuleChain(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable(EDGE_ID) String strEdgeId, @ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION, required = true) @PathVariable("ruleChainId") String strRuleChainId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    checkParameter("ruleChainId", strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        checkRuleChain(ruleChainId, Operation.WRITE);
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.WRITE);
        accessControlService.checkPermission(getCurrentUser(), Resource.EDGE, Operation.WRITE, edge.getId(), edge);
        Edge updatedEdge = edgeNotificationService.setEdgeRootRuleChain(getTenantId(), edge, ruleChainId);
        tbClusterService.broadcastEntityStateChangeEvent(updatedEdge.getTenantId(), updatedEdge.getId(), ComponentLifecycleEvent.UPDATED);
        logEntityAction(updatedEdge.getId(), updatedEdge, null, ActionType.UPDATED, null);
        return updatedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.UPDATED, e, strEdgeId);
        throw handleException(e);
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) 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 18 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeController method assignEdgeToPublicCustomer.

@ApiOperation(value = "Make edge publicly available (assignEdgeToPublicCustomer)", notes = "Edge will be available for non-authorized (not logged-in) users. " + "This is useful to create dashboards that you plan to share/embed on a publicly available website. " + "However, users that are logged-in and belong to different tenant will not be able to access the edge." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/edge/{edgeId}", method = RequestMethod.POST)
@ResponseBody
public Edge assignEdgeToPublicCustomer(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable(EDGE_ID) String strEdgeId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.ASSIGN_TO_CUSTOMER);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(edge.getTenantId());
        Edge savedEdge = checkNotNull(edgeService.assignEdgeToCustomer(getCurrentUser().getTenantId(), edgeId, publicCustomer.getId()));
        tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, ComponentLifecycleEvent.UPDATED);
        logEntityAction(edgeId, savedEdge, savedEdge.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strEdgeId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strEdgeId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) 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 19 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class RuleChainController method unassignRuleChainFromEdge.

@ApiOperation(value = "Unassign rule chain from edge (unassignRuleChainFromEdge)", notes = "Clears assignment of the rule chain to the edge. " + EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive an 'unassign' command to remove rule chain " + EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once 'unassign' command will be delivered to edge service, it's going to remove rule chain locally." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
@ResponseBody
public RuleChain unassignRuleChainFromEdge(@PathVariable("edgeId") String strEdgeId, @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter("edgeId", strEdgeId);
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.WRITE);
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.READ);
        RuleChain savedRuleChain = checkNotNull(ruleChainService.unassignRuleChainFromEdge(getCurrentUser().getTenantId(), ruleChainId, edgeId, false));
        logEntityAction(ruleChainId, ruleChain, null, ActionType.UNASSIGNED_FROM_EDGE, null, strRuleChainId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedRuleChain.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE);
        return savedRuleChain;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UNASSIGNED_FROM_EDGE, e, strRuleChainId, strEdgeId);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Edge(org.thingsboard.server.common.data.edge.Edge) 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 20 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class RuleChainController method assignRuleChainToEdge.

@ApiOperation(value = "Assign rule chain to edge (assignRuleChainToEdge)", notes = "Creates assignment of an existing rule chain to an instance of The Edge. " + EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive a copy of assignment rule chain " + EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once rule chain will be delivered to edge service, it's going to start processing messages locally. " + "\n\nOnly rule chain with type 'EDGE' can be assigned to edge." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/ruleChain/{ruleChainId}", method = RequestMethod.POST)
@ResponseBody
public RuleChain assignRuleChainToEdge(@PathVariable("edgeId") String strEdgeId, @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter("edgeId", strEdgeId);
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.WRITE);
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        checkRuleChain(ruleChainId, Operation.READ);
        RuleChain savedRuleChain = checkNotNull(ruleChainService.assignRuleChainToEdge(getCurrentUser().getTenantId(), ruleChainId, edgeId));
        logEntityAction(ruleChainId, savedRuleChain, null, ActionType.ASSIGNED_TO_EDGE, null, strRuleChainId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedRuleChain.getId(), EdgeEventActionType.ASSIGNED_TO_EDGE);
        return savedRuleChain;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.ASSIGNED_TO_EDGE, e, strRuleChainId, strEdgeId);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Edge(org.thingsboard.server.common.data.edge.Edge) 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

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13