AsyncInventoryStorage implements InventoryStorage { @Override public <L, S extends Session<L>> void receivedEvent(InventoryEvent<L, S> event) { try { MonitoredEndpoint<EndpointConfiguration> endpoint = event.getEndpointService().getMonitoredEndpoint(); log.debugf("Received inventory event for endpoint: %s", endpoint); long timestamp = System.currentTimeMillis(); List<RawResource> importResources = new ArrayList<>(); List<org.hawkular.inventory.api.model.ResourceType> importTypes = new ArrayList<>(); Inventory importData = new Inventory(importResources, importTypes); if (event.getResourceTypeManager().isPresent()) { ResourceTypeManager<L> resourceTypeManager = event.getResourceTypeManager().get(); List<ResourceType<L>> allResourceTypes = resourceTypeManager.getResourceTypesBreadthFirst(); for (ResourceType<L> rt : allResourceTypes) { if (rt.isPersisted()) { continue; } log.debugf("Updating resource type: %s", rt.getID().getIDString()); org.hawkular.inventory.api.model.ResourceType.Builder rtb = org.hawkular.inventory.api.model.ResourceType .builder(); rtb.id(getInventoryId(rt)); rt.getProperties().forEach((k, v) -> { rtb.property(k, v.toString()); }); for (Operation<L> op : rt.getOperations()) { org.hawkular.inventory.api.model.Operation.Builder ob = org.hawkular.inventory.api.model.Operation .builder(); ob.name(op.getName().getNameString()); for (OperationParam param : op.getParameters()) { Map<String, String> metadata = new HashMap<>(); metadata.put("description", param.getDescription()); metadata.put("defaultValue", param.getDefaultValue()); metadata.put("type", param.getType()); ob.parameter(param.getName(), metadata); } rtb.operation(ob.build()); } org.hawkular.inventory.api.model.ResourceType resourceType = rtb.build(); log.debugf("Adding resource type: %s", resourceType); importTypes.add(resourceType); rt.setPersistedTime(timestamp); } } event.getAddedOrModified().forEach(r -> { log.debugf("Updating resource: %s", r.getID().getIDString()); addResourceToImport(r, importResources); r.setPersistedTime(timestamp); r.getMetrics().forEach(m -> m.setPersistedTime(timestamp)); }); if (!event.getRemoved().isEmpty()) { List<String> resourcesToRemove = event.getRemoved().stream() .map(r -> r.getID().getIDString()) .collect(Collectors.toList()); log.debugf("Removing resources: %s", resourcesToRemove); deleteInventoryData(resourcesToRemove); } if (!importResources.isEmpty() || !importTypes.isEmpty()) { importInventoryData(importData); } } catch (Exception e) { log.errorf(e, "Failed to process inventory event"); } } AsyncInventoryStorage(
            String feedId,
            StorageAdapterConfiguration config,
            HttpClientBuilder httpClientBuilder,
            Diagnostics diagnostics); void shutdown(); @Override void receivedEvent(InventoryEvent<L, S> event);  }
@Test public void testDiscoverySequence() throws InterruptedException { storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, resourceManager.getResourcesBreadthFirst(), Collections.emptyList())); Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size()); expectCalls(collectedPostCalls, "http: final long initialTime = R_1.getPersistedTime(); Assert.assertTrue(initialTime > 0); Assert.assertEquals(initialTime, R_2.getPersistedTime()); Assert.assertEquals(initialTime, RT_1.getPersistedTime()); Thread.sleep(10); storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, Collections.emptyList(), Collections.emptyList())); Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size()); Assert.assertEquals(0, collectedPostCalls.size()); Assert.assertEquals(initialTime, RT_1.getPersistedTime()); Assert.assertEquals(initialTime, R_1.getPersistedTime()); Thread.sleep(10); Resource<AnyLocation> r3 = Resource.<AnyLocation>builder() .id(new ID("r3")) .name(new Name("Resource 3")) .location(new AnyLocation("/2/2/3")) .type(RT_2) .parent(R_2) .build(); resourceManager.addResource(r3); storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, Collections.singletonList(r3), Collections.emptyList())); Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size()); expectCalls(collectedPostCalls, "http: Assert.assertEquals(initialTime, RT_1.getPersistedTime()); Assert.assertEquals(initialTime, R_1.getPersistedTime()); final long intermediateR2Time = R_2.getPersistedTime(); Thread.sleep(10); resourceManager.removeResource(R_1); storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, Collections.emptyList(), Collections.singletonList(R_1))); expectCalls(collectedDeleteCalls, "http: Assert.assertEquals(0, collectedPostCalls.size()); Assert.assertEquals(initialTime, RT_1.getPersistedTime()); Assert.assertEquals(intermediateR2Time, R_2.getPersistedTime()); Thread.sleep(10); resourceManager.removeResource(r3); storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, Collections.emptyList(), Collections.singletonList(r3))); Assert.assertEquals(0, collectedPostCalls.size()); expectCalls(collectedDeleteCalls, "http: Assert.assertEquals(initialTime, RT_1.getPersistedTime()); Assert.assertEquals(intermediateR2Time, R_2.getPersistedTime()); }
@Test public void testNonDiscoverySequence() { storage.receivedEvent(InventoryEvent.discovery( endpointService, resourceManager, resourceTypeManager, resourceManager.getResourcesBreadthFirst(), Collections.emptyList())); Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size()); expectCalls(collectedPostCalls, "http: Resource<AnyLocation> r3 = Resource.<AnyLocation>builder() .id(new ID("r3")) .name(new Name("Resource 3")) .location(new AnyLocation("/2/2/3")) .type(RT_2) .parent(R_2) .build(); resourceManager.addResource(r3); storage.receivedEvent(InventoryEvent.addedOrModified( endpointService, resourceManager, Collections.singletonList(r3))); Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size()); expectCalls(collectedPostCalls, "http: resourceManager.removeResource(R_1); storage.receivedEvent(InventoryEvent.removed( endpointService, resourceManager, Collections.singletonList(R_1))); expectCalls(collectedDeleteCalls, "http: Assert.assertEquals(0, collectedPostCalls.size()); resourceManager.removeResource(r3); storage.receivedEvent(InventoryEvent.removed( endpointService, resourceManager, Collections.singletonList(r3))); expectCalls(collectedDeleteCalls, "http: Assert.assertEquals(0, collectedPostCalls.size()); }