ncs.dp Module
Callback module for connecting data providers to ConfD/NCS.
Functions
return_worker_socket
return_worker_socket(state, key)Return worker socket associated with a worker thread from Daemon/state.
Return worker socket to pool.
take_worker_socket
take_worker_socket(state, name, key=None)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
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.
Arguments:
daemon -- Daemon instance (dp.Daemon)
actionpoint -- actionpoint name (str)
log -- logging object (optional)
init_args -- additional arguments (optional)
Members:
action(...)
Static method:
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)
cb_init(...)
Method:
The cb_init callback must always be implemented.
This default implementation will associate a new worker socket with this callback.
init(...)
Method:
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.
rpc(...)
Static method:
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)
class Daemon
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.
Members:
daemon
A boolean value indicating whether this thread is a daemon thread.
This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when only daemon threads are left.
getName(...)
Method:
Return a string used for identification purposes only.
This method is deprecated, use the name attribute instead.
ident
Readonly property
Thread identifier of this thread or None if it has not been started.
This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.
isDaemon(...)
Method:
Return whether this thread is a daemon.
This method is deprecated, use the daemon attribute instead.
is_alive(...)
Method:
Return whether the thread is alive.
This method returns True just before the run() method starts until just after the run() method terminates. See also the module function enumerate().
join(...)
Method:
Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method is called terminates -- either normally or through an unhandled exception or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a floating-point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened -- if the thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
name
A string used for identification purposes only.
It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
native_id
Readonly property
Native integral thread ID of this thread, or None if it has not been started.
This is a non-negative integer. See the get_native_id() function. This represents the Thread ID as reported by the kernel.
register_trans_cb(...)
Method:
Register a transaction callback class.
It's not necessary to call this method. Only do that if a custom transaction callback will be used.
register_trans_validate_cb(...)
Method:
Register a transaction validation callback class.
It's not necessary to call this method. Only do that if a custom transaction callback will be used.
run(...)
Method:
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.
setDaemon(...)
Method:
Set whether this thread is a daemon.
This method is deprecated, use the .daemon property instead.
setName(...)
Method:
Set the name string for this thread.
This method is deprecated, use the name attribute instead.
start(...)
Method:
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
Base class for state managers used with Daemon
Members:
class TransValidateCallback
Default transaction validation callback implementation class.
When registering validation points in ConfD/NCS a transaction validation callback handler must be provided. This class is a generic implementation of such a handler. It implements the required callbacks 'cb_init' and 'cb_stop'.
Initialize a TransValidateCallback object.
The argument 'state' is the dict representation of a daemon.
Members:
cb_init(...)
Method:
The cb_init callback must always be implemented.
It is required to prepare for future validation callbacks. This default implementation allocates a worker thread and socket pair and associates it with the transaction.
cb_stop(...)
Method:
The cb_stop callback must always be implemented.
Clean up resources previously allocated in the cb_init callback. This default implementation returnes the worker thread and socket pair to the pool of workers.
class TransactionCallback
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.
Members:
cb_finish(...)
Method:
The cb_finish callback of TransactionCallback.
This implementation returns worker socket associated with a worker thread from Daemon/state.
cb_init(...)
Method:
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.
class ValidationError
Exception raised to indicate a failed validation
Members:
class ValidationPoint
Validation Point callback.
This class makes it easy to create and register validation point callbacks by subclassing it and implementing cb_validate with the @validate or @validate_with_trans decorator.
Members:
init(...)
Method:
Custom initialization.
When registering a validation point using ncs.application.Application this method will be called with the 'init_args' passed into the register_validation() function.
validate(...)
Static method:
Decorator for the cb_validate callback.
Using this decorator alters the signature of the cb_validate callback and passes in the validationpoint as the last argument.
In addition it logs unhandled exceptions, handles ValidationError exception setting the transaction error and returns _tm.CONFD_ERR.
Example of a decorated cb_validate:
Callback arguments:
tctx - transaction context (TransCtxRef)
kp -- path to the node being validated (HKeypathRef)
value -- new value of keypath (Value)
validationpoint - name of the validation point (str)
validate_with_trans(...)
Static method:
Decorator for the cb_validate callback.
Using this decorator alters the signature of the cb_validate callback and passes in root node attached to the transaction being validated and the validationpoint as the last argument.
In addition it logs unhandled exceptions, handles ValidationError exception setting the transaction error and returns _tm.CONFD_ERR.
Example of a decorated cb_validate:
Callback arguments:
tctx - transaction context (TransCtxRef)
root -- root node (maagic.Root)
kp -- path to the node being validated (HKeypathRef)
value -- new value of keypath (Value)
validationpoint - name of the validation point (str)
Predefined Values
Last updated
Was this helpful?

