-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathncc-yang-push
executable file
·336 lines (306 loc) · 10.9 KB
/
ncc-yang-push
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python
#
# Copyright (c) 2019 Cisco and/or its affiliates
#
import sys
from argparse import ArgumentParser
from functools import partial
from ncclient import manager
from lxml import etree
import logging
import time
import os
#
# Capability constants
#
NC_WRITABLE_RUNNING = 'urn:ietf:params:netconf:capability:writable-running:1.0'
NC_CANDIDATE = 'urn:ietf:params:netconf:capability:candidate:1.0'
NC_NOTIF_1_1 = 'urn:ietf:params:netconf:capability:notification:1.1'
#
# simple XML template to replace periodic suscriptions via netconf
#
replace_periodic_subscription = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<mdt-config-data xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-mdt-cfg">
<mdt-subscription nc:operation="replace">
<subscription-id>{subs_id}</subscription-id>
<base>
<stream>yang-push</stream>
<encoding>encode-kvgpb</encoding>
<period>{period}</period>
<xpath>{xpath}</xpath>
</base>
<mdt-receivers>
<address>{receiver_ipv4}</address>
<port>{receiver_port}</port>
<protocol>grpc-tcp</protocol>
</mdt-receivers>
</mdt-subscription>
</mdt-config-data>
</config>
'''
#
# simple XML template to replace on-change suscriptions via netconf
#
replace_onchange_subscription = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<mdt-config-data xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-mdt-cfg">
<mdt-subscription nc:operation="replace">
<subscription-id>{subs_id}</subscription-id>
<base>
<stream>yang-push</stream>
<encoding>encode-kvgpb</encoding>
<dampening-period>{dampening_period}</dampening-period>
<xpath>{xpath}</xpath>
</base>
<mdt-receivers>
<address>{receiver_ipv4}</address>
<port>{receiver_port}</port>
<protocol>grpc-tcp</protocol>
</mdt-receivers>
</mdt-subscription>
</mdt-config-data>
</config>
'''
#
# siple XML template to remove a subscription by id
#
del_subscription = '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<mdt-config-data xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-mdt-cfg">
<mdt-subscription nc:operation="remove">
<subscription-id>{subs_id}</subscription-id>
</mdt-subscription>
</mdt-config-data>
</config>
'''
#
# display template for a slightly more human readable view of the key
# attributes of a subscription
#
subscription_periodic = '''
Subscription Id {sub_id}
Receiver {receiver_ipv4}:{receiver_port}
Period {period}
XPath {xpath}
'''
def replace_subscription(
edit_config,
subs_id=None,
xpath=None,
receiver_ipv4=None,
receiver_port=None,
period=None,
dampening_period=None):
'''
Replace (with netconf semantics) the subscription identified by
subs_id with the definition passed in, using the edit-config
function passed in.
'''
assert edit_config is not None
assert subs_id is not None
assert xpath is not None
assert receiver_ipv4 is not None
assert receiver_port is not None
if period is not None:
edit_config(replace_periodic_subscription.format(
subs_id=subs_id,
xpath=xpath,
receiver_ipv4=receiver_ipv4,
receiver_port=receiver_port,
period=period))
elif dampening_period is not None:
edit_config(replace_onchange_subscription.format(
subs_id=subs_id,
xpath=xpath,
receiver_ipv4=receiver_ipv4,
receiver_port=receiver_port,
dampening_period=dampening_period))
else:
print('Must have period or dampening period!', file=sys.stderr)
def delete_subscription(
edit_config,
subs_id=None):
'''
Delete a subscription by subscription-id, using the edit-confog
function passed in.
'''
assert edit_config is not None
assert subs_id is not None
edit_config(del_subscription.format(subs_id=subs_id))
def list_subscriptions(m):
'''
Get and list, in a human readable format, all the subscriptions on
the device passed in.
'''
c = m.get_config(source='running', filter=('xpath', '/mdt-config-data'))
subs = c.data.xpath('//*[local-name()="mdt-subscription"]')
for sub in subs:
#
# TOTO: somewhat messy code here. Could be better. An XSLT for
# example, would be much nicer!
#
period = None
dampening_period = None
sub_id = sub.xpath('*[local-name()="subscription-id"]')[0].text
xpath = sub.xpath('*/*[local-name()="xpath"]')[0].text
receiver_ipv4 = sub.xpath('*/*[local-name()="address"]')[0].text
receiver_port = sub.xpath('*/*[local-name()="port"]')[0].text
try:
period = sub.xpath('*/*[local-name()="period"]')[0].text
except:
pass
try:
dampening_period = sub.xpath('*/*[local-name()="dampening-period"]')[0].text
except:
pass
if period:
print(subscription_periodic.format(
sub_id=sub_id,
xpath=xpath,
receiver_ipv4=receiver_ipv4,
receiver_port=receiver_port,
period=period))
def compose_edit_config(m):
'''
Look at the capabilities of the device we are connected to for the
session, and create and return a very specific function to do
single edit-config operations against that device, including
proper locking and unlocking and use of commit if necessary.
'''
if NC_CANDIDATE in m.server_capabilities:
def candidate_edit_config(data):
m.lock(target='running')
m.lock(target='candidate')
m.edit_config(data,
format='xml',
target='candidate')
m.commit()
m.unlock(target='candidate')
m.unlock(target='running')
return candidate_edit_config
else:
def writable_running_edit_config(data):
m.lock(target='running')
m.edit_config(data,
format='xml',
target='running')
m.unlock(target='running')
return writable_running_edit_config
if __name__ == '__main__':
parser = ArgumentParser(description='Select your YANG push 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="Do I really need to explain?")
# basic action options
g = parser.add_mutually_exclusive_group()
g.add_argument('-l', '--list-subscriptions', action='store_true',
help="List all subscriptions and key parameters")
g.add_argument('-a', '--add-subscription', action='store_true',
help="Add a subscription with provided id")
g.add_argument('-d', '--delete-subscription', action='store_true',
help="Delete a subscription with provided id")
#
# parameters for adding a subscription
#
# fixed fields:
# - stream = yang-push
# - encoding = encode-kvgpb
# - protocol = grpc-tcp
#
# configurable fields
# - subscription id
# - xpath
# - period or dampening-period (mutually exclusive)
# - receiver ipv4 address
# - receiver port
#
parser.add_argument('-s', '--subscription-id', type=int,
help="Subscription id")
parser.add_argument('-x', '--xpath', type=str,
help="The XPath to subscribe to")
parser.add_argument('--rx-ipv4', type=str,
help="Receiver IPv4 address")
parser.add_argument('--rx-port', type=int,
help="Receiver port")
g = parser.add_mutually_exclusive_group()
g.add_argument('--period', type=int, default=None,
help="Subscription period")
g.add_argument('--dampening-period', type=int, default=None,
help="Subscription dampening period")
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)
#
# check we can do this; looks ugly because IOS XE advertises
# NC_NOTIF_1_1 with extra whitespace characters :-(
#
has_yang_push = False
for cap in m.server_capabilities:
if NC_NOTIF_1_1 in cap:
has_yang_push = True
break
if not has_yang_push:
print('NETCONF server does not have capability {}'.format(NC_NOTIF_1_1),
file=sys.stderr)
m.close_session()
sys.exit(1)
#
# create appropriate edit config function
#
fn_edit_config = compose_edit_config(m)
#
# do requested operation
#
if args.list_subscriptions:
list_subscriptions(m)
elif args.add_subscription:
replace_subscription(
fn_edit_config,
subs_id=args.subscription_id,
xpath=args.xpath,
receiver_ipv4=args.rx_ipv4,
receiver_port=args.rx_port,
period=args.period,
dampening_period=args.dampening_period)
elif args.delete_subscription:
delete_subscription(
fn_edit_config,
args.subscription_id)
#
# graceful shutdown
#
m.close_session()