Search in sources :

Example 16 with Criteria

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()));
}
Also used : CategoryHopCriteria(org.opennms.features.topology.app.internal.support.CategoryHopCriteria) SearchCriteria(org.opennms.features.topology.api.topo.SearchCriteria) Criteria(org.opennms.features.topology.api.topo.Criteria) IpLikeHopCriteria(org.opennms.features.topology.app.internal.support.IpLikeHopCriteria) AlarmHopCriteria(org.opennms.features.topology.app.internal.support.AlarmHopCriteria) HistoryAwareSearchProvider(org.opennms.features.topology.api.support.HistoryAwareSearchProvider) Test(org.junit.Test)

Example 17 with Criteria

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));
}
Also used : SelectionManager(org.opennms.features.topology.api.SelectionManager) BoundingBox(org.opennms.features.topology.api.BoundingBox) MapViewManager(org.opennms.features.topology.api.MapViewManager) CategoryHopCriteria(org.opennms.features.topology.app.internal.support.CategoryHopCriteria) SearchCriteria(org.opennms.features.topology.api.topo.SearchCriteria) Criteria(org.opennms.features.topology.api.topo.Criteria) IpLikeHopCriteria(org.opennms.features.topology.app.internal.support.IpLikeHopCriteria) AlarmHopCriteria(org.opennms.features.topology.app.internal.support.AlarmHopCriteria) VertexRef(org.opennms.features.topology.api.topo.VertexRef) CircleLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.CircleLayoutAlgorithm)

Example 18 with Criteria

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)));
}
Also used : Path(java.nio.file.Path) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) EdgeStatusProvider(org.opennms.features.topology.api.topo.EdgeStatusProvider) SimpleScriptContext(javax.script.SimpleScriptContext) AbstractVertex(org.opennms.features.topology.api.topo.AbstractVertex) EdgeProvider(org.opennms.features.topology.api.topo.EdgeProvider) DirectoryStream(java.nio.file.DirectoryStream) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) Map(java.util.Map) Path(java.nio.file.Path) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) OnmsNode(org.opennms.netmgt.model.OnmsNode) ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) MeasurementsWrapper(org.opennms.features.topology.api.info.MeasurementsWrapper) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) SimpleConnector(org.opennms.features.topology.api.topo.SimpleConnector) Collectors(java.util.stream.Collectors) ScriptContext(javax.script.ScriptContext) Objects(java.util.Objects) SimpleBindings(javax.script.SimpleBindings) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Criteria(org.opennms.features.topology.api.topo.Criteria) CompiledScript(javax.script.CompiledScript) ScriptEngine(javax.script.ScriptEngine) Optional(java.util.Optional) Status(org.opennms.features.topology.api.topo.Status) Collections(java.util.Collections) FilenameUtils(org.apache.commons.io.FilenameUtils) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 19 with Criteria

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());
}
Also used : HistoryOperation(org.opennms.features.topology.api.HistoryOperation) SearchProvider(org.opennms.features.topology.api.topo.SearchProvider) SearchResult(org.opennms.features.topology.api.topo.SearchResult) CollapsibleCriteria(org.opennms.features.topology.api.topo.CollapsibleCriteria) SearchCriteria(org.opennms.features.topology.api.topo.SearchCriteria) Criteria(org.opennms.features.topology.api.topo.Criteria)

Example 20 with Criteria

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;
}
Also used : CollapsibleCriteria(org.opennms.features.topology.api.topo.CollapsibleCriteria) CollapsibleCriteria(org.opennms.features.topology.api.topo.CollapsibleCriteria) SearchCriteria(org.opennms.features.topology.api.topo.SearchCriteria) Criteria(org.opennms.features.topology.api.topo.Criteria) VertexRef(org.opennms.features.topology.api.topo.VertexRef) HashSet(java.util.HashSet)

Aggregations

Criteria (org.opennms.features.topology.api.topo.Criteria)36 VertexHopCriteria (org.opennms.features.topology.api.support.VertexHopGraphProvider.VertexHopCriteria)15 CollapsibleCriteria (org.opennms.features.topology.api.topo.CollapsibleCriteria)13 VertexRef (org.opennms.features.topology.api.topo.VertexRef)12 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 List (java.util.List)5 SearchCriteria (org.opennms.features.topology.api.topo.SearchCriteria)5 CategoryHopCriteria (org.opennms.features.topology.app.internal.support.CategoryHopCriteria)5 Lists (com.google.common.collect.Lists)4 Set (java.util.Set)4 Test (org.junit.Test)4 DefaultVertexRef (org.opennms.features.topology.api.topo.DefaultVertexRef)4 Status (org.opennms.features.topology.api.topo.Status)4 AlarmHopCriteria (org.opennms.features.topology.app.internal.support.AlarmHopCriteria)4 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 AlarmSummary (org.opennms.netmgt.model.alarm.AlarmSummary)4 Collection (java.util.Collection)3