Search in sources :

Example 1 with ClaimResult

use of org.thingsboard.server.dao.device.claim.ClaimResult in project thingsboard by thingsboard.

the class ClaimDevicesServiceImpl method claimDevice.

@Override
public ListenableFuture<ClaimResult> claimDevice(Device device, CustomerId customerId, String secretKey) throws ExecutionException, InterruptedException {
    Cache cache = cacheManager.getCache(CLAIM_DEVICES_CACHE);
    ClaimDataInfo claimData = getClaimData(cache, device);
    if (claimData != null) {
        long currTs = System.currentTimeMillis();
        if (currTs > claimData.getData().getExpirationTime() || !secretKeyIsEmptyOrEqual(secretKey, claimData.getData().getSecretKey())) {
            log.warn("The claiming timeout occurred or wrong 'secretKey' provided for the device [{}]", device.getName());
            if (claimData.isFromCache()) {
                cache.evict(claimData.getKey());
            }
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.FAILURE));
        } else {
            if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
                device.setCustomerId(customerId);
                Device savedDevice = deviceService.saveDevice(device);
                clusterService.onDeviceUpdated(savedDevice, device);
                return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(savedDevice, ClaimResponse.SUCCESS), MoreExecutors.directExecutor());
            }
            return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(null, ClaimResponse.CLAIMED), MoreExecutors.directExecutor());
        }
    } else {
        log.warn("Failed to find the device's claiming message![{}]", device.getName());
        if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.FAILURE));
        } else {
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.CLAIMED));
        }
    }
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) Device(org.thingsboard.server.common.data.Device) ClaimDataInfo(org.thingsboard.server.dao.device.ClaimDataInfo) Cache(org.springframework.cache.Cache)

Example 2 with ClaimResult

use of org.thingsboard.server.dao.device.claim.ClaimResult in project thingsboard by thingsboard.

the class AbstractCoapClaimDeviceTest method validateClaimResponse.

protected void validateClaimResponse(boolean emptyPayload, CoapClient client, byte[] payloadBytes, byte[] failurePayloadBytes) throws Exception {
    postClaimRequest(client, failurePayloadBytes);
    loginUser(customerAdmin.getName(), CUSTOMER_USER_PASSWORD);
    ClaimRequest claimRequest;
    if (!emptyPayload) {
        claimRequest = new ClaimRequest("value");
    } else {
        claimRequest = new ClaimRequest(null);
    }
    ClaimResponse claimResponse = doExecuteWithRetriesAndInterval(() -> doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest()), 100, 200);
    assertEquals(claimResponse, ClaimResponse.FAILURE);
    postClaimRequest(client, payloadBytes);
    ClaimResult claimResult = doExecuteWithRetriesAndInterval(() -> doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResult.class, status().isOk()), 100, 200);
    assertEquals(claimResult.getResponse(), ClaimResponse.SUCCESS);
    Device claimedDevice = claimResult.getDevice();
    assertNotNull(claimedDevice);
    assertNotNull(claimedDevice.getCustomerId());
    assertEquals(customerAdmin.getCustomerId(), claimedDevice.getCustomerId());
    claimResponse = doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest());
    assertEquals(claimResponse, ClaimResponse.CLAIMED);
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) ClaimRequest(org.thingsboard.server.common.data.ClaimRequest) ClaimResponse(org.thingsboard.server.dao.device.claim.ClaimResponse) Device(org.thingsboard.server.common.data.Device)

Example 3 with ClaimResult

use of org.thingsboard.server.dao.device.claim.ClaimResult in project thingsboard by thingsboard.

the class AbstractMqttClaimDeviceTest method validateGatewayClaimResponse.

