blob: fd4112589da8f74fc7027ef337c669f0b487ac69 [file] [log] [blame]
Hyunsun Moon483e29f2016-02-05 16:55:33 -08001/*
2 * Copyright 2016 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.cordvtn;
17
18import com.google.common.collect.Sets;
19import com.google.common.io.CharStreams;
20import com.jcraft.jsch.Channel;
21import com.jcraft.jsch.ChannelExec;
22import com.jcraft.jsch.JSch;
23import com.jcraft.jsch.JSchException;
24import com.jcraft.jsch.Session;
25import org.onlab.packet.IpPrefix;
26import org.slf4j.Logger;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.util.Set;
32import java.util.regex.Pattern;
33import java.util.stream.Collectors;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * {@code RemoteIpCommandUtil} provides methods to help execute Linux IP commands to a remote server.
39 * It opens individual exec channels for each command. User can create a session with {@code connect}
40 * method and then execute a series commands. After done with all commands, the session must be closed
41 * explicitly by calling {@code disconnect}.
42 */
43public final class RemoteIpCommandUtil {
44
45 protected static final Logger log = getLogger(RemoteIpCommandUtil.class);
46
47 private static final String STRICT_HOST_CHECKING = "StrictHostKeyChecking";
48 private static final String DEFAULT_STRICT_HOST_CHECKING = "no";
49 private static final int DEFAULT_SESSION_TIMEOUT = 60000; // milliseconds
50
51 private static final String IP_PATTERN = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.)" +
52 "{3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$";
53
54 private static final String IP_ADDR_SHOW = "sudo ip addr show %s";
55 private static final String IP_ADDR_FLUSH = "sudo ip addr flush %s";
56 private static final String IP_ADDR_ADD = "sudo ip addr add %s dev %s";
57 private static final String IP_ADDR_DELETE = "sudo ip addr delete %s dev %s";
58 private static final String IP_LINK_SHOW = "sudo ip link show %s";
59 private static final String IP_LINK_UP = "sudo ip link set %s up";
60
61 /**
62 * Default constructor.
63 */
64 private RemoteIpCommandUtil() {
65 }
66
67 /**
68 * Adds a given IP address to a given device.
69 *
70 * @param session ssh connection
71 * @param ip ip address
72 * @param device device name to assign the ip address
73 * @return true if the command succeeds, or false
74 */
75 public static boolean addIp(Session session, IpPrefix ip, String device) {
76 if (session == null || !session.isConnected()) {
77 return false;
78 }
79
80 executeCommand(session, String.format(IP_ADDR_ADD, ip, device));
81 Set<IpPrefix> result = getCurrentIps(session, device);
82 return result.contains(ip);
83 }
84
85 /**
86 * Removes the IP address from a given device.
87 *
88 * @param session ssh connection
89 * @param ip ip address
90 * @param device device name
91 * @return true if the command succeeds, or false
92 */
93 public static boolean deleteIp(Session session, IpPrefix ip, String device) {
94 if (session == null || !session.isConnected()) {
95 return false;
96 }
97
98 executeCommand(session, String.format(IP_ADDR_DELETE, ip, device));
99 Set<IpPrefix> result = getCurrentIps(session, device);
100 return !result.contains(ip);
101 }
102
103 /**
104 * Removes all IP address on a given device.
105 *
106 * @param session ssh connection
107 * @param device device name
108 * @return true if the command succeeds, or false
109 */
110 public static boolean flushIp(Session session, String device) {
111 if (session == null || !session.isConnected()) {
112 return false;
113 }
114
115 executeCommand(session, String.format(IP_ADDR_FLUSH, device));
116 return getCurrentIps(session, device).isEmpty();
117 }
118
119 /**
120 * Returns a set of IP address that a given device has.
121 *
122 * @param session ssh connection
123 * @param device device name
124 * @return set of IP prefix or empty set
125 */
126 public static Set<IpPrefix> getCurrentIps(Session session, String device) {
127 if (session == null || !session.isConnected()) {
128 return Sets.newHashSet();
129 }
130
131 String output = executeCommand(session, String.format(IP_ADDR_SHOW, device));
132 Set<IpPrefix> result = Pattern.compile(" ")
133 .splitAsStream(output)
134 .filter(s -> s.matches(IP_PATTERN))
135 .map(IpPrefix::valueOf)
136 .collect(Collectors.toSet());
137
138 return result;
139 }
140
141 /**
142 * Sets link state up for a given device.
143 *
144 * @param session ssh connection
145 * @param device device name
146 * @return true if the command succeeds, or false
147 */
148 public static boolean setInterfaceUp(Session session, String device) {
149 if (session == null || !session.isConnected()) {
150 return false;
151 }
152
153 executeCommand(session, String.format(IP_LINK_UP, device));
154 return isInterfaceUp(session, device);
155 }
156
157 /**
158 * Checks if a given interface is up or not.
159 *
160 * @param session ssh connection
161 * @param device device name
162 * @return true if the interface is up, or false
163 */
164 public static boolean isInterfaceUp(Session session, String device) {
165 if (session == null || !session.isConnected()) {
166 return false;
167 }
168
169 String output = executeCommand(session, String.format(IP_LINK_SHOW, device));
170 return output != null && output.contains("UP");
171 }
172
173 /**
174 * Creates a new session with a given access information.
175 *
176 * @param sshInfo information to ssh to the remove server
177 * @return ssh session, or null
178 */
179 public static Session connect(SshAccessInfo sshInfo) {
180 try {
181 JSch jsch = new JSch();
182 jsch.addIdentity(sshInfo.privateKey());
183
184 Session session = jsch.getSession(sshInfo.user(),
185 sshInfo.remoteIp().toString(),
186 sshInfo.port().toInt());
187 session.setConfig(STRICT_HOST_CHECKING, DEFAULT_STRICT_HOST_CHECKING);
188 session.connect(DEFAULT_SESSION_TIMEOUT);
189
190 return session;
191 } catch (JSchException e) {
192 log.debug("Failed to connect to {} due to {}", sshInfo.toString(), e.toString());
193 return null;
194 }
195 }
196
197 /**
198 * Closes a connection.
199 *
200 * @param session session
201 */
202 public static void disconnect(Session session) {
203 if (session.isConnected()) {
204 session.disconnect();
205 }
206 }
207
208 /**
209 * Executes a given command. It opens exec channel for the command and closes
210 * the channel when it's done.
211 *
212 * @param session ssh connection to a remote server
213 * @param command command to execute
214 * @return command output string if the command succeeds, or null
215 */
216 private static String executeCommand(Session session, String command) {
217 if (session == null || !session.isConnected()) {
218 return null;
219 }
220
221 log.debug("Execute command {} to {}", command, session.getHost());
222
223 try {
224 Channel channel = session.openChannel("exec");
225 ((ChannelExec) channel).setCommand(command);
226 channel.setInputStream(null);
227 InputStream output = channel.getInputStream();
228
229 channel.connect();
230 String result = CharStreams.toString(new InputStreamReader(output));
231 channel.disconnect();
232
233 return result;
234 } catch (JSchException | IOException e) {
235 log.debug("Failed to execute command {} due to {}", command, e.toString());
236 return null;
237 }
238 }
239}