Search in sources :

Example 51 with EdgeId

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

the class EdgeController method assignEdgeToCustomer.

@ApiOperation(value = "Assign edge to customer (assignEdgeToCustomer)", notes = "Creates assignment of the edge to customer. Customer will be able to query edge afterwards." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/{customerId}/edge/{edgeId}", method = RequestMethod.POST)
@ResponseBody
public Edge assignEdgeToCustomer(@ApiParam(value = CUSTOMER_ID_PARAM_DESCRIPTION, required = true) @PathVariable("customerId") String strCustomerId, @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable(EDGE_ID) String strEdgeId) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    checkParameter(EDGE_ID, strEdgeId);
    try {
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        Customer customer = checkCustomerId(customerId, Operation.READ);
        EdgeId edgeId = new EdgeId(toUUID(strEdgeId));
        checkEdgeId(edgeId, Operation.ASSIGN_TO_CUSTOMER);
        Edge savedEdge = checkNotNull(edgeService.assignEdgeToCustomer(getCurrentUser().getTenantId(), edgeId, customerId));
        tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, ComponentLifecycleEvent.UPDATED);
        logEntityAction(edgeId, savedEdge, savedEdge.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strEdgeId, strCustomerId, customer.getName());
        sendEntityAssignToCustomerNotificationMsg(savedEdge.getTenantId(), savedEdge.getId(), customerId, EdgeEventActionType.ASSIGNED_TO_CUSTOMER);
        return savedEdge;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.EDGE), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strEdgeId, strCustomerId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) EdgeId(org.thingsboard.server.common.data.id.EdgeId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) 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 52 with EdgeId

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

the class EntityViewController method assignEntityViewToEdge.

@ApiOperation(value = "Assign entity view to edge (assignEntityViewToEdge)", notes = "Creates assignment of an existing entity view to an instance of The Edge. " + EDGE_ASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive a copy of assignment entity view " + EDGE_ASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once entity view 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}/entityView/{entityViewId}", method = RequestMethod.POST)
@ResponseBody
public EntityView assignEntityViewToEdge(@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));
        checkEntityViewId(entityViewId, Operation.READ);
        EntityView savedEntityView = checkNotNull(entityViewService.assignEntityViewToEdge(getTenantId(), entityViewId, edgeId));
        logEntityAction(entityViewId, savedEntityView, savedEntityView.getCustomerId(), ActionType.ASSIGNED_TO_EDGE, null, strEntityViewId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedEntityView.getId(), EdgeEventActionType.ASSIGNED_TO_EDGE);
        return savedEntityView;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ENTITY_VIEW), null, null, ActionType.ASSIGNED_TO_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 53 with EdgeId

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

the class AssetController method unassignAssetFromEdge.

@ApiOperation(value = "Unassign asset from edge (unassignAssetFromEdge)", notes = "Clears assignment of the asset to the edge. " + EDGE_UNASSIGN_ASYNC_FIRST_STEP_DESCRIPTION + "Second, remote edge service will receive an 'unassign' command to remove asset " + EDGE_UNASSIGN_RECEIVE_STEP_DESCRIPTION + "Third, once 'unassign' command will be delivered to edge service, it's going to remove asset locally.", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/edge/{edgeId}/asset/{assetId}", method = RequestMethod.DELETE)
@ResponseBody
public Asset unassignAssetFromEdge(@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));
        Asset asset = checkAssetId(assetId, Operation.READ);
        Asset savedAsset = checkNotNull(assetService.unassignAssetFromEdge(getTenantId(), assetId, edgeId));
        logEntityAction(assetId, asset, asset.getCustomerId(), ActionType.UNASSIGNED_FROM_EDGE, null, strAssetId, strEdgeId, edge.getName());
        sendEntityAssignToEdgeNotificationMsg(getTenantId(), edgeId, savedAsset.getId(), EdgeEventActionType.UNASSIGNED_FROM_EDGE);
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.UNASSIGNED_FROM_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 54 with EdgeId

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

the class BaseEntityService method fetchEntityCustomerId.

@Override
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
    log.trace("Executing fetchEntityCustomerId [{}]", entityId);
    HasCustomerId hasCustomerId = null;
    switch(entityId.getEntityType()) {
        case TENANT:
        case RULE_CHAIN:
        case RULE_NODE:
        case DASHBOARD:
        case WIDGETS_BUNDLE:
        case WIDGET_TYPE:
        case TENANT_PROFILE:
        case DEVICE_PROFILE:
        case API_USAGE_STATE:
        case TB_RESOURCE:
        case OTA_PACKAGE:
            break;
        case CUSTOMER:
            hasCustomerId = () -> new CustomerId(entityId.getId());
            break;
        case USER:
            hasCustomerId = userService.findUserById(tenantId, new UserId(entityId.getId()));
            break;
        case ASSET:
            hasCustomerId = assetService.findAssetById(tenantId, new AssetId(entityId.getId()));
            break;
        case DEVICE:
            hasCustomerId = deviceService.findDeviceById(tenantId, new DeviceId(entityId.getId()));
            break;
        case ALARM:
            try {
                hasCustomerId = alarmService.findAlarmByIdAsync(tenantId, new AlarmId(entityId.getId())).get();
            } catch (Exception e) {
            }
            break;
        case ENTITY_VIEW:
            hasCustomerId = entityViewService.findEntityViewById(tenantId, new EntityViewId(entityId.getId()));
            break;
        case EDGE:
            hasCustomerId = edgeService.findEdgeById(tenantId, new EdgeId(entityId.getId()));
            break;
    }
    return hasCustomerId != null ? hasCustomerId.getCustomerId() : new CustomerId(NULL_UUID);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) UserId(org.thingsboard.server.common.data.id.UserId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EdgeId(org.thingsboard.server.common.data.id.EdgeId) HasCustomerId(org.thingsboard.server.common.data.HasCustomerId) HasCustomerId(org.thingsboard.server.common.data.HasCustomerId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException)

Example 55 with EdgeId

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

the class BaseDashboardServiceTest method testAssignDashboardToNonExistentEdge.

@Test(expected = DataValidationException.class)
public void testAssignDashboardToNonExistentEdge() {
    Dashboard dashboard = new Dashboard();
    dashboard.setTitle("My dashboard");
    dashboard.setTenantId(tenantId);
    dashboard = dashboardService.saveDashboard(dashboard);
    try {
        dashboardService.assignDashboardToEdge(tenantId, dashboard.getId(), new EdgeId(Uuids.timeBased()));
    } finally {
        dashboardService.deleteDashboard(tenantId, dashboard.getId());
    }
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) Dashboard(org.thingsboard.server.common.data.Dashboard) Test(org.junit.Test)

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