Search in sources :

Example 26 with Lwm2mDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class LwM2mClientContextImpl method doGetAndCache.

private Lwm2mDeviceProfileTransportConfiguration doGetAndCache(UUID profileId) {
    Lwm2mDeviceProfileTransportConfiguration result = profiles.get(profileId);
    if (result == null) {
        log.debug("Fetching profile [{}]", profileId);
        DeviceProfile deviceProfile = deviceProfileCache.get(new DeviceProfileId(profileId));
        if (deviceProfile != null) {
            result = profileUpdate(deviceProfile);
        } else {
            log.warn("Device profile was not found! Most probably device profile [{}] has been removed from the database.", profileId);
        }
    }
    return result;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)

Example 27 with Lwm2mDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class LwM2mClientContextImpl method profileUpdate.

@Override
public Lwm2mDeviceProfileTransportConfiguration profileUpdate(DeviceProfile deviceProfile) {
    Lwm2mDeviceProfileTransportConfiguration clientProfile = LwM2MTransportUtil.toLwM2MClientProfile(deviceProfile);
    profiles.put(deviceProfile.getUuidId(), clientProfile);
    return clientProfile;
}
Also used : Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)

Example 28 with Lwm2mDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method onDeviceProfileUpdate.

// TODO: review and optimize the logic to minimize number of the requests to device.
private void onDeviceProfileUpdate(List<LwM2mClient> clients, Lwm2mDeviceProfileTransportConfiguration oldProfile, DeviceProfile deviceProfile) {
    if (clientContext.profileUpdate(deviceProfile) != null) {
        TelemetryMappingConfiguration oldTelemetryParams = oldProfile.getObserveAttr();
        Set<String> attributeSetOld = oldTelemetryParams.getAttribute();
        Set<String> telemetrySetOld = oldTelemetryParams.getTelemetry();
        Set<String> observeOld = oldTelemetryParams.getObserve();
        Map<String, String> keyNameOld = oldTelemetryParams.getKeyName();
        Map<String, ObjectAttributes> attributeLwm2mOld = oldTelemetryParams.getAttributeLwm2m();
        var newProfile = clientContext.getProfile(deviceProfile.getUuidId());
        TelemetryMappingConfiguration newTelemetryParams = newProfile.getObserveAttr();
        Set<String> attributeSetNew = newTelemetryParams.getAttribute();
        Set<String> telemetrySetNew = newTelemetryParams.getTelemetry();
        Set<String> observeNew = newTelemetryParams.getObserve();
        Map<String, String> keyNameNew = newTelemetryParams.getKeyName();
        Map<String, ObjectAttributes> attributeLwm2mNew = newTelemetryParams.getAttributeLwm2m();
        Set<String> observeToAdd = diffSets(observeOld, observeNew);
        Set<String> observeToRemove = diffSets(observeNew, observeOld);
        Set<String> newObjectsToRead = new HashSet<>();
        Set<String> newObjectsToCancelRead = new HashSet<>();
        if (!attributeSetOld.equals(attributeSetNew)) {
            newObjectsToRead.addAll(diffSets(attributeSetOld, attributeSetNew));
            newObjectsToCancelRead.addAll(diffSets(attributeSetNew, attributeSetOld));
        }
        if (!telemetrySetOld.equals(telemetrySetNew)) {
            newObjectsToRead.addAll(diffSets(telemetrySetOld, telemetrySetNew));
            newObjectsToCancelRead.addAll(diffSets(telemetrySetNew, telemetrySetOld));
        }
        if (!keyNameOld.equals(keyNameNew)) {
            ParametersAnalyzeResult keyNameChange = this.getAnalyzerKeyName(keyNameOld, keyNameNew);
            newObjectsToRead.addAll(keyNameChange.getPathPostParametersAdd());
        }
        ParametersAnalyzeResult analyzerParameters = getAttributesAnalyzer(attributeLwm2mOld, attributeLwm2mNew);
        clients.forEach(client -> {
            LwM2MModelConfig modelConfig = new LwM2MModelConfig(client.getEndpoint());
            modelConfig.getToRead().addAll(diffSets(observeToAdd, newObjectsToRead));
            modelConfig.getToCancelRead().addAll(newObjectsToCancelRead);
            modelConfig.getToCancelObserve().addAll(observeToRemove);
            modelConfig.getToObserve().addAll(observeToAdd);
            Set<String> clientObjects = clientContext.getSupportedIdVerInClient(client);
            Set<String> pathToAdd = analyzerParameters.getPathPostParametersAdd().stream().filter(target -> clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1])).collect(Collectors.toUnmodifiableSet());
            modelConfig.getAttributesToAdd().putAll(pathToAdd.stream().collect(Collectors.toMap(t -> t, attributeLwm2mNew::get)));
            Set<String> pathToRemove = analyzerParameters.getPathPostParametersDel().stream().filter(target -> clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1])).collect(Collectors.toUnmodifiableSet());
            modelConfig.getAttributesToRemove().addAll(pathToRemove);
            modelConfigService.sendUpdates(client, modelConfig);
        });
        // update value in fwInfo
        OtherConfiguration newLwM2mSettings = newProfile.getClientLwM2mSettings();
        OtherConfiguration oldLwM2mSettings = oldProfile.getClientLwM2mSettings();
        if (!newLwM2mSettings.getFwUpdateStrategy().equals(oldLwM2mSettings.getFwUpdateStrategy()) || (StringUtils.isNotEmpty(newLwM2mSettings.getFwUpdateResource()) && !newLwM2mSettings.getFwUpdateResource().equals(oldLwM2mSettings.getFwUpdateResource()))) {
            clients.forEach(lwM2MClient -> otaService.onFirmwareStrategyUpdate(lwM2MClient, newLwM2mSettings));
        }
        if (!newLwM2mSettings.getSwUpdateStrategy().equals(oldLwM2mSettings.getSwUpdateStrategy()) || (StringUtils.isNotEmpty(newLwM2mSettings.getSwUpdateResource()) && !newLwM2mSettings.getSwUpdateResource().equals(oldLwM2mSettings.getSwUpdateResource()))) {
            clients.forEach(lwM2MClient -> otaService.onCurrentSoftwareStrategyUpdate(lwM2MClient, newLwM2mSettings));
        }
    }
}
Also used : LwM2MTransportServerConfig(org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig) LwM2MTelemetryLogService(org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService) TypeToken(com.google.gson.reflect.TypeToken) SW_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_VER_ID) TbLwM2MLatchCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MLatchCallback) LwM2MSessionManager(org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) TbLwM2MReadRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MReadRequest) LwM2MModelConfig(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig) GsonBuilder(com.google.gson.GsonBuilder) TenantId(org.thingsboard.server.common.data.id.TenantId) LwM2MExecutorAwareService(org.thingsboard.server.transport.lwm2m.server.common.LwM2MExecutorAwareService) TbLwM2MReadCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MReadCallback) Observation(org.eclipse.leshan.core.observation.Observation) Map(java.util.Map) SW_3_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_3_VER_ID) LwM2mClientContext(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TbLwM2mSecurityStore(org.thingsboard.server.transport.lwm2m.server.store.TbLwM2mSecurityStore) StringUtils(org.thingsboard.server.common.data.StringUtils) OtaPackageUtil(org.thingsboard.server.common.data.ota.OtaPackageUtil) Set(java.util.Set) FW_RESULT_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_RESULT_ID) TbLwM2MWriteAttributesRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) LOG_LWM2M_INFO(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_INFO) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Lazy(org.springframework.context.annotation.Lazy) LwM2MTransportUtil(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient) LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) OtherConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.OtherConfiguration) LwM2mVersionedModelProvider(org.thingsboard.server.transport.lwm2m.server.LwM2mVersionedModelProvider) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) Device(org.thingsboard.server.common.data.Device) TbLwM2mTransportComponent(org.thingsboard.server.queue.util.TbLwM2mTransportComponent) ArrayList(java.util.ArrayList) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) TbLwM2MObserveCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MObserveCallback) ReadCompositeResponse(org.eclipse.leshan.core.response.ReadCompositeResponse) Service(org.springframework.stereotype.Service) LwM2MModelConfigService(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfigService) ParametersAnalyzeResult(org.thingsboard.server.transport.lwm2m.server.client.ParametersAnalyzeResult) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) TelemetryMappingConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.TelemetryMappingConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) LOG_LWM2M_ERROR(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_ERROR) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2MTransportUtil.convertOtaUpdateValueToString(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString) FW_STATE_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_STATE_ID) LwM2mOtaConvert(org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert) LOG_LWM2M_WARN(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_WARN) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) LwM2MOtaUpdateService(org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService) LwM2mValueConverterImpl(org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl) FW_DELIVERY_METHOD(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_DELIVERY_METHOD) LwM2MTransportUtil.fromVersionedIdToObjectId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.fromVersionedIdToObjectId) FW_3_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_3_VER_ID) LwM2MAttributesService(org.thingsboard.server.transport.lwm2m.server.attributes.LwM2MAttributesService) LwM2mTransportServerHelper(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonObject(com.google.gson.JsonObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DonAsynchron(org.thingsboard.common.util.DonAsynchron) LwM2MTransportUtil.convertObjectIdToVersionedId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertObjectIdToVersionedId) TbLwM2MWriteAttributesCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesCallback) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Random(java.util.Random) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DownlinkRequestCallback(org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback) LwM2mTransportContext(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext) PreDestroy(javax.annotation.PreDestroy) LwM2MClientState(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientState) Registration(org.eclipse.leshan.server.registration.Registration) Gson(com.google.gson.Gson) ResultsAddKeyValueProto(org.thingsboard.server.transport.lwm2m.server.client.ResultsAddKeyValueProto) SW_STATE_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_STATE_ID) FW_NAME_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_NAME_ID) LWM2M_SEPARATOR_PATH(org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH) LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) DeviceId(org.thingsboard.server.common.data.id.DeviceId) LwM2mDownlinkMsgHandler(org.thingsboard.server.transport.lwm2m.server.downlink.LwM2mDownlinkMsgHandler) SW_NAME_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_NAME_ID) ResponseCode(org.eclipse.leshan.core.ResponseCode) LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException) Collection(java.util.Collection) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) TransportService(org.thingsboard.server.common.transport.TransportService) Collectors(java.util.stream.Collectors) CollectionsUtil.diffSets(org.thingsboard.common.util.CollectionsUtil.diffSets) FW_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_VER_ID) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TbLwM2MObserveRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MObserveRequest) Optional(java.util.Optional) TbLwM2MDtlsSessionStore(org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MDtlsSessionStore) TbLwM2MCancelObserveCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MCancelObserveCallback) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) SW_RESULT_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_RESULT_ID) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) TbLwM2MCancelObserveRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MCancelObserveRequest) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) WriteCompositeRequest(org.eclipse.leshan.core.request.WriteCompositeRequest) TimeUnit(java.util.concurrent.TimeUnit) LwM2mResourceInstance(org.eclipse.leshan.core.node.LwM2mResourceInstance) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) RegistrationStore(org.eclipse.leshan.server.registration.RegistrationStore) Collections(java.util.Collections) OtherConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.OtherConfiguration) ParametersAnalyzeResult(org.thingsboard.server.transport.lwm2m.server.client.ParametersAnalyzeResult) TelemetryMappingConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.TelemetryMappingConfiguration) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) LwM2MTransportUtil.convertOtaUpdateValueToString(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString) LwM2MModelConfig(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig) HashSet(java.util.HashSet)

