Search in sources :

Example 66 with Edge

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

the class DeviceController method unassignDeviceFromEdge.

@ApiOperation(value = "Unassign device from edge (unassignDeviceFromEdge)", notes = "Clears assignment of the device to the edge. " + EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive an 'unassign' command to remove device " + EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once 'unassign' command will be delivered to edge service, it's going to remove device locally." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/device/{deviceId}", method = RequestMethod.DELETE)
@ResponseBody
public Device unassignDeviceFromEdge(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION) @PathVariable(EDGE_ID) String strEdgeId, @ApiParam(value = DEVICE_ID_PARAM_DESCRIPTION) @PathVariable(DEVICE_ID) String strDeviceId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    checkParameter(DEVICE_ID, strDeviceId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.READ);
        DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
        Device device = checkDeviceId(deviceId, Operation.READ);
        Device savedDevice = checkNotNull(deviceService.unassignDeviceFromEdge(getCurrentUser().getTenantId(), deviceId, edgeId));
        logEntityAction(deviceId, device, device.getCustomerId(), ActionType.UNASSIGNED_FROM_EDGE, null, strDeviceId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedDevice.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE);
        return savedDevice;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DEVICE), null, null, ActionType.UNASSIGNED_FROM_EDGE, e, strDeviceId, strEdgeId);
        throw handleException(e);
    }
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Edge(org.thingsboard.server.common.data.edge.Edge) IOException(java.io.IOException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) 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 67 with Edge

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

the class AssetController method assignAssetToEdge.

@ApiOperation(value = "Assign asset to edge (assignAssetToEdge)", notes = "Creates assignment of an existing asset to an instance of The Edge. " + EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive a copy of assignment asset " + EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once asset will be delivered to edge service, it's going to be available for usage on remote edge instance.", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToEdge(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION) @PathVariable(EDGE_ID) String strEdgeId, @ApiParam(value = ASSET_ID_PARAM_DESCRIPTION) @PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    checkParameter(ASSET_ID, strAssetId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.READ);
        AssetId assetId = new AssetId(toUUID(strAssetId));
        checkAssetId(assetId, Operation.READ);
        Asset savedAsset = checkNotNull(assetService.assignAssetToEdge(getTenantId(), assetId, edgeId));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_EDGE, null, strAssetId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedAsset.getId(), EdgeEventActionType.ASSIGNED_TO_EDGE);
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_EDGE, e, strAssetId, strEdgeId);
        throw handleException(e);
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) Asset(org.thingsboard.server.common.data.asset.Asset) Edge(org.thingsboard.server.common.data.edge.Edge) AssetId(org.thingsboard.server.common.data.id.AssetId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) 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 68 with Edge

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

the class DashboardController method unassignDashboardFromEdge.

@ApiOperation(value = "Unassign dashboard from edge (unassignDashboardFromEdge)", notes = "Clears assignment of the dashboard to the edge. " + EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive an 'unassign' command to remove dashboard " + EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once 'unassign' command will be delivered to edge service, it's going to remove dashboard locally." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/dashboard/{dashboardId}", method = RequestMethod.DELETE)
@ResponseBody
public Dashboard unassignDashboardFromEdge(@PathVariable("edgeId") String strEdgeId, @PathVariable(DASHBOARD_ID) String strDashboardId) throws ThingsboardException {
    checkParameter("edgeId", strEdgeId);
    checkParameter(DASHBOARD_ID, strDashboardId);
    try {
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        Edge edge = checkEdgeId(edgeId, Operation.READ);
        DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
        Dashboard dashboard = checkDashboardId(dashboardId, Operation.READ);
        Dashboard savedDashboard = checkNotNull(dashboardService.unassignDashboardFromEdge(getCurrentUser().getTenantId(), dashboardId, edgeId));
        logEntityAction(dashboardId, dashboard, null, ActionType.UNASSIGNED_FROM_EDGE, null, strDashboardId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedDashboard.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE);
        return savedDashboard;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.DASHBOARD), null, null, ActionType.UNASSIGNED_FROM_EDGE, e, strDashboardId, strEdgeId);
        throw handleException(e);
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) Dashboard(org.thingsboard.server.common.data.Dashboard) HomeDashboard(org.thingsboard.server.common.data.HomeDashboard) DashboardId(org.thingsboard.server.common.data.id.DashboardId) 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 69 with Edge

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

the class AssetController method getEdgeAssets.

