blob: 44109a12da19a6271aeba3435c76f6ba85f3eb1f [file] [log] [blame]
rajendrajaiswal6a61ad82019-12-16 14:24:02 +00001#!/usr/bin/env python
2import re
3import sysrepo as sr
4from pnf import PNF
5
6
7def module_change_cb(sess, module_name, event, private_ctx):
8 """ Handle event change based on yang operation. """
9 try:
10 change_path = "/" + module_name + ":*"
11 iterate = sess.get_changes_iter(change_path)
12 change = sess.get_change_next(iterate)
13 changelist = []
14 operation = change.oper()
15 pnf = PNF()
16 if event == sr.SR_EV_APPLY:
17 print("------------------> Start Handle Change <------------------")
18 if operation == sr.SR_OP_CREATED:
19 while True:
20 change = sess.get_change_next(iterate)
21 if change is None:
22 break
23 changelist.append(change.new_val().to_string())
24 result = re.findall(r'\'(.*?)\'', changelist[0])
25 jobid = result[0]
26 print("Subscription Created : " + changelist[0])
27 pnf.create_job_id(jobid, changelist)
28 pnf.pm_job()
29 elif operation == sr.SR_OP_DELETED:
30 changelist.append(change.old_val().to_string())
31 result = re.findall(r'\'(.*?)\'', changelist[0])
32 jobid = result[0]
33 print("Subscription Deleted : " + changelist[0])
34 pnf.delete_job_id(jobid)
35 pnf.pm_job()
36 elif operation == sr.SR_OP_MODIFIED:
37 changelist.append(change.new_val().to_string())
38 element = changelist[0]
39 print("Subscription Modified :" + element)
40 result = re.findall(r'\'(.*?)\'', changelist[0])
41 jobid = result[0]
42 administrative_state = ((element.rsplit('/', 1)[1]).split('=', 1))[1].strip()
43 if administrative_state == "LOCKED":
44 pnf.delete_job_id(jobid)
45 pnf.pm_job()
46 elif administrative_state == "UNLOCKED":
47 select_xpath = "/" + module_name + ":*//*"
48 values = sess.get_items(select_xpath)
49 if values is not None:
50 for i in range(values.val_cnt()):
51 if jobid in values.val(i).to_string():
52 changelist.append(values.val(i).to_string())
53 pnf.create_job_id(jobid, changelist)
54 pnf.pm_job()
55 else:
56 print("Unknown Operation")
57 print("------------------> End Handle Change <------------------")
58 except Exception as error:
59 print(error)
60 return sr.SR_ERR_OK
61
62
63def start():
64 """ main function to create connection based on moudule name. """
65 try:
66 module_name = "pnf-subscriptions"
67 conn = sr.Connection(module_name)
68 sess = sr.Session(conn)
69 subscribe = sr.Subscribe(sess)
70 subscribe.module_change_subscribe(module_name, module_change_cb)
71 sr.global_loop()
72 print("Application exit requested, exiting.")
73 except Exception as error:
74 print(error)
75
76
77if __name__ == '__main__':
78 start()