Browse Source

Improve Default Device Profile creation

pull/6536/head
Andrii Shvaika 4 years ago
parent
commit
c96b70fcb4
  1. 3
      dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java
  2. 11
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java
  3. 27
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java
  4. 5
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  5. 1
      dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java
  6. 12
      dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java
  7. 7
      dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileServiceImpl.java

3
dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -129,6 +129,7 @@ public class BaseAssetService extends AbstractCachedEntityService<AssetCacheKey,
savedAsset = assetDao.save(asset.getTenantId(), asset); savedAsset = assetDao.save(asset.getTenantId(), asset);
publishEvictEvent(evictEvent); publishEvictEvent(evictEvent);
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(evictEvent);
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("asset_name_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("asset_name_unq_key")) {
throw new DataValidationException("Asset with such name already exists!"); throw new DataValidationException("Asset with such name already exists!");

11
dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -103,15 +103,16 @@ public class DeviceCredentialsServiceImpl extends AbstractCachedEntityService<St
formatCredentials(deviceCredentials); formatCredentials(deviceCredentials);
log.trace("Executing updateDeviceCredentials [{}]", deviceCredentials); log.trace("Executing updateDeviceCredentials [{}]", deviceCredentials);
credentialsValidator.validate(deviceCredentials, id -> tenantId); credentialsValidator.validate(deviceCredentials, id -> tenantId);
DeviceCredentials oldDeviceCredentials = null;
if (deviceCredentials.getDeviceId() != null) {
oldDeviceCredentials = deviceCredentialsDao.findByDeviceId(tenantId, deviceCredentials.getDeviceId().getId());
}
try { try {
DeviceCredentials oldDeviceCredentials = null;
if (deviceCredentials.getDeviceId() != null) {
oldDeviceCredentials = deviceCredentialsDao.findByDeviceId(tenantId, deviceCredentials.getDeviceId().getId());
}
var value = deviceCredentialsDao.saveAndFlush(tenantId, deviceCredentials); var value = deviceCredentialsDao.saveAndFlush(tenantId, deviceCredentials);
publishEvictEvent(new DeviceCredentialsEvictEvent(value.getCredentialsId(), oldDeviceCredentials != null ? oldDeviceCredentials.getCredentialsId() : null)); publishEvictEvent(new DeviceCredentialsEvictEvent(value.getCredentialsId(), oldDeviceCredentials != null ? oldDeviceCredentials.getCredentialsId() : null));
return value; return value;
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(new DeviceCredentialsEvictEvent(deviceCredentials.getCredentialsId(), oldDeviceCredentials != null ? oldDeviceCredentials.getCredentialsId() : null));
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null if (e != null && e.getConstraintName() != null
&& (e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key") || e.getConstraintName().equalsIgnoreCase("device_credentials_device_id_unq_key"))) { && (e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key") || e.getConstraintName().equalsIgnoreCase("device_credentials_device_id_unq_key"))) {

27
dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -57,6 +57,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; private static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
private static final String INCORRECT_DEVICE_PROFILE_ID = "Incorrect deviceProfileId "; private static final String INCORRECT_DEVICE_PROFILE_ID = "Incorrect deviceProfileId ";
private static final String INCORRECT_DEVICE_PROFILE_NAME = "Incorrect deviceProfileName "; private static final String INCORRECT_DEVICE_PROFILE_NAME = "Incorrect deviceProfileName ";
private static final String DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS = "Device profile with such name already exists!";
@Autowired @Autowired
private DeviceProfileDao deviceProfileDao; private DeviceProfileDao deviceProfileDao;
@ -76,8 +77,10 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
@Override @Override
public void handleEvictEvent(DeviceProfileEvictEvent event) { public void handleEvictEvent(DeviceProfileEvictEvent event) {
List<DeviceProfileCacheKey> keys = new ArrayList<>(2); List<DeviceProfileCacheKey> keys = new ArrayList<>(2);
keys.add(DeviceProfileCacheKey.fromId(event.getDeviceProfileId()));
keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getNewName())); keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getNewName()));
if (event.getDeviceProfileId() != null) {
keys.add(DeviceProfileCacheKey.fromId(event.getDeviceProfileId()));
}
if (event.isDefaultProfile()) { if (event.isDefaultProfile()) {
keys.add(DeviceProfileCacheKey.defaultProfile(event.getTenantId())); keys.add(DeviceProfileCacheKey.defaultProfile(event.getTenantId()));
} }
@ -117,19 +120,21 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
DeviceProfile savedDeviceProfile; DeviceProfile savedDeviceProfile;
try { try {
savedDeviceProfile = deviceProfileDao.saveAndFlush(deviceProfile.getTenantId(), deviceProfile); savedDeviceProfile = deviceProfileDao.saveAndFlush(deviceProfile.getTenantId(), deviceProfile);
publishEvictEvent(new DeviceProfileEvictEvent(savedDeviceProfile.getTenantId(), savedDeviceProfile.getName(),
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, savedDeviceProfile.getId(), savedDeviceProfile.isDefault()));
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(),
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, null, deviceProfile.isDefault()));
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) {
//TODO: refactor this to return existing device profile. If they are equal - no need to throw exception. Then we can make this call @Transactional and tests will not fail. //TODO: refactor this to return existing device profile. If they are equal - no need to throw exception. Then we can make this call @Transactional and tests will not fail.
throw new DataValidationException("Device profile with such name already exists!"); throw new DataValidationException(DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS);
} else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_provision_key_unq_key")) { } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_provision_key_unq_key")) {
throw new DataValidationException("Device profile with such provision device key already exists!"); throw new DataValidationException("Device profile with such provision device key already exists!");
} else { } else {
throw t; throw t;
} }
} }
publishEvictEvent(new DeviceProfileEvictEvent(savedDeviceProfile.getTenantId(), savedDeviceProfile.getName(),
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, savedDeviceProfile.getId(), savedDeviceProfile.isDefault()));
if (oldDeviceProfile != null && !oldDeviceProfile.getName().equals(deviceProfile.getName())) { if (oldDeviceProfile != null && !oldDeviceProfile.getName().equals(deviceProfile.getName())) {
PageLink pageLink = new PageLink(100); PageLink pageLink = new PageLink(100);
PageData<Device> pageData; PageData<Device> pageData;
@ -195,14 +200,14 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
log.trace("Executing findOrCreateDefaultDeviceProfile"); log.trace("Executing findOrCreateDefaultDeviceProfile");
DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name); DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name);
if (deviceProfile == null) { if (deviceProfile == null) {
findOrCreateLock.lock();
try { try {
deviceProfile = findDeviceProfileByName(tenantId, name); deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default"));
if (deviceProfile == null) { } catch (DataValidationException e) {
deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default")); if (DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS.equals(e.getMessage())) {
deviceProfile = findDeviceProfileByName(tenantId, name);
} else {
throw e;
} }
} finally {
findOrCreateLock.unlock();
} }
} }
return deviceProfile; return deviceProfile;

5
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -239,10 +239,9 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
publishEvictEvent(deviceCacheEvictEvent); publishEvictEvent(deviceCacheEvictEvent);
return result; return result;
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(deviceCacheEvictEvent);
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
// remove device from cache in case null value cached in the distributed redis.
handleEvictEvent(deviceCacheEvictEvent);
throw new DataValidationException("Device with such name already exists!"); throw new DataValidationException("Device with such name already exists!");
} else { } else {
throw t; throw t;

1
dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java

@ -158,6 +158,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
publishEvictEvent(evictEvent); publishEvictEvent(evictEvent);
return savedEdge; return savedEdge;
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(evictEvent);
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null if (e != null && e.getConstraintName() != null
&& e.getConstraintName().equalsIgnoreCase("edge_name_unq_key")) { && e.getConstraintName().equalsIgnoreCase("edge_name_unq_key")) {

12
dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -77,14 +77,17 @@ public class BaseOtaPackageService extends AbstractCachedEntityService<OtaPackag
throw new DataValidationException("Ota package URL should be specified!"); throw new DataValidationException("Ota package URL should be specified!");
} }
otaPackageInfoValidator.validate(otaPackageInfo, OtaPackageInfo::getTenantId); otaPackageInfoValidator.validate(otaPackageInfo, OtaPackageInfo::getTenantId);
OtaPackageId otaPackageId = otaPackageInfo.getId();
try { try {
OtaPackageId otaPackageId = otaPackageInfo.getId();
var result = otaPackageInfoDao.save(otaPackageInfo.getTenantId(), otaPackageInfo); var result = otaPackageInfoDao.save(otaPackageInfo.getTenantId(), otaPackageInfo);
if (otaPackageId != null) { if (otaPackageId != null) {
publishEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId)); publishEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId));
} }
return result; return result;
} catch (Exception t) { } catch (Exception t) {
if (otaPackageId != null) {
handleEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId));
}
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) {
throw new DataValidationException("OtaPackage with such title and version already exists!"); throw new DataValidationException("OtaPackage with such title and version already exists!");
@ -99,14 +102,17 @@ public class BaseOtaPackageService extends AbstractCachedEntityService<OtaPackag
public OtaPackage saveOtaPackage(OtaPackage otaPackage) { public OtaPackage saveOtaPackage(OtaPackage otaPackage) {
log.trace("Executing saveOtaPackage [{}]", otaPackage); log.trace("Executing saveOtaPackage [{}]", otaPackage);
otaPackageValidator.validate(otaPackage, OtaPackageInfo::getTenantId); otaPackageValidator.validate(otaPackage, OtaPackageInfo::getTenantId);
OtaPackageId otaPackageId = otaPackage.getId();
try { try {
OtaPackageId otaPackageId = otaPackage.getId();
var result = otaPackageDao.save(otaPackage.getTenantId(), otaPackage); var result = otaPackageDao.save(otaPackage.getTenantId(), otaPackage);
if (otaPackageId != null) { if (otaPackageId != null) {
publishEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId)); publishEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId));
} }
return result; return result;
} catch (Exception t) { } catch (Exception t) {
if (otaPackageId != null) {
handleEvictEvent(new OtaPackageCacheEvictEvent(otaPackageId));
}
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) {
throw new DataValidationException("OtaPackage with such title and version already exists!"); throw new DataValidationException("OtaPackage with such title and version already exists!");

7
dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileServiceImpl.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -57,7 +57,9 @@ public class TenantProfileServiceImpl extends AbstractCachedEntityService<Tenant
@Override @Override
public void handleEvictEvent(TenantProfileEvictEvent event) { public void handleEvictEvent(TenantProfileEvictEvent event) {
List<TenantProfileCacheKey> keys = new ArrayList<>(2); List<TenantProfileCacheKey> keys = new ArrayList<>(2);
keys.add(TenantProfileCacheKey.fromId(event.getTenantProfileId())); if (event.getTenantProfileId() != null) {
keys.add(TenantProfileCacheKey.fromId(event.getTenantProfileId()));
}
if (event.isDefaultProfile()) { if (event.isDefaultProfile()) {
keys.add(TenantProfileCacheKey.defaultProfile()); keys.add(TenantProfileCacheKey.defaultProfile());
} }
@ -89,6 +91,7 @@ public class TenantProfileServiceImpl extends AbstractCachedEntityService<Tenant
savedTenantProfile = tenantProfileDao.save(tenantId, tenantProfile); savedTenantProfile = tenantProfileDao.save(tenantId, tenantProfile);
publishEvictEvent(new TenantProfileEvictEvent(savedTenantProfile.getId(), savedTenantProfile.isDefault())); publishEvictEvent(new TenantProfileEvictEvent(savedTenantProfile.getId(), savedTenantProfile.isDefault()));
} catch (Exception t) { } catch (Exception t) {
handleEvictEvent(new TenantProfileEvictEvent(null, tenantProfile.isDefault()));
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("tenant_profile_name_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("tenant_profile_name_unq_key")) {
throw new DataValidationException("Tenant profile with such name already exists!"); throw new DataValidationException("Tenant profile with such name already exists!");

Loading…
Cancel
Save