2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.provider.netconf.flow.impl;
18 import static org.onlab.util.Tools.delay;
19 import static org.slf4j.LoggerFactory.getLogger;
21 import java.io.IOException;
23 import org.slf4j.Logger;
25 import com.tailf.jnc.Capabilities;
26 import com.tailf.jnc.JNCException;
27 import com.tailf.jnc.SSHConnection;
28 import com.tailf.jnc.SSHSession;
31 * This is to carry necessary information to connect and execute NETCONF
34 public class NetconfOperation {
35 private final Logger log = getLogger(NetconfOperation.class);
36 private static final int EVENTINTERVAL = 2000;
37 private static final int CONNECTION_CHECK_INTERVAL = 3;
38 private static final String INPUT_HELLO_XML_MSG = new StringBuilder(
39 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
40 .append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">")
41 .append("<capabilities><capability>urn:ietf:params:netconf:base:1.0</capability>")
42 .append("</capabilities></hello>").toString();
45 * This will send a Xml message to the device.
46 * @param xmlMsg XML to send
47 * @param username user name
48 * @param password pass word
49 * @param deviceIp ip address of the device
50 * @param devicePort port on the device
52 protected void sendXmlMessage(String xmlMsg, String username,
53 String password, String deviceIp,
55 SSHSession ssh = null;
57 SSHConnection sshConnection = getConnection(username, password,
58 deviceIp, devicePort);
59 ssh = new SSHSession(sshConnection);
60 executeMessage(ssh, INPUT_HELLO_XML_MSG);
64 executeMessage(ssh, xmlMsg);
66 } catch (IOException e) {
67 log.error("Unable to send Hello Message to the device: ", e);
68 } catch (JNCException e) {
69 log.error("Authentication fail while sending Hello Message to the device: ",
71 } catch (Exception e) {
72 log.error("Unable to send Hello Message to the device: ", e);
74 log.debug("Closing the session after successful execution");
79 private void executeMessage(SSHSession ssh, String xmlMsg)
80 throws IOException, JNCException {
81 String helloRequestXML = xmlMsg.trim();
83 log.debug("Sending Hello");
84 ssh.print(helloRequestXML);
86 String xmlResponse = null;
87 int i = CONNECTION_CHECK_INTERVAL;
88 while (!ssh.ready() && i > 0) {
94 StringBuffer readOne = ssh.readOne();
95 if (readOne == null) {
96 log.error("The Hello Contains No Capabilites");
97 throw new JNCException(
98 JNCException.SESSION_ERROR,
99 "server does not support NETCONF base capability: "
100 + Capabilities.NETCONF_BASE_CAPABILITY);
102 xmlResponse = readOne.toString().trim();
104 log.debug("Reading Capabilities: "
105 + ssh.getSSHConnection().getGanymedConnection()
112 * To establish SSH Connection.
114 * @param username user name
115 * @param password pass word
116 * @param sshHost host
117 * @param sshPort port
118 * @return new SSH connection
119 * @throws IOException if connection fails
120 * @throws JNCException if connection causes an error
122 public SSHConnection getConnection(String username, String password,
123 String sshHost, Integer sshPort)
124 throws IOException, JNCException {
125 SSHConnection sshConnection;
127 sshConnection = new SSHConnection(sshHost, sshPort);
128 sshConnection.authenticateWithPassword(username, password);
129 } catch (IOException e) {
130 log.error("Unable to create a connection to the device: ");
132 } catch (JNCException e) {
133 log.error("Failed to connect to the device: ");
136 return sshConnection;