Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1554: Improve compare_config for NXOS partial merging #1567

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion docs/support/nxos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ As a result, merges are **not atomic**.
Diffs
_____

Diffs for merges are simply the lines in the merge candidate config.
Diffs for merges are simply the lines in the merge candidate config. `Netutils <https://netutils.readthedocs.io/en/latest/>`__ is used for creating the merge diff between the candidate and running configurations.
One caveat of using netutils diff of configurations is that the diff is performed offline and not online in the device.

Example assuming that the device config contains:

.. code-block::

interface loopback0
ip address 10.1.4.4/32
ip router ospf 100 area 0.0.0.1

Then what you will get with the diff:

.. code-block:: python

candidate_cfg = """
interface loopback0
ip address 10.1.4.5/32
ip router ospf 100 area 0.0.0.1
"""

nxos1.load_merge_candidate(config=candidate_cfg)

print(nxos1.compare_config())
interface loopback0
ip address 10.1.4.5/32
38 changes: 24 additions & 14 deletions napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from netaddr.core import AddrFormatError
from netmiko import file_transfer
from requests.exceptions import ConnectionError
from netutils.config.compliance import diff_network_config

import napalm.base.constants as c

Expand Down Expand Up @@ -209,22 +210,31 @@ def _commit_merge(self) -> None:

def _get_merge_diff(self) -> str:
"""
The merge diff is not necessarily what needs to be loaded
for example under NTP, even though the 'ntp commit' command might be
alread configured, it is mandatory to be sent
otherwise it won't take the new configuration - see:
https://github.com/napalm-automation/napalm-nxos/issues/59
therefore this method will return the real diff (but not necessarily what is
being sent by the merge_load_config()
Uses netutils diff_network_config to create a partial configuration
with the proper hierarchy.
Note: the netutils utility performs the diff offline.

Returns: diff with the proper hierarchy of commands
that are missing from the current config.
Examples:
Candidate configuration:
interface loopback0
ip address 10.1.4.5/32
ip router ospf 100 area 0.0.0.1

Base (on device) - relevant section:
...
interface loopback0
ip address 10.1.4.4/32
ip router ospf 100 area 0.0.0.1
...

Diff that respects the required command hierarchy:
interface loopback0
ip address 10.1.4.5/32
"""
diff = []
running_config = self.get_config(retrieve="running")["running"]
running_lines = running_config.splitlines()
for line in self.merge_candidate.splitlines():
if line not in running_lines and line:
if line[0].strip() != "!":
diff.append(line)
return "\n".join(diff)
return diff_network_config(self.merge_candidate, running_config, "cisco_nxos")
cmsirbu marked this conversation as resolved.
Show resolved Hide resolved

def _get_diff(self) -> str:
"""Get a diff between running config and a proposed file."""
Expand Down