aboutsummaryrefslogtreecommitdiffstats
path: root/docker-compose/config/pnfsim/netconf-config/subscriber.py
diff options
context:
space:
mode:
authordanielhanrahan <daniel.hanrahan@est.tech>2025-02-11 18:34:57 +0000
committerdanielhanrahan <daniel.hanrahan@est.tech>2025-03-28 11:38:10 +0000
commit88e2a82e5bb6181efbc054a210c4f3ed655b4528 (patch)
tree3cd52b27e58fe418c777b0d20efce961cd98167d /docker-compose/config/pnfsim/netconf-config/subscriber.py
parenta0993069311561c2b48e79df9273d421eb252723 (diff)
Move SDNC and PNFSim into docker-compose for CSITs
To improve reliability of healthchecks in CSITs, they are implemented in docker-compose. This commit moves SDNC and dependencies into main docker-compose and adds healthchecks to them. This improves the CSIT in that tests will quickly fail if SDNC or PNFsim containers fail to start (currently tests run anyway even if containers are not ready). - Move SDNC, MariaDB and PNFSim containers to main docker-compose - Add healthchecks and timeouts for those containers - Move node mounting script as a sidecar container so it is protected by healthchecks Issue-ID: CPS-2632 Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech> Change-Id: Ib53522c2f756d3ce2c6d6b7472cb4c65359fe355
Diffstat (limited to 'docker-compose/config/pnfsim/netconf-config/subscriber.py')
-rwxr-xr-xdocker-compose/config/pnfsim/netconf-config/subscriber.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/docker-compose/config/pnfsim/netconf-config/subscriber.py b/docker-compose/config/pnfsim/netconf-config/subscriber.py
new file mode 100755
index 0000000000..5147c93458
--- /dev/null
+++ b/docker-compose/config/pnfsim/netconf-config/subscriber.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python3
+
+__author__ = "Mislav Novakovic <mislav.novakovic@sartura.hr>"
+__copyright__ = "Copyright 2018, Deutsche Telekom AG"
+__license__ = "Apache 2.0"
+
+# ============LICENSE_START=======================================================
+# Copyright (C) 2018 Deutsche Telekom AG
+# Modifications Copyright (C) 2021 Nordix Foundation
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+
+import sysrepo as sr
+import sys
+
+
+# Helper function for printing changes given operation, old and new value.
+def print_change(op, old_val, new_val):
+ if op == sr.SR_OP_CREATED:
+ print(f"CREATED: {new_val.to_string()}")
+ elif op == sr.SR_OP_DELETED:
+ print(f"DELETED: {old_val.to_string()}")
+ elif op == sr.SR_OP_MODIFIED:
+ print(f"MODIFIED: {old_val.to_string()} to {new_val.to_string()}")
+ elif op == sr.SR_OP_MOVED:
+ print(f"MOVED: {new_val.xpath()} after {old_val.xpath()}")
+
+
+# Helper function for printing events.
+def ev_to_str(ev):
+ if ev == sr.SR_EV_VERIFY:
+ return "verify"
+ elif ev == sr.SR_EV_APPLY:
+ return "apply"
+ elif ev == sr.SR_EV_ABORT:
+ return "abort"
+ else:
+ return "unknown"
+
+
+# Function to print current configuration state.
+# It does so by loading all the items of a session and printing them out.
+def print_current_config(session, module_name):
+ select_xpath = f"/{module_name}:*//*"
+
+ values = session.get_items(select_xpath)
+
+ if values is not None:
+ print("========== BEGIN CONFIG ==========")
+ for i in range(values.val_cnt()):
+ print(values.val(i).to_string(), end='')
+ print("=========== END CONFIG ===========")
+
+
+# Function to be called for subscribed client of given session whenever configuration changes.
+def module_change_cb(sess, module_name, event, private_ctx):
+ try:
+ print("========== Notification " + ev_to_str(event) + " =============================================")
+ if event == sr.SR_EV_APPLY:
+ print_current_config(sess, module_name)
+
+ print("========== CHANGES: =============================================")
+
+ change_path = f"/{module_name}:*"
+
+ it = sess.get_changes_iter(change_path)
+
+ while True:
+ change = sess.get_change_next(it)
+ if change is None:
+ break
+ print_change(change.oper(), change.old_val(), change.new_val())
+
+ print("========== END OF CHANGES =======================================")
+ except Exception as e:
+ print(e)
+
+ return sr.SR_ERR_OK
+
+
+def main():
+ # Notable difference between c implementation is using exception mechanism for open handling unexpected events.
+ # Here it is useful because `Connection`, `Session` and `Subscribe` could throw an exception.
+ try:
+ module_name = "ietf-interfaces"
+ if len(sys.argv) > 1:
+ module_name = sys.argv[1]
+ else:
+ print("\nYou can pass the module name to be subscribed as the first argument")
+
+ print(f"Application will watch for changes in {module_name}")
+
+ # connect to sysrepo
+ conn = sr.Connection(module_name)
+
+ # start session
+ sess = sr.Session(conn)
+
+ # subscribe for changes in running config */
+ subscribe = sr.Subscribe(sess)
+
+ subscribe.module_change_subscribe(module_name, module_change_cb)
+
+ try:
+ print_current_config(sess, module_name)
+ except Exception as e:
+ print(e)
+
+ print("========== STARTUP CONFIG APPLIED AS RUNNING ==========")
+
+ sr.global_loop()
+
+ print("Application exit requested, exiting.")
+
+ except Exception as e:
+ print(e)
+
+
+if __name__ == '__main__':
+ main()