cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2550
Views
17
Helpful
12
Replies

Validation callback in python

frjansso
Cisco Employee
Cisco Employee

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')

12 Replies 12

rogaglia
Cisco Employee
Cisco Employee

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

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?

Hi Jan,

Personally, that was my understanding and why I was asking how tailf:validate works on lists.

Roque

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.

frjansso
Cisco Employee
Cisco Employee

Makes sense now, I never tried to att the validation point to a single leaf.

mareis
Cisco Employee
Cisco Employee

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

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(...)

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

mohamkh7
Cisco Employee
Cisco Employee

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

 

You mean to tie the validation callback to a service class? I'm actually not sure... but I guess the question is why? :)

You mean to tie the validation callback to a service class? I'm actually not sure... but I guess my question is why? :)

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: