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.openflowjava.protocol.api.connection.ConnectionReadyListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SendMultipartRequestMessageInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
58 import org.opendaylight.yangtools.yang.binding.DataContainer;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.opendaylight.yangtools.yang.binding.Notification;
61 import org.opendaylight.yangtools.yang.common.RpcError;
62 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
63 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.cache.Cache;
69 import com.google.common.cache.CacheBuilder;
70 import com.google.common.cache.RemovalListener;
71 import com.google.common.cache.RemovalNotification;
72 import com.google.common.collect.Lists;
73 import com.google.common.util.concurrent.SettableFuture;
77 * @author michal.polkorab
79 public class ConnectionAdapterImpl implements ConnectionFacade {
81 /** after this time, rpc future response objects will be thrown away (in minutes) */
82 public static final int RPC_RESPONSE_EXPIRATION = 1;
84 protected static final Logger LOG = LoggerFactory
85 .getLogger(ConnectionAdapterImpl.class);
87 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
88 private static final String TAG = "OPENFLOW";
89 private Channel channel;
90 private OpenflowProtocolListener messageListener;
91 /** expiring cache for future rpcResponses */
92 protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
93 private SystemNotificationsListener systemListener;
94 private boolean disconnectOccured = false;
96 protected ConnectionReadyListener connectionReadyListener;
101 public ConnectionAdapterImpl() {
102 responseCache = CacheBuilder.newBuilder()
104 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
105 .removalListener(new ResponseRemovalListener()).build();
106 LOG.debug("ConnectionAdapter created");
110 * @param channel the channel to be set - used for communication
112 public void setChannel(Channel channel) {
113 this.channel = channel;
117 public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
118 return sendToSwitchExpectRpcResultFuture(
119 input, BarrierOutput.class, "barrier-input sending failed");
123 public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
124 return sendToSwitchExpectRpcResultFuture(
125 input, EchoOutput.class, "echo-input sending failed");
129 public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
130 return sendToSwitchFuture(input, "echo-reply sending failed");
134 public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
135 return sendToSwitchFuture(input, "experimenter sending failed");
139 public Future<RpcResult<Void>> flowMod(FlowModInput input) {
140 return sendToSwitchFuture(input, "flow-mod sending failed");
144 public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
145 return sendToSwitchExpectRpcResultFuture(
146 input, GetConfigOutput.class, "get-config-input sending failed");
150 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
151 GetFeaturesInput input) {
152 return sendToSwitchExpectRpcResultFuture(
153 input, GetFeaturesOutput.class, "get-features-input sending failed");
157 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
158 GetQueueConfigInput input) {
159 return sendToSwitchExpectRpcResultFuture(
160 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
164 public Future<RpcResult<Void>> groupMod(GroupModInput input) {
165 return sendToSwitchFuture(input, "group-mod-input sending failed");
169 public Future<RpcResult<Void>> hello(HelloInput input) {
170 return sendToSwitchFuture(input, "hello-input sending failed");
174 public Future<RpcResult<Void>> meterMod(MeterModInput input) {
175 return sendToSwitchFuture(input, "meter-mod-input sending failed");
179 public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
180 return sendToSwitchFuture(input, "packet-out-input sending failed");
184 public Future<RpcResult<Void>> sendMultipartRequestMessage(SendMultipartRequestMessageInput input) {
185 return sendToSwitchFuture(input, "multi-part-request sending failed");
189 public Future<RpcResult<Void>> portMod(PortModInput input) {
190 return sendToSwitchFuture(input, "port-mod-input sending failed");
194 public Future<RpcResult<RoleRequestOutput>> roleRequest(
195 RoleRequestInput input) {
196 return sendToSwitchExpectRpcResultFuture(
197 input, RoleRequestOutput.class, "role-request-config-input sending failed");
201 public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
202 return sendToSwitchFuture(input, "set-config-input sending failed");
206 public Future<RpcResult<Void>> tableMod(TableModInput input) {
207 return sendToSwitchFuture(input, "table-mod-input sending failed");
211 public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
212 return sendToSwitchExpectRpcResultFuture(
213 input, GetAsyncOutput.class, "get-async-input sending failed");
217 public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
218 return sendToSwitchFuture(input, "set-async-input sending failed");
222 public Future<Boolean> disconnect() {
223 ChannelFuture disconnectResult = channel.disconnect();
224 responseCache.invalidateAll();
225 disconnectOccured = true;
227 String failureInfo = "switch disconnecting failed";
228 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
229 String message = "Check the switch connection";
230 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
234 public boolean isAlive() {
235 return channel.isOpen();
239 public void setMessageListener(OpenflowProtocolListener messageListener) {
240 this.messageListener = messageListener;
244 public void consume(DataObject message) {
245 LOG.debug("ConsumeIntern msg");
246 if (disconnectOccured ) {
249 if (message instanceof Notification) {
251 if (message instanceof DisconnectEvent) {
252 systemListener.onDisconnectEvent((DisconnectEvent) message);
253 responseCache.invalidateAll();
254 disconnectOccured = true;
255 } else if (message instanceof SwitchIdleEvent) {
256 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
259 else if (message instanceof EchoRequestMessage) {
260 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
261 } else if (message instanceof ErrorMessage) {
262 messageListener.onErrorMessage((ErrorMessage) message);
263 } else if (message instanceof ExperimenterMessage) {
264 messageListener.onExperimenterMessage((ExperimenterMessage) message);
265 } else if (message instanceof FlowRemovedMessage) {
266 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
267 } else if (message instanceof HelloMessage) {
268 LOG.info("Hello received / branch");
269 messageListener.onHelloMessage((HelloMessage) message);
270 } else if (message instanceof MultipartReplyMessage) {
271 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
272 } else if (message instanceof MultipartRequestMessage) {
273 messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
274 } else if (message instanceof PacketInMessage) {
275 messageListener.onPacketInMessage((PacketInMessage) message);
276 } else if (message instanceof PortStatusMessage) {
277 messageListener.onPortStatusMessage((PortStatusMessage) message);
279 LOG.warn("message listening not supported for type: "+message.getClass());
282 if (message instanceof OfHeader) {
283 LOG.debug("OFheader msg received");
284 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
285 final SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
286 if (rpcFuture != null) {
287 LOG.debug("corresponding rpcFuture found");
288 List<RpcError> errors = Collections.emptyList();
289 LOG.debug("before setting rpcFuture");
290 rpcFuture.set(Rpcs.getRpcResult(true, message, errors));
291 LOG.debug("after setting rpcFuture");
292 responseCache.invalidate(key);
294 LOG.warn("received unexpected rpc response: "+key);
297 LOG.warn("message listening not supported for type: "+message.getClass());
303 * sends given message to switch, sending result will be reported via return value
304 * @param input message to send
305 * @param failureInfo describes, what type of message caused failure by sending
306 * @return future object, <ul>
307 * <li>if send successful, {@link RpcResult} without errors and successful
308 * status will be returned, </li>
309 * <li>else {@link RpcResult} will contain errors and failed status</li>
312 private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
313 DataObject input, final String failureInfo) {
314 LOG.debug("going to flush");
315 ChannelFuture resultFuture = channel.writeAndFlush(input);
316 LOG.debug("flushed");
318 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
319 String errorMessage = "check switch connection";
320 return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
324 * sends given message to switch, sending result or switch response will be reported via return value
325 * @param input message to send
326 * @param responseClazz type of response
327 * @param failureInfo describes, what type of message caused failure by sending
328 * @return future object, <ul>
329 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
330 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
331 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
332 * <ul><li>either switch will manage to answer
333 * and then corresponding response message will be set into returned future</li>
334 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
338 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
339 IN input, Class<OUT> responseClazz, final String failureInfo) {
340 LOG.debug("going to flush");
341 SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
342 RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz);
343 responseCache.put(key, rpcResult);
344 ChannelFuture resultFuture = channel.writeAndFlush(input);
345 LOG.debug("flushed");
347 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
348 String errorMessage = "check switch connection";
350 return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity,
351 errorMessage, input, responseClazz, rpcResult, key);
355 * @param resultFuture
359 private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
360 ChannelFuture resultFuture, final String failureInfo,
361 final ErrorSeverity errorSeverity, final String errorMessage) {
363 final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
364 LOG.debug("handlerpcchannelfuture");
365 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
368 public void operationComplete(
369 io.netty.util.concurrent.Future<? super Void> future)
371 LOG.debug("operation complete");
372 Collection<RpcError> errors = Collections.emptyList();
374 if (future.cause() != null) {
375 LOG.debug("future.cause != null");
376 RpcError rpcError = buildRpcError(failureInfo,
377 errorSeverity, errorMessage, future.cause());
378 errors = Lists.newArrayList(rpcError);
381 rpcResult.set(Rpcs.getRpcResult(
392 * @param resultFuture
394 * @param errorSeverity
395 * @param errorMessage
397 * @param responseClazz
398 * @param key of rpcResponse
401 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
402 ChannelFuture resultFuture, final String failureInfo,
403 final ErrorSeverity errorSeverity, final String errorMessage,
404 final IN input, Class<OUT> responseClazz, final SettableFuture<RpcResult<OUT>> rpcResult, final RpcResponseKey key) {
405 LOG.debug("handleRpcchanfuture with response");
407 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
410 public void operationComplete(
411 io.netty.util.concurrent.Future<? super Void> future)
414 LOG.debug("operation complete");
415 Collection<RpcError> errors = Collections.emptyList();
416 if (future.cause() != null) {
417 LOG.debug("ChannelFuture.cause != null");
418 RpcError rpcError = buildRpcError(failureInfo,
419 errorSeverity, errorMessage, future.cause());
420 errors = Lists.newArrayList(rpcError);
421 rpcResult.set(Rpcs.getRpcResult(
426 responseCache.invalidate(key);
428 LOG.debug("ChannelFuture.cause == null");
429 if (responseCache.getIfPresent(key) == null) {
430 LOG.debug("responcache: key wasn't present");
439 * @param resultFuture
441 * @param errorSeverity
445 private static SettableFuture<Boolean> handleTransportChannelFuture(
446 ChannelFuture resultFuture, final String failureInfo,
447 final ErrorSeverity errorSeverity, final String message) {
449 final SettableFuture<Boolean> transportResult = SettableFuture.create();
451 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
454 public void operationComplete(
455 io.netty.util.concurrent.Future<? super Void> future)
457 transportResult.set(future.isSuccess());
458 if (!future.isSuccess()) {
459 transportResult.setException(future.cause());
463 return transportResult;
470 protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
472 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
473 ErrorType.RPC, cause);
481 protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
483 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
484 ErrorType.TRANSPORT, cause);
492 private static RpcResponseKey createRpcResponseKey(OfHeader message) {
493 return new RpcResponseKey(message.getXid(), message.getClass());
499 @SuppressWarnings("unchecked")
500 private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
501 return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
505 public void setSystemListener(SystemNotificationsListener systemListener) {
506 this.systemListener = systemListener;
510 public void checkListeners() {
511 StringBuffer buffer = new StringBuffer();
512 if (systemListener == null) {
513 buffer.append("SystemListener ");
515 if (messageListener == null) {
516 buffer.append("MessageListener ");
518 if (connectionReadyListener == null) {
519 buffer.append("ConnectionReadyListener ");
522 if (buffer.length() > 0) {
523 throw new IllegalStateException("Missing listeners: " + buffer.toString());
527 static class ResponseRemovalListener implements RemovalListener<RpcResponseKey, SettableFuture<?>> {
529 public void onRemoval(
530 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
531 SettableFuture<?> future = notification.getValue();
532 if (!future.isDone()) {
533 LOG.warn("rpc response discarded: " + notification.getKey());
540 * Class is used ONLY for exiting msgQueue processing thread
541 * @author michal.polkorab
543 static class ExitingDataObject implements DataObject {
545 public Class<? extends DataContainer> getImplementedInterface() {
551 public void fireConnectionReadyNotification() {
552 new Thread(new Runnable() {
555 connectionReadyListener.onConnectionReady();
562 public void setConnectionReadyListener(
563 ConnectionReadyListener connectionReadyListener) {
564 this.connectionReadyListener = connectionReadyListener;