net.juniper.netconf
Class Device

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

public class Device
extends java.lang.Object

A Device is used to define a Netconf server.

Typically, one

  1. creates a Device object.
  2. perform netconf operations on the Device object.
  3. If needed, call the method createNetconfSession() to create another NetconfSession.
  4. Finally, one must close the Device and release resources with the close() method.


Constructor Summary
Device()
          Prepares a new Device object, with default client capabilities and default port 830, which can then be used to perform netconf operations.
Device(java.lang.String hostName, java.lang.String userName, java.lang.String password, java.lang.String pemKeyFile)
          Prepares a new Device object, with default client capabilities and default port 830, which can then be used to perform netconf operations.
Device(java.lang.String hostName, java.lang.String userName, java.lang.String password, java.lang.String pemKeyFile, java.util.ArrayList capabilities)
          Prepares a new Device object, with user-defined client capabilities and default port 830 which can then be used to perform netconf operations.
Device(java.lang.String hostName, java.lang.String userName, java.lang.String password, java.lang.String pemKeyFile, int port)
          Prepares a new Device object, with default client capabilities and user-defined port which can then be used to perform netconf operations.
Device(java.lang.String hostName, java.lang.String userName, java.lang.String password, java.lang.String pemKeyFile, int port, java.util.ArrayList capabilities)
          Prepares a new Device object, with user-defined client capabilities and user-defined port which can then be used to perform netconf operations.
 
Method Summary
 void close()
          Close the connection to the Netconf server.
 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 format.
 void connect()
          Connect to the Device, and establish a default NETCONF session.
 NetconfSession createNetconfSession()
          Create a new Netconf session.
 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.util.ArrayList getDefaultClientCapabilities()
          Get the client capabilities that are advertised to the Netconf server by default.
 java.lang.String gethostName()
          Get hostname of the Netconf server.
 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 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.
 java.lang.String runCliCommand(java.lang.String command)
          Run a cli command, and get the corresponding output.
 java.io.BufferedReader runCliCommandRunning(java.lang.String command)
          Run a cli command.
 java.lang.String runShellCommand(java.lang.String command)
          Execute a command in shell mode.
 java.io.BufferedReader runShellCommandRunning(java.lang.String command)
          Execute a command in shell mode.
 void setCapabilities(java.util.ArrayList capabilities)
          Set the client capabilities to be advertised to the Netconf server.
 void setHostname(java.lang.String hostName)
          Set the hostname of the Netconf server.
 void setPassword(java.lang.String password)
          Set the password of the Netconf server.
 void setPemKeyFile(java.lang.String pemKeyFile)
          Set path of the RSA/DSA private key.
 void setPort(int port)
          Set the port number to establish Netconf session over SSH-2.
 void setTimeOut(int timeout)
          Set the timeout value for connecting to the Device.
 void setUserName(java.lang.String userName)
          Set the username of the Netconf server.
 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

Device

public Device()
       throws javax.xml.parsers.ParserConfigurationException
Prepares a new Device object, with default client capabilities and default port 830, which can then be used to perform netconf operations.

Throws:
javax.xml.parsers.ParserConfigurationException

Device

public Device(java.lang.String hostName,
              java.lang.String userName,
              java.lang.String password,
              java.lang.String pemKeyFile)
       throws NetconfException,
              javax.xml.parsers.ParserConfigurationException
Prepares a new Device object, with default client capabilities and default port 830, which can then be used to perform netconf operations.

Parameters:
hostName - the hostname of the Netconf server.
userName - the login username of the Netconf server.
password - the login password of the Netconf server.
pemKeyFile - path of the file containing RSA/DSA private key, in PEM format. For user-password based authentication, let this be null.
Throws:
NetconfException
javax.xml.parsers.ParserConfigurationException

Device

public Device(java.lang.String hostName,
              java.lang.String userName,
              java.lang.String password,
              java.lang.String pemKeyFile,
              int port)
       throws NetconfException,
              javax.xml.parsers.ParserConfigurationException
Prepares a new Device object, with default client capabilities and user-defined port which can then be used to perform netconf operations.

Parameters:
hostName - the hostname of the Netconf server.
userName - the login username of the Netconf server.
password - the login password of the Netconf server.
pemKeyFile - path of the file containing RSA/DSA private key, in PEM format. For user-password based authentication, let this be null.
port - port number to establish Netconf session over SSH-2.
Throws:
NetconfException
javax.xml.parsers.ParserConfigurationException

Device

public Device(java.lang.String hostName,
              java.lang.String userName,
              java.lang.String password,
              java.lang.String pemKeyFile,
              java.util.ArrayList capabilities)
       throws NetconfException,
              javax.xml.parsers.ParserConfigurationException
Prepares a new Device object, with user-defined client capabilities and default port 830 which can then be used to perform netconf operations.

