diff --git a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java index 315ab25fd3..773514e716 100644 --- a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java +++ b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java @@ -3,8 +3,6 @@ package org.thingsboard.server.service.iot_hub; import org.thingsboard.server.common.data.Dashboard; -import org.thingsboard.server.common.data.OtaPackageInfo; -import org.thingsboard.server.dao.ota.OtaPackageService; import com.fasterxml.jackson.databind.JsonNode; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; @@ -69,6 +67,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; @@ -106,7 +105,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; @@ -784,10 +782,10 @@ 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 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 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 packageEntityIds) { if (packageEntityIds == null || packageEntityIds.isEmpty()) { @@ -815,12 +813,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)) { @@ -885,15 +877,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 DELETE_ORDER = Map.of(EntityType.DEVICE, 0, EntityType.DEVICE_PROFILE, 1, + EntityType.DASHBOARD, 2, EntityType.RULE_CHAIN, 3); + + /** Removes the entities, each before what it refers to. Returns the ones that failed, in order of creation, with the reason. */ private Map deleteDevicePackageEntities(TenantId tenantId, List createdEntityIds, SecurityUser user) { Map failed = new LinkedHashMap<>(); if (createdEntityIds == null || createdEntityIds.isEmpty()) { return failed; } - List 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. Within a type, the reverse of the creation order. + List 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) { diff --git a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java index 69d378243a..e9fff66f19 100644 --- a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java @@ -8,10 +8,6 @@ import org.thingsboard.server.common.data.id.AssetProfileId; import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.rule.RuleChain; -import org.thingsboard.server.common.data.id.OtaPackageId; -import org.thingsboard.server.common.data.ota.OtaPackageType; -import org.thingsboard.server.common.data.OtaPackageInfo; -import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.asset.AssetProfileService; import org.thingsboard.server.dao.rule.RuleChainService; import com.fasterxml.jackson.databind.JsonNode; @@ -93,8 +89,6 @@ class DefaultIotHubServiceTest { @Mock private AssetProfileService assetProfileService; @Mock - private OtaPackageService otaPackageService; - @Mock private DashboardService dashboardService; @Mock private TbDashboardService tbDashboardService; @@ -485,7 +479,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())); @@ -514,8 +508,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); @@ -883,22 +877,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) { @@ -907,7 +906,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; }