3 __author__ = "Mislav Novakovic <mislav.novakovic@sartura.hr>"
4 __copyright__ = "Copyright 2018, Deutsche Telekom AG"
5 __license__ = "Apache 2.0"
7 # ============LICENSE_START=======================================================
8 # Copyright (C) 2018 Deutsche Telekom AG
9 # Modifications Copyright (C) 2021 Nordix Foundation
10 # ================================================================================
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22 # ============LICENSE_END=========================================================
28 # Helper function for printing changes given operation, old and new value.
29 def print_change(op, old_val, new_val):
30 if op == sr.SR_OP_CREATED:
31 print(f"CREATED: {new_val.to_string()}")
32 elif op == sr.SR_OP_DELETED:
33 print(f"DELETED: {old_val.to_string()}")
34 elif op == sr.SR_OP_MODIFIED:
35 print(f"MODIFIED: {old_val.to_string()} to {new_val.to_string()}")
36 elif op == sr.SR_OP_MOVED:
37 print(f"MOVED: {new_val.xpath()} after {old_val.xpath()}")
40 # Helper function for printing events.
42 if ev == sr.SR_EV_VERIFY:
44 elif ev == sr.SR_EV_APPLY:
46 elif ev == sr.SR_EV_ABORT:
52 # Function to print current configuration state.
53 # It does so by loading all the items of a session and printing them out.
54 def print_current_config(session, module_name):
55 select_xpath = f"/{module_name}:*//*"
57 values = session.get_items(select_xpath)
59 if values is not None:
60 print("========== BEGIN CONFIG ==========")
61 for i in range(values.val_cnt()):
62 print(values.val(i).to_string(), end='')
63 print("=========== END CONFIG ===========")
66 # Function to be called for subscribed client of given session whenever configuration changes.
67 def module_change_cb(sess, module_name, event, private_ctx):
69 print("========== Notification " + ev_to_str(event) + " =============================================")
70 if event == sr.SR_EV_APPLY:
71 print_current_config(sess, module_name)
73 print("========== CHANGES: =============================================")
75 change_path = f"/{module_name}:*"
77 it = sess.get_changes_iter(change_path)
80 change = sess.get_change_next(it)
83 print_change(change.oper(), change.old_val(), change.new_val())
85 print("========== END OF CHANGES =======================================")
86 except Exception as e:
93 # Notable difference between c implementation is using exception mechanism for open handling unexpected events.
94 # Here it is useful because `Connection`, `Session` and `Subscribe` could throw an exception.
96 module_name = "ietf-interfaces"
98 module_name = sys.argv[1]
100 print("\nYou can pass the module name to be subscribed as the first argument")
102 print(f"Application will watch for changes in {module_name}")
105 conn = sr.Connection(module_name)
108 sess = sr.Session(conn)
110 # subscribe for changes in running config */
111 subscribe = sr.Subscribe(sess)
113 subscribe.module_change_subscribe(module_name, module_change_cb)
116 print_current_config(sess, module_name)
117 except Exception as e:
120 print("========== STARTUP CONFIG APPLIED AS RUNNING ==========")
124 print("Application exit requested, exiting.")
126 except Exception as e:
130 if __name__ == '__main__':