protected void validateGatewayClaimResponse(String deviceName, boolean emptyPayload, MqttAsyncClient client, byte[] failurePayloadBytes, byte[] payloadBytes) throws Exception {
    client.publish(MqttTopics.GATEWAY_CLAIM_TOPIC, new MqttMessage(failurePayloadBytes));
    Device savedDevice = doExecuteWithRetriesAndInterval(() -> doGet("/api/tenant/devices?deviceName=" + deviceName, Device.class), 20, 100);
    assertNotNull(savedDevice);
    loginUser(customerAdmin.getName(), CUSTOMER_USER_PASSWORD);
    ClaimRequest claimRequest;
    if (!emptyPayload) {
        claimRequest = new ClaimRequest("value");
    } else {
        claimRequest = new ClaimRequest(null);
    }
    ClaimResponse claimResponse = doPostClaimAsync("/api/customer/device/" + deviceName + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest());
    assertEquals(claimResponse, ClaimResponse.FAILURE);
    client.publish(MqttTopics.GATEWAY_CLAIM_TOPIC, new MqttMessage(payloadBytes));
    ClaimResult claimResult = doExecuteWithRetriesAndInterval(() -> doPostClaimAsync("/api/customer/device/" + deviceName + "/claim", claimRequest, ClaimResult.class, status().isOk()), 20, 100);
    assertEquals(claimResult.getResponse(), ClaimResponse.SUCCESS);
    Device claimedDevice = claimResult.getDevice();
    assertNotNull(claimedDevice);
    assertNotNull(claimedDevice.getCustomerId());
    assertEquals(customerAdmin.getCustomerId(), claimedDevice.getCustomerId());
    claimResponse = doPostClaimAsync("/api/customer/device/" + deviceName + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest());
    assertEquals(claimResponse, ClaimResponse.CLAIMED);
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) ClaimRequest(org.thingsboard.server.common.data.ClaimRequest) ClaimResponse(org.thingsboard.server.dao.device.claim.ClaimResponse) Device(org.thingsboard.server.common.data.Device)

Example 4 with ClaimResult

use of org.thingsboard.server.dao.device.claim.ClaimResult in project thingsboard by thingsboard.

the class AbstractMqttClaimDeviceTest method validateClaimResponse.

protected void validateClaimResponse(boolean emptyPayload, MqttAsyncClient client, byte[] payloadBytes, byte[] failurePayloadBytes) throws Exception {
    client.publish(MqttTopics.DEVICE_CLAIM_TOPIC, new MqttMessage(failurePayloadBytes));
    loginUser(customerAdmin.getName(), CUSTOMER_USER_PASSWORD);
    ClaimRequest claimRequest;
    if (!emptyPayload) {
        claimRequest = new ClaimRequest("value");
    } else {
        claimRequest = new ClaimRequest(null);
    }
    ClaimResponse claimResponse = doExecuteWithRetriesAndInterval(() -> doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest()), 20, 100);
    assertEquals(claimResponse, ClaimResponse.FAILURE);
    client.publish(MqttTopics.DEVICE_CLAIM_TOPIC, new MqttMessage(payloadBytes));
    ClaimResult claimResult = doExecuteWithRetriesAndInterval(() -> doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResult.class, status().isOk()), 20, 100);
    assertEquals(claimResult.getResponse(), ClaimResponse.SUCCESS);
    Device claimedDevice = claimResult.getDevice();
    assertNotNull(claimedDevice);
    assertNotNull(claimedDevice.getCustomerId());
    assertEquals(customerAdmin.getCustomerId(), claimedDevice.getCustomerId());
    claimResponse = doPostClaimAsync("/api/customer/device/" + savedDevice.getName() + "/claim", claimRequest, ClaimResponse.class, status().isBadRequest());
    assertEquals(claimResponse, ClaimResponse.CLAIMED);
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) ClaimRequest(org.thingsboard.server.common.data.ClaimRequest) ClaimResponse(org.thingsboard.server.dao.device.claim.ClaimResponse) Device(org.thingsboard.server.common.data.Device)

Example 5 with ClaimResult

