c2854bec95d35e7bcb5ca718124375ae2b1d08e8
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ConnectionAdapterImpl.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9
10 package org.opendaylight.openflowjava.protocol.impl.core.connection;
11
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.util.concurrent.GenericFutureListener;
15
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.RejectedExecutionException;
19 import java.util.concurrent.TimeUnit;
20
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;
68
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;
77
78 /**
79  * Handles messages (notifications + rpcs) and connections
80  * @author mirehak
81  * @author michal.polkorab
82  */
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;
86
87     /**
88      * Default depth of write queue, e.g. we allow these many messages
89      * to be queued up before blocking producers.
90      */
91     public static final int DEFAULT_QUEUE_DEPTH = 1024;
92
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");
97
98     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
99             new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
100         @Override
101         public void onRemoval(
102                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
103             if (! notification.getCause().equals(RemovalCause.EXPLICIT)) {
104                 notification.getValue().discard();
105             }
106         }
107     };
108
109     /** expiring cache for future rpcResponses */
110     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> responseCache;
111
112     private final ChannelOutboundQueue output;
113     private final Channel channel;
114
115     private ConnectionReadyListener connectionReadyListener;
116     private OpenflowProtocolListener messageListener;
117     private SystemNotificationsListener systemListener;
118     private boolean disconnectOccured = false;
119     private StatisticsCounters statisticsCounters;
120
121     /**
122      * default ctor
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))
126      */
127     public ConnectionAdapterImpl(final Channel channel, final InetSocketAddress address) {
128         responseCache = CacheBuilder.newBuilder()
129                 .concurrencyLevel(1)
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");
137     }
138
139     @Override
140     public Future<RpcResult<BarrierOutput>> barrier(final BarrierInput input) {
141         return sendToSwitchExpectRpcResultFuture(
142                 input, BarrierOutput.class, "barrier-input sending failed");
143     }
144
145     @Override
146     public Future<RpcResult<EchoOutput>> echo(final EchoInput input) {
147         return sendToSwitchExpectRpcResultFuture(
148                 input, EchoOutput.class, "echo-input sending failed");
149     }
150
151     @Override
152     public Future<RpcResult<Void>> echoReply(final EchoReplyInput input) {
153         return sendToSwitchFuture(input, "echo-reply sending failed");
154     }
155
156     @Override
157     public Future<RpcResult<Void>> experimenter(final ExperimenterInput input) {
158         return sendToSwitchFuture(input, "experimenter sending failed");
159     }
160
161     @Override
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");
165     }
166
167     @Override
168     public Future<RpcResult<GetConfigOutput>> getConfig(final GetConfigInput input) {
169         return sendToSwitchExpectRpcResultFuture(
170                 input, GetConfigOutput.class, "get-config-input sending failed");
171     }
172
173     @Override
174     public Future<RpcResult<GetFeaturesOutput>> getFeatures(
175             final GetFeaturesInput input) {
176         return sendToSwitchExpectRpcResultFuture(
177                 input, GetFeaturesOutput.class, "get-features-input sending failed");
178     }
179
180     @Override
181     public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
182             final GetQueueConfigInput input) {
183         return sendToSwitchExpectRpcResultFuture(
184                 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
185     }
186
187     @Override
188     public Future<RpcResult<Void>> groupMod(final GroupModInput input) {
189         return sendToSwitchFuture(input, "group-mod-input sending failed");
190     }
191
192     @Override
193     public Future<RpcResult<Void>> hello(final HelloInput input) {
194         return sendToSwitchFuture(input, "hello-input sending failed");
195     }
196
197     @Override
198     public Future<RpcResult<Void>> meterMod(final MeterModInput input) {
199         return sendToSwitchFuture(input, "meter-mod-input sending failed");
200     }
201
202     @Override
203     public Future<RpcResult<Void>> packetOut(final PacketOutInput input) {
204         return sendToSwitchFuture(input, "packet-out-input sending failed");
205     }
206
207     @Override
208     public Future<RpcResult<Void>> multipartRequest(final MultipartRequestInput input) {
209         return sendToSwitchFuture(input, "multi-part-request sending failed");
210     }
211
212     @Override
213     public Future<RpcResult<Void>> portMod(final PortModInput input) {
214         return sendToSwitchFuture(input, "port-mod-input sending failed");
215     }
216
217     @Override
218     public Future<RpcResult<RoleRequestOutput>> roleRequest(
219             final RoleRequestInput input) {
220         return sendToSwitchExpectRpcResultFuture(
221                 input, RoleRequestOutput.class, "role-request-config-input sending failed");
222     }
223
224     @Override
225     public Future<RpcResult<Void>> setConfig(final SetConfigInput input) {
226         return sendToSwitchFuture(input, "set-config-input sending failed");
227     }
228
229     @Override
230     public Future<RpcResult<Void>> tableMod(final TableModInput input) {
231         return sendToSwitchFuture(input, "table-mod-input sending failed");
232     }
233
234     @Override
235     public Future<RpcResult<GetAsyncOutput>> getAsync(final GetAsyncInput input) {
236         return sendToSwitchExpectRpcResultFuture(
237                 input, GetAsyncOutput.class, "get-async-input sending failed");
238     }
239
240     @Override
241     public Future<RpcResult<Void>> setAsync(final SetAsyncInput input) {
242         return sendToSwitchFuture(input, "set-async-input sending failed");
243     }
244
245     @Override
246     public Future<Boolean> disconnect() {
247         ChannelFuture disconnectResult = channel.disconnect();
248         responseCache.invalidateAll();
249         disconnectOccured = true;
250
251         return handleTransportChannelFuture(disconnectResult);
252     }
253
254     @Override
255     public boolean isAlive() {
256         return channel.isOpen();
257     }
258
259     @Override
260     public void setMessageListener(final OpenflowProtocolListener messageListener) {
261         this.messageListener = messageListener;
262     }
263
264     @Override
265     public void consume(final DataObject message) {
266         LOG.debug("ConsumeIntern msg");
267         if (disconnectOccured ) {
268             return;
269         }
270         if (message instanceof Notification) {
271
272             // System events
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);
279                 // OpenFlow messages
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);
305             } else {
306                 LOG.warn("message listening not supported for type: {}", message.getClass());
307             }
308         } else {
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);
319                 } else {
320                     LOG.warn("received unexpected rpc response: {}", key);
321                 }
322             } else {
323                 LOG.warn("message listening not supported for type: {}", message.getClass());
324             }
325         }
326     }
327
328     private <T> ListenableFuture<RpcResult<T>> enqueueMessage(final AbstractRpcListener<T> promise) {
329         LOG.debug("Submitting promise {}", promise);
330
331         if (!output.enqueue(promise)) {
332             LOG.debug("Message queue is full, rejecting execution");
333             promise.failedRpc(QUEUE_FULL_EXCEPTION);
334         } else {
335             LOG.debug("Promise enqueued successfully");
336         }
337
338         return promise.getResult();
339     }
340
341     /**
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>
349      *  </ul>
350      */
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));
355     }
356
357     /**
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>
369      *  </li>
370      *  </ul>
371      */
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);
379     }
380
381     /**
382      * @param resultFuture
383      * @param failureInfo
384      * @param errorSeverity
385      * @param message
386      * @return
387      */
388     private static SettableFuture<Boolean> handleTransportChannelFuture(
389             final ChannelFuture resultFuture) {
390
391         final SettableFuture<Boolean> transportResult = SettableFuture.create();
392
393         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
394
395             @Override
396             public void operationComplete(
397                     final io.netty.util.concurrent.Future<? super Void> future)
398                     throws Exception {
399                 transportResult.set(future.isSuccess());
400                 if (!future.isSuccess()) {
401                     transportResult.setException(future.cause());
402                 }
403             }
404         });
405         return transportResult;
406     }
407
408     /**
409      * @param message
410      * @return
411      */
412     private static RpcResponseKey createRpcResponseKey(final OfHeader message) {
413         return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName());
414     }
415
416     /**
417      * @return
418      */
419     private ResponseExpectedRpcListener<?> findRpcResponse(final RpcResponseKey key) {
420         return responseCache.getIfPresent(key);
421     }
422
423     @Override
424     public void setSystemListener(final SystemNotificationsListener systemListener) {
425         this.systemListener = systemListener;
426     }
427
428     @Override
429     public void checkListeners() {
430         final StringBuilder buffer =  new StringBuilder();
431         if (systemListener == null) {
432             buffer.append("SystemListener ");
433         }
434         if (messageListener == null) {
435             buffer.append("MessageListener ");
436         }
437         if (connectionReadyListener == null) {
438             buffer.append("ConnectionReadyListener ");
439         }
440
441         Preconditions.checkState(buffer.length() == 0, "Missing listeners: %s", buffer.toString());
442     }
443
444     @Override
445     public void fireConnectionReadyNotification() {
446         new Thread(new Runnable() {
447             @Override
448             public void run() {
449                 connectionReadyListener.onConnectionReady();
450             }
451         }).start();
452     }
453
454     @Override
455     public void setConnectionReadyListener(
456             final ConnectionReadyListener connectionReadyListener) {
457         this.connectionReadyListener = connectionReadyListener;
458     }
459
460     @Override
461     public InetSocketAddress getRemoteAddress() {
462         return (InetSocketAddress) channel.remoteAddress();
463     }
464
465     /**
466      * Used only for testing purposes
467      * @param cache
468      */
469     public void setResponseCache(Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache) {
470         this.responseCache = cache;
471     }
472 }