public class CdbSession extends Object
CdbSession
represents a session against
the Cdb database.
It contains methods for reading and writing to/from the CDB.
A Cdb session is a short-lived sessions where it is possible to read configuration data , and to read and write operational data.
When starting a Cdb Session against running datastore the (with the flag CdbDBType.CDB_RUNNING ) entire configuration part of CDB is locked for writing while any CDB read session is active.
If it is considered necessary to have more
detailed control over some aspects of the
CDB session use the method Cdb.startSession(CdbDBType,EnumSet)
where the EnumSet
contains a set of lock flags for this method.
The flags affect sessions for the different database types as follows:
CDB_RUNNING -
LOCK_SESSION
obtains a read lock for the complete
session, i.e. using this flag alone is equivalent to calling
Cdb.startSession()
or Cdb.startSession(CdbDBType)
.
LOCK_REQUEST
obtains a read lock only for the
duration of each read request. This means that values of
elements read in different requests may be inconsistent with each other,
and the consequences of this must be carefully considered.
In particular, the use of CdbSession.getNumberOfInstances(ConfPath)
and the [n]
"integer index" notation in keypaths is inherently unsafe in this mode.
Note: The implementation will not
actually obtain a lock for a single-value request, since that is an
atomic operation anyway.
The LOCK_PARTIAL
flag is not allowed.
CDB_STARTUP - Same as CDB_RUNNING.
CDB_PRE_COMMIT_RUNNING
- This database type does not have any locks, which means that it
is an error to call startSession
with any lock flags.
CDB_OPERATIONAL -
LOCK_REQUEST
obtains a "subscription lock" for the
duration of each write request. This can be described as an
"advisory exclusive" lock, i.e. only one client at a time can hold
the lock (unless LOCK_PARTIAL
is used),
but the lock does not affect clients that do not attempt to obtain it.
It also does not affect the reading of operational data.
The purpose of this lock is to indicate that the client
wants the write operation to generate subscription notifications.
The lock remains in effect until any/all subscription notifications
generated as a result of the write has been delivered.
If the LOCK_PARTIAL
flag is used together with
LOCK_REQUEST
,
the "subscription lock" only applies to the smallest data subtree that
includes all the data in the write request.
This means that multiple writes that generates subscription
notifications, and delivery of the
corresponding notifications, can proceed in parallel as long as they
affect disjunct parts of the data tree.
The LOCK_SESSION
flag is not allowed.
In all cases of using LOCK_SESSION
or
LOCK_REQUEST
described above, adding the LOCK_WAIT
flag means that
instead of failing with ErrorCode.ERR_LOCKED
if the lock
can not be obtained immediately, requests will wait for the lock to
become available. When used with
LOCK_SESSION
it pertains to startSession
itself,
with LOCK_REQUEST
it pertains to the individual requests.
An example usage of stating/ending a CDB Session against the running datastore:
// create new socket and Cdb instance. // int port = Conf.PORT for ConfD or Conf.NCS_PORT for NCS Socket sock = new Socket(host, port); Cdb cdb = new Cdb("test",sock); // start session towards running data ( with the lock LOCK_SESSION ) CdbSession sess = cdb.startSession(CdbDBType.CDB_RUNNING); // get a leaf using a specified path ConfPath path = new ConfPath("/mtest/servers/server{%s}/port", new Object[] { new String("www") }); ConfValue val = sess.getElem(path); // now do something with the read value .... // end session when we are finished sess.endSession(); sock.close();
Constructor and Description |
---|
CdbSession(Cdb cdb)
Creates a new CDB session
instance against the running database.
|
CdbSession(Cdb cdb,
CdbDBType dbtype)
Starts a new session on an already connected Cdb instance.
|
CdbSession(Cdb cdb,
CdbDBType dbtype,
EnumSet<CdbLockType> lockflags)
Starts a new session on an already established Cdb with
explicitly given lockflags.
|
Modifier and Type | Method and Description |
---|---|
void |
cd(ConfPath path)
Changes the working directory.
|
void |
cd(String fmt,
Object... arguments)
Change working directory to container specified by path string
|
void |
create(ConfPath path)
Create a new optional element or list entry.
|
void |
create(String fmt,
Object... arguments)
similar to
CdbSession.create(ConfPath) but specifies element using path
string |
void |
delete(ConfPath path)
Delete an optional element or list entry and all its child elements.
|
void |
delete(String fmt,
Object... arguments)
similar to
CdbSession.delete(ConfPath) but specifies element using path
string |
void |
endSession()
Ends the data session
|
boolean |
exists(ConfPath path)
Containers and leafs in a YANG model may be optional.
|
boolean |
exists(String fmt,
Object... arguments)
Checks whether an element exists.
|
ConfObject |
getCase(String choice,
ConfPath path)
Retrieve the currently selected case.
|
ConfObject |
getCase(String choice,
String fmt,
Object... arguments)
Retrieve the currently selected case.
|
Cdb |
getCdb() |
String |
getcwd()
Returns the current position as previously set by
CdbSession.cd(ConfPath) ,
CdbSession.pushd(ConfPath) , or CdbSession.popd() as a string. |
ConfObject[] |
getcwdPath()
Returns the current position as previously set by
CdbSession.cd(ConfPath) ,
CdbSession.pushd(ConfPath) , or CdbSession.popd() as a
ConfObject array. |
CdbDBType |
getDbType()
retrieve the dbType for this session.
|
ConfValue |
getElem(ConfPath path)
This reads a a value from the path.
|
ConfValue |
getElem(String fmt,
Object... arguments)
similar to
CdbSession.getElem(ConfPath) but specifies element using path
string |
int |
getNumberOfInstances(ConfPath path)
Returns the number of elements of a container type.
|
int |
getNumberOfInstances(String fmt,
Object... arguments)
similar to
CdbSession.getNumberOfInstances(ConfPath) but specifies element
using path string |
ConfObject[] |
getObject(int numOfObjects,
ConfPath path)
Same functionality as getObject(numOfObjects, fmt, arguments) but takes a
already constructed ConfPath object as argument instead of fmt,
arguments.
|
ConfObject[] |
getObject(int numOfObjects,
String fmt,
Object... arguments)
In some cases it can be motivated to read multiple values in one request
- this will be more efficient since it only incurs a single round trip to
the server, but usage is a bit more complex.
|
List<ConfObject[]> |
getObjects(int numOfObjects,
int instance,
int numOfInstances,
ConfPath path)
Same functionality as getObjects(numOfObjects, instance, fmt, arguments)
but takes a already constructed ConfPath object as argument instead of
fmt, arguments.
|
List<ConfObject[]> |
getObjects(int numOfObjects,
int instance,
int numOfInstances,
String fmt,
Object... arguments)
Similar to cdb.getObject(), but reads multiple instances of a dynamic
container based on the "instance integer" otherwise given within square
brackets in the path - here the path must specify the dynamic container
without the instance integer.
|
ConfXMLParam[] |
getValues(ConfXMLParam[] params,
ConfPath path)
Same functionality as getValues(params, fmt, arguments) but takes a
already constructed ConfPath object as argument instead of fmt,
arguments.
|
ConfXMLParam[] |
getValues(ConfXMLParam[] params,
String fmt,
Object... arguments)
Read an arbitrary set of sub-elements of a container element.
|
int |
index(ConfPath path)
Returns the position of a key
|
int |
index(String fmt,
Object... arguments)
similar to
CdbSession.index(ConfPath) but specifies element using path
string |
boolean |
isDefault(ConfPath path)
This method returns true for a leaf which has a default value defined
in the data model when no value has been set, i.e.
|
boolean |
isDefault(String fmt,
Object... arguments)
similar to
CdbSession.isDefault(ConfPath) but specifies element using path
string |
int |
nextIndex(ConfPath path)
Returns the position of the next key
|
int |
nextIndex(String fmt,
Object... arguments)
similar to
CdbSession.nextIndex(ConfPath) but specifies element using path
string |
int |
numInstances(ConfPath path)
Deprecated.
in favor to getNumberOfInstances, which follows java naming
conventions.
Returns the number of elements of a container type.
|
int |
numInstances(String fmt,
Object... arguments)
Deprecated.
in favor to getNumberOfInstances, which follows java naming
conventions.
similar to
CdbSession.numInstances(ConfPath) but specifies element using
path string |
void |
popd()
Pops the top element from the directory stack and changes directory to
previous directory.
|
void |
pushd(ConfPath path)
Similar to
CdbSession.cd(ConfPath) but pushes the previous current
directory on a stack. |
void |
pushd(String fmt,
Object... arguments)
similar to
CdbSession.pushd(ConfPath) but specifies position using path
string |
void |
setCase(String choice,
String scase,
ConfPath path)
Same functionality as setCase(choice, scase, fmt, arguments) but takes a
already constructed ConfPath object as argument instead of fmt,
arguments.
|
void |
setCase(String choice,
String scase,
String fmt,
Object... arguments)
When we use the YANG choice construct in the data model, this function
can be used to select the current case.
|
void |
setElem(ConfValue value,
ConfPath path)
Sets an element in operational data.
|
void |
setElem(ConfValue value,
String fmt,
Object... arguments)
similar to
CdbSession.setElem(ConfValue, ConfPath) but specifies element
using path string |
void |
setNamespace(ConfNamespace ns)
Before we start to read data from CDB we need to set the namespace.
|
void |
setObject(ConfValue[] values,
ConfPath path)
Set all elements corresponding to the complete contents of a container
element, except for list entry sub-elements.
|
void |
setObject(ConfValue[] values,
String fmt,
Object... arguments)
similar to
CdbSession.setObject(ConfValue[], ConfPath) but specifies
element using path string |
void |
setValues(ConfXMLParam[] params,
ConfPath path)
Set arbitrary sub-elements of a container element.
|
void |
setValues(ConfXMLParam[] params,
String fmt,
Object... arguments)
similar to
CdbSession.setValues(ConfXMLParam[], ConfPath) but specifies
element using path string |
void |
setValues(List<ConfXMLParam> params,
ConfPath path) |
String |
toString() |
public CdbSession(Cdb cdb) throws IOException, ConfException
CDB_RUNNING
)cdb
- The CDB instance the session belongs toIOException
ConfException
public CdbSession(Cdb cdb, CdbDBType dbtype) throws ConfException, IOException
cdb
- The CDB instance with a socket connected to ConfD/NCS.dbtype
- see CdbDBType
CdbException
- Failed to start sessionIOException
- Failed to read/write socketConfException
public CdbSession(Cdb cdb, CdbDBType dbtype, EnumSet<CdbLockType> lockflags) throws ConfException, IOException
cdb
- dbtype
- see CdbDBType
lockflags
- CdbLockType
IOException
ConfException
public Cdb getCdb()
public CdbDBType getDbType()
public void setNamespace(ConfNamespace ns) throws ConfException, IOException
ns
- Name space objectCdbException
- Failed to set namespaceIOException
- Failed to read/write cdb socketConfException
public void endSession() throws ConfException, IOException
CdbException
- Failed to end sessionIOException
- Failed to read/write cdb socketConfException
public boolean exists(ConfPath path) throws ConfException, IOException
path
- ConfPath objectCdbException
- Failed to check existsIOException
- Failed to read/write cdb socketConfException
public boolean exists(String fmt, Object... arguments) throws ConfException, IOException
fmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void cd(ConfPath path) throws ConfException, IOException
path
- ConfPath objectCdbException
- Failed to 'cd'IOException
- Failed to read/write cdb socketConfException
public void cd(String fmt, Object... arguments) throws ConfException, IOException
fmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void pushd(ConfPath path) throws ConfException, IOException
CdbSession.cd(ConfPath)
but pushes the previous current
directory on a stack.path
- ConfPath objectCdbException
- Failed to 'pushd'IOException
- Failed to read/write cdb socketConfException
public void pushd(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.pushd(ConfPath)
but specifies position using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void popd() throws ConfException, IOException
CdbException
- Failed to 'popd'IOException
- Failed to read/write cdb socketConfException
public String getcwd() throws ConfException, IOException
CdbSession.cd(ConfPath)
,
CdbSession.pushd(ConfPath)
, or CdbSession.popd()
as a string.CdbException
- Failed to 'getcwd'IOException
- Failed to read/write cdb socketConfException
public ConfObject[] getcwdPath() throws ConfException, IOException
CdbSession.cd(ConfPath)
,
CdbSession.pushd(ConfPath)
, or CdbSession.popd()
as a
ConfObject
array.CdbException
- Failed to 'getcwd'IOException
- Failed to read/write cdb socketConfException
public int getNumberOfInstances(ConfPath path) throws ConfException, IOException
path
- The pathCdbException
- Failed to get number of instancesIOException
- Failed to read/write cdb socketConfException
public int getNumberOfInstances(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.getNumberOfInstances(ConfPath)
but specifies element
using path stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
@Deprecated public int numInstances(ConfPath path) throws ConfException, IOException
path
- The pathCdbException
- Failed to get number of instancesIOException
- Failed to read/write cdb socketConfException
@Deprecated public int numInstances(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.numInstances(ConfPath)
but specifies element using
path stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public int nextIndex(ConfPath path) throws ConfException, IOException
path
- The pathCdbException
- Failed to get next positionIOException
- Failed to read/write cdb socketConfException
public int nextIndex(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.nextIndex(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public int index(ConfPath path) throws ConfException, IOException
path
- The pathCdbException
- Failed to get positionIOException
- Failed to read/write cdb socketConfException
public int index(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.index(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public boolean isDefault(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.isDefault(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public boolean isDefault(ConfPath path) throws ConfException, IOException
path
- as a ConfPathConfException
IOException
public ConfValue getElem(ConfPath path) throws ConfException, IOException
path
- The pathCdbException
- Failed to get elementIOException
- Failed to read/write cdb socketConfException
public ConfValue getElem(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.getElem(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void setElem(ConfValue value, ConfPath path) throws ConfException, IOException
Sets an element in operational data.
It is possible for an application to store operational data (i.e. status and statistical information) in CDB.
The operational database avoids the use of transactions and locks in order to provide light-weight access methods, however when the multi-value API method are used, all updates requested by a given method call are carried out atomically.
To establish a session for operational data, the application
must use Cdb.startSession(CdbDBType,EnumSet)
with
CdbDBType
set to CDB_OPERATIONAL
.
After this, all the read and access functions are available for use with operational data, and additionally the write methods.
Configuration data can not be accessed in a session for operational data, nor vice versa, The write functions can never be used in a session for configuration data.
value
- ConfValue objectpath
- ConfPath objectCdbException
- Failed to set elementIOException
- Failed to read/write to underlying socketConfException
public void setElem(ConfValue value, String fmt, Object... arguments) throws ConfException, IOException
CdbSession.setElem(ConfValue, ConfPath)
but specifies element
using path stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void create(ConfPath path) throws ConfException, IOException
CdbSession.setObject(ConfValue[], ConfPath)
or
CdbSession.setValues(ConfXMLParam[], ConfPath)
may be preferred in this
case.path
- ConfPath objectCdbException
- Failed to createIOException
- Failed to write to cdb socketConfException
public void create(String fmt, Object... arguments) throws ConfException, IOException
CdbSession.create(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtConfException
IOException
public void delete(ConfPath path) throws ConfException, IOException
path
- ConfPath objectCdbException
- Failed to delete elementIOException
- Failed to read/write cdb socketConfException
public void delete(String fmt, Object... arguments) throws IOException, ConfException
CdbSession.delete(ConfPath)
but specifies element using path
stringfmt
- path stringarguments
- optional parameters for substitution in fmtIOException
ConfException
public void setObject(ConfValue[] values, ConfPath path) throws IOException, ConfException
values
- Array of ConfValue objectspath
- ConfPath objectCdbException
- Failed to set objectIOException
- Failed to read/write cdb socketConfException
public ConfObject[] getObject(int numOfObjects, String fmt, Object... arguments) throws IOException, ConfException
String path = "/mtest/servers/server{www}/interface{%s}"; ConfObject[] objArr = cdbsess.getObject(4, path, "eth0"); ConfBuf name = (ConfBuf) objArr[0]; ConfInt64 mtu = (ConfInt64) objArr[1];
numOfObjects
- number of objects to retrievefmt
- arguments
- IOException
ConfException
public ConfObject[] getObject(int numOfObjects, ConfPath path) throws IOException, ConfException
numOfObjects
- number of objects to retrievepath
- IOException
ConfException
public List<ConfObject[]> getObjects(int numOfObjects, int instance, int numOfInstances, String fmt, Object... arguments) throws IOException, ConfException
String path = "/mtest/servers/server"; int n = cdbsess.numInstances(path); List<ConfObject[]> objArrList = cdbsess.getObjects(4, 0, n, path); ConfBuf[] name = new ConfBuf[n]; ConfIPv4[] ip = new ConfIPv4[n]; ConfUInt16[] port = new ConfUInt16[n]; for (int i = 0; i < objArrList.size(); i++) { ConfObject[] objArr = objArrList.get(i); name[i] = (ConfBuf) objArr[0]; ip[i] = (ConfIPv4) objArr[1]; port[i] = (ConfUInt16) objArr[2]; }
numOfObjects
- number of objects to retrieveinstance
- instance integernumOfInstances
- number if instances to retrievefmt
- arguments
- IOException
ConfException
public List<ConfObject[]> getObjects(int numOfObjects, int instance, int numOfInstances, ConfPath path) throws IOException, ConfException
numOfObjects
- number of objects to retrieveinstance
- instance integernumOfInstances
- number if instances to retrievepath
- IOException
ConfException
public void setObject(ConfValue[] values, String fmt, Object... arguments) throws IOException, ConfException
CdbSession.setObject(ConfValue[], ConfPath)
but specifies
element using path stringfmt
- path stringarguments
- optional parameters for substitution in fmtIOException
ConfException
public void setValues(ConfXMLParam[] params, ConfPath path) throws IOException, ConfException
params
- path
- IOException
ConfException
public void setValues(ConfXMLParam[] params, String fmt, Object... arguments) throws IOException, ConfException
CdbSession.setValues(ConfXMLParam[], ConfPath)
but specifies
element using path stringparams
- fmt
- arguments
- IOException
ConfException
public void setValues(List<ConfXMLParam> params, ConfPath path) throws IOException, ConfException
IOException
ConfException
public ConfXMLParam[] getValues(ConfXMLParam[] params, String fmt, Object... arguments) throws IOException, ConfException
String path = "/mtest/servers/server"; int n = cdbsess.getNumberOfInstances(path); List<ConfObject[]> objArrList = cdbsess.getObjects(4,0,n,path); //when reading ip/port, we need 5 elements per interface: //begin + name (key) + ip + port + end ConfXMLParam[] values = new ConfXMLParam[5*n]; int j = 0; for (int i=0; i<n; i++) { values[j++] = new ConfXMLParamCdbStart(ns, "server", i); values[j++] = new ConfXMLParamLeaf(ns, "name"); values[j++] = new ConfXMLParamLeaf(ns, "ip"); values[j++] = new ConfXMLParamLeaf(ns, "port"); values[j++] = new ConfXMLParamStop(ns, "server"); } path = "/mtest/servers"; ConfXMLParam[] resvalues = cdbsess.getValues(values, path); // extract name for enabled interfaces String name = null; int noOfInterfaces = 0; for (int i = 0; i < n; i++) { ConfIPv4 ip4 = (ConfIPv4) resvalues[i*5+2].getValue(); name = resvalues[i*5+1].getValue().toString(); path = "/mtest/servers/server{%s}/interface"; noOfInterfaces = cdbsess.getNumberOfInstances(path, name); System.out.println("name = "+ name); } int n_if = j; j = 0; values = new ConfXMLParam[noOfInterfaces*4]; for (int i=0; i<noOfInterfaces; i++) { values[j++] = new ConfXMLParamCdbStart(ns.hash(), ns.mtest_interface, i); values[j++] = new ConfXMLParamLeaf(ns.hash(), ns.mtest_name); values[j++] = new ConfXMLParamLeaf(ns.hash(), ns.mtest_mtu); values[j++] = new ConfXMLParamStop(ns.hash(), ns.mtest_interface); } path = "/mtest/servers/server{%s}"; resvalues = cdbsess.getValues(values, path, name); ...
params
- fmt
- arguments
- IOException
ConfException
public ConfXMLParam[] getValues(ConfXMLParam[] params, ConfPath path) throws IOException, ConfException
params
- path
- IOException
ConfException
public ConfObject getCase(String choice, String fmt, Object... arguments) throws IOException, ConfException
Retrieve the currently selected case.
When we use the YANG choice construct in the data model, this method
can be used to find the currently selected case, avoiding useless
CdbSession.getElem(ConfPath)
etc requests for elements
that belong to other
cases.
The fmt
give the path to the container where the
choice is defined,
The choice
is the name of the choice.
The case value is returned as type ConfTag
.
If no case is currently selected (i.e. for an optional choice
that does not have a default case), the function will fail with a an
exception with code ConfException.ERR_NOEXISTS
.
choice
- the name of the choicefmt
- format string pointing to the choicearguments
- replacement arguments for fmt stringConfTag
IOException
ConfException
public ConfObject getCase(String choice, ConfPath path) throws IOException, ConfException
Retrieve the currently selected case.
Same functionality as CdbSession.getCase(String, String, Object...)
but
takes a already constructed ConfPath
object as argument
instead of fmt, arguments.
choice
- the name of choicepath
- ConfPath
pointing to the choiceConfTag
IOException
ConfException
public void setCase(String choice, String scase, String fmt, Object... arguments) throws IOException, ConfException
When we use the YANG choice construct in the data model, this function can be used to select the current case.
When configuration data is modified by northbound agents, the current case is implicitly selected (and elements for other cases potentially deleted) by the setting of elements in a choice.
For operational data in CDB however, this is under direct control of the application, which needs to explicitly set the current case. Setting the case will also automatically delete elements belonging to other cases, but it is up to the application to not set any elements in the "wrong" case. The fmt, ... arguments give the path to the container where the choice is defined, and choice and scase are the choice and case names. For an optional choice, it is possible to have no case at all selected. To indicate that the previously selected case should be deleted without selecting another case, we can pass NULL for the scase argument.
choice
- choice stringscase
- case stringfmt
- format string pointing to the choicearguments
- replacement arguments for fmt stringIOException
ConfException
public void setCase(String choice, String scase, ConfPath path) throws IOException, ConfException
choice
- choice stringscase
- case stringpath
- ConfPath object pointing to the choiceIOException
ConfException