diff options
author | rajendrajaiswal <rajendra.jaiswal@ericsson.com> | 2019-12-16 14:24:02 +0000 |
---|---|---|
committer | Morgan Richomme <morgan.richomme@orange.com> | 2020-01-21 14:34:09 +0000 |
commit | 6a61ad84e1df33bec201df43dc0b217d892f91b9 (patch) | |
tree | 0c340af84099a65e49ec44b8c53d51b13f79aa3e /test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py | |
parent | f2ec458f12c470552462a75f26ef7e1704482a72 (diff) |
PNF Simulator to support Control Loop subscription model
Change-Id: I9919edb32f3f68f86fad28c908f808fcee3fc548
Issue-ID: INT-1312
Signed-off-by: rajendrajaiswal <rajendra.jaiswal@ericsson.com>
Diffstat (limited to 'test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py')
-rw-r--r-- | test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py b/test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py new file mode 100644 index 000000000..44109a12d --- /dev/null +++ b/test/mocks/pmsh-pnf-sim/docker-compose/subscriber.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +import re +import sysrepo as sr +from pnf import PNF + + +def module_change_cb(sess, module_name, event, private_ctx): + """ Handle event change based on yang operation. """ + try: + change_path = "/" + module_name + ":*" + iterate = sess.get_changes_iter(change_path) + change = sess.get_change_next(iterate) + changelist = [] + operation = change.oper() + pnf = PNF() + if event == sr.SR_EV_APPLY: + print("------------------> Start Handle Change <------------------") + if operation == sr.SR_OP_CREATED: + while True: + change = sess.get_change_next(iterate) + if change is None: + break + changelist.append(change.new_val().to_string()) + result = re.findall(r'\'(.*?)\'', changelist[0]) + jobid = result[0] + print("Subscription Created : " + changelist[0]) + pnf.create_job_id(jobid, changelist) + pnf.pm_job() + elif operation == sr.SR_OP_DELETED: + changelist.append(change.old_val().to_string()) + result = re.findall(r'\'(.*?)\'', changelist[0]) + jobid = result[0] + print("Subscription Deleted : " + changelist[0]) + pnf.delete_job_id(jobid) + pnf.pm_job() + elif operation == sr.SR_OP_MODIFIED: + changelist.append(change.new_val().to_string()) + element = changelist[0] + print("Subscription Modified :" + element) + result = re.findall(r'\'(.*?)\'', changelist[0]) + jobid = result[0] + administrative_state = ((element.rsplit('/', 1)[1]).split('=', 1))[1].strip() + if administrative_state == "LOCKED": + pnf.delete_job_id(jobid) + pnf.pm_job() + elif administrative_state == "UNLOCKED": + select_xpath = "/" + module_name + ":*//*" + values = sess.get_items(select_xpath) + if values is not None: + for i in range(values.val_cnt()): + if jobid in values.val(i).to_string(): + changelist.append(values.val(i).to_string()) + pnf.create_job_id(jobid, changelist) + pnf.pm_job() + else: + print("Unknown Operation") + print("------------------> End Handle Change <------------------") + except Exception as error: + print(error) + return sr.SR_ERR_OK + + +def start(): + """ main function to create connection based on moudule name. """ + try: + module_name = "pnf-subscriptions" + conn = sr.Connection(module_name) + sess = sr.Session(conn) + subscribe = sr.Subscribe(sess) + subscribe.module_change_subscribe(module_name, module_change_cb) + sr.global_loop() + print("Application exit requested, exiting.") + except Exception as error: + print(error) + + +if __name__ == '__main__': + start() |