Skip to content

Implemented get-data #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/java/net/juniper/netconf/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.NonNull;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import net.juniper.netconf.element.Datastore;
import net.juniper.netconf.element.Hello;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -838,14 +839,41 @@ public XML getRunningConfig(String configTree) throws SAXException,
return this.netconfSession.getRunningConfig(configTree);
}

public XML getRunningConfigAndState(String filter) throws IOException, SAXException {
/**
* Retrieve the running configuration, or part of the configuration.
*
* @param xpathFilter example <code><filter xmlns:model='urn:path:for:my:model' select='/model:*'></filter></code>
* @return configuration data as XML object.
* @throws java.io.IOException If there are errors communicating with the netconf server.
* @throws org.xml.sax.SAXException If there are errors parsing the XML reply.
*/
public XML getRunningConfigAndState(String xpathFilter) throws IOException, SAXException {
if (netconfSession == null) {
throw new IllegalStateException("Cannot execute RPC, you need to " +
"establish a connection first.");
}
return this.netconfSession.getRunningConfigAndState(filter);
return this.netconfSession.getRunningConfigAndState(xpathFilter);
}


/**
* Run the <get-data> call to netconf server and retrieve data as an XML.
*
* @param xpathFilter example <code><filter xmlns:model='urn:path:for:my:model' select='/model:*'></filter></code>
* @param datastore running, startup, candidate, or operational
* @return configuration data as XML object.
* @throws java.io.IOException If there are errors communicating with the netconf server.
* @throws org.xml.sax.SAXException If there are errors parsing the XML reply.
*/
public XML getData(String xpathFilter, @NonNull Datastore datastore) throws IOException, SAXException {
if (netconfSession == null) {
throw new IllegalStateException("Cannot execute RPC, you need to " +
"establish a connection first.");
}
return this.netconfSession.getData(xpathFilter, datastore);
}


/**
* Retrieve the whole candidate configuration.
*
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/net/juniper/netconf/NetconfSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.jcraft.jsch.JSchException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.juniper.netconf.element.Datastore;
import net.juniper.netconf.element.Hello;
import net.juniper.netconf.element.RpcReply;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -159,6 +160,8 @@ private void sendRpcRequest(String rpc) throws IOException {
// RFC conformance for XML type, namespaces and message ids for RPCs
messageId++;
rpc = rpc.replace("<rpc>", "<rpc" + getRpcAttributes() + " message-id=\"" + messageId + "\">").trim();
rpc = rpc.replace("<datastore>", "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">");
rpc = rpc.replace("<get-data>", "<get-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">");
if (!rpc.contains(NetconfConstants.XML_VERSION)) {
rpc = NetconfConstants.XML_VERSION + rpc;
}
Expand Down Expand Up @@ -301,17 +304,31 @@ private String getConfig(String configTree) throws IOException {
return lastRpcReply;
}

public XML getRunningConfigAndState(String filter) throws IOException, SAXException {
public XML getRunningConfigAndState(String xpathFilter) throws IOException, SAXException {
String rpc = "<rpc>" +
"<get>" +
(filter == null ? "" : filter) +
(xpathFilter == null ? "" : xpathFilter) +
"</get>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
setLastRpcReply(getRpcReply(rpc));
return convertToXML(lastRpcReply);
}

public XML getData(String xpathFilter, @NonNull Datastore datastore)
throws IOException, SAXException {

String rpc = "<rpc>" +
"<get-data>" +
"<datastore>ds:" + datastore + "</datastore>" +
(xpathFilter == null ? "" : xpathFilter) +
"</get-data>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
return convertToXML(lastRpcReply);
}

private String getConfig(String target, String configTree)
throws IOException {

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/juniper/netconf/element/Datastore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.juniper.netconf.element;

import java.util.Locale;

/**
* Datastore
* <p>
* As defined by RFC-8342.
* See https://datatracker.ietf.org/doc/html/rfc8342#section-5
*/
public enum Datastore {
RUNNING, CANDIDATE, STARTUP, INTENDED, OPERATIONAL;

@Override
public String toString() {
return this.name().toLowerCase(Locale.US);
}
}
18 changes: 18 additions & 0 deletions src/test/java/net/juniper/netconf/DatastoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.juniper.netconf;

import net.juniper.netconf.element.Datastore;
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class DatastoreTest {
@Test
public void testDatastoreName() {
assertThat(Datastore.OPERATIONAL.toString(), is("operational"));
assertThat(Datastore.RUNNING.toString(), is("running"));
assertThat(Datastore.CANDIDATE.toString(), is("candidate"));
assertThat(Datastore.STARTUP.toString(), is("startup"));
assertThat(Datastore.INTENDED.toString(), is("intended"));
}
}