Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 22 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 23 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 24 | import org.onosproject.netconf.NetconfException; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.io.BufferedReader; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.io.OutputStream; |
| 33 | import java.io.PrintWriter; |
| 34 | import java.util.List; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 35 | import java.util.Optional; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 36 | import 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 | */ |
| 43 | public 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 Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 52 | private static final String MESSAGE_ID = "message-id="; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 53 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 54 | 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 Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 61 | = Lists.newArrayList(); |
| 62 | |
| 63 | public NetconfStreamThread(final InputStream in, final OutputStream out, |
| 64 | final InputStream err, NetconfDeviceInfo deviceInfo, |
| 65 | NetconfSessionDelegate delegate) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 66 | this.in = in; |
| 67 | this.err = err; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 68 | 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 Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 78 | log.debug("Sending message {} to device {}", request, netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 79 | 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 Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 156 | public void run() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 157 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 158 | try { |
| 159 | boolean socketClosed = false; |
| 160 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
| 161 | while (!socketClosed) { |
| 162 | int cInt = bufferReader.read(); |
| 163 | if (cInt == -1) { |
| 164 | socketClosed = true; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 165 | log.debug("char {} " + bufferReader.read()); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 166 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 167 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 168 | null, null, Optional.of(-1), netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 169 | netconfDeviceEventListeners.forEach( |
| 170 | listener -> listener.event(event)); |
| 171 | } |
| 172 | char c = (char) cInt; |
| 173 | state = state.evaluateChar(c); |
| 174 | deviceReplyBuilder.append(c); |
| 175 | if (state == NetconfMessageState.END_PATTERN) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 176 | String deviceReply = deviceReplyBuilder.toString(); |
| 177 | if (deviceReply.equals(END_PATTERN)) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 178 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 179 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 180 | null, null, Optional.of(-1), netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 181 | netconfDeviceEventListeners.forEach( |
| 182 | listener -> listener.event(event)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 183 | } else { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 184 | deviceReply = deviceReply.replace(END_PATTERN, ""); |
| 185 | if (deviceReply.contains(RPC_REPLY) || |
| 186 | deviceReply.contains(RPC_ERROR) || |
| 187 | deviceReply.contains(HELLO)) { |
| 188 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 189 | NetconfDeviceOutputEvent.Type.DEVICE_REPLY, |
| 190 | null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); |
| 191 | sessionDelegate.notify(event); |
| 192 | netconfDeviceEventListeners.forEach( |
| 193 | listener -> listener.event(event)); |
| 194 | } else if (deviceReply.contains(NOTIFICATION_LABEL)) { |
| 195 | final String finalDeviceReply = deviceReply; |
| 196 | netconfDeviceEventListeners.forEach( |
| 197 | listener -> listener.event(new NetconfDeviceOutputEvent( |
| 198 | NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, |
| 199 | null, finalDeviceReply, getMsgId(finalDeviceReply), |
| 200 | netconfDeviceInfo))); |
| 201 | } else { |
| 202 | log.info("Error on replay from device {} ", deviceReply); |
| 203 | } |
| 204 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 205 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } catch (IOException e) { |
| 209 | log.warn("Error in reading from the session for device " + netconfDeviceInfo, e); |
| 210 | throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" + |
| 211 | netconfDeviceInfo, e)); |
| 212 | //TODO should we send a socket closed message to listeners ? |
| 213 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 216 | private static Optional<Integer> getMsgId(String reply) { |
| 217 | if (reply.contains(HELLO)) { |
| 218 | return Optional.of(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 219 | } |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame^] | 220 | if (reply.contains(RPC_ERROR) && !reply.contains(MESSAGE_ID)) { |
| 221 | return Optional.empty(); |
| 222 | } |
| 223 | String[] outer = reply.split(MESSAGE_ID); |
| 224 | Preconditions.checkArgument(outer.length != 1, |
| 225 | "Error in retrieving the message id"); |
| 226 | String messageID = outer[1].substring(0, 3).replace("\"", ""); |
| 227 | Preconditions.checkNotNull(Integer.parseInt(messageID), |
| 228 | "Error in retrieving the message id"); |
| 229 | return Optional.of(Integer.parseInt(messageID)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public void addDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 233 | if (!netconfDeviceEventListeners.contains(listener)) { |
| 234 | netconfDeviceEventListeners.add(listener); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | public void removeDeviceEventListener(NetconfDeviceOutputEventListener listener) { |
| 239 | netconfDeviceEventListeners.remove(listener); |
| 240 | } |
| 241 | } |