ConfObject[ ] to JSON transformation

Hello,

Is there any way to transform ConfObject[ ] to JSON equivalent in Java ?

Thanx,

Since I have not taken any answer so far…

Is it any way to “convert” a ConfObject[] to XMLParams[] ?

Is there any binding somehow ?

Too little info in a ConfObject alone. In what context are you trying to do this?

I am trying to create a json message after the reception of a message from confD.
If it was like in the case of rpc class where we get the parameters of message in XMLParams then it is easy since there is a function that turns this to XML payload and then there are various implementations that XML is transformed to JSON.
In the case of callback functions I don’t have this option since everything is coming within ConfObject[].
As a result especially in the case we have multiple keys in the YANG model for a list
i.e. /config/vm/vm{key1 key2 key3 key4}/hostName …

then this makes the things complicated since in order to create the json message i need that names of the keys also.
Hence access to maapi in order to get the names for the paths and then mapping each one to each element etc…

Is there any convenient way to extract the iinfo from ConfObject to truncated to XMLParams or to any JSON equivalent at least ?

You can use the schema information to get the node tag for the key:

// "kp" is a ConfObject[]
String confdAddress = "127.0.0.1";
int confdPort = Conf.PORT;
Socket sock = new Socket(confdAddress, confdPort);
Maapi maapi = new Maapi(sock);
MaapiSchemas cs = maapi.loadSchemas();
ConfPath path = new ConfPath(kp);
MaapiSchemas.CSNode node = cs.findCSNode(new myyangnsclass().uri(), path.toString());
System.err.printf("%s %s\n", path.toString(), node.getKey(0).getTag());

“node.getKey(0).getTag()” above get a string representation of key 0’s node tag.
You can also use “getKeys()” to get for example all the key node tags.

Another option is to use maapi.saveConfig():

     public void printJson(ConfObject[] kp) throws Exception {
        ConfPath path = new ConfPath(kp);
	String host = "localhost";
	int port = Conf.PORT;
	Socket s = new Socket(host, port);
	Maapi maapi = new Maapi(s);
	maapi.startUserSession("admin",
			   InetAddress.getByName("localhost"),
			       "maapi",
			       new String[] { "admin" },
			       MaapiUserSessionFlag.PROTO_TCP);
	int tid = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ);

	MaapiInputStream in =
	    maapi.saveConfig(tid,
			     EnumSet.of(MaapiConfigFlag.MAAPI_CONFIG_JSON),
			     path.toString());

	String jstr = IOUtils.toString(in, "UTF8");
	System.err.printf("%s\n", jstr);
	in.close();
	
	maapi.finishTrans(tid);
	s.close();
    }

With the last example I don’t get any input !

i.e I get only
{
“data”: {
}
}

What context are you printing from? Should there be any data in the CDB RUNNING datastore under the keypath you provided?

Ok I made it work finally. The thing is that this function (saveConfig) is calling “back” confD with getElem etc.

But now we have the following issue. When we received something we cannot really call it inside getElem/Finish or what so ever since we are entering in endless loops.
i.e finish is calling saveconfig and save config in the new run calls confD again which is turn calls saveConfig etc.

If you were implementing for example a CDB subscriber, the printJson() option would have worked as the data you read is stored in CDB.

Since you are now (indirectly) saying that you are implementing an external DB data provider, as you discovered, you get a loop issue.

You can of course prevent the loop from occurring by checking if you are calling printJson() from within a printJson(), but then you end up with a “chicken or the egg” problem.

For data providers, I suggest you use the findCSNode() getKeys() getTag() suggestion to create your own JSON representation of the path.

Yet another option could be to get the more JSON friendly XPath representation of the ConfObject[] keypath.

Example:

System.err.printf("%s %s\n", new ConfPath(kp).toString(), new ConfPath(kp).toXPathString());

ConfPath(kp).toString() output example:
/hst:hosts/host{test}/name

ConfPath(kp).toXPathString():
hst:hosts/hst:host[hst:name='test']/hst:name