Search in sources :

Example 26 with OtaPackage

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

the class BaseOtaPackageServiceTest method testDeleteFirmwareWithReferenceByDevice.

@Test
public void testDeleteFirmwareWithReferenceByDevice() {
    OtaPackage savedFirmware = createFirmware(tenantId, VERSION);
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setDeviceProfileId(deviceProfileId);
    device.setFirmwareId(savedFirmware.getId());
    Device savedDevice = deviceService.saveDevice(device);
    try {
        thrown.expect(DataValidationException.class);
        thrown.expectMessage("The otaPackage referenced by the devices cannot be deleted!");
        otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
    } finally {
        deviceService.deleteDevice(tenantId, savedDevice.getId());
        otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
    }
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) Device(org.thingsboard.server.common.data.Device) Test(org.junit.Test)

Example 27 with OtaPackage

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

the class BaseOtaPackageServiceTest method testSaveFirmwareWithUrl.

@Test
public void testSaveFirmwareWithUrl() {
    OtaPackageInfo firmware = new OtaPackageInfo();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(deviceProfileId);
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    firmware.setUrl(URL);
    firmware.setDataSize(0L);
    OtaPackageInfo savedFirmware = otaPackageService.saveOtaPackageInfo(firmware, true);
    Assert.assertNotNull(savedFirmware);
    Assert.assertNotNull(savedFirmware.getId());
    Assert.assertTrue(savedFirmware.getCreatedTime() > 0);
    Assert.assertEquals(firmware.getTenantId(), savedFirmware.getTenantId());
    Assert.assertEquals(firmware.getTitle(), savedFirmware.getTitle());
    Assert.assertEquals(firmware.getFileName(), savedFirmware.getFileName());
    Assert.assertEquals(firmware.getContentType(), savedFirmware.getContentType());
    savedFirmware.setAdditionalInfo(JacksonUtil.newObjectNode());
    otaPackageService.saveOtaPackageInfo(savedFirmware, true);
    OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, savedFirmware.getId());
    Assert.assertEquals(foundFirmware.getTitle(), savedFirmware.getTitle());
    otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 28 with OtaPackage

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

the class BaseDeviceServiceTest method testAssignFirmwareToDeviceWithDifferentDeviceProfile.

@Test
public void testAssignFirmwareToDeviceWithDifferentDeviceProfile() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    Assert.assertNotNull(savedDevice);
    DeviceProfile deviceProfile = createDeviceProfile(tenantId, "New device Profile");
    DeviceProfile savedProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
    Assert.assertNotNull(savedProfile);
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(savedProfile.getId());
    firmware.setType(FIRMWARE);
    firmware.setTitle("my firmware");
    firmware.setVersion("v1.0");
    firmware.setFileName("test.txt");
    firmware.setContentType("text/plain");
    firmware.setChecksumAlgorithm(ChecksumAlgorithm.SHA256);
    firmware.setChecksum("4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a");
    firmware.setData(ByteBuffer.wrap(new byte[] { 1 }));
    firmware.setDataSize(1L);
    OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
    savedDevice.setFirmwareId(savedFirmware.getId());
    thrown.expect(DataValidationException.class);
    thrown.expectMessage("Can't assign firmware with different deviceProfile!");
    deviceService.saveDevice(savedDevice);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Device(org.thingsboard.server.common.data.Device) OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Example 29 with OtaPackage

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

OtaPackage (org.thingsboard.server.common.data.OtaPackage)29 Test (org.junit.Test)21 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)5 OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)5 Device (org.thingsboard.server.common.data.Device)4 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)4 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 MessagingException (javax.mail.MessagingException)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)1 DashboardInfo (org.thingsboard.server.common.data.DashboardInfo)1