Search in sources :

Example 1 with ProvisionFailedException

use of org.thingsboard.server.dao.device.provision.ProvisionFailedException in project thingsboard by thingsboard.

the class DeviceProvisionServiceImpl method processCreateDevice.

private ProvisionResponse processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
    try {
        if (StringUtils.isEmpty(provisionRequest.getDeviceName())) {
            String newDeviceName = RandomStringUtils.randomAlphanumeric(20);
            log.info("Device name not found in provision request. Generated name is: {}", newDeviceName);
            provisionRequest.setDeviceName(newDeviceName);
        }
        Device savedDevice = deviceService.saveDevice(provisionRequest, profile);
        clusterService.onDeviceUpdated(savedDevice, null);
        saveProvisionStateAttribute(savedDevice).get();
        pushDeviceCreatedEventToRuleEngine(savedDevice);
        notify(savedDevice, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
        return new ProvisionResponse(getDeviceCredentials(savedDevice), ProvisionResponseStatus.SUCCESS);
    } catch (Exception e) {
        log.warn("[{}] Error during device creation from provision request: [{}]", provisionRequest.getDeviceName(), provisionRequest, e);
        Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName());
        if (device != null) {
            notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
        }
        throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
    }
}
Also used : ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device) ProvisionResponse(org.thingsboard.server.dao.device.provision.ProvisionResponse) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProvisionFailedException

use of org.thingsboard.server.dao.device.provision.ProvisionFailedException in project thingsboard by thingsboard.

the class DeviceProvisionServiceImpl method provisionDevice.

@Override
public ProvisionResponse provisionDevice(ProvisionRequest provisionRequest) {
    String provisionRequestKey = provisionRequest.getCredentials().getProvisionDeviceKey();
    String provisionRequestSecret = provisionRequest.getCredentials().getProvisionDeviceSecret();
    if (!StringUtils.isEmpty(provisionRequest.getDeviceName())) {
        provisionRequest.setDeviceName(provisionRequest.getDeviceName().trim());
        if (StringUtils.isEmpty(provisionRequest.getDeviceName())) {
            log.warn("Provision request contains empty device name!");
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        }
    }
    if (StringUtils.isEmpty(provisionRequestKey) || StringUtils.isEmpty(provisionRequestSecret)) {
        throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
    }
    DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKey(provisionRequestKey);
    if (targetProfile == null || targetProfile.getProfileData().getProvisionConfiguration() == null || targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret() == null) {
        throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
    }
    Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null);
    switch(targetProfile.getProvisionType()) {
        case ALLOW_CREATE_NEW_DEVICES:
            if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
                if (targetDevice != null) {
                    log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
                    notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
                    throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
                } else {
                    return createDevice(provisionRequest, targetProfile);
                }
            }
            break;
        case CHECK_PRE_PROVISIONED_DEVICES:
            if (targetProfile.getProfileData().getProvisionConfiguration().getProvisionDeviceSecret().equals(provisionRequestSecret)) {
                if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())) {
                    return processProvision(targetDevice, provisionRequest);
                } else {
                    log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
                    throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
                }
            }
            break;
    }
    throw new ProvisionFailedException(ProvisionResponseStatus.NOT_FOUND.name());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device)

Example 3 with ProvisionFailedException

use of org.thingsboard.server.dao.device.provision.ProvisionFailedException in project thingsboard by thingsboard.

the class DeviceProvisionServiceImpl method processProvision.

private ProvisionResponse processProvision(Device device, ProvisionRequest provisionRequest) {
    try {
        Optional<AttributeKvEntry> provisionState = attributesService.find(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, DEVICE_PROVISION_STATE).get();
        if (provisionState != null && provisionState.isPresent() && !provisionState.get().getValueAsString().equals(PROVISIONED_STATE)) {
            notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        } else {
            saveProvisionStateAttribute(device).get();
            notify(device, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
    }
    return new ProvisionResponse(deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId()), ProvisionResponseStatus.SUCCESS);
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) ProvisionResponse(org.thingsboard.server.dao.device.provision.ProvisionResponse) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ProvisionFailedException

use of org.thingsboard.server.dao.device.provision.ProvisionFailedException in project thingsboard by thingsboard.

the class DeviceServiceImpl method saveDevice.

@Override
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#profile.tenantId, #provisionRequest.deviceName}")
@Transactional
public Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
    Device device = new Device();
    device.setName(provisionRequest.getDeviceName());
    device.setType(profile.getName());
    device.setTenantId(profile.getTenantId());
    Device savedDevice = saveDevice(device);
    if (!StringUtils.isEmpty(provisionRequest.getCredentialsData().getToken()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getX509CertHash()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId())) {
        DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getTenantId(), savedDevice.getId());
        if (deviceCredentials == null) {
            deviceCredentials = new DeviceCredentials();
        }
        deviceCredentials.setDeviceId(savedDevice.getId());
        deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
        switch(provisionRequest.getCredentialsType()) {
            case ACCESS_TOKEN:
                deviceCredentials.setCredentialsId(provisionRequest.getCredentialsData().getToken());
                break;
            case MQTT_BASIC:
                BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
                mqttCredentials.setClientId(provisionRequest.getCredentialsData().getClientId());
                mqttCredentials.setUserName(provisionRequest.getCredentialsData().getUsername());
                mqttCredentials.setPassword(provisionRequest.getCredentialsData().getPassword());
                deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
                break;
            case X509_CERTIFICATE:
                deviceCredentials.setCredentialsValue(provisionRequest.getCredentialsData().getX509CertHash());
                break;
            case LWM2M_CREDENTIALS:
                break;
        }
        try {
            deviceCredentialsService.updateDeviceCredentials(savedDevice.getTenantId(), deviceCredentials);
        } catch (Exception e) {
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        }
    }
    // eviction by name is described as annotation @CacheEvict above
    cacheManager.removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId());
    return savedDevice;
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException) CacheEvict(org.springframework.cache.annotation.CacheEvict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ProvisionFailedException (org.thingsboard.server.dao.device.provision.ProvisionFailedException)4 ExecutionException (java.util.concurrent.ExecutionException)3 Device (org.thingsboard.server.common.data.Device)3 ProvisionResponse (org.thingsboard.server.dao.device.provision.ProvisionResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Transactional (org.springframework.transaction.annotation.Transactional)1 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 BasicMqttCredentials (org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1