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

Commit

Permalink
Merge pull request #25 from napalm-automation/develop
Browse files Browse the repository at this point in the history
Release beta 0.0.4
  • Loading branch information
dbarrosop committed Mar 27, 2017
2 parents 98ded5c + e62e141 commit d7d1d65
Show file tree
Hide file tree
Showing 9 changed files with 820 additions and 72 deletions.
816 changes: 778 additions & 38 deletions interactive_demo/tutorial.ipynb

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions napalm_yang/jinja_filters/ip_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ def filters():


def netmask_to_cidr(value):
""" Converts a network mask to it's CIDR value. """
"""
Converts a network mask to it's CIDR value.
Examples:
>>> "{{ '255.255.255.0'|netmask_to_cidr }}" -> "24"
"""
return netaddr.IPAddress(value).netmask_bits()


def cidr_to_netmask(value):
""" Converts a CIDR prefix-length to a network mask. """
"""
Converts a CIDR prefix-length to a network mask.
Examples:
>>> "{{ '24'|cidr_to_netmask }}" -> "255.255.255.0"
"""
return netaddr.IPNetwork("1.1.1.1/{}".format(value)).netmask
11 changes: 6 additions & 5 deletions napalm_yang/mappings/eos/translators/openconfig-if-ip/ipv4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ ipv4:
addresses:
_translation: unnecessary
address:
_translation: unnecessary
_translation:
mode: container
key_value: " ip address {{ model.config.ip }}/{{ model.config.prefix_length }} {{ 'secondary' if model.config.secondary else '' }}\n"
negate: " default ip address {{ model.config.ip }}/{{ model.config.prefix_length }} {{ 'secondary' if model.config.secondary else '' }}\n"
replace: false
ip:
_translation: unnecessary
config:
_translation: unnecessary
ip:
_translation: unnecessary
prefix_length:
_translation:
- mode: element
value: " ip address {{ model._parent.ip }}/{{ model }} {{ 'secondary' if model._parent.secondary else '' }}\n"
negate: " default ip address {{ model._parent.ip }}/{{ model }} {{ 'secondary' if model._parent.secondary else '' }}\n"
_translation: unnecessary
vrrp:
_translation: not_implemented
vrrp_group:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ ipv4:
_translation: unnecessary
address:
_translation:
mode: default_only
mode: container
key_value: " ip address {{ model.config.ip }} {{ model.config.prefix_length|cidr_to_netmask }}{{ ' secondary' if model.config.secondary else '' }}\n"
negate: " default ip address {{ model.config.ip }} {{ model.config.prefix_length|cidr_to_netmask }}{{ ' secondary' if model.config.secondary else '' }}\n"
replace: false
ip:
_translation: unnecessary
config:
_translation: unnecessary
ip:
_translation: unnecessary
prefix_length:
_translation:
- mode: element
value: " ip address {{ model._parent.ip }} {{ model|cidr_to_netmask }}{{ ' secondary' if model._parent.secondary else '' }}\n"
_translation: unnecessary
vrrp:
_translation: not_implemented
vrrp_group:
Expand Down
4 changes: 2 additions & 2 deletions napalm_yang/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def _translate_list(self, attribute, model, mapping, translation, other):
translation_point)

if et is None:
logger.info("Skipping {} as it seems to be not implemented".format(attribute))
break
logger.info("Skipping {} as not implemented or objects are equal".format(attribute))
continue

self.bookmarks[attribute][key] = et
self.bookmarks["parent"] = et
Expand Down
26 changes: 17 additions & 9 deletions napalm_yang/translators/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from lxml import etree

import napalm_yang


class TextTranslator(XMLTranslator):

Expand All @@ -30,11 +32,19 @@ def _parse_leaf_element(self, attribute, model, other, mapping, translation):
mapping["element"] = "command"
super()._parse_leaf_element(attribute, model, other, mapping, translation, force)

def _init_element_default_only(self, attribute, model, other, mapping, translation):
# There is nothing to do here, default_only action is only useful to remove lists
return translation

def _init_element_container(self, attribute, model, other, mapping, translation):
if other is not None:
if not napalm_yang.utils.diff(model, other) and not self.replace:
# If objects are equal we return None as that aborts translating
# the rest of the object
return

if not model._changed() and other is not None and not self.replace:
print(attribute, model, other)
mapping["key_value"] = mapping["negate"]
if not model._changed() and other is not None and self.replace:
return translation

mapping["key_element"] = "command"
mapping["container"] = model._yang_name
return super()._init_element_container(attribute, model, other, mapping, translation)
Expand All @@ -45,6 +55,9 @@ def _init_element_container(self, attribute, model, other, mapping, translation)
# return super()._init_element_container(attribute, model, other, mapping, translation)

def _default_element_container(self, mapping, translation, replacing):
if (replacing or self.replace) and not mapping.get("replace", True):
return

if not self.merge and not self.replace:
return

Expand All @@ -54,11 +67,6 @@ def _default_element_container(self, mapping, translation, replacing):
e = etree.SubElement(translation, "command")
e.text = mapping["negate"]

def _default_element_default_only(self, mapping, translation, replacing):
if self.replace:
return
self._default_element_container(mapping, translation, replacing)

def _xml_to_text(self, xml, text=""):
for element in xml:
if element.tag == "command":
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="napalm-yang",
version="0.0.3",
version="0.0.4",
packages=find_packages(),
author="David Barroso",
author_email="dbarrosop@dravetech.com",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
interface Port-channel1
interface Port-channel1.1
interface Loopback1
interface GigabitEthernet1
default description
interface GigabitEthernet2
Expand All @@ -9,5 +6,3 @@ interface GigabitEthernet2.1
no encapsulation dot1q
ip address 192.168.20.1 255.255.255.0 secondary
default ip address 172.20.0.1 255.255.255.0 secondary
interface GigabitEthernet2.2
interface GigabitEthernet3
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
interface Port-Channel1
default mtu
interface Port-Channel1.1
interface Ethernet1
interface Ethernet2
interface Ethernet2.1
interface Ethernet2.2
interface Management1
interface Loopback0
description new loopback
no interface Loopback1

0 comments on commit d7d1d65

Please sign in to comment.