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