use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class BundleContextHistoryManagerTest method verifySearchCriteriaPersistence.
/**
* <p>This method tests the correctness of {@link org.opennms.features.topology.api.support.HistoryAwareSearchProvider#buildCriteriaFromQuery(SearchResult)} method.
* The {@link SearchCriteria} objects, generated by this method, are compared to those created directly, with the use of a corresponding constructor</p>
* <p>
* Additionally, it checks whether the {@link SavedHistory} objects are saved / loaded correctly and whether there is any data loss / distortion
* </p>
*/
@Test
public void verifySearchCriteriaPersistence() {
// Test 1 - checking whether method buildCriteriaFromQuery is working correctly
// Testing for CategoryHopCriteria & CategorySearchProvider
Criteria criterionNew = ((HistoryAwareSearchProvider) this.startingProviders.get(CriteriaTypes.category)).buildCriteriaFromQuery(this.startingSearchResults.get(CriteriaTypes.category));
Assert.assertEquals(this.startingCriteria.get(CriteriaTypes.category), criterionNew);
// Testing for IpLikeHopCriteria & IpLikeSearchProvider
criterionNew = ((HistoryAwareSearchProvider) this.startingProviders.get(CriteriaTypes.ipLike)).buildCriteriaFromQuery(this.startingSearchResults.get(CriteriaTypes.ipLike));
Assert.assertEquals(this.startingCriteria.get(CriteriaTypes.ipLike), criterionNew);
// Testing for AlarmHopCriteria & AlarmSearchProvider
criterionNew = ((HistoryAwareSearchProvider) this.startingProviders.get(CriteriaTypes.alarm)).buildCriteriaFromQuery(this.startingSearchResults.get(CriteriaTypes.alarm));
Assert.assertEquals(this.startingCriteria.get(CriteriaTypes.alarm), criterionNew);
// Step 1 - providing GraphContainer with a starting Criteria set
for (Criteria criteria : this.startingCriteria.values()) {
graphContainerMock.addCriteria(criteria);
}
// Saving and then loading history
String fragment = historyManager.saveOrUpdateHistory("admin", graphContainerMock);
historyManager.applyHistory(fragment, graphContainerMock);
Assert.assertNotNull(capturedCriteria);
Assert.assertThat(startingCriteria.values(), containsInAnyOrder(capturedCriteria.toArray()));
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class BundleContextHistoryManagerTest method setBehaviour.
private void setBehaviour(GraphContainer graphContainerMock) {
MapViewManager mapViewManagerMock = Mockito.mock(MapViewManager.class);
SelectionManager selectionManagerMock = Mockito.mock(SelectionManager.class);
Mockito.when(mapViewManagerMock.getCurrentBoundingBox()).thenReturn(new BoundingBox(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE));
Mockito.when(selectionManagerMock.getSelectedVertexRefs()).thenReturn(new ArrayList<VertexRef>());
Mockito.when(graphContainerMock.getGraph()).thenReturn(graphMock);
Mockito.when(graphContainerMock.getSemanticZoomLevel()).thenReturn(0);
Mockito.when(graphContainerMock.getLayoutAlgorithm()).thenReturn(new CircleLayoutAlgorithm());
Mockito.when(graphContainerMock.getMapViewManager()).thenReturn(mapViewManagerMock);
Mockito.when(graphContainerMock.getSelectionManager()).thenReturn(selectionManagerMock);
Mockito.doAnswer(invocationOnMock -> capturedCriteria.toArray(new Criteria[capturedCriteria.size()])).when(graphContainerMock).getCriteria();
Mockito.doAnswer(invocationOnMock -> {
capturedCriteria.clear();
return null;
}).when(graphContainerMock).clearCriteria();
Mockito.doAnswer(invocation -> {
if (invocation.getArguments()[0] != null && invocation.getArguments()[0] instanceof Criteria) {
capturedCriteria.add((Criteria) invocation.getArguments()[0]);
}
return null;
}).when(graphContainerMock).addCriteria(Matchers.any(Criteria.class));
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class GraphMLEdgeStatusProvider method getStatusForEdges.
@Override
public Map<EdgeRef, Status> getStatusForEdges(EdgeProvider edgeProvider, Collection<EdgeRef> edges, Criteria[] criteria) {
final List<StatusScript> scripts = Lists.newArrayList();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(getScriptPath())) {
for (final Path path : stream) {
// ignore readme
if (".readme".equals(path.getFileName().toString())) {
LOG.debug("Skipping .readme");
continue;
}
final String extension = FilenameUtils.getExtension(path.toString());
final ScriptEngine scriptEngine = this.scriptEngineManager.getEngineByExtension(extension);
if (scriptEngine == null) {
LOG.warn("No script engine found for extension '{}'", extension);
continue;
}
LOG.debug("Found script: path={}, extension={}, engine={}", path, extension, scriptEngine);
try (final Stream<String> lines = Files.lines(path, Charset.defaultCharset())) {
final String source = lines.collect(Collectors.joining("\n"));
scripts.add(new StatusScript(scriptEngine, source));
}
}
} catch (final IOException e) {
LOG.error("Failed to walk template directory: {}", getScriptPath());
return Collections.emptyMap();
}
return serviceAccessor.getTransactionOperations().execute(transactionStatus -> edges.stream().filter(eachEdge -> eachEdge instanceof GraphMLEdge).map(edge -> (GraphMLEdge) edge).map(edge -> new HashMap.SimpleEntry<>(edge, computeEdgeStatus(scripts, edge))).filter(e -> e.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class SavedHistory method apply.
public void apply(GraphContainer graphContainer, Collection<HistoryOperation> operations, ServiceLocator serviceLocator) {
LOG.debug("Applying " + toString());
graphContainer.clearCriteria();
// Apply the history for each registered HistoryOperation
for (HistoryOperation operation : operations) {
try {
operation.applyHistory(graphContainer, m_settings);
} catch (Throwable e) {
LOG.warn("Failed to perform applyHistory() operation", e);
}
}
// Browse through all available search providers that have a "history" functionality
List<SearchProvider> searchProviders = serviceLocator.findServices(SearchProvider.class, null);
for (SearchProvider searchProvider : searchProviders) {
if (searchProvider instanceof HistoryAwareSearchProvider) {
// Add resulting Criteria to the graph container
for (SearchResult searchQuery : m_searchQueries) {
if (searchProvider.getSearchProviderNamespace().equals(searchQuery.getNamespace()) || searchProvider.contributesTo(searchQuery.getNamespace())) {
Criteria searchCriteria = ((HistoryAwareSearchProvider) searchProvider).buildCriteriaFromQuery(searchQuery);
graphContainer.addCriteria(searchCriteria);
}
}
}
}
// Set Vertices in Focus after all other operations are applied, otherwise the topology provider may have changed
// which results in a graphContainer.clearCriteria()
applyVerticesInFocus(m_focusVertices, graphContainer);
applySavedLocations(m_locations, graphContainer.getGraph().getLayout());
graphContainer.setSemanticZoomLevel(getSemanticZoomLevel());
// Apply the selected vertices
graphContainer.getSelectionManager().setSelectedVertexRefs(m_selectedVertices);
graphContainer.getMapViewManager().setBoundingBox(getBoundingBox());
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class SavedHistory method getFocusVertices.
private static Set<VertexRef> getFocusVertices(GraphContainer graphContainer) {
final Set<VertexRef> retVal = new HashSet<>();
Criteria[] criterias = graphContainer.getCriteria();
for (Criteria crit : criterias) {
if (crit instanceof VertexHopGraphProvider.VertexHopCriteria && !(crit instanceof CollapsibleCriteria)) {
retVal.addAll(((VertexHopGraphProvider.VertexHopCriteria) crit).getVertices());
}
}
return retVal;
}
Aggregations