Search in sources :

Example 16 with EdgeRef

use of org.opennms.features.topology.api.topo.EdgeRef in project opennms by OpenNMS.

the class ISOMLayoutAlgorithm method updateLayout.

@Override
public void updateLayout(final Graph graph) {
    final Layout graphLayout = graph.getLayout();
    SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>();
    Collection<? extends Vertex> vertices = graph.getDisplayVertices();
    for (Vertex v : vertices) {
        jungGraph.addVertex(v);
    }
    Collection<? extends Edge> edges = graph.getDisplayEdges();
    for (Edge e : edges) {
        jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
    }
    NonStupidISOMLayout layout = new NonStupidISOMLayout(jungGraph, graphLayout);
    layout.setInitializer(initializer(graphLayout));
    layout.setSize(selectLayoutSize(graph));
    while (!layout.done()) {
        layout.step();
    }
    for (Vertex v : vertices) {
        graphLayout.setLocation(v, new Point(layout.getX(v), layout.getY(v)));
    }
}
Also used : SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Vertex(org.opennms.features.topology.api.topo.Vertex) ISOMLayout(edu.uci.ics.jung.algorithms.layout.ISOMLayout) Layout(org.opennms.features.topology.api.Layout) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) Point(org.opennms.features.topology.api.Point) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Example 17 with EdgeRef

use of org.opennms.features.topology.api.topo.EdgeRef 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 18 with EdgeRef

use of org.opennms.features.topology.api.topo.EdgeRef in project opennms by OpenNMS.

the class BreadcrumbPathCalculator method getIncomingEdgeMap.

static Map<VertexRef, EdgeRef> getIncomingEdgeMap(TopologyServiceClient topologyServiceClient) {
    // Convert to JUNG graph
    // We build one big graph out of all graph providers in order to determine the shortest path between each vertex
    // when we want to calculate the SHORTEST_PATH_TO_ROOT
    final DirectedSparseGraph<VertexRef, EdgeRef> sparseGraph = new DirectedSparseGraph<>();
    topologyServiceClient.getGraphProviders().forEach(eachGraph -> {
        for (Vertex eachVertex : eachGraph.getVertices(new IgnoreHopCriteria())) {
            sparseGraph.addVertex(eachVertex);
        }
        for (EdgeRef eachEdge : eachGraph.getEdges()) {
            sparseGraph.addEdge(eachEdge, ((Edge) eachEdge).getSource().getVertex(), ((Edge) eachEdge).getTarget().getVertex());
        }
    });
    // Link the layers
    final IdGenerator idGenerator = new IdGenerator();
    sparseGraph.getVertices().forEach(eachVertex -> {
        topologyServiceClient.getOppositeVertices(eachVertex).forEach(oppositeVertex -> {
            sparseGraph.addEdge(new AbstractEdge("$$outer-space$$", "" + idGenerator.nextId(), eachVertex, oppositeVertex), eachVertex, oppositeVertex);
        });
    });
    // Create dummy root
    sparseGraph.addVertex(rootVertex);
    for (Vertex eachVertex : topologyServiceClient.getDefaultGraphProvider().getVertices(new IgnoreHopCriteria())) {
        sparseGraph.addEdge(new AbstractEdge("$$outer-space$$", "" + idGenerator.nextId(), rootVertex, eachVertex), rootVertex, eachVertex);
    }
    // Build shortest path for graph
    final UnweightedShortestPath<VertexRef, EdgeRef> shortestPath = new UnweightedShortestPath<>(sparseGraph);
    Map<VertexRef, EdgeRef> incomingEdgeMap = shortestPath.getIncomingEdgeMap(rootVertex);
    return incomingEdgeMap;
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) AbstractVertex(org.opennms.features.topology.api.topo.AbstractVertex) AbstractEdge(org.opennms.features.topology.api.topo.AbstractEdge) IgnoreHopCriteria(org.opennms.features.topology.api.support.IgnoreHopCriteria) UnweightedShortestPath(edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge) AbstractEdge(org.opennms.features.topology.api.topo.AbstractEdge) DirectedSparseGraph(edu.uci.ics.jung.graph.DirectedSparseGraph)

Example 19 with EdgeRef

use of org.opennms.features.topology.api.topo.EdgeRef in project opennms by OpenNMS.

the class D3LayoutTest method testD3Layout.

@Test
public void testD3Layout() throws IOException {
    Graph g = m_graphContainer.getGraph();
    List<Vertex> vertices = new ArrayList<>(g.getDisplayVertices());
    D3TopoLayout<VertexRef, EdgeRef> layout = runD3Layout(1, g, g.getLayout(), vertices);
    Vertex v1 = vertices.get(0);
    Vertex v2 = vertices.get(1);
    Vertex v3 = vertices.get(2);
    double distance = calcDistance(layout, v1, v2);
    double distance2 = calcDistance(layout, v2, v3);
    double distance3 = calcDistance(layout, v1, v3);
    LOG.info("distance: " + distance);
    LOG.info("distance2: " + distance2);
    LOG.info("distance3: " + distance3);
    D3TopoLayout<VertexRef, EdgeRef> layout2 = runD3Layout(2, g, g.getLayout(), vertices);
    distance = calcDistance(layout2, v1, v2);
    distance2 = calcDistance(layout2, v2, v3);
    distance3 = calcDistance(layout2, v1, v3);
    LOG.info("distance: " + distance);
    LOG.info("distance2: " + distance2);
    LOG.info("distance3: " + distance3);
    D3TopoLayout<VertexRef, EdgeRef> layout3 = runD3Layout(3, g, g.getLayout(), vertices);
    distance = calcDistance(layout3, v1, v2);
    distance2 = calcDistance(layout3, v2, v3);
    distance3 = calcDistance(layout3, v1, v3);
    LOG.info("distance: " + distance);
    LOG.info("distance2: " + distance2);
    LOG.info("distance3: " + distance3);
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Graph(org.opennms.features.topology.api.Graph) ArrayList(java.util.ArrayList) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Test(org.junit.Test)

Example 20 with EdgeRef

use of org.opennms.features.topology.api.topo.EdgeRef in project opennms by OpenNMS.

the class D3LayoutTest method createJungGraph.

private SparseGraph<VertexRef, EdgeRef> createJungGraph(Graph g) {
    SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<>();
    Collection<Vertex> vertices = g.getDisplayVertices();
    for (Vertex v : vertices) {
        jungGraph.addVertex(v);
    }
    Collection<Edge> edges = g.getDisplayEdges();
    for (Edge e : edges) {
        jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
    }
    return jungGraph;
}
Also used : SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Vertex(org.opennms.features.topology.api.topo.Vertex) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Aggregations

EdgeRef (org.opennms.features.topology.api.topo.EdgeRef)30 VertexRef (org.opennms.features.topology.api.topo.VertexRef)18 Vertex (org.opennms.features.topology.api.topo.Vertex)14 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 Edge (org.opennms.features.topology.api.topo.Edge)11 SparseGraph (edu.uci.ics.jung.graph.SparseGraph)10 Status (org.opennms.features.topology.api.topo.Status)10 Point (org.opennms.features.topology.api.Point)9 Dimension (java.awt.Dimension)6 Layout (org.opennms.features.topology.api.Layout)6 FRLayout (edu.uci.ics.jung.algorithms.layout.FRLayout)3 SpringLayout (edu.uci.ics.jung.algorithms.layout.SpringLayout)3 AbstractVertex (org.opennms.features.topology.api.topo.AbstractVertex)3 HashMap (java.util.HashMap)2 Graph (org.opennms.features.topology.api.Graph)2 Predicate (com.google.common.base.Predicate)1 Lists (com.google.common.collect.Lists)1 ISOMLayout (edu.uci.ics.jung.algorithms.layout.ISOMLayout)1 UnweightedShortestPath (edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath)1