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 com.google.common.base.Preconditions;
13 import com.google.common.cache.Cache;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.RemovalCause;
16 import com.google.common.cache.RemovalListener;
17 import com.google.common.cache.RemovalNotification;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.SettableFuture;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.util.concurrent.GenericFutureListener;
23 import java.net.InetSocketAddress;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.RejectedExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
28 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandler;
29 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
30 import org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector;
31 import org.opendaylight.openflowjava.protocol.impl.core.PipelineHandlers;
32 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
33 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
73 import org.opendaylight.yangtools.yang.binding.DataObject;
74 import org.opendaylight.yangtools.yang.binding.Notification;
75 import org.opendaylight.yangtools.yang.common.RpcResult;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
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 RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
100 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
102 public void onRemoval(
103 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
104 if (! notification.getCause().equals(RemovalCause.EXPLICIT)) {
105 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 OutboundQueueManager<?> outputManager;
120 private boolean disconnectOccured = false;
121 private final StatisticsCounters statisticsCounters;
122 private OFVersionDetector versionDetector;
123 private final InetSocketAddress address;
125 private final boolean useBarrier;
129 * @param channel the channel to be set - used for communication
130 * @param address client address (used only in case of UDP communication,
131 * as there is no need to store address over tcp (stable channel))
133 public ConnectionAdapterImpl(final Channel channel, final InetSocketAddress address, final boolean useBarrier) {
134 this.channel = Preconditions.checkNotNull(channel);
135 this.output = new ChannelOutboundQueue(channel, DEFAULT_QUEUE_DEPTH, address);
136 this.address = address;
138 responseCache = CacheBuilder.newBuilder()
140 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
141 .removalListener(REMOVAL_LISTENER).build();
143 this.useBarrier = useBarrier;
144 channel.pipeline().addLast(output);
145 statisticsCounters = StatisticsCounters.getInstance();
147 LOG.debug("ConnectionAdapter created");
151 public Future<RpcResult<BarrierOutput>> barrier(final BarrierInput input) {
152 return sendToSwitchExpectRpcResultFuture(
153 input, BarrierOutput.class, "barrier-input sending failed");
157 public Future<RpcResult<EchoOutput>> echo(final EchoInput input) {
158 return sendToSwitchExpectRpcResultFuture(
159 input, EchoOutput.class, "echo-input sending failed");
163 public Future<RpcResult<Void>> echoReply(final EchoReplyInput input) {
164 return sendToSwitchFuture(input, "echo-reply sending failed");
168 public Future<RpcResult<Void>> experimenter(final ExperimenterInput input) {
169 return sendToSwitchFuture(input, "experimenter reply sending failed");
173 public Future<RpcResult<Void>> flowMod(final FlowModInput input) {
174 statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_ENTERED);
175 return sendToSwitchFuture(input, "flow-mod sending failed");
179 public Future<RpcResult<GetConfigOutput>> getConfig(final GetConfigInput input) {
180 return sendToSwitchExpectRpcResultFuture(
181 input, GetConfigOutput.class, "get-config-input sending failed");
185 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
186 final GetFeaturesInput input) {
187 return sendToSwitchExpectRpcResultFuture(
188 input, GetFeaturesOutput.class, "get-features-input sending failed");
192 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
193 final GetQueueConfigInput input) {
194 return sendToSwitchExpectRpcResultFuture(
195 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
199 public Future<RpcResult<Void>> groupMod(final GroupModInput input) {
200 return sendToSwitchFuture(input, "group-mod-input sending failed");
204 public Future<RpcResult<Void>> hello(final HelloInput input) {
205 return sendToSwitchFuture(input, "hello-input sending failed");
209 public Future<RpcResult<Void>> meterMod(final MeterModInput input) {
210 return sendToSwitchFuture(input, "meter-mod-input sending failed");
214 public Future<RpcResult<Void>> packetOut(final PacketOutInput input) {
215 return sendToSwitchFuture(input, "packet-out-input sending failed");
219 public Future<RpcResult<Void>> multipartRequest(final MultipartRequestInput input) {
220 return sendToSwitchFuture(input, "multi-part-request sending failed");
224 public Future<RpcResult<Void>> portMod(final PortModInput input) {
225 return sendToSwitchFuture(input, "port-mod-input sending failed");
229 public Future<RpcResult<RoleRequestOutput>> roleRequest(
230 final RoleRequestInput input) {
231 return sendToSwitchExpectRpcResultFuture(
232 input, RoleRequestOutput.class, "role-request-config-input sending failed");
236 public Future<RpcResult<Void>> setConfig(final SetConfigInput input) {
237 return sendToSwitchFuture(input, "set-config-input sending failed");
241 public Future<RpcResult<Void>> tableMod(final TableModInput input) {
242 return sendToSwitchFuture(input, "table-mod-input sending failed");
246 public Future<RpcResult<GetAsyncOutput>> getAsync(final GetAsyncInput input) {
247 return sendToSwitchExpectRpcResultFuture(
248 input, GetAsyncOutput.class, "get-async-input sending failed");
252 public Future<RpcResult<Void>> setAsync(final SetAsyncInput input) {
253 return sendToSwitchFuture(input, "set-async-input sending failed");
257 public Future<Boolean> disconnect() {
258 final ChannelFuture disconnectResult = channel.disconnect();
259 responseCache.invalidateAll();
260 disconnectOccured = true;
262 return handleTransportChannelFuture(disconnectResult);
266 public boolean isAlive() {
267 return channel.isOpen();
271 public void setMessageListener(final OpenflowProtocolListener messageListener) {
272 this.messageListener = messageListener;
276 public void consume(final DataObject message) {
277 LOG.debug("ConsumeIntern msg on {}", channel);
278 if (disconnectOccured ) {
281 if (message instanceof Notification) {
284 if (message instanceof DisconnectEvent) {
285 systemListener.onDisconnectEvent((DisconnectEvent) message);
286 responseCache.invalidateAll();
287 disconnectOccured = true;
288 } else if (message instanceof SwitchIdleEvent) {
289 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
291 } else if (message instanceof EchoRequestMessage) {
292 if (outputManager != null) {
293 outputManager.onEchoRequest((EchoRequestMessage) message);
295 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
297 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
298 } else if (message instanceof ErrorMessage) {
299 // Send only unmatched errors
300 if (outputManager == null || !outputManager.onMessage((OfHeader) message)) {
301 messageListener.onErrorMessage((ErrorMessage) message);
303 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
304 } else if (message instanceof ExperimenterMessage) {
305 if (outputManager != null) {
306 outputManager.onMessage((OfHeader) message);
308 messageListener.onExperimenterMessage((ExperimenterMessage) message);
309 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
310 } else if (message instanceof FlowRemovedMessage) {
311 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
312 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
313 } else if (message instanceof HelloMessage) {
314 LOG.info("Hello received / branch");
315 messageListener.onHelloMessage((HelloMessage) message);
316 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
317 } else if (message instanceof MultipartReplyMessage) {
318 if (outputManager != null) {
319 outputManager.onMessage((OfHeader) message);
321 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
322 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
323 } else if (message instanceof PacketInMessage) {
324 messageListener.onPacketInMessage((PacketInMessage) message);
325 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
326 } else if (message instanceof PortStatusMessage) {
327 messageListener.onPortStatusMessage((PortStatusMessage) message);
328 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
330 LOG.warn("message listening not supported for type: {}", message.getClass());
332 } else if (message instanceof OfHeader) {
333 LOG.debug("OFheader msg received");
335 if (outputManager == null || !outputManager.onMessage((OfHeader) message)) {
336 final RpcResponseKey key = createRpcResponseKey((OfHeader) message);
337 final ResponseExpectedRpcListener<?> listener = findRpcResponse(key);
338 if (listener != null) {
339 LOG.debug("corresponding rpcFuture found");
340 listener.completed((OfHeader)message);
341 statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS);
342 LOG.debug("after setting rpcFuture");
343 responseCache.invalidate(key);
345 LOG.warn("received unexpected rpc response: {}", key);
349 LOG.warn("message listening not supported for type: {}", message.getClass());
353 private <T> ListenableFuture<RpcResult<T>> enqueueMessage(final AbstractRpcListener<T> promise) {
354 LOG.debug("Submitting promise {}", promise);
356 if (!output.enqueue(promise)) {
357 LOG.debug("Message queue is full, rejecting execution");
358 promise.failedRpc(QUEUE_FULL_EXCEPTION);
360 LOG.debug("Promise enqueued successfully");
363 return promise.getResult();
367 * sends given message to switch, sending result will be reported via return value
368 * @param input message to send
369 * @param failureInfo describes, what type of message caused failure by sending
370 * @return future object, <ul>
371 * <li>if send successful, {@link RpcResult} without errors and successful
372 * status will be returned, </li>
373 * <li>else {@link RpcResult} will contain errors and failed status</li>
376 private ListenableFuture<RpcResult<Void>> sendToSwitchFuture(
377 final DataObject input, final String failureInfo) {
378 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA);
379 return enqueueMessage(new SimpleRpcListener(input, failureInfo));
383 * sends given message to switch, sending result or switch response will be reported via return value
384 * @param input message to send
385 * @param responseClazz type of response
386 * @param failureInfo describes, what type of message caused failure by sending
387 * @return future object, <ul>
388 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
389 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
390 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
391 * <ul><li>either switch will manage to answer
392 * and then corresponding response message will be set into returned future</li>
393 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
397 private <IN extends OfHeader, OUT extends OfHeader> ListenableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
398 final IN input, final Class<OUT> responseClazz, final String failureInfo) {
399 final RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName());
400 final ResponseExpectedRpcListener<OUT> listener =
401 new ResponseExpectedRpcListener<>(input, failureInfo, responseCache, key);
402 statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA);
403 return enqueueMessage(listener);
407 * @param resultFuture
409 * @param errorSeverity
413 private static SettableFuture<Boolean> handleTransportChannelFuture(
414 final ChannelFuture resultFuture) {
416 final SettableFuture<Boolean> transportResult = SettableFuture.create();
418 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
421 public void operationComplete(
422 final io.netty.util.concurrent.Future<? super Void> future)
424 transportResult.set(future.isSuccess());
425 if (!future.isSuccess()) {
426 transportResult.setException(future.cause());
430 return transportResult;
437 private static RpcResponseKey createRpcResponseKey(final OfHeader message) {
438 return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName());
444 private ResponseExpectedRpcListener<?> findRpcResponse(final RpcResponseKey key) {
445 return responseCache.getIfPresent(key);
449 public void setSystemListener(final SystemNotificationsListener systemListener) {
450 this.systemListener = systemListener;
454 public void checkListeners() {
455 final StringBuilder buffer = new StringBuilder();
456 if (systemListener == null) {
457 buffer.append("SystemListener ");
459 if (messageListener == null) {
460 buffer.append("MessageListener ");
462 if (connectionReadyListener == null) {
463 buffer.append("ConnectionReadyListener ");
466 Preconditions.checkState(buffer.length() == 0, "Missing listeners: %s", buffer.toString());
470 public void fireConnectionReadyNotification() {
471 versionDetector = (OFVersionDetector) channel.pipeline().get(PipelineHandlers.OF_VERSION_DETECTOR.name());
472 Preconditions.checkState(versionDetector != null);
474 new Thread(new Runnable() {
477 connectionReadyListener.onConnectionReady();
483 public void setConnectionReadyListener(
484 final ConnectionReadyListener connectionReadyListener) {
485 this.connectionReadyListener = connectionReadyListener;
489 public InetSocketAddress getRemoteAddress() {
490 return (InetSocketAddress) channel.remoteAddress();
494 * Used only for testing purposes
497 public void setResponseCache(final Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache) {
498 this.responseCache = cache;
502 public boolean isAutoRead() {
503 return channel.config().isAutoRead();
507 public void setAutoRead(final boolean autoRead) {
508 channel.config().setAutoRead(autoRead);
512 public <T extends OutboundQueueHandler> OutboundQueueHandlerRegistration<T> registerOutboundQueueHandler(
513 final T handler, final int maxQueueDepth, final long maxBarrierNanos) {
514 Preconditions.checkState(outputManager == null, "Manager %s already registered", outputManager);
520 final OutboundQueueManager<T> ret = new OutboundQueueManager<>(this, address, handler, maxQueueDepth, maxBarrierNanos);
522 channel.pipeline().addLast(outputManager);
524 return new OutboundQueueHandlerRegistrationImpl<T>(handler) {
526 protected void removeRegistration() {
527 outputManager.close();
528 channel.pipeline().remove(outputManager);
529 outputManager = null;
534 Channel getChannel() {
539 public void setPacketInFiltering(final boolean enabled) {
540 versionDetector.setFilterPacketIns(enabled);
541 LOG.debug("PacketIn filtering {}abled", enabled ? "en" : "dis");