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.core.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.openflowjava.protocol.api.connection.ConnectionReadyListener;
22 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
23 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
63 import org.opendaylight.yangtools.yang.binding.DataObject;
64 import org.opendaylight.yangtools.yang.binding.Notification;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 import com.google.common.base.Preconditions;
70 import com.google.common.cache.Cache;
71 import com.google.common.cache.CacheBuilder;
72 import com.google.common.cache.RemovalCause;
73 import com.google.common.cache.RemovalListener;
74 import com.google.common.cache.RemovalNotification;
75 import com.google.common.util.concurrent.ListenableFuture;
76 import com.google.common.util.concurrent.SettableFuture;
79 * Handles messages (notifications + rpcs) and connections
81 * @author michal.polkorab
83 public class ConnectionAdapterImpl implements ConnectionFacade {
84 /** after this time, RPC future response objects will be thrown away (in minutes) */
85 public static final int RPC_RESPONSE_EXPIRATION = 1;
88 * Default depth of write queue, e.g. we allow these many messages
89 * to be queued up before blocking producers.
91 public static final int DEFAULT_QUEUE_DEPTH = 1024;
93 private static final Logger LOG = LoggerFactory
94 .getLogger(ConnectionAdapterImpl.class);
95 private static final Exception QUEUE_FULL_EXCEPTION =
96 new RejectedExecutionException("Output queue is full");
98 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
99 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
101 public void onRemoval(
102 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
103 if (! notification.getCause().equals(RemovalCause.EXPLICIT)) {
104 notification.getValue().discard();
109 /** expiring cache for future rpcResponses */
110 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache;
112 private final ChannelOutboundQueue output;
113 private final Channel channel;
115 private ConnectionReadyListener connectionReadyListener;
116 private OpenflowProtocolListener messageListener;
117 private SystemNotificationsListener systemListener;
118 private boolean disconnectOccured = false;
119 private StatisticsCounters statisticsCounters;
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, address);
134 channel.pipeline().addLast(output);
135 statisticsCounters = StatisticsCounters.getInstance();
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 statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_ENTERED);
164 return sendToSwitchFuture(input, "flow-mod sending failed");
168 public Future<RpcResult<GetConfigOutput>> getConfig(final GetConfigInput input) {
169 return sendToSwitchExpectRpcResultFuture(
170 input, GetConfigOutput.class, "get-config-input sending failed");
174 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
175 final GetFeaturesInput input) {
176 return sendToSwitchExpectRpcResultFuture(
177 input, GetFeaturesOutput.class, "get-features-input sending failed");
181 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
182 final GetQueueConfigInput input) {
183 return sendToSwitchExpectRpcResultFuture(
184 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
188 public Future<RpcResult<Void>> groupMod(final GroupModInput input) {
189 return sendToSwitchFuture(input, "group-mod-input sending failed");
193 public Future<RpcResult<Void>> hello(final HelloInput input) {
194 return sendToSwitchFuture(input, "hello-input sending failed");
198 public Future<RpcResult<Void>> meterMod(final MeterModInput input) {
199 return sendToSwitchFuture(input, "meter-mod-input sending failed");
203 public Future<RpcResult<Void>> packetOut(final PacketOutInput input) {
204 return sendToSwitchFuture(input, "packet-out-input sending failed");
208 public Future<RpcResult<Void>> multipartRequest(final MultipartRequestInput input) {
209 return sendToSwitchFuture(input, "multi-part-request sending failed");
213 public Future<RpcResult<Void>> portMod(final PortModInput input) {
214 return sendToSwitchFuture(input, "port-mod-input sending failed");
218 public Future<RpcResult<RoleRequestOutput>> roleRequest(
219 final RoleRequestInput input) {
220 return sendToSwitchExpectRpcResultFuture(
221 input, RoleRequestOutput.class, "role-request-config-input sending failed");
225 public Future<RpcResult<Void>> setConfig(final SetConfigInput input) {
226 return sendToSwitchFuture(input, "set-config-input sending failed");
230 public Future<RpcResult<Void>> tableMod(final TableModInput input) {
231 return sendToSwitchFuture(input, "table-mod-input sending failed");
235 public Future<RpcResult<GetAsyncOutput>> getAsync(final GetAsyncInput input) {
236 return sendToSwitchExpectRpcResultFuture(
237 input, GetAsyncOutput.class, "get-async-input sending failed");
241 public Future<RpcResult<Void>> setAsync(final SetAsyncInput input) {
242 return sendToSwitchFuture(input, "set-async-input sending failed");
246 public Future<Boolean> disconnect() {
247 ChannelFuture disconnectResult = channel.disconnect();
248 responseCache.invalidateAll();
249 disconnectOccured = true;
251 return handleTransportChannelFuture(disconnectResult);
255 public boolean isAlive() {
256 return channel.isOpen();
260 public void setMessageListener(final OpenflowProtocolListener messageListener) {
261 this.messageListener = messageListener;
265 public void consume(final DataObject message) {
266 LOG.debug("ConsumeIntern msg");
267 if (disconnectOccured ) {
270 if (message instanceof Notification) {
273 if (message instanceof DisconnectEvent) {
274 systemListener.onDisconnectEvent((DisconnectEvent) message);
275 responseCache.invalidateAll();
276 disconnectOccured = true;
277 } else if (message instanceof SwitchIdleEvent) {
278 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
280 } else if (message instanceof EchoRequestMessage) {
281 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
282 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
283 } else if (message instanceof ErrorMessage) {
284 messageListener.onErrorMessage((ErrorMessage) message);
285 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
286 } else if (message instanceof ExperimenterMessage) {
287 messageListener.onExperimenterMessage((ExperimenterMessage) message);
288 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
289 } else if (message instanceof FlowRemovedMessage) {
290 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
291 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
292 } else if (message instanceof HelloMessage) {
293 LOG.info("Hello received / branch");
294 messageListener.onHelloMessage((HelloMessage) message);
295 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
296 } else if (message instanceof MultipartReplyMessage) {
297 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
298 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
299 } else if (message instanceof PacketInMessage) {
300 messageListener.onPacketInMessage((PacketInMessage) message);
301 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
302 } else if (message instanceof PortStatusMessage) {
303 messageListener.onPortStatusMessage((PortStatusMessage) message);
304 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
306 LOG.warn("message listening not supported for type: {}", message.getClass());
309 if (message instanceof OfHeader) {
310 LOG.debug("OFheader msg received");
311 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
312 final ResponseExpectedRpcListener<?> listener = findRpcResponse(key);
313 if (listener != null) {
314 LOG.debug("corresponding rpcFuture found");
315 listener.completed((OfHeader)message);
316 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
317 LOG.debug("after setting rpcFuture");
318 responseCache.invalidate(key);
320 LOG.warn("received unexpected rpc response: {}", key);
323 LOG.warn("message listening not supported for type: {}", message.getClass());
328 private <T> ListenableFuture<RpcResult<T>> enqueueMessage(final AbstractRpcListener<T> promise) {
329 LOG.debug("Submitting promise {}", promise);
331 if (!output.enqueue(promise)) {
332 LOG.debug("Message queue is full, rejecting execution");
333 promise.failedRpc(QUEUE_FULL_EXCEPTION);
335 LOG.debug("Promise enqueued successfully");
338 return promise.getResult();
342 * sends given message to switch, sending result will be reported via return value
343 * @param input message to send
344 * @param failureInfo describes, what type of message caused failure by sending
345 * @return future object, <ul>
346 * <li>if send successful, {@link RpcResult} without errors and successful
347 * status will be returned, </li>
348 * <li>else {@link RpcResult} will contain errors and failed status</li>
351 private ListenableFuture<RpcResult<Void>> sendToSwitchFuture(
352 final DataObject input, final String failureInfo) {
353 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA);
354 return enqueueMessage(new SimpleRpcListener(input, failureInfo));
358 * sends given message to switch, sending result or switch response will be reported via return value
359 * @param input message to send
360 * @param responseClazz type of response
361 * @param failureInfo describes, what type of message caused failure by sending
362 * @return future object, <ul>
363 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
364 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
365 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
366 * <ul><li>either switch will manage to answer
367 * and then corresponding response message will be set into returned future</li>
368 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
372 private <IN extends OfHeader, OUT extends OfHeader> ListenableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
373 final IN input, final Class<OUT> responseClazz, final String failureInfo) {
374 final RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName());
375 final ResponseExpectedRpcListener<OUT> listener =
376 new ResponseExpectedRpcListener<>(input, failureInfo, responseCache, key);
377 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA);
378 return enqueueMessage(listener);
382 * @param resultFuture
384 * @param errorSeverity
388 private static SettableFuture<Boolean> handleTransportChannelFuture(
389 final ChannelFuture resultFuture) {
391 final SettableFuture<Boolean> transportResult = SettableFuture.create();
393 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
396 public void operationComplete(
397 final io.netty.util.concurrent.Future<? super Void> future)
399 transportResult.set(future.isSuccess());
400 if (!future.isSuccess()) {
401 transportResult.setException(future.cause());
405 return transportResult;
412 private static RpcResponseKey createRpcResponseKey(final OfHeader message) {
413 return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName());
419 private ResponseExpectedRpcListener<?> findRpcResponse(final RpcResponseKey key) {
420 return responseCache.getIfPresent(key);
424 public void setSystemListener(final SystemNotificationsListener systemListener) {
425 this.systemListener = systemListener;
429 public void checkListeners() {
430 final StringBuilder buffer = new StringBuilder();
431 if (systemListener == null) {
432 buffer.append("SystemListener ");
434 if (messageListener == null) {
435 buffer.append("MessageListener ");
437 if (connectionReadyListener == null) {
438 buffer.append("ConnectionReadyListener ");
441 Preconditions.checkState(buffer.length() == 0, "Missing listeners: %s", buffer.toString());
445 public void fireConnectionReadyNotification() {
446 new Thread(new Runnable() {
449 connectionReadyListener.onConnectionReady();
455 public void setConnectionReadyListener(
456 final ConnectionReadyListener connectionReadyListener) {
457 this.connectionReadyListener = connectionReadyListener;
461 public InetSocketAddress getRemoteAddress() {
462 return (InetSocketAddress) channel.remoteAddress();
466 * Used only for testing purposes
469 public void setResponseCache(Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache) {
470 this.responseCache = cache;
474 public boolean isAutoRead() {
475 return channel.config().isAutoRead();
479 public void setAutoRead(boolean autoRead) {
480 channel.config().setAutoRead(autoRead);