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