Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 27 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 28 | import java.io.BufferedReader; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 29 | import java.io.InputStream; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 30 | import java.io.OutputStream; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 31 | import java.io.OutputStreamWriter; |
| 32 | import java.nio.charset.StandardCharsets; |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 33 | import java.nio.channels.ClosedByInterruptException; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 34 | import java.io.InputStreamReader; |
| 35 | import java.io.IOException; |
| 36 | import java.io.UnsupportedEncodingException; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 37 | import java.util.List; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 38 | import java.util.Map; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 39 | import java.util.ArrayList; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 40 | import java.util.Optional; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 41 | import java.util.concurrent.CompletableFuture; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 42 | import java.util.regex.MatchResult; |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 43 | import java.util.regex.Matcher; |
| 44 | import java.util.regex.Pattern; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Thread that gets spawned each time a session is established and handles all the input |
| 48 | * and output from the session's streams to and from the NETCONF device the session is |
| 49 | * established with. |
| 50 | */ |
| 51 | public class NetconfStreamThread extends Thread implements NetconfStreamHandler { |
| 52 | |
| 53 | private static final Logger log = LoggerFactory |
| 54 | .getLogger(NetconfStreamThread.class); |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 55 | private static final String HELLO = "<hello"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 56 | private static final String END_PATTERN = "]]>]]>"; |
| 57 | private static final String RPC_REPLY = "rpc-reply"; |
| 58 | private static final String RPC_ERROR = "rpc-error"; |
heisenberg | b7017d7 | 2016-04-13 02:16:07 -0700 | [diff] [blame] | 59 | private static final String NOTIFICATION_LABEL = "<notification"; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 60 | private static final String MESSAGE_ID = "message-id="; |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 61 | private static final Pattern MSGID_PATTERN = Pattern.compile(MESSAGE_ID + "\"(\\d+)\""); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 62 | private static final String MSGLEN_REGEX_PATTERN = "\n#\\d+\n"; |
| 63 | // pattern to verify whole Chunked-Message format |
| 64 | private static final Pattern CHUNKED_FRAMING_PATTERN = |
| 65 | Pattern.compile("(\\n#([1-9][0-9]*)\\n(.+))+\\n##\\n", Pattern.DOTALL); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 66 | private static final String CHUNKED_END_REGEX_PATTERN = "\n##\n"; |
| 67 | // pattern to parse each chunk-size in ChunkedMessage chunk |
| 68 | private static final Pattern CHUNKED_SIZE_PATTERN = Pattern.compile("\\n#([1-9][0-9]*)\\n"); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 69 | private static final char HASH_CHAR = '#'; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 70 | private static final char LF_CHAR = '\n'; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 71 | |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 72 | private OutputStreamWriter outputStream; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 73 | private final InputStream err; |
| 74 | private final InputStream in; |
| 75 | private NetconfDeviceInfo netconfDeviceInfo; |
| 76 | private NetconfSessionDelegate sessionDelegate; |
| 77 | private NetconfMessageState state; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 78 | private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners |
| 79 | = Lists.newCopyOnWriteArrayList(); |
| 80 | private boolean enableNotifications = true; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 81 | private Map<Integer, CompletableFuture<String>> replies; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 82 | |
| 83 | public NetconfStreamThread(final InputStream in, final OutputStream out, |
| 84 | final InputStream err, NetconfDeviceInfo deviceInfo, |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 85 | NetconfSessionDelegate delegate, |
| 86 | Map<Integer, CompletableFuture<String>> replies) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 87 | this.in = in; |
| 88 | this.err = err; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 89 | outputStream = new OutputStreamWriter(out, StandardCharsets.UTF_8); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 90 | netconfDeviceInfo = deviceInfo; |
| 91 | state = NetconfMessageState.NO_MATCHING_PATTERN; |
| 92 | sessionDelegate = delegate; |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 93 | this.replies = replies; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 94 | log.debug("Stream thread for device {} session started", deviceInfo); |
| 95 | start(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<String> sendMessage(String request) { |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 100 | Optional<Integer> messageId = getMsgId(request); |
| 101 | return sendMessage(request, messageId.get()); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public CompletableFuture<String> sendMessage(String request, int messageId) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 106 | log.debug("Sending message {} to device {}", request, netconfDeviceInfo); |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 107 | CompletableFuture<String> cf = new CompletableFuture<>(); |
| 108 | replies.put(messageId, cf); |
| 109 | |
| 110 | synchronized (outputStream) { |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 111 | try { |
| 112 | outputStream.write(request); |
| 113 | outputStream.flush(); |
| 114 | } catch (IOException e) { |
| 115 | log.error("Writing to {} failed", netconfDeviceInfo, e); |
| 116 | cf.completeExceptionally(e); |
| 117 | } |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return cf; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public enum NetconfMessageState { |
| 124 | |
| 125 | NO_MATCHING_PATTERN { |
| 126 | @Override |
| 127 | NetconfMessageState evaluateChar(char c) { |
| 128 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 129 | return FIRST_BRACKET; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 130 | } else if (c == '\n') { |
| 131 | return FIRST_LF; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 132 | } else { |
| 133 | return this; |
| 134 | } |
| 135 | } |
| 136 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 137 | FIRST_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 138 | @Override |
| 139 | NetconfMessageState evaluateChar(char c) { |
| 140 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 141 | return SECOND_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 142 | } else { |
| 143 | return NO_MATCHING_PATTERN; |
| 144 | } |
| 145 | } |
| 146 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 147 | SECOND_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 148 | @Override |
| 149 | NetconfMessageState evaluateChar(char c) { |
| 150 | if (c == '>') { |
| 151 | return FIRST_BIGGER; |
| 152 | } else { |
| 153 | return NO_MATCHING_PATTERN; |
| 154 | } |
| 155 | } |
| 156 | }, |
| 157 | FIRST_BIGGER { |
| 158 | @Override |
| 159 | NetconfMessageState evaluateChar(char c) { |
| 160 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 161 | return THIRD_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 162 | } else { |
| 163 | return NO_MATCHING_PATTERN; |
| 164 | } |
| 165 | } |
| 166 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 167 | THIRD_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 168 | @Override |
| 169 | NetconfMessageState evaluateChar(char c) { |
| 170 | if (c == ']') { |
| 171 | return ENDING_BIGGER; |
| 172 | } else { |
| 173 | return NO_MATCHING_PATTERN; |
| 174 | } |
| 175 | } |
| 176 | }, |
| 177 | ENDING_BIGGER { |
| 178 | @Override |
| 179 | NetconfMessageState evaluateChar(char c) { |
| 180 | if (c == '>') { |
| 181 | return END_PATTERN; |
| 182 | } else { |
| 183 | return NO_MATCHING_PATTERN; |
| 184 | } |
| 185 | } |
| 186 | }, |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 187 | FIRST_LF { |
| 188 | @Override |
| 189 | NetconfMessageState evaluateChar(char c) { |
| 190 | if (c == '#') { |
| 191 | return FIRST_HASH; |
| 192 | } else if (c == ']') { |
| 193 | return FIRST_BRACKET; |
| 194 | } else if (c == '\n') { |
| 195 | return this; |
| 196 | } else { |
| 197 | return NO_MATCHING_PATTERN; |
| 198 | } |
| 199 | } |
| 200 | }, |
| 201 | FIRST_HASH { |
| 202 | @Override |
| 203 | NetconfMessageState evaluateChar(char c) { |
| 204 | if (c == '#') { |
| 205 | return SECOND_HASH; |
| 206 | } else { |
| 207 | return NO_MATCHING_PATTERN; |
| 208 | } |
| 209 | } |
| 210 | }, |
| 211 | SECOND_HASH { |
| 212 | @Override |
| 213 | NetconfMessageState evaluateChar(char c) { |
| 214 | if (c == '\n') { |
| 215 | return END_CHUNKED_PATTERN; |
| 216 | } else { |
| 217 | return NO_MATCHING_PATTERN; |
| 218 | } |
| 219 | } |
| 220 | }, |
| 221 | END_CHUNKED_PATTERN { |
| 222 | @Override |
| 223 | NetconfMessageState evaluateChar(char c) { |
| 224 | return NO_MATCHING_PATTERN; |
| 225 | } |
| 226 | }, |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 227 | END_PATTERN { |
| 228 | @Override |
| 229 | NetconfMessageState evaluateChar(char c) { |
| 230 | return NO_MATCHING_PATTERN; |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | abstract NetconfMessageState evaluateChar(char c); |
| 235 | } |
| 236 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 237 | @Override |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 238 | public void run() { |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 239 | BufferedReader bufferReader = null; |
| 240 | while (bufferReader == null) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 241 | try { |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 242 | bufferReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); |
| 243 | } catch (UnsupportedEncodingException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 244 | throw new IllegalStateException(e); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | try { |
| 249 | boolean socketClosed = false; |
| 250 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 251 | while (!socketClosed && !this.isInterrupted()) { |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 252 | int cInt = bufferReader.read(); |
| 253 | if (cInt == -1) { |
| 254 | log.debug("Netconf device {} sent error char in session," + |
Andrea Campanella | 59227c3 | 2019-01-07 14:52:35 +0100 | [diff] [blame] | 255 | " will need to be reopened", netconfDeviceInfo); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 256 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 257 | NetconfDeviceOutputEvent.Type.SESSION_CLOSED, |
| 258 | null, null, Optional.of(-1), netconfDeviceInfo); |
| 259 | netconfDeviceEventListeners.forEach( |
| 260 | listener -> listener.event(event)); |
| 261 | socketClosed = true; |
| 262 | log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo); |
| 263 | } |
| 264 | char c = (char) cInt; |
| 265 | state = state.evaluateChar(c); |
| 266 | deviceReplyBuilder.append(c); |
| 267 | if (state == NetconfMessageState.END_PATTERN) { |
| 268 | String deviceReply = deviceReplyBuilder.toString(); |
| 269 | if (deviceReply.equals(END_PATTERN)) { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 270 | socketClosed = true; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 271 | close(deviceReply); |
| 272 | } else { |
| 273 | deviceReply = deviceReply.replace(END_PATTERN, ""); |
| 274 | dealWithReply(deviceReply); |
| 275 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 276 | } |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 277 | } else if (state == NetconfMessageState.END_CHUNKED_PATTERN) { |
| 278 | String deviceReply = deviceReplyBuilder.toString(); |
| 279 | if (!validateChunkedFraming(deviceReply)) { |
| 280 | log.debug("Netconf device {} send badly framed message {}", |
| 281 | netconfDeviceInfo, deviceReply); |
| 282 | socketClosed = true; |
| 283 | close(deviceReply); |
| 284 | } else { |
| 285 | deviceReply = deviceReply.replaceAll(MSGLEN_REGEX_PATTERN, ""); |
| 286 | deviceReply = deviceReply.replaceAll(CHUNKED_END_REGEX_PATTERN, ""); |
| 287 | dealWithReply(deviceReply); |
| 288 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 291 | } |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 292 | } catch (ClosedByInterruptException i) { |
| 293 | log.debug("Connection to device {} was terminated on request", netconfDeviceInfo.toString()); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 294 | } catch (IOException e) { |
| 295 | log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 296 | throw new IllegalStateException(new NetconfException("Error in reading from the session for device {}" + |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 297 | netconfDeviceInfo, e)); |
| 298 | //TODO should we send a socket closed message to listeners ? |
| 299 | } |
| 300 | } |
| 301 | |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 302 | public void close() { |
| 303 | close("on request"); |
| 304 | } |
| 305 | |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 306 | private void close(String deviceReply) { |
| 307 | log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", |
| 308 | netconfDeviceInfo, deviceReply); |
| 309 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 310 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
| 311 | null, null, Optional.of(-1), netconfDeviceInfo); |
| 312 | netconfDeviceEventListeners.forEach( |
| 313 | listener -> listener.event(event)); |
| 314 | this.interrupt(); |
| 315 | } |
| 316 | |
| 317 | private void dealWithReply(String deviceReply) { |
| 318 | if (deviceReply.contains(RPC_REPLY) || |
| 319 | deviceReply.contains(RPC_ERROR) || |
| 320 | deviceReply.contains(HELLO)) { |
| 321 | log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", |
| 322 | netconfDeviceInfo, getMsgId(deviceReply), deviceReply); |
| 323 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 324 | NetconfDeviceOutputEvent.Type.DEVICE_REPLY, |
| 325 | null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); |
| 326 | sessionDelegate.notify(event); |
| 327 | netconfDeviceEventListeners.forEach( |
| 328 | listener -> listener.event(event)); |
| 329 | } else if (deviceReply.contains(NOTIFICATION_LABEL)) { |
| 330 | log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", |
| 331 | netconfDeviceInfo, enableNotifications, |
| 332 | getMsgId(deviceReply), deviceReply); |
| 333 | if (enableNotifications) { |
| 334 | log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size()); |
| 335 | final String finalDeviceReply = deviceReply; |
| 336 | netconfDeviceEventListeners.forEach( |
| 337 | listener -> listener.event(new NetconfDeviceOutputEvent( |
| 338 | NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, |
| 339 | null, finalDeviceReply, getMsgId(finalDeviceReply), |
| 340 | netconfDeviceInfo))); |
| 341 | } |
| 342 | } else { |
| 343 | log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static boolean validateChunkedFraming(String reply) { |
| 348 | Matcher matcher = CHUNKED_FRAMING_PATTERN.matcher(reply); |
| 349 | if (!matcher.matches()) { |
| 350 | log.debug("Error on reply {}", reply); |
| 351 | return false; |
| 352 | } |
| 353 | Matcher chunkM = CHUNKED_SIZE_PATTERN.matcher(reply); |
| 354 | List<MatchResult> chunks = new ArrayList<>(); |
| 355 | String chunkdataStr = ""; |
| 356 | while (chunkM.find()) { |
| 357 | chunks.add(chunkM.toMatchResult()); |
| 358 | // extract chunk-data (and later) in bytes |
| 359 | int bytes = Integer.parseInt(chunkM.group(1)); |
| 360 | byte[] chunkdata = reply.substring(chunkM.end()).getBytes(StandardCharsets.UTF_8); |
| 361 | if (bytes > chunkdata.length) { |
| 362 | log.debug("Error on reply - wrong chunk size {}", reply); |
| 363 | return false; |
| 364 | } |
| 365 | //check if after chunk-size bytes there is next chunk or message ending |
| 366 | if (chunkdata[bytes] != LF_CHAR || chunkdata[bytes + 1] != HASH_CHAR) { |
| 367 | log.debug("Error on reply - wrong chunk size {}", reply); |
| 368 | return false; |
| 369 | } |
| 370 | // convert (only) chunk-data part into String |
| 371 | chunkdataStr = new String(chunkdata, 0, bytes, StandardCharsets.UTF_8); |
| 372 | // skip chunk-data part from next match |
| 373 | chunkM.region(chunkM.end() + chunkdataStr.length(), reply.length()); |
| 374 | } |
| 375 | if (!"\n##\n".equals(reply.substring(chunks.get(chunks.size() - 1).end() + chunkdataStr.length()))) { |
| 376 | log.debug("Error on reply {}", reply); |
| 377 | return false; |
| 378 | } |
| 379 | return true; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame] | 382 | protected static Optional<Integer> getMsgId(String reply) { |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 383 | Matcher matcher = MSGID_PATTERN.matcher(reply); |
| 384 | if (matcher.find()) { |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 385 | try { |
| 386 | return Optional.of(Integer.valueOf(matcher.group(1))); |
| 387 | } catch (NumberFormatException e) { |
| 388 | log.warn("Failed to parse message-id from {}", matcher.group(), e); |
| 389 | } |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 390 | } |
| 391 | if (reply.contains(HELLO)) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 392 | return Optional.of(-1); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 393 | } |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 394 | return Optional.empty(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 397 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 398 | public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 399 | if (!netconfDeviceEventListeners.contains(listener)) { |
| 400 | netconfDeviceEventListeners.add(listener); |
| 401 | } |
| 402 | } |
| 403 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 404 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 405 | public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 406 | netconfDeviceEventListeners.remove(listener); |
| 407 | } |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 408 | |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 409 | @Override |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 410 | public void setEnableNotifications(boolean enableNotifications) { |
| 411 | this.enableNotifications = enableNotifications; |
| 412 | } |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 413 | } |