blob: 4d713f60313fc22ad6197a5446c01757963c308f [file] [log] [blame]
Andrea Campanella101417d2015-12-11 17:58:07 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Andrea Campanella101417d2015-12-11 17:58:07 -08003 *
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
17package org.onosproject.netconf.ctl;
18
19import com.google.common.base.Preconditions;
20import com.google.common.collect.Lists;
21import org.onosproject.netconf.NetconfDeviceInfo;
22import org.onosproject.netconf.NetconfDeviceOutputEvent;
23import org.onosproject.netconf.NetconfDeviceOutputEventListener;
24import org.onosproject.netconf.NetconfException;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.io.BufferedReader;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.InputStreamReader;
32import java.io.OutputStream;
33import java.io.PrintWriter;
34import java.util.List;
Sean Condond2c8d472017-02-17 17:09:39 +000035import java.util.Map;
Andreas Papazoisd4712e22016-02-10 15:59:55 +020036import java.util.Optional;
Andrea Campanella101417d2015-12-11 17:58:07 -080037import java.util.concurrent.CompletableFuture;
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -050038import java.util.regex.Matcher;
39import java.util.regex.Pattern;
Andrea Campanella101417d2015-12-11 17:58:07 -080040
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 */
46public class NetconfStreamThread extends Thread implements NetconfStreamHandler {
47
48 private static final Logger log = LoggerFactory
49 .getLogger(NetconfStreamThread.class);
Andrea Campanella1311ea02016-03-04 17:51:25 -080050 private static final String HELLO = "<hello";
Andrea Campanella101417d2015-12-11 17:58:07 -080051 private static final String END_PATTERN = "]]>]]>";
52 private static final String RPC_REPLY = "rpc-reply";
53 private static final String RPC_ERROR = "rpc-error";
heisenbergb7017d72016-04-13 02:16:07 -070054 private static final String NOTIFICATION_LABEL = "<notification";
Andreas Papazoisd4712e22016-02-10 15:59:55 +020055 private static final String MESSAGE_ID = "message-id=";
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -050056 private static final Pattern MSGID_PATTERN = Pattern.compile(MESSAGE_ID + "\"(\\d+)\"");
Andrea Campanella101417d2015-12-11 17:58:07 -080057
Andrea Campanellab029b9e2016-01-29 11:05:36 -080058 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;
helenyrwu0407c642016-06-09 12:01:30 -070064 private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
65 = Lists.newCopyOnWriteArrayList();
66 private boolean enableNotifications = true;
Sean Condond2c8d472017-02-17 17:09:39 +000067 private Map<Integer, CompletableFuture<String>> replies;
Andrea Campanella101417d2015-12-11 17:58:07 -080068
69 public NetconfStreamThread(final InputStream in, final OutputStream out,
70 final InputStream err, NetconfDeviceInfo deviceInfo,
Sean Condond2c8d472017-02-17 17:09:39 +000071 NetconfSessionDelegate delegate,
72 Map<Integer, CompletableFuture<String>> replies) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080073 this.in = in;
74 this.err = err;
Andrea Campanella101417d2015-12-11 17:58:07 -080075 outputStream = new PrintWriter(out);
76 netconfDeviceInfo = deviceInfo;
77 state = NetconfMessageState.NO_MATCHING_PATTERN;
78 sessionDelegate = delegate;
Sean Condond2c8d472017-02-17 17:09:39 +000079 this.replies = replies;
Andrea Campanella101417d2015-12-11 17:58:07 -080080 log.debug("Stream thread for device {} session started", deviceInfo);
81 start();
82 }
83
84 @Override
85 public CompletableFuture<String> sendMessage(String request) {
Sean Condond2c8d472017-02-17 17:09:39 +000086 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 Campanellab029b9e2016-01-29 11:05:36 -080092 log.debug("Sending message {} to device {}", request, netconfDeviceInfo);
Sean Condond2c8d472017-02-17 17:09:39 +000093 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 Campanella101417d2015-12-11 17:58:07 -0800102 }
103
104 public enum NetconfMessageState {
105
106 NO_MATCHING_PATTERN {
107 @Override
108 NetconfMessageState evaluateChar(char c) {
109 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800110 return FIRST_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800111 } else {
112 return this;
113 }
114 }
115 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800116 FIRST_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800117 @Override
118 NetconfMessageState evaluateChar(char c) {
119 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800120 return SECOND_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800121 } else {
122 return NO_MATCHING_PATTERN;
123 }
124 }
125 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800126 SECOND_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800127 @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 Campanella1311ea02016-03-04 17:51:25 -0800140 return THIRD_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800141 } else {
142 return NO_MATCHING_PATTERN;
143 }
144 }
145 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800146 THIRD_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800147 @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 Campanellab029b9e2016-01-29 11:05:36 -0800176 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800177 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800178 try {
179 boolean socketClosed = false;
180 StringBuilder deviceReplyBuilder = new StringBuilder();
181 while (!socketClosed) {
182 int cInt = bufferReader.read();
183 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800184 log.debug("Netconf device {} sent error char in session," +
185 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella1311ea02016-03-04 17:51:25 -0800186 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;
xueliangcf527d12016-11-07 14:31:06 +0900192 log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800193 }
194 char c = (char) cInt;
195 state = state.evaluateChar(c);
196 deviceReplyBuilder.append(c);
197 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800198 String deviceReply = deviceReplyBuilder.toString();
199 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800200 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900201 log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
202 netconfDeviceInfo, deviceReply);
Andrea Campanella101417d2015-12-11 17:58:07 -0800203 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800204 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200205 null, null, Optional.of(-1), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800206 netconfDeviceEventListeners.forEach(
207 listener -> listener.event(event));
Andrea Campanella86294db2016-03-07 11:42:49 -0800208 this.interrupt();
Andrea Campanella101417d2015-12-11 17:58:07 -0800209 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800210 deviceReply = deviceReply.replace(END_PATTERN, "");
211 if (deviceReply.contains(RPC_REPLY) ||
212 deviceReply.contains(RPC_ERROR) ||
213 deviceReply.contains(HELLO)) {
xueliangcf527d12016-11-07 14:31:06 +0900214 log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}",
215 netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800216 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)) {
xueliangcf527d12016-11-07 14:31:06 +0900223 log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}",
224 netconfDeviceInfo, enableNotifications,
225 getMsgId(deviceReply), deviceReply);
helenyrwu0407c642016-06-09 12:01:30 -0700226 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 Campanellab029b9e2016-01-29 11:05:36 -0800234 } else {
xueliangcf527d12016-11-07 14:31:06 +0900235 log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800236 }
237 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800238 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800239 }
240 }
241 } catch (IOException e) {
Andrea Campanellad264b492016-03-01 09:46:06 -0800242 log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
Andrea Campanella101417d2015-12-11 17:58:07 -0800243 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 Campanella101417d2015-12-11 17:58:07 -0800247 }
248
Sean Condond2c8d472017-02-17 17:09:39 +0000249 protected static Optional<Integer> getMsgId(String reply) {
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -0500250 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 Papazoisd4712e22016-02-10 15:59:55 +0200257 return Optional.of(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800258 }
Andrea Campanella1311ea02016-03-04 17:51:25 -0800259 return Optional.empty();
Andrea Campanella101417d2015-12-11 17:58:07 -0800260 }
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 }
helenyrwu0407c642016-06-09 12:01:30 -0700271
272 public void setEnableNotifications(boolean enableNotifications) {
273 this.enableNotifications = enableNotifications;
274 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800275}