net.juniper.netconf
Class NetconfSession

java.lang.Object
  extended bynet.juniper.netconf.NetconfSession

public class NetconfSession
extends java.lang.Object

A NetconfSession object is used to call the Netconf driver methods. This is derived by creating a Device first, and calling createNetconfSession().

Typically, one

  1. creates a Device object.
  2. calls the createNetconfSession() method to get a NetconfSession object.
  3. perform operations on the NetconfSession object.
  4. finally, one must close the NetconfSession and release resources with the close() method.


Constructor Summary
protected NetconfSession(Session netconfSession, java.lang.String hello, javax.xml.parsers.DocumentBuilder builder)
           
 
Method Summary
 void close()
          Close the Netconf session.
 void closeConfiguration()
          This method should be called to close a private session, in case its started.
 void commit()
          Commit the candidate configuration.
 void commitConfirm(long seconds)
          Commit the candidate configuration, temporarily.
 void commitThisConfiguration(java.lang.String configFile, java.lang.String loadType)
          Loads and commits the candidate configuration, Configuration can be in text/xml/set format.
 XML executeRPC(org.w3c.dom.Document rpcDoc)
          Send an RPC(as Document object) over the Netconf session and get the response as an XML object.
 XML executeRPC(java.lang.String rpcContent)
          Send an RPC(as String object) over the default Netconf session and get the response as an XML object.
 XML executeRPC(XML rpc)
          Send an RPC(as XML object) over the Netconf session and get the response as an XML object.
 java.io.BufferedReader executeRPCRunning(org.w3c.dom.Document rpcDoc)
          Send an RPC(as Document object) over the Netconf session and get the response as a BufferedReader.
 java.io.BufferedReader executeRPCRunning(java.lang.String rpcContent)
          Send an RPC(as String object) over the default Netconf session and get the response as a BufferedReader.
 java.io.BufferedReader executeRPCRunning(XML rpc)
          Send an RPC(as XML object) over the Netconf session and get the response as a BufferedReader.
 XML getCandidateConfig()
          Retrieve the whole candidate configuration.
 XML getCandidateConfig(java.lang.String configTree)
          Retrieve the candidate configuration, or part of the configuration.
 java.lang.String getLastRPCReply()
          Returns the last RPC reply sent by Netconf server.
 XML getRunningConfig()
          Retrieve the whole running configuration.
 XML getRunningConfig(java.lang.String configTree)
          Retrieve the running configuration, or part of the configuration.
 java.lang.String getServerCapability()
          Get capability of the Netconf server.
 java.lang.String getSessionId()
          Get the session ID of the Netconf session.
 boolean hasError()
          Check if the last RPC reply returned from Netconf server has any error.
 boolean hasWarning()
          Check if the last RPC reply returned from Netconf server has any warning.
 boolean isOK()
          Check if the last RPC reply returned from Netconf server, contains <ok/> tag.
 void loadSetConfiguration(java.lang.String configuration)
          Loads the candidate configuration, Configuration should be in set format.
 void loadSetFile(java.lang.String configFile)
          Loads the candidate configuration from file, configuration should be in set format.
 void loadTextConfiguration(java.lang.String configuration, java.lang.String loadType)
          Loads the candidate configuration, Configuration should be in text/tree format.
 void loadTextFile(java.lang.String configFile, java.lang.String loadType)
          Loads the candidate configuration from file, configuration should be in text/tree format.
 void loadXMLConfiguration(java.lang.String configuration, java.lang.String loadType)
          Loads the candidate configuration, Configuration should be in XML format.
 void loadXMLFile(java.lang.String configFile, java.lang.String loadType)
          Loads the candidate configuration from file, configuration should be in XML format.
 boolean lockConfig()
          Locks the candidate configuration.
 void openConfiguration(java.lang.String mode)
          This method should be called for load operations to happen in 'private' mode.
 java.lang.String reboot()
          Reboot the device corresponding to the Netconf Session.
 java.lang.String runCliCommand(java.lang.String command)
          Run a cli command.
 java.io.BufferedReader runCliCommandRunning(java.lang.String command)
          Run a cli command.
 boolean unlockConfig()
          Unlocks the candidate configuration.
 boolean validate()
          Validate the candidate configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetconfSession

