Sanjay S | e8dcfee | 2015-04-23 10:07:08 +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.device.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.util.Tools.delay; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.StringReader; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.jdom2.Document; |
| 28 | import org.jdom2.Element; |
| 29 | import org.jdom2.input.SAXBuilder; |
| 30 | import org.jdom2.output.Format; |
| 31 | import org.jdom2.output.XMLOutputter; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import com.tailf.jnc.Capabilities; |
| 35 | import com.tailf.jnc.JNCException; |
| 36 | import com.tailf.jnc.SSHConnection; |
| 37 | import com.tailf.jnc.SSHSession; |
| 38 | |
| 39 | /** |
| 40 | * This is a logical representation of actual NETCONF device, carrying all the |
| 41 | * necessary information to connect and execute NETCONF operations. |
| 42 | */ |
| 43 | public class NetconfDevice { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 44 | private final Logger log = getLogger(NetconfDevice.class); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * The Device State is used to determine whether the device is active or |
| 48 | * inactive. This state infomation will help Device Creator to add or delete |
| 49 | * the device from the core. |
| 50 | */ |
| 51 | public static enum DeviceState { |
| 52 | /* Used to specify Active state of the device */ |
| 53 | ACTIVE, |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 54 | /* Used to specify inactive state of the device */ |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 55 | INACTIVE, |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 56 | /* Used to specify invalid state of the device */ |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 57 | INVALID |
| 58 | } |
| 59 | |
| 60 | private static final int DEFAULT_SSH_PORT = 22; |
Sanjay S | 59f86fd | 2015-05-20 12:23:48 +0530 | [diff] [blame^] | 61 | private static final int DEFAULT_CON_TIMEOUT = 0; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 62 | private static final String XML_CAPABILITY_KEY = "capability"; |
| 63 | private static final int EVENTINTERVAL = 2000; |
| 64 | private static final int CONNECTION_CHECK_INTERVAL = 3; |
| 65 | private static final String INPUT_HELLO_XML_MSG = new StringBuilder( |
| 66 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") |
| 67 | .append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">") |
| 68 | .append("<capabilities><capability>urn:ietf:params:netconf:base:1.0</capability>") |
| 69 | .append("</capabilities></hello>").toString(); |
| 70 | |
| 71 | private String sshHost; |
| 72 | private int sshPort = DEFAULT_SSH_PORT; |
Sanjay S | 59f86fd | 2015-05-20 12:23:48 +0530 | [diff] [blame^] | 73 | private int connectTimeout = DEFAULT_CON_TIMEOUT; |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 74 | private String username; |
| 75 | private String password; |
| 76 | private boolean reachable = false; |
| 77 | |
| 78 | private List<String> capabilities = new ArrayList<String>(); |
| 79 | private SSHConnection sshConnection = null; |
| 80 | |
| 81 | private DeviceState deviceState = DeviceState.INVALID; |
| 82 | |
| 83 | protected NetconfDevice(String sshHost, int sshPort, String username, |
| 84 | String password) { |
| 85 | this.username = checkNotNull(username, |
| 86 | "Netconf Username Cannot be null"); |
| 87 | this.sshHost = checkNotNull(sshHost, "Netconf Device IP cannot be null"); |
| 88 | this.sshPort = checkNotNull(sshPort, |
| 89 | "Netconf Device SSH port cannot be null"); |
| 90 | this.password = password; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * This will try to connect to NETCONF device and find all the capabilities. |
Thomas Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 95 | * |
| 96 | * @throws Exception if unable to connect to the device |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 97 | */ |
Thomas Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 98 | // FIXME: this should not be a generic Exception; perhaps wrap in some RuntimeException |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 99 | public void init() throws Exception { |
| 100 | try { |
| 101 | if (sshConnection == null) { |
Sanjay S | 59f86fd | 2015-05-20 12:23:48 +0530 | [diff] [blame^] | 102 | sshConnection = new SSHConnection(sshHost, sshPort, connectTimeout); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 103 | sshConnection.authenticateWithPassword(username, password); |
| 104 | } |
| 105 | // Send hello message to retrieve capabilities. |
| 106 | } catch (IOException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 107 | log.error("Fatal Error while creating connection to the device: " |
| 108 | + deviceInfo(), e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 109 | throw e; |
| 110 | } catch (JNCException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 111 | log.error("Failed to connect to the device: " + deviceInfo(), e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 112 | throw e; |
| 113 | } |
| 114 | |
| 115 | hello(); |
| 116 | } |
| 117 | |
| 118 | private void hello() { |
| 119 | SSHSession ssh = null; |
| 120 | try { |
| 121 | ssh = new SSHSession(sshConnection); |
| 122 | String helloRequestXML = INPUT_HELLO_XML_MSG.trim(); |
| 123 | |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 124 | log.debug("++++++++++++++++++++++++++++++++++Sending Hello: " |
| 125 | + sshConnection.getGanymedConnection().getHostname() |
| 126 | + "++++++++++++++++++++++++++++++++++"); |
| 127 | printPrettyXML(helloRequestXML); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 128 | ssh.print(helloRequestXML); |
| 129 | // ssh.print(endCharSeq); |
| 130 | ssh.flush(); |
| 131 | String xmlResponse = null; |
| 132 | int i = CONNECTION_CHECK_INTERVAL; |
| 133 | while (!ssh.ready() && i > 0) { |
| 134 | delay(EVENTINTERVAL); |
| 135 | i--; |
| 136 | } |
| 137 | |
| 138 | if (ssh.ready()) { |
| 139 | StringBuffer readOne = ssh.readOne(); |
| 140 | if (readOne == null) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 141 | log.error("The Hello Contains No Capabilites"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 142 | throw new JNCException( |
| 143 | JNCException.SESSION_ERROR, |
| 144 | "server does not support NETCONF base capability: " |
| 145 | + Capabilities.NETCONF_BASE_CAPABILITY); |
| 146 | } else { |
| 147 | xmlResponse = readOne.toString().trim(); |
| 148 | |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 149 | log.debug("++++++++++++++++++++++++++++++++++Reading Capabilities: " |
| 150 | + sshConnection.getGanymedConnection() |
| 151 | .getHostname() |
| 152 | + "++++++++++++++++++++++++++++++++++"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 153 | |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 154 | printPrettyXML(xmlResponse); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 155 | processCapabilities(xmlResponse); |
| 156 | } |
| 157 | } |
| 158 | reachable = true; |
| 159 | } catch (IOException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 160 | log.error("Fatal Error while sending Hello Message to the device: " |
| 161 | + deviceInfo(), e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 162 | } catch (JNCException e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 163 | log.error("Fatal Error while sending Hello Message to the device: " |
| 164 | + deviceInfo(), e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 165 | } finally { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 166 | log.debug("Closing the session after successful execution"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 167 | ssh.close(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private void processCapabilities(String xmlResponse) throws JNCException { |
| 172 | if (xmlResponse.isEmpty()) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 173 | log.error("The capability response cannot be empty"); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 174 | throw new JNCException( |
| 175 | JNCException.SESSION_ERROR, |
| 176 | "server does not support NETCONF base capability: " |
| 177 | + Capabilities.NETCONF_BASE_CAPABILITY); |
| 178 | } |
| 179 | try { |
| 180 | Document doc = new SAXBuilder() |
| 181 | .build(new StringReader(xmlResponse)); |
| 182 | Element rootElement = doc.getRootElement(); |
| 183 | processCapabilities(rootElement); |
| 184 | } catch (Exception e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 185 | log.error("ERROR while parsing the XML " + xmlResponse); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | private void processCapabilities(Element rootElement) { |
| 190 | List<Element> children = rootElement.getChildren(); |
| 191 | if (children.isEmpty()) { |
| 192 | return; |
| 193 | } |
| 194 | for (Element child : children) { |
| 195 | |
| 196 | if (child.getName().equals(XML_CAPABILITY_KEY)) { |
| 197 | capabilities.add(child.getValue()); |
| 198 | } |
| 199 | if (!child.getChildren().isEmpty()) { |
| 200 | processCapabilities(child); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private void printPrettyXML(String xmlstring) { |
| 206 | try { |
| 207 | Document doc = new SAXBuilder().build(new StringReader(xmlstring)); |
| 208 | XMLOutputter xmOut = new XMLOutputter(Format.getPrettyFormat()); |
| 209 | String outputString = xmOut.outputString(doc); |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 210 | log.debug(outputString); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 211 | } catch (Exception e) { |
Sanjay S | eb5eebb | 2015-04-24 15:44:50 +0530 | [diff] [blame] | 212 | log.error("ERROR while parsing the XML " + xmlstring, e); |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 213 | |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * This would return host IP and host Port, used by this particular Netconf |
| 219 | * Device. |
| 220 | * @return Device Information. |
| 221 | */ |
| 222 | public String deviceInfo() { |
| 223 | return new StringBuilder("host: ").append(sshHost).append(". port: ") |
| 224 | .append(sshPort).toString(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * This will terminate the device connection. |
| 229 | */ |
| 230 | public void disconnect() { |
| 231 | sshConnection.close(); |
| 232 | reachable = false; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * This will list down all the capabilities supported on the device. |
| 237 | * @return Capability list. |
| 238 | */ |
| 239 | public List<String> getCapabilities() { |
| 240 | return capabilities; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * This api is intended to know whether the device is connected or not. |
| 245 | * @return true if connected |
| 246 | */ |
| 247 | public boolean isReachable() { |
| 248 | return reachable; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * This will return the IP used connect ssh on the device. |
| 253 | * @return Netconf Device IP |
| 254 | */ |
| 255 | public String getSshHost() { |
| 256 | return sshHost; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * This will return the SSH Port used connect the device. |
| 261 | * @return SSH Port number |
| 262 | */ |
| 263 | public int getSshPort() { |
| 264 | return sshPort; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * The usename used to connect Netconf Device. |
| 269 | * @return Device Username |
| 270 | */ |
| 271 | public String getUsername() { |
| 272 | return username; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Retrieve current state of the device. |
| 277 | * @return Current Device State |
| 278 | */ |
| 279 | public DeviceState getDeviceState() { |
| 280 | return deviceState; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * This is set the state information for the device. |
| 285 | * @param deviceState Next Device State |
| 286 | */ |
| 287 | public void setDeviceState(DeviceState deviceState) { |
| 288 | this.deviceState = deviceState; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Check whether the device is in Active state. |
| 293 | * @return true if the device is Active |
| 294 | */ |
| 295 | public boolean isActive() { |
| 296 | return deviceState == DeviceState.ACTIVE ? true : false; |
| 297 | } |
Sanjay S | 59f86fd | 2015-05-20 12:23:48 +0530 | [diff] [blame^] | 298 | |
| 299 | public void setConnectTimeout(int connectTimeout) { |
| 300 | this.connectTimeout = connectTimeout; |
| 301 | } |
Sanjay S | e8dcfee | 2015-04-23 10:07:08 +0530 | [diff] [blame] | 302 | } |