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