Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.provider.netconf.flow.impl; |
| 17 | |
| 18 | import static org.onlab.util.Tools.delay; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import com.tailf.jnc.Capabilities; |
| 26 | import com.tailf.jnc.JNCException; |
| 27 | import com.tailf.jnc.SSHConnection; |
| 28 | import com.tailf.jnc.SSHSession; |
| 29 | |
| 30 | /** |
| 31 | * This is to carry necessary information to connect and execute NETCONF |
| 32 | * operations. |
| 33 | */ |
| 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(); |
| 43 | |
| 44 | /** |
| 45 | * This will send a Xml message to the device. |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 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 |
Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame] | 51 | */ |
| 52 | protected void sendXmlMessage(String xmlMsg, String username, |
| 53 | String password, String deviceIp, |
| 54 | Integer devicePort) { |
| 55 | SSHSession ssh = null; |
| 56 | try { |
| 57 | SSHConnection sshConnection = getConnection(username, password, |
| 58 | deviceIp, devicePort); |
| 59 | ssh = new SSHSession(sshConnection); |
| 60 | executeMessage(ssh, INPUT_HELLO_XML_MSG); |
| 61 | /* |
| 62 | * execute acl message |
| 63 | */ |
| 64 | executeMessage(ssh, xmlMsg); |
| 65 | |
| 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: ", |
| 70 | e); |
| 71 | } catch (Exception e) { |
| 72 | log.error("Unable to send Hello Message to the device: ", e); |
| 73 | } finally { |
| 74 | log.debug("Closing the session after successful execution"); |
Satish K | 7c3b6ac | 2015-11-28 14:20:00 +0530 | [diff] [blame] | 75 | if (ssh != null) { |
| 76 | ssh.close(); |
| 77 | } |
Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | private void executeMessage(SSHSession ssh, String xmlMsg) |
| 82 | throws IOException, JNCException { |
| 83 | String helloRequestXML = xmlMsg.trim(); |
| 84 | |
| 85 | log.debug("Sending Hello"); |
| 86 | ssh.print(helloRequestXML); |
| 87 | ssh.flush(); |
| 88 | String xmlResponse = null; |
| 89 | int i = CONNECTION_CHECK_INTERVAL; |
| 90 | while (!ssh.ready() && i > 0) { |
| 91 | delay(EVENTINTERVAL); |
| 92 | i--; |
| 93 | } |
| 94 | |
| 95 | if (ssh.ready()) { |
| 96 | StringBuffer readOne = ssh.readOne(); |
| 97 | if (readOne == null) { |
| 98 | log.error("The Hello Contains No Capabilites"); |
| 99 | throw new JNCException( |
| 100 | JNCException.SESSION_ERROR, |
| 101 | "server does not support NETCONF base capability: " |
| 102 | + Capabilities.NETCONF_BASE_CAPABILITY); |
| 103 | } else { |
| 104 | xmlResponse = readOne.toString().trim(); |
| 105 | |
| 106 | log.debug("Reading Capabilities: " |
| 107 | + ssh.getSSHConnection().getGanymedConnection() |
| 108 | .getHostname()); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * To establish SSH Connection. |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 115 | * |
| 116 | * @param username user name |
| 117 | * @param password pass word |
| 118 | * @param sshHost host |
| 119 | * @param sshPort port |
| 120 | * @return new SSH connection |
| 121 | * @throws IOException if connection fails |
| 122 | * @throws JNCException if connection causes an error |
Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame] | 123 | */ |
| 124 | public SSHConnection getConnection(String username, String password, |
| 125 | String sshHost, Integer sshPort) |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 126 | throws IOException, JNCException { |
Samir Anand | 01c77c4 | 2015-08-06 14:50:29 +0530 | [diff] [blame] | 127 | SSHConnection sshConnection; |
| 128 | try { |
| 129 | sshConnection = new SSHConnection(sshHost, sshPort); |
| 130 | sshConnection.authenticateWithPassword(username, password); |
| 131 | } catch (IOException e) { |
| 132 | log.error("Unable to create a connection to the device: "); |
| 133 | throw e; |
| 134 | } catch (JNCException e) { |
| 135 | log.error("Failed to connect to the device: "); |
| 136 | throw e; |
| 137 | } |
| 138 | return sshConnection; |
| 139 | } |
| 140 | |
| 141 | } |