Module ncs.dp
Callback module for connecting data providers to ConfD/NCS.
Functions
def return_worker_socket(state, key)
-
Return worker socket associated with a worker thread from Daemon/state.
Return worker socket to pool.
def take_worker_socket(state, name, key)
-
Take worker socket associated with a worker thread from Daemon/state.
Take worker socket from pool, must be returned with dp.return_worker_socket after use.
Classes
class Action (daemon, actionpoint, log=None, init_args=None)
-
Action callback.
This class makes it easy to create and register action callbacks by sub-classing it and implementing cb_action in the derived class.
Initialize this object.
The 'daemon' argument should be a Daemon instance. 'actionpoint' is the name of the tailf:actionpoint to manage. 'log' can be any log object, and if not set the Daemon logger will be used. 'init_args' may be any object that will be passed into init() when this object is constructed. Lastly, the low-level function dp.register_action_cbs() will be called.
When using this class together with ncs.application.Application there is no need to manually initialize this object as it is done by the Application.register_action() method.
Static methods
def action(fn)
-
Decorator for the cb_action callback.
Only use this decorator for actions of tailf:action type.
Using this decorator alters the signature of the cb_action callback and passes in maagic.Node objects for input and output action data.
Example of a decorated cb_action:
Callback arguments:
- uinfo – a UserInfo object
- name – the tailf:action name (string)
- kp – the keypath of the action (HKeypathRef)
- input – input node (maagic.Node)
- output – output node (maagic.Node)
- trans – read only transaction, same as action transaction if executed with an action context. (maapi.Transaction)
def rpc(fn)
-
Decorator for the cb_action callback.
Only use this decorator for rpc:s.
Using this decorator alters the signature of the cb_action callback and passes in maagic.Node objects for input and output action data.
Example of a decorated cb_action:
Callback arguments:
- uinfo – a UserInfo object
- name – the rpc name (string)
- input – input node (maagic.Node)
- output – output node (maagic.Node)
Methods
def cb_init(self, uinfo)
-
The cb_init callback must always be implemented.
This default implementation will associate a new worker socket with this callback.
def init(self, init_args)
-
Custom initialization.
When registering an action using ncs.application.Application this method will be called with the 'init_args' passed into the register_action() function.
def start(self)
-
Custom actionpoint start triggered when Python VM starts up.
def stop(self)
-
Custom actionpoint stop triggered when Python VM shuts down.
class Daemon (name, log=None, ip='127.0.0.1', port=4569, path=None, state_mgr=None)
-
Manage a data provider connection towards ConfD/NCS.
Initialize a Daemon object.
The 'name' argument should be unique. It will show up in the CLI and in error messages. All other arguments are optional. Argument 'log' can be any log object, and if not set the standard logging mechanism will be used. Set 'ip' and 'port' to where your Confd/NCS server is. 'path' is a filename to a unix domain socket to be used in place of 'ip' and 'port'. If 'path' is provided, 'ip' and 'port' arguments are ignored.
Daemon supports automatic restarting in case of error if a state manager is provided using the state_mgr parameter.
Ancestors
Class variables
var INIT_RETRY_INTERVAL_S
var State
-
State access to dp.Daemon state, primarily used in callbacks.
Instance variables
var wsock
Methods
def ctx(self)
-
Return the daemon context.
def finish(self)
-
Stop the daemon thread.
def ip(self)
-
Return the ip address.
def load_schemas(self)
-
Load schema information into the process memory.
def path(self)
-
Return the unix domain socket path.
def port(self)
-
Return the port.
def register_trans_cb(self, trans_cb_cls=ncs.dp.TransactionCallback)
-
Register a transaction callback class.
It's not necessary to call this method. Only do that if a custom transaction callback will be used.
def run(self)
-
Daemon thread processing loop.
Don't call this method explicitly. It handles reading of control and worker sockets and notifying ConfD/NCS that it should continue processing by calling the low-level function dp.fd_ready(). If the connection towards ConfD/NCS is broken or if finish() is explicitly called, this function (and the thread) will end.
def start(self)
-
Start daemon work thread.
After registering any callbacks (action, services and such), call this function to start processing. The low-level function dp.register_done() will be called before the thread is started.
class StateManager (log)
-
Base class for state managers used with Daemon
Subclasses
- ncs.application._StateManager
Methods
def setup(self, state, previous_state)
-
Not Implemented.
def teardown(self, state, finished)
-
Not Implemented.
class TransactionCallback (state)
-
Default transaction callback implementation class.
When connecting data providers to ConfD/NCS a transaction callback handler must be provided. This class is a generic implementation of such a handler. It implements the only required callback 'cb_init'.
Initialize a TransactionCallback object.
The argument 'wsock' is the connected worker socket and 'log' is a log object.
Methods
def cb_finish(self, tctx)
-
The cb_finish callback of TransactionCallback.
This implementation returns worker socket associated with a worker thread from Daemon/state.
def cb_init(self, tctx)
-
The cb_init callback must always be implemented.
It is required to prepare for future read/write operations towards the data source. This default implementation associates a worker socket with a transaction.