Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions netdiff/parsers/netjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def parse(self, data):
# create graph
for node in data['nodes']:
graph.add_node(node['id'],
label=node['label'] if 'label' in node else None,
local_addresses=node.get('local_addresses', []),
**node.get('properties', {}))
for link in data['links']:
Expand Down
4 changes: 4 additions & 0 deletions netdiff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ def _netjson_networkgraph(protocol, version, revision, metric,
# must copy properties dict to avoid modifying data
properties = node[1].copy()
local_addresses = properties.pop('local_addresses', None)
label = properties.pop('label', 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
# append label only if not empty
if label:
netjson_node['label'] = label
node_list.append(netjson_node)
# prepare links
link_list = []
Expand Down
2 changes: 2 additions & 0 deletions tests/static/netjson-2-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"local_addresses": [
"192.168.1.3"
],
"label": "nodeA",
"properties": {
"hostname": "router.3nnx"
}
Expand All @@ -19,6 +20,7 @@
"local_addresses": [
"192.168.1.2"
],
"label": "nodeB",
"properties": {
"hostname": "router2.nnx"
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_netjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def test_json_dict(self):
self.assertIn('hostname', data['nodes'][0]['properties'])
# ensure local_addresses is present
self.assertIn('local_addresses', data['nodes'][0])
# ensure label is present when present in source
self.assertIn('label', data['nodes'][0])
# ensure label is absent when absent in source
self.assertNotIn('label', data['nodes'][2])
# ensure additional link properties are present
self.assertIn('properties', data['links'][0])
self.assertIn('custom_property', data['links'][0]['properties'])
Expand Down
1 change: 0 additions & 1 deletion tests/test_openvpn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import networkx
import six
from netdiff import OpenvpnParser
from netdiff.tests import TestCase

Expand Down