Skip to content

Commit 4d6d480

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, created a Datastore enum. Overriding toString in enum to ensure lowercase.
1 parent 5720d52 commit 4d6d480

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import lombok.NonNull;
1818
import lombok.ToString;
1919
import lombok.extern.slf4j.Slf4j;
20+
import net.juniper.netconf.element.Datastore;
2021
import net.juniper.netconf.element.Hello;
2122
import org.w3c.dom.Document;
2223
import org.xml.sax.SAXException;
@@ -838,14 +839,41 @@ 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 {
842851
if (netconfSession == null) {
843852
throw new IllegalStateException("Cannot execute RPC, you need to " +
844853
"establish a connection first.");
845854
}
846-
return this.netconfSession.getRunningConfigAndState(filter);
855+
return this.netconfSession.getRunningConfigAndState(xpathFilter);
847856
}
848857

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, @NonNull Datastore datastore) throws IOException, SAXException {
869+
if (netconfSession == null) {
870+
throw new IllegalStateException("Cannot execute RPC, you need to " +
871+
"establish a connection first.");
872+
}
873+
return this.netconfSession.getData(xpathFilter, datastore);
874+
}
875+
876+
849877
/**
850878
* Retrieve the whole candidate configuration.
851879
*

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.jcraft.jsch.JSchException;
1515
import lombok.NonNull;
1616
import lombok.extern.slf4j.Slf4j;
17+
import net.juniper.netconf.element.Datastore;
1718
import net.juniper.netconf.element.Hello;
1819
import net.juniper.netconf.element.RpcReply;
1920
import org.w3c.dom.Document;
@@ -159,6 +160,8 @@ private void sendRpcRequest(String rpc) throws IOException {
159160
// RFC conformance for XML type, namespaces and message ids for RPCs
160161
messageId++;
161162
rpc = rpc.replace("<rpc>", "<rpc" + getRpcAttributes() + " message-id=\"" + messageId + "\">").trim();
163+
rpc = rpc.replace("<datastore>", "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">");
164+
rpc = rpc.replace("<get-data>", "<get-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">");
162165
if (!rpc.contains(NetconfConstants.XML_VERSION)) {
163166
rpc = NetconfConstants.XML_VERSION + rpc;
164167
}
@@ -301,17 +304,31 @@ private String getConfig(String configTree) throws IOException {
301304
return lastRpcReply;
302305
}
303306

304-
public XML getRunningConfigAndState(String filter) throws IOException, SAXException {
307+
public XML getRunningConfigAndState(String xpathFilter) throws IOException, SAXException {
305308
String rpc = "<rpc>" +
306309
"<get>" +
307-
(filter == null ? "" : filter) +
310+
(xpathFilter == null ? "" : xpathFilter) +
308311
"</get>" +
309312
"</rpc>" +
310313
NetconfConstants.DEVICE_PROMPT;
311314
setLastRpcReply(getRpcReply(rpc));
312315
return convertToXML(lastRpcReply);
313316
}
314317

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.juniper.netconf.element;
2+
3+
import java.util.Locale;
4+
5+
/**
6+
* Datastore
7+
* <p>
8+
* As defined by RFC-8342.
9+
* See https://datatracker.ietf.org/doc/html/rfc8342#section-5
10+
*/
11+
public enum Datastore {
12+
RUNNING, CANDIDATE, STARTUP, INTENDED, OPERATIONAL;
13+
14+
@Override
15+
public String toString() {
16+
return this.name().toLowerCase(Locale.US);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.juniper.netconf;
2+
3+
import net.juniper.netconf.element.Datastore;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.Matchers.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
public class DatastoreTest {
10+
@Test
11+
public void testDatastoreName() {
12+
assertThat(Datastore.OPERATIONAL.toString(), is("operational"));
13+
assertThat(Datastore.RUNNING.toString(), is("running"));
14+
assertThat(Datastore.CANDIDATE.toString(), is("candidate"));
15+
assertThat(Datastore.STARTUP.toString(), is("startup"));
16+
assertThat(Datastore.INTENDED.toString(), is("intended"));
17+
}
18+
}

0 commit comments

Comments
 (0)