Skip to main content

Calculates a diff of a network topology

Project description

https://travis-ci.org/ninuxorg/netdiff.png https://coveralls.io/repos/ninuxorg/netdiff/badge.png Code Health Requirements Status https://badge.fury.io/py/netdiff.png https://img.shields.io/pypi/dm/netdiff.svg

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).

It’s currently also used in Nodeshot to update the network links that are shown on the map.

If you are a developer of another community network node-db project and you want to use netdiff to update the topology stored in your database, please get in touch!

Install stable version from pypi

Install from pypi:

pip install netdiff

Install development version

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

Basic Usage Example

Calculate diff of an OLSR 0.6.x topology:

from netdiff import OlsrParser
from netdiff import diff

old = OlsrParser('./stored-olsr.json')
new = OlsrParser('telnet://127.0.0.1:9090')
diff(old, new)

In alternative, you may also use the subtraction operator:

from netdiff import OlsrParser
from netdiff import diff

old = OlsrParser('./stored-olsr.json')
new = OlsrParser('telnet://127.0.0.1:9090')
old - new

The output will be an ordered dictionary with three keys:

  • added

  • removed

  • changed

Each key will contain a dict compatible with the NetJSON NetworkGraph format representing respectively:

  • the nodes and links that have been added to the topology

  • the nodes and links that have been removed from the topology

  • links that are present in both topologies but their weight changed

If no changes are present, keys will contain None.

So if between old and new there are no changes, the result will be:

{
    "added": None
    "removed": None,
    "changed": None
}

While if there are changes, the result will look like:

{
    "added": {
        "type": "NetworkGraph",
        "protocol": "OLSR",
        "version": "0.6.6",
        "revision": "5031a799fcbe17f61d57e387bc3806de",
        "metric": "ETX",
        "nodes": [
            {
                "id": "10.150.0.7"
            },
            {
                "id": "10.150.0.6"
            }
        ],
        "links": [
            {
                "source": "10.150.0.3",
                "target": "10.150.0.7",
                "weight": 1.50390625
            },
            {
                "source": "10.150.0.3",
                "target": "10.150.0.6",
                "weight": 1.0
            }
        ]
    },
    "removed": {
        "type": "NetworkGraph",
        "protocol": "OLSR",
        "version": "0.6.6",
        "revision": "5031a799fcbe17f61d57e387bc3806de",
        "metric": "ETX",
        "nodes": [
            {
                "id": "10.150.0.8"
            }
        ],
        "links": [
            {
                "source": "10.150.0.7",
                "target": "10.150.0.8",
                "weight": 1.0
            }
        ]
    },
    "changed": {
        "type": "NetworkGraph",
        "protocol": "OLSR",
        "version": "0.6.6",
        "revision": "5031a799fcbe17f61d57e387bc3806de",
        "metric": "ETX",
        "nodes": [],
        "links": [
            {
                "source": "10.150.0.3",
                "target": "10.150.0.2",
                "weight": 1.0
            }
        ]
    }
}

Parsers

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:

Initialization arguments

data: the only required argument, different inputs are accepted:

  • JSON formatted string representing the topology

  • python dict (or subclass of dict) representing the topology

  • string representing a HTTP URL where the data resides

  • string representing a telnet URL where the data resides

  • string representing a file path where the data resides

timeout: integer representing timeout in seconds for HTTP or telnet requests, defaults to None

verify: boolean indicating to the request library whether to do SSL certificate verification or not

Initialization examples

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/netjson/master/examples/network-graph.json'
NetJsonParser(url)

Telnet example with timeout:

from netdiff import OlsrParser
OlsrParser('telnet://127.0.1:8080', timeout=5)

HTTPS example with self-signed SSL certificate using verify=False:

from netdiff import NetJsonParser
OlsrParser('https://myserver.mydomain.com/topology.json', verify=False)

NetJSON output

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
        }
    ]
}

Exceptions

All the exceptions are subclasses of netdiff.exceptions.NetdiffException.

ConversionException

netdiff.exceptions.ConversionException

Raised when netdiff can’t recognize the format passed to the parser.

Not necessarily an error, should be caught and managed in order to support additional formats.

The data which was retrieved from network/storage can be accessed via the “data” attribute, eg:

def to_python(self, data):
    try:
        return super(OlsrParser, self).to_python(data)
    except ConversionException as e:
        return self._txtinfo_to_jsoninfo(e.data)

ParserError

netdiff.exceptions.ParserError

Raised when the format is recognized but the data is invalid.

NetJsonError

netdiff.exceptions.NetJsonError

Raised when the json method of netdiff.parsers.BaseParser does not have enough data to be compliant with the NetJSON NetworkGraph specification.

TopologyRetrievalError

netdiff.exceptions.TopologyRetrievalError

Raised when it is not possible to retrieve the topology data (eg: the URL might be temporary unreachable).

Running tests

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.test_olsr  # run only olsr related tests
nosetests tests/test_olsr.py  # variant form of the previous command
nosetests tests.test_olsr:TestOlsrParser  # variant form of the previous command
nosetests tests.test_olsr:TestOlsrParser.test_parse  # run specific test

See test coverage with:

coverage run --source=netdiff runtests.py && coverage report

Contributing

  1. Join the ninux-dev mailing list

  2. Fork this repo and install it

  3. Follow PEP8, Style Guide for Python Code

  4. Write code

  5. Write tests for your code

  6. Ensure all tests pass

  7. Ensure test coverage is not under 90%

  8. Document your changes

  9. Send pull request

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netdiff-0.4.2.tar.gz (71.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

netdiff-0.4.2-py2.py3-none-any.whl (17.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file netdiff-0.4.2.tar.gz.

File metadata

  • Download URL: netdiff-0.4.2.tar.gz
  • Upload date:
  • Size: 71.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for netdiff-0.4.2.tar.gz
Algorithm Hash digest
SHA256 59e9ecbfa338d9ddacb7ade98f036871863aba7ef3baff2f9c28b8b850bb96a6
MD5 5c9a6179580af60d2c053370bb447cd6
BLAKE2b-256 aea21354d24300d1b5200edc151679eac9ce276b8393ff41d365d5a0e89cab03

See more details on using hashes here.

File details

Details for the file netdiff-0.4.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for netdiff-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 189cc341441f3df0a7b55c66d719b26d3cc603f727249fd2dc93b5f7d4dd7875
MD5 b2de8177e17a4610430fb88d51627fd2
BLAKE2b-256 66ec6fb675f8faae6ea39fd5d43060a32b2e08e9301b4c2ce25c8289629fa6db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page