Search in sources :

Example 6 with GraphMLGraph

use of org.opennms.features.graphml.model.GraphMLGraph in project opennms by OpenNMS.

the class AssetGraphGenerator method getPath.

/**
 * Calculates a path through all layers (from top to bottom) to the given <code>node</code>.
 *
 * Ensure that this path exist, otherwise an Exception is thrown.
 *
 * @param node The node to calculate the path to.
 * @param graphs All generated graphs, containing all nodes created.
 * @param layers All layers from which the graphs were created.
 *
 * @return A list of {@link GraphMLNode}s (the path) through all layers to the given <code>node</code>.
 */
private static List<GraphMLNode> getPath(OnmsNode node, List<GraphMLGraph> graphs, List<Layer> layers) {
    // The graphs were created based on the layers. If the count differs, the path cannot be calculated
    if (graphs.size() != layers.size()) {
        throw new IllegalArgumentException("Cannot calculate path. There are more layers than graphs, but the count must be identical");
    }
    final List<GraphMLNode> path = new ArrayList<>();
    for (int i = 0; i < graphs.size(); i++) {
        // the layer graph
        final GraphMLGraph graph = graphs.get(i);
        // the layer definition
        final Layer layer = layers.get(i);
        // the the value for the specified node in that layer (it must exist)
        final Object item = layer.getItemProvider().getItem(node);
        final String itemId = layer.getNodeDecorator().getId(item);
        final String nodeId = layer.getIdGenerator().generateId(layers.subList(0, i), node, itemId);
        final GraphMLNode GraphMlNode = graph.getNodeById(nodeId);
        if (GraphMlNode != null) {
            path.add(GraphMlNode);
        } else {
            throw new IllegalStateException(String.format("Could not find a node with id {} in graph with id {}.", nodeId, graph.getId()));
        }
    }
    return path;
}
Also used : ArrayList(java.util.ArrayList) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) GraphMLNode(org.opennms.features.graphml.model.GraphMLNode) Layer(org.opennms.features.topology.plugins.topo.asset.layers.Layer)

Aggregations

GraphMLGraph (org.opennms.features.graphml.model.GraphMLGraph)6 GraphMLEdge (org.opennms.features.graphml.model.GraphMLEdge)3 GraphMLNode (org.opennms.features.graphml.model.GraphMLNode)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 GraphML (org.opennms.features.graphml.model.GraphML)2 VertexRef (org.opennms.features.topology.api.topo.VertexRef)2 Layer (org.opennms.features.topology.plugins.topo.asset.layers.Layer)2 GraphMLServiceAccessor (org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 InvalidGraphException (org.opennms.features.graphml.model.InvalidGraphException)1