blob: 7b56b8865b5ef65797d2946c223ccf163400a277 [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;
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 Vachuska266b4432015-04-30 18:13:25 -070093 *
94 * @throws Exception if unable to connect to the device
Sanjay Se8dcfee2015-04-23 10:07:08 +053095 */
Thomas Vachuska266b4432015-04-30 18:13:25 -070096 // FIXME: this should not be a generic Exception; perhaps wrap in some RuntimeException
Sanjay Se8dcfee2015-04-23 10:07:08 +053097 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 Seb5eebb2015-04-24 15:44:50 +0530105 log.error("Fatal Error while creating connection to the device: "
106 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530107 throw e;
108 } catch (JNCException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530109 log.error("Failed to connect to the device: " + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530110 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 Seb5eebb2015-04-24 15:44:50 +0530122 log.debug("++++++++++++++++++++++++++++++++++Sending Hello: "
123 + sshConnection.getGanymedConnection().getHostname()
124 + "++++++++++++++++++++++++++++++++++");
125 printPrettyXML(helloRequestXML);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530126 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 Seb5eebb2015-04-24 15:44:50 +0530139 log.error("The Hello Contains No Capabilites");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530140 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 Seb5eebb2015-04-24 15:44:50 +0530147 log.debug("++++++++++++++++++++++++++++++++++Reading Capabilities: "
148 + sshConnection.getGanymedConnection()
149 .getHostname()
150 + "++++++++++++++++++++++++++++++++++");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530151
Sanjay Seb5eebb2015-04-24 15:44:50 +0530152 printPrettyXML(xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530153 processCapabilities(xmlResponse);
154 }
155 }
156 reachable = true;
157 } catch (IOException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530158 log.error("Fatal Error while sending Hello Message to the device: "
159 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530160 } catch (JNCException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530161 log.error("Fatal Error while sending Hello Message to the device: "
162 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530163 } finally {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530164 log.debug("Closing the session after successful execution");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530165 ssh.close();
166 }
167 }
168
169 private void processCapabilities(String xmlResponse) throws JNCException {
170 if (xmlResponse.isEmpty()) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530171 log.error("The capability response cannot be empty");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530172 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 Seb5eebb2015-04-24 15:44:50 +0530183 log.error("ERROR while parsing the XML " + xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530184 }
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 Seb5eebb2015-04-24 15:44:50 +0530208 log.debug(outputString);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530209 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530210 log.error("ERROR while parsing the XML " + xmlstring, e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530211
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}