Search in sources :

Example 11 with DeviceProfile

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

the class BaseDeviceProfileControllerTest method testSaveDeviceProfile.

@Test
public void testSaveDeviceProfile() throws Exception {
    DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", null);
    DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class);
    Assert.assertNotNull(savedDeviceProfile);
    Assert.assertNotNull(savedDeviceProfile.getId());
    Assert.assertTrue(savedDeviceProfile.getCreatedTime() > 0);
    Assert.assertEquals(deviceProfile.getName(), savedDeviceProfile.getName());
    Assert.assertEquals(deviceProfile.getDescription(), savedDeviceProfile.getDescription());
    Assert.assertEquals(deviceProfile.getProfileData(), savedDeviceProfile.getProfileData());
    Assert.assertEquals(deviceProfile.isDefault(), savedDeviceProfile.isDefault());
    Assert.assertEquals(deviceProfile.getDefaultRuleChainId(), savedDeviceProfile.getDefaultRuleChainId());
    Assert.assertEquals(DeviceProfileProvisionType.DISABLED, savedDeviceProfile.getProvisionType());
    savedDeviceProfile.setName("New device profile");
    doPost("/api/deviceProfile", savedDeviceProfile, DeviceProfile.class);
    DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString(), DeviceProfile.class);
    Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfile.getName());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Test(org.junit.Test)

Example 12 with DeviceProfile

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

the class BaseOtaPackageControllerTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant2@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", null);
    DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class);
    Assert.assertNotNull(savedDeviceProfile);
    deviceProfileId = savedDeviceProfile.getId();
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Before(org.junit.Before)

Example 13 with DeviceProfile

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

the class TbLwM2MDtlsCertificateVerifier method verifyCertificate.

@Override
public CertificateVerificationResult verifyCertificate(ConnectionId cid, ServerNames serverName, InetSocketAddress remotePeer, boolean clientUsage, boolean verifySubject, boolean truncateCertificatePath, CertificateMessage message) {
    CertPath certChain = message.getCertificateChain();
    if (certChain == null) {
        // We trust all RPK on this layer, and use TbLwM2MAuthorizer
        PublicKey publicKey = message.getPublicKey();
        return new CertificateVerificationResult(cid, publicKey, null);
    } else {
        try {
            boolean x509CredentialsFound = false;
            X509Certificate[] chain = certChain.getCertificates().toArray(new X509Certificate[0]);
            for (X509Certificate cert : chain) {
                try {
                    if (!skipValidityCheckForClientCert) {
                        cert.checkValidity();
                    }
                    TbLwM2MSecurityInfo securityInfo = null;
                    if (staticCertificateVerifier != null) {
                        HandshakeException exception = staticCertificateVerifier.verifyCertificate(cid, serverName, remotePeer, clientUsage, verifySubject, truncateCertificatePath, message).getException();
                        if (exception == null) {
                            try {
                                String endpoint = config.getTrustSslCredentials().getValueFromSubjectNameByKey(cert.getSubjectX500Principal().getName(), "CN");
                                if (StringUtils.isNotEmpty(endpoint)) {
                                    securityInfo = securityInfoValidator.getEndpointSecurityInfoByCredentialsId(endpoint, CLIENT);
                                }
                            } catch (LwM2MAuthException e) {
                                log.trace("Certificate trust validation failed.", e);
                            }
                        } else {
                            log.trace("Certificate trust validation failed.", exception);
                        }
                    }
                    // if not trust or cert trust securityInfo == null
                    String strCert = SslUtil.getCertificateString(cert);
                    String sha3Hash = EncryptionUtil.getSha3Hash(strCert);
                    if (securityInfo == null || securityInfo.getMsg() == null) {
                        try {
                            securityInfo = securityInfoValidator.getEndpointSecurityInfoByCredentialsId(sha3Hash, CLIENT);
                        } catch (LwM2MAuthException e) {
                            log.trace("Failed find security info: {}", sha3Hash, e);
                        }
                    }
                    ValidateDeviceCredentialsResponse msg = securityInfo != null ? securityInfo.getMsg() : null;
                    if (msg != null && StringUtils.isNotEmpty(msg.getCredentials())) {
                        LwM2MClientCredentials credentials = JacksonUtil.fromString(msg.getCredentials(), LwM2MClientCredentials.class);
                        if (!credentials.getClient().getSecurityConfigClientMode().equals(LwM2MSecurityMode.X509)) {
                            continue;
                        }
                        X509ClientCredential config = (X509ClientCredential) credentials.getClient();
                        String certBody = config.getCert();
                        String endpoint = config.getEndpoint();
                        if (StringUtils.isBlank(certBody) || strCert.equals(certBody)) {
                            x509CredentialsFound = true;
                            DeviceProfile deviceProfile = msg.getDeviceProfile();
                            if (msg.hasDeviceInfo() && deviceProfile != null) {
                                sessionStorage.put(endpoint, new TbX509DtlsSessionInfo(cert.getSubjectX500Principal().getName(), msg));
                                try {
                                    securityStore.putX509(securityInfo);
                                } catch (NonUniqueSecurityInfoException e) {
                                    log.trace("Failed to add security info: {}", securityInfo, e);
                                }
                                break;
                            }
                        } else {
                            log.trace("[{}][{}] Certificate mismatch. Expected: {}, Actual: {}", endpoint, sha3Hash, strCert, certBody);
                        }
                    }
                } catch (CertificateEncodingException | CertificateExpiredException | CertificateNotYetValidException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (!x509CredentialsFound) {
                AlertMessage alert = new AlertMessage(AlertMessage.AlertLevel.FATAL, AlertMessage.AlertDescription.INTERNAL_ERROR);
                throw new HandshakeException("x509 verification not enabled!", alert);
            }
            return new CertificateVerificationResult(cid, certChain, null);
        } catch (HandshakeException e) {
            log.trace("Certificate validation failed!", e);
            return new CertificateVerificationResult(cid, e, null);
        }
    }
}
Also used : CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) PublicKey(java.security.PublicKey) LwM2MAuthException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MAuthException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate) CertificateVerificationResult(org.eclipse.californium.scandium.dtls.CertificateVerificationResult) X509ClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) NonUniqueSecurityInfoException(org.eclipse.leshan.server.security.NonUniqueSecurityInfoException) LwM2MClientCredentials(org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MClientCredentials) CertPath(java.security.cert.CertPath) HandshakeException(org.eclipse.californium.scandium.dtls.HandshakeException) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) AlertMessage(org.eclipse.californium.scandium.dtls.AlertMessage)

