blob: 1ed3f392a9f03ffcf8eb38b0277938c4add56472 [file] [log] [blame]
Andrea Campanella101417d2015-12-11 17:58:07 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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);
47 private static final String HELLO = "hello";
48 private static final String END_PATTERN = "]]>]]>";
49 private static final String RPC_REPLY = "rpc-reply";
50 private static final String RPC_ERROR = "rpc-error";
51 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;
60 private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners
Andrea Campanella101417d2015-12-11 17:58:07 -080061 = Lists.newArrayList();
62
63 public NetconfStreamThread(final InputStream in, final OutputStream out,
64 final InputStream err, NetconfDeviceInfo deviceInfo,
65 NetconfSessionDelegate delegate) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080066 this.in = in;
67 this.err = err;
Andrea Campanella101417d2015-12-11 17:58:07 -080068 outputStream = new PrintWriter(out);
69 netconfDeviceInfo = deviceInfo;
70 state = NetconfMessageState.NO_MATCHING_PATTERN;
71 sessionDelegate = delegate;
72 log.debug("Stream thread for device {} session started", deviceInfo);
73 start();
74 }
75
76 @Override
77 public CompletableFuture<String> sendMessage(String request) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -080078 log.debug("Sending message {} to device {}", request, netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -080079 outputStream.print(request);
80 outputStream.flush();
81 return new CompletableFuture<>();
82 }
83
84 public enum NetconfMessageState {
85
86 NO_MATCHING_PATTERN {
87 @Override
88 NetconfMessageState evaluateChar(char c) {
89 if (c == ']') {
90 return FIRST_BRAKET;
91 } else {
92 return this;
93 }
94 }
95 },
96 FIRST_BRAKET {
97 @Override
98 NetconfMessageState evaluateChar(char c) {
99 if (c == ']') {
100 return SECOND_BRAKET;
101 } else {
102 return NO_MATCHING_PATTERN;
103 }
104 }
105 },
106 SECOND_BRAKET {
107 @Override
108 NetconfMessageState evaluateChar(char c) {
109 if (c == '>') {
110 return FIRST_BIGGER;
111 } else {
112 return NO_MATCHING_PATTERN;
113 }
114 }
115 },
116 FIRST_BIGGER {
117 @Override
118 NetconfMessageState evaluateChar(char c) {
119 if (c == ']') {
120 return THIRD_BRAKET;
121 } else {
122 return NO_MATCHING_PATTERN;
123 }
124 }
125 },
126 THIRD_BRAKET {
127 @Override
128 NetconfMessageState evaluateChar(char c) {
129 if (c == ']') {
130 return ENDING_BIGGER;
131 } else {
132 return NO_MATCHING_PATTERN;
133 }
134 }
135 },
136 ENDING_BIGGER {
137 @Override
138 NetconfMessageState evaluateChar(char c) {
139 if (c == '>') {
140 return END_PATTERN;
141 } else {
142 return NO_MATCHING_PATTERN;
143 }
144 }
145 },
146 END_PATTERN {
147 @Override
148 NetconfMessageState evaluateChar(char c) {
149 return NO_MATCHING_PATTERN;
150 }
151 };
152
153 abstract NetconfMessageState evaluateChar(char c);
154 }
155
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800156 public void run() {
Andrea Campanella101417d2015-12-11 17:58:07 -0800157 BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));
Andrea Campanella101417d2015-12-11 17:58:07 -0800158 try {
159 boolean socketClosed = false;
160 StringBuilder deviceReplyBuilder = new StringBuilder();
161 while (!socketClosed) {
162 int cInt = bufferReader.read();
163 if (cInt == -1) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800164 log.debug("Netconf device {} sent error char in session," +
165 " will need to be reopend", netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800166 }
167 char c = (char) cInt;
168 state = state.evaluateChar(c);
169 deviceReplyBuilder.append(c);
170 if (state == NetconfMessageState.END_PATTERN) {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800171 String deviceReply = deviceReplyBuilder.toString();
172 if (deviceReply.equals(END_PATTERN)) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800173 socketClosed = true;
Andrea Campanella101417d2015-12-11 17:58:07 -0800174 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800175 NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED,
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200176 null, null, Optional.of(-1), netconfDeviceInfo);
Andrea Campanella101417d2015-12-11 17:58:07 -0800177 netconfDeviceEventListeners.forEach(
178 listener -> listener.event(event));
Andrea Campanella101417d2015-12-11 17:58:07 -0800179 } else {
Andrea Campanellab029b9e2016-01-29 11:05:36 -0800180 deviceReply = deviceReply.replace(END_PATTERN, "");
181 if (deviceReply.contains(RPC_REPLY) ||
182 deviceReply.contains(RPC_ERROR) ||
183 deviceReply.contains(HELLO)) {
184 NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(
185 NetconfDeviceOutputEvent.Type.DEVICE_REPLY,
186 null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
187 sessionDelegate.notify(event);
188 netconfDeviceEventListeners.forEach(
189 listener -> listener.event(event));
190 } else if (deviceReply.contains(NOTIFICATION_LABEL)) {
191 final String finalDeviceReply = deviceReply;
192 netconfDeviceEventListeners.forEach(
193 listener -> listener.event(new NetconfDeviceOutputEvent(
194 NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION,
195 null, finalDeviceReply, getMsgId(finalDeviceReply),
196 netconfDeviceInfo)));
197 } else {
198 log.info("Error on replay from device {} ", deviceReply);
199 }
200 deviceReplyBuilder.setLength(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800201 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800202 }
203 }
204 } catch (IOException e) {
205 log.warn("Error in reading from the session for device " + netconfDeviceInfo, e);
206 throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" +
207 netconfDeviceInfo, e));
208 //TODO should we send a socket closed message to listeners ?
209 }
Andrea Campanella101417d2015-12-11 17:58:07 -0800210 }
211
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200212 private static Optional<Integer> getMsgId(String reply) {
213 if (reply.contains(HELLO)) {
214 return Optional.of(0);
Andrea Campanella101417d2015-12-11 17:58:07 -0800215 }
Andreas Papazoisd4712e22016-02-10 15:59:55 +0200216 if (reply.contains(RPC_ERROR) && !reply.contains(MESSAGE_ID)) {
217 return Optional.empty();
218 }
219 String[] outer = reply.split(MESSAGE_ID);
220 Preconditions.checkArgument(outer.length != 1,
221 "Error in retrieving the message id");
222 String messageID = outer[1].substring(0, 3).replace("\"", "");
223 Preconditions.checkNotNull(Integer.parseInt(messageID),
224 "Error in retrieving the message id");
225 return Optional.of(Integer.parseInt(messageID));
Andrea Campanella101417d2015-12-11 17:58:07 -0800226 }
227
228 public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) {
229 if (!netconfDeviceEventListeners.contains(listener)) {
230 netconfDeviceEventListeners.add(listener);
231 }
232 }
233
234 public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) {
235 netconfDeviceEventListeners.remove(listener);
236 }
237}