1515import lombok .NonNull ;
1616import lombok .extern .slf4j .Slf4j ;
1717import net .juniper .netconf .element .Hello ;
18+ import net .juniper .netconf .element .RpcReply ;
1819import org .w3c .dom .Document ;
1920import org .w3c .dom .Element ;
2021import org .xml .sax .InputSource ;
@@ -68,6 +69,7 @@ public class NetconfSession {
6869 private final OutputStream stdOutStreamToDevice ;
6970
7071 private String lastRpcReply ;
72+ private RpcReply lastRpcReplyObject ;
7173 private final DocumentBuilder builder ;
7274 private final int commandTimeout ;
7375
@@ -117,14 +119,7 @@ private XML convertToXML(String xml) throws SAXException, IOException {
117119 }
118120
119121 private void sendHello (String hello ) throws IOException {
120- String reply = getRpcReply (hello );
121- serverCapability = reply ;
122- lastRpcReply = reply ;
123- try {
124- serverHello = Hello .from (reply );
125- } catch (final ParserConfigurationException | SAXException | XPathExpressionException e ) {
126- throw new NetconfException ("Invalid <hello> message from server: " + reply , e );
127- }
122+ setHelloReply (getRpcReply (hello ));
128123 }
129124
130125 @ VisibleForTesting
@@ -228,11 +223,30 @@ public void loadXMLConfiguration(String configuration, String loadType) throws I
228223 "</edit-config>" +
229224 "</rpc>" +
230225 NetconfConstants .DEVICE_PROMPT ;
231- lastRpcReply = getRpcReply (rpc );
226+ setLastRpcReply ( getRpcReply (rpc ) );
232227 if (hasError () || !isOK ())
233228 throw new LoadException ("Load operation returned error." );
234229 }
235230
231+ private void setHelloReply (final String reply ) throws IOException {
232+ this .serverCapability = reply ;
233+ this .lastRpcReply = reply ;
234+ try {
235+ this .serverHello = Hello .from (reply );
236+ } catch (final ParserConfigurationException | SAXException | XPathExpressionException e ) {
237+ throw new NetconfException ("Invalid <hello> message from server: " + reply , e );
238+ }
239+ }
240+
241+ private void setLastRpcReply (final String reply ) throws IOException {
242+ this .lastRpcReply = reply ;
243+ try {
244+ this .lastRpcReplyObject = RpcReply .from (reply );
245+ } catch (final ParserConfigurationException | SAXException | XPathExpressionException e ) {
246+ throw new NetconfException ("Invalid <rpc-reply> message from server: " + lastRpcReply , e );
247+ }
248+ }
249+
236250 /**
237251 * Loads the candidate configuration, Configuration should be in text/tree
238252 * format.
@@ -265,7 +279,7 @@ public void loadTextConfiguration(String configuration, String loadType) throws
265279 "</edit-config>" +
266280 "</rpc>" +
267281 NetconfConstants .DEVICE_PROMPT ;
268- lastRpcReply = getRpcReply (rpc );
282+ setLastRpcReply ( getRpcReply (rpc ) );
269283 if (hasError () || !isOK ())
270284 throw new LoadException ("Load operation returned error" );
271285 }
@@ -283,7 +297,7 @@ private String getConfig(String configTree) throws IOException {
283297 "</get-config>" +
284298 "</rpc>" +
285299 NetconfConstants .DEVICE_PROMPT ;
286- lastRpcReply = getRpcReply (rpc );
300+ setLastRpcReply ( getRpcReply (rpc ) );
287301 return lastRpcReply ;
288302 }
289303
@@ -294,7 +308,7 @@ public XML getRunningConfigAndState(String filter) throws IOException, SAXExcept
294308 "</get>" +
295309 "</rpc>" +
296310 NetconfConstants .DEVICE_PROMPT ;
297- lastRpcReply = getRpcReply (rpc );
311+ setLastRpcReply ( getRpcReply (rpc ) );
298312 return convertToXML (lastRpcReply );
299313 }
300314
@@ -310,7 +324,7 @@ private String getConfig(String target, String configTree)
310324 "</get-config>" +
311325 "</rpc>" +
312326 NetconfConstants .DEVICE_PROMPT ;
313- lastRpcReply = getRpcReply (rpc );
327+ setLastRpcReply ( getRpcReply (rpc ) );
314328 return lastRpcReply ;
315329 }
316330
@@ -348,7 +362,7 @@ public Hello getServerHello() {
348362 */
349363 public XML executeRPC (String rpcContent ) throws SAXException , IOException {
350364 String rpcReply = getRpcReply (fixupRpc (rpcContent ));
351- lastRpcReply = rpcReply ;
365+ setLastRpcReply ( rpcReply ) ;
352366 return convertToXML (rpcReply );
353367 }
354368
@@ -481,7 +495,7 @@ public void close() throws IOException {
481495 "<close-session/>" +
482496 "</rpc>" +
483497 NetconfConstants .DEVICE_PROMPT ;
484- lastRpcReply = getRpcReply (rpc );
498+ setLastRpcReply ( getRpcReply (rpc ) );
485499 netconfChannel .disconnect ();
486500 }
487501
@@ -493,18 +507,7 @@ public void close() throws IOException {
493507 * @throws java.io.IOException If there are issues communicating with the netconf server.
494508 */
495509 public boolean hasError () throws SAXException , IOException {
496- if (lastRpcReply == null || !(lastRpcReply .contains ("<rpc-error>" )))
497- return false ;
498- String errorSeverity = parseForErrors (lastRpcReply );
499- return errorSeverity != null && errorSeverity .equals ("error" );
500- }
501-
502- private String parseForErrors (String inputXmlReply ) throws IOException , SAXException {
503- XML xmlReply = convertToXML (lastRpcReply );
504- List <String > tagList = new ArrayList <>();
505- tagList .add ("rpc-error" );
506- tagList .add ("error-severity" );
507- return xmlReply .findValue (tagList );
510+ return lastRpcReplyObject .hasErrors ();
508511 }
509512
510513 /**
@@ -515,10 +518,7 @@ private String parseForErrors(String inputXmlReply) throws IOException, SAXExcep
515518 * @throws java.io.IOException If there are issues communicating with the netconf server.
516519 */
517520 public boolean hasWarning () throws SAXException , IOException {
518- if (lastRpcReply == null || !(lastRpcReply .contains ("<rpc-error>" )))
519- return false ;
520- String errorSeverity = parseForErrors (lastRpcReply );
521- return errorSeverity != null && errorSeverity .equals ("warning" );
521+ return lastRpcReplyObject .hasWarnings ();
522522 }
523523
524524 /**
@@ -528,7 +528,7 @@ public boolean hasWarning() throws SAXException, IOException {
528528 * @return true if <ok/> tag is found in last RPC reply.
529529 */
530530 public boolean isOK () {
531- return lastRpcReply != null && lastRpcReply . contains ( "<ok/>" );
531+ return lastRpcReplyObject . isOk ( );
532532 }
533533
534534 /**
@@ -547,7 +547,7 @@ public boolean lockConfig() throws IOException, SAXException {
547547 "</lock>" +
548548 "</rpc>" +
549549 NetconfConstants .DEVICE_PROMPT ;
550- lastRpcReply = getRpcReply (rpc );
550+ setLastRpcReply ( getRpcReply (rpc ) );
551551 return !hasError () && isOK ();
552552 }
553553
@@ -567,7 +567,7 @@ public boolean unlockConfig() throws IOException, SAXException {
567567 "</unlock>" +
568568 "</rpc>" +
569569 NetconfConstants .DEVICE_PROMPT ;
570- lastRpcReply = getRpcReply (rpc );
570+ setLastRpcReply ( getRpcReply (rpc ) );
571571 return !hasError () && isOK ();
572572 }
573573
@@ -591,7 +591,7 @@ public void loadSetConfiguration(String configuration) throws IOException, SAXEx
591591 "</configuration-set>" +
592592 "</load-configuration>" +
593593 "</rpc>" ;
594- lastRpcReply = getRpcReply (rpc );
594+ setLastRpcReply ( getRpcReply (rpc ) );
595595 if (hasError () || !isOK ())
596596 throw new LoadException ("Load operation returned error" );
597597 }
@@ -725,7 +725,7 @@ public void commit() throws IOException, SAXException {
725725 "<commit/>" +
726726 "</rpc>" +
727727 NetconfConstants .DEVICE_PROMPT ;
728- lastRpcReply = getRpcReply (rpc );
728+ setLastRpcReply ( getRpcReply (rpc ) );
729729 if (hasError () || !isOK ())
730730 throw new CommitException ("Commit operation returned error." );
731731 }
@@ -747,7 +747,7 @@ public void commitConfirm(long seconds) throws IOException, SAXException {
747747 "</commit>" +
748748 "</rpc>" +
749749 NetconfConstants .DEVICE_PROMPT ;
750- lastRpcReply = getRpcReply (rpc );
750+ setLastRpcReply ( getRpcReply (rpc ) );
751751 if (hasError () || !isOK ())
752752 throw new CommitException ("Commit operation returned " +
753753 "error." );
@@ -767,7 +767,7 @@ public void commitFull() throws CommitException, IOException, SAXException {
767767 "</commit-configuration>" +
768768 "</rpc>" +
769769 NetconfConstants .DEVICE_PROMPT ;
770- lastRpcReply = getRpcReply (rpc );
770+ setLastRpcReply ( getRpcReply (rpc ) );
771771 if (hasError () || !isOK ())
772772 throw new CommitException ("Commit operation returned error." );
773773 }
@@ -842,7 +842,7 @@ public boolean validate() throws IOException, SAXException {
842842 "</validate>" +
843843 "</rpc>" +
844844 NetconfConstants .DEVICE_PROMPT ;
845- lastRpcReply = getRpcReply (rpc );
845+ setLastRpcReply ( getRpcReply (rpc ) );
846846 return !hasError () && isOK ();
847847 }
848848
@@ -878,7 +878,7 @@ public String runCliCommand(String command) throws IOException, SAXException {
878878 "</rpc>" +
879879 NetconfConstants .DEVICE_PROMPT ;
880880 String rpcReply = getRpcReply (rpc );
881- lastRpcReply = rpcReply ;
881+ setLastRpcReply ( rpcReply ) ;
882882 XML xmlReply = convertToXML (rpcReply );
883883 List <String > tags = new ArrayList <>();
884884 tags .add ("output" );
@@ -927,7 +927,7 @@ public void openConfiguration(String mode) throws IOException {
927927 rpc .append ("</open-configuration>" );
928928 rpc .append ("</rpc>" );
929929 rpc .append (NetconfConstants .DEVICE_PROMPT );
930- lastRpcReply = getRpcReply (rpc .toString ());
930+ setLastRpcReply ( getRpcReply (rpc .toString () ));
931931 }
932932
933933 /**
@@ -941,7 +941,7 @@ public void closeConfiguration() throws IOException {
941941 "<close-configuration/>" +
942942 "</rpc>" +
943943 NetconfConstants .DEVICE_PROMPT ;
944- lastRpcReply = getRpcReply (rpc );
944+ setLastRpcReply ( getRpcReply (rpc ) );
945945 }
946946
947947 /**
@@ -953,6 +953,15 @@ public String getLastRPCReply() {
953953 return this .lastRpcReply ;
954954 }
955955
956+ /**
957+ * Returns the last RPC reply sent by Netconf server.
958+ *
959+ * @return Last RPC reply, as a RpcReply object.
960+ */
961+ public RpcReply getLastRpcReplyObject () {
962+ return lastRpcReplyObject ;
963+ }
964+
956965 /**
957966 * Adds an Attribute to the set of RPC attributes used in the RPC XML envelope. Resets the rpcAttributes value
958967 * to null for generation on the next request.
0 commit comments