Example 14 with DeviceProfile

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

the class TbDeviceProfileNode method getOrCreateDeviceState.

protected DeviceState getOrCreateDeviceState(TbContext ctx, DeviceId deviceId, RuleNodeState rns) {
    DeviceState deviceState = deviceStates.get(deviceId);
    if (deviceState == null) {
        DeviceProfile deviceProfile = cache.get(ctx.getTenantId(), deviceId);
        if (deviceProfile != null) {
            deviceState = new DeviceState(ctx, config, deviceId, new ProfileState(deviceProfile), rns);
            deviceStates.put(deviceId, deviceState);
        }
    }
    return deviceState;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile)

Example 15 with DeviceProfile

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

the class DeviceBulkImportService method setUpLwM2mDeviceProfile.

private DeviceProfile setUpLwM2mDeviceProfile(TenantId tenantId, Device device) {
    DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileByName(tenantId, device.getType());
    if (deviceProfile != null) {
        if (deviceProfile.getTransportType() != DeviceTransportType.LWM2M) {
            deviceProfile.setTransportType(DeviceTransportType.LWM2M);
            deviceProfile.getProfileData().setTransportConfiguration(new Lwm2mDeviceProfileTransportConfiguration());
            deviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
        }
    } else {
        findOrCreateDeviceProfileLock.lock();
        try {
            deviceProfile = deviceProfileService.findDeviceProfileByName(tenantId, device.getType());
            if (deviceProfile == null) {
                deviceProfile = new DeviceProfile();
                deviceProfile.setTenantId(tenantId);
                deviceProfile.setType(DeviceProfileType.DEFAULT);
                deviceProfile.setName(device.getType());
                deviceProfile.setTransportType(DeviceTransportType.LWM2M);
                deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
                DeviceProfileData deviceProfileData = new DeviceProfileData();
                DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
                DeviceProfileTransportConfiguration transportConfiguration = new Lwm2mDeviceProfileTransportConfiguration();
                DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null);
                deviceProfileData.setConfiguration(configuration);
                deviceProfileData.setTransportConfiguration(transportConfiguration);
                deviceProfileData.setProvisionConfiguration(provisionConfiguration);
                deviceProfile.setProfileData(deviceProfileData);
                deviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
            }
        } finally {
            findOrCreateDeviceProfileLock.unlock();
        }
    }
    return deviceProfile;
}
Also used : DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration)

Aggregations

DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)110 Test (org.junit.Test)48 Device (org.thingsboard.server.common.data.Device)30 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)26 List (java.util.List)20 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)19 TbMsg (org.thingsboard.server.common.msg.TbMsg)19 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)18 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)18 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)17 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)17 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)16 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)16 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)16 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)14 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)13 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13 AttributeKvEntity (org.thingsboard.server.dao.model.sql.AttributeKvEntity)13 UUID (java.util.UUID)12