-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr-create-subscription.py
86 lines (71 loc) · 3.93 KB
/
csr-create-subscription.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
import subprocess
from ncclient import manager
from ncclient.xml_ import to_ele
if len(sys.argv) < 4:
print("Error - Incorrect arguments")
print('Usage: python3 csr-create-subscription.py <container_name> <XPath> <subscription_type> [<period_in_cs>]')
print('Periodic subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/interfaces-state/interface[name=\'GigabitEthernet2\']" periodic 1000')
print('On-Change subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/native/hostname" on-change')
exit(1)
else:
container_name = sys.argv[1]
check_container = subprocess.getoutput("docker ps -a | awk '{print $NF}' | grep " + container_name)
if check_container != container_name:
print("Error - Incorrect arguments: You need to specify the container name of the network device.")
print('Usage: python3 csr-create-subscription.py <container_name> <XPath> <subscription_type> [<period_in_cs>]')
print('Periodic subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/interfaces-state/interface[name=\'GigabitEthernet2\']" periodic 1000')
print('On-Change subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/native/hostname" on-change')
exit(1)
else:
if sys.argv[3] != "on-change" and sys.argv[3] != "periodic":
print("Error - Incorrect arguments")
print('Usage: python3 csr-create-subscription.py <container_name> <XPath> <subscription_type> [<period_in_cs>]')
print('Periodic subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/interfaces-state/interface[name=\'GigabitEthernet2\']" periodic 1000')
print('On-Change subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/native/hostname" on-change')
exit(1)
elif sys.argv[3] == "periodic" and len(sys.argv) != 5:
print("Error - Incorrect arguments for periodic subscription")
print('Usage: python3 csr-create-subscription.py <container_name> <XPath> <subscription_type> [<period_in_cs>]')
print('Periodic subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/interfaces-state/interface[name=\'GigabitEthernet2\']" periodic 1000')
exit(1)
elif sys.argv[3] == "on-change" and len(sys.argv) != 4:
print("Error - Incorrect arguments for on-change subscription")
print('Usage: python3 csr-create-subscription.py <container_name> <XPath> <subscription_type> [<period_in_cs>]')
print('On-Change subscription example: python3 csr-create-subscription.py clab-telemetry-testbed-r1 "/native/hostname" on-change')
exit(1)
r = {
"host": container_name,
"port": 830,
"username": "admin",
"password": "admin",
"hostkey_verify": False,
"device_params": {"name": "csr"}
}
session = manager.connect(**r)
print ("\nSession ID: ", session.session_id)
if (sys.argv[3]=="periodic"):
subscription = "period"
period = sys.argv[4]
else:
subscription = "dampening-period"
period = 0
xpath = sys.argv[2]
# When building the RPC request XML, use dampening-period for on-change notifications (when supported).
# Otherwise, use period and specify an integer value for the time in centiseconds.
rpc = """
<establish-subscription xmlns="urn:ietf:params:xml:ns:yang:ietf-event-notifications"
xmlns:yp="urn:ietf:params:xml:ns:yang:ietf-yang-push">
<stream>yp:yang-push</stream>
<yp:xpath-filter>{0}</yp:xpath-filter>
<yp:{1}>{2}</yp:{1}>
</establish-subscription>
""".format(xpath, subscription, period)
request = session.dispatch(to_ele(rpc))
print(request)
print("\nYANG-Push notifications for XPath " + sys.argv[2] + " of network device "+ container_name + ": \n")
while True:
sub_data = session.take_notification()
if (sub_data != None):
print(sub_data.notification_xml)
print("------------------------")