blob: 212a4e68ae4b2c745747a1fdf1610da0f9f8823e [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
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
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700176 @Override
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800177 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800178 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800179 try {
180 boolean socketClosed = false;
181 StringBuilder deviceReplyBuilder = new StringBuilder();
182 while (!socketClosed) {
183 int cInt = bufferReader.read();
184 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800185 log.debug("Netconf device {} sent error char in session," +
186 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella1311ea02016-03-04 17:51:25 -0800187 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellac3627842017-04-04 18:06:54 +0200188 NetconfDeviceOutputEvent.Type.SESSION_CLOSED,
Andrea Campanella1311ea02016-03-04 17:51:25 -0800189 null, null, Optional.of(-1), netconfDeviceInfo);
190 netconfDeviceEventListeners.forEach(
191 listener -> listener.event(event));
192 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900193 log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800194 }
195 char c = (char) cInt;
196 state = state.evaluateChar(c);
197 deviceReplyBuilder.append(c);
198 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800199 String deviceReply = deviceReplyBuilder.toString();
200 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800201 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900202 log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
203 netconfDeviceInfo, deviceReply);
Andrea Campanella101417d2015-12-11 17:58:07 -0800204 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800205 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200206 null, null, Optional.of(-1), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800207 netconfDeviceEventListeners.forEach(
208 listener -> listener.event(event));
Andrea Campanella86294db2016-03-07 11:42:49 -0800209 this.interrupt();
Andrea Campanella101417d2015-12-11 17:58:07 -0800210 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800211 deviceReply = deviceReply.replace(END_PATTERN, "");
212 if (deviceReply.contains(RPC_REPLY) ||
213 deviceReply.contains(RPC_ERROR) ||
214 deviceReply.contains(HELLO)) {
xueliangcf527d12016-11-07 14:31:06 +0900215 log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}",
216 netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800217 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
218 NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
219 null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
220 sessionDelegate.notify(event);
221 netconfDeviceEventListeners.forEach(
222 listener -> listener.event(event));
223 } else if (deviceReply.contains(NOTIFICATION_LABEL)) {
xueliangcf527d12016-11-07 14:31:06 +0900224 log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}",
225 netconfDeviceInfo, enableNotifications,
226 getMsgId(deviceReply), deviceReply);
helenyrwu0407c642016-06-09 12:01:30 -0700227 if (enableNotifications) {
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700228 log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size());
helenyrwu0407c642016-06-09 12:01:30 -0700229 final String finalDeviceReply = deviceReply;
230 netconfDeviceEventListeners.forEach(
231 listener -> listener.event(new NetconfDeviceOutputEvent(
232 NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
233 null, finalDeviceReply, getMsgId(finalDeviceReply),
234 netconfDeviceInfo)));
235 }
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800236 } else {
xueliangcf527d12016-11-07 14:31:06 +0900237 log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800238 }
239 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800240 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800241 }
242 }
243 } catch (IOException e) {
Andrea Campanellad264b492016-03-01 09:46:06 -0800244 log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
Andrea Campanella101417d2015-12-11 17:58:07 -0800245 throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" +
246 netconfDeviceInfo, e));
247 //TODO should we send a socket closed message to listeners ?
248 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800249 }
250
Sean Condond2c8d472017-02-17 17:09:39 +0000251 protected static Optional<Integer> getMsgId(String reply) {
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -0500252 Matcher matcher = MSGID_PATTERN.matcher(reply);
253 if (matcher.find()) {
254 Integer messageId = Integer.parseInt(matcher.group(1));
255 Preconditions.checkNotNull(messageId, "Error in retrieving the message id");
256 return Optional.of(messageId);
257 }
258 if (reply.contains(HELLO)) {
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200259 return Optional.of(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800260 }
Andrea Campanella1311ea02016-03-04 17:51:25 -0800261 return Optional.empty();
Andrea Campanella101417d2015-12-11 17:58:07 -0800262 }
263
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700264 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -0800265 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
266 if (!netconfDeviceEventListeners.contains(listener)) {
267 netconfDeviceEventListeners.add(listener);
268 }
269 }
270
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700271 @Override
Andrea Campanella101417d2015-12-11 17:58:07 -0800272 public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) {
273 netconfDeviceEventListeners.remove(listener);
274 }
helenyrwu0407c642016-06-09 12:01:30 -0700275
Yuta HIGUCHI0454d702017-03-17 10:08:38 -0700276 @Override
helenyrwu0407c642016-06-09 12:01:30 -0700277 public void setEnableNotifications(boolean enableNotifications) {
278 this.enableNotifications = enableNotifications;
279 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800280}