08-04-2017 03:58 PM - edited 03-01-2019 03:56 AM
Python code demonstrating a validation callback.
# -*- mode: python; python-indent: 4 -*-
import ncs
from ncs.dp import Daemon
from ncs.maapi import Maapi
from _ncs.dp import register_valpoint_cb, register_trans_validate_cb, trans_set_fd
class Validation(object):
def __init__(self, log):
self.log = log
self.daemon = Daemon("foo-validation-daemon", log=log)
self.maapi = Maapi()
register_trans_validate_cb(self.daemon.ctx(), self)
register_valpoint_cb(self.daemon.ctx(),
'the-validation-callpoint-name',
self)
self.daemon.start()
def cb_validate(self, tctx, kp, newval):
self.log.info('validate called')
# return ncs.CONFD_ERR to fail the validation otherwise
# ncs.CONFD_OK
return ncs.CONFD_OK
def cb_init(self, tctx):
self.maapi.attach(tctx)
trans_set_fd(tctx, self.daemon._wsock)
def cb_stop(self, tctx):
self.maapi.detach(tctx)
# ---------------------------------------------
# COMPONENT THREAD THAT WILL BE STARTED BY NCS.
# ---------------------------------------------
class Main(ncs.application.Application):
def setup(self):
# The application class sets up logging for us. It is accessible
# through 'self.log' and is a ncs.log.Log instance.
self.log.info('Main RUNNING')
self.v = Validation(self.log)
# If we registered any callback(s) above, the Application class
# took care of creating a daemon (related to the service/action point).
# When this setup method is finished, all registrations are
# considered done and the application is 'started'.
def teardown(self):
# When the application is finished (which would happen if NCS went
# down, packages were reloaded or some error occurred) this teardown
# method will be called.
self.log.info('Main FINISHED')
08-06-2017 02:52 PM
Hi Fredrik,
Great example as I had this question this week.
One point that you may want to add is a description in the cb_validate() method where you clarify that there is where validation happens and which are the accepted outputs (other than CONFD_OK, I believe it is CONFD_ERR).
Finally, I was wondering what is the format for "newval" when the callback is applied to a list instead of a leaf. The values of the keys? are they comma separated?
Roque
08-06-2017 10:14 PM
Not sure what newval actually does, it's always 'undefined' in my tests.
If you have a list, cb_validation is called once for each list entry.
08-07-2017 04:08 AM
newval is supposed to hold the new value of the leaf, if the validation point is placed on a leaf. Did you try that, and if so, did you find the mechanism is broken?
08-07-2017 04:18 AM
Hi Jan,
Personally, that was my understanding and why I was asking how tailf:validate works on lists.
Roque
08-07-2017 04:28 AM
For a validation point placed on a list (or container, type empty leaf, ...) , newval will be undefined. You will need to read the value(s) you want using MAAGIC/MAAPI etc.
08-07-2017 07:27 AM
Makes sense now, I never tried to att the validation point to a single leaf.
02-15-2018 04:15 PM
Hi Fredrik,
I'm looking for a way to return a message to the user when validation fails.
Returning ncs.CONFD_ERR I get:
admin@ncs(config-device-IOS-0)# description srt
admin@ncs(config-device-IOS-0)# commit
Aborted: application error
Which is not very user friendly.
By raising an exception I can customize a message to the user. For instance:
admin@ncs(config-device-IOS-0)# description 123
admin@ncs(config-device-IOS-0)# commit
Aborted: 'services loopback device IOS-0 description': Python cb_validate error. This is not a good description
Is that the proper way to do it or is there a better option?
Thanks
Marcelo
02-16-2018 01:17 AM
You have a python validation callback defined? Or an error message register_error_cb(...) formatter callback? Not entirely clear. Basically you can use error_seterr(...)
02-16-2018 06:28 AM
Hi Jan,
I have a validation callback defined, similar to the example above. It seems I can't call error_seterr from cb_validate as it expects an _ncs.UserInfo object, which I don't have within cb_validate. Would I need a register_error_cb for that?
Thanks
Marcelo
01-30-2019 10:24 PM
Hi,
As per the example of above, New Daemon(Independent Thread) is registered to handle tailf:validation.
Can we attach the validation callback to existing Service(Service uses Daemon internally)
Or this is the only way exists ?
Regards
Zahid
02-08-2019 09:59 AM
You mean to tie the validation callback to a service class? I'm actually not sure... but I guess the question is why? :)
02-08-2019 10:00 AM
You mean to tie the validation callback to a service class? I'm actually not sure... but I guess my question is why? :)
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