Search in sources :

Example 1 with IgnoreHopCriteria

use of org.opennms.features.topology.api.support.IgnoreHopCriteria 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)

Aggregations

UnweightedShortestPath (edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath)1 DirectedSparseGraph (edu.uci.ics.jung.graph.DirectedSparseGraph)1 IgnoreHopCriteria (org.opennms.features.topology.api.support.IgnoreHopCriteria)1 AbstractEdge (org.opennms.features.topology.api.topo.AbstractEdge)1 AbstractVertex (org.opennms.features.topology.api.topo.AbstractVertex)1 Edge (org.opennms.features.topology.api.topo.Edge)1 EdgeRef (org.opennms.features.topology.api.topo.EdgeRef)1 Vertex (org.opennms.features.topology.api.topo.Vertex)1 VertexRef (org.opennms.features.topology.api.topo.VertexRef)1