Python code demonstrating a validation callback.
# -*- mode: python; python-indent: 4 -*-
from ncs.dp import Daemon
from ncs.maapi import Maapi
from _ncs.dp import register_valpoint_cb, register_trans_validate_cb, trans_set_fd
def __init__(self, log):
self.log = log
self.daemon = Daemon("foo-validation-daemon", log=log)
self.maapi = Maapi()
def cb_validate(self, tctx, kp, newval):
# return ncs.CONFD_ERR to fail the validation otherwise
def cb_init(self, tctx):
def cb_stop(self, tctx):
# COMPONENT THREAD THAT WILL BE STARTED BY NCS.
# The application class sets up logging for us. It is accessible
# through 'self.log' and is a ncs.log.Log instance.
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'.
# 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.
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?
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.
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?
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.
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
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
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?
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(...)
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?
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 ?