Example 29 with Lwm2mDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class LwM2MBootstrapSecurityStore method addValueToStore.

public SecurityInfo addValueToStore(TbLwM2MSecurityInfo store, String endpoint) {
    /* add value to store  from BootstrapJson */
    SecurityInfo securityInfo = null;
    if (store != null && store.getBootstrapCredentialConfig() != null && store.getSecurityMode() != null) {
        securityInfo = store.getSecurityInfo();
        this.setBootstrapConfigSecurityInfo(store);
        BootstrapConfig bsConfigNew = store.getBootstrapConfig();
        if (bsConfigNew != null) {
            try {
                boolean bootstrapServerUpdateEnable = ((Lwm2mDeviceProfileTransportConfiguration) store.getDeviceProfile().getProfileData().getTransportConfiguration()).isBootstrapServerUpdateEnable();
                if (!bootstrapServerUpdateEnable) {
                    Optional<Map.Entry<Integer, BootstrapConfig.ServerSecurity>> securities = bsConfigNew.security.entrySet().stream().filter(sec -> sec.getValue().bootstrapServer).findAny();
                    if (securities.isPresent()) {
                        bsConfigNew.security.entrySet().remove(securities.get());
                        int serverSortId = securities.get().getValue().serverId;
                        Optional<Map.Entry<Integer, BootstrapConfig.ServerConfig>> serverConfigs = bsConfigNew.servers.entrySet().stream().filter(serv -> (serv.getValue()).shortId == serverSortId).findAny();
                        if (serverConfigs.isPresent()) {
                            bsConfigNew.servers.entrySet().remove(serverConfigs.get());
                        }
                    }
                }
                for (String config : bootstrapConfigStore.getAll().keySet()) {
                    if (config.equals(endpoint)) {
                        bootstrapConfigStore.remove(config);
                    }
                }
                bootstrapConfigStore.add(endpoint, bsConfigNew);
            } catch (InvalidConfigurationException e) {
                if (e.getMessage().contains("Psk identity") && e.getMessage().contains("already used for this bootstrap server")) {
                    log.trace("Invalid Bootstrap Configuration", e);
                } else {
                    log.error("Invalid Bootstrap Configuration", e);
                }
            }
        }
    }
    return securityInfo;
}
Also used : AbstractLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential) LwM2mSessionMsgListener(org.thingsboard.server.transport.lwm2m.server.LwM2mSessionMsgListener) LwM2MSecurityMode(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MSecurityMode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) LwM2mTransportContext(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext) InvalidConfigurationException(org.eclipse.leshan.server.bootstrap.InvalidConfigurationException) Service(org.springframework.stereotype.Service) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) Map(java.util.Map) TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) TbLwM2mBootstrapTransportComponent(org.thingsboard.server.queue.util.TbLwM2mBootstrapTransportComponent) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) LOG_LWM2M_ERROR(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_ERROR) EditableBootstrapConfigStore(org.eclipse.leshan.server.bootstrap.EditableBootstrapConfigStore) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LwM2MBootstrapConfig(org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapConfig) LwM2MAuthException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MAuthException) UUID(java.util.UUID) BOOTSTRAP(org.thingsboard.server.transport.lwm2m.server.uplink.LwM2mTypeServer.BOOTSTRAP) Slf4j(lombok.extern.slf4j.Slf4j) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) LwM2mCredentialsSecurityInfoValidator(org.thingsboard.server.transport.lwm2m.secure.LwM2mCredentialsSecurityInfoValidator) LwM2mTransportServerHelper(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper) Optional(java.util.Optional) BootstrapSecurityStore(org.eclipse.leshan.server.security.BootstrapSecurityStore) Collections(java.util.Collections) LOG_LWM2M_TELEMETRY(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_TELEMETRY) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) LwM2MBootstrapConfig(org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapConfig) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) SecurityInfo(org.eclipse.leshan.server.security.SecurityInfo) TbLwM2MSecurityInfo(org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo) InvalidConfigurationException(org.eclipse.leshan.server.bootstrap.InvalidConfigurationException)

