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 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 22 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 23 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 24 | import org.onosproject.netconf.NetconfException; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.io.BufferedReader; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.io.OutputStream; |
| 33 | import java.io.PrintWriter; |
| 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 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 58 | private PrintWriter outputStream; |
| 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; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 75 | outputStream = new PrintWriter(out); |
| 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) { |
| 97 | outputStream.print(request); |
| 98 | outputStream.flush(); |
| 99 | } |
| 100 | |
| 101 | return cf; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public enum NetconfMessageState { |
| 105 | |
| 106 | NO_MATCHING_PATTERN { |
| 107 | @Override |
| 108 | NetconfMessageState evaluateChar(char c) { |
| 109 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 110 | return FIRST_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 111 | } else { |
| 112 | return this; |
| 113 | } |
| 114 | } |
| 115 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 116 | FIRST_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 117 | @Override |
| 118 | NetconfMessageState evaluateChar(char c) { |
| 119 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 120 | return SECOND_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 121 | } else { |
| 122 | return NO_MATCHING_PATTERN; |
| 123 | } |
| 124 | } |
| 125 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 126 | SECOND_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 127 | @Override |
| 128 | NetconfMessageState evaluateChar(char c) { |
| 129 | if (c == '>') { |
| 130 | return FIRST_BIGGER; |
| 131 | } else { |
| 132 | return NO_MATCHING_PATTERN; |
| 133 | } |
| 134 | } |
| 135 | }, |
| 136 | FIRST_BIGGER { |
| 137 | @Override |
| 138 | NetconfMessageState evaluateChar(char c) { |
| 139 | if (c == ']') { |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 140 | return THIRD_BRACKET; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 141 | } else { |
| 142 | return NO_MATCHING_PATTERN; |
| 143 | } |
| 144 | } |
| 145 | }, |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 146 | THIRD_BRACKET { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 147 | @Override |
| 148 | NetconfMessageState evaluateChar(char c) { |
| 149 | if (c == ']') { |
| 150 | return ENDING_BIGGER; |
| 151 | } else { |
| 152 | return NO_MATCHING_PATTERN; |
| 153 | } |
| 154 | } |
| 155 | }, |
| 156 | ENDING_BIGGER { |
| 157 | @Override |
| 158 | NetconfMessageState evaluateChar(char c) { |
| 159 | if (c == '>') { |
| 160 | return END_PATTERN; |
| 161 | } else { |
| 162 | return NO_MATCHING_PATTERN; |
| 163 | } |
| 164 | } |
| 165 | }, |
| 166 | END_PATTERN { |
| 167 | @Override |
| 168 | NetconfMessageState evaluateChar(char c) { |
| 169 | return NO_MATCHING_PATTERN; |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | abstract NetconfMessageState evaluateChar(char c); |
| 174 | } |
| 175 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 176 | public void run() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 177 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 178 | try { |
| 179 | boolean socketClosed = false; |
| 180 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
| 181 | while (!socketClosed) { |
| 182 | int cInt = bufferReader.read(); |
| 183 | if (cInt == -1) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 184 | log.debug("Netconf device {} sent error char in session," + |
| 185 | " will need to be reopend", netconfDeviceInfo); |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 186 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 187 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
| 188 | null, null, Optional.of(-1), netconfDeviceInfo); |
| 189 | netconfDeviceEventListeners.forEach( |
| 190 | listener -> listener.event(event)); |
| 191 | socketClosed = true; |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 192 | log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 193 | } |
| 194 | char c = (char) cInt; |
| 195 | state = state.evaluateChar(c); |
| 196 | deviceReplyBuilder.append(c); |
| 197 | if (state == NetconfMessageState.END_PATTERN) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 198 | String deviceReply = deviceReplyBuilder.toString(); |
| 199 | if (deviceReply.equals(END_PATTERN)) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 200 | socketClosed = true; |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 201 | log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", |
| 202 | netconfDeviceInfo, deviceReply); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 203 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 204 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 205 | null, null, Optional.of(-1), netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 206 | netconfDeviceEventListeners.forEach( |
| 207 | listener -> listener.event(event)); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 208 | this.interrupt(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 209 | } else { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 210 | deviceReply = deviceReply.replace(END_PATTERN, ""); |
| 211 | if (deviceReply.contains(RPC_REPLY) || |
| 212 | deviceReply.contains(RPC_ERROR) || |
| 213 | deviceReply.contains(HELLO)) { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 214 | log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", |
| 215 | netconfDeviceInfo, getMsgId(deviceReply), deviceReply); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 216 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 217 | NetconfDeviceOutputEvent.Type.DEVICE_REPLY, |
| 218 | null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); |
| 219 | sessionDelegate.notify(event); |
| 220 | netconfDeviceEventListeners.forEach( |
| 221 | listener -> listener.event(event)); |
| 222 | } else if (deviceReply.contains(NOTIFICATION_LABEL)) { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 223 | log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", |
| 224 | netconfDeviceInfo, enableNotifications, |
| 225 | getMsgId(deviceReply), deviceReply); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 226 | if (enableNotifications) { |
| 227 | final String finalDeviceReply = deviceReply; |
| 228 | netconfDeviceEventListeners.forEach( |
| 229 | listener -> listener.event(new NetconfDeviceOutputEvent( |
| 230 | NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, |
| 231 | null, finalDeviceReply, getMsgId(finalDeviceReply), |
| 232 | netconfDeviceInfo))); |
| 233 | } |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 234 | } else { |
xueliang | cf527d1 | 2016-11-07 14:31:06 +0900 | [diff] [blame] | 235 | log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 236 | } |
| 237 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 238 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } catch (IOException e) { |
Andrea Campanella | d264b49 | 2016-03-01 09:46:06 -0800 | [diff] [blame] | 242 | log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 243 | throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" + |
| 244 | netconfDeviceInfo, e)); |
| 245 | //TODO should we send a socket closed message to listeners ? |
| 246 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Sean Condon | d2c8d47 | 2017-02-17 17:09:39 +0000 | [diff] [blame^] | 249 | protected static Optional<Integer> getMsgId(String reply) { |
Konstantinos Kanonakis | 1b8b559 | 2016-09-09 14:34:37 -0500 | [diff] [blame] | 250 | Matcher matcher = MSGID_PATTERN.matcher(reply); |
| 251 | if (matcher.find()) { |
| 252 | Integer messageId = Integer.parseInt(matcher.group(1)); |
| 253 | Preconditions.checkNotNull(messageId, "Error in retrieving the message id"); |
| 254 | return Optional.of(messageId); |
| 255 | } |
| 256 | if (reply.contains(HELLO)) { |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 257 | return Optional.of(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 258 | } |
Andrea Campanella | 1311ea0 | 2016-03-04 17:51:25 -0800 | [diff] [blame] | 259 | return Optional.empty(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 263 | if (!netconfDeviceEventListeners.contains(listener)) { |
| 264 | netconfDeviceEventListeners.add(listener); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 269 | netconfDeviceEventListeners.remove(listener); |
| 270 | } |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 271 | |
| 272 | public void setEnableNotifications(boolean enableNotifications) { |
| 273 | this.enableNotifications = enableNotifications; |
| 274 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 275 | } |