use of org.thingsboard.server.dao.device.claim.ClaimResult in project thingsboard by thingsboard.

the class DeviceController method claimDevice.

@ApiOperation(value = "Claim device (claimDevice)", notes = "Claiming makes it possible to assign a device to the specific customer using device/server side claiming data (in the form of secret key)." + "To make this happen you have to provide unique device name and optional claiming data (it is needed only for device-side claiming)." + "Once device is claimed, the customer becomes its owner and customer users may access device data as well as control the device. \n" + "In order to enable claiming devices feature a system parameter security.claim.allowClaimingByDefault should be set to true, " + "otherwise a server-side claimingAllowed attribute with the value true is obligatory for provisioned devices. \n" + "See official documentation for more details regarding claiming." + CUSTOMER_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('CUSTOMER_USER')")
@RequestMapping(value = "/customer/device/{deviceName}/claim", method = RequestMethod.POST)
@ResponseBody
public DeferredResult<ResponseEntity> claimDevice(@ApiParam(value = "Unique name of the device which is going to be claimed") @PathVariable(DEVICE_NAME) String deviceName, @ApiParam(value = "Claiming request which can optionally contain secret key") @RequestBody(required = false) ClaimRequest claimRequest) throws ThingsboardException {
    checkParameter(DEVICE_NAME, deviceName);
    try {
        final DeferredResult<ResponseEntity> deferredResult = new DeferredResult<>();
        SecurityUser user = getCurrentUser();
        TenantId tenantId = user.getTenantId();
        CustomerId customerId = user.getCustomerId();
        Device device = checkNotNull(deviceService.findDeviceByTenantIdAndName(tenantId, deviceName));
        accessControlService.checkPermission(user, Resource.DEVICE, Operation.CLAIM_DEVICES, device.getId(), device);
        String secretKey = getSecretKey(claimRequest);
        ListenableFuture<ClaimResult> future = claimDevicesService.claimDevice(device, customerId, secretKey);
        Futures.addCallback(future, new FutureCallback<ClaimResult>() {

            @Override
            public void onSuccess(@Nullable ClaimResult result) {
                HttpStatus status;
                if (result != null) {
                    if (result.getResponse().equals(ClaimResponse.SUCCESS)) {
                        status = HttpStatus.OK;
                        deferredResult.setResult(new ResponseEntity<>(result, status));
                        try {
                            logEntityAction(user, device.getId(), result.getDevice(), customerId, ActionType.ASSIGNED_TO_CUSTOMER, null, device.getId().toString(), customerId.toString(), customerService.findCustomerById(tenantId, customerId).getName());
                        } catch (ThingsboardException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        status = HttpStatus.BAD_REQUEST;
                        deferredResult.setResult(new ResponseEntity<>(result.getResponse(), status));
                    }
                } else {
                    deferredResult.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
                }
            }

            @Override
            public void onFailure(Throwable t) {
                deferredResult.setErrorResult(t);
            }
        }, MoreExecutors.directExecutor());
        return deferredResult;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) HttpStatus(org.springframework.http.HttpStatus) Device(org.thingsboard.server.common.data.Device) CustomerId(org.thingsboard.server.common.data.id.CustomerId) IOException(java.io.IOException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseEntity(org.springframework.http.ResponseEntity) TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) DeferredResult(org.springframework.web.context.request.async.DeferredResult) 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

Device (org.thingsboard.server.common.data.Device)5 ClaimResult (org.thingsboard.server.dao.device.claim.ClaimResult)5 ClaimRequest (org.thingsboard.server.common.data.ClaimRequest)3 ClaimResponse (org.thingsboard.server.dao.device.claim.ClaimResponse)3 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 Cache (org.springframework.cache.Cache)1 HttpStatus (org.springframework.http.HttpStatus)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 ClaimDataInfo (org.thingsboard.server.dao.device.ClaimDataInfo)1 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1