cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
904
Views
10
Helpful
7
Replies

Does NSO5.8 provide a way to write xml to cdb in a transaction?

JennyW
Level 1
Level 1

In generic NED the show() callback is responsible to write sync-from response (i.e. running-config) from device to /config node in CDB. I don't think NED should be aware of the details of the configuration, however, I don't see maapi provides an interface to write xml to cdb. I did notice maapi provides loadConfig to take a xml file and write into cdb config but that doesn't really apply to this scenario.

1 Accepted Solution

Accepted Solutions

I manage to fix your issue by changing the flags that you were using

 

maapi.loadConfigCmds(tHandle,EnumSet.of(MaapiConfigFlag.MAAPI_CONFIG_MERGE, MaapiConfigFlag.MAAPI_CONFIG_XML),xml_resp ,path0)

View solution in original post

7 Replies 7

Nabsch
Spotlight
Spotlight

Hello

 

In Java API, there is 3 available method that can be used to write Config into CDB.

 

Here a link to Java Doc 

 

voidloadConfig(int tid, EnumSet<MaapiConfigFlag> flags, String filename)
This function loads a configuration from filename into the server.
voidloadConfigCmds(int tid, EnumSet<MaapiConfigFlag> flags, String cmds, String fmt, Object... arguments)
This function loads a configuration from a string into the server.
MaapiOutputStreamloadConfigStream(int tid, EnumSet<MaapiConfigFlag> flags)
Load configuration from a OuputStream into ConfD/NCS.

Thank you, Nabsch, loadConfigCmds seems to be the API I can use, just tried it but got authentication error, using maapi.setElem doesn't give me this error. I didn't see Java Doc mention anything about this authentication.

I think you forgot to upload the log. Can you provide us your code as well? 

 

 

I noticed that the exception error says "no mount id exists for 

/ncs:devices/ncs:device[ncs:name='test']/ncs:config but I do have it:

% show ncs-devices device test config

hardware {

    component Simulator {

        class    chassis;

        alias    test;

        asset-id test-1;

    }

}

[ok]

 

This is the code:

String xml_resp =

"<hardware xmlns=\"urn:ietf:params:xml:ns:yang:ietf-hardware\">" +

"<component>" +

"<name>Chassis</name>" +

"<alias>simulator</alias>" +

"<class xmlns:ianahw=\"urn:ietf:params:xml:ns:yang:iana-hardware\">ianahw:chassis</class>" +

"</component>" +

"</hardware>";

String path0 = "/ncs:devices/device{" + deviceName + "}/config";

maapi.loadConfigCmds(tHandle, EnumSet.of(MaapiConfigFlag.XML_FORMAT, MaapiConfigFlag.MAAPI_CONFIG_XML), xml_resp, path0);

 

and the log:

<INFO> 13-Jun-2022::22:26:13.851 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id --> {admin}

<INFO> 13-Jun-2022::22:26:13.851 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=8,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<admin>>}},[]},{#Bin<<admin>>}}]) -->

<INFO> 13-Jun-2022::22:26:13.851 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=8,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<support>>}},[]},{#Bin<<support>>}}]) --> {support}

<INFO> 13-Jun-2022::22:26:13.852 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id --> {support}

<INFO> 13-Jun-2022::22:26:13.852 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=8,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<support>>}},[]},{#Bin<<support>>}}]) -->

<INFO> 13-Jun-2022::22:26:13.852 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (8) --> null

<INFO> 13-Jun-2022::22:26:13.855 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user -->

<INFO> 13-Jun-2022::22:26:13.856 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user --> cursor-id 9

<INFO> 13-Jun-2022::22:26:13.856 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=9,path=/aaa:aaa/authentication/users/user,prev=first]) -->

<INFO> 13-Jun-2022::22:26:13.860 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=9,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<admin>>}},[]},{#Bin<<admin>>}}]) --> {admin}

<INFO> 13-Jun-2022::22:26:13.860 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id 9) --> {admin}

<INFO> 13-Jun-2022::22:26:13.860 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=9,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<admin>>}},[]},{#Bin<<admin>>}}]) -->

<INFO> 13-Jun-2022::22:26:13.861 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=9,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<support>>}},[]},{#Bin<<support>>}}]) --> {support}

<INFO> 13-Jun-2022::22:26:13.861 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id 9) --> {support}

<INFO> 13-Jun-2022::22:26:13.861 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=9,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<support>>}},[]},{#Bin<<support>>}}]) -->

<INFO> 13-Jun-2022::22:26:13.862 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (9) --> null

<INFO> 13-Jun-2022::22:26:13.868 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user -->

<INFO> 13-Jun-2022::22:26:13.868 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user --> cursor-id 10

<INFO> 13-Jun-2022::22:26:13.869 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=10,path=/aaa:aaa/authentication/users/user,prev=first]) -->

<INFO> 13-Jun-2022::22:26:13.874 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=10,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<admin>>}},[]},{#Bin<<admin>>}}]) --> {admin}

<INFO> 13-Jun-2022::22:26:13.875 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id 10) --> {admin}

