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 dfe46e0aa6..f427262afd 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 @@ -905,6 +905,18 @@ public class DefaultIotHubService implements IotHubService { if (descriptor.getDashboardId() == null) { descriptor.setDashboardId(tracked.getDashboardId()); } + // PE: the wizard registers its own install state after each group of entities. The placement of the removed + // device stays until the new version tracks a device again, so that an update after a failure still creates it + // in the same group and for the same customer. + JsonNode placement = tracked.getInstallState() != null ? tracked.getInstallState().get(PLACEMENT_STATE) : null; + boolean tracksDevice = descriptor.getCreatedEntityIds() != null + && descriptor.getCreatedEntityIds().stream().anyMatch(id -> id.getEntityType() == EntityType.DEVICE); + if (placement != null && !tracksDevice) { + Map installState = descriptor.getInstallState() != null + ? new LinkedHashMap<>(descriptor.getInstallState()) : new LinkedHashMap<>(); + installState.putIfAbsent(PLACEMENT_STATE, placement); + descriptor.setInstallState(installState); + } } private IotHubInstalledItem findDevicePackage(TenantId tenantId, IotHubInstalledItemId installedItemId) { 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 3387f2747a..b40773644b 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 @@ -641,6 +641,45 @@ class DefaultIotHubServiceTest { assertThat(next.getDashboardId()).isEqualTo(dashboard); } + @Test + void registerDeviceInstall_replace_keepsThePlacementUntilADeviceIsTracked() { + mockTenant(); + String itemId = UUID.randomUUID().toString(); + // After the update removed the entities: nothing tracked, the placement of the removed device kept. + ObjectNode placement = JacksonUtil.newObjectNode().put("entityGroupId", UUID.randomUUID().toString()) + .put("customerId", UUID.randomUUID().toString()); + DeviceInstalledItemDescriptor tracked = new DeviceInstalledItemDescriptor(); + tracked.setInstallState(new LinkedHashMap<>(Map.of("Configuration", JacksonUtil.newObjectNode(), "$placement", placement))); + IotHubInstalledItem item = installedItem(UUID.randomUUID()); + item.setItemType("DEVICE"); + item.setItemId(UUID.fromString(itemId)); + item.setDescriptor(tracked); + String newVersionId = UUID.randomUUID().toString(); + when(iotHubInstalledItemService.findById(tenantId, item.getId())).thenReturn(item); + when(iotHubRestClient.getVersionInfo(newVersionId)).thenReturn(version(newVersionId, itemId, "DEVICE", "Sensor", "1.0.2")); + when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); + EntityId profile = new DeviceProfileId(UUID.randomUUID()); + DeviceInstalledItemDescriptor afterProfile = new DeviceInstalledItemDescriptor(); + afterProfile.setCreatedEntityIds(List.of(profile)); + afterProfile.setInstallState(Map.of("Configuration", JacksonUtil.newObjectNode())); + + service.registerDeviceInstall(user, newVersionId, afterProfile, item.getId()); + + // The wizard registered the profile only, with its own install state, and may fail before the device: the + // placement stays for the next update. + assertThat(afterProfile.getInstallState().get("$placement")).isEqualTo(placement); + + EntityId device = new DeviceId(UUID.randomUUID()); + DeviceInstalledItemDescriptor afterDevice = new DeviceInstalledItemDescriptor(); + afterDevice.setCreatedEntityIds(List.of(profile, device)); + afterDevice.setInstallState(Map.of("Configuration", JacksonUtil.newObjectNode())); + + service.registerDeviceInstall(user, newVersionId, afterDevice, item.getId()); + + // The new device is tracked: the next update reads its group and customer from it. + assertThat(afterDevice.getInstallState()).doesNotContainKey("$placement"); + } + @Test void registerDeviceInstall_replacedItemGone_savesANewItem() { mockTenant();