You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
birdvisu/poor_mans_visualisation.py

101 lines
2.8 KiB
Python

#!/usr/bin/env python3
# Get topologies
import sys
from birdvisu import maps_new
from birdvisu.maps_new import providers
ref_topo_file = 'reference.ospf'
if len(sys.argv) > 1:
ref_topo_file = sys.argv[1]
with open(ref_topo_file) as ref_file:
ref_topo = providers.OspfFileTopologyProvider(ref_file).get_topology()
try:
cur_topo = providers.RunningBirdTopologyProvider().get_topology()
except OSError:
# HACK!
import traceback as tb
tb.print_exc(file=sys.stderr)
print(f'Could not get current topology from BIRD,\n'
'will load reference one with random omissions\n----', file=sys.stderr)
with open(ref_topo_file) as ref_file:
cur_topo = providers.OspfFileTopologyProvider(ref_file).get_topology()
import random
random.seed('birdvisu_demo')
# Routers:
for _ in range(random.randint(1, 3)):
key, router_to_delete = random.choice(list(cur_topo.routers.items()))
for l in router_to_delete.links:
lk = (l.router.ident, l.network.ident)
del cur_topo.links[lk]
l.network.links.remove(l)
del cur_topo.routers[key]
# Networks:
for _ in range(random.randint(1, 3)):
key, network_to_delete = random.choice(list(cur_topo.networks.items()))
for l in network_to_delete.links:
lk = (l.router.ident, l.network.ident)
del cur_topo.links[lk]
l.router.links.remove(l)
del cur_topo.networks[key]
# Links:
for _ in range(random.randint(1, 3)):
key, link_to_delete = random.choice(list(cur_topo.links.items()))
link_to_delete.router.links.remove(link_to_delete)
link_to_delete.network.links.remove(link_to_delete)
del cur_topo.links[key]
# Combine topologies
combiner = maps_new.TopologyCombiner()
# NOTE: the following string literals convey meaning for annotators, so they
# should not be changed carelessly.
combiner.add_topology('reference', ref_topo)
combiner.add_topology('actual', cur_topo)
final_topo = combiner.get_complete_topology()
# Annotate it
from birdvisu.visualisation import annotators
# annotators.create_qgritems does not like being run without Qt initialization.
from PySide2 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication([])
annotated_topology = maps_new.annotate_topology(final_topo,
# A semi-canonical set of annotators:
[
annotators.difference_annotator,
annotators.extract_positions,
annotators.random_position,
annotators.assign_brushes,
annotators.create_qgritems,
]
)
# Render the widget
scene = QtWidgets.QGraphicsScene()
for tagsrc in [
annotated_topology.router_annotations.values(),
annotated_topology.network_annotations.values(),
annotated_topology.link_annotations.values(),
]:
for taglist in tagsrc:
assert len(taglist) > 0
assert isinstance(taglist[-1], QtWidgets.QGraphicsItem)
scene.addItem(taglist[-1])
view = QtWidgets.QGraphicsView(scene)
view.show()
app.exec_()