@@ -28,6 +28,8 @@ def parse(self, data):
28
28
graph = networkx .Graph ()
29
29
if 'topology' not in data :
30
30
raise ParserError ('Parse error, "topology" key not found' )
31
+ elif 'mid' not in data :
32
+ raise ParserError ('Parse error, "mid" key not found' )
31
33
32
34
# determine version and revision
33
35
if 'config' in data :
@@ -38,25 +40,36 @@ def parse(self, data):
38
40
version_info [- 1 ] = version_info [- 1 ].split ('hash_' )[- 1 ]
39
41
self .revision = version_info [- 1 ]
40
42
43
+ # process alias list
44
+ alias_dict = {}
45
+ for node in data ['mid' ]:
46
+ local_addresses = [alias ['ipAddress' ] for alias in node ['aliases' ]]
47
+ alias_dict [node ['ipAddress' ]] = local_addresses
48
+
41
49
# loop over topology section and create networkx graph
42
50
for link in data ['topology' ]:
43
51
try :
44
52
source = link ['lastHopIP' ]
45
- dest = link ['destinationIP' ]
53
+ target = link ['destinationIP' ]
46
54
cost = link ['tcEdgeCost' ]
47
55
properties = {
48
56
'link_quality' : link ['linkQuality' ],
49
57
'neighbor_link_quality' : link ['neighborLinkQuality' ]
50
58
}
51
59
except KeyError as e :
52
60
raise ParserError ('Parse error, "%s" key not found' % e )
61
+ # add nodes with their local_addresses
62
+ for node in [source , target ]:
63
+ if node not in alias_dict :
64
+ continue
65
+ graph .add_node (node , local_addresses = alias_dict [node ])
53
66
# skip links with infinite cost
54
67
if cost == float ('inf' ):
55
68
continue
56
69
# original olsrd cost (jsoninfo multiplies by 1024)
57
70
cost = float (cost ) / 1024.0
58
71
# add link to Graph
59
- graph .add_edge (source , dest , weight = cost , ** properties )
72
+ graph .add_edge (source , target , weight = cost , ** properties )
60
73
return graph
61
74
62
75
def _txtinfo_to_jsoninfo (self , data ):
@@ -68,22 +81,44 @@ def _txtinfo_to_jsoninfo(self, data):
68
81
# find interesting section
69
82
lines = data .split ('\n ' )
70
83
84
+ # process links in topology section
71
85
try :
72
86
start = lines .index ('Table: Topology' ) + 2
73
87
end = lines [start :].index ('' ) + start
74
88
except ValueError :
75
89
raise ParserError ('Unrecognized format' )
76
-
77
90
topology_lines = lines [start :end ]
78
- # convert interesting section to jsoninfo format
79
- parsed_lines = []
91
+ # convert topology section to jsoninfo format
92
+ topology = []
80
93
for line in topology_lines :
81
94
values = line .split ('\t ' )
82
- parsed_lines .append ({
95
+ topology .append ({
83
96
'destinationIP' : values [0 ],
84
97
'lastHopIP' : values [1 ],
85
98
'linkQuality' : float (values [2 ]),
86
99
'neighborLinkQuality' : float (values [3 ]),
87
100
'tcEdgeCost' : float (values [4 ]) * 1024.0
88
101
})
89
- return {'topology' : parsed_lines }
102
+
103
+ # process alias (MID) section
104
+ try :
105
+ start = lines .index ('Table: MID' ) + 2
106
+ end = lines [start :].index ('' ) + start
107
+ except ValueError :
108
+ raise ParserError ('Unrecognized format' )
109
+ mid_lines = lines [start :end ]
110
+ # convert mid section to jsoninfo format
111
+ mid = []
112
+ for line in mid_lines :
113
+ values = line .split ('\t ' )
114
+ node = values [0 ]
115
+ aliases = values [1 ].split (';' )
116
+ mid .append ({
117
+ 'ipAddress' : node ,
118
+ 'aliases' : [{'ipAddress' : alias } for alias in aliases ]
119
+ })
120
+
121
+ return {
122
+ 'topology' : topology ,
123
+ 'mid' : mid
124
+ }
0 commit comments