blob: 457b9c0db17f56e4f68835c00ebf41bee3b095ce [file] [log] [blame]
Samir Anand01c77c42015-08-06 14:50:29 +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.flow.impl;
17
18import static org.onlab.util.Tools.delay;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.IOException;
22
23import org.slf4j.Logger;
24
25import com.tailf.jnc.Capabilities;
26import com.tailf.jnc.JNCException;
27import com.tailf.jnc.SSHConnection;
28import com.tailf.jnc.SSHSession;
29
30/**
31 * This is to carry necessary information to connect and execute NETCONF
32 * operations.
33 */
34public class NetconfOperation {
35 private final Logger log = getLogger(NetconfOperation.class);
36 private static final int EVENTINTERVAL = 2000;
37 private static final int CONNECTION_CHECK_INTERVAL = 3;
38 private static final String INPUT_HELLO_XML_MSG = new StringBuilder(
39 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
40 .append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">")
41 .append("<capabilities><capability>urn:ietf:params:netconf:base:1.0</capability>")
42 .append("</capabilities></hello>").toString();
43
44 /**
45 * This will send a Xml message to the device.
46 */
47 protected void sendXmlMessage(String xmlMsg, String username,
48 String password, String deviceIp,
49 Integer devicePort) {
50 SSHSession ssh = null;
51 try {
52 SSHConnection sshConnection = getConnection(username, password,
53 deviceIp, devicePort);
54 ssh = new SSHSession(sshConnection);
55 executeMessage(ssh, INPUT_HELLO_XML_MSG);
56 /*
57 * execute acl message
58 */
59 executeMessage(ssh, xmlMsg);
60
61 } catch (IOException e) {
62 log.error("Unable to send Hello Message to the device: ", e);
63 } catch (JNCException e) {
64 log.error("Authentication fail while sending Hello Message to the device: ",
65 e);
66 } catch (Exception e) {
67 log.error("Unable to send Hello Message to the device: ", e);
68 } finally {
69 log.debug("Closing the session after successful execution");
70 ssh.close();
71 }
72 }
73
74 private void executeMessage(SSHSession ssh, String xmlMsg)
75 throws IOException, JNCException {
76 String helloRequestXML = xmlMsg.trim();
77
78 log.debug("Sending Hello");
79 ssh.print(helloRequestXML);
80 ssh.flush();
81 String xmlResponse = null;
82 int i = CONNECTION_CHECK_INTERVAL;
83 while (!ssh.ready() && i > 0) {
84 delay(EVENTINTERVAL);
85 i--;
86 }
87
88 if (ssh.ready()) {
89 StringBuffer readOne = ssh.readOne();
90 if (readOne == null) {
91 log.error("The Hello Contains No Capabilites");
92 throw new JNCException(
93 JNCException.SESSION_ERROR,
94 "server does not support NETCONF base capability: "
95 + Capabilities.NETCONF_BASE_CAPABILITY);
96 } else {
97 xmlResponse = readOne.toString().trim();
98
99 log.debug("Reading Capabilities: "
100 + ssh.getSSHConnection().getGanymedConnection()
101 .getHostname());
102 }
103 }
104 }
105
106 /**
107 * To establish SSH Connection.
108 */
109 public SSHConnection getConnection(String username, String password,
110 String sshHost, Integer sshPort)
111 throws Exception {
112 SSHConnection sshConnection;
113 try {
114 sshConnection = new SSHConnection(sshHost, sshPort);
115 sshConnection.authenticateWithPassword(username, password);
116 } catch (IOException e) {
117 log.error("Unable to create a connection to the device: ");
118 throw e;
119 } catch (JNCException e) {
120 log.error("Failed to connect to the device: ");
121 throw e;
122 }
123 return sshConnection;
124 }
125
126}