Skip to content
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
10 changes: 2 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>net.juniper.netconf</groupId>
<artifactId>netconf-java</artifactId>
<version>2.1.1.5</version>
<version>2.1.1.6-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -206,16 +206,10 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.24.0</version>
<version>3.11.2</version>
<scope>test</scope>
</dependency>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/juniper/netconf/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private String createHelloRPC(List<String> capabilities) {
return Hello.builder()
.capabilities(capabilities)
.build()
.toXML()
.getXml()
+ NetconfConstants.DEVICE_PROMPT;
}

Expand Down
93 changes: 51 additions & 42 deletions src/main/java/net/juniper/netconf/NetconfSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.juniper.netconf.element.Hello;
import net.juniper.netconf.element.RpcReply;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class NetconfSession {
private final OutputStream stdOutStreamToDevice;

private String lastRpcReply;
private RpcReply lastRpcReplyObject;
private final DocumentBuilder builder;
private final int commandTimeout;

Expand Down Expand Up @@ -117,14 +119,7 @@ private XML convertToXML(String xml) throws SAXException, IOException {
}

private void sendHello(String hello) throws IOException {
String reply = getRpcReply(hello);
serverCapability = reply;
lastRpcReply = reply;
try {
serverHello = Hello.from(reply);
} catch (final ParserConfigurationException | SAXException | XPathExpressionException e) {
throw new NetconfException("Invalid <hello> message from server: " + reply, e);
}
setHelloReply(getRpcReply(hello));
}

@VisibleForTesting
Expand Down Expand Up @@ -228,11 +223,30 @@ public void loadXMLConfiguration(String configuration, String loadType) throws I
"</edit-config>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new LoadException("Load operation returned error.");
}

private void setHelloReply(final String reply) throws IOException {
this.serverCapability = reply;
this.lastRpcReply = reply;
try {
this.serverHello = Hello.from(reply);
} catch (final ParserConfigurationException | SAXException | XPathExpressionException e) {
throw new NetconfException("Invalid <hello> message from server: " + reply, e);
}
}

private void setLastRpcReply(final String reply) throws IOException {
this.lastRpcReply = reply;
try {
this.lastRpcReplyObject = RpcReply.from(reply);
} catch (final ParserConfigurationException | SAXException | XPathExpressionException e) {
throw new NetconfException("Invalid <rpc-reply> message from server: " + lastRpcReply, e);
}
}

/**
* Loads the candidate configuration, Configuration should be in text/tree
* format.
Expand Down Expand Up @@ -265,7 +279,7 @@ public void loadTextConfiguration(String configuration, String loadType) throws
"</edit-config>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new LoadException("Load operation returned error");
}
Expand All @@ -283,7 +297,7 @@ private String getConfig(String configTree) throws IOException {
"</get-config>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return lastRpcReply;
}

Expand All @@ -294,7 +308,7 @@ public XML getRunningConfigAndState(String filter) throws IOException, SAXExcept
"</get>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return convertToXML(lastRpcReply);
}

Expand All @@ -310,7 +324,7 @@ private String getConfig(String target, String configTree)
"</get-config>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return lastRpcReply;
}

Expand Down Expand Up @@ -348,7 +362,7 @@ public Hello getServerHello() {
*/
public XML executeRPC(String rpcContent) throws SAXException, IOException {
String rpcReply = getRpcReply(fixupRpc(rpcContent));
lastRpcReply = rpcReply;
setLastRpcReply(rpcReply);
return convertToXML(rpcReply);
}

Expand Down Expand Up @@ -481,7 +495,7 @@ public void close() throws IOException {
"<close-session/>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
netconfChannel.disconnect();
}

