Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 17 | package org.onosproject.netconf.ctl.impl; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 18 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 21 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 22 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 23 | import org.onosproject.netconf.NetconfException; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.io.BufferedReader; |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.InputStreamReader; |
| 31 | import java.io.OutputStream; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame^] | 32 | import java.io.OutputStreamWriter; |
| 33 | import java.nio.charset.StandardCharsets; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 34 | import java.util.List; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 35 | import java.util.Map; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 36 | import java.util.Optional; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 37 | import java.util.concurrent.CompletableFuture; |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 38 | import java.util.regex.Matcher; |
| 39 | import java.util.regex.Pattern; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Thread that gets spawned each time a session is established and handles all the input |
| 43 | * and output from the session's streams to and from the NETCONF device the session is |
| 44 | * established with. |
| 45 | */ |
| 46 | public class NetconfStreamThread extends Thread implements NetconfStreamHandler { |
| 47 | |
| 48 | private static final Logger log = LoggerFactory |
| 49 | .getLogger(NetconfStreamThread.class); |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 50 | private static final String HELLO = "<hello"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 51 | private static final String END_PATTERN = "]]>]]>"; |
| 52 | private static final String RPC_REPLY = "rpc-reply"; |
| 53 | private static final String RPC_ERROR = "rpc-error"; |
heisenberg | b7017d7 | 2016-04-13 02:16:07 -0700 | [diff] [blame] | 54 | private static final String NOTIFICATION_LABEL = "<notification"; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 55 | private static final String MESSAGE_ID = "message-id="; |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 56 | private static final Pattern MSGID_PATTERN = Pattern.compile(MESSAGE_ID + "\"(\\d+)\""); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 57 | |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame^] | 58 | private OutputStreamWriter outputStream; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 59 | private final InputStream err; |
| 60 | private final InputStream in; |
| 61 | private NetconfDeviceInfo netconfDeviceInfo; |
| 62 | private NetconfSessionDelegate sessionDelegate; |
| 63 | private NetconfMessageState state; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 64 | private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners |
| 65 | = Lists.newCopyOnWriteArrayList(); |
| 66 | private boolean enableNotifications = true; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 67 | private Map<Integer, CompletableFuture<String>> replies; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 68 | |
| 69 | public NetconfStreamThread(final InputStream in, final OutputStream out, |
| 70 | final InputStream err, NetconfDeviceInfo deviceInfo, |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 71 | NetconfSessionDelegate delegate, |
| 72 | Map<Integer, CompletableFuture<String>> replies) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 73 | this.in = in; |
| 74 | this.err = err; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame^] | 75 | outputStream = new OutputStreamWriter(out, StandardCharsets.UTF_8); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 76 | netconfDeviceInfo = deviceInfo; |
| 77 | state = NetconfMessageState.NO_MATCHING_PATTERN; |
| 78 | sessionDelegate = delegate; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 79 | this.replies = replies; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 80 | log.debug("Stream thread for device {} session started", deviceInfo); |
| 81 | start(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public CompletableFuture<String> sendMessage(String request) { |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 86 | Optional<Integer> messageId = getMsgId(request); |
| 87 | return sendMessage(request, messageId.get()); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public CompletableFuture<String> sendMessage(String request, int messageId) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 92 | log.debug("Sending message {} to device {}", request, netconfDeviceInfo); |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 93 | CompletableFuture<String> cf = new CompletableFuture<>(); |
| 94 | replies.put(messageId, cf); |
| 95 | |
| 96 | synchronized (outputStream) { |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame^] | 97 | try { |
| 98 | outputStream.write(request); |
| 99 | outputStream.flush(); |
| 100 | } catch (IOException e) { |
| 101 | log.error("Writing to {} failed", netconfDeviceInfo, e); |
| 102 | cf.completeExceptionally(e); |
| 103 | } |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return cf; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public enum NetconfMessageState { |
| 110 | |
| 111 | NO_MATCHING_PATTERN { |
| 112 | @Override |
| 113 | NetconfMessageState evaluateChar(char c) { |
| 114 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 115 | return FIRST_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 116 | } else { |
| 117 | return this; |
| 118 | } |
| 119 | } |
| 120 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 121 | FIRST_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 122 | @Override |
| 123 | NetconfMessageState evaluateChar(char c) { |
| 124 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 125 | return SECOND_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 126 | } else { |
| 127 | return NO_MATCHING_PATTERN; |
| 128 | } |
| 129 | } |
| 130 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 131 | SECOND_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 132 | @Override |
| 133 | NetconfMessageState evaluateChar(char c) { |
| 134 | if (c == '>') { |
| 135 | return FIRST_BIGGER; |
| 136 | } else { |
| 137 | return NO_MATCHING_PATTERN; |
| 138 | } |
| 139 | } |
| 140 | }, |
| 141 | FIRST_BIGGER { |
| 142 | @Override |
| 143 | NetconfMessageState evaluateChar(char c) { |
| 144 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 145 | return THIRD_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 146 | } else { |
| 147 | return NO_MATCHING_PATTERN; |
| 148 | } |
| 149 | } |
| 150 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 151 | THIRD_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 152 | @Override |
| 153 | NetconfMessageState evaluateChar(char c) { |
| 154 | if (c == ']') { |
| 155 | return ENDING_BIGGER; |
| 156 | } else { |
| 157 | return NO_MATCHING_PATTERN; |
| 158 | } |
| 159 | } |
| 160 | }, |
| 161 | ENDING_BIGGER { |
| 162 | @Override |
| 163 | NetconfMessageState evaluateChar(char c) { |
| 164 | if (c == '>') { |
| 165 | return END_PATTERN; |
| 166 | } else { |
| 167 | return NO_MATCHING_PATTERN; |
| 168 | } |
| 169 | } |
| 170 | }, |
| 171 | END_PATTERN { |
| 172 | @Override |
| 173 | NetconfMessageState evaluateChar(char c) { |
| 174 | return NO_MATCHING_PATTERN; |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | abstract NetconfMessageState evaluateChar(char c); |
| 179 | } |
| 180 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 181 | @Override |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 182 | public void run() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 183 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 184 | try { |
| 185 | boolean socketClosed = false; |
| 186 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
| 187 | while (!socketClosed) { |
| 188 | int cInt = bufferReader.read(); |
| 189 | if (cInt == -1) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 190 | log.debug("Netconf device {} sent error char in session," + |
| 191 | " will need to be reopend", netconfDeviceInfo); |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 192 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 193 | NetconfDeviceOutputEvent.Type.SESSION_CLOSED, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 194 | null, null, Optional.of(-1), netconfDeviceInfo); |
| 195 | netconfDeviceEventListeners.forEach( |
| 196 | listener -> listener.event(event)); |
| 197 | socketClosed = true; |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 198 | log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 199 | } |
| 200 | char c = (char) cInt; |
| 201 | state = state.evaluateChar(c); |
| 202 | deviceReplyBuilder.append(c); |
| 203 | if (state == NetconfMessageState.END_PATTERN) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 204 | String deviceReply = deviceReplyBuilder.toString(); |
| 205 | if (deviceReply.equals(END_PATTERN)) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 206 | socketClosed = true; |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 207 | log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", |
| 208 | netconfDeviceInfo, deviceReply); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 209 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 210 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 211 | null, null, Optional.of(-2), netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 212 | netconfDeviceEventListeners.forEach( |
| 213 | listener -> listener.event(event)); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 214 | this.interrupt(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 215 | } else { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 216 | deviceReply = deviceReply.replace(END_PATTERN, ""); |
| 217 | if (deviceReply.contains(RPC_REPLY) || |
| 218 | deviceReply.contains(RPC_ERROR) || |
| 219 | deviceReply.contains(HELLO)) { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 220 | log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", |
| 221 | netconfDeviceInfo, getMsgId(deviceReply), deviceReply); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 222 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 223 | NetconfDeviceOutputEvent.Type.DEVICE_REPLY, |
| 224 | null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); |
| 225 | sessionDelegate.notify(event); |
| 226 | netconfDeviceEventListeners.forEach( |
| 227 | listener -> listener.event(event)); |
| 228 | } else if (deviceReply.contains(NOTIFICATION_LABEL)) { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 229 | log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", |
| 230 | netconfDeviceInfo, enableNotifications, |
| 231 | getMsgId(deviceReply), deviceReply); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 232 | if (enableNotifications) { |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 233 | log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size()); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 234 | final String finalDeviceReply = deviceReply; |
| 235 | netconfDeviceEventListeners.forEach( |
| 236 | listener -> listener.event(new NetconfDeviceOutputEvent( |
| 237 | NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, |
| 238 | null, finalDeviceReply, getMsgId(finalDeviceReply), |
| 239 | netconfDeviceInfo))); |
| 240 | } |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 241 | } else { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 242 | log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 243 | } |
| 244 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 245 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } catch (IOException e) { |
Andrea Campanella | d264b49 | 2016-03-01 09:46:06 -0800 | [diff] [blame] | 249 | log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 250 | throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" + |
| 251 | netconfDeviceInfo, e)); |
| 252 | //TODO should we send a socket closed message to listeners ? |
| 253 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 256 | protected static Optional<Integer> getMsgId(String reply) { |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 257 | Matcher matcher = MSGID_PATTERN.matcher(reply); |
| 258 | if (matcher.find()) { |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame^] | 259 | try { |
| 260 | return Optional.of(Integer.valueOf(matcher.group(1))); |
| 261 | } catch (NumberFormatException e) { |
| 262 | log.warn("Failed to parse message-id from {}", matcher.group(), e); |
| 263 | } |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 264 | } |
| 265 | if (reply.contains(HELLO)) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 266 | return Optional.of(-1); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 267 | } |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 268 | return Optional.empty(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 271 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 272 | public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 273 | if (!netconfDeviceEventListeners.contains(listener)) { |
| 274 | netconfDeviceEventListeners.add(listener); |
| 275 | } |
| 276 | } |
| 277 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 278 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 279 | public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 280 | netconfDeviceEventListeners.remove(listener); |
| 281 | } |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 282 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 283 | @Override |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 284 | public void setEnableNotifications(boolean enableNotifications) { |
| 285 | this.enableNotifications = enableNotifications; |
| 286 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 287 | } |