blob: f626688ba8673ad2d815e6bf59d763925ee63920 [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.
93 */
94 public void init() throws Exception {
95 try {
96 if (sshConnection == null) {
97 sshConnection = new SSHConnection(sshHost, sshPort);
98 sshConnection.authenticateWithPassword(username, password);
99 }
100 // Send hello message to retrieve capabilities.
101 } catch (IOException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530102 log.error("Fatal Error while creating connection to the device: "
103 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530104 throw e;
105 } catch (JNCException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530106 log.error("Failed to connect to the device: " + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530107 throw e;
108 }
109
110 hello();
111 }
112
113 private void hello() {
114 SSHSession ssh = null;
115 try {
116 ssh = new SSHSession(sshConnection);
117 String helloRequestXML = INPUT_HELLO_XML_MSG.trim();
118
Sanjay Seb5eebb2015-04-24 15:44:50 +0530119 log.debug("++++++++++++++++++++++++++++++++++Sending Hello: "
120 + sshConnection.getGanymedConnection().getHostname()
121 + "++++++++++++++++++++++++++++++++++");
122 printPrettyXML(helloRequestXML);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530123 ssh.print(helloRequestXML);
124 // ssh.print(endCharSeq);
125 ssh.flush();
126 String xmlResponse = null;
127 int i = CONNECTION_CHECK_INTERVAL;
128 while (!ssh.ready() && i > 0) {
129 delay(EVENTINTERVAL);
130 i--;
131 }
132
133 if (ssh.ready()) {
134 StringBuffer readOne = ssh.readOne();
135 if (readOne == null) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530136 log.error("The Hello Contains No Capabilites");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530137 throw new JNCException(
138 JNCException.SESSION_ERROR,
139 "server does not support NETCONF base capability: "
140 + Capabilities.NETCONF_BASE_CAPABILITY);
141 } else {
142 xmlResponse = readOne.toString().trim();
143
Sanjay Seb5eebb2015-04-24 15:44:50 +0530144 log.debug("++++++++++++++++++++++++++++++++++Reading Capabilities: "
145 + sshConnection.getGanymedConnection()
146 .getHostname()
147 + "++++++++++++++++++++++++++++++++++");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530148
Sanjay Seb5eebb2015-04-24 15:44:50 +0530149 printPrettyXML(xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530150 processCapabilities(xmlResponse);
151 }
152 }
153 reachable = true;
154 } catch (IOException e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530155 log.error("Fatal Error while sending Hello Message to the device: "
156 + deviceInfo(), e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530157 } catch (JNCException 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 } finally {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530161 log.debug("Closing the session after successful execution");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530162 ssh.close();
163 }
164 }
165
166 private void processCapabilities(String xmlResponse) throws JNCException {
167 if (xmlResponse.isEmpty()) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530168 log.error("The capability response cannot be empty");
Sanjay Se8dcfee2015-04-23 10:07:08 +0530169 throw new JNCException(
170 JNCException.SESSION_ERROR,
171 "server does not support NETCONF base capability: "
172 + Capabilities.NETCONF_BASE_CAPABILITY);
173 }
174 try {
175 Document doc = new SAXBuilder()
176 .build(new StringReader(xmlResponse));
177 Element rootElement = doc.getRootElement();
178 processCapabilities(rootElement);
179 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530180 log.error("ERROR while parsing the XML " + xmlResponse);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530181 }
182 }
183
184 private void processCapabilities(Element rootElement) {
185 List<Element> children = rootElement.getChildren();
186 if (children.isEmpty()) {
187 return;
188 }
189 for (Element child : children) {
190
191 if (child.getName().equals(XML_CAPABILITY_KEY)) {
192 capabilities.add(child.getValue());
193 }
194 if (!child.getChildren().isEmpty()) {
195 processCapabilities(child);
196 }
197 }
198 }
199
200 private void printPrettyXML(String xmlstring) {
201 try {
202 Document doc = new SAXBuilder().build(new StringReader(xmlstring));
203 XMLOutputter xmOut = new XMLOutputter(Format.getPrettyFormat());
204 String outputString = xmOut.outputString(doc);
Sanjay Seb5eebb2015-04-24 15:44:50 +0530205 log.debug(outputString);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530206 } catch (Exception e) {
Sanjay Seb5eebb2015-04-24 15:44:50 +0530207 log.error("ERROR while parsing the XML " + xmlstring, e);
Sanjay Se8dcfee2015-04-23 10:07:08 +0530208
209 }
210 }
211
212 /**
213 * This would return host IP and host Port, used by this particular Netconf
214 * Device.
215 * @return Device Information.
216 */
217 public String deviceInfo() {
218 return new StringBuilder("host: ").append(sshHost).append(". port: ")
219 .append(sshPort).toString();
220 }
221
222 /**
223 * This will terminate the device connection.
224 */
225 public void disconnect() {
226 sshConnection.close();
227 reachable = false;
228 }
229
230 /**
231 * This will list down all the capabilities supported on the device.
232 * @return Capability list.
233 */
234 public List<String> getCapabilities() {
235 return capabilities;
236 }
237
238 /**
239 * This api is intended to know whether the device is connected or not.
240 * @return true if connected
241 */
242 public boolean isReachable() {
243 return reachable;
244 }
245
246 /**
247 * This will return the IP used connect ssh on the device.
248 * @return Netconf Device IP
249 */
250 public String getSshHost() {
251 return sshHost;
252 }
253
254 /**
255 * This will return the SSH Port used connect the device.
256 * @return SSH Port number
257 */
258 public int getSshPort() {
259 return sshPort;
260 }
261
262 /**
263 * The usename used to connect Netconf Device.
264 * @return Device Username
265 */
266 public String getUsername() {
267 return username;
268 }
269
270 /**
271 * Retrieve current state of the device.
272 * @return Current Device State
273 */
274 public DeviceState getDeviceState() {
275 return deviceState;
276 }
277
278 /**
279 * This is set the state information for the device.
280 * @param deviceState Next Device State
281 */
282 public void setDeviceState(DeviceState deviceState) {
283 this.deviceState = deviceState;
284 }
285
286 /**
287 * Check whether the device is in Active state.
288 * @return true if the device is Active
289 */
290 public boolean isActive() {
291 return deviceState == DeviceState.ACTIVE ? true : false;
292 }
293}