Use the NETCONF Java Toolkit to Parse an RPC Reply
After submitting an operational or configuration request to the NETCONF server, the server responds with an RPC reply. You can use several approaches to parse the RPC reply in order to extract the desired information.
XML rpc_reply = device.executeRPC(“get-chassis-inventory”);
There are two approaches to parse an XML reply within the context of the NETCONF Java toolkit:
Get the
org.w3c.dom.Document
object and use the native parsing methods available in the standard Java class libraries for aDocument
object.Use the
findValue(List list)
andfindNodes(List list)
methods available in thenet.juniper.netconf.XML
class on the XML object.
For the first approach, call the getOwnerDocument()
method on the reply object to return the Document
object.
Document doc = rpc_reply.getOwnerDocument();
You can then use methods in the standard Java libraries on the
resulting Document
object. This method
is useful for the flexibility and options available in terms of the
standard Java library methods.
For the second approach, the net.juniper.netconf.XML
class contains the findValue(List list)
and findNodes(List list)
methods, which you can use to parse the XML object.
You must include the “import java.util.*;” statement in
your program code to use the functionality of the List
interface or to create an Arrays
object
as shown in the corresponding examples.
Study the following RPC reply for the get-interface-information
operational request:
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/11.3I0/junos"> <interface-information> <physical-interface> <name>ge-0/0/0</name> <admin-status>up</admin-status> <oper-status>up</oper-status> /* hierarchy truncated for brevity */ </physical-interface> <physical-interface> <name>ge-0/0/1</name> /* hierarchy truncated for brevity */ </physical-interface> </interface-information> </rpc-reply>
Parsing an RPC Reply Using findValue()
You can use the findValue()
method to determine the value of a given element at any level of
the hierarchy. In the example RPC reply for get-interface-information
, suppose you want to determine the value of the <admin-status>
element of the physical interface ge-0/0/0. Being aware of the format
of the RPC reply, you can extract this information using the following
code:
XML rpc_reply = device.executeRPC(“get-interface-information”); List<String> list = Arrays.asList("interface-information","physical-interface", "name~ge-0/0/0", “admin-status"); String admin_status = rpc_reply.findValue(list); System.out.println(admin_status);
Note that the interface name uses a tilde (~) character to identify the particular element. Execution of this code prints “up” to standard output.
Parsing an RPC Reply Using findNodes()
You can use the findNodes()
method to obtain the list of all nodes under a given hierarchy as org.w3c.dom.Node
objects. The following code snippet
obtains a list of all <physical-interface>
nodes under the <interface-information>
element in the hierarchy:
XML rpc_reply = device.executeRPC("get-interface-information"); List<String> list = Arrays.asList("interface-information","physical-interface"); List physical_interfaces_list = rpc_reply.findNodes(list);
However, you might want to extract a specific node. The following code returns the hierarchy for the ge-0/0/1 interface only:
XML rpc_reply = device.executeRPC("get-interface-information"); List<String> list = Arrays.asList("interface-information","physical-interface", "name~ge-0/0/1"); List physical_interfaces_list = rpc_reply.findNodes(list); Node ge001_node = (Node)physical_interfaces_list.get(0);
Example: Parsing an RPC Reply Using findNodes() (Detailed)
The following example takes this approach a step further
and parses through the child nodes to extract and print the content
for just the <name>
elements. This sample
code focuses on the portion of the program that parses the RPC reply
and does not represent a complete program.
import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /* code omitted for brevity */ XML rpc_reply = device.executeRPC("get-interface-information"); // Obtain a list of list of ‘org.w3c.dom.Node’ objects List<String> list = Arrays.asList("interface-information","physical-interface"); List physical_interfaces_list = rpc_reply.findNodes(list); // Print the value for each of the name elements: Iterator iter = physical_interfaces_list.iterator(); while(iter.hasNext()) { Node node = (Node)iter.next(); NodeList child_nodes_of_phy_interface = node.getChildNodes(); // child_nodes_of_phy_interface contains nodes like <name> and <admin-status> // Get each <name> node from the NodeList for (int i = 0; i < child_nodes_of_phy_interface.getLength(); i++) { Node child_node = child_nodes_of_phy_interface.item(i); if (child_node.getNodeType() != Node.ELEMENT_NODE){ continue; } if (child_node.getNodeName().equals("name")) { // Print the text value of the <name> node System.out.println(child_node.getTextContent()); } break; } }