Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Merge a3136bb into ab8a779
Browse files Browse the repository at this point in the history
  • Loading branch information
matejv authored Jun 9, 2017
2 parents ab8a779 + a3136bb commit b7ec979
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions napalm_yang/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import jinja2

from napalm_yang.jinja_filters import ip_filters
from napalm_yang import jinja_filters

import logging
logger = logging.getLogger("napalm-yang")
Expand Down Expand Up @@ -110,7 +110,11 @@ def resolve_rule(rule, attribute, keys, extra_vars=None, translation_model=None,
kwargs["extra_vars"] = extra_vars

for k, v in rule.items():
rule[k] = _resolve_rule(v, **kwargs)
if k.startswith('post_process_'):
# don't process post processing rules now, they'll be processed on a second pass
rule[k] = v
else:
rule[k] = _resolve_rule(v, **kwargs)

if "when" in rule.keys():
w = rule["when"]
Expand All @@ -131,7 +135,7 @@ def template(string, **kwargs):
undefined=jinja2.StrictUndefined,
keep_trailing_newline=True,
)
env.filters.update(ip_filters.filters())
env.filters.update(jinja_filters.load_filters())

template = env.from_string(string)

Expand Down
16 changes: 16 additions & 0 deletions napalm_yang/jinja_filters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ip_filters

JINJA_FILTERS = [
ip_filters,
]


def load_filters():
"""
Loads and returns all filters.
"""
all_filters = {}
for m in JINJA_FILTERS:
if hasattr(m, 'filters'):
all_filters.update(m.filters())
return all_filters
13 changes: 13 additions & 0 deletions napalm_yang/jinja_filters/ip_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def filters():
return {
"netmask_to_cidr": netmask_to_cidr,
"cidr_to_netmask": cidr_to_netmask,
"addrmask_to_cidr": addrmask_to_cidr,
}


Expand All @@ -26,3 +27,15 @@ def cidr_to_netmask(value):
>>> "{{ '24'|cidr_to_netmask }}" -> "255.255.255.0"
"""
return netaddr.IPNetwork("1.1.1.1/{}".format(value)).netmask


def addrmask_to_cidr(value):
"""
Converts an address and network mask to it's CIDR representation.
Examples:
>>> "{{ '192.168.0.0 255.255.255.0'|addrmask_to_cidr }}" -> "192.168.0.0/24"
"""
value = value.replace(' ', '/')
return str(netaddr.IPNetwork(value))

8 changes: 8 additions & 0 deletions napalm_yang/parsers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _parse_list_block(cls, mapping):
else:
composite_key = mapping.get("composite_key", None)
forced_key = mapping.get("key", None)
post_process_filter = mapping.get("post_process_filter", None)

extra_vars = match.groupdict()
block = extra_vars.pop("block")
Expand All @@ -30,6 +31,13 @@ def _parse_list_block(cls, mapping):
else:
key = extra_vars.pop("key")

if post_process_filter:
from napalm_yang.helpers import template
kwargs = dict()
kwargs['key'] = key
kwargs["extra_vars"] = extra_vars
key = template(post_process_filter, **kwargs)

extra_vars["_get_duplicates"] = mapping.get("flat", False)

yield key, block, extra_vars
Expand Down

0 comments on commit b7ec979

Please sign in to comment.