Skip to content

Commit

Permalink
[BatmanParser] Added local_addresses to nodes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Sep 27, 2015
1 parent ae5985b commit 8d9f571
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions netdiff/parsers/batman.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _parse_alfred_vis(self, data):
for node in data["vis"]:
for neigh in node["neighbors"]:
graph.add_node(node['primary'], **{
'local_addresses': node.get('secondary', []),
'clients': node.get('clients', [])
})
primary_neigh = self._get_primary_address(neigh['neighbor'],
Expand Down
4 changes: 4 additions & 0 deletions netdiff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def _netjson_networkgraph(protocol, version, revision, metric,
netjson_node = {'id': node[0]}
# must copy properties dict to avoid modifying data
properties = node[1].copy()
local_addresses = properties.pop('local_addresses', None)
# append local_addresses only if not empty
if local_addresses:
netjson_node['local_addresses'] = local_addresses
# append properties only if not empty
if properties:
netjson_node['properties'] = properties
Expand Down
10 changes: 10 additions & 0 deletions tests/test_batman.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_parse(self):
self.assertIsInstance(properties['weight'], float)
# test additional properties in nodes of networkx graph
properties = p.graph.nodes(data=True)[0][1]
self.assertIsInstance(properties['local_addresses'], list)
self.assertIsInstance(properties['clients'], list)

def test_parse_exception(self):
Expand Down Expand Up @@ -50,10 +51,19 @@ def test_json_dict(self):
found = False
for node in data['nodes']:
if node['id'] == '90:f6:52:f2:8c:2c':
self.assertIsInstance(node['local_addresses'], list)
self.assertIsInstance(node['properties']['clients'], list)
found = True
break
self.assertTrue(found)
found = False
# ensure local_addresses not present if empty
for node in data['nodes']:
if node['id'] == 'a0:f3:c1:96:94:06':
self.assertFalse('local_addresses' in node)
found = True
break
self.assertTrue(found)

def test_json_string(self):
p = BatmanParser(iulinet)
Expand Down

0 comments on commit 8d9f571

Please sign in to comment.