Search in sources :

Example 11 with Operation

use of org.thingsboard.server.service.security.permission.Operation in project thingsboard by thingsboard.

the class EdgeController method saveEdge.

@ApiOperation(value = "Create Or Update Edge (saveEdge)", notes = "Create or update the Edge. When creating edge, platform generates Edge Id as " + UUID_WIKI_LINK + "The newly created edge id will be present in the response. " + "Specify existing Edge id to update the edge. " + "Referencing non-existing Edge Id will cause 'Not Found' error." + "\n\nEdge name is unique in the scope of tenant. Use unique identifiers like MAC or IMEI for the edge names and non-unique 'label' field for user-friendly visualization purposes." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge", method = RequestMethod.POST)
@ResponseBody
public Edge saveEdge(@ApiParam(value = "A JSON value representing the edge.", required = true) @RequestBody Edge edge) throws ThingsboardException {
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        edge.setTenantId(tenantId);
        boolean created = edge.getId() == null;
        RuleChain edgeTemplateRootRuleChain = null;
        if (created) {
            edgeTemplateRootRuleChain = ruleChainService.getEdgeTemplateRootRuleChain(tenantId);
            if (edgeTemplateRootRuleChain == null) {
                throw new DataValidationException("Root edge rule chain is not available!");
            }
        }
        Operation operation = created ? Operation.CREATE : Operation.WRITE;
        accessControlService.checkPermission(getCurrentUser(), Resource.EDGE, operation, edge.getId(), edge);
        Edge savedEdge = checkNotNull(edgeService.saveEdge(edge));
        onEdgeCreatedOrUpdated(tenantId, savedEdge, edgeTemplateRootRuleChain, !created, getCurrentUser());
        return savedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), edge, null, edge.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ApiOperation(io.swagger.annotations.ApiOperation) Operation(org.thingsboard.server.service.security.permission.Operation) 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)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 Function (com.google.common.base.Function)10 FutureCallback (com.google.common.util.concurrent.FutureCallback)10 Futures (com.google.common.util.concurrent.Futures)10 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ExecutorService (java.util.concurrent.ExecutorService)10 Executors (java.util.concurrent.Executors)10 BiConsumer (java.util.function.BiConsumer)10 Nullable (javax.annotation.Nullable)10 PostConstruct (javax.annotation.PostConstruct)10 PreDestroy (javax.annotation.PreDestroy)10 Autowired (org.springframework.beans.factory.annotation.Autowired)10 HttpStatus (org.springframework.http.HttpStatus)10 ResponseEntity (org.springframework.http.ResponseEntity)10 Component (org.springframework.stereotype.Component)10 DeferredResult (org.springframework.web.context.request.async.DeferredResult)10 ThingsBoardThreadFactory (org.thingsboard.common.util.ThingsBoardThreadFactory)10 ApiUsageState (org.thingsboard.server.common.data.ApiUsageState)10 Customer (org.thingsboard.server.common.data.Customer)10