Example 30 with Lwm2mDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class DeviceProfileDataValidator method validateDataImpl.

@Override
protected void validateDataImpl(TenantId tenantId, DeviceProfile deviceProfile) {
    if (org.thingsboard.server.common.data.StringUtils.isEmpty(deviceProfile.getName())) {
        throw new DataValidationException("Device profile name should be specified!");
    }
    if (deviceProfile.getType() == null) {
        throw new DataValidationException("Device profile type should be specified!");
    }
    if (deviceProfile.getTransportType() == null) {
        throw new DataValidationException("Device profile transport type should be specified!");
    }
    if (deviceProfile.getTenantId() == null) {
        throw new DataValidationException("Device profile should be assigned to tenant!");
    } else {
        Tenant tenant = tenantDao.findById(deviceProfile.getTenantId(), deviceProfile.getTenantId().getId());
        if (tenant == null) {
            throw new DataValidationException("Device profile is referencing to non-existent tenant!");
        }
    }
    if (deviceProfile.isDefault()) {
        DeviceProfile defaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(tenantId);
        if (defaultDeviceProfile != null && !defaultDeviceProfile.getId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Another default device profile is present in scope of current tenant!");
        }
    }
    if (!org.thingsboard.server.common.data.StringUtils.isEmpty(deviceProfile.getDefaultQueueName()) && queueService != null) {
        if (!queueService.getQueuesByServiceType(ServiceType.TB_RULE_ENGINE).contains(deviceProfile.getDefaultQueueName())) {
            throw new DataValidationException("Device profile is referencing to non-existent queue!");
        }
    }
    if (deviceProfile.getProvisionType() == null) {
        deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    }
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    transportConfiguration.validate();
    if (transportConfiguration instanceof MqttDeviceProfileTransportConfiguration) {
        MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
        if (mqttTransportConfiguration.getTransportPayloadTypeConfiguration() instanceof ProtoTransportPayloadConfiguration) {
            ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) mqttTransportConfiguration.getTransportPayloadTypeConfiguration();
            validateProtoSchemas(protoTransportPayloadConfiguration);
            validateTelemetryDynamicMessageFields(protoTransportPayloadConfiguration);
            validateRpcRequestDynamicMessageFields(protoTransportPayloadConfiguration);
        }
    } else if (transportConfiguration instanceof CoapDeviceProfileTransportConfiguration) {
        CoapDeviceProfileTransportConfiguration coapDeviceProfileTransportConfiguration = (CoapDeviceProfileTransportConfiguration) transportConfiguration;
        CoapDeviceTypeConfiguration coapDeviceTypeConfiguration = coapDeviceProfileTransportConfiguration.getCoapDeviceTypeConfiguration();
        if (coapDeviceTypeConfiguration instanceof DefaultCoapDeviceTypeConfiguration) {
            DefaultCoapDeviceTypeConfiguration defaultCoapDeviceTypeConfiguration = (DefaultCoapDeviceTypeConfiguration) coapDeviceTypeConfiguration;
            TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = defaultCoapDeviceTypeConfiguration.getTransportPayloadTypeConfiguration();
            if (transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration) {
                ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
                validateProtoSchemas(protoTransportPayloadConfiguration);
                validateTelemetryDynamicMessageFields(protoTransportPayloadConfiguration);
                validateRpcRequestDynamicMessageFields(protoTransportPayloadConfiguration);
            }
        }
    } else if (transportConfiguration instanceof Lwm2mDeviceProfileTransportConfiguration) {
        List<LwM2MBootstrapServerCredential> lwM2MBootstrapServersConfigurations = ((Lwm2mDeviceProfileTransportConfiguration) transportConfiguration).getBootstrap();
        if (lwM2MBootstrapServersConfigurations != null) {
            validateLwm2mServersConfigOfBootstrapForClient(lwM2MBootstrapServersConfigurations, ((Lwm2mDeviceProfileTransportConfiguration) transportConfiguration).isBootstrapServerUpdateEnable());
            for (LwM2MBootstrapServerCredential bootstrapServerCredential : lwM2MBootstrapServersConfigurations) {
                validateLwm2mServersCredentialOfBootstrapForClient(bootstrapServerCredential);
            }
        }
    }
    List<DeviceProfileAlarm> profileAlarms = deviceProfile.getProfileData().getAlarms();
    if (!CollectionUtils.isEmpty(profileAlarms)) {
        Set<String> alarmTypes = new HashSet<>();
        for (DeviceProfileAlarm alarm : profileAlarms) {
            String alarmType = alarm.getAlarmType();
            if (StringUtils.isEmpty(alarmType)) {
                throw new DataValidationException("Alarm rule type should be specified!");
            }
            if (!alarmTypes.add(alarmType)) {
                throw new DataValidationException(String.format("Can't create device profile with the same alarm rule types: \"%s\"!", alarmType));
            }
        }
    }
    if (deviceProfile.getDefaultRuleChainId() != null) {
        RuleChain ruleChain = ruleChainService.findRuleChainById(tenantId, deviceProfile.getDefaultRuleChainId());
        if (ruleChain == null) {
            throw new DataValidationException("Can't assign non-existent rule chain!");
        }
    }
    if (deviceProfile.getDefaultDashboardId() != null) {
        DashboardInfo dashboard = dashboardService.findDashboardInfoById(tenantId, deviceProfile.getDefaultDashboardId());
        if (dashboard == null) {
            throw new DataValidationException("Can't assign non-existent dashboard!");
        }
    }
    if (deviceProfile.getFirmwareId() != null) {
        OtaPackage firmware = otaPackageService.findOtaPackageById(tenantId, deviceProfile.getFirmwareId());
        if (firmware == null) {
            throw new DataValidationException("Can't assign non-existent firmware!");
        }
        if (!firmware.getType().equals(OtaPackageType.FIRMWARE)) {
            throw new DataValidationException("Can't assign firmware with type: " + firmware.getType());
        }
        if (firmware.getData() == null && !firmware.hasUrl()) {
            throw new DataValidationException("Can't assign firmware with empty data!");
        }
        if (!firmware.getDeviceProfileId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Can't assign firmware with different deviceProfile!");
        }
    }
    if (deviceProfile.getSoftwareId() != null) {
        OtaPackage software = otaPackageService.findOtaPackageById(tenantId, deviceProfile.getSoftwareId());
        if (software == null) {
            throw new DataValidationException("Can't assign non-existent software!");
        }
        if (!software.getType().equals(OtaPackageType.SOFTWARE)) {
            throw new DataValidationException("Can't assign software with type: " + software.getType());
        }
        if (software.getData() == null && !software.hasUrl()) {
            throw new DataValidationException("Can't assign software with empty data!");
        }
        if (!software.getDeviceProfileId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Can't assign firmware with different deviceProfile!");
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) AbstractLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential) RPKLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.RPKLwM2MBootstrapServerCredential) LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.LwM2MBootstrapServerCredential) X509LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration) DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) CoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceTypeConfiguration) HashSet(java.util.HashSet)

Aggregations

Lwm2mDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration)30 LwM2MDeviceCredentials (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials)19 Test (org.junit.Test)16 AbstractSecurityLwM2MIntegrationTest (org.thingsboard.server.transport.lwm2m.security.AbstractSecurityLwM2MIntegrationTest)13 PrivateKey (java.security.PrivateKey)10 X509Certificate (java.security.cert.X509Certificate)10 Security (org.eclipse.leshan.client.object.Security)8 Device (org.thingsboard.server.common.data.Device)6 X509ClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential)6 MvcResult (org.springframework.test.web.servlet.MvcResult)5 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)4 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 PSKClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.PSKClientCredential)3 RPKClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.RPKClientCredential)3 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)3 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)3 OtaPackageUpdateStatus (org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus)3 AbstractOtaLwM2MIntegrationTest (org.thingsboard.server.transport.lwm2m.ota.AbstractOtaLwM2MIntegrationTest)3 Collections (java.util.Collections)2