Parameters:
hostName - the hostname of the Netconf server.
userName - the login username of the Netconf server.
password - the login password of the Netconf server.
pemKeyFile - path of the file containing RSA/DSA private key, in PEM format. For user-password based authentication, let this be null.
capabilities - the client capabilities to be advertised to Netconf server.
Throws:
NetconfException
javax.xml.parsers.ParserConfigurationException

Device

public Device(java.lang.String hostName,
              java.lang.String userName,
              java.lang.String password,
              java.lang.String pemKeyFile,
              int port,
              java.util.ArrayList capabilities)
       throws NetconfException,
              javax.xml.parsers.ParserConfigurationException
Prepares a new Device object, with user-defined client capabilities and user-defined port which can then be used to perform netconf operations.

Parameters:
hostName - the hostname of the Netconf server.
userName - the login username of the Netconf server.
password - the login password of the Netconf server.
pemKeyFile - path of the file containing RSA/DSA private key, in PEM format. For user-password based authentication, let this be null.
port - port number to establish Netconf session over SSH-2.
capabilities - the client capabilities to be advertised to Netconf server.
Throws:
NetconfException
javax.xml.parsers.ParserConfigurationException
Method Detail

connect

public void connect()
             throws NetconfException
Connect to the Device, and establish a default NETCONF session.

Throws:
NetconfException

setTimeOut

public void setTimeOut(int timeout)
                throws NetconfException
Set the timeout value for connecting to the Device.

Parameters:
timeout - timeout in milliseconds.
Throws:
NetconfException

setHostname

public void setHostname(java.lang.String hostName)
                 throws NetconfException
Set the hostname of the Netconf server.

Parameters:
hostName - hostname of the Netconf server, to be set.
Throws:
NetconfException

setUserName

public void setUserName(java.lang.String userName)
                 throws NetconfException
Set the username of the Netconf server.

Parameters:
userName - username of the Netconf server, to be set.
Throws:
NetconfException

setPassword

public void setPassword(java.lang.String password)
                 throws NetconfException
Set the password of the Netconf server.

Parameters:
password - password of the Netconf server, to be set.
Throws:
NetconfException

setPemKeyFile

public void setPemKeyFile(java.lang.String pemKeyFile)
                   throws NetconfException
Set path of the RSA/DSA private key.

Parameters:
pemKeyFile - Path of the file containing RSA/DSA private key.
Throws:
NetconfException

setCapabilities

public void setCapabilities(java.util.ArrayList capabilities)
                     throws NetconfException
Set the client capabilities to be advertised to the Netconf server.

Parameters:
capabilities - Client capabilities to be advertised to the Netconf server.
Throws:
NetconfException

setPort

public void setPort(int port)
             throws NetconfException
Set the port number to establish Netconf session over SSH-2.

Parameters:
port - Port number.
Throws:
NetconfException

gethostName

public java.lang.String gethostName()
Get hostname of the Netconf server.

Returns:
Hostname of the device.

createNetconfSession

public NetconfSession createNetconfSession()
                                    throws NetconfException
Create a new Netconf session.

Returns:
NetconfSession
Throws:
NetconfException

reboot

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

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

close

public void close()
Close the connection to the Netconf server. All associated Netconf sessions will be closed, too. Can be called at any time. Don't forget to call this once you don't need the device anymore.


runShellCommand

public java.lang.String runShellCommand(java.lang.String command)
                                 throws java.io.IOException
Execute a command in shell mode.

Parameters:
command - The command to be executed in shell mode.
Returns:
Result of the command execution, as a String.
Throws:
java.io.IOException

runShellCommandRunning

public java.io.BufferedReader runShellCommandRunning(java.lang.String command)
                                              throws java.io.IOException
Execute a command in shell mode.

Parameters:
command - The command to be executed in shell mode.
Returns:
Result of the command execution, 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

getDefaultClientCapabilities

public java.util.ArrayList getDefaultClientCapabilities()
Get the client capabilities that are advertised to the Netconf server by default.

Returns:
Arraylist of default client capabilities.

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 org.xml.sax.SAXException,
                                                java.io.IOException
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 rpc execution completes.
Throws:
org.xml.sax.SAXException
java.io.IOException

executeRPCRunning

public java.io.BufferedReader executeRPCRunning(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 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:
org.xml.sax.SAXException
java.io.IOException

executeRPCRunning

public java.io.BufferedReader executeRPCRunning(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 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

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

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

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 format.

Parameters:
configFile - Path name of file containing configuration,in text/xml 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.
loadType - You can choose "merge" or "replace" as the loadType.
Throws:
LoadException
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

runCliCommand

public java.lang.String runCliCommand(java.lang.String command)
                               throws java.io.IOException,
                                      org.xml.sax.SAXException
Run a cli command, and get the corresponding output. 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.
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