-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add contextual diff computation mode #23
Add contextual diff computation mode #23
Conversation
Any updates on my PR? |
Hi @angely-dev sorry i haven't replied before. I will read and review your proposal in detail this weekend. |
Thanks @codingnetworksb. I am available for any question or to resolve the conflict which just appeared after the recent activity. |
Hi @angely-dev, this weekend i started to update the library because it was very outdated compared with original napalm library. I'm making sure it in complete compliance with getters structure, output, etc. I'm also implementing mocked data to for automatic testing when we make pull request. I couldn't review your pull request. I will do it as soon as i finish updating, hope during the week or weekend. And i'll let you know at that time to check the conflicts. |
Hi @angely-dev, i'm done until now with my updates. Could you check and fix the conflicts? |
Thanks for maintaining the repo @codingnetworksb. Please allow some time to fix the conflicts which appeared. |
So I checked and fixed the conflict @codingnetworksb. It was just a reformatting issue (see 502e930). |
The new code failed the Checks i have included recently. It's failing in Black format check. As napalm original project, we have included Black formatter check to this project, please format the code using Black formatter and push the code again. We appreciate your contribution to this project. |
So I did run the Black code formatter @codingnetworksb and committed the changes in my fork (a8a8bf7). Is it OK for you at this point? Thanks! Edit 1: I'm now seeing that the "type checker" workflow step is failing. I'm on it. Edit 2: OK this is fixed. Just like for the other modules, I ignored mypy missing imports for the |
Any updates @codingnetworksb? Or some concerns about the PR? Not only I fixed the failed checks, but I also added a workflow in my project as well 😊 It includes the Black code formatting check and it ensures unit tests have passed (from Python 3.8 to 3.12). I also released the new version on PyPI. |
No concern about the PR. I got distracted with other projects, but this weekend I'll catch up on these PRs |
Hi @angely-dev , I was testing the PR in my local environment, using a Virtual NE40E and seems to work as you explain it. Too sad that deletions cannot be computed. I understood the complexity of this. Thank you for documenting this PR so well. I will approve and merge today. Regards, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
4306aab
into
napalm-automation-community:master
Hi @codingnetworksb, Thanks for the feedback and for approving the PR. I hope users will take benefit of it despite the limitations. I realized I didn't include an update of the README in the PR. We could update it this way:
from napalm import get_network_driver
driver = get_network_driver('huawei_vrp')
device = driver(
hostname='192.168.76.10',
username='admin',
password='this_is_not_a_secure_password',
optional_args={'contextual_diff': True} # enable contextual diff mode
)
device.open()
# Send Any CLI command
# …
I prefer the second way, but I let you choose if you are willing to update the README directly. Otherwise, I could do another PR just for this update. |
Hi @angely-dev , Both options sounds good to me. Please submit a new PR just that update. If you create a new subsection make it as summary as possible. |
Problem
As you may know, the current implementation of
_get_merge_diff()
is not really sufficient.Something as simple as:
Using the basic Python sample:
Will result in:
Inconsistencies:
GigabitEthernet0/0/2
are not displayedIn other words, the diff is unable to compute changes per-context (per-indented block).
Proposed solution
I ended up doing DiffPlus, a lightweight module to compute an incremental and contextual diff between two indented configs. Taking about a hundred lines of code, it only relies on Python builtins and has no extra dependencies.
Though I did it originally for this specific use case, the module is generic enough to work with any indented config, theoretically. I ran it successfully on multiple Huawei platforms (S5732, NE20, NE05, CE6810) having from 2K to 16K number of lines.
Demo
The module is now on PyPI.
Diff only
Using the same configs as above:
Will output:
Merging
Alternatively, we can
merge
to get a preview of the full config before applying it:Will output:
Limitations
It is important to understand this module does NOT check syntax or semantic. By essence, it computes the diff between two indented texts, which makes it both simple and generic.
Also, deletions cannot be computed. I explain why here with an example. To sum up, it would require to re-implement the config logic in some way. Though it is tempting, that is far from easy and very platform-dependent.
Changes in NAPALM Huawei VRP
Option 1: modify
_get_merge_diff()
You may find this option too radical.
Option 2: add an
optional_args
This option is more prudent. We let users choose the diff computation mode: either they stick with the actual mode (by default, for legacy purposes) or they experiment the contextual mode through an optional argument
contextual_diff
.This is what I implemented in the pull request and I'd be glad to have your view.