protected NetconfSession(Session netconfSession,
                         java.lang.String hello,
                         javax.xml.parsers.DocumentBuilder builder)
                  throws NetconfException,
                         java.io.IOException
Method Detail

getServerCapability

public java.lang.String getServerCapability()
Get capability of the Netconf server.

Returns:
server capability

executeRPC

public XML executeRPC(java.lang.String rpcContent)
               throws org.xml.sax.SAXException,
                      java.io.IOException
Send an RPC(as String object) over the default Netconf session and get the response as an XML object.

Parameters:
rpcContent - RPC content to be sent. For example, to send an rpc <rpc><get-chassis-inventory/></rpc>, the String to be passed can be "<get-chassis-inventory/>" OR "get-chassis-inventory" OR "<rpc><get-chassis-inventory/></rpc>"
Returns:
RPC reply sent by Netconf server
Throws:
org.xml.sax.SAXException
java.io.IOException

executeRPC

public XML executeRPC(XML rpc)
               throws org.xml.sax.SAXException,
                      java.io.IOException
Send an RPC(as XML object) over the Netconf session and get the response as an XML object.

Parameters:
rpc - RPC to be sent. Use the XMLBuilder to create RPC as an XML object.
Returns:
RPC reply sent by Netconf server
Throws:
org.xml.sax.SAXException
java.io.IOException

executeRPC

public XML executeRPC(org.w3c.dom.Document rpcDoc)
               throws org.xml.sax.SAXException,
                      java.io.IOException
Send an RPC(as Document object) over the Netconf session and get the response as an XML object.

Parameters:
rpcDoc - RPC content to be sent, as a org.w3c.dom.Document object.
Returns:
RPC reply sent by Netconf server
Throws:
org.xml.sax.SAXException
java.io.IOException

executeRPCRunning

public java.io.BufferedReader executeRPCRunning(java.lang.String rpcContent)
                                         throws java.io.IOException,
                                                org.xml.sax.SAXException
Send an RPC(as String object) over the default Netconf session and get the response as a BufferedReader.

Parameters:
rpcContent - RPC content to be sent. For example, to send an rpc <rpc><get-chassis-inventory/></rpc>, the String to be passed can be "<get-chassis-inventory/>" OR "get-chassis-inventory" OR "<rpc><get-chassis-inventory/></rpc>"
Returns:
RPC reply sent by Netconf server as a BufferedReader. This is useful if we want continuous stream of output, rather than wait for whole output till command execution completes.
Throws:
java.io.IOException
org.xml.sax.SAXException

executeRPCRunning

public java.io.BufferedReader executeRPCRunning(XML rpc)
                                         throws java.io.IOException,
                                                org.xml.sax.SAXException
Send an RPC(as XML object) over the Netconf session and get the response as a BufferedReader.

Parameters:
rpc - RPC to be sent. Use the XMLBuilder to create RPC as an XML object.
Returns:
RPC reply sent by Netconf server as a BufferedReader. This is useful if we want continuous stream of output, rather than wait for whole output till command execution completes.
Throws:
java.io.IOException
org.xml.sax.SAXException

executeRPCRunning

public java.io.BufferedReader executeRPCRunning(org.w3c.dom.Document rpcDoc)
                                         throws java.io.IOException,
                                                org.xml.sax.SAXException
Send an RPC(as Document object) over the Netconf session and get the response as a BufferedReader.

Parameters:
rpcDoc - RPC content to be sent, as a org.w3c.dom.Document object.
Returns:
RPC reply sent by Netconf server as a BufferedReader. This is useful if we want continuous stream of output, rather than wait for whole output till command execution completes.
Throws:
org.xml.sax.SAXException
java.io.IOException

getSessionId

public java.lang.String getSessionId()
Get the session ID of the Netconf session.

Returns:
Session ID as a string.

close

public void close()
           throws java.io.IOException
Close the Netconf session. You should always call this once you don't need the session anymore.

Throws:
java.io.IOException

hasError

public boolean hasError()
                 throws org.xml.sax.SAXException,
                        java.io.IOException
Check if the last RPC reply returned from Netconf server has any error.

Returns:
true if any errors are found in last RPC reply.
Throws:
org.xml.sax.SAXException
java.io.IOException

