-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholsr.py
More file actions
115 lines (104 loc) · 3.96 KB
/
olsr.py
File metadata and controls
115 lines (104 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import networkx
from netdiff.parsers.base import BaseParser
from netdiff.exceptions import ParserError, ConversionException
class OlsrParser(BaseParser):
""" OLSR 1 jsoninfo parser """
protocol = 'OLSR'
version = '0.8'
metric = 'ETX'
def to_python(self, data):
"""
Adds support for txtinfo format
"""
try:
return super(OlsrParser, self).to_python(data)
except ConversionException as e:
return self._txtinfo_to_jsoninfo(e.data)
def parse(self, data):
"""
Converts a dict representing an OLSR 0.6.x topology
to a NetworkX Graph object, which is then returned.
Additionally checks for "config" data in order to determine version and revision.
"""
graph = networkx.Graph()
if 'topology' not in data:
raise ParserError('Parse error, "topology" key not found')
# process alias list
alias_dict = {}
if 'mid' in data:
for node in data['mid']:
local_addresses = [alias['ipAddress']
for alias in node['aliases']]
alias_dict[node['ipAddress']] = local_addresses
# loop over topology section and create networkx graph
for link in data['topology']:
try:
source = link['lastHopIP']
target = link['destinationIP']
cost = link['tcEdgeCost']
properties = {
'link_quality': link['linkQuality'],
'neighbor_link_quality': link['neighborLinkQuality']
}
except KeyError as e:
raise ParserError('Parse error, "%s" key not found' % e)
# add nodes with their local_addresses
for node in [source, target]:
if node not in alias_dict:
continue
graph.add_node(node, local_addresses=alias_dict[node])
# skip links with infinite cost
if cost == float('inf'):
continue
# original olsrd cost (jsoninfo multiplies by 1024)
cost = float(cost) / 1024.0
# add link to Graph
graph.add_edge(source, target, weight=cost, **properties)
return graph
def _txtinfo_to_jsoninfo(self, data):
"""
converts olsr 1 txtinfo format to jsoninfo
"""
# replace INFINITE with inf, which is convertible to float
data = data.replace('INFINITE', 'inf')
# find interesting section
lines = data.split('\n')
# process links in topology section
try:
start = lines.index('Table: Topology') + 2
end = lines[start:].index('') + start
except ValueError:
raise ParserError('Unrecognized format')
topology_lines = lines[start:end]
# convert topology section to jsoninfo format
topology = []
for line in topology_lines:
values = line.split('\t')
topology.append({
'destinationIP': values[0],
'lastHopIP': values[1],
'linkQuality': float(values[2]),
'neighborLinkQuality': float(values[3]),
'tcEdgeCost': float(values[4]) * 1024.0
})
# process alias (MID) section
try:
start = lines.index('Table: MID') + 2
end = lines[start:].index('') + start
except ValueError:
raise ParserError('Unrecognized format')
mid_lines = lines[start:end]
# convert mid section to jsoninfo format
mid = []
for line in mid_lines:
values = line.split('\t')
node = values[0]
aliases = values[1].split(';')
mid.append({
'ipAddress': node,
'aliases': [{'ipAddress': alias} for alias in aliases]
})
return {
'topology': topology,
'mid': mid
}