Skip to content

Commit

Permalink
Merge pull request #1567 from mundruid/nanog84-nxos-merge
Browse files Browse the repository at this point in the history
Fix #1554: Improve compare_config for NXOS partial merging
  • Loading branch information
mirceaulinic committed Mar 25, 2022
2 parents 31acd89 + 2fa2f1d commit 4c748fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
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")

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

0 comments on commit 4c748fa

Please sign in to comment.