Skip to content

Commit

Permalink
add address_to_mask and address_to_wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
mkomon committed Jul 24, 2023
1 parent 37eb65b commit 2e5d1cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/netom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pkg_resources import get_distribution

from .exception import NetomValidationError
from .j2_filters import address_to_mask, address_to_wildcard

# TODO move out of this namespace
from .models import BgpNeighbor
Expand Down Expand Up @@ -57,6 +58,8 @@ def set_search_path(self, search_path):
self.engine = tmpl.get_engine("jinja2")(search_path=search_path)
self.engine.engine.filters["make_variable_name"] = make_variable_name
self.engine.engine.filters["ip_version"] = ip_version
self.engine.engine.filters["address_to_mask"] = address_to_mask
self.engine.engine.filters["address_to_wildcard"] = address_to_wildcard
# self.engine.search_path = os.path.dirname(search_path)

def _render(self, filename, data, fobj):
Expand Down
21 changes: 21 additions & 0 deletions src/netom/j2_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ipaddress
from ipaddress import IPv4Address

def address_to_mask(addr):
"""
Transform A.B.C.D/nn into A.B.C.D M.M.M.M subnet mask format.
"""
ipaddr4 = ipaddress.ip_interface(addr)
return f"{ipaddr4.ip} {ipaddr4.netmask}"


def address_to_wildcard(addr):
"""
Transform A.B.C.D/nn into A.B.C.D W.W.W.W subnet wildcard format.
credit for the mask shenanigans to George Shuklin
https://medium.com/opsops/wildcard-masks-operations-in-python-16acf1c35683
"""
ipnet4 = ipaddress.ip_network(addr)
wildcard = str(IPv4Address(int(IPv4Address(ipnet4.netmask))^(2**32-1)))
return f"{ipnet4.network_address} {wildcard}"
26 changes: 26 additions & 0 deletions tests/test_j2_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

from netom.j2_filters import address_to_mask, address_to_wildcard
import pytest


def test_address_to_mask():
assert address_to_mask("192.168.0.1/4") == "192.168.0.1 240.0.0.0"
assert address_to_mask("192.168.0.1/8") == "192.168.0.1 255.0.0.0"
assert address_to_mask("192.168.0.1/9") == "192.168.0.1 255.128.0.0"
assert address_to_mask("192.168.0.1/16") == "192.168.0.1 255.255.0.0"
assert address_to_mask("192.168.0.1/24") == "192.168.0.1 255.255.255.0"
assert address_to_mask("192.168.0.1/25") == "192.168.0.1 255.255.255.128"
assert address_to_mask("192.168.0.1/30") == "192.168.0.1 255.255.255.252"
assert address_to_mask("192.168.0.1/31") == "192.168.0.1 255.255.255.254"
assert address_to_mask("192.168.0.1/32") == "192.168.0.1 255.255.255.255"

def test_address_to_wildcard():
assert address_to_wildcard("192.0.0.0/4") == "192.0.0.0 15.255.255.255"
assert address_to_wildcard("192.0.0.0/8") == "192.0.0.0 0.255.255.255"
assert address_to_wildcard("192.0.0.0/9") == "192.0.0.0 0.127.255.255"
assert address_to_wildcard("192.168.0.0/16") == "192.168.0.0 0.0.255.255"
assert address_to_wildcard("192.168.0.0/24") == "192.168.0.0 0.0.0.255"
assert address_to_wildcard("192.168.0.0/25") == "192.168.0.0 0.0.0.127"
assert address_to_wildcard("192.168.0.0/30") == "192.168.0.0 0.0.0.3"
assert address_to_wildcard("192.168.0.0/31") == "192.168.0.0 0.0.0.1"
assert address_to_wildcard("192.168.0.0/32") == "192.168.0.0 0.0.0.0"

0 comments on commit 2e5d1cf

Please sign in to comment.