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

Commit 95ba140

Browse files
authored
Merge pull request #30 from napalm-automation/validate
Load dict now updates even on default values and added compliance_report to Root class
2 parents c9cdf79 + 3dded32 commit 95ba140

File tree

14 files changed

+872
-14
lines changed

14 files changed

+872
-14
lines changed

1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
r 609e192 Load dict now update even on defaults and added compliance_report to Root class
2+
f 90f6a83 Added docstring
3+
4+
# Rebase c9cdf79..90f6a83 onto c9cdf79 (2 commands)
5+
#
6+
# Commands:
7+
# p, pick = use commit
8+
# r, reword = use commit, but edit the commit message
9+
# e, edit = use commit, but stop for amending
10+
# s, squash = use commit, but meld into previous commit
11+
# f, fixup = like "squash", but discard this commit's log message
12+
# x, exec = run command (the rest of the line) using shell
13+
# d, drop = remove commit
14+
#
15+
# These lines can be re-ordered; they are executed from top to bottom.
16+
#
17+
# If you remove a line here THAT COMMIT WILL BE LOST.
18+
#
19+
# However, if you remove everything, the rebase will be aborted.
20+
#
21+
# Note that empty commits are commented out

interactive_demo/tutorial.ipynb

Lines changed: 217 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@
24542454
" }, \n",
24552455
" \"enabled\": True, \n",
24562456
" \"ifindex\": 531, \n",
2457-
" \"last-change\": 254456, \n",
2457+
" \"last-change\": 255203, \n",
24582458
" \"mtu\": 1518, \n",
24592459
" \"oper-status\": \"DOWN\"\n",
24602460
" }, \n",
@@ -2518,16 +2518,16 @@
25182518
" \"in-discards\": 0, \n",
25192519
" \"in-errors\": 0, \n",
25202520
" \"in-multicast-pkts\": 0, \n",
2521-
" \"in-unicast-pkts\": 15740, \n",
2521+
" \"in-unicast-pkts\": 16877, \n",
25222522
" \"out-broadcast-pkts\": 0, \n",
25232523
" \"out-errors\": 0, \n",
25242524
" \"out-multicast-pkts\": 0, \n",
2525-
" \"out-unicast-pkts\": 14704\n",
2525+
" \"out-unicast-pkts\": 15742\n",
25262526
" }, \n",
25272527
" \"description\": \"management interface\", \n",
25282528
" \"enabled\": True, \n",
25292529
" \"ifindex\": 507, \n",
2530-
" \"last-change\": 257720, \n",
2530+
" \"last-change\": 258467, \n",
25312531
" \"mtu\": 1400, \n",
25322532
" \"oper-status\": \"UP\"\n",
25332533
" }, \n",
@@ -2562,7 +2562,7 @@
25622562
" \"description\": \"ge-0/0/1\", \n",
25632563
" \"enabled\": False, \n",
25642564
" \"ifindex\": 508, \n",
2565-
" \"last-change\": 254454, \n",
2565+
" \"last-change\": 255201, \n",
25662566
" \"mtu\": 1514, \n",
25672567
" \"oper-status\": \"DOWN\"\n",
25682568
" }\n",
@@ -2584,7 +2584,7 @@
25842584
" }, \n",
25852585
" \"enabled\": True, \n",
25862586
" \"ifindex\": 509, \n",
2587-
" \"last-change\": 257720, \n",
2587+
" \"last-change\": 258467, \n",
25882588
" \"mtu\": 1514, \n",
25892589
" \"oper-status\": \"UP\"\n",
25902590
" }\n",
@@ -2718,7 +2718,7 @@
27182718
" \"admin-status\": \"UP\", \n",
27192719
" \"enabled\": True, \n",
27202720
" \"ifindex\": 518, \n",
2721-
" \"last-change\": 257721, \n",
2721+
" \"last-change\": 258468, \n",
27222722
" \"mtu\": 1504, \n",
27232723
" \"oper-status\": \"UP\"\n",
27242724
" }\n",
@@ -2821,7 +2821,7 @@
28212821
" }, \n",
28222822
" \"enabled\": True, \n",
28232823
" \"ifindex\": 515, \n",
2824-
" \"last-change\": 257721, \n",
2824+
" \"last-change\": 258468, \n",
28252825
" \"mtu\": 9192, \n",
28262826
" \"oper-status\": \"UP\"\n",
28272827
" }, \n",
@@ -2881,7 +2881,7 @@
28812881
" }, \n",
28822882
" \"enabled\": True, \n",
28832883
" \"ifindex\": 506, \n",
2884-
" \"last-change\": 257729, \n",
2884+
" \"last-change\": 258476, \n",
28852885
" \"mtu\": 1518, \n",
28862886
" \"oper-status\": \"DOWN\", \n",
28872887
" \"type\": \"l2vlan\"\n",
@@ -2990,6 +2990,214 @@
29902990
"source": [
29912991
"Diff'ing models with state is also supported."
29922992
]
2993+
},
2994+
{
2995+
"cell_type": "markdown",
2996+
"metadata": {
2997+
"deletable": true,
2998+
"editable": true
2999+
},
3000+
"source": [
3001+
"# Compliance Report\n",
3002+
"\n",
3003+
"This feature also works with YANG models. Let's assume we want to verify we have set the MTU of all of our interfaces to 9000."
3004+
]
3005+
},
3006+
{
3007+
"cell_type": "code",
3008+
"execution_count": 28,
3009+
"metadata": {
3010+
"collapsed": false,
3011+
"deletable": true,
3012+
"editable": true
3013+
},
3014+
"outputs": [],
3015+
"source": [
3016+
"data = {\n",
3017+
" \"interfaces\": {\n",
3018+
" \"interface\":{\n",
3019+
" \"Et1\": {\n",
3020+
" \"config\": {\n",
3021+
" \"mtu\": 9000\n",
3022+
" },\n",
3023+
" },\n",
3024+
" \"Et2\": {\n",
3025+
" \"config\": {\n",
3026+
" \"mtu\": 1500\n",
3027+
" }\n",
3028+
" }\n",
3029+
" }\n",
3030+
" }\n",
3031+
"}\n",
3032+
"# We load a dict for convenience, any source will do\n",
3033+
"config = napalm_yang.base.Root()\n",
3034+
"config.add_model(napalm_yang.models.openconfig_interfaces())\n",
3035+
"config.load_dict(data)"
3036+
]
3037+
},
3038+
{
3039+
"cell_type": "markdown",
3040+
"metadata": {
3041+
"collapsed": true,
3042+
"deletable": true,
3043+
"editable": true,
3044+
"slideshow": {
3045+
"slide_type": "-"
3046+
}
3047+
},
3048+
"source": [
3049+
"Now we can load the validation file. Here is the content for reference:\n",
3050+
"\n",
3051+
"```\n",
3052+
"---\n",
3053+
"- to_dict:\n",
3054+
" _kwargs:\n",
3055+
" filter: true\n",
3056+
" interfaces:\n",
3057+
" interface:\n",
3058+
" Et1:\n",
3059+
" config:\n",
3060+
" mtu: 9000\n",
3061+
" Et2:\n",
3062+
" config:\n",
3063+
" mtu: 9000\n",
3064+
" _mode: strict\n",
3065+
"```\n",
3066+
"\n",
3067+
"Note that there is a major difference between using the `compliance_report` method on getters and on `YANG` objects. With the former you have to specify how to get the data, with the later you have to get the data yourself by any means and then specify you want to convert the data into a `dict` with the `to_dict` method."
3068+
]
3069+
},
3070+
{
3071+
"cell_type": "code",
3072+
"execution_count": 29,
3073+
"metadata": {
3074+
"collapsed": false
3075+
},
3076+
"outputs": [
3077+
{
3078+
"name": "stdout",
3079+
"output_type": "stream",
3080+
"text": [
3081+
"{\n",
3082+
" \"complies\": false, \n",
3083+
" \"skipped\": [], \n",
3084+
" \"to_dict\": {\n",
3085+
" \"complies\": false, \n",
3086+
" \"extra\": [], \n",
3087+
" \"missing\": [], \n",
3088+
" \"present\": {\n",
3089+
" \"interfaces\": {\n",
3090+
" \"complies\": false, \n",
3091+
" \"diff\": {\n",
3092+
" \"complies\": false, \n",
3093+
" \"extra\": [], \n",
3094+
" \"missing\": [], \n",
3095+
" \"present\": {\n",
3096+
" \"interface\": {\n",
3097+
" \"complies\": false, \n",
3098+
" \"diff\": {\n",
3099+
" \"complies\": false, \n",
3100+
" \"extra\": [], \n",
3101+
" \"missing\": [], \n",
3102+
" \"present\": {\n",
3103+
" \"Et1\": {\n",
3104+
" \"complies\": true, \n",
3105+
" \"nested\": true\n",
3106+
" }, \n",
3107+
" \"Et2\": {\n",
3108+
" \"complies\": false, \n",
3109+
" \"diff\": {\n",
3110+
" \"complies\": false, \n",
3111+
" \"extra\": [], \n",
3112+
" \"missing\": [], \n",
3113+
" \"present\": {\n",
3114+
" \"config\": {\n",
3115+
" \"complies\": false, \n",
3116+
" \"diff\": {\n",
3117+
" \"complies\": false, \n",
3118+
" \"extra\": [], \n",
3119+
" \"missing\": [], \n",
3120+
" \"present\": {\n",
3121+
" \"mtu\": {\n",
3122+
" \"actual_value\": 1500, \n",
3123+
" \"complies\": false, \n",
3124+
" \"nested\": false\n",
3125+
" }\n",
3126+
" }\n",
3127+
" }, \n",
3128+
" \"nested\": true\n",
3129+
" }\n",
3130+
" }\n",
3131+
" }, \n",
3132+
" \"nested\": true\n",
3133+
" }\n",
3134+
" }\n",
3135+
" }, \n",
3136+
" \"nested\": true\n",
3137+
" }\n",
3138+
" }\n",
3139+
" }, \n",
3140+
" \"nested\": true\n",
3141+
" }\n",
3142+
" }\n",
3143+
" }\n",
3144+
"}\n"
3145+
]
3146+
}
3147+
],
3148+
"source": [
3149+
"report = config.compliance_report(\"validate.yaml\")\n",
3150+
"pretty_print(report)"
3151+
]
3152+
},
3153+
{
3154+
"cell_type": "markdown",
3155+
"metadata": {},
3156+
"source": [
3157+
"We can see it's complaining that the value of `Et2`'s MTU is 1500. Let's fix it and try again:"
3158+
]
3159+
},
3160+
{
3161+
"cell_type": "code",
3162+
"execution_count": 30,
3163+
"metadata": {
3164+
"collapsed": false
3165+
},
3166+
"outputs": [
3167+
{
3168+
"name": "stdout",
3169+
"output_type": "stream",
3170+
"text": [
3171+
"{\n",
3172+
" \"complies\": true, \n",
3173+
" \"skipped\": [], \n",
3174+
" \"to_dict\": {\n",
3175+
" \"complies\": true, \n",
3176+
" \"extra\": [], \n",
3177+
" \"missing\": [], \n",
3178+
" \"present\": {\n",
3179+
" \"interfaces\": {\n",
3180+
" \"complies\": true, \n",
3181+
" \"nested\": true\n",
3182+
" }\n",
3183+
" }\n",
3184+
" }\n",
3185+
"}\n"
3186+
]
3187+
}
3188+
],
3189+
"source": [
3190+
"config.interfaces.interface[\"Et2\"].config.mtu = 9000\n",
3191+
"report = config.compliance_report(\"validate.yaml\")\n",
3192+
"pretty_print(report)"
3193+
]
3194+
},
3195+
{
3196+
"cell_type": "markdown",
3197+
"metadata": {},
3198+
"source": [
3199+
"Now we can see in the first `complies` element of the report that we are complying. This works for state as the rest of the features too."
3200+
]
29933201
}
29943202
],
29953203
"metadata": {

interactive_demo/validate.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
- to_dict:
3+
_kwargs:
4+
filter: true
5+
interfaces:
6+
interface:
7+
Et1:
8+
config:
9+
mtu: 9000
10+
Et2:
11+
config:
12+
mtu: 9000
13+
_mode: strict

0 commit comments

Comments
 (0)