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; |
| 35 | import java.util.concurrent.CompletableFuture; |
| 36 | |
| 37 | /** |
| 38 | * Thread that gets spawned each time a session is established and handles all the input |
| 39 | * and output from the session's streams to and from the NETCONF device the session is |
| 40 | * established with. |
| 41 | */ |
| 42 | public class NetconfStreamThread extends Thread implements NetconfStreamHandler { |
| 43 | |
| 44 | private static final Logger log = LoggerFactory |
| 45 | .getLogger(NetconfStreamThread.class); |
| 46 | private static final String HELLO = "hello"; |
| 47 | private static final String END_PATTERN = "]]>]]>"; |
| 48 | private static final String RPC_REPLY = "rpc-reply"; |
| 49 | private static final String RPC_ERROR = "rpc-error"; |
| 50 | private static final String NOTIFICATION_LABEL = "<notification>"; |
| 51 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 52 | private PrintWriter outputStream; |
| 53 | private final InputStream err; |
| 54 | private final InputStream in; |
| 55 | private NetconfDeviceInfo netconfDeviceInfo; |
| 56 | private NetconfSessionDelegate sessionDelegate; |
| 57 | private NetconfMessageState state; |
| 58 | private List<NetconfDeviceOutputEventListener> netconfDeviceEventListeners |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 59 | = Lists.newArrayList(); |
| 60 | |
| 61 | public NetconfStreamThread(final InputStream in, final OutputStream out, |
| 62 | final InputStream err, NetconfDeviceInfo deviceInfo, |
| 63 | NetconfSessionDelegate delegate) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 64 | this.in = in; |
| 65 | this.err = err; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 66 | outputStream = new PrintWriter(out); |
| 67 | netconfDeviceInfo = deviceInfo; |
| 68 | state = NetconfMessageState.NO_MATCHING_PATTERN; |
| 69 | sessionDelegate = delegate; |
| 70 | log.debug("Stream thread for device {} session started", deviceInfo); |
| 71 | start(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public CompletableFuture<String> sendMessage(String request) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 76 | log.debug("Sending message {} to device {}", request, netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 77 | outputStream.print(request); |
| 78 | outputStream.flush(); |
| 79 | return new CompletableFuture<>(); |
| 80 | } |
| 81 | |
| 82 | public enum NetconfMessageState { |
| 83 | |
| 84 | NO_MATCHING_PATTERN { |
| 85 | @Override |
| 86 | NetconfMessageState evaluateChar(char c) { |
| 87 | if (c == ']') { |
| 88 | return FIRST_BRAKET; |
| 89 | } else { |
| 90 | return this; |
| 91 | } |
| 92 | } |
| 93 | }, |
| 94 | FIRST_BRAKET { |
| 95 | @Override |
| 96 | NetconfMessageState evaluateChar(char c) { |
| 97 | if (c == ']') { |
| 98 | return SECOND_BRAKET; |
| 99 | } else { |
| 100 | return NO_MATCHING_PATTERN; |
| 101 | } |
| 102 | } |
| 103 | }, |
| 104 | SECOND_BRAKET { |
| 105 | @Override |
| 106 | NetconfMessageState evaluateChar(char c) { |
| 107 | if (c == '>') { |
| 108 | return FIRST_BIGGER; |
| 109 | } else { |
| 110 | return NO_MATCHING_PATTERN; |
| 111 | } |
| 112 | } |
| 113 | }, |
| 114 | FIRST_BIGGER { |
| 115 | @Override |
| 116 | NetconfMessageState evaluateChar(char c) { |
| 117 | if (c == ']') { |
| 118 | return THIRD_BRAKET; |
| 119 | } else { |
| 120 | return NO_MATCHING_PATTERN; |
| 121 | } |
| 122 | } |
| 123 | }, |
| 124 | THIRD_BRAKET { |
| 125 | @Override |
| 126 | NetconfMessageState evaluateChar(char c) { |
| 127 | if (c == ']') { |
| 128 | return ENDING_BIGGER; |
| 129 | } else { |
| 130 | return NO_MATCHING_PATTERN; |
| 131 | } |
| 132 | } |
| 133 | }, |
| 134 | ENDING_BIGGER { |
| 135 | @Override |
| 136 | NetconfMessageState evaluateChar(char c) { |
| 137 | if (c == '>') { |
| 138 | return END_PATTERN; |
| 139 | } else { |
| 140 | return NO_MATCHING_PATTERN; |
| 141 | } |
| 142 | } |
| 143 | }, |
| 144 | END_PATTERN { |
| 145 | @Override |
| 146 | NetconfMessageState evaluateChar(char c) { |
| 147 | return NO_MATCHING_PATTERN; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | abstract NetconfMessageState evaluateChar(char c); |
| 152 | } |
| 153 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 154 | public void run() { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 155 | BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 156 | try { |
| 157 | boolean socketClosed = false; |
| 158 | StringBuilder deviceReplyBuilder = new StringBuilder(); |
| 159 | while (!socketClosed) { |
| 160 | int cInt = bufferReader.read(); |
| 161 | if (cInt == -1) { |
| 162 | socketClosed = true; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 163 | log.debug("char {} " + bufferReader.read()); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 164 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 165 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
| 166 | null, null, -1, netconfDeviceInfo); |
| 167 | netconfDeviceEventListeners.forEach( |
| 168 | listener -> listener.event(event)); |
| 169 | } |
| 170 | char c = (char) cInt; |
| 171 | state = state.evaluateChar(c); |
| 172 | deviceReplyBuilder.append(c); |
| 173 | if (state == NetconfMessageState.END_PATTERN) { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 174 | String deviceReply = deviceReplyBuilder.toString(); |
| 175 | if (deviceReply.equals(END_PATTERN)) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 176 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 177 | NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, |
| 178 | null, null, -1, netconfDeviceInfo); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 179 | netconfDeviceEventListeners.forEach( |
| 180 | listener -> listener.event(event)); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 181 | } else { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame^] | 182 | deviceReply = deviceReply.replace(END_PATTERN, ""); |
| 183 | if (deviceReply.contains(RPC_REPLY) || |
| 184 | deviceReply.contains(RPC_ERROR) || |
| 185 | deviceReply.contains(HELLO)) { |
| 186 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 187 | NetconfDeviceOutputEvent.Type.DEVICE_REPLY, |
| 188 | null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo); |
| 189 | sessionDelegate.notify(event); |
| 190 | netconfDeviceEventListeners.forEach( |
| 191 | listener -> listener.event(event)); |
| 192 | } else if (deviceReply.contains(NOTIFICATION_LABEL)) { |
| 193 | final String finalDeviceReply = deviceReply; |
| 194 | netconfDeviceEventListeners.forEach( |
| 195 | listener -> listener.event(new NetconfDeviceOutputEvent( |
| 196 | NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, |
| 197 | null, finalDeviceReply, getMsgId(finalDeviceReply), |
| 198 | netconfDeviceInfo))); |
| 199 | } else { |
| 200 | log.info("Error on replay from device {} ", deviceReply); |
| 201 | } |
| 202 | deviceReplyBuilder.setLength(0); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 203 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } catch (IOException e) { |
| 207 | log.warn("Error in reading from the session for device " + netconfDeviceInfo, e); |
| 208 | throw new RuntimeException(new NetconfException("Error in reading from the session for device {}" + |
| 209 | netconfDeviceInfo, e)); |
| 210 | //TODO should we send a socket closed message to listeners ? |
| 211 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private static int getMsgId(String reply) { |
| 215 | if (!reply.contains(HELLO)) { |
| 216 | String[] outer = reply.split("message-id="); |
| 217 | Preconditions.checkArgument(outer.length != 1, |
| 218 | "Error in retrieving the message id"); |
| 219 | String messageID = outer[1].substring(0, 3).replace("\"", ""); |
| 220 | Preconditions.checkNotNull(Integer.parseInt(messageID), |
| 221 | "Error in retrieving the message id"); |
| 222 | return Integer.parseInt(messageID); |
| 223 | } else { |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 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 | } |