forked from CiscoDevNet/ncc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncc-establish-subscription.py
executable file
·159 lines (142 loc) · 5.63 KB
/
ncc-establish-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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
#
# Copyright (c) 2018 Cisco and/or its affiliates
#
import datetime
import logging
import os
import signal
import sys
import time
from argparse import ArgumentParser
from lxml import etree
from ncclient import manager
from ncclient.transport.session import SessionListener
if __name__ == '__main__':
parser = ArgumentParser(description='Select your telemetry parameters:')
# Input parameters
parser.add_argument('--host', type=str,
default=os.environ.get('NCC_HOST','127.0.0.1'),
help="The IP address for the device to connect to "
"(default localhost)")
parser.add_argument('-u', '--username', type=str,
default=os.environ.get('NCC_USERNAME', 'cisco'),
help="Username to use for SSH authentication "
"(default 'cisco')")
parser.add_argument('-p', '--password', type=str,
default=os.environ.get('NCC_PASSWORD', 'cisco'),
help="Password to use for SSH authentication "
"(default 'cisco')")
parser.add_argument('--port', type=int,
default=os.environ.get('NCC_PORT',830),
help="Specify this if you want a non-default port "
"(default 830)")
parser.add_argument('-v', '--verbose', action='store_true',
help="Exceedingly verbose logging to the console")
parser.add_argument('--delete-after', type=int,
help="Delete the established subscription after N seconds")
parser.add_argument('-x', '--xpaths', type=str, nargs='+', required=True,
help="List of xpaths to subscribe to, one or more")
parser.add_argument('--streamns', type=str,
help="Namespace for a custom stream identity, "
"e.g. urn:cisco:params:xml:ns:yang:cisco-xe-ietf-yang-push-ext")
parser.add_argument('--callback', type=str,
help="Module that a callback is defined in")
g = parser.add_mutually_exclusive_group(required=True)
g.add_argument('--period', type=int,
help="Period in centiseconds for periodic subscription")
g.add_argument('--dampening-period', type=int,
help="Dampening period in centiseconds for on-change subscription")
g.add_argument('--streamident', type=str,
help="Custom stream identifier (e.g. yang-notif-native)")
args = parser.parse_args()
if args.verbose:
handler = logging.StreamHandler()
for l in ['ncclient.transport.ssh', 'ncclient.transport.session', 'ncclient.operations.rpc']:
logger = logging.getLogger(l)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
#
# Connect
#
def unknown_host_cb(host, fingerprint):
return True
m = manager.connect(host=args.host,
port=args.port,
username=args.username,
password=args.password,
allow_agent=False,
look_for_keys=False,
hostkey_verify=False,
unknown_host_cb=unknown_host_cb)
#
# set up a ctrl+c handler to tear down the netconf session
#
def sigint_handler(signal, frame):
m.close_session()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
#
# A really simple callback, just spit out a header with some key
# details plus the XML payload pretty-printed.
#
def callback(notif):
print('-->>')
print('(Default Callback)')
print('Event time : %s' % notif.event_time)
print('Subscription Id : %d' % notif.subscription_id)
print('Type : %d' % notif.type)
print('Data :')
print(etree.tostring(notif.datastore_ele, pretty_print=True).decode('utf-8'))
print('<<--')
def errback(notif):
pass
#
# Select the callback to use
#
selected_init = None
selected_callback = callback
selected_errback = errback
if args.callback:
import importlib
module = importlib.import_module(args.callback)
selected_init = getattr(module, 'init')
selected_callback = getattr(module, 'callback')
selected_errback = getattr(module, 'errback')
#
# If we have a callback "module" specified, call its init function
# with the list of xpaths we are dealing with.
#
if selected_init:
selected_init(args.xpaths)
#
# iterate over the list of xpaths and create subscriptions
#
subs = []
for xpath in args.xpaths:
s = m.establish_subscription(
selected_callback,
selected_errback,
xpath=xpath,
period=args.period,
dampening_period=args.dampening_period,
streamident=args.streamident,
streamns=args.streamns)
print('Subscription Result : %s' % s.subscription_result)
if s.subscription_result.endswith('ok'):
print('Subscription Id : %d' % s.subscription_id)
subs.append(s.subscription_id)
if not len(subs):
print('No active subscriptions, exiting.')
sys.exit(1)
#
# simple forever loop
#
if args.delete_after:
time.sleep(args.delete_after)
for s in subs:
r = m.delete_subscription(s)
print('delete subscription result = %s' % r.subscription_result)
else:
while True:
time.sleep(5)