hasWarning

public boolean hasWarning()
                   throws org.xml.sax.SAXException,
                          java.io.IOException
Check if the last RPC reply returned from Netconf server has any warning.

Returns:
true if any errors are found in last RPC reply.
Throws:
org.xml.sax.SAXException
java.io.IOException

isOK

public boolean isOK()
Check if the last RPC reply returned from Netconf server, contains <ok/> tag.

Returns:
true if <ok/> tag is found in last RPC reply.

lockConfig

public boolean lockConfig()
                   throws java.io.IOException,
                          org.xml.sax.SAXException
Locks the candidate configuration.

Returns:
true if successful.
Throws:
java.io.IOException
org.xml.sax.SAXException

unlockConfig

public boolean unlockConfig()
                     throws java.io.IOException,
                            org.xml.sax.SAXException
Unlocks the candidate configuration.

Returns:
true if successful.
Throws:
java.io.IOException
org.xml.sax.SAXException

loadXMLConfiguration

public void loadXMLConfiguration(java.lang.String configuration,
                                 java.lang.String loadType)
                          throws LoadException,
                                 java.io.IOException,
                                 org.xml.sax.SAXException
Loads the candidate configuration, Configuration should be in XML format.

Parameters:
configuration - Configuration,in XML format, to be loaded. For example, "<configuration><system><services><ftp/>< services/></system></configuration/>" will load 'ftp' under the 'systems services' hierarchy.
loadType - You can choose "merge" or "replace" as the loadType.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

loadTextConfiguration

public void loadTextConfiguration(java.lang.String configuration,
                                  java.lang.String loadType)
                           throws LoadException,
                                  java.io.IOException,
                                  org.xml.sax.SAXException
Loads the candidate configuration, Configuration should be in text/tree format.

Parameters:
configuration - Configuration,in text/tree format, to be loaded. For example, " system { services { ftp; } }" will load 'ftp' under the 'systems services' hierarchy.
loadType - You can choose "merge" or "replace" as the loadType.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

loadSetConfiguration

public void loadSetConfiguration(java.lang.String configuration)
                          throws LoadException,
                                 java.io.IOException,
                                 org.xml.sax.SAXException
Loads the candidate configuration, Configuration should be in set format. NOTE: This method is applicable only for JUNOS release 11.4 and above.

Parameters:
configuration - Configuration,in set format, to be loaded. For example, "set system services ftp" will load 'ftp' under the 'systems services' hierarchy. To load multiple set statements, separate them by '\n' character.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

loadXMLFile

public void loadXMLFile(java.lang.String configFile,
                        java.lang.String loadType)
                 throws LoadException,
                        java.io.IOException,
                        org.xml.sax.SAXException
Loads the candidate configuration from file, configuration should be in XML format.

Parameters:
configFile - Path name of file containing configuration,in xml format, to be loaded.
loadType - You can choose "merge" or "replace" as the loadType.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

loadTextFile

public void loadTextFile(java.lang.String configFile,
                         java.lang.String loadType)
                  throws LoadException,
                         java.io.IOException,
                         org.xml.sax.SAXException
Loads the candidate configuration from file, configuration should be in text/tree format.

Parameters:
configFile - Path name of file containing configuration,in xml format, to be loaded.
loadType - You can choose "merge" or "replace" as the loadType.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

loadSetFile

public void loadSetFile(java.lang.String configFile)
                 throws java.io.IOException,
                        LoadException,
                        org.xml.sax.SAXException
Loads the candidate configuration from file, configuration should be in set format. NOTE: This method is applicable only for JUNOS release 11.4 and above.

Parameters:
configFile - Path name of file containing configuration,in set format, to be loaded.
Throws:
LoadException
java.io.IOException
org.xml.sax.SAXException

commitThisConfiguration

public void commitThisConfiguration(java.lang.String configFile,
                                    java.lang.String loadType)
                             throws LoadException,
                                    CommitException,
                                    java.io.IOException,
                                    org.xml.sax.SAXException
Loads and commits the candidate configuration, Configuration can be in text/xml/set format.

