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

Commit

Permalink
added preprocessing and postprocessing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Dec 13, 2018
1 parent bcc9eaf commit bf140fc
Show file tree
Hide file tree
Showing 16 changed files with 621 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
metadata:
prefix: "oc-ethernet"
processor: TextTree

ethernet:
_process: unnecessary
config:
_process: unnecessary
auto-negotiate:
_process: not_implemented
duplex-mode:
_process: not_implemented
enable-flow-control:
_process: not_implemented
mac-address:
_process: not_implemented
port-speed:
_process: not_implemented
state:
_process: not_implemented
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
metadata:
processor: JunosConfig
pre_processor: openconfig_vlan_switched_vlan

switched-vlan:
_process:
- path: "switched-vlan.config"
config:
_process: unnecessary
interface-mode:
_process:
- path: "interface-mode"
access-vlan:
_process:
- path: "access-vlans"
native-vlan:
_process: not_implemented
trunk-vlans:
_process:
- path: "trunk-vlans"
state:
_process: not_implemented
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
metadata:
processor: XMLTranslator

ethernet:
_process: unnecessary
config:
_process: unnecessary
auto-negotiate:
_process: not_implemented
duplex-mode:
_process: not_implemented
enable-flow-control:
_process: not_implemented
mac-address:
_process: not_implemented
port-speed:
_process: not_implemented
state:
_process: not_implemented
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
metadata:
processor: JunosTranslator

