1
1
import json
2
2
import networkx
3
+ from netdiff .nxparser import Parser
3
4
4
5
5
- class Olsr1Parser (object ):
6
+ class Olsr1Parser (Parser ):
6
7
""" OLSR v1 Topology Parser """
7
-
8
- def __init__ (self , old , new ):
9
- """
10
- Initializes a new Olsr1Parser
11
-
12
- :param str old: a JSON or dict representing the old OLSR1 topology
13
- :param str new: a JSON or dict representing the new OLSR1 topology
14
- """
15
- self .old_graph = self ._parse (old )
16
- self .new_graph = self ._parse (new )
17
-
18
- def diff (self ):
19
- """
20
- Returns netdiff in a python dictionary
21
- """
22
- return {
23
- "added" : self ._make_diff (self .new_graph , self .old_graph ),
24
- "removed" : self ._make_diff (self .old_graph , self .new_graph )
25
- }
26
-
27
- def diff_json (self , ** kwargs ):
28
- """
29
- Returns netdiff in a JSON string
30
- """
31
- return json .dumps (self .diff (), ** kwargs )
32
-
33
- # --- private methods --- #
34
-
35
8
def _parse (self , topology ):
36
9
"""
37
10
Converts a topology in a NetworkX MultiGraph object.
@@ -50,21 +23,3 @@ def _parse(self, topology):
50
23
link ["destinationIP" ],
51
24
weight = link ["tcEdgeCost" ])
52
25
return graph
53
-
54
- def _make_diff (self , old , new ):
55
- """
56
- calculates differences between topologies 'old' and 'new'
57
- returns a list of links
58
- """
59
- # make a copy of old topology to avoid tampering with it
60
- diff = old .copy ()
61
- not_different = []
62
- # loop over all links
63
- for edge in old .edges ():
64
- # if link is also in new topology add it to the list
65
- if edge in new .edges ():
66
- not_different .append (edge )
67
- # keep only differences
68
- diff .remove_edges_from (not_different )
69
- # return list of links
70
- return diff .edges ()
0 commit comments