Parameters:
configFile - Path name of file containing configuration,in text/xml/set format, to be loaded. For example, " system { services { ftp; } }" will load 'ftp' under the 'systems services' hierarchy. OR "<configuration><system><services><ftp/>< services/></system></configuration/>" will load 'ftp' under the 'systems services' hierarchy. OR "set system services ftp" will load 'ftp' under the 'systems services' hierarchy.
loadType - You can choose "merge" or "replace" as the loadType. NOTE: This parameter's value is redundant in case the file contains configuration in 'set' format.
Throws:
LoadException
CommitException
java.io.IOException
org.xml.sax.SAXException

commit

public void commit()
            throws CommitException,
                   java.io.IOException,
                   org.xml.sax.SAXException
Commit the candidate configuration.

Throws:
CommitException
java.io.IOException
org.xml.sax.SAXException

commitConfirm

public void commitConfirm(long seconds)
                   throws CommitException,
                          java.io.IOException,
                          org.xml.sax.SAXException
Commit the candidate configuration, temporarily. This is equivalent of 'commit confirm'

Parameters:
seconds - Time in seconds, after which the previous active configuration is reverted back to.
Throws:
CommitException
java.io.IOException
org.xml.sax.SAXException

getCandidateConfig

public XML getCandidateConfig(java.lang.String configTree)
                       throws org.xml.sax.SAXException,
                              java.io.IOException
Retrieve the candidate configuration, or part of the configuration.

Parameters:
configTree - configuration hierarchy to be retrieved as the argument. For example, to get the whole configuration, argument should be <configuration></configuration>
Returns:
configuration data as XML object.
Throws:
org.xml.sax.SAXException
java.io.IOException

getRunningConfig

public XML getRunningConfig(java.lang.String configTree)
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Retrieve the running configuration, or part of the configuration.

Parameters:
configTree - configuration hierarchy to be retrieved as the argument. For example, to get the whole configuration, argument should be <configuration></configuration>
Returns:
configuration data as XML object.
Throws:
org.xml.sax.SAXException
java.io.IOException

getCandidateConfig

public XML getCandidateConfig()
                       throws org.xml.sax.SAXException,
                              java.io.IOException
Retrieve the whole candidate configuration.

Returns:
configuration data as XML object.
Throws:
org.xml.sax.SAXException
java.io.IOException

getRunningConfig

public XML getRunningConfig()
                     throws org.xml.sax.SAXException,
                            java.io.IOException
Retrieve the whole running configuration.

Returns:
configuration data as XML object.
Throws:
org.xml.sax.SAXException
java.io.IOException

validate

public boolean validate()
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Validate the candidate configuration.

Returns:
true if validation successful.
Throws:
java.io.IOException
org.xml.sax.SAXException

reboot

public java.lang.String reboot()
                        throws org.xml.sax.SAXException,
                               java.io.IOException
Reboot the device corresponding to the Netconf Session.

Returns:
RPC reply sent by Netconf server.
Throws:
org.xml.sax.SAXException
java.io.IOException

runCliCommand

public java.lang.String runCliCommand(java.lang.String command)
                               throws java.io.IOException,
                                      org.xml.sax.SAXException
Run a cli command. NOTE: The text output is supported for JUNOS 11.4 and later.

Parameters:
command - the cli command to be executed.
Returns:
result of the command, as a String.
Throws:
java.io.IOException
org.xml.sax.SAXException

runCliCommandRunning

public java.io.BufferedReader runCliCommandRunning(java.lang.String command)
                                            throws org.xml.sax.SAXException,
                                                   java.io.IOException
Run a cli command.

Parameters:
command - the cli command to be executed.
Returns:
result of the command, as a BufferedReader. This is useful if we want continuous stream of output, rather than wait for whole output till command execution completes.
Throws:
org.xml.sax.SAXException
java.io.IOException

openConfiguration

public void openConfiguration(java.lang.String mode)
                       throws java.io.IOException
This method should be called for load operations to happen in 'private' mode.

Parameters:
mode - Mode in which to open the configuration. Permissible mode(s): "private"
Throws:
java.io.IOException

closeConfiguration

public void closeConfiguration()
                        throws java.io.IOException
This method should be called to close a private session, in case its started.

Throws:
java.io.IOException

getLastRPCReply

public java.lang.String getLastRPCReply()
Returns the last RPC reply sent by Netconf server.

Returns:
Last RPC reply, as a string.