Netdiff is an experimental Python library that provides utilities for parsing network topologies of open source dynamic routing protocols and calculating changes in these topologies.
It was developed to abstract the differences between the different JSON structures of the open source dynamic routing protocols (like OLSR and batman-advanced).
Install from pypi:
pip install netdiff
Install tarball:
pip install https://github.com/ninuxorg/netdiff/tarball/master
Alternatively you can install via pip using git:
pip install -e git+git://github.com/ninuxorg/netdiff#egg=netdiff
If you want to contribute, install your cloned fork:
git clone git@github.com:<your_fork>/netdiff.git
cd netdiff
python setup.py develop
Calculate diff of an OLSR 0.6.x topology:
from netdiff import OlsrParser
from netdiff import diff
stored = OlsrParser('./stored-olsr.json')
latest = OlsrParser('telnet://127.0.0.1:9090')
diff(stored, latest)
The output will be a dictionary with the following structure:
{
"added": []
"removed": []
}
Parsers are classes that extend netdiff.base.BaseParser
and implement a parse
method
which is in charge of converting a python data structure into networkx.Graph
object.
Parsers also have a json
method which returns valid NetJSON output.
The available parsers are:
netdiff.OlsrParser
: parser for the olsrd jsoninfo pluginnetdiff.BatmanParser
: parser for the batman-advanced alfred toolnetdiff.Bmx6Parser
: parser for the BMX6 b6m toolnetdiff.CnmlParser
: parser for CNML 0.1netdiff.NetJsonParser
: parser for theNetworkGraph
NetJSON object.
Parsers must be initialized with a string which can represent one of the following:
- path to JSON file
- HTTP url to JSON file
- telnet url to JSON file
- JSON formatted string
- python dictionary representing a JSON structure
Local file example:
from netdiff import BatmanParser
BatmanParser('./my-stored-topology.json')
HTTP example:
from netdiff import NetJsonParser
url = 'https://raw.githubusercontent.com/interop-dev/json-for-networks/master/examples/network-graph.json'
NetJsonParser(url)
Netdiff parsers can return a valid NetJSON
NetworkGraph
object:
from netdiff import OlsrParser
olsr = OlsrParser('telnet://127.0.0.1:9090')
# will return a dict
olsr.json(dict=True)
# will return a JSON formatted string
print(olsr.json(indent=4))
Output:
{
"type": "NetworkGraph",
"protocol": "OLSR",
"version": "0.6.6",
"revision": "5031a799fcbe17f61d57e387bc3806de",
"metric": "ETX",
"nodes": [
{
"id": "10.150.0.3"
},
{
"id": "10.150.0.2"
},
{
"id": "10.150.0.4"
}
],
"links": [
{
"source": "10.150.0.3",
"target": "10.150.0.2",
"weight": 2.4
},
{
"source": "10.150.0.3",
"target": "10.150.0.4",
"weight": 1.0
}
]
}
Install your forked repo:
git clone git://github.com/<your_fork>/netdiff
cd netdiff/
python setup.py develop
Install test requirements:
pip install -r requirements-test.txt
Run tests with:
./runtests.py
Alternatively, you can use the nose
command (which has a ton of available options):
nosetests
nosetests tests.olsr
nosetests tests.olsr:TestOlsrParser
nosetests tests.olsr:TestOlsrParser.test_parse
See test coverage with:
coverage run --source=netdiff runtests.py && coverage report
- Join the ninux-dev mailing list
- Fork this repo and install it
- Follow PEP8, Style Guide for Python Code
- Write code
- Write tests for your code
- Ensure all tests pass
- Ensure test coverage is not under 90%
- Document your changes
- Send pull request