BUG-1075: ingress back pressure
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / MDController.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.openflow.md.core;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.BlockingQueue;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.LinkedBlockingQueue;
22 import java.util.concurrent.RejectedExecutionException;
23 import java.util.concurrent.RejectedExecutionHandler;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
29 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
30 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
31 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
32 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
33 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorV10Translator;
34 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
35 import org.opendaylight.openflowplugin.openflow.md.core.translator.FeaturesV10ToNodeConnectorUpdatedTranslator;
36 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
37 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
38 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
39 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTableFeaturesToTableUpdatedTranslator;
40 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultipartReplyTranslator;
41 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
42 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInV10Translator;
43 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
44 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeakerPopListener;
45 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy;
46 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadActionErrorNotification;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadInstructionErrorNotification;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadMatchErrorNotification;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.BadRequestErrorNotification;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.ExperimenterErrorNotification;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.FlowModErrorNotification;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.GroupModErrorNotification;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.HelloFailedErrorNotification;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.MeterModErrorNotification;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.PortModErrorNotification;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.QueueOpErrorNotification;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.RoleRequestErrorNotification;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.SwitchConfigErrorNotification;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableFeaturesErrorNotification;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.error.service.rev140410.TableModErrorNotification;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
88 import org.opendaylight.yangtools.yang.binding.DataContainer;
89 import org.opendaylight.yangtools.yang.binding.DataObject;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92
93 import com.google.common.collect.Lists;
94 import com.google.common.util.concurrent.ForwardingBlockingQueue;
95 import com.google.common.util.concurrent.Futures;
96 import com.google.common.util.concurrent.ListenableFuture;
97 import com.google.common.util.concurrent.ListeningExecutorService;
98 import com.google.common.util.concurrent.MoreExecutors;
99
100 /**
101  *
102  */
103 public class MDController implements IMDController, AutoCloseable {
104
105     private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
106
107     private Collection<SwitchConnectionProvider> switchConnectionProviders;
108
109     private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
110     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
111     private MessageSpy<DataContainer> messageSpyCounter;
112
113     final private int OF10 = OFConstants.OFP_VERSION_1_0;
114     final private int OF13 = OFConstants.OFP_VERSION_1_3;
115
116     private ErrorHandlerSimpleImpl errorHandler;
117
118     /**
119      * @return translator mapping
120      */
121     public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
122         return messageTranslators;
123     }
124
125     /**
126      * provisioning of translator mapping
127      */
128     public void init() {
129         LOG.debug("init");
130         messageTranslators = new ConcurrentHashMap<>();
131         popListeners = new ConcurrentHashMap<>();
132         //TODO: move registration to factory
133         addMessageTranslator(ErrorMessage.class, OF10, new ErrorV10Translator());
134         addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
135         addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
136         addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
137         addMessageTranslator(PacketInMessage.class,OF10, new PacketInV10Translator());
138         addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
139         addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
140         addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
141         addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
142         addMessageTranslator(MultipartReplyMessage.class,OF10, new MultiPartMessageDescToNodeUpdatedTranslator());
143         addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
144         addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
145         addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
146         addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
147         addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
148         addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
149
150         NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
151         notificationPopListener.setNotificationProviderService(
152                 OFSessionUtil.getSessionManager().getNotificationProviderService());
153         notificationPopListener.setMessageSpy(messageSpyCounter);
154
155         //TODO: move registration to factory
156         addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
157         addMessagePopListener(BadActionErrorNotification.class, notificationPopListener);
158         addMessagePopListener(BadInstructionErrorNotification.class, notificationPopListener);
159         addMessagePopListener(BadMatchErrorNotification.class, notificationPopListener);
160         addMessagePopListener(BadRequestErrorNotification.class, notificationPopListener);
161         addMessagePopListener(ExperimenterErrorNotification.class, notificationPopListener);
162         addMessagePopListener(FlowModErrorNotification.class, notificationPopListener);
163         addMessagePopListener(GroupModErrorNotification.class, notificationPopListener);
164         addMessagePopListener(HelloFailedErrorNotification.class, notificationPopListener);
165         addMessagePopListener(MeterModErrorNotification.class, notificationPopListener);
166         addMessagePopListener(PortModErrorNotification.class, notificationPopListener);
167         addMessagePopListener(QueueOpErrorNotification.class, notificationPopListener);
168         addMessagePopListener(RoleRequestErrorNotification.class, notificationPopListener);
169         addMessagePopListener(SwitchConfigErrorNotification.class, notificationPopListener);
170         addMessagePopListener(TableFeaturesErrorNotification.class, notificationPopListener);
171         addMessagePopListener(TableModErrorNotification.class, notificationPopListener);
172         addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
173         addMessagePopListener(PacketReceived.class,notificationPopListener);
174         addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
175         addMessagePopListener(NodeUpdated.class, notificationPopListener);
176
177         addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
178         addMessagePopListener(TableUpdated.class, notificationPopListener);
179
180         //Notification registration for flow statistics
181         addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
182         addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
183
184         //Notification registrations for group-statistics
185         addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
186         addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
187         addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
188
189         //Notification registrations for meter-statistics
190         addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
191         addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
192         addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
193
194         //Notification registration for port-statistics
195         addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
196
197         //Notification registration for flow-table statistics
198         addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
199
200         //Notification registration for queue-statistics
201         addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
202
203         //Notification for LLDPSpeaker
204         LLDPSpeakerPopListener<DataObject> lldpPopListener  = new LLDPSpeakerPopListener<DataObject>();
205         addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
206
207         // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
208         OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
209         OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
210         OFSessionUtil.getSessionManager().setMessageSpy(messageSpyCounter);
211
212         // prepare worker pool for rpc
213         // TODO: get size from configSubsystem
214         int rpcThreadLimit = 10;
215         ListeningExecutorService rpcPoolDelegator = createRpcPoolSpyDecorated(rpcThreadLimit, messageSpyCounter);
216         OFSessionUtil.getSessionManager().setRpcPool(rpcPoolDelegator);
217
218     }
219
220     /**
221      * @param rpcThreadLimit
222      * @param messageSpy
223      * @return
224      */
225     private static ListeningExecutorService createRpcPoolSpyDecorated(final int rpcThreadLimit, final MessageSpy<DataContainer> messageSpy) {
226         final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(100000);
227         final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
228             @Override
229             protected BlockingQueue<Runnable> delegate() {
230                 return delegate;
231             }
232
233             @Override
234             public boolean offer(final Runnable r) {
235                 // ThreadPoolExecutor will spawn a new thread after core size is reached only
236                 // if the queue.offer returns false.
237                 return false;
238             }
239         };
240
241         ThreadPoolLoggingExecutor rpcPool = new ThreadPoolLoggingExecutor(rpcThreadLimit, rpcThreadLimit, 0L,
242                 TimeUnit.MILLISECONDS, queue, "OFRpc");
243         rpcPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
244                         @Override
245                         public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
246                                 try {
247                                         executor.getQueue().put(r);
248                                 } catch (InterruptedException e) {
249                                         throw new RejectedExecutionException("Interrupted while waiting on queue", e);
250                                 }
251
252                         }
253                 });
254         ListeningExecutorService listeningRpcPool = MoreExecutors.listeningDecorator(rpcPool);
255         RpcListeningExecutorService rpcPoolDecorated = new RpcListeningExecutorService(listeningRpcPool);
256         rpcPoolDecorated.setMessageSpy(messageSpy);
257         return rpcPoolDecorated;
258     }
259
260     /**
261      * @param switchConnectionProviders
262      *            the switchConnectionProviders to set
263      */
264     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
265         this.switchConnectionProviders = switchConnectionProviders;
266     }
267
268     /**
269      * Function called by dependency manager after "init ()" is called and after
270      * the services provided by the class are registered in the service registry
271      *
272      */
273     public void start() {
274         LOG.debug("starting ..");
275         LOG.debug("switchConnectionProvider: " + switchConnectionProviders);
276         // setup handler
277         SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
278         switchConnectionHandler.setMessageSpy(messageSpyCounter);
279
280         errorHandler = new ErrorHandlerSimpleImpl();
281
282         switchConnectionHandler.setErrorHandler(errorHandler);
283         switchConnectionHandler.init();
284
285         List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
286         for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
287             switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
288             ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
289             starterChain.add(isOnlineFuture);
290         }
291
292         Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
293     }
294
295     /**
296      * @return wished connections configurations
297      * @deprecated use configSubsystem
298      */
299     @Deprecated
300     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
301         // TODO:: get config from state manager
302         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
303         ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
304         return Lists.newArrayList(configuration, configurationLegacy);
305     }
306
307     /**
308      * Function called by the dependency manager before the services exported by
309      * the component are unregistered, this will be followed by a "destroy ()"
310      * calls
311      *
312      */
313     public void stop() {
314         LOG.debug("stopping");
315         List<ListenableFuture<Boolean>> stopChain = new ArrayList<>(switchConnectionProviders.size());
316         try {
317             for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
318                 ListenableFuture<Boolean> shutdown =  switchConnectionPrv.shutdown();
319                 stopChain.add(shutdown);
320             }
321             Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
322         } catch (InterruptedException | ExecutionException | TimeoutException e) {
323             LOG.error(e.getMessage(), e);
324         }
325         close();
326     }
327
328     /**
329      * Function called by the dependency manager when at least one dependency
330      * become unsatisfied or when the component is shutting down because for
331      * example bundle is being stopped.
332      *
333      */
334     public void destroy() {
335         close();
336     }
337
338     @Override
339     public void addMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
340         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
341
342         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
343         if (existingValues == null) {
344             existingValues = new LinkedHashSet<>();
345             messageTranslators.put(tKey, existingValues);
346         }
347         existingValues.add(translator);
348         LOG.debug("{} is now translated by {}", messageType, translator);
349     }
350
351     @Override
352     public void removeMessageTranslator(final Class<? extends DataObject> messageType, final int version, final IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
353         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
354         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
355         if (values != null) {
356             values.remove(translator);
357             if (values.isEmpty()) {
358                 messageTranslators.remove(tKey);
359             }
360             LOG.debug("{} is now removed from translators", translator);
361          }
362     }
363
364     @Override
365     public void addMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
366         Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
367         if (existingValues == null) {
368             existingValues = new LinkedHashSet<>();
369             popListeners.put(messageType, existingValues);
370         }
371         existingValues.add(popListener);
372         LOG.debug("{} is now popListened by {}", messageType, popListener);
373     }
374
375     @Override
376     public void removeMessagePopListener(final Class<? extends DataObject> messageType, final PopListener<DataObject> popListener) {
377         Collection<PopListener<DataObject>> values = popListeners.get(messageType);
378         if (values != null) {
379             values.remove(popListener);
380             if (values.isEmpty()) {
381                 popListeners.remove(messageType);
382             }
383             LOG.debug("{} is now removed from popListeners", popListener);
384          }
385     }
386
387     /**
388      * @param messageSpyCounter the messageSpyCounter to set
389      */
390     public void setMessageSpyCounter(
391             final MessageSpy<DataContainer> messageSpyCounter) {
392         this.messageSpyCounter = messageSpyCounter;
393     }
394
395     @Override
396     public void close() {
397         LOG.debug("close");
398         messageSpyCounter = null;
399         messageTranslators = null;
400         popListeners = null;
401         for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
402             switchConnectionPrv.setSwitchConnectionHandler(null);
403         }
404         switchConnectionProviders = null;
405         OFSessionUtil.releaseSessionManager();
406         errorHandler = null;
407     }
408 }