Skip to content

Commit b11f677

Browse files
committed
Implemented get-data
- RFC-8526: https://datatracker.ietf.org/doc/html/rfc8526#section-3.1.1 - Also, updated filter variable name to be specific to xpathfilter - Per PR discussion, using Locale.US isntead of Locale.ROOT to be more specific to US letters, as the RFC is in US english.
1 parent 5720d52 commit b11f677

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/main/java/net/juniper/netconf/Device.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.charset.Charset;
3232
import java.util.Arrays;
3333
import java.util.List;
34+
import java.util.Locale;
3435

3536
/**
3637
* A <code>Device</code> is used to define a Netconf server.
@@ -838,14 +839,48 @@ public XML getRunningConfig(String configTree) throws SAXException,
838839
return this.netconfSession.getRunningConfig(configTree);
839840
}
840841

841-
public XML getRunningConfigAndState(String filter) throws IOException, SAXException {
842+
/**
843+
* Retrieve the running configuration, or part of the configuration.
844+
*
845+
* @param xpathFilter example <code><filter xmlns:model='urn:path:for:my:model' select='/model:*'></filter></code>
846+
* @return configuration data as XML object.
847+
* @throws java.io.IOException If there are errors communicating with the netconf server.
848+
* @throws org.xml.sax.SAXException If there are errors parsing the XML reply.
849+
*/
850+
public XML getRunningConfigAndState(String xpathFilter) throws IOException, SAXException {
851+
if (netconfSession == null) {
852+
throw new IllegalStateException("Cannot execute RPC, you need to " +
853+
"establish a connection first.");
854+
}
855+
return this.netconfSession.getRunningConfigAndState(xpathFilter);
856+
}
857+
858+
859+
/**
860+
* Run the <get-data> call to netconf server and retrieve data as an XML.
861+
*
862+
* @param xpathFilter example <code><filter xmlns:model='urn:path:for:my:model' select='/model:*'></filter></code>
863+
* @param datastore running, startup, candidate, or operational
864+
* @return configuration data as XML object.
865+
* @throws java.io.IOException If there are errors communicating with the netconf server.
866+
* @throws org.xml.sax.SAXException If there are errors parsing the XML reply.
867+
*/
868+
public XML getData(String xpathFilter, String datastore) throws IOException, SAXException {
842869
if (netconfSession == null) {
843870
throw new IllegalStateException("Cannot execute RPC, you need to " +
844871
"establish a connection first.");
845872
}
846-
return this.netconfSession.getRunningConfigAndState(filter);
873+
if (datastore != null) switch (datastore.trim().toLowerCase(Locale.US)) {
874+
case "running":
875+
case "startup":
876+
case "candidate":
877+
case "operational":
878+
return this.netconfSession.getData(xpathFilter, datastore.trim().toLowerCase(Locale.US));
879+
}
880+
throw new IllegalStateException("Unable to process datastore: " + datastore);
847881
}
848882

883+
849884
/**
850885
* Retrieve the whole candidate configuration.
851886
*

src/main/java/net/juniper/netconf/NetconfSession.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ private void sendRpcRequest(String rpc) throws IOException {
159159
// RFC conformance for XML type, namespaces and message ids for RPCs
160160
messageId++;
161161
rpc = rpc.replace("<rpc>", "<rpc" + getRpcAttributes() + " message-id=\"" + messageId + "\">").trim();
162+
rpc = rpc.replace("<datastore>", "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">");
163+
rpc = rpc.replace("<get-data>", "<get-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">");
162164
if (!rpc.contains(NetconfConstants.XML_VERSION)) {
163165
rpc = NetconfConstants.XML_VERSION + rpc;
164166
}
@@ -301,17 +303,31 @@ private String getConfig(String configTree) throws IOException {
301303
return lastRpcReply;
302304
}
303305

304-
public XML getRunningConfigAndState(String filter) throws IOException, SAXException {
306+
public XML getRunningConfigAndState(String xpathFilter) throws IOException, SAXException {
305307
String rpc = "<rpc>" +
306308
"<get>" +
307-
(filter == null ? "" : filter) +
309+
(xpathFilter == null ? "" : xpathFilter) +
308310
"</get>" +
309311
"</rpc>" +
310312
NetconfConstants.DEVICE_PROMPT;
311313
setLastRpcReply(getRpcReply(rpc));
312314
return convertToXML(lastRpcReply);
313315
}
314316

317+
public XML getData(String xpathFilter, String datastore)
318+
throws IOException, SAXException {
319+
320+
String rpc = "<rpc>" +
321+
"<get-data>" +
322+
"<datastore>ds:" + datastore + "</datastore>" +
323+
(xpathFilter == null ? "" : xpathFilter) +
324+
"</get-data>" +
325+
"</rpc>" +
326+
NetconfConstants.DEVICE_PROMPT;
327+
lastRpcReply = getRpcReply(rpc);
328+
return convertToXML(lastRpcReply);
329+
}
330+
315331
private String getConfig(String target, String configTree)
316332
throws IOException {
317333

0 commit comments

Comments
 (0)