<INFO> 13-Jun-2022::22:26:13.875 Maapi Did-46-Worker-14: - MAAPI_GET_ELEM (tid:245,path:/aaa:aaa/authentication/users/user{admin}/name) -->

<INFO> 13-Jun-2022::22:26:13.876 Maapi Did-46-Worker-14: - MAAPI_GET_ELEM --> admin

<INFO> 13-Jun-2022::22:26:13.877 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user -->

<INFO> 13-Jun-2022::22:26:13.877 Maapi Did-46-Worker-14: - MAAPI Cursor create tid 245 /aaa:aaa/authentication/users/user --> cursor-id 11

<INFO> 13-Jun-2022::22:26:13.877 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=11,path=/aaa:aaa/authentication/users/user,prev=first]) -->

<INFO> 13-Jun-2022::22:26:13.879 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (MaapiCursor[th=245,cursor_id=11,path=/aaa:aaa/authentication/users/user,prev={{xds_iid,{next,{#Bin<<admin>>}},[]},{#Bin<<admin>>}}]) --> {admin}

<INFO> 13-Jun-2022::22:26:13.879 Maapi Did-46-Worker-14: - MAAPI_GET_NEXT (cursor-id 11) --> {admin}

<INFO> 13-Jun-2022::22:26:13.879 Maapi Did-46-Worker-14: - MAAPI_GET_ELEM (tid:245,path:/aaa:aaa/authentication/users/user{admin}/name) -->

<INFO> 13-Jun-2022::22:26:13.884 Maapi Did-46-Worker-14: - MAAPI_GET_ELEM --> admin

<ERROR> 13-Jun-2022::22:26:13.926 NedWorker Ned-Worker-Thread-(testgn-gen-1.0:testgn-gen-1.0)-0: - NedWorker error for ned_show_generic

com.tailf.ned.NedException: 

at com.vecima.testgn.TestNed.show(TestNed.java:374) ~[testgn.jar:?]

at com.tailf.ned.NedWorker.dorun(NedWorker.java:1934) [ncs-5.8.jar:?]

at com.tailf.ned.NedWorker.run(NedWorker.java:329) [ncs-5.8.jar:?]

Caused by: com.tailf.conf.ConfException: Error on line 1: no mount id exists for: /ncs:devices/ncs:device[ncs:name='test']/ncs:config

 

at com.tailf.conf.ConfException.mk(ConfException.java:123) ~[conf-api-7.8.jar:?]

at com.tailf.conf.ConfException.mk(ConfException.java:67) ~[conf-api-7.8.jar:?]

at com.tailf.maapi.Maapi.loadConfigCmds(Maapi.java:7776) ~[conf-api-7.8.jar:?]

at com.vecima.testgn.TestNed.show(TestNed.java:363) ~[testgn.jar:?]

... 2 more

I manage to fix your issue by changing the flags that you were using

 

maapi.loadConfigCmds(tHandle,EnumSet.of(MaapiConfigFlag.MAAPI_CONFIG_MERGE, MaapiConfigFlag.MAAPI_CONFIG_XML),xml_resp ,path0)

That is it! Thank you so much. 

Nabsch
Spotlight
Spotlight

Here a working example with loadConfigCmds. I used a string that contain the XML content .

 

Here a link to the available flag 

 

 

//Socket for Action
import java.net.Socket;
import java.util.ArrayList;
import java.util.EnumSet;

import com.tailf.maapi.MaapiConfigFlag;
import com.tailf.maapi.MaapiUserSessionFlag;
import com.tailf.maapi.Maapi;


import com.tailf.ncs.NcsMain;

		try {

			
			Socket socket = new Socket(NcsMain.getInstance().getNcsHost(), NcsMain.getInstance().getNcsPort());
			Maapi maapi = new Maapi(socket);

			maapi.startUserSession("admin", maapi.getSocket().getInetAddress(), "system", new String[] { "admin" },
					MaapiUserSessionFlag.PROTO_TCP);
			int th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ_WRITE);


			ArrayList<MaapiConfigFlag> listConfigFlag= new ArrayList<MaapiConfigFlag>();
			listConfigFlag.add(MaapiConfigFlag.MAAPI_CONFIG_MERGE);
			listConfigFlag.add(MaapiConfigFlag.MAAPI_CONFIG_XML);
			EnumSet<MaapiConfigFlag> flags = EnumSet.copyOf(listConfigFlag);

			String cmds = "<vpn xmlns=\"http://com/example/l3vpn\">\n"
					+ "  <l3vpn>\n"
					+ "    <name>aa</name>\n"
					+ "  </l3vpn>\n"
					+ "</vpn>"; // string representing the configuration to be loaded

			String fmt="/"; // path relative to which the configuration should be loaded

			maapi.loadConfigCmds(th,flags,cmds,fmt);
			maapi.applyTrans(th,true);

			maapi.finishTrans(th);// Finish the transaction
			maapi.endUserSession(); // Close user session
			socket.close();// Close the Socket

		} catch (Exception e) {
			throw new Exception(e);
		}