blob: 307c917b1ff9d31ae79862a15c0bed12bb247701 [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;
37
38/**
39 * Thread that gets spawned each time a session is established and handles all the input
40 * and output from the session's streams to and from the NETCONF device the session is
41 * established with.
42 */
43public class NetconfStreamThread extends Thread implements NetconfStreamHandler {
44
45 private static final Logger log = LoggerFactory
46 .getLogger(NetconfStreamThread.class);
Andrea Campanella1311ea02016-03-04 17:51:25 -080047 private static final String HELLO = "<hello";
Andrea Campanella101417d2015-12-11 17:58:07 -080048 private static final String END_PATTERN = "]]>]]>";
49 private static final String RPC_REPLY = "rpc-reply";
50 private static final String RPC_ERROR = "rpc-error";
heisenbergb7017d72016-04-13 02:16:07 -070051 private static final String NOTIFICATION_LABEL = "<notification";
Andreas Papazoisd4712e22016-02-10 15:59:55 +020052 private static final String MESSAGE_ID = "message-id=";
Andrea Campanella101417d2015-12-11 17:58:07 -080053
Andrea Campanellab029b9e2016-01-29 11:05:36 -080054 private PrintWriter outputStream;
55 private final InputStream err;
56 private final InputStream in;
57 private NetconfDeviceInfo netconfDeviceInfo;
58 private NetconfSessionDelegate sessionDelegate;
59 private NetconfMessageState state;
helenyrwu0407c642016-06-09 12:01:30 -070060 private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
61 = Lists.newCopyOnWriteArrayList();
62 private boolean enableNotifications = true;
Andrea Campanella101417d2015-12-11 17:58:07 -080063
64 public NetconfStreamThread(final InputStream in, final OutputStream out,
65 final InputStream err, NetconfDeviceInfo deviceInfo,
66 NetconfSessionDelegate delegate) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080067 this.in = in;
68 this.err = err;
Andrea Campanella101417d2015-12-11 17:58:07 -080069 outputStream = new PrintWriter(out);
70 netconfDeviceInfo = deviceInfo;
71 state = NetconfMessageState.NO_MATCHING_PATTERN;
72 sessionDelegate = delegate;
73 log.debug("Stream thread for device {} session started", deviceInfo);
74 start();
75 }
76
77 @Override
78 public CompletableFuture<String> sendMessage(String request) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080079 log.debug("Sending message {} to device {}", request, netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -080080 outputStream.print(request);
81 outputStream.flush();
82 return new CompletableFuture<>();
83 }
84
85 public enum NetconfMessageState {
86
87 NO_MATCHING_PATTERN {
88 @Override
89 NetconfMessageState evaluateChar(char c) {
90 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -080091 return FIRST_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -080092 } else {
93 return this;
94 }
95 }
96 },
Andrea Campanella1311ea02016-03-04 17:51:25 -080097 FIRST_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -080098 @Override
99 NetconfMessageState evaluateChar(char c) {
100 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800101 return SECOND_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800102 } else {
103 return NO_MATCHING_PATTERN;
104 }
105 }
106 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800107 SECOND_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800108 @Override
109 NetconfMessageState evaluateChar(char c) {
110 if (c == '>') {
111 return FIRST_BIGGER;
112 } else {
113 return NO_MATCHING_PATTERN;
114 }
115 }
116 },
117 FIRST_BIGGER {
118 @Override
119 NetconfMessageState evaluateChar(char c) {
120 if (c == ']') {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800121 return THIRD_BRACKET;
Andrea Campanella101417d2015-12-11 17:58:07 -0800122 } else {
123 return NO_MATCHING_PATTERN;
124 }
125 }
126 },
Andrea Campanella1311ea02016-03-04 17:51:25 -0800127 THIRD_BRACKET {
Andrea Campanella101417d2015-12-11 17:58:07 -0800128 @Override
129 NetconfMessageState evaluateChar(char c) {
130 if (c == ']') {
131 return ENDING_BIGGER;
132 } else {
133 return NO_MATCHING_PATTERN;
134 }
135 }
136 },
137 ENDING_BIGGER {
138 @Override
139 NetconfMessageState evaluateChar(char c) {
140 if (c == '>') {
141 return END_PATTERN;
142 } else {
143 return NO_MATCHING_PATTERN;
144 }
145 }
146 },
147 END_PATTERN {
148 @Override
149 NetconfMessageState evaluateChar(char c) {
150 return NO_MATCHING_PATTERN;
151 }
152 };
153
154 abstract NetconfMessageState evaluateChar(char c);
155 }
156
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800157 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800158 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800159 try {
160 boolean socketClosed = false;
161 StringBuilder deviceReplyBuilder = new StringBuilder();
162 while (!socketClosed) {
163 int cInt = bufferReader.read();
164 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800165 log.debug("Netconf device {} sent error char in session," +
166 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella1311ea02016-03-04 17:51:25 -0800167 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
168 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
169 null, null, Optional.of(-1), netconfDeviceInfo);
170 netconfDeviceEventListeners.forEach(
171 listener -> listener.event(event));
172 socketClosed = true;
Andrea Campanella101417d2015-12-11 17:58:07 -0800173 }
174 char c = (char) cInt;
175 state = state.evaluateChar(c);
176 deviceReplyBuilder.append(c);
177 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800178 String deviceReply = deviceReplyBuilder.toString();
179 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800180 socketClosed = true;
Andrea Campanella101417d2015-12-11 17:58:07 -0800181 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800182 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200183 null, null, Optional.of(-1), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800184 netconfDeviceEventListeners.forEach(
185 listener -> listener.event(event));
Andrea Campanella86294db2016-03-07 11:42:49 -0800186 this.interrupt();
Andrea Campanella101417d2015-12-11 17:58:07 -0800187 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800188 deviceReply = deviceReply.replace(END_PATTERN, "");
189 if (deviceReply.contains(RPC_REPLY) ||
190 deviceReply.contains(RPC_ERROR) ||
191 deviceReply.contains(HELLO)) {
192 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
193 NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
194 null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
195 sessionDelegate.notify(event);
196 netconfDeviceEventListeners.forEach(
197 listener -> listener.event(event));
198 } else if (deviceReply.contains(NOTIFICATION_LABEL)) {
helenyrwu0407c642016-06-09 12:01:30 -0700199 if (enableNotifications) {
200 final String finalDeviceReply = deviceReply;
201 netconfDeviceEventListeners.forEach(
202 listener -> listener.event(new NetconfDeviceOutputEvent(
203 NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
204 null, finalDeviceReply, getMsgId(finalDeviceReply),
205 netconfDeviceInfo)));
206 }
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800207 } else {
208 log.info("Error on replay from device {} ", deviceReply);
209 }
210 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800211 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800212 }
213 }
214 } catch (IOException e) {
Andrea Campanellad264b492016-03-01 09:46:06 -0800215 log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e);
Andrea Campanella101417d2015-12-11 17:58:07 -0800216 throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" +
217 netconfDeviceInfo, e));
218 //TODO should we send a socket closed message to listeners ?
219 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800220 }
221
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200222 private static Optional<Integer> getMsgId(String reply) {
Andrea Campanella1311ea02016-03-04 17:51:25 -0800223 if (reply.contains(MESSAGE_ID)) {
224 String[] outer = reply.split(MESSAGE_ID);
225 Preconditions.checkArgument(outer.length != 1,
226 "Error in retrieving the message id");
227 String messageID = outer[1].substring(0, 3).replace("\"", "");
228 Preconditions.checkNotNull(Integer.parseInt(messageID),
229 "Error in retrieving the message id");
230 return Optional.of(Integer.parseInt(messageID));
231 } else if (reply.contains(HELLO)) {
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200232 return Optional.of(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800233 }
Andrea Campanella1311ea02016-03-04 17:51:25 -0800234 return Optional.empty();
Andrea Campanella101417d2015-12-11 17:58:07 -0800235 }
236
237 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
238 if (!netconfDeviceEventListeners.contains(listener)) {
239 netconfDeviceEventListeners.add(listener);
240 }
241 }
242
243 public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) {
244 netconfDeviceEventListeners.remove(listener);
245 }
helenyrwu0407c642016-06-09 12:01:30 -0700246
247 public void setEnableNotifications(boolean enableNotifications) {
248 this.enableNotifications = enableNotifications;
249 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800250}