Browse Source

IoT Hub device package update: the placement of the removed device stays until the new version tracks a device (PROD-9245, review of #16625)

pull/16625/head
dpinkevych 3 weeks ago
parent
commit
4ee5497ac5
  1. 12
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java
  2. 39
      application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java

12
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) { if (descriptor.getDashboardId() == null) {
descriptor.setDashboardId(tracked.getDashboardId()); 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<String, JsonNode> 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) { private IotHubInstalledItem findDevicePackage(TenantId tenantId, IotHubInstalledItemId installedItemId) {

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

@ -641,6 +641,45 @@ class DefaultIotHubServiceTest {
assertThat(next.getDashboardId()).isEqualTo(dashboard); 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 @Test
void registerDeviceInstall_replacedItemGone_savesANewItem() { void registerDeviceInstall_replacedItemGone_savesANewItem() {
mockTenant(); mockTenant();

Loading…
Cancel
Save