public class Dp extends Object
NOTE: For NCS users the Dp class should not be used directly. Instead written callbacks must be packaged in a .jar file and referred to by the package-meta-data.xml file loaded by NCS.
The class library is also used to populate items in the YANG model which are not data or configuration items, such as statistics items.
The library consists of a number of API methods whose purpose is to install different callback methods at different points in the XML tree which is the representation of the device configuration. Read more about callpoints in tailf_yang_extensions(5).
There are different types of callbacks that can be registered to perform different tasks. Data callbacks are used to deliver data that is defined in the data model. If the data is statistics data, e.g. "config = false;" data, the callback objects only have to be able to respond to read requests, whereas if the data is configuration data, the callback objects must be able to also write data to persistent storage.
To register a data callback in ConfD, we can do:
// int port = Conf.PORT for ConfD or Conf.NCS_PORT for NCS Socket ctrl_socket= new Socket("127.0.0.1", port); final Dp dp = new Dp("server_daemon", ctrl_socket); dp.registerAnnotatedCallbacks(new SimpleTransCb()); dp.registerAnnotatedCallbacks(new SimpleDataCb()); dp.registerDone(); Thread dpTh = new Thread(new Runnable() { public void run() { try { while (true) dp.read(); } catch (Exception e) { e.printStackTrace(); return; } } }); dpTh.start();
To register a data callback in NCS , we must package the classes in the .jar file and place it in the package/my-package/private-jar directory and reference the annotated callbacks in the package/my-package/package-meta-data.xml:
<ncs-package xmlns="http://tail-f.com/ns/ncs-packages"> <name>My-package</name> <package-version>1.0</package-version> <description>Abstraction test</description> <ncs-min-version>2.0</ncs-min-version> <component> <name>MyCallbacks</name> <callback> <java-class-name> com.example.mypackage.SimpleTransCb </java-class-name> <java-class-name> com.example.mypackage.SysDataCb </java-class-name> </callback> </component> </ncs-package>
Where the two classes MyTransCb and MyDataCb need to be annotated to indicate what capabilities they have. The following code show the annotation used ( Applies to both ConfD/NCS):
public class MyTransCb { @TransCallback(callType=TransCBType.INIT) public void init(DpTrans trans) throws DpCallbackException { trace("init(): userinfo= " + trans.getUserInfo()); } @TransCallback(callType=TransCBType.FINISH) public void finish(DpTrans trans) throws DpCallbackException { trace("finish()"); } } // And so on ...
The MyTransCb methods init , finish get called at the start and finish of each transactions. There exists a number of different possible entry points within the transaction. See @see TransCBType. And the data callbacks that need to be able to deliver the actual data. Given a data model that looks looks like:
container servers { config false; tailf:callpoint simplecp { } list server { key name; max-elements 64; leaf name { type string; } leaf ip { type inet:ip-address; } leaf port { type inet:port-number; } leaf macaddr { type tailf:hex-list; } leaf snmpref { type yang:object-identifier; mandatory true; } leaf prefixmask { type tailf:octet-list; mandatory true; } } }
The callback class to deliver the data could look like:
public class SimpleDataCb { @DataCallback(callPoint="simplecp", callType=DataCBType.ITERATOR) public Iterator<Object> iterator(DpTrans trans, ConfObject[] kp) throws DpCallbackException { return Foobar.iterator(); } @DataCallback(callPoint="simplecp", callType=DataCBType.GET_NEXT) public ConfKey getKey(DpTrans trans, ConfObject[] kp, Object obj) throws DpCallbackException { Server s = (Server) obj; return new ConfKey( new ConfObject[] { new ConfBuf(s.name) }); } @DataCallback(callPoint="simplecp", callType=DataCBType.GET_ELEM) public ConfValue getElem(DpTrans trans, ConfObject[] kp) throws DpCallbackException { return Foobar.get(kp); } @DataCallback(callPoint="simplecp", callType=DataCBType.GET_OBJECT) public ConfValue[] getObject(DpTrans trans, ConfObject[] kp) throws DpCallbackException { String name = ((ConfKey) kp[0]).elementAt(0).toString(); Server s = Foobar.findServer( name ); if (s == null) return null; return getObject(trans, kp, s); } @DataCallback(callPoint="simplecp", callType=DataCBType.GET_NEXT_OBJECT) public ConfValue[] getObject(DpTrans trans, ConfObject[] kp, Object obj) throws DpCallbackException { Server s = (Server) obj; return new ConfValue[] { new ConfBuf(s.name), new ConfIPv4( s.addr), new ConfUInt16( s.port), new ConfHexList( s.macaddr ), } }
Dp
uses ThreadPoolExecutor
as managing its
internal worker threads. Dp
could be configured to prestart
worker threads so the overhead of creating and starting threads is minimized.
Dp
delivers a task (DpTrans
,DpValidateTrans
,
DpActionTrans
to worker threads when requests comes
from controller socket.
'
The worker threads could either wait for task to arrive to them or
they could be started when needed. Dp
provides constructor for
setting various parameters to tune the behavior of threads pool
Constructor and Description |
---|
Dp(String name,
Socket socket)
This constructor will initialize the Dp class library and connect to
ConfD/NCS on the provided control socket.
|
Dp(String name,
Socket ctrlSocket,
boolean isNcs)
This constructor will initialize the Dp class library and connect to
ConfD/NCS on the provided control socket.
|
Dp(String name,
Socket ctrlSocket,
boolean isNcs,
int minThreadPoolSize,
int maxThreadPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> queue,
boolean prestartCoreThreads)
This constructor will initialize the Dp class library and connect to
ConfD/NCS on the provided control socket.
|
Modifier and Type | Method and Description |
---|---|
void |
closeAllWorkerSockets() |
DpNotifStream |
createNotifStream(String name)
Creates (and registers) a notifications stream with ConfD/NCS.
|
DpNotifStream |
createNotifStream(String name,
DpNotifReplayCallback replayCb)
Creates (and registers) a notifications stream with ConfD/NCS.
|
DpNotifStream |
createNotifStream(String name,
DpNotifReplayCallback replayCb,
Socket socket)
Creates (and registers) a notifications stream with ConfD/NCS.
|
DpSnmpNotifier |
createSnmpNotifier(String name,
String context_name)
Creates (and registers) a SNMP Notifer @see
DpSnmpNotifier . |
DpSnmpNotifier |
createSnmpNotifier(String notify_name,
String context_name,
Object inform_cb)
Creates (and registers) a SNMP Notifier @see
DpSnmpNotifier . |
DpSnmpNotifier |
createSnmpNotifier(String notify_name,
String context_name,
Object inform_cb,
Socket socket)
Creates (and registers) a SNMP Notifier see
DpSnmpNotifier . |
Socket |
getCtrlSocket()
The control socket which is connected to ConfD/NCS.
|
int |
getDaemonId()
The daemon identifier (assigned by ConfD/NCS).
|
ErrorMessageFormatter |
getErrorMessageFormatter()
Return the errorMessageFormatter for this Dp.
|
ErrorVerbosity |
getErrorVerbosity()
Get the local verbosity level for reported errors
If this verbosity is null the the default level governs the error
verbosity of this Dp
|
DpExceptionReporter |
getExceptionReporter() |
ArrayList<ConfNamespace> |
getNsList()
Get a list of the installed namespaces.
|
Maapi |
getServicePointMaapi() |
DpUserInfo |
getUserInfo(int usid)
Retrieves the user information.
|
ThreadPoolExecutor |
getWorkerPool()
Get current WorkerThreadPool
|
void |
read()
Receives data on the control socket which is connected to ConfD/NCS.
|
void |
registerAnnotatedCallbacks(Object obj)
All Data, Trans, Action, Validate, TransValidate and DB callbacks
are registered using this method.
|
void |
registerAnnotatedCallbacks(String mountId,
Object obj) |
void |
registerAnnotatedMountedCbs(DpMountIdInterface MountIdMethod,
Object obj) |
void |
registerAnnotatedRangeActionCallbacks(Object obj,
ConfValue[] lower,
ConfValue[] higher,
ConfPath path) |
void |
registerAnnotatedRangeDataCallbacks(Object obj,
ConfValue[] lower,
ConfValue[] higher,
ConfPath path)
DataCallbacks can be registered for a range of values using this method
|
void |
registerDone()
When we have registered all the callbacks for a daemon
we must call this function to synchronize with ConfD/NCS.
|
void |
reRegisterAnnotatedCallbacks(Object obj)
reRegisters an existing callback.
|
void |
reRegisterAnnotatedCallbacks(String mountId,
Object obj) |
void |
reRegisterAnnotatedMountedCbs(DpMountIdInterface MountIdMethod,
Object obj) |
void |
reRegisterAnnotatedRangeActionCallbacks(Object obj)
reRegisters an existing callback.
|
void |
reRegisterAnnotatedRangeDataCallbacks(Object obj)
reRegisters an existing callback.
|
void |
setErrorVerbosity(ErrorVerbosity verbosity)
set the local verbosity level for reported errors
If this verbosity is set to null the the default level governs the error
verbosity of this Dp
|
void |
setExceptionReporter(DpExceptionReporter exReporter) |
void |
setNumFreeWorkerSockets(int numSockets)
This method is used to control the number of workersockets that
should be keept open for reuse.
|
void |
shutDownThreadPool()
Initiates an orderly shutdown in which previously
transactions are executed, but no new transactions
will be accepted.
|
int |
shutDownThreadPoolNow()
Attempts to stop all actively executing transactions,
halts the processing of waiting transactions, and returns number
of the tasks that were awaiting execution.
|
public Dp(String name, Socket socket) throws IOException, ConfException
// int port = Conf.PORT for ConfD or Conf.NCS_PORT for NCS
Dp dp = new Dp("xyz", new Socket("localhost", port));
If encrypted communication towards ConfD/NCS is desired,
an environment variable "CONFD_IPC_ACCESS_FILE" or "NCS_IPC_ACCESS_FILE"
need to be set.
This variable is expected to point to a file containing a secret salt.
example:
export CONFD_IPC_ACCESS_FILE=./secret_file.txt
An alternative to the environment variable is to set an java system
property with the same name pointing to the file.
name
- A name that uniquely identifies the daemonsocket
- A control socket connected to ConfD/NCS.DpException
- Failed to initialize connection.DpCallbackException
- Callback method failed.IOException
- Failed to read from control socket.ConfException
- Failed to decode or other internal failure.public Dp(String name, Socket ctrlSocket, boolean isNcs) throws IOException, ConfException
If encrypted communication towards ConfD/NCS is desired, an environment variable "CONFD_IPC_ACCESS_FILE" or "NCS_IPC_ACCESS_FILE" need to be set. This variable is expected to point to a file containing a secret salt. example: export CONFD_IPC_ACCESS_FILE=./secret_file.txt An alternative to the environment variable is to set an java system property with the same name pointing to the file.
The thread pool behavior is that it prestarts 5 worker threads and poolsize could max grow to 256 working threads. Keep alive time is set to 60 sec.
The keepAlive time is the idle time of a thread before it is terminated. This value is only in effect when current thread is greater than corePoolSize. The default Queue is ArrayBlockingQueue with a queue size of 10. When more than 10 elements is in the queue then additional threads will be created.
name
- A name that uniquely identifies the daemonctrlSocket
- The control socket connected to ConfD/NCS.isNcs
- set to true if the data provider is managed by NcsDpMuxDpException
- Failed to initialize connection to ConfD/NCS.DpCallbackException
- Callback method failed.IOException
- Failed to read from control socket.ConfException
- Failed to decode or other internal failure.public Dp(String name, Socket ctrlSocket, boolean isNcs, int minThreadPoolSize, int maxThreadPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> queue, boolean prestartCoreThreads) throws IOException, ConfException
If encrypted communication towards ConfD/NCS is desired, an environment variable "CONFD_IPC_ACCESS_FILE" or "NCS_IPC_ACCESS_FILE" need to be set. This variable is expected to point to a file containing a secret salt. example: export CONFD_IPC_ACCESS_FILE=./secret_file.txt An alternative to the environment variable is to set an java system property with the same name pointing to the file. Various parameters for tuning the worker thread pool could be specified here. The thread pool behavior is that it prestarts minThreadPoolSize worker threads if prestartCoreThreads is set to true. The poolsize could max grow to maxThreadPoolSize threads. Keep alive time is set to keepAliveTime units time. The keepAlive time is the idle time of a thread before it is terminated. This value is only in effect when current thread is greater than corePoolSize. The queue could be specified as bounded or not bounded which will interact with the poolsize.
name
- A name that uniquely identifies the daemonctrlSocket
- The control socket connected to ConfD/NCS.isNcs
- set to true if the data provider is managed by NcsDpMuxminThreadPoolSize
- Minimum worker thread in the worker poolmaxThreadPoolSize
- Maximum worker thread in the worker pool. This parameter
works only when queue is boundedkeepAliveTime
- idle time for a thread before it is terminatedunit
- keepAlive units timequeue
- Queue that holds tasks. The use of this queue
interacts with pool sizing.DpException
- Failed to initialize connection to ConfD/NCS.DpCallbackException
- Callback method failed.IOException
- Failed to read from control socket.ConfException
- Failed to decode or other internal failure.public int getDaemonId()
public Socket getCtrlSocket()
public void setExceptionReporter(DpExceptionReporter exReporter)
public DpExceptionReporter getExceptionReporter()
public void registerDone() throws DpException
DpException
- Failed with registerDone()public void registerAnnotatedMountedCbs(DpMountIdInterface MountIdMethod, Object obj) throws DpException
DpException
public void reRegisterAnnotatedMountedCbs(DpMountIdInterface MountIdMethod, Object obj) throws DpException
DpException
public void registerAnnotatedCallbacks(Object obj) throws DpException
Example of a DataCallback:
final class cbDataDemo { @DataCallback(callPoint="abc", callType=DataCBType.ITERATOR) public Iterator<Object> abc(DpTrans trans, ConfObject[] kp) { // Change this to the real implementation return null; } @DataCallback(callPoint="abc", callType=DataCBType.GET_NEXT) public ConfKey getKey(DpTrans trans, ConfObject[] kp, Object obj) { // Change this to the real implementation return null; } // And so on ... }
The respective annotations are
The callType defines the callback method and each annotation has an Enum of legal values
obj
- object to register as callbackDpException
public void registerAnnotatedCallbacks(String mountId, Object obj) throws DpException
DpException
public void reRegisterAnnotatedCallbacks(Object obj) throws DpException
obj
- DpException
public void reRegisterAnnotatedCallbacks(String mountId, Object obj) throws DpException
DpException
public void registerAnnotatedRangeActionCallbacks(Object obj, ConfValue[] lower, ConfValue[] higher, ConfPath path) throws DpException
DpException
public void reRegisterAnnotatedRangeActionCallbacks(Object obj) throws DpException
obj
- DpException
public void registerAnnotatedRangeDataCallbacks(Object obj, ConfValue[] lower, ConfValue[] higher, ConfPath path) throws DpException
obj
- object to register as callbacklower
- Array of lower bound key valueshigher
- Array of higher bound key valuespath
- A pathDpException
- Failed to register range data callback.public void reRegisterAnnotatedRangeDataCallbacks(Object obj) throws DpException
obj
- DpException
public void read() throws IOException, ConfException
ConfException
- Failed to decode.SocketTimeoutException
- The control socket timed out.IOException
- Failed to read from control socket.public DpUserInfo getUserInfo(int usid)
usid
- User identifierpublic ArrayList<ConfNamespace> getNsList()
public ThreadPoolExecutor getWorkerPool()
public void setNumFreeWorkerSockets(int numSockets)
numSockets
- number of sockets.public DpNotifStream createNotifStream(String name) throws ConfException, IOException
name
- A name that uniquely identifies the notification streamDpCallbackException
- Callback method failed.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpNotifStream
public DpNotifStream createNotifStream(String name, DpNotifReplayCallback replayCb) throws ConfException, IOException
name
- A name that uniquely identifies the notification streamreplayCb
- A callback that implements 'replay' functionality.
(optional)DpCallbackException
- Callback method failed.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpNotifStream
public DpNotifStream createNotifStream(String name, DpNotifReplayCallback replayCb, Socket socket) throws ConfException, IOException
name
- A name that uniquely identifies the notification streamreplayCb
- A callback that implements 'replay' functionality.
(optional)socket
- A worker socket to send notification on (optional)DpCallbackException
- Callback method failed.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpNotifStream
public DpSnmpNotifier createSnmpNotifier(String name, String context_name) throws ConfException, IOException
DpSnmpNotifier
.name
- a name uniquely identifying the SNMP notifier.context_name
- the SNMP context.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpSnmpNotifier
public DpSnmpNotifier createSnmpNotifier(String notify_name, String context_name, Object inform_cb) throws ConfException, IOException
DpSnmpNotifier
.notify_name
- a name uniquely identifying the SNMP notifier.context_name
- the SNMP context.inform_cb
- the callback to be called for Inform Responses.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpSnmpNotifier
public DpSnmpNotifier createSnmpNotifier(String notify_name, String context_name, Object inform_cb, Socket socket) throws ConfException, IOException
DpSnmpNotifier
.notify_name
- a name uniquely identifying the SNMP notifier.context_name
- the SNMP context.inform_cb
- the callback to be called for Inform Responses.socket
- socket to be used by DpSnmpNotifier
as
worker socket.IOException
- Failed to read from notification socket.ConfException
- Failed to decode or other internal failure.DpSnmpNotifier
public void closeAllWorkerSockets()
public void shutDownThreadPool()
public int shutDownThreadPoolNow()
public ErrorVerbosity getErrorVerbosity()
public void setErrorVerbosity(ErrorVerbosity verbosity)
verbosity
- public ErrorMessageFormatter getErrorMessageFormatter()
public Maapi getServicePointMaapi() throws IOException, ConfException
IOException
ConfException