use of org.opennms.features.topology.api.topo.AbstractEdge 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;
}
use of org.opennms.features.topology.api.topo.AbstractEdge in project opennms by OpenNMS.
the class SFreeTopologyProvider method createBARandomTopology.
private void createBARandomTopology(int numberOfNodes, double averageNumberofNeighboors) {
Map<Integer, SimpleLeafVertex> nodes = new HashMap<Integer, SimpleLeafVertex>();
List<AbstractEdge> edges = new ArrayList<AbstractEdge>();
for (int i = 0; i < 2 * averageNumberofNeighboors; i++) {
LOG.debug("Creating First Cluster from: {}", i);
int j = (i + 1) % ((int) Math.round(2 * averageNumberofNeighboors));
SimpleLeafVertex vertexi = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertexi.setIconKey("sfree.system");
vertexi.setLabel("BarabasiAlbertNode" + i);
if (!nodes.containsKey(i)) {
nodes.put(i, vertexi);
LOG.debug("Added Node: {}", vertexi.getId());
}
SimpleLeafVertex vertexj = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(j), 0, 0);
vertexj.setIconKey("sfree.system");
vertexj.setLabel("BarabasiAlbertNode" + j);
if (!nodes.containsKey(j)) {
nodes.put(j, vertexj);
LOG.debug("Added Node: {}", vertexj.getId());
}
String edgeId = "link:" + i + "-" + j;
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(i).getId() + "-" + edgeId + "-connector", nodes.get(i));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(j).getId() + "-" + edgeId + "-connector", nodes.get(j));
edges.add(new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target));
LOG.debug("Added Link: {}", edgeId);
}
Random r = new Random((new Date()).getTime());
for (int i = ((int) Math.floor(2 * averageNumberofNeighboors)); i < numberOfNodes; i++) {
SimpleLeafVertex vertexi = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertexi.setIconKey("sfree.system");
vertexi.setLabel("BarabasiAlbertNode" + i);
nodes.put(i, vertexi);
LOG.debug("Adding Node: {}", i);
for (int times = 0; times < averageNumberofNeighboors; times++) {
AbstractEdge edge;
// choose node to attach to
double d = r.nextDouble() * nodes.size();
LOG.debug("Generated random position: {}", d);
Long j = (long) d;
LOG.debug("Try Adding edge: {}--->{}", j, i);
String edgeId = "link:" + i + "-" + j.intValue();
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(i).getId() + "-" + edgeId + "-connector", nodes.get(i));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(j.intValue()).getId() + "-" + edgeId + "-connector", nodes.get(j.intValue()));
edge = new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target);
if (i == j.intValue())
continue;
edges.add(edge);
}
// m links added
}
addVertices(nodes.values().toArray(new Vertex[] {}));
addEdges(edges.toArray(new Edge[] {}));
}
use of org.opennms.features.topology.api.topo.AbstractEdge in project opennms by OpenNMS.
the class EnhancedLinkdMockDataPopulator method check.
@SuppressWarnings("deprecation")
public void check(GraphProvider topologyProvider) {
String vertexNamespace = topologyProvider.getNamespace();
Assert.assertEquals(8, topologyProvider.getVertices().size());
Assert.assertEquals(9, topologyProvider.getEdges().size());
Assert.assertTrue(topologyProvider.containsVertexId("1"));
Assert.assertTrue(topologyProvider.containsVertexId("2"));
Assert.assertTrue(topologyProvider.containsVertexId("3"));
Assert.assertTrue(topologyProvider.containsVertexId("4"));
Assert.assertTrue(topologyProvider.containsVertexId("5"));
Assert.assertTrue(topologyProvider.containsVertexId("6"));
Assert.assertTrue(topologyProvider.containsVertexId("7"));
Assert.assertTrue(topologyProvider.containsVertexId("8"));
Assert.assertTrue(!topologyProvider.containsVertexId("15"));
Assert.assertEquals(3, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "1")).length);
Assert.assertEquals(3, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "2")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "3")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "4")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "5")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "6")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "7")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "8")).length);
/**
* This is a little hokey because it relies on the fact that edges are only judged to be equal based
* on the namespace and id tuple.
*/
Vertex mockVertex = EasyMock.createMock(Vertex.class);
EasyMock.expect(mockVertex.getId()).andReturn("v0").anyTimes();
EasyMock.expect(mockVertex.getLabel()).andReturn(null).anyTimes();
EasyMock.replay(mockVertex);
AbstractEdge[] edgeidsforvertex1 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10018|10081", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10012|10021", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.OSPF_EDGE_NAMESPACE, "10112|10121", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex2 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10023|10032", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10012|10021", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.OSPF_EDGE_NAMESPACE, "10112|10121", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex3 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10023|10032", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10034|10043", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex4 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10045|10054", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10034|10043", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex5 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10045|10054", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10056|10065", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex6 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10056|10065", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10067|10076", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex7 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10078|10087", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10067|10076", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex8 = { new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10018|10081", mockVertex, mockVertex), new AbstractEdge(EnhancedLinkdTopologyProvider.LLDP_EDGE_NAMESPACE, "10078|10087", mockVertex, mockVertex) };
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "1")), edgeidsforvertex1);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "2")), edgeidsforvertex2);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "3")), edgeidsforvertex3);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "4")), edgeidsforvertex4);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "5")), edgeidsforvertex5);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "6")), edgeidsforvertex6);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "7")), edgeidsforvertex7);
OnmsAssert.assertArrayEqualsIgnoreOrder(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "8")), edgeidsforvertex8);
}
use of org.opennms.features.topology.api.topo.AbstractEdge in project opennms by OpenNMS.
the class EasyMockDataPopulator method check.
@SuppressWarnings("deprecation")
public void check(GraphProvider topologyProvider) {
String vertexNamespace = topologyProvider.getNamespace();
Assert.assertEquals(8, topologyProvider.getVertices().size());
Assert.assertEquals(8, topologyProvider.getEdges().size());
Assert.assertTrue(topologyProvider.containsVertexId("1"));
Assert.assertTrue(topologyProvider.containsVertexId("2"));
Assert.assertTrue(topologyProvider.containsVertexId("3"));
Assert.assertTrue(topologyProvider.containsVertexId("4"));
Assert.assertTrue(topologyProvider.containsVertexId("5"));
Assert.assertTrue(topologyProvider.containsVertexId("6"));
Assert.assertTrue(topologyProvider.containsVertexId("7"));
Assert.assertTrue(topologyProvider.containsVertexId("8"));
Assert.assertTrue(!topologyProvider.containsVertexId("15"));
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "1")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "2")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "3")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "4")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "5")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "6")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "7")).length);
Assert.assertEquals(2, topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "8")).length);
/**
* This is a little hokey because it relies on the fact that edges are only judged to be equal based
* on the namespace and id tuple.
*/
Vertex mockVertex = EasyMock.createMock(Vertex.class);
EasyMock.expect(mockVertex.getId()).andReturn("v0").anyTimes();
EasyMock.expect(mockVertex.getLabel()).andReturn(null).anyTimes();
EasyMock.replay(mockVertex);
AbstractEdge[] edgeidsforvertex1 = { new AbstractEdge("nodes", "10012", mockVertex, mockVertex), new AbstractEdge("nodes", "10081", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex2 = { new AbstractEdge("nodes", "10012", mockVertex, mockVertex), new AbstractEdge("nodes", "10023", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex3 = { new AbstractEdge("nodes", "10023", mockVertex, mockVertex), new AbstractEdge("nodes", "10034", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex4 = { new AbstractEdge("nodes", "10034", mockVertex, mockVertex), new AbstractEdge("nodes", "10045", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex5 = { new AbstractEdge("nodes", "10045", mockVertex, mockVertex), new AbstractEdge("nodes", "10056", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex6 = { new AbstractEdge("nodes", "10056", mockVertex, mockVertex), new AbstractEdge("nodes", "10067", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex7 = { new AbstractEdge("nodes", "10067", mockVertex, mockVertex), new AbstractEdge("nodes", "10078", mockVertex, mockVertex) };
AbstractEdge[] edgeidsforvertex8 = { new AbstractEdge("nodes", "10078", mockVertex, mockVertex), new AbstractEdge("nodes", "10081", mockVertex, mockVertex) };
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "1")), edgeidsforvertex1);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "2")), edgeidsforvertex2);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "3")), edgeidsforvertex3);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "4")), edgeidsforvertex4);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "5")), edgeidsforvertex5);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "6")), edgeidsforvertex6);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "7")), edgeidsforvertex7);
Assert.assertArrayEquals(topologyProvider.getEdgeIdsForVertex(new DefaultVertexRef(vertexNamespace, "8")), edgeidsforvertex8);
}
use of org.opennms.features.topology.api.topo.AbstractEdge in project opennms by OpenNMS.
the class SFreeTopologyProvider method createERRandomTopology.
private void createERRandomTopology(int numberOfNodes, double averageNumberofNeighboors) {
Map<Integer, SimpleLeafVertex> nodes = new HashMap<Integer, SimpleLeafVertex>();
List<AbstractEdge> edges = new ArrayList<AbstractEdge>();
for (Integer i = 0; i < numberOfNodes; i++) {
SimpleLeafVertex vertex = new SimpleLeafVertex(TOPOLOGY_NAMESPACE_SFREE, Integer.toString(i), 0, 0);
vertex.setIconKey("sfree.system");
vertex.setLabel("ErdosReniyNode" + i);
nodes.put(i, vertex);
}
Double z = 0.5 * (numberOfNodes - 1);
// Double p = averageNumberofNeighboors/z;
Random r = new Random((new Date()).getTime());
for (Integer start = 0; start < numberOfNodes; start++) {
for (Integer end = start + 1; end < numberOfNodes; end++) {
if (z * r.nextDouble() < averageNumberofNeighboors) {
String edgeId = "link:" + start + "-" + end;
SimpleConnector source = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(start).getId() + "-" + edgeId + "-connector", nodes.get(start));
SimpleConnector target = new SimpleConnector(TOPOLOGY_NAMESPACE_SFREE, nodes.get(end).getId() + "-" + edgeId + "-connector", nodes.get(end));
edges.add(new AbstractEdge(TOPOLOGY_NAMESPACE_SFREE, edgeId, source, target));
}
}
}
addVertices(nodes.values().toArray(new Vertex[] {}));
addEdges(edges.toArray(new Edge[] {}));
}
Aggregations