@ApiOperation(value = "Get assets assigned to edge (getEdgeAssets)", notes = "Returns a page of assets assigned to edge. " + PAGE_DATA_PARAMETERS, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/edge/{edgeId}/assets", params = { "pageSize", "page" }, method = RequestMethod.GET)
@ResponseBody
public PageData<Asset> getEdgeAssets(@ApiParam(value = EDGE_ID_PARAM_DESCRIPTION) @PathVariable(EDGE_ID) String strEdgeId, @ApiParam(value = PAGE_SIZE_DESCRIPTION) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION) @RequestParam int page, @ApiParam(value = ASSET_TYPE_DESCRIPTION) @RequestParam(required = false) String type, @ApiParam(value = ASSET_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = ASSET_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder, @ApiParam(value = "Timestamp. Assets with creation time before it won't be queried") @RequestParam(required = false) Long startTime, @ApiParam(value = "Timestamp. Assets with creation time after it won't be queried") @RequestParam(required = false) Long endTime) throws ThingsboardException {
    checkParameter(EDGE_ID, strEdgeId);
    try {
        TenantId tenantId = getCurrentUser().getTenantId();
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        checkEdgeId(edgeId, Operation.READ);
        TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime);
        PageData<Asset> nonFilteredResult;
        if (type != null && type.trim().length() > 0) {
            nonFilteredResult = assetService.findAssetsByTenantIdAndEdgeIdAndType(tenantId, edgeId, type, pageLink);
        } else {
            nonFilteredResult = assetService.findAssetsByTenantIdAndEdgeId(tenantId, edgeId, pageLink);
        }
        List<Asset> filteredAssets = nonFilteredResult.getData().stream().filter(asset -> {
            try {
                accessControlService.checkPermission(getCurrentUser(), Resource.ASSET, Operation.READ, asset.getId(), asset);
                return true;
            } catch (ThingsboardException e) {
                return false;
            }
        }).collect(Collectors.toList());
        PageData<Asset> filteredResult = new PageData<>(filteredAssets, nonFilteredResult.getTotalPages(), nonFilteredResult.getTotalElements(), nonFilteredResult.hasNext());
        return checkNotNull(filteredResult);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH(org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Edge(org.thingsboard.server.common.data.edge.Edge) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Customer(org.thingsboard.server.common.data.Customer) SORT_ORDER_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES) ApiParam(io.swagger.annotations.ApiParam) CUSTOMER_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID_PARAM_DESCRIPTION) TenantId(org.thingsboard.server.common.data.id.TenantId) ApiOperation(io.swagger.annotations.ApiOperation) EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION) EntityType(org.thingsboard.server.common.data.EntityType) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PostMapping(org.springframework.web.bind.annotation.PostMapping) PageLink(org.thingsboard.server.common.data.page.PageLink) TENANT_AUTHORITY_PARAGRAPH(org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH) TB_SERVICE_QUEUE(org.thingsboard.server.dao.asset.BaseAssetService.TB_SERVICE_QUEUE) MediaType(org.springframework.http.MediaType) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) PAGE_DATA_PARAMETERS(org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS) UUID_WIKI_LINK(org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LINK) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BulkImportResult(org.thingsboard.server.service.importing.BulkImportResult) ASSET_SORT_PROPERTY_ALLOWABLE_VALUES(org.thingsboard.server.controller.ControllerConstants.ASSET_SORT_PROPERTY_ALLOWABLE_VALUES) ASSET_TEXT_SEARCH_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ASSET_TEXT_SEARCH_DESCRIPTION) EDGE_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_ID_PARAM_DESCRIPTION) Operation(org.thingsboard.server.service.security.permission.Operation) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) SORT_ORDER_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION) PAGE_NUMBER_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) SORT_PROPERTY_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION) ASSET_TYPE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ASSET_TYPE_DESCRIPTION) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AssetId(org.thingsboard.server.common.data.id.AssetId) AssetBulkImportService(org.thingsboard.server.service.asset.AssetBulkImportService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ASSET_ID_PARAM_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ASSET_ID_PARAM_DESCRIPTION) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) ActionType(org.thingsboard.server.common.data.audit.ActionType) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) PAGE_SIZE_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION) EDGE_ID(org.thingsboard.server.controller.EdgeController.EDGE_ID) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) BulkImportRequest(org.thingsboard.server.service.importing.BulkImportRequest) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AssetSearchQuery(org.thingsboard.server.common.data.asset.AssetSearchQuery) HttpStatus(org.springframework.http.HttpStatus) ASSET_INFO_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ASSET_INFO_DESCRIPTION) EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION) EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION) AssetInfo(org.thingsboard.server.common.data.asset.AssetInfo) PageData(org.thingsboard.server.common.data.page.PageData) ASSET_NAME_DESCRIPTION(org.thingsboard.server.controller.ControllerConstants.ASSET_NAME_DESCRIPTION) ThingsboardErrorCode(org.thingsboard.server.common.data.exception.ThingsboardErrorCode) Resource(org.thingsboard.server.service.security.permission.Resource) Asset(org.thingsboard.server.common.data.asset.Asset) TenantId(org.thingsboard.server.common.data.id.TenantId) PageData(org.thingsboard.server.common.data.page.PageData) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) Asset(org.thingsboard.server.common.data.asset.Asset) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) 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 70 with Edge

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

the class EdgeController method deleteEdge.

@ApiOperation(value = "Delete edge (deleteEdge)", notes = "Deletes the edge. Referencing non-existing edge Id will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteEdge(@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.DELETE);
        edgeService.deleteEdge(getTenantId(), edgeId);
        tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, ComponentLifecycleEvent.DELETED);
        logEntityAction(edgeId, edge, null, ActionType.DELETED, null, strEdgeId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.DELETED, e, strEdgeId);
        throw handleException(e);
    }
}
Also used : 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) 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)

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