Start reimplementing poor_mans_visualisation.py

This script serves as a simple demonstration of how to use birdvisu.
Over time, it should become shorter, as more of its features are
integrated into birdvisu itself, until at last it becomes only a very
thin wrapper.

The functionality will probably be incorporated into some core /
orchestrator birdvisu module, but there is no such thing yet.
styling
LEdoian 1 year ago
parent 266408b99b
commit 57f148d50d

@ -3,75 +3,63 @@
# Get topologies # Get topologies
import sys import sys
from birdvisu import maps_new from birdvisu.providers import BirdSocketTopologyProvider, OspfFileTopologyProvider
from birdvisu.maps_new import providers
ref_topo_file = 'reference.ospf' ref_topo_file = 'reference.ospf'
if len(sys.argv) > 1: if len(sys.argv) > 1:
ref_topo_file = sys.argv[1] ref_topo_file = sys.argv[1]
with open(ref_topo_file) as ref_file: ref_topo = OspfFileTopologyProvider(ref_topo_file).get_topology()
ref_topo = providers.OspfFileTopologyProvider(ref_file).get_topology()
try: try:
cur_topo = providers.BirdSocketTopologyProvider().get_topology() cur_topo = BirdSocketTopologyProvider(instance='ospfv2', area=1).get_topology()
except OSError: except OSError as e:
# HACK! raise NotImplementedError('Cannot create a mock topology atm') from e
import traceback as tb
tb.print_exc(file=sys.stderr) # Create combined topology
print(f'Could not get current topology from BIRD,\n'
'will load reference one with random omissions\n----', file=sys.stderr) from birdvisu.topo_v3 import TopologyV3
with open(ref_topo_file) as ref_file:
cur_topo = providers.OspfFileTopologyProvider(ref_file).get_topology() combined_topology = TopologyV3.combine_topologies(reference=ref_topo, current=cur_topo)
import random combined_topology.freeze()
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 # Annotate it
from birdvisu.topo_v3 import VertexID
from birdvisu.annotations import AnnotatedTopology, AnnotatorID
from birdvisu.annotations.analysis import TopologyDifference, ShortestPathTree
from ipaddress import IPv4Address
annot_topo = AnnotatedTopology(combined_topology)
annotators = [
AnnotatorID(TopologyDifference),
AnnotatorID(ShortestPathTree, (VertexID(
family=None,
is_router=True,
address=None,
router_id=int(IPv4Address('172.23.100.10')),
dr_id=None,
discriminator=None
), 'current')),
]
for ann_id in annotators:
annot_topo.run_annotator(ann_id)
# ---
# Show it
from birdvisu.visualisation import annotators from birdvisu.visualisation import annotators
from birdvisu import maps_new
# annotators.create_qgritems does not like being run without Qt initialization. # annotators.create_qgritems does not like being run without Qt initialization.
from PySide6 import QtCore, QtGui, QtWidgets from PySide6 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication([]) app = QtWidgets.QApplication([])
annotated_topology = maps_new.annotate_topology(final_topo, annotated_topology = maps_new.annotate_topology(combined_topology,
# A semi-canonical set of annotators: # A semi-canonical set of annotators:
[ [
annotators.difference_annotator,
annotators.extract_positions, annotators.extract_positions,
annotators.random_position, annotators.random_position,
annotators.assign_brushes, annotators.assign_brushes,

Loading…
Cancel
Save