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