Browse Source

IoT Hub device package update: no OTA check (the firmware goes with its profile), delete each entity before what it refers to (PROD-9245, by hand from #16553)

pull/16625/head
dpinkevych 3 weeks ago
parent
commit
c4ef3a6a6f
  1. 34
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java
  2. 38
      application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java

34
application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java

@ -3,8 +3,6 @@
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.service.iot_hub;
import org.thingsboard.server.dao.ota.OtaPackageService;
import org.thingsboard.server.common.data.OtaPackageInfo;
import org.thingsboard.server.common.data.page.PageDataIterable;
import org.thingsboard.server.common.data.converter.Converter;
import org.thingsboard.server.common.data.integration.Integration;
@ -89,6 +87,7 @@ import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.LinkedHashMap;
@ -127,7 +126,6 @@ public class DefaultIotHubService implements IotHubService {
private final IotHubInstalledItemService iotHubInstalledItemService;
private final WidgetTypeService widgetTypeService;
private final DashboardService dashboardService;
private final OtaPackageService otaPackageService;
private final CalculatedFieldService calculatedFieldService;
private final DeviceProfileService deviceProfileService;
private final DeviceService deviceService;
@ -926,11 +924,11 @@ public class DefaultIotHubService implements IotHubService {
/**
* The update removes the package's entities one by one. ThingsBoard refuses to delete a device profile that is the
* default one, that devices outside the package use or that a firmware or software package refers to; a converter
* that an integration outside the package uses; a dashboard that a profile outside the package uses as its default
* dashboard; and a rule chain that is the root one or the default rule chain of a profile outside the package. Found
* after the first deletes, such a refusal would leave the package half removed and fail every Retry the same way, so
* it is checked before anything is removed.
* default one or that devices outside the package use; a converter that an integration outside the package uses; a
* dashboard that a profile outside the package uses as its default dashboard; and a rule chain that is the root one
* or the default rule chain of a profile outside the package. (The firmware and software of a profile are deleted
* with it.) Found after the first deletes, such a refusal would leave the package half removed and fail every Retry
* the same way, so it is checked before anything is removed.
*/
private void checkPackageEntitiesCanBeRemoved(TenantId tenantId, List<EntityId> packageEntityIds) {
if (packageEntityIds == null || packageEntityIds.isEmpty()) {
@ -958,12 +956,6 @@ public class DefaultIotHubService implements IotHubService {
if (otherDevices > 0) {
blockers.add("the device profile '" + profile.getName() + "' is used by " + otherDevices + " other device(s)");
}
for (OtaPackageInfo ota : new PageDataIterable<>(link -> otaPackageService.findTenantOtaPackagesByTenantId(tenantId, link), 1000)) {
if (profile.getId().equals(ota.getDeviceProfileId())) {
blockers.add("the device profile '" + profile.getName() + "' is used by the " + ota.getType().name().toLowerCase()
+ " '" + ota.getTitle() + " " + ota.getVersion() + "'");
}
}
} else if (entityId.getEntityType() == EntityType.DASHBOARD) {
Dashboard dashboard = dashboardService.findDashboardById(tenantId, new DashboardId(entityId.getId()));
if (!isOwnedBy(dashboard, tenantId)) {
@ -1038,15 +1030,21 @@ public class DefaultIotHubService implements IotHubService {
return tracked.stream().filter(id -> !usedExisting.contains(id.getId().toString())).toList();
}
/** Removes the entities in reverse order of creation. Returns the ones that failed, in order of creation, with the reason. */
private static final Map<EntityType, Integer> DELETE_ORDER = Map.of(EntityType.DEVICE, 0, EntityType.DEVICE_PROFILE, 1,
EntityType.DASHBOARD, 2, EntityType.RULE_CHAIN, 3, EntityType.INTEGRATION, 4, EntityType.CONVERTER, 5);
/** Removes the entities, each before what it refers to. Returns the ones that failed, in order of creation, with the reason. */
private Map<EntityId, String> deleteDevicePackageEntities(TenantId tenantId, List<EntityId> createdEntityIds, SecurityUser user) {
Map<EntityId, String> failed = new LinkedHashMap<>();
if (createdEntityIds == null || createdEntityIds.isEmpty()) {
return failed;
}
List<EntityId> reversed = new ArrayList<>(createdEntityIds);
Collections.reverse(reversed);
for (EntityId entityId : reversed) {
// An entity goes before what it refers to: a device before its profile, a profile before its default dashboard
// and rule chain, an integration before its converters. Within a type, the reverse of the creation order.
List<EntityId> ordered = new ArrayList<>(createdEntityIds);
Collections.reverse(ordered);
ordered.sort(Comparator.comparingInt(id -> DELETE_ORDER.getOrDefault(id.getEntityType(), DELETE_ORDER.size())));
for (EntityId entityId : ordered) {
try {
deleteDevicePackageEntity(tenantId, entityId, user);
} catch (Exception e) {

38
application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java

@ -5,10 +5,6 @@ package org.thingsboard.server.service.iot_hub;
import org.thingsboard.server.dao.asset.AssetProfileService;
import org.thingsboard.server.service.rule.TbRuleChainService;
import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.dao.ota.OtaPackageService;
import org.thingsboard.server.common.data.OtaPackageInfo;
import org.thingsboard.server.common.data.ota.OtaPackageType;
import org.thingsboard.server.common.data.id.OtaPackageId;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.asset.AssetProfile;
@ -92,8 +88,6 @@ class DefaultIotHubServiceTest {
@Mock
private TbDeviceService tbDeviceService;
@Mock
private OtaPackageService otaPackageService;
@Mock
private AssetProfileService assetProfileService;
@Mock
private TbRuleChainService tbRuleChainService;
@ -496,7 +490,7 @@ class DefaultIotHubServiceTest {
}
@Test
void removeDevicePackageEntities_removesEntitiesInReverseOrderAndKeepsTheItemVersion() throws Exception {
void removeDevicePackageEntities_removesTheDeviceBeforeTheDashboardAndKeepsTheItemVersion() throws Exception {
mockTenant();
Device device = new Device(new DeviceId(UUID.randomUUID()));
Dashboard dashboard = new Dashboard(new DashboardId(UUID.randomUUID()));
@ -525,8 +519,8 @@ class DefaultIotHubServiceTest {
service.removeDevicePackageEntities(user, item.getId(), newVersionId);
InOrder order = inOrder(tbDashboardService, tbDeviceService, iotHubInstalledItemService);
order.verify(tbDashboardService).delete(dashboard, user);
order.verify(tbDeviceService).delete(device, user);
order.verify(tbDashboardService).delete(dashboard, user);
order.verify(iotHubInstalledItemService).save(tenantId, item);
// The item keeps its version until the wizard registers the new one, and lists nothing to remove again.
assertThat(item.getItemVersionId()).isEqualTo(versionId);
@ -928,22 +922,27 @@ class DefaultIotHubServiceTest {
}
@Test
void removeDevicePackageEntities_profileOfAFirmware_refusedBeforeRemoving() {
void removeDevicePackageEntities_dashboardOfThePackageProfile_profileDeletedFirst() throws Exception {
mockTenant();
DeviceProfile profile = packageProfile(false);
Dashboard dashboard = new Dashboard(new DashboardId(UUID.randomUUID()));
dashboard.setTenantId(tenantId);
dashboard.setTitle("Package dashboard");
profile.setDefaultDashboardId(dashboard.getId());
when(dashboardService.findDashboardById(tenantId, dashboard.getId())).thenReturn(dashboard);
when(deviceProfileService.findDeviceProfiles(eq(tenantId), any())).thenReturn(page(profile));
lenient().when(assetProfileService.findAssetProfiles(eq(tenantId), any())).thenReturn(page());
when(deviceService.findDeviceIdsByTenantIdAndDeviceProfileId(eq(tenantId), eq(profile.getId()), any())).thenReturn(page());
OtaPackageInfo firmware = new OtaPackageInfo(new OtaPackageId(UUID.randomUUID()));
firmware.setDeviceProfileId(profile.getId());
firmware.setType(OtaPackageType.FIRMWARE);
firmware.setTitle("fw");
firmware.setVersion("1.2");
when(otaPackageService.findTenantOtaPackagesByTenantId(eq(tenantId), any())).thenReturn(page(firmware));
IotHubInstalledItem item = devicePackage(profile.getId());
// Created in this order by the wizard: the profile, then the dashboard
IotHubInstalledItem item = devicePackage(profile.getId(), dashboard.getId());
String newVersionId = mockNewVersion(item);
assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("'Package profile' is used by the firmware 'fw 1.2'");
service.removeDevicePackageEntities(user, item.getId(), newVersionId);
// The profile refers to the dashboard: deleting the dashboard first would fail on fk_default_dashboard_device_profile
InOrder order = inOrder(tbDeviceProfileService, tbDashboardService);
order.verify(tbDeviceProfileService).delete(profile, user);
order.verify(tbDashboardService).delete(dashboard, user);
}
private DeviceProfile packageProfile(boolean isDefault) {
@ -952,7 +951,6 @@ class DefaultIotHubServiceTest {
profile.setName("Package profile");
profile.setDefault(isDefault);
when(deviceProfileService.findDeviceProfileById(tenantId, profile.getId())).thenReturn(profile);
lenient().when(otaPackageService.findTenantOtaPackagesByTenantId(eq(tenantId), any())).thenReturn(page());
return profile;
}

Loading…
Cancel
Save