blob: 184a074271eba8b48d39591865204d8fd1456729 [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;
Andreas Papazoisd4712e22016-02-10 15:59:55 +020035import java.util.Optional;
Andrea Campanella101417d2015-12-11 17:58:07 -080036import java.util.concurrent.CompletableFuture;
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -050037import java.util.regex.Matcher;
38import java.util.regex.Pattern;
Andrea Campanella101417d2015-12-11 17:58:07 -080039
40/**
41 * Thread that gets spawned each time a session is established and handles all the input
42 * and output from the session's streams to and from the NETCONF device the session is
43 * established with.
44 */
45public class NetconfStreamThread extends Thread implements NetconfStreamHandler {
46
47 private static final Logger log = LoggerFactory
48 .getLogger(NetconfStreamThread.class);
Andrea Campanella1311ea02016-03-04 17:51:25 -080049 private static final String HELLO = "<hello";
Andrea Campanella101417d2015-12-11 17:58:07 -080050 private static final String END_PATTERN = "]]>]]>";
51 private static final String RPC_REPLY = "rpc-reply";
52 private static final String RPC_ERROR = "rpc-error";
heisenbergb7017d72016-04-13 02:16:07 -070053 private static final String NOTIFICATION_LABEL = "<notification";
Andreas Papazoisd4712e22016-02-10 15:59:55 +020054 private static final String MESSAGE_ID = "message-id=";
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -050055 private static final Pattern MSGID_PATTERN = Pattern.compile(MESSAGE_ID + "\"(\\d+)\"");
Andrea Campanella101417d2015-12-11 17:58:07 -080056
Andrea Campanellab029b9e2016-01-29 11:05:36 -080057 private PrintWriter outputStream;
58 private final InputStream err;
59 private final InputStream in;
60 private NetconfDeviceInfo netconfDeviceInfo;
61 private NetconfSessionDelegate sessionDelegate;
62 private NetconfMessageState state;
helenyrwu0407c642016-06-09 12:01:30 -070063 private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
64 = Lists.newCopyOnWriteArrayList();
65 private boolean enableNotifications = true;
Andrea Campanella101417d2015-12-11 17:58:07 -080066
67 public NetconfStreamThread(final InputStream in, final OutputStream out,
68 final InputStream err, NetconfDeviceInfo deviceInfo,
69 NetconfSessionDelegate delegate) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080070 this.in = in;
71 this.err = err;
Andrea Campanella101417d2015-12-11 17:58:07 -080072 outputStream = new PrintWriter(out);
73 netconfDeviceInfo = deviceInfo;
74 state = NetconfMessageState.NO_MATCHING_PATTERN;
75 sessionDelegate = delegate;
76 log.debug("Stream thread for device {} session started", deviceInfo);
77 start();
78 }
79
80 @Override
81 public CompletableFuture<String> sendMessage(String request) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080082 log.debug("Sending message {} to device {}", request, netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -080083 outputStream.print(request);
84 outputStream.flush();
85 return new CompletableFuture<>();
86 }
87
88 public enum NetconfMessageState {
89
90 NO_MATCHING_PATTERN {
91 @Override
92 NetconfMessageState evaluateChar(char c) {
93 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -080094 return FIRST_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -080095 } else {
96 return this;
97 }
98 }
99 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800100 FIRST_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800101 @Override
102 NetconfMessageState evaluateChar(char c) {
103 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800104 return SECOND_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800105 } else {
106 return NO_MATCHING_PATTERN;
107 }
108 }
109 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800110 SECOND_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800111 @Override
112 NetconfMessageState evaluateChar(char c) {
113 if (c == '>') {
114 return FIRST_BIGGER;
115 } else {
116 return NO_MATCHING_PATTERN;
117 }
118 }
119 },
120 FIRST_BIGGER {
121 @Override
122 NetconfMessageState evaluateChar(char c) {
123 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800124 return THIRD_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800125 } else {
126 return NO_MATCHING_PATTERN;
127 }
128 }
129 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800130 THIRD_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800131 @Override
132 NetconfMessageState evaluateChar(char c) {
133 if (c == ']') {
134 return ENDING_BIGGER;
135 } else {
136 return NO_MATCHING_PATTERN;
137 }
138 }
139 },
140 ENDING_BIGGER {
141 @Override
142 NetconfMessageState evaluateChar(char c) {
143 if (c == '>') {
144 return END_PATTERN;
145 } else {
146 return NO_MATCHING_PATTERN;
147 }
148 }
149 },
150 END_PATTERN {
151 @Override
152 NetconfMessageState evaluateChar(char c) {
153 return NO_MATCHING_PATTERN;
154 }
155 };
156
157 abstract NetconfMessageState evaluateChar(char c);
158 }
159
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800160 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800161 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800162 try {
163 boolean socketClosed = false;
164 StringBuilder deviceReplyBuilder = new StringBuilder();
165 while (!socketClosed) {
166 int cInt = bufferReader.read();
167 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800168 log.debug("Netconf device {} sent error char in session," +
169 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella1311ea02016-03-04 17:51:25 -0800170 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
171 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
172 null, null, Optional.of(-1), netconfDeviceInfo);
173 netconfDeviceEventListeners.forEach(
174 listener -> listener.event(event));
175 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900176 log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800177 }
178 char c = (char) cInt;
179 state = state.evaluateChar(c);
180 deviceReplyBuilder.append(c);
181 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800182 String deviceReply = deviceReplyBuilder.toString();
183 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800184 socketClosed = true;
xueliangcf527d12016-11-07 14:31:06 +0900185 log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}",
186 netconfDeviceInfo, deviceReply);
Andrea Campanella101417d2015-12-11 17:58:07 -0800187 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800188 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200189 null, null, Optional.of(-1), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800190 netconfDeviceEventListeners.forEach(
191 listener -> listener.event(event));
Andrea Campanella86294db2016-03-07 11:42:49 -0800192 this.interrupt();
Andrea Campanella101417d2015-12-11 17:58:07 -0800193 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800194 deviceReply = deviceReply.replace(END_PATTERN, "");
195 if (deviceReply.contains(RPC_REPLY) ||
196 deviceReply.contains(RPC_ERROR) ||
197 deviceReply.contains(HELLO)) {
xueliangcf527d12016-11-07 14:31:06 +0900198 log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}",
199 netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800200 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
201 NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
202 null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
203 sessionDelegate.notify(event);
204 netconfDeviceEventListeners.forEach(
205 listener -> listener.event(event));
206 } else if (deviceReply.contains(NOTIFICATION_LABEL)) {
xueliangcf527d12016-11-07 14:31:06 +0900207 log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}",
208 netconfDeviceInfo, enableNotifications,
209 getMsgId(deviceReply), deviceReply);
helenyrwu0407c642016-06-09 12:01:30 -0700210 if (enableNotifications) {
211 final String finalDeviceReply = deviceReply;
212 netconfDeviceEventListeners.forEach(
213 listener -> listener.event(new NetconfDeviceOutputEvent(
214 NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
215 null, finalDeviceReply, getMsgId(finalDeviceReply),
216 netconfDeviceInfo)));
217 }
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800218 } else {
xueliangcf527d12016-11-07 14:31:06 +0900219 log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800220 }
221 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800222 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800223 }
224 }
225 } catch (IOException e) {
Andrea Campanellad264b492016-03-01 09:46:06 -0800226 log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
Andrea Campanella101417d2015-12-11 17:58:07 -0800227 throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" +
228 netconfDeviceInfo, e));
229 //TODO should we send a socket closed message to listeners ?
230 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800231 }
232
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200233 private static Optional<Integer> getMsgId(String reply) {
Konstantinos Kanonakis1b8b5592016-09-09 14:34:37 -0500234 Matcher matcher = MSGID_PATTERN.matcher(reply);
235 if (matcher.find()) {
236 Integer messageId = Integer.parseInt(matcher.group(1));
237 Preconditions.checkNotNull(messageId, "Error in retrieving the message id");
238 return Optional.of(messageId);
239 }
240 if (reply.contains(HELLO)) {
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200241 return Optional.of(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800242 }
Andrea Campanella1311ea02016-03-04 17:51:25 -0800243 return Optional.empty();
Andrea Campanella101417d2015-12-11 17:58:07 -0800244 }
245
246 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
247 if (!netconfDeviceEventListeners.contains(listener)) {
248 netconfDeviceEventListeners.add(listener);
249 }
250 }
251
252 public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) {
253 netconfDeviceEventListeners.remove(listener);
254 }
helenyrwu0407c642016-06-09 12:01:30 -0700255
256 public void setEnableNotifications(boolean enableNotifications) {
257 this.enableNotifications = enableNotifications;
258 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800259}