Search in sources :

Example 31 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class AccessValidator method validateEntityView.

private void validateEntityView(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
    if (currentUser.isSystemAdmin()) {
        callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION));
    } else {
        ListenableFuture<EntityView> entityViewFuture = entityViewService.findEntityViewByIdAsync(currentUser.getTenantId(), new EntityViewId(entityId.getId()));
        Futures.addCallback(entityViewFuture, getCallback(callback, entityView -> {
            if (entityView == null) {
                return ValidationResult.entityNotFound(ENTITY_VIEW_WITH_REQUESTED_ID_NOT_FOUND);
            } else {
                try {
                    accessControlService.checkPermission(currentUser, Resource.ENTITY_VIEW, operation, entityId, entityView);
                } catch (ThingsboardException e) {
                    return ValidationResult.accessDenied(e.getMessage());
                }
                return ValidationResult.ok(entityView);
            }
        }), executor);
    }
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) HttpValidationCallback(org.thingsboard.server.controller.HttpValidationCallback) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Rpc(org.thingsboard.server.common.data.rpc.Rpc) ResourceService(org.thingsboard.server.dao.resource.ResourceService) RpcService(org.thingsboard.server.dao.rpc.RpcService) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) AssetService(org.thingsboard.server.dao.asset.AssetService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) EdgeService(org.thingsboard.server.dao.edge.EdgeService) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) Executors(java.util.concurrent.Executors) EntityView(org.thingsboard.server.common.data.EntityView) PostConstruct(javax.annotation.PostConstruct) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Operation(org.thingsboard.server.service.security.permission.Operation) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) OtaPackageService(org.thingsboard.server.dao.ota.OtaPackageService) UserService(org.thingsboard.server.dao.user.UserService) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AssetId(org.thingsboard.server.common.data.id.AssetId) Tenant(org.thingsboard.server.common.data.Tenant) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) User(org.thingsboard.server.common.data.User) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) DeviceService(org.thingsboard.server.dao.device.DeviceService) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) CustomerService(org.thingsboard.server.dao.customer.CustomerService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) RpcId(org.thingsboard.server.common.data.id.RpcId) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) DeviceProfileService(org.thingsboard.server.dao.device.DeviceProfileService) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) UserId(org.thingsboard.server.common.data.id.UserId) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Resource(org.thingsboard.server.service.security.permission.Resource) ResponseEntity(org.springframework.http.ResponseEntity) Asset(org.thingsboard.server.common.data.asset.Asset) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) EntityView(org.thingsboard.server.common.data.EntityView) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException)

Example 32 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseController method checkEntityViewId.

protected EntityView checkEntityViewId(EntityViewId entityViewId, Operation operation) throws ThingsboardException {
    try {
        validateId(entityViewId, "Incorrect entityViewId " + entityViewId);
        EntityView entityView = entityViewService.findEntityViewById(getCurrentUser().getTenantId(), entityViewId);
        checkNotNull(entityView, "Entity view with id [" + entityViewId + "] is not found");
        accessControlService.checkPermission(getCurrentUser(), Resource.ENTITY_VIEW, operation, entityViewId, entityView);
        return entityView;
    } catch (Exception e) {
        throw handleException(e, false);
    }
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) MessagingException(javax.mail.MessagingException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 33 with EntityView

use of org.thingsboard.server.common.data.EntityView 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 34 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEdgeTest method testEntityView.

@Test
public void testEntityView() throws Exception {
    // 1
    edgeImitator.expectMessageAmount(1);
    Device device = findDeviceByName("Edge Device 1");
    EntityView entityView = new EntityView();
    entityView.setName("Edge EntityView 1");
    entityView.setType("test");
    entityView.setEntityId(device.getId());
    EntityView savedEntityView = doPost("/api/entityView", entityView, EntityView.class);
    doPost("/api/edge/" + edge.getUuidId() + "/entityView/" + savedEntityView.getUuidId(), EntityView.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    verifyEntityViewUpdateMsg(savedEntityView, device);
    // 2
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    EntityViewsRequestMsg.Builder entityViewsRequestBuilder = EntityViewsRequestMsg.newBuilder();
    entityViewsRequestBuilder.setEntityIdMSB(device.getUuidId().getMostSignificantBits());
    entityViewsRequestBuilder.setEntityIdLSB(device.getUuidId().getLeastSignificantBits());
    entityViewsRequestBuilder.setEntityType(device.getId().getEntityType().name());
    testAutoGeneratedCodeByProtobuf(entityViewsRequestBuilder);
    uplinkMsgBuilder.addEntityViewsRequestMsg(entityViewsRequestBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    verifyEntityViewUpdateMsg(savedEntityView, device);
    // 3
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/edge/" + edge.getUuidId() + "/entityView/" + savedEntityView.getUuidId(), EntityView.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityViewUpdateMsg);
    EntityViewUpdateMsg entityViewUpdateMsg = (EntityViewUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, entityViewUpdateMsg.getMsgType());
    Assert.assertEquals(entityViewUpdateMsg.getIdMSB(), savedEntityView.getUuidId().getMostSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getIdLSB(), savedEntityView.getUuidId().getLeastSignificantBits());
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/entityView/" + savedEntityView.getUuidId()).andExpect(status().isOk());
    Assert.assertFalse(edgeImitator.waitForMessages(1));
}
Also used : EntityViewsRequestMsg(org.thingsboard.server.gen.edge.v1.EntityViewsRequestMsg) AbstractMessage(com.google.protobuf.AbstractMessage) EntityView(org.thingsboard.server.common.data.EntityView) Device(org.thingsboard.server.common.data.Device) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) EntityViewUpdateMsg(org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 35 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testDeleteEntityView.

@Test
public void testDeleteEntityView() throws Exception {
    EntityView view = getNewSavedEntityView("Test entity view");
    Customer customer = doPost("/api/customer", getNewCustomer("My customer"), Customer.class);
    view.setCustomerId(customer.getId());
    EntityView savedView = doPost("/api/entityView", view, EntityView.class);
    doDelete("/api/entityView/" + savedView.getId().getId().toString()).andExpect(status().isOk());
    doGet("/api/entityView/" + savedView.getId().getId().toString()).andExpect(status().isNotFound());
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Aggregations

EntityView (org.thingsboard.server.common.data.EntityView)51 TelemetryEntityView (org.thingsboard.server.common.data.objects.TelemetryEntityView)21 Test (org.junit.Test)18 AttributesEntityView (org.thingsboard.server.common.data.objects.AttributesEntityView)17 ExecutionException (java.util.concurrent.ExecutionException)16 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)15 ArrayList (java.util.ArrayList)13 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Customer (org.thingsboard.server.common.data.Customer)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ApiOperation (io.swagger.annotations.ApiOperation)9 List (java.util.List)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 Edge (org.thingsboard.server.common.data.edge.Edge)8 PageLink (org.thingsboard.server.common.data.page.PageLink)7 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6