1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
3 package org.opendaylight.openflowjava.protocol.impl.connection;
5 import io.netty.channel.Channel;
6 import io.netty.channel.ChannelFuture;
7 import io.netty.util.concurrent.GenericFutureListener;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.controller.sal.common.util.RpcErrors;
16 import org.opendaylight.controller.sal.common.util.Rpcs;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
56 import org.opendaylight.yangtools.yang.binding.DataObject;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.opendaylight.yangtools.yang.common.RpcError;
59 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
60 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
61 import org.opendaylight.yangtools.yang.common.RpcResult;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.common.cache.Cache;
66 import com.google.common.cache.CacheBuilder;
67 import com.google.common.cache.RemovalListener;
68 import com.google.common.cache.RemovalNotification;
69 import com.google.common.collect.Lists;
70 import com.google.common.util.concurrent.SettableFuture;
74 * @author michal.polkorab
76 public class ConnectionAdapterImpl implements ConnectionFacade {
78 /** after this time, rpc future response objects will be thrown away (in minutes) */
79 public static final int RPC_RESPONSE_EXPIRATION = 1;
81 protected static final Logger LOG = LoggerFactory
82 .getLogger(ConnectionAdapterImpl.class);
84 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
85 private static final String TAG = "OPENFLOW";
86 private Channel channel;
87 private OpenflowProtocolListener messageListener;
88 /** expiring cache for future rpcResponses */
89 protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
90 private SystemNotificationsListener systemListener;
91 private boolean disconnectOccured = false;
96 public ConnectionAdapterImpl() {
97 responseCache = CacheBuilder.newBuilder()
99 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
100 .removalListener(new ResponseRemovalListener()).build();
101 LOG.info("ConnectionAdapter created");
107 * @param channel the channel to be set - used for communication
109 public void setChannel(Channel channel) {
110 this.channel = channel;
114 public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
115 return sendToSwitchExpectRpcResultFuture(
116 input, BarrierOutput.class, "barrier-input sending failed");
120 public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
121 return sendToSwitchExpectRpcResultFuture(
122 input, EchoOutput.class, "echo-input sending failed");
126 public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
127 return sendToSwitchFuture(input, "echo-reply sending failed");
131 public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
132 return sendToSwitchFuture(input, "experimenter sending failed");
136 public Future<RpcResult<Void>> flowMod(FlowModInput input) {
137 return sendToSwitchFuture(input, "flow-mod sending failed");
141 public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
142 return sendToSwitchExpectRpcResultFuture(
143 input, GetConfigOutput.class, "get-config-input sending failed");
147 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
148 GetFeaturesInput input) {
149 return sendToSwitchExpectRpcResultFuture(
150 input, GetFeaturesOutput.class, "get-features-input sending failed");
154 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
155 GetQueueConfigInput input) {
156 return sendToSwitchExpectRpcResultFuture(
157 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
161 public Future<RpcResult<Void>> groupMod(GroupModInput input) {
162 return sendToSwitchFuture(input, "group-mod-input sending failed");
166 public Future<RpcResult<Void>> hello(HelloInput input) {
167 return sendToSwitchFuture(input, "hello-input sending failed");
171 public Future<RpcResult<Void>> meterMod(MeterModInput input) {
172 return sendToSwitchFuture(input, "meter-mod-input sending failed");
176 public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
177 return sendToSwitchFuture(input, "packet-out-input sending failed");
181 public Future<RpcResult<Void>> portMod(PortModInput input) {
182 return sendToSwitchFuture(input, "port-mod-input sending failed");
186 public Future<RpcResult<RoleRequestOutput>> roleRequest(
187 RoleRequestInput input) {
188 return sendToSwitchExpectRpcResultFuture(
189 input, RoleRequestOutput.class, "role-request-config-input sending failed");
193 public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
194 return sendToSwitchFuture(input, "set-config-input sending failed");
198 public Future<RpcResult<Void>> tableMod(TableModInput input) {
199 return sendToSwitchFuture(input, "table-mod-input sending failed");
203 public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
204 return sendToSwitchExpectRpcResultFuture(
205 input, GetAsyncOutput.class, "get-async-input sending failed");
209 public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
210 return sendToSwitchFuture(input, "set-async-input sending failed");
214 public Future<Boolean> disconnect() {
215 ChannelFuture disconnectResult = channel.disconnect();
216 responseCache.invalidateAll();
217 disconnectOccured = true;
219 String failureInfo = "switch disconnecting failed";
220 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
221 String message = "Check the switch connection";
222 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
226 public boolean isAlive() {
227 return channel.isOpen();
231 public void setMessageListener(OpenflowProtocolListener messageListener) {
232 this.messageListener = messageListener;
236 public void consume(DataObject message) {
237 LOG.debug("Consume msg");
238 if (disconnectOccured ) {
241 if (message instanceof Notification) {
243 if (message instanceof DisconnectEvent) {
244 systemListener.onDisconnectEvent((DisconnectEvent) message);
245 responseCache.invalidateAll();
246 disconnectOccured = true;
247 } else if (message instanceof SwitchIdleEvent) {
248 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
251 else if (message instanceof EchoRequestMessage) {
252 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
253 } else if (message instanceof ErrorMessage) {
254 messageListener.onErrorMessage((ErrorMessage) message);
255 } else if (message instanceof ExperimenterMessage) {
256 messageListener.onExperimenterMessage((ExperimenterMessage) message);
257 } else if (message instanceof FlowRemovedMessage) {
258 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
259 } else if (message instanceof HelloMessage) {
260 LOG.info("Hello received / branch");
261 messageListener.onHelloMessage((HelloMessage) message);
262 } else if (message instanceof MultipartReplyMessage) {
263 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
264 } else if (message instanceof MultipartRequestMessage) {
265 messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
266 } else if (message instanceof PacketInMessage) {
267 messageListener.onPacketInMessage((PacketInMessage) message);
268 } else if (message instanceof PortStatusMessage) {
269 messageListener.onPortStatusMessage((PortStatusMessage) message);
271 LOG.warn("message listening not supported for type: "+message.getClass());
274 if (message instanceof OfHeader) {
275 LOG.debug("OFheader msg received");
276 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
277 SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
278 if (rpcFuture != null) {
279 LOG.debug("corresponding rpcFuture found");
280 List<RpcError> errors = Collections.emptyList();
281 rpcFuture.set(Rpcs.getRpcResult(true, message, errors));
282 responseCache.invalidate(key);
284 LOG.warn("received unexpected rpc response: "+key);
287 LOG.warn("message listening not supported for type: "+message.getClass());
293 * sends given message to switch, sending result will be reported via return value
294 * @param input message to send
295 * @param failureInfo describes, what type of message caused failure by sending
296 * @return future object, <ul>
297 * <li>if send successful, {@link RpcResult} without errors and successful
298 * status will be returned, </li>
299 * <li>else {@link RpcResult} will contain errors and failed status</li>
302 private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
303 DataObject input, final String failureInfo) {
304 LOG.debug("going to flush");
305 ChannelFuture resultFuture = channel.writeAndFlush(input);
306 LOG.debug("flushed");
308 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
309 String errorMessage = "check switch connection";
310 return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
314 * sends given message to switch, sending result or switch response will be reported via return value
315 * @param input message to send
316 * @param responseClazz type of response
317 * @param failureInfo describes, what type of message caused failure by sending
318 * @return future object, <ul>
319 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
320 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
321 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
322 * <ul><li>either switch will manage to answer
323 * and then corresponding response message will be set into returned future</li>
324 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
328 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
329 IN input, Class<OUT> responseClazz, final String failureInfo) {
330 LOG.debug("going to flush");
331 SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
332 RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz);
333 responseCache.put(key, rpcResult);
334 ChannelFuture resultFuture = channel.writeAndFlush(input);
335 LOG.debug("flushed");
337 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
338 String errorMessage = "check switch connection";
340 return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity,
341 errorMessage, input, responseClazz, rpcResult, key);
345 * @param resultFuture
349 private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
350 ChannelFuture resultFuture, final String failureInfo,
351 final ErrorSeverity errorSeverity, final String errorMessage) {
353 final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
354 LOG.debug("handlerpcchannelfuture");
355 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
358 public void operationComplete(
359 io.netty.util.concurrent.Future<? super Void> future)
361 LOG.debug("operation complete");
362 Collection<RpcError> errors = Collections.emptyList();
364 if (future.cause() != null) {
365 LOG.debug("future.cause != null");
366 RpcError rpcError = buildRpcError(failureInfo,
367 errorSeverity, errorMessage, future.cause());
368 errors = Lists.newArrayList(rpcError);
371 rpcResult.set(Rpcs.getRpcResult(
382 * @param resultFuture
384 * @param errorSeverity
385 * @param errorMessage
387 * @param responseClazz
392 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
393 ChannelFuture resultFuture, final String failureInfo,
394 final ErrorSeverity errorSeverity, final String errorMessage,
395 final IN input, Class<OUT> responseClazz, final SettableFuture<RpcResult<OUT>> rpcResult, final RpcResponseKey key) {
396 LOG.debug("handleRpcchanfuture with response");
398 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
401 public void operationComplete(
402 io.netty.util.concurrent.Future<? super Void> future)
405 LOG.debug("operation complete");
406 Collection<RpcError> errors = Collections.emptyList();
407 if (future.cause() != null) {
408 LOG.debug("ChannelFuture.cause != null");
409 RpcError rpcError = buildRpcError(failureInfo,
410 errorSeverity, errorMessage, future.cause());
411 errors = Lists.newArrayList(rpcError);
412 rpcResult.set(Rpcs.getRpcResult(
417 responseCache.invalidate(key);
419 LOG.debug("ChannelFuture.cause == null");
420 if (responseCache.getIfPresent(key) == null) {
421 LOG.debug("responcache: key wasn't present");
430 * @param resultFuture
432 * @param errorSeverity
436 private static SettableFuture<Boolean> handleTransportChannelFuture(
437 ChannelFuture resultFuture, final String failureInfo,
438 final ErrorSeverity errorSeverity, final String message) {
440 final SettableFuture<Boolean> transportResult = SettableFuture.create();
442 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
445 public void operationComplete(
446 io.netty.util.concurrent.Future<? super Void> future)
448 transportResult.set(future.isSuccess());
449 transportResult.setException(future.cause());
452 return transportResult;
459 protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
461 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
462 ErrorType.RPC, cause);
470 protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
472 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
473 ErrorType.TRANSPORT, cause);
481 private static RpcResponseKey createRpcResponseKey(OfHeader message) {
482 return new RpcResponseKey(message.getXid(), message.getClass());
488 @SuppressWarnings("unchecked")
489 private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
490 return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
494 public void setSystemListener(SystemNotificationsListener systemListener) {
495 this.systemListener = systemListener;
499 public void checkListeners() {
500 StringBuffer buffer = new StringBuffer();
501 if (systemListener == null) {
502 buffer.append("SystemListener ");
504 if (messageListener == null) {
505 buffer.append("MessageListener ");
508 if (buffer.length() > 0) {
509 throw new IllegalStateException("Missing listeners: " + buffer.toString());
513 static class ResponseRemovalListener implements RemovalListener<RpcResponseKey, SettableFuture<?>> {
516 public void onRemoval(
517 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
518 SettableFuture<?> future = notification.getValue();
519 if (!future.isDone()) {
520 LOG.warn("rpc response discarded: " + notification.getKey());