blob: 6b231b3af61570305d8c70be26da92f2bfafb33b [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
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070017package org.onosproject.netconf.ctl.impl;
Andrea Campanella101417d2015-12-11 17:58:07 -080018
Andrea Campanella101417d2015-12-11 17:58:07 -080019import com.google.common.collect.Lists;
20import org.onosproject.netconf.NetconfDeviceInfo;
21import org.onosproject.netconf.NetconfDeviceOutputEvent;
22import org.onosproject.netconf.NetconfDeviceOutputEventListener;
23import org.onosproject.netconf.NetconfException;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.io.BufferedReader;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.io.OutputStream;
Sean Condon7347de92017-07-21 12:17:25 +010032import java.io.OutputStreamWriter;
33import java.nio.charset.StandardCharsets;
Andrea Campanella101417d2015-12-11 17:58:07 -080034import 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
Sean Condon7347de92017-07-21 12:17:25 +010058 private OutputStreamWriter outputStream;
Andrea Campanellab029b9e2016-01-29 11:05:36 -080059 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;
Sean Condon7347de92017-07-21 12:17:25 +010075 outputStream = new OutputStreamWriter(out, StandardCharsets.UTF_8);
Andrea Campanella101417d2015-12-11 17:58:07 -080076 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) {
Sean Condon7347de92017-07-21 12:17:25 +010097 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 Condond2c8d472017-02-17 17:09:39 +0000104 }
105
106 return cf;
Andrea Campanella101417d2015-12-11 17:58:07 -0800107 }
108
109 public enum NetconfMessageState {
110
111 NO_MATCHING_PATTERN {
112 @Override
113 NetconfMessageState evaluateChar(char c) {
114 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800115 return FIRST_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800116 } else {
117 return this;
118 }
119 }
120 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800121 FIRST_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800122 @Override
123 NetconfMessageState evaluateChar(char c) {
124 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800125 return SECOND_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800126 } else {
127 return NO_MATCHING_PATTERN;
128 }
129 }
130 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800131 SECOND_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800132 @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 Campanella1311ea02016-03-04 17:51:25 -0800145 return THIRD_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800146 } else {
147 return NO_MATCHING_PATTERN;
148 }
149 }
150 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800151 THIRD_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800152 @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 HIGUCHI0454d702017-03-17 10:08:38 -0700181 @Override
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800182 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800183 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800184 try {
185 boolean socketClosed = false;
186 StringBuilder deviceReplyBuilder = new StringBuilder();
187 while (!socketClosed) {
188 int cInt = bufferReader.read();
189 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800190 log.debug("Netconf device {} sent error char in session," +
191 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella1311ea02016-03-04 17:51:25 -0800192 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellac3627842017-04-04 18:06:54 +0200193 NetconfDeviceOutputEvent.Type.SESSION_CLOSED,
Andrea Campanella1311ea02016-03-04 17:51:25 -0800194 null, null, Optional.of(-1), netconfDeviceInfo);
195 netconfDeviceEventListeners.forEach(
196 listener -> listener.event(event));
197 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900198 log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800199 }
200 char c = (char) cInt;
201 state = state.evaluateChar(c);
202 deviceReplyBuilder.append(c);
203 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800204 String deviceReply = deviceReplyBuilder.toString();
205 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800206 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900207 log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
208 netconfDeviceInfo, deviceReply);
Andrea Campanella101417d2015-12-11 17:58:07 -0800209 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800210 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700211 null, null, Optional.of(-2), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800212 netconfDeviceEventListeners.forEach(
213 listener -> listener.event(event));
Andrea Campanella86294db2016-03-07 11:42:49 -0800214 this.interrupt();
Andrea Campanella101417d2015-12-11 17:58:07 -0800215 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800216 deviceReply = deviceReply.replace(END_PATTERN, "");
217 if (deviceReply.contains(RPC_REPLY) ||
218 deviceReply.contains(RPC_ERROR) ||
219 deviceReply.contains(HELLO)) {
xueliangcf527d12016-11-07 14:31:06 +0900220 log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}",
221 netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800222 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)) {
xueliangcf527d12016-11-07 14:31:06 +0900229 log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}",
230 netconfDeviceInfo, enableNotifications,
231 getMsgId(deviceReply), deviceReply);
helenyrwu0407c642016-06-09 12:01:30 -0700232 if (enableNotifications) {
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700233 log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size());
helenyrwu0407c642016-06-09 12:01:30 -0700234 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 Campanellab029b9e2016-01-29 11:05:36 -0800241 } else {
xueliangcf527d12016-11-07 14:31:06 +0900242 log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800243 }
244 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800245 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800246 }
247 }
248 } catch (IOException e) {
Andrea Campanellad264b492016-03-01 09:46:06 -0800249 log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
Andrea Campanella101417d2015-12-11 17:58:07 -0800250 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 Campanella101417d2015-12-11 17:58:07 -0800254 }
255
Sean Condond2c8d472017-02-17 17:09:39 +0000256 protected static Optional<Integer> getMsgId(String reply) {
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -0500257 Matcher matcher = MSGID_PATTERN.matcher(reply);
258 if (matcher.find()) {
Sean Condon7347de92017-07-21 12:17:25 +0100259 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 Kanonakis1b8b5592016-09-09 14:34:37 -0500264 }
265 if (reply.contains(HELLO)) {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700266 return Optional.of(-1);
Andrea Campanella101417d2015-12-11 17:58:07 -0800267 }
Andrea Campanella1311ea02016-03-04 17:51:25 -0800268 return Optional.empty();
Andrea Campanella101417d2015-12-11 17:58:07 -0800269 }
270
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700271 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -0800272 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
273 if (!netconfDeviceEventListeners.contains(listener)) {
274 netconfDeviceEventListeners.add(listener);
275 }
276 }
277
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700278 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -0800279 public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) {
280 netconfDeviceEventListeners.remove(listener);
281 }
helenyrwu0407c642016-06-09 12:01:30 -0700282
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700283 @Override
helenyrwu0407c642016-06-09 12:01:30 -0700284 public void setEnableNotifications(boolean enableNotifications) {
285 this.enableNotifications = enableNotifications;
286 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800287}