blob: 440c04520c386add87ead4899b7f61991eefeaff [file] [log] [blame]
Sanjay Se8dcfee2015-04-23 10:07:08 +05301/*
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 */
16package org.onosproject.provider.netconf.device.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.util.Tools.delay;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.io.IOException;
23import java.io.StringReader;
24import java.util.ArrayList;
25import java.util.List;
26
27import org.jdom2.Document;
28import org.jdom2.Element;
29import org.jdom2.input.SAXBuilder;
30import org.jdom2.output.Format;
31import org.jdom2.output.XMLOutputter;
32import org.slf4j.Logger;
33
34import com.tailf.jnc.Capabilities;
35import com.tailf.jnc.JNCException;
36import com.tailf.jnc.SSHConnection;
37import 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 */
43public class NetconfDevice {
Sanjay Seb5eebb2015-04-24 15:44:50 +053044 private final Logger log = getLogger(NetconfDevice.class);
Sanjay Se8dcfee2015-04-23 10:07:08 +053045
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 Seb5eebb2015-04-24 15:44:50 +053054 /* Used to specify inactive state of the device */
Sanjay Se8dcfee2015-04-23 10:07:08 +053055 INACTIVE,
Sanjay Seb5eebb2015-04-24 15:44:50 +053056 /* Used to specify invalid state of the device */
Sanjay Se8dcfee2015-04-23 10:07:08 +053057 INVALID
58 }
59
60 private static final int DEFAULT_SSH_PORT = 22;
Sanjay S59f86fd2015-05-20 12:23:48 +053061 private static final int DEFAULT_CON_TIMEOUT = 0;
Sanjay Se8dcfee2015-04-23 10:07:08 +053062 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 S59f86fd2015-05-20 12:23:48 +053073 private int connectTimeout = DEFAULT_CON_TIMEOUT;
Sanjay Se8dcfee2015-04-23 10:07:08 +053074 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 Vachuska266b4432015-04-30 18:13:25 -070095 *
96 * @throws Exception if unable to connect to the device
Sanjay Se8dcfee2015-04-23 10:07:08 +053097 */
Thomas Vachuska266b4432015-04-30 18:13:25 -070098 // FIXME: this should not be a generic Exception; perhaps wrap in some RuntimeException
Sanjay Se8dcfee2015-04-23 10:07:08 +053099 public void init() throws Exception {
100 try {
101 if (sshConnection == null) {
Sanjay S59f86fd2015-05-20 12:23:48 +0530102 sshConnection = new SSHConnection(sshHost, sshPort, connectTimeout);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530103 sshConnection.authenticateWithPassword(username, password);
104 }
105 // Send hello message to retrieve capabilities.
106 } catch (IOException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530107 log.error("Fatal Error while creating connection to the device: "
108 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530109 throw e;
110 } catch (JNCException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530111 log.error("Failed to connect to the device: " + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530112 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 Seb5eebb2015-04-24 15:44:50 +0530124 log.debug("++++++++++++++++++++++++++++++++++Sending Hello: "
125 + sshConnection.getGanymedConnection().getHostname()
126 + "++++++++++++++++++++++++++++++++++");
127 printPrettyXML(helloRequestXML);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530128 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 Seb5eebb2015-04-24 15:44:50 +0530141 log.error("The Hello Contains No Capabilites");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530142 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 Seb5eebb2015-04-24 15:44:50 +0530149 log.debug("++++++++++++++++++++++++++++++++++Reading Capabilities: "
150 + sshConnection.getGanymedConnection()
151 .getHostname()
152 + "++++++++++++++++++++++++++++++++++");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530153
Sanjay Seb5eebb2015-04-24 15:44:50 +0530154 printPrettyXML(xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530155 processCapabilities(xmlResponse);
156 }
157 }
158 reachable = true;
159 } catch (IOException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530160 log.error("Fatal Error while sending Hello Message to the device: "
161 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530162 } catch (JNCException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530163 log.error("Fatal Error while sending Hello Message to the device: "
164 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530165 } finally {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530166 log.debug("Closing the session after successful execution");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530167 ssh.close();
168 }
169 }
170
171 private void processCapabilities(String xmlResponse) throws JNCException {
172 if (xmlResponse.isEmpty()) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530173 log.error("The capability response cannot be empty");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530174 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 Seb5eebb2015-04-24 15:44:50 +0530185 log.error("ERROR while parsing the XML " + xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530186 }
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 Seb5eebb2015-04-24 15:44:50 +0530210 log.debug(outputString);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530211 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530212 log.error("ERROR while parsing the XML " + xmlstring, e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530213
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 S59f86fd2015-05-20 12:23:48 +0530298
299 public void setConnectTimeout(int connectTimeout) {
300 this.connectTimeout = connectTimeout;
301 }
Sanjay Se8dcfee2015-04-23 10:07:08 +0530302}