switched-vlan:
_process:
- gate: true
when: "{{ not model.config.interface_mode }}"
- in: subinterface.0
container: family/ethernet-switching
config:
_process: unnecessary
access-vlan:
_process:
- element: vlan/members
interface-mode:
_process:
- element: interface-mode
value: "{{ model|lower }}"
native-vlan:
_process: not_implemented
trunk-vlans:
_process:
- function: openconfig_vlan_vlan_members
state:
_process: not_implemented
6 changes: 6 additions & 0 deletions napalm_yang/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def __init__(
self.parser = get_parser(self.mapping["metadata"]["processor"])(
self.keys, self.extra_vars
)
if self.mapping["metadata"].get("pre_processor"):
self._pre_processor = getattr(self.parser, self.mapping["metadata"]["pre_processor"])
else:
self._pre_processor = None

self.bookmarks = bookmarks or {}

Expand Down Expand Up @@ -111,6 +115,8 @@ def parse(self):
self.bookmarks["root_{}".format(self._yang_name)] = self.native
if "parent" not in self.bookmarks:
self.bookmarks["parent".format(self._yang_name)] = self.native
if self._pre_processor:
self._pre_processor(self)
self._parse(self._yang_name, self.model, self.mapping[self._yang_name])

def _parse(self, attribute, model, mapping):
Expand Down
4 changes: 4 additions & 0 deletions napalm_yang/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from napalm_yang.parsers.jsonp import JSONParser
from napalm_yang.parsers.junos_config import JunosConfig
from napalm_yang.parsers.text import TextParser
from napalm_yang.parsers.text_tree import TextTree
from napalm_yang.parsers.xml import XMLParser

from napalm_yang.translators.text import TextTranslator
from napalm_yang.translators.junos import JunosTranslator
from napalm_yang.translators.xml import XMLTranslator


def get_parser(parser):
parsers = {
"JSONParser": JSONParser,
"JunosConfig": JunosConfig,
"TextParser": TextParser,
"TextTree": TextTree,
"XMLParser": XMLParser,
"TextTranslator": TextTranslator,
"JunosTranslator": JunosTranslator,
"XMLTranslator": XMLTranslator,
}
return parsers[parser]
30 changes: 30 additions & 0 deletions napalm_yang/parsers/junos_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from napalm_yang.jinja_filters.vlan_filters import vlan_range_to_oc
from napalm_yang.parsers.base import _resolve_path
from napalm_yang.parsers.jsonp import JSONParser


class JunosConfig(JSONParser):

def openconfig_vlan_switched_vlan(self, parser):
result = {"switched-vlan": {"config": {}}}
config = result["switched-vlan"]["config"]

try:
d = _resolve_path(parser.bookmarks["parent"], ["unit", "family", "ethernet-switching"])
except Exception:
return
vlans = _resolve_path(d, ["vlan", "members"])
if isinstance(vlans, list):
vlans = ",".join([v["#text"] for v in vlans])
else:
vlans = vlans["#text"]
if vlans == "all":
vlans = "1-4094"

if d["interface-mode"]["#text"] == "trunk":
config["interface-mode"] = "TRUNK"
config["trunk-vlans"] = vlan_range_to_oc(vlans)
else:
config["interface-mode"] = "ACCESS"
config["access-vlans"] = vlans
parser.bookmarks["parent"] = result
12 changes: 12 additions & 0 deletions napalm_yang/translators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def init_element(self, attribute, model, other, mapping, translation, bookmarks)
elif "gate" in m:
return None, {}

if "function" in m:
return getattr(self, m["function"])(attribute, model, other, mapping, translation, bookmarks)

mode = m.get("mode", "default")
t = _find_translation_point(m, bookmarks, et)
method_name = "_init_element_{}".format(mode)
Expand All @@ -49,6 +52,9 @@ def default_element(self, mapping, translation, bookmarks, replacing=False):
elif "gate" in m:
return {}

if "function" in m:
return getattr(self, m["function"])(mapping, translation, bookmarks, replacing)

mode = m.get("mode", "default")
t = _find_translation_point(m, bookmarks, t)
method_name = "_default_element_{}".format(mode)
Expand All @@ -64,6 +70,9 @@ def translate_leaf(self, attribute, model, other, mapping, translation, bookmark
elif "gate" in m:
return

if "function" in m:
return getattr(self, m["function"])(attribute, model, other, mapping, translation, bookmarks)

mode = m.get("mode", "default")
t = _find_translation_point(m, bookmarks, translation)
method_name = "_translate_leaf_{}".format(mode)
Expand All @@ -81,6 +90,9 @@ def translate_container(
elif "gate" in m:
return None, {}

if "function" in m:
return getattr(self, m["function"])(attribute, model, other, mapping, translation, bookmarks)

mode = m.get("mode", "default")
t = _find_translation_point(m, bookmarks, et)
method_name = "_translate_container_{}".format(mode)
Expand Down
19 changes: 19 additions & 0 deletions napalm_yang/translators/junos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ast

from napalm_yang.translators.xml import XMLTranslator

from lxml import etree


class JunosTranslator(XMLTranslator):

def openconfig_vlan_vlan_members(self, attribute, model,
other, mapping, translation, bookmarks):
vlans = ast.literal_eval(str(model))
vlan_xml = translation.find("vlan") or etree.SubElement(translation, "vlan")
for v in vlans:
t = etree.SubElement(vlan_xml, "members")
v = str(v)
if v == "1..4094":
v = "all"
t.text = str(v)
1 change: 1 addition & 0 deletions test/integration/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def pretty_json(dictionary):
["eos", "config", napalm_yang.models.openconfig_system, "default"],
["eos", "state", napalm_yang.models.openconfig_interfaces, "default"],
["junos", "config", napalm_yang.models.openconfig_interfaces, "default"],
["junos", "config", napalm_yang.models.openconfig_interfaces, "l2_ports"],
["junos", "config", napalm_yang.models.openconfig_network_instance, "default"],
["junos", "config", napalm_yang.models.openconfig_network_instance, "ospf"],
["junos", "config", napalm_yang.models.openconfig_network_instance, "mpls"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"interfaces": {
"interface": {
"xe-0/0/0": {
"config": {
"enabled": 1,
"name": "xe-0/0/0",
"type": "ethernetCsmacd"
},
"ethernet": {
"switched-vlan": {
"config": {
"interface-mode": "TRUNK",
"trunk-vlans": [
"10",
"20"
]
}
}
},
"name": "xe-0/0/0",
"routed-vlan": {
"ipv4": {
"config": {
"enabled": 0
}
}
},
"subinterfaces": {
"subinterface": {
"0": {
"config": {
"enabled": 1,
"name": "0"
},
"index": "0",
"ipv4": {
"config": {
"enabled": 0
}
}
}
}
}
},
"xe-0/0/1": {
"config": {
"enabled": 1,
"name": "xe-0/0/1",
"type": "ethernetCsmacd"
},
"ethernet": {
"switched-vlan": {
"config": {
"interface-mode": "TRUNK",
"trunk-vlans": [
"10",
"20"
]
}
}
},
"name": "xe-0/0/1",
"routed-vlan": {
"ipv4": {
"config": {
"enabled": 0
}
}
},
"subinterfaces": {
"subinterface": {
"0": {
"config": {
"enabled": 1,
"name": "0"
},
"index": "0",
"ipv4": {
"config": {
"enabled": 0
}
}
}
}
}
},
"xe-0/0/3": {
"config": {
"enabled": 1,
"name": "xe-0/0/3",
"type": "ethernetCsmacd"
},
"ethernet": {
"switched-vlan": {
"config": {
"interface-mode": "TRUNK",
"trunk-vlans": [
10,
20,
100
]
}
}
},
"name": "xe-0/0/3",
"routed-vlan": {
"ipv4": {
"config": {
"enabled": 0
}
}
},
"subinterfaces": {
"subinterface": {
"0": {
"config": {
"enabled": 1,
"name": "0"
},
"index": "0",
"ipv4": {
"config": {
"enabled": 0
}
}
}
}
}
}
}
}
}

0 comments on commit bf140fc

Please sign in to comment.