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