2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.openflowjava.protocol.impl.connection;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.util.concurrent.GenericFutureListener;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.RejectedExecutionException;
19 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.controller.sal.common.util.RpcErrors;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
62 import org.opendaylight.yangtools.yang.binding.DataObject;
63 import org.opendaylight.yangtools.yang.binding.Notification;
64 import org.opendaylight.yangtools.yang.common.RpcError;
65 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
66 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
67 import org.opendaylight.yangtools.yang.common.RpcResult;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 import com.google.common.base.Preconditions;
72 import com.google.common.cache.Cache;
73 import com.google.common.cache.CacheBuilder;
74 import com.google.common.cache.RemovalListener;
75 import com.google.common.cache.RemovalNotification;
76 import com.google.common.util.concurrent.ListenableFuture;
77 import com.google.common.util.concurrent.SettableFuture;
80 * Handles messages (notifications + rpcs) and connections
82 * @author michal.polkorab
84 public class ConnectionAdapterImpl implements ConnectionFacade {
85 /** after this time, RPC future response objects will be thrown away (in minutes) */
86 public static final int RPC_RESPONSE_EXPIRATION = 1;
89 * Default depth of write queue, e.g. we allow these many messages
90 * to be queued up before blocking producers.
92 public static final int DEFAULT_QUEUE_DEPTH = 1024;
94 private static final Logger LOG = LoggerFactory
95 .getLogger(ConnectionAdapterImpl.class);
96 private static final Exception QUEUE_FULL_EXCEPTION =
97 new RejectedExecutionException("Output queue is full");
99 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
100 private static final String TAG = "OPENFLOW";
101 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
102 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
104 public void onRemoval(
105 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
106 notification.getValue().discard();
110 /** expiring cache for future rpcResponses */
111 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache;
113 private final ChannelOutboundQueue output;
114 private final Channel channel;
116 private ConnectionReadyListener connectionReadyListener;
117 private OpenflowProtocolListener messageListener;
118 private SystemNotificationsListener systemListener;
119 private boolean disconnectOccured = false;
123 * @param channel the channel to be set - used for communication
124 * @param address client address (used only in case of UDP communication,
125 * as there is no need to store address over tcp (stable channel))
127 public ConnectionAdapterImpl(final Channel channel, final InetSocketAddress address) {
128 responseCache = CacheBuilder.newBuilder()
130 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
131 .removalListener(REMOVAL_LISTENER).build();
132 this.channel = Preconditions.checkNotNull(channel);
133 this.output = new ChannelOutboundQueue(channel, DEFAULT_QUEUE_DEPTH);
134 output.setAddress(address);
135 channel.pipeline().addLast(output);
136 LOG.debug("ConnectionAdapter created");
140 public Future<RpcResult<BarrierOutput>> barrier(final BarrierInput input) {
141 return sendToSwitchExpectRpcResultFuture(
142 input, BarrierOutput.class, "barrier-input sending failed");
146 public Future<RpcResult<EchoOutput>> echo(final EchoInput input) {
147 return sendToSwitchExpectRpcResultFuture(
148 input, EchoOutput.class, "echo-input sending failed");
152 public Future<RpcResult<Void>> echoReply(final EchoReplyInput input) {
153 return sendToSwitchFuture(input, "echo-reply sending failed");
157 public Future<RpcResult<Void>> experimenter(final ExperimenterInput input) {
158 return sendToSwitchFuture(input, "experimenter sending failed");
162 public Future<RpcResult<Void>> flowMod(final FlowModInput input) {
163 return sendToSwitchFuture(input, "flow-mod sending failed");
167 public Future<RpcResult<GetConfigOutput>> getConfig(final GetConfigInput input) {
168 return sendToSwitchExpectRpcResultFuture(
169 input, GetConfigOutput.class, "get-config-input sending failed");
173 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
174 final GetFeaturesInput input) {
175 return sendToSwitchExpectRpcResultFuture(
176 input, GetFeaturesOutput.class, "get-features-input sending failed");
180 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
181 final GetQueueConfigInput input) {
182 return sendToSwitchExpectRpcResultFuture(
183 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
187 public Future<RpcResult<Void>> groupMod(final GroupModInput input) {
188 return sendToSwitchFuture(input, "group-mod-input sending failed");
192 public Future<RpcResult<Void>> hello(final HelloInput input) {
193 return sendToSwitchFuture(input, "hello-input sending failed");
197 public Future<RpcResult<Void>> meterMod(final MeterModInput input) {
198 return sendToSwitchFuture(input, "meter-mod-input sending failed");
202 public Future<RpcResult<Void>> packetOut(final PacketOutInput input) {
203 return sendToSwitchFuture(input, "packet-out-input sending failed");
207 public Future<RpcResult<Void>> multipartRequest(final MultipartRequestInput input) {
208 return sendToSwitchFuture(input, "multi-part-request sending failed");
212 public Future<RpcResult<Void>> portMod(final PortModInput input) {
213 return sendToSwitchFuture(input, "port-mod-input sending failed");
217 public Future<RpcResult<RoleRequestOutput>> roleRequest(
218 final RoleRequestInput input) {
219 return sendToSwitchExpectRpcResultFuture(
220 input, RoleRequestOutput.class, "role-request-config-input sending failed");
224 public Future<RpcResult<Void>> setConfig(final SetConfigInput input) {
225 return sendToSwitchFuture(input, "set-config-input sending failed");
229 public Future<RpcResult<Void>> tableMod(final TableModInput input) {
230 return sendToSwitchFuture(input, "table-mod-input sending failed");
234 public Future<RpcResult<GetAsyncOutput>> getAsync(final GetAsyncInput input) {
235 return sendToSwitchExpectRpcResultFuture(
236 input, GetAsyncOutput.class, "get-async-input sending failed");
240 public Future<RpcResult<Void>> setAsync(final SetAsyncInput input) {
241 return sendToSwitchFuture(input, "set-async-input sending failed");
245 public Future<Boolean> disconnect() {
246 ChannelFuture disconnectResult = channel.disconnect();
247 responseCache.invalidateAll();
248 disconnectOccured = true;
250 String failureInfo = "switch disconnecting failed";
251 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
252 String message = "Check the switch connection";
253 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
257 public boolean isAlive() {
258 return channel.isOpen();
262 public void setMessageListener(final OpenflowProtocolListener messageListener) {
263 this.messageListener = messageListener;
267 public void consume(final DataObject message) {
268 LOG.debug("ConsumeIntern msg");
269 if (disconnectOccured ) {
272 if (message instanceof Notification) {
274 if (message instanceof DisconnectEvent) {
275 systemListener.onDisconnectEvent((DisconnectEvent) message);
276 responseCache.invalidateAll();
277 disconnectOccured = true;
278 } else if (message instanceof SwitchIdleEvent) {
279 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
282 else if (message instanceof EchoRequestMessage) {
283 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
284 } else if (message instanceof ErrorMessage) {
285 messageListener.onErrorMessage((ErrorMessage) message);
286 } else if (message instanceof ExperimenterMessage) {
287 messageListener.onExperimenterMessage((ExperimenterMessage) message);
288 } else if (message instanceof FlowRemovedMessage) {
289 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
290 } else if (message instanceof HelloMessage) {
291 LOG.info("Hello received / branch");
292 messageListener.onHelloMessage((HelloMessage) message);
293 } else if (message instanceof MultipartReplyMessage) {
294 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
295 } else if (message instanceof PacketInMessage) {
296 messageListener.onPacketInMessage((PacketInMessage) message);
297 } else if (message instanceof PortStatusMessage) {
298 messageListener.onPortStatusMessage((PortStatusMessage) message);
300 LOG.warn("message listening not supported for type: {}", message.getClass());
303 if (message instanceof OfHeader) {
304 LOG.debug("OFheader msg received");
305 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
306 final ResponseExpectedRpcListener<?> listener = findRpcResponse(key);
307 if (listener != null) {
308 LOG.debug("corresponding rpcFuture found");
309 listener.completed((OfHeader)message);
310 LOG.debug("after setting rpcFuture");
311 responseCache.invalidate(key);
313 LOG.warn("received unexpected rpc response: {}", key);
316 LOG.warn("message listening not supported for type: {}", message.getClass());
321 private <T> ListenableFuture<RpcResult<T>> enqueueMessage(final AbstractRpcListener<T> promise) {
322 LOG.debug("Submitting promise {}", promise);
324 if (!output.enqueue(promise)) {
325 LOG.debug("Message queue is full, rejecting execution");
326 promise.failedRpc(QUEUE_FULL_EXCEPTION);
328 LOG.debug("Promise enqueued successfully");
331 return promise.getResult();
335 * sends given message to switch, sending result will be reported via return value
336 * @param input message to send
337 * @param failureInfo describes, what type of message caused failure by sending
338 * @return future object, <ul>
339 * <li>if send successful, {@link RpcResult} without errors and successful
340 * status will be returned, </li>
341 * <li>else {@link RpcResult} will contain errors and failed status</li>
344 private ListenableFuture<RpcResult<Void>> sendToSwitchFuture(
345 final DataObject input, final String failureInfo) {
346 return enqueueMessage(new SimpleRpcListener(input, failureInfo));
350 * sends given message to switch, sending result or switch response will be reported via return value
351 * @param input message to send
352 * @param responseClazz type of response
353 * @param failureInfo describes, what type of message caused failure by sending
354 * @return future object, <ul>
355 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
356 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
357 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
358 * <ul><li>either switch will manage to answer
359 * and then corresponding response message will be set into returned future</li>
360 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
364 private <IN extends OfHeader, OUT extends OfHeader> ListenableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
365 final IN input, final Class<OUT> responseClazz, final String failureInfo) {
366 final RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName());
367 final ResponseExpectedRpcListener<OUT> listener =
368 new ResponseExpectedRpcListener<>(input, failureInfo, responseCache, key);
369 return enqueueMessage(listener);
373 * @param resultFuture
375 * @param errorSeverity
379 private static SettableFuture<Boolean> handleTransportChannelFuture(
380 final ChannelFuture resultFuture, final String failureInfo,
381 final ErrorSeverity errorSeverity, final String message) {
383 final SettableFuture<Boolean> transportResult = SettableFuture.create();
385 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
388 public void operationComplete(
389 final io.netty.util.concurrent.Future<? super Void> future)
391 transportResult.set(future.isSuccess());
392 if (!future.isSuccess()) {
393 transportResult.setException(future.cause());
397 return transportResult;
404 static RpcError buildRpcError(final String info, final ErrorSeverity severity, final String message,
405 final Throwable cause) {
406 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
407 ErrorType.RPC, cause);
415 protected static RpcError buildTransportError(final String info, final ErrorSeverity severity, final String message,
416 final Throwable cause) {
417 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
418 ErrorType.TRANSPORT, cause);
426 private static RpcResponseKey createRpcResponseKey(final OfHeader message) {
427 return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName());
433 private ResponseExpectedRpcListener<?> findRpcResponse(final RpcResponseKey key) {
434 return responseCache.getIfPresent(key);
438 public void setSystemListener(final SystemNotificationsListener systemListener) {
439 this.systemListener = systemListener;
443 public void checkListeners() {
444 final StringBuilder buffer = new StringBuilder();
445 if (systemListener == null) {
446 buffer.append("SystemListener ");
448 if (messageListener == null) {
449 buffer.append("MessageListener ");
451 if (connectionReadyListener == null) {
452 buffer.append("ConnectionReadyListener ");
455 Preconditions.checkState(buffer.length() == 0, "Missing listeners: %s", buffer.toString());
459 public void fireConnectionReadyNotification() {
460 new Thread(new Runnable() {
463 connectionReadyListener.onConnectionReady();
469 public void setConnectionReadyListener(
470 final ConnectionReadyListener connectionReadyListener) {
471 this.connectionReadyListener = connectionReadyListener;
475 public InetSocketAddress getRemoteAddress() {
476 return (InetSocketAddress) channel.remoteAddress();
480 * Used only for testing purposes
483 public void setResponseCache(Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache) {
484 this.responseCache = cache;