03-23-2018 01:22 PM - edited 03-01-2019 04:07 AM
Got a requirement to create a NSO action wherein customer want ability to run action to see its dry-run output, before actually committing the action. Is it possible? Any example would be great help.
Solved! Go to Solution.
03-23-2018 01:32 PM
Dry-run is a service action, here is an example of how it can be run, this does native device output, can also used cli and xml - see tailf-ncs-services.yang for details:
# Run dry-run before committing
dryRun = root.services.commit_dry_run
drInput = dryRun.get_input()
drInput.outformat = 'native'
self.log.info('tryAction: Dry-run outformat : ', drInput.outformat)
drOutput = dryRun(drInput)
if drOutput.native != None :
devlist = drOutput.native.device
for dev in devlist :
self.log.info('tryAction: Dry-run device: ', dev.name)
self.log.info('tryAction: Dry-run data: ', dev.data)
-Larry
03-23-2018 01:32 PM
Dry-run is a service action, here is an example of how it can be run, this does native device output, can also used cli and xml - see tailf-ncs-services.yang for details:
# Run dry-run before committing
dryRun = root.services.commit_dry_run
drInput = dryRun.get_input()
drInput.outformat = 'native'
self.log.info('tryAction: Dry-run outformat : ', drInput.outformat)
drOutput = dryRun(drInput)
if drOutput.native != None :
devlist = drOutput.native.device
for dev in devlist :
self.log.info('tryAction: Dry-run device: ', dev.name)
self.log.info('tryAction: Dry-run data: ', dev.data)
-Larry
03-24-2018 11:38 AM
Hi Larry,
Thanks for sharing above way to output dry run output. But i am still missing something.
My action(Its not a service) is applying a template. So requirement is to run this action to produce dry-run output before actually applying changes to network.
So my action yang is taking an input argument to check if invocation is just to produce dry-run or actual commit.
based on this argument, i need to write python logic to either just produce dry-run output for user OR actual push.
Thanks
Vishal.
03-25-2018 07:09 AM
Hi Vishal
Larry gave you the correct way to do this...
But to add a few more details, assuming you have (in part) some YANG like this:
container action {
tailf:action trystuff {
tailf:actionpoint tempBar-action;
input {
(...skip)
leaf doDryCommit {
tailf:info "dryrun or commit";
type enumeration {
enum dry-run;
enum commit;
}
default "dry-run";
}
} // input
then you Python will look (something) like this:
with ncs.maapi.single_write_trans('admin', 'tempBar') as t:
try:
vars = ncs.template.Variables()
vars.add('DEVICE', input.device)
vars.add('HNAME', input.hname)
vars.add('DNAME', input.dname)
#
root = ncs.maagic.get_root(t)
context_node = root.ncs__devices
tmpl = ncs.template.Template(context_node)
tmpl.apply('tempBar-template', vars)
#....
# dryrun-native
#....
if str(input.doDryCommit) == 'dry-run':
dryRun = root.services.commit_dry_run
drInput = dryRun.get_input()
drInput.outformat = 'native'
drOutput = dryRun(drInput)
if drOutput.native != None :
devlist = drOutput.native.device
if len(devlist) != 0:
for dev in devlist:
self.log.info('tempBar: Dry-run device : ', dev.name)
self.log.info('tempBar: Dry-run output : ', dev.data)
#....
else:
self.log.info('tempBar: Dry-run device : ', dev.name)
self.log.info('tempBar: Dry-run output : NO Dry-Run found')
#....
# commit
#....
else: # commit selected
self.log.info('tempBar: commit template apply')
# commit transaction
t.apply()
self.log.info('tryAction: Host ... : ', input.hname)
output.result = "all done"
except Exception, cause:
res = str(cause)
self.log.info('tryAction: Exception: %s ', res)
output.result = "exception"
finally:
t.finish_trans()
hope that helps
Gregg
03-25-2018 11:17 AM
Thanks Gregg for detailed example.
I am able to implement it in my action, appriciate your inputs. Thanks to Larry too.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide