-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathncc-capture-schema
executable file
·640 lines (574 loc) · 21.3 KB
/
ncc-capture-schema
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/usr/bin/env python
#
# Copyright (c) 2018 Cisco and/or its affiliates
#
from __future__ import print_function
from bs4 import BeautifulSoup
from argparse import ArgumentParser
from lxml import etree
from ncclient import manager
from ncclient.operations.rpc import RPCError
from nccutil import repoutil
from netmiko import ConnectHandler
from os import listdir, makedirs
from os.path import isfile, join, basename, exists, getsize
import logging
import os
import pyang
import pyang.repository
import pyang.context
import re
import string
import sys
import time
from git.exc import GitCommandError
import json
#
# setup logging
#
logger = logging.getLogger('schemacap')
#
# Check models script
#
check_models = '''
#!/bin/sh
#
# Simple run of pyang with the "--lint" flag over all yang files in
# this directory, ignoring some warnings. Prior to pushing to git, the
# validation was run with pyang 1.5. This script should be run with
# the working doirectory set to a directory containing the yang files
# to run "pyang --lint" over.
#
# The modules as uploaded exhibit a number of RFC 6087 amd RFC 6020
# errors and warnings that are judged to be cosmetic at this time and
# which do not impact the ability of a client to interact with a
# device supporting the module. The exact content ignored may be
# identified by reviewing the "grep -v" commands below.
#
EGREP=`command -v egrep`
GREP=`command -v grep`
PYANG=`command -v pyang`
CHECK_BC=""
PYANG_FLAGS=""
#
# simple function to check for existence of a binary on the current
# path
#
checkExists() {
bin=`command -v $1`
if [ -z "$bin" ]
then
echo this script requires $1 to be on your path
exit 1
fi
}
#
# check we have the utilties we need
#
checkExists pyang
checkExists egrep
checkExists grep
#
# brief help for the options we support
#
show_help () {
echo Options for check-models.sh:
printf "\n"
printf " -h Show this help\n"
printf " -b <ver> Check backwards compatibility\n"
printf "\n"
}
OPTIND=1
while getopts "hb:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
b) CHECK_BC="$OPTARG"
;;
esac
done
#
# Run pyang over all the yang modules, ignoring certain errors and
# warnings.
#
echo Checking all models with "--lint" flag
for m in *.yang
do
pyang $PYANG_FLAGS --lint $m 2>&1 | \\
grep -v "warning: RFC 6087" | \\
grep -v "error: RFC 6087: 4.2" | \\
grep -v "error: RFC 6087: 4.7" | \\
grep -v "error: RFC 6087: 4.11,4.12" | \\
grep -v "error: RFC 6087: 4.12" | \\
grep -v "not in canonical order" | \\
grep -v "warning: locally scoped grouping" | \\
egrep -v "warning: imported module\s[a-zA-Z0-9\-]+\snot used"
done
#
# Check if we're doing a BC check, if not, exit status 0
#
if [ -z "$CHECK_BC" ]; then
exit 0
fi
#
# Run pyang over all the models in the 533 directory that also exist
# in the 532 peer directory, using the --check-update-from option.
# This requires pyang 1.5 or better, so we check this first.
#
version=`pyang --version | awk '{print $NF}'`
if ! awk -v ver="$version" 'BEGIN { if (ver < 1.5) exit 1; }'; then
printf 'ERROR: pyang version 1.5 or higher required\n'
exit 1
fi
UPDATE_FROM_PATH=../../../../standard/ietf/RFC
echo Comparing all models that also exist in ../$CHECK_BC AND that have
echo changed since version 600 with "--check-update-from" flag:
echo
for m in *.yang
do
VER_FROM="../$CHECK_BC/$m"
if [ -e "$VER_FROM" ]
then
DIFF=`diff $VER_FROM $m`
if [ ! -z "$DIFF" ]
then
pyang \
--check-update-from $VER_FROM \
--check-update-from-path $UPDATE_FROM_PATH \
$m
fi
fi
done
'''
#
# The get filter we need to retrieve the schemas a device claims to have
#
schemas_filter = '''<netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
<schemas/>
</netconf-state>'''
#
# print to stderr
#
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
#
# return an etree of the data retrieved
#
def get(m, filter=None):
if filter and len(filter) > 0:
c = m.get(filter=('subtree', filter))
else:
c = m.get()
return c
#
# get a list of schema and save to the provided directory
#
def get_schema(m, schema_nodes, output_dir):
failed_download = []
for s, v in schema_nodes:
try:
logger.log(logging.INFO, 'Downloading schema %s@%s...' % (s, v))
c = m.get_schema(s, version=v)
with open(output_dir+'/'+s+'@'+v+'.yang', 'w') as yang:
print(c.data, file=yang)
yang.close()
except RPCError as e:
logger.log(logging.INFO, 'Failed to download schema %s@%s' % (s, v))
failed_download.append((s,v))
return failed_download
if __name__ == '__main__':
parser = ArgumentParser(description='Provide device and output 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('--ssh-port', type=int, required=False, default=22,
help="Optional port to contact for plain ssh")
parser.add_argument('--device-type', type=str, default='cisco_xr',
help="Device type connecting to for netmiko")
parser.add_argument('-t', '--timeout', type=int, required=False, default=30,
help="Netconf timeout; needed for slow devices")
parser.add_argument('--git-repo', required=True, type=str,
help='Git reository to capture data to; should include any credentials required')
parser.add_argument('--git-path', required=True, type=str,
help='Relative path in git repository to place schema and capabilities')
parser.add_argument('-v', '--verbose', action='store_true',
help="Do some verbose logging")
parser.add_argument('--trace', action='store_true',
help="Trace schema capture tasks specifically")
args = parser.parse_args()
#
# if you enable verbose logging, it is INCREDIBLY verbose...you
# have been warned!!
#
if args.verbose:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s'))
for l in ['ncclient.transport.ssession',
'ncclient.operations.rpc']:
ll = logging.getLogger(l)
ll.addHandler(handler)
ll.setLevel(logging.DEBUG)
#
# Setup schema capture-specific logs
#
if args.trace:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s'))
for l in ['schemacap']:
ll = logging.getLogger(l)
ll.addHandler(handler)
ll.setLevel(logging.DEBUG)
#
# Initialize OS & version strings for targetdir
#
ver = 'unknown'
os = 'unknown'
#
# Connect over netmiko
#
logger.log(logging.INFO, 'Connecting to using plain SSH to %s:%d' % (args.host, args.ssh_port))
platform_metadata = {
'vendor': 'cisco',
'product-ids': [],
'name': '',
'os-type': '',
'software-flavor': '',
'software-version': '',
'module-list-file': {}
}
#
# Connect over netmiko
#
d = ConnectHandler(device_type=args.device_type,
ip=args.host,
port=args.ssh_port,
username=args.username,
password=args.password)
version_output = d.send_command('show version')
if args.device_type=='cisco_xr':
logger.log(logging.INFO, 'Dealing with an IOS-XR device')
os = 'xr'
platform_metadata['os-type'] = 'IOS-XR'
# TODO What do we want to track for software flavor?
platform_metadata['software-flavor'] = 'ALL'
inventory_output = d.send_command('show inventory all | begin Chassis')
v = re.search(
r'Version +:? *([0-9\.A-Z]+)',
version_output)
if v is not None:
ver = v.group(1)
platform_metadata['software-version'] = v.group(1)
pn = re.search(
r'^cisco ([^\(]+)\(',
version_output, re.M)
if pn is not None:
platform_metadata['name'] = pn.group(1).replace('Series', '').strip().replace(' ', '-')
else:
platform_metadata['name'] = 'ios-xr'
pid = re.search(
r'PID: ([^,]+),', inventory_output)
if pid is not None:
platform_metadata['product-ids'].append(pid.group(1).strip())
else:
inventory_output = d.send_command('show inventory rack')
pid = re.search(
r'^\s+ 0\s+([^\s]+)',
inventory_output, re.M)
if pid is not None:
platform_metadata['product-ids'].append(pid.group(1))
elif args.device_type=='cisco_ios' or args.device_type=='cisco_xe':
logger.log(logging.INFO, 'Dealing with an IOS/IOS-XE device')
os = 'xe'
platform_metadata['os-type'] = 'IOS-XE'
# TODO: Do we want to track licenses for XE here?
platform_metadata['software-flavor'] = 'ALL'
inventory_output = d.send_command('show inventory')
v = re.search(
r'Cisco IOS XE Software, Version ([a-zA-Z0-9_\.]+)',
version_output)
if v is not None:
ver = v.group(1)
platform_metadata['software-version'] = v.group(1)
# This pattern seems complex, but it allows us to get the "C3850" part out
# of "WS-C3850-48P" as an example.
pn = re.search(
r'^cisco (WS-)?([a-zA-Z0-9\-/]+?)(-[0-9][0-9A-Z]+)? \([^\)]+\) processor',
version_output, re.M)
if pn is not None:
platform_metadata['name'] = pn.group(2)
else:
platform_metadata['name'] = 'ios-xe'
pid = re.search(
r'PID: ([^,]+),', inventory_output)
if pid is not None:
platform_metadata['product-ids'].append(pid.group(1).strip())
elif args.device_type=='cisco_nxos':
logger.log(logging.INFO, 'Dealing with an NX-OS device')
os = 'nx'
platform_metadata['os-type'] = 'NX-OS'
# TODO: What do we want to track for NX-OS?
platform_metadata['software-flavor'] = 'ALL'
inventory_output = d.send_command('show inventory')
v = re.search(r'^\s+NXOS: version ([0-9A-Za-z\.\(\)_]+)',
version_output, re.M)
if v is not None:
ver = v.group(1).replace('(', '-').replace(')', '-').strip('-')
platform_metadata['software-version'] = v.group(1)
pn = re.search(r'^\s+cisco ([^\s]+)\s.*Chassis',
version_output, re.M)
if pn is not None:
platform_metadata['name'] = pn.group(1)
else:
platform_metadata['name'] = 'nx-os'
pid = re.search(
r'PID: ([^,]+),', inventory_output)
if pid is not None:
platform_metadata['product-ids'].append(pid.group(1).strip())
logger.log(logging.INFO, 'Found device software version \'%s\'' % ver)
args.git_path = '%s/%s/%s' % (args.git_path, os, ver)
logger.log(logging.INFO, 'Capturing schemas to relative path %s' % args.git_path)
#
# Pull down the repo and create the file output directory
#
logger.log(logging.INFO, 'Cloning target git repository...')
repo = repoutil.RepoUtil(args.git_repo)
repo.clone()
logger.log(logging.INFO, 'Cloned target git repository to %s' % repo.localdir)
#
# start creating metadata
#
targetdir = repo.localdir + '/' + args.git_path
caps_name = platform_metadata['name'].lower().replace('/', '_').replace(':', '_').replace('\\', '_') + '-capabilities.xml'
caps_file = targetdir + '/' + caps_name
platform_metadata['module-list-file']['type'] = 'capabilities'
platform_metadata['module-list-file']['path'] = args.git_path + '/' + caps_name
platform_metadata['module-list-file']['owner'] = repo.get_repo_owner()
platform_metadata['module-list-file']['repository'] = repo.get_repo_dir()
#
# create any missing directories in the repo
#
if not exists(targetdir):
makedirs(targetdir)
# testing
# targetdir = '.' + '/' + args.git_path
# if not exists(targetdir):
# makedirs(targetdir)
#
# Connect to the router
#
logger.log(logging.INFO, 'Connecting using netconf to %s:%d' % (args.host, args.port))
def unknown_host_cb(host, fingerprint):
return True
mgr = manager.connect(host=args.host,
port=args.port,
username=args.username,
password=args.password,
timeout=args.timeout,
allow_agent=False,
look_for_keys=False,
hostkey_verify=False,
unknown_host_cb=unknown_host_cb)
#
# Attempt to get the ietf-yang-library if available.
# If not, fall back to capabilities.
#
do_caps = False
try:
logger.log(logging.INFO, 'Trying to use YANG Library...')
response = mgr.get(('xpath', '/modules-state')).xml
lib_data = etree.fromstring(response)
lib_tags = lib_data.findall('.//{urn:ietf:params:xml:ns:yang:ietf-yang-library}modules-state')
if len(lib_tags) == 0:
logger.log(logging.INFO, 'YANG Library not supported!')
raise Exception('No support for ietf-yang-library')
with open(caps_file, 'w') as capsfile:
for lib_tag in lib_tags:
capsfile.write(etree.tostring(lib_tag, pretty_print=True))
capsfile.close()
platform_metadata['module-list-file']['type'] = 'yang-library'
except (RPCError, Exception) as rpce:
do_caps = True
if do_caps:
#
# Save out capabilities
#
logger.log(logging.INFO, 'Logging capabilities')
with open(caps_file, 'w') as capsfile:
capsfile.write('''<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n <capabilities>\n''')
for c in mgr.server_capabilities:
capsfile.write(' <capability>{}</capability>\n'.format(c))
capsfile.write(''' </capabilities>\n</hello>\n''')
capsfile.close()
#
# Save out metadata (append if it exists)
#
md_file = targetdir + '/' + 'platform-metadata.json'
md = {}
md['platforms'] = { 'platform': [] }
if isfile(md_file) and getsize(md_file) > 0:
mdfile = open(md_file, 'r')
md = json.load(mdfile)
mdfile.close()
found_platform = False
for platform in md['platforms']['platform']:
if platform['vendor']=='cisco' and platform['name']==platform_metadata['name']:
found_platform = True
if platform_metadata['product-ids'][0] not in platform['product-ids']:
platform['product-ids'].append(platform_metadata['product-ids'][0])
break
if not found_platform:
md['platforms']['platform'].append(platform_metadata)
else:
md['platforms']['platform'].append(platform_metadata)
mdfile = open(md_file, 'w')
json.dump(md, mdfile, indent=4)
mdfile.close()
#
# Open up a report file
#
logger.log(logging.INFO, 'creating a report file')
reportfile = open(targetdir+'/'+'REPORT.md', 'w')
reportfile.write('# Schema & Capabilities Capture Report\n\n')
reportfile.write('- Operating System: %s\n' % os)
reportfile.write('- Version: %s\n\n' % ver)
#
# retrieve the schemas datatree and extract all the schema
# identifiers
#
logger.log(logging.INFO, 'Retrieving schema identifiers...')
schema_tree = get(mgr, schemas_filter)
soup = BeautifulSoup(schema_tree.data_xml, "lxml")
schema_nodes = [(s.findChild('identifier').getText(),
s.findChild('version').getText())
for s in soup.findAll('schema')]
#
# check the schema list against server capabilities
#
logger.log(logging.INFO, 'Checking schema list against capabilities...')
not_in_schemas = set()
for c in mgr.server_capabilities:
model = re.search('module=([^&]*)&revision=([0-9]+-[0-9]+-[0-9]+)', c)
if model is not None:
m = model.group(1)
v = model.group(2)
logger.log(logging.INFO, 'Schema %s@%s advertised in capabilities' % (m, v))
if (m, v) not in schema_nodes:
logger.log(logging.INFO, 'Schema %s@%s not in /netconf-state/schemas' % (m, v))
not_in_schemas.add((m,v))
if len(not_in_schemas) > 0:
reportfile.write('The following models are advertised in capabilities but are not in schemas tree:\n\n')
for m, v in sorted(not_in_schemas):
reportfile.write('- {}, revision={}\n'.format(m, v))
#
# this dict is for keeping track of the schemas that failed to
# download
#
failed_download = set()
#
# Now download all the schema, which also returns a list of any
# that failed to be downloaded. If we downloaded, list the failed
# downloads (if any).
#
failed = get_schema(mgr, schema_nodes, targetdir)
for f in failed:
failed_download.add(f)
#
# Now let's check all the schema that we downloaded (from this run
# and any other) and parse them with pyang to extract any imports
# or includes and verify that they were on the advertised schema
# list and didn't fail download.
#
# TODO: cater for explicitly revisioned imports & includes
#
logger.log(logging.INFO, 'Checking downloaded schema for imports and includes...')
imports_and_includes = set()
repos = pyang.repository.FileRepository(targetdir)
yangfiles = [f for f in listdir(targetdir) if isfile(join(targetdir, f))]
for fname in yangfiles:
ctx = pyang.context.Context(repos)
fd = open(targetdir+'/'+fname, 'r')
text = fd.read()
ctx.add_module(fname, text)
this_module = basename(fname).split(".")[0]
for ((m,r), module) in ctx.modules.items():
if m==this_module:
for s in module.substmts:
if (s.keyword=='import') or (s.keyword=='include'):
logger.log(logging.INFO, 'Adding import/include %s' % s.arg)
imports_and_includes.add(s.arg)
#
# Verify that all imports and includes appeared in the advertised
# schema
#
schema_list = [m for m, r in schema_nodes]
not_advertised = [i for i in imports_and_includes if i not in schema_list]
if len(not_advertised)>0:
#
# list the not-advertised schemas
#
logger.log(logging.INFO, 'Writing not-advertised schema to report...' % m)
reportfile.write('\nThe following schema are imported or included, but not listed in schemas tree:\n\n')
for m in sorted(not_advertised, key=str.lower):
reportfile.write('- {}\n'.format(m))
#
# try to download the not-advertised schemas
#
for m in not_advertised:
try:
logger.log(logging.INFO, 'Downloading schema %s...' % m)
c = mgr.get_schema(m)
with open(targetdir+'/'+m+'.yang', 'w') as yang:
print(c.data, file=yang)
yang.close()
except RPCError as e:
failed_download.add(str(m))
#
# List out the schema that are imported or included and NOT
# downloaded successfully.
#
if len(failed_download)>0:
logger.log(logging.INFO, 'Writing failed schema downloads to report...' % m)
reportfile.write('\nThe following schema are imported, included or advertised, but not downloadable:\n\n')
for m, v in sorted(failed_download):
reportfile.write('- {}\n'.format(m))
reportfile.write('\n')
#
# Craete check-models.sh
#
with open(targetdir+'/check-models.sh', 'w') as f:
f.write(check_models)
f.close()
#
# cleanup
#
reportfile.close()
#
# Commit everything to local repo and push to origin
#
try:
repo.add_all_untracked()
repo.commit_all(message='Push version %s models.' % ver)
logger.log(logging.INFO, 'Pushing schema updates to repo...')
repo.push()
except GitCommandError as e:
eprint("Add, Commit Or Push Failed:")
eprint(e.stdout)
eprint(e.stderr)
logger.log(logging.INFO, 'Tidying up clone...')
repo.remove()
logger.log(logging.INFO, 'Done!')