Expand All @@ -493,18 +507,7 @@ public void close() throws IOException {
* @throws java.io.IOException If there are issues communicating with the netconf server.
*/
public boolean hasError() throws SAXException, IOException {
if (lastRpcReply == null || !(lastRpcReply.contains("<rpc-error>")))
return false;
String errorSeverity = parseForErrors(lastRpcReply);
return errorSeverity != null && errorSeverity.equals("error");
}

private String parseForErrors(String inputXmlReply) throws IOException, SAXException {
XML xmlReply = convertToXML(lastRpcReply);
List<String> tagList = new ArrayList<>();
tagList.add("rpc-error");
tagList.add("error-severity");
return xmlReply.findValue(tagList);
return lastRpcReplyObject.hasErrors();
}

/**
Expand All @@ -515,10 +518,7 @@ private String parseForErrors(String inputXmlReply) throws IOException, SAXExcep
* @throws java.io.IOException If there are issues communicating with the netconf server.
*/
public boolean hasWarning() throws SAXException, IOException {
if (lastRpcReply == null || !(lastRpcReply.contains("<rpc-error>")))
return false;
String errorSeverity = parseForErrors(lastRpcReply);
return errorSeverity != null && errorSeverity.equals("warning");
return lastRpcReplyObject.hasWarnings();
}

/**
Expand All @@ -528,7 +528,7 @@ public boolean hasWarning() throws SAXException, IOException {
* @return true if &lt;ok/&gt; tag is found in last RPC reply.
*/
public boolean isOK() {
return lastRpcReply != null && lastRpcReply.contains("<ok/>");
return lastRpcReplyObject.isOk();
}

/**
Expand All @@ -547,7 +547,7 @@ public boolean lockConfig() throws IOException, SAXException {
"</lock>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return !hasError() && isOK();
}

Expand All @@ -567,7 +567,7 @@ public boolean unlockConfig() throws IOException, SAXException {
"</unlock>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return !hasError() && isOK();
}

Expand All @@ -591,7 +591,7 @@ public void loadSetConfiguration(String configuration) throws IOException, SAXEx
"</configuration-set>" +
"</load-configuration>" +
"</rpc>";
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new LoadException("Load operation returned error");
}
Expand Down Expand Up @@ -725,7 +725,7 @@ public void commit() throws IOException, SAXException {
"<commit/>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new CommitException("Commit operation returned error.");
}
Expand All @@ -747,7 +747,7 @@ public void commitConfirm(long seconds) throws IOException, SAXException {
"</commit>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new CommitException("Commit operation returned " +
"error.");
Expand All @@ -767,7 +767,7 @@ public void commitFull() throws CommitException, IOException, SAXException {
"</commit-configuration>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
if (hasError() || !isOK())
throw new CommitException("Commit operation returned error.");
}
Expand Down Expand Up @@ -842,7 +842,7 @@ public boolean validate() throws IOException, SAXException {
"</validate>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
return !hasError() && isOK();
}

Expand Down Expand Up @@ -878,7 +878,7 @@ public String runCliCommand(String command) throws IOException, SAXException {
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
String rpcReply = getRpcReply(rpc);
lastRpcReply = rpcReply;
setLastRpcReply(rpcReply);
XML xmlReply = convertToXML(rpcReply);
List<String> tags = new ArrayList<>();
tags.add("output");
Expand Down Expand Up @@ -927,7 +927,7 @@ public void openConfiguration(String mode) throws IOException {
rpc.append("</open-configuration>");
rpc.append("</rpc>");
rpc.append(NetconfConstants.DEVICE_PROMPT);
lastRpcReply = getRpcReply(rpc.toString());
setLastRpcReply(getRpcReply(rpc.toString()));
}

/**
Expand All @@ -941,7 +941,7 @@ public void closeConfiguration() throws IOException {
"<close-configuration/>" +
"</rpc>" +
NetconfConstants.DEVICE_PROMPT;
lastRpcReply = getRpcReply(rpc);
setLastRpcReply(getRpcReply(rpc));
}

/**
Expand All @@ -953,6 +953,15 @@ public String getLastRPCReply() {
return this.lastRpcReply;
}

/**
* Returns the last RPC reply sent by Netconf server.
*
* @return Last RPC reply, as a RpcReply object.
*/
public RpcReply getLastRpcReplyObject() {
return lastRpcReplyObject;
}

/**
* Adds an Attribute to the set of RPC attributes used in the RPC XML envelope. Resets the rpcAttributes value
* to null for generation on the next request.
Expand Down
Loading