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.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.sal.common.util.RpcErrors;
18 import org.opendaylight.controller.sal.common.util.Rpcs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
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.DataObject;
59 import org.opendaylight.yangtools.yang.binding.Notification;
60 import org.opendaylight.yangtools.yang.common.RpcError;
61 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
62 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 import com.google.common.cache.Cache;
68 import com.google.common.cache.CacheBuilder;
69 import com.google.common.cache.RemovalListener;
70 import com.google.common.cache.RemovalNotification;
71 import com.google.common.collect.Lists;
72 import com.google.common.util.concurrent.SettableFuture;
76 * @author michal.polkorab
78 public class ConnectionAdapterImpl implements ConnectionFacade {
80 /** after this time, rpc future response objects will be thrown away (in minutes) */
81 public static final int RPC_RESPONSE_EXPIRATION = 1;
83 protected static final Logger LOG = LoggerFactory
84 .getLogger(ConnectionAdapterImpl.class);
86 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
87 private static final String TAG = "OPENFLOW";
88 private Channel channel;
89 private OpenflowProtocolListener messageListener;
90 /** expiring cache for future rpcResponses */
91 protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
92 private SystemNotificationsListener systemListener;
93 private boolean disconnectOccured = false;
94 private ExecutorService threadPool;
99 public ConnectionAdapterImpl() {
100 responseCache = CacheBuilder.newBuilder()
102 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
103 .removalListener(new ResponseRemovalListener()).build();
104 threadPool = Executors.newCachedThreadPool();
105 LOG.info("ConnectionAdapter created");
109 * @param channel the channel to be set - used for communication
111 public void setChannel(Channel channel) {
112 this.channel = channel;
116 public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
117 return sendToSwitchExpectRpcResultFuture(
118 input, BarrierOutput.class, "barrier-input sending failed");
122 public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
123 return sendToSwitchExpectRpcResultFuture(
124 input, EchoOutput.class, "echo-input sending failed");
128 public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
129 return sendToSwitchFuture(input, "echo-reply sending failed");
133 public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
134 return sendToSwitchFuture(input, "experimenter sending failed");
138 public Future<RpcResult<Void>> flowMod(FlowModInput input) {
139 return sendToSwitchFuture(input, "flow-mod sending failed");
143 public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
144 return sendToSwitchExpectRpcResultFuture(
145 input, GetConfigOutput.class, "get-config-input sending failed");
149 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
150 GetFeaturesInput input) {
151 return sendToSwitchExpectRpcResultFuture(
152 input, GetFeaturesOutput.class, "get-features-input sending failed");
156 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
157 GetQueueConfigInput input) {
158 return sendToSwitchExpectRpcResultFuture(
159 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
163 public Future<RpcResult<Void>> groupMod(GroupModInput input) {
164 return sendToSwitchFuture(input, "group-mod-input sending failed");
168 public Future<RpcResult<Void>> hello(HelloInput input) {
169 return sendToSwitchFuture(input, "hello-input sending failed");
173 public Future<RpcResult<Void>> meterMod(MeterModInput input) {
174 return sendToSwitchFuture(input, "meter-mod-input sending failed");
178 public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
179 return sendToSwitchFuture(input, "packet-out-input sending failed");
183 public Future<RpcResult<Void>> portMod(PortModInput input) {
184 return sendToSwitchFuture(input, "port-mod-input sending failed");
188 public Future<RpcResult<RoleRequestOutput>> roleRequest(
189 RoleRequestInput input) {
190 return sendToSwitchExpectRpcResultFuture(
191 input, RoleRequestOutput.class, "role-request-config-input sending failed");
195 public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
196 return sendToSwitchFuture(input, "set-config-input sending failed");
200 public Future<RpcResult<Void>> tableMod(TableModInput input) {
201 return sendToSwitchFuture(input, "table-mod-input sending failed");
205 public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
206 return sendToSwitchExpectRpcResultFuture(
207 input, GetAsyncOutput.class, "get-async-input sending failed");
211 public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
212 return sendToSwitchFuture(input, "set-async-input sending failed");
216 public Future<Boolean> disconnect() {
217 ChannelFuture disconnectResult = channel.disconnect();
218 responseCache.invalidateAll();
219 disconnectOccured = true;
221 String failureInfo = "switch disconnecting failed";
222 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
223 String message = "Check the switch connection";
224 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
228 public boolean isAlive() {
229 return channel.isOpen();
233 public void setMessageListener(OpenflowProtocolListener messageListener) {
234 this.messageListener = messageListener;
238 public void consume(final DataObject message) {
239 threadPool.execute(new Runnable() {
242 consumeIntern(message);
247 protected void consumeIntern(final DataObject message) {
248 LOG.debug("Consume msg");
249 if (disconnectOccured ) {
252 if (message instanceof Notification) {
254 if (message instanceof DisconnectEvent) {
255 systemListener.onDisconnectEvent((DisconnectEvent) message);
256 responseCache.invalidateAll();
257 disconnectOccured = true;
258 } else if (message instanceof SwitchIdleEvent) {
259 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
262 else if (message instanceof EchoRequestMessage) {
263 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
264 } else if (message instanceof ErrorMessage) {
265 messageListener.onErrorMessage((ErrorMessage) message);
266 } else if (message instanceof ExperimenterMessage) {
267 messageListener.onExperimenterMessage((ExperimenterMessage) message);
268 } else if (message instanceof FlowRemovedMessage) {
269 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
270 } else if (message instanceof HelloMessage) {
271 LOG.info("Hello received / branch");
272 messageListener.onHelloMessage((HelloMessage) message);
273 } else if (message instanceof MultipartReplyMessage) {
274 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
275 } else if (message instanceof MultipartRequestMessage) {
276 messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
277 } else if (message instanceof PacketInMessage) {
278 messageListener.onPacketInMessage((PacketInMessage) message);
279 } else if (message instanceof PortStatusMessage) {
280 messageListener.onPortStatusMessage((PortStatusMessage) message);
282 LOG.warn("message listening not supported for type: "+message.getClass());
285 if (message instanceof OfHeader) {
286 LOG.debug("OFheader msg received");
287 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
288 final SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
289 if (rpcFuture != null) {
290 LOG.debug("corresponding rpcFuture found");
291 List<RpcError> errors = Collections.emptyList();
292 LOG.debug("before setting rpcFuture");
293 rpcFuture.set(Rpcs.getRpcResult(true, message, errors));
294 LOG.debug("after setting rpcFuture");
295 responseCache.invalidate(key);
297 LOG.warn("received unexpected rpc response: "+key);
300 LOG.warn("message listening not supported for type: "+message.getClass());
306 * sends given message to switch, sending result will be reported via return value
307 * @param input message to send
308 * @param failureInfo describes, what type of message caused failure by sending
309 * @return future object, <ul>
310 * <li>if send successful, {@link RpcResult} without errors and successful
311 * status will be returned, </li>
312 * <li>else {@link RpcResult} will contain errors and failed status</li>
315 private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
316 DataObject input, final String failureInfo) {
317 LOG.debug("going to flush");
318 ChannelFuture resultFuture = channel.writeAndFlush(input);
319 LOG.debug("flushed");
321 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
322 String errorMessage = "check switch connection";
323 return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
327 * sends given message to switch, sending result or switch response will be reported via return value
328 * @param input message to send
329 * @param responseClazz type of response
330 * @param failureInfo describes, what type of message caused failure by sending
331 * @return future object, <ul>
332 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
333 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
334 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
335 * <ul><li>either switch will manage to answer
336 * and then corresponding response message will be set into returned future</li>
337 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
341 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
342 IN input, Class<OUT> responseClazz, final String failureInfo) {
343 LOG.debug("going to flush");
344 SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
345 RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz);
346 responseCache.put(key, rpcResult);
347 ChannelFuture resultFuture = channel.writeAndFlush(input);
348 LOG.debug("flushed");
350 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
351 String errorMessage = "check switch connection";
353 return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity,
354 errorMessage, input, responseClazz, rpcResult, key);
358 * @param resultFuture
362 private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
363 ChannelFuture resultFuture, final String failureInfo,
364 final ErrorSeverity errorSeverity, final String errorMessage) {
366 final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
367 LOG.debug("handlerpcchannelfuture");
368 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
371 public void operationComplete(
372 io.netty.util.concurrent.Future<? super Void> future)
374 LOG.debug("operation complete");
375 Collection<RpcError> errors = Collections.emptyList();
377 if (future.cause() != null) {
378 LOG.debug("future.cause != null");
379 RpcError rpcError = buildRpcError(failureInfo,
380 errorSeverity, errorMessage, future.cause());
381 errors = Lists.newArrayList(rpcError);
384 rpcResult.set(Rpcs.getRpcResult(
395 * @param resultFuture
397 * @param errorSeverity
398 * @param errorMessage
400 * @param responseClazz
405 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
406 ChannelFuture resultFuture, final String failureInfo,
407 final ErrorSeverity errorSeverity, final String errorMessage,
408 final IN input, Class<OUT> responseClazz, final SettableFuture<RpcResult<OUT>> rpcResult, final RpcResponseKey key) {
409 LOG.debug("handleRpcchanfuture with response");
411 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
414 public void operationComplete(
415 io.netty.util.concurrent.Future<? super Void> future)
418 LOG.debug("operation complete");
419 Collection<RpcError> errors = Collections.emptyList();
420 if (future.cause() != null) {
421 LOG.debug("ChannelFuture.cause != null");
422 RpcError rpcError = buildRpcError(failureInfo,
423 errorSeverity, errorMessage, future.cause());
424 errors = Lists.newArrayList(rpcError);
425 rpcResult.set(Rpcs.getRpcResult(
430 responseCache.invalidate(key);
432 LOG.debug("ChannelFuture.cause == null");
433 if (responseCache.getIfPresent(key) == null) {
434 LOG.debug("responcache: key wasn't present");
443 * @param resultFuture
445 * @param errorSeverity
449 private static SettableFuture<Boolean> handleTransportChannelFuture(
450 ChannelFuture resultFuture, final String failureInfo,
451 final ErrorSeverity errorSeverity, final String message) {
453 final SettableFuture<Boolean> transportResult = SettableFuture.create();
455 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
458 public void operationComplete(
459 io.netty.util.concurrent.Future<? super Void> future)
461 transportResult.set(future.isSuccess());
462 if (!future.isSuccess()) {
463 transportResult.setException(future.cause());
467 return transportResult;
474 protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
476 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
477 ErrorType.RPC, cause);
485 protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
487 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
488 ErrorType.TRANSPORT, cause);
496 private static RpcResponseKey createRpcResponseKey(OfHeader message) {
497 return new RpcResponseKey(message.getXid(), message.getClass());
503 @SuppressWarnings("unchecked")
504 private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
505 return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
509 public void setSystemListener(SystemNotificationsListener systemListener) {
510 this.systemListener = systemListener;
514 public void checkListeners() {
515 StringBuffer buffer = new StringBuffer();
516 if (systemListener == null) {
517 buffer.append("SystemListener ");
519 if (messageListener == null) {
520 buffer.append("MessageListener ");
523 if (buffer.length() > 0) {
524 throw new IllegalStateException("Missing listeners: " + buffer.toString());
528 static class ResponseRemovalListener implements RemovalListener<RpcResponseKey, SettableFuture<?>> {
531 public void onRemoval(
532 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
533 SettableFuture<?> future = notification.getValue();
534 if (!future.isDone()) {
535 LOG.warn("rpc response discarded: " + notification.getKey());