From 57f148d50d9157a49f5cdce7abdc8885dd0e7a80 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Mon, 10 Jul 2023 18:18:27 +0200 Subject: [PATCH] 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. --- poor_mans_visualisation.py | 90 +++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/poor_mans_visualisation.py b/poor_mans_visualisation.py index 7a6d2db..7965f92 100755 --- a/poor_mans_visualisation.py +++ b/poor_mans_visualisation.py @@ -3,75 +3,63 @@ # Get topologies import sys -from birdvisu import maps_new -from birdvisu.maps_new import providers +from birdvisu.providers import BirdSocketTopologyProvider, OspfFileTopologyProvider + 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() +ref_topo = OspfFileTopologyProvider(ref_topo_file).get_topology() try: - cur_topo = providers.BirdSocketTopologyProvider().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() + cur_topo = BirdSocketTopologyProvider(instance='ospfv2', area=1).get_topology() +except OSError as e: + raise NotImplementedError('Cannot create a mock topology atm') from e + +# Create combined topology + +from birdvisu.topo_v3 import TopologyV3 + +combined_topology = TopologyV3.combine_topologies(reference=ref_topo, current=cur_topo) +combined_topology.freeze() # 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 import maps_new # annotators.create_qgritems does not like being run without Qt initialization. from PySide6 import QtCore, QtGui, QtWidgets 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: [ - annotators.difference_annotator, annotators.extract_positions, annotators.random_position, annotators.assign_brushes,