From 4df83aba1bf47bc45a96fe67fa7077fce4313e27 Mon Sep 17 00:00:00 2001 From: dpinkevych Date: Wed, 11 Nov 2026 17:08:32 +0200 Subject: [PATCH] IoT Hub: review of #16553 by hand: the removal check covers dashboards and firmware, the connector step owns no entity (PROD-9245) - A dashboard of the package that a profile outside the package uses as its default dashboard, and a device profile of the package that a firmware or software package refers to, refuse the update before anything is removed. - The refusal no longer says that nothing was removed: an earlier attempt may have removed part of the package. - The GATEWAY_CONNECTOR step neither marks the gateway as created in this wizard nor tracks it, so a gateway the user had and chose to use stays theirs after Back reruns the steps. - The doc of packageOwnedEntityIds is back above it; tests for the rule chain, the dashboard and the firmware. --- .../service/iot_hub/DefaultIotHubService.java | 43 ++++- .../iot_hub/DefaultIotHubServiceTest.java | 147 +++++++++++++++++- .../device-install-dialog.component.ts | 7 +- 3 files changed, 186 insertions(+), 11 deletions(-) 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 b8b6320ed9..78d7b6b80c 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,6 +3,8 @@ // 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; @@ -125,6 +127,7 @@ 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; @@ -921,15 +924,13 @@ public class DefaultIotHubService implements IotHubService { } } - /** - * The entities the package created: the tracked ones without those the user chose, on a name conflict, to use as they - * were (the install state keeps that choice). The default device profile, for example, is never the package's. - */ /** * The update removes the package's entities one by one. ThingsBoard refuses to delete a device profile that is the - * default one or that devices outside the package use, a converter that an integration outside the package uses, - * 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: a 400 removes nothing. + * 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. */ private void checkPackageEntitiesCanBeRemoved(TenantId tenantId, List packageEntityIds) { if (packageEntityIds == null || packageEntityIds.isEmpty()) { @@ -957,6 +958,27 @@ 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)) { + continue; + } + for (DeviceProfile profile : new PageDataIterable<>(link -> deviceProfileService.findDeviceProfiles(tenantId, link), 1000)) { + if (dashboard.getId().equals(profile.getDefaultDashboardId()) && !owned.contains(profile.getId().getId())) { + blockers.add("the dashboard '" + dashboard.getTitle() + "' is the default dashboard of the device profile '" + profile.getName() + "'"); + } + } + for (AssetProfile profile : new PageDataIterable<>(link -> assetProfileService.findAssetProfiles(tenantId, link), 1000)) { + if (dashboard.getId().equals(profile.getDefaultDashboardId())) { + blockers.add("the dashboard '" + dashboard.getTitle() + "' is the default dashboard of the asset profile '" + profile.getName() + "'"); + } + } } else if (entityId.getEntityType() == EntityType.CONVERTER) { Converter converter = converterService.findConverterById(tenantId, new ConverterId(entityId.getId())); if (!isOwnedBy(converter, tenantId)) { @@ -988,11 +1010,16 @@ public class DefaultIotHubService implements IotHubService { } } if (!blockers.isEmpty()) { + // Not "nothing was removed": an earlier attempt may have removed part of the package already. throw new IllegalArgumentException("The package can't be updated: " + String.join("; ", blockers) - + ". Move them to other profiles or rule chains first, or delete them. Nothing was removed."); + + ". Change or delete them first."); } } + /** + * The entities the package created: the tracked ones without those the user chose, on a name conflict, to use as they + * were (the install state keeps that choice). The default device profile, for example, is never the package's. + */ private static List packageOwnedEntityIds(DeviceInstalledItemDescriptor descriptor) { List tracked = descriptor.getCreatedEntityIds(); if (tracked == null || descriptor.getInstallState() == null) { 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 bfbf16794e..6b87889815 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 @@ -2,6 +2,18 @@ // SPDX-License-Identifier: Apache-2.0 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; +import org.thingsboard.server.common.data.id.AssetProfileId; +import static org.mockito.Mockito.lenient; import org.thingsboard.server.dao.converter.ConverterService; import org.thingsboard.server.dao.integration.IntegrationService; import org.thingsboard.server.common.data.id.IntegrationId; @@ -80,6 +92,14 @@ class DefaultIotHubServiceTest { @Mock private TbDeviceService tbDeviceService; @Mock + private OtaPackageService otaPackageService; + @Mock + private AssetProfileService assetProfileService; + @Mock + private TbRuleChainService tbRuleChainService; + @Mock + private RuleChainService ruleChainService; + @Mock private IntegrationService integrationService; @Mock private ConverterService converterService; @@ -786,7 +806,7 @@ class DefaultIotHubServiceTest { assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("'Package profile' is used by 1 other device(s)") - .hasMessageContaining("Nothing was removed"); + .hasMessageContaining("Change or delete them first"); verify(tbDeviceService, never()).delete(any(Device.class), any()); verify(iotHubInstalledItemService, never()).save(any(), any()); } @@ -824,12 +844,137 @@ class DefaultIotHubServiceTest { verify(tbDeviceProfileService).delete(profile, user); } + @Test + void removeDevicePackageEntities_rootRuleChain_refusedBeforeRemoving() { + mockTenant(); + RuleChain ruleChain = packageRuleChain(true); + IotHubInstalledItem item = devicePackage(ruleChain.getId()); + String newVersionId = mockNewVersion(item); + + assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("'Package rule chain' is the root rule chain"); + verify(iotHubInstalledItemService, never()).save(any(), any()); + } + + @Test + void removeDevicePackageEntities_ruleChainOfAnotherProfile_refusedBeforeRemoving() { + mockTenant(); + RuleChain ruleChain = packageRuleChain(false); + DeviceProfile other = otherProfile("User profile"); + other.setDefaultRuleChainId(ruleChain.getId()); + when(deviceProfileService.findDeviceProfiles(eq(tenantId), any())).thenReturn(page(other)); + IotHubInstalledItem item = devicePackage(ruleChain.getId()); + String newVersionId = mockNewVersion(item); + + assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("'Package rule chain' is the rule chain of the device profile 'User profile'"); + verify(iotHubInstalledItemService, never()).save(any(), any()); + } + + @Test + void removeDevicePackageEntities_ruleChainOfThePackageProfile_removed() throws Exception { + mockTenant(); + RuleChain ruleChain = packageRuleChain(false); + DeviceProfile profile = packageProfile(false); + profile.setDefaultRuleChainId(ruleChain.getId()); + when(deviceProfileService.findDeviceProfiles(eq(tenantId), any())).thenReturn(page(profile)); + when(deviceService.findDeviceIdsByTenantIdAndDeviceProfileId(eq(tenantId), eq(profile.getId()), any())).thenReturn(page()); + IotHubInstalledItem item = devicePackage(ruleChain.getId(), profile.getId()); + String newVersionId = mockNewVersion(item); + + service.removeDevicePackageEntities(user, item.getId(), newVersionId); + + verify(tbDeviceProfileService).delete(profile, user); + verify(tbRuleChainService).delete(ruleChain, user); + } + + @Test + void removeDevicePackageEntities_ruleChainOfAnAssetProfile_refusedBeforeRemoving() { + mockTenant(); + RuleChain ruleChain = packageRuleChain(false); + AssetProfile assetProfile = new AssetProfile(new AssetProfileId(UUID.randomUUID())); + assetProfile.setName("Building"); + assetProfile.setDefaultRuleChainId(ruleChain.getId()); + when(assetProfileService.findAssetProfiles(eq(tenantId), any())).thenReturn(page(assetProfile)); + IotHubInstalledItem item = devicePackage(ruleChain.getId()); + String newVersionId = mockNewVersion(item); + + assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("'Package rule chain' is the rule chain of the asset profile 'Building'"); + } + + @Test + void removeDevicePackageEntities_dashboardOfAnotherProfile_refusedBeforeRemoving() { + mockTenant(); + Dashboard dashboard = new Dashboard(new DashboardId(UUID.randomUUID())); + dashboard.setTenantId(tenantId); + dashboard.setTitle("Package dashboard"); + when(dashboardService.findDashboardById(tenantId, dashboard.getId())).thenReturn(dashboard); + DeviceProfile other = otherProfile("User profile"); + other.setDefaultDashboardId(dashboard.getId()); + when(deviceProfileService.findDeviceProfiles(eq(tenantId), any())).thenReturn(page(other)); + lenient().when(assetProfileService.findAssetProfiles(eq(tenantId), any())).thenReturn(page()); + IotHubInstalledItem item = devicePackage(dashboard.getId()); + String newVersionId = mockNewVersion(item); + + // Deleted first (reverse order), the dashboard would fail after nothing else, and the rest after it: refused now. + assertThatThrownBy(() -> service.removeDevicePackageEntities(user, item.getId(), newVersionId)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("'Package dashboard' is the default dashboard of the device profile 'User profile'"); + verify(tbDashboardService, never()).delete(any(), any()); + } + + @Test + void removeDevicePackageEntities_profileOfAFirmware_refusedBeforeRemoving() { + mockTenant(); + DeviceProfile profile = packageProfile(false); + 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()); + 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'"); + } + private DeviceProfile packageProfile(boolean isDefault) { DeviceProfile profile = new DeviceProfile(new DeviceProfileId(UUID.randomUUID())); profile.setTenantId(tenantId); 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; + } + + private static PageData page(T... items) { + return new PageData<>(List.of(items), 1, items.length, false); + } + + private RuleChain packageRuleChain(boolean root) { + RuleChain ruleChain = new RuleChain(new RuleChainId(UUID.randomUUID())); + ruleChain.setTenantId(tenantId); + ruleChain.setName("Package rule chain"); + ruleChain.setRoot(root); + when(ruleChainService.findRuleChainById(tenantId, ruleChain.getId())).thenReturn(ruleChain); + lenient().when(deviceProfileService.findDeviceProfiles(eq(tenantId), any())).thenReturn(page()); + lenient().when(assetProfileService.findAssetProfiles(eq(tenantId), any())).thenReturn(page()); + return ruleChain; + } + + private DeviceProfile otherProfile(String name) { + DeviceProfile profile = new DeviceProfile(new DeviceProfileId(UUID.randomUUID())); + profile.setTenantId(tenantId); + profile.setName(name); return profile; } diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts b/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts index 8ad5fc9c76..7e2c0c5843 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts @@ -835,10 +835,13 @@ export class TbDeviceInstallDialogComponent extends DialogComponent