acd3814e7f192c9b1cb479794ae8fea1ff8bd6b8
[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 ErrorHandlerQueueImpl 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 ErrorHandlerQueueImpl();
230         new Thread(errorHandler).start();
231         
232         switchConnectionHandler.setErrorHandler(errorHandler);
233         switchConnectionHandler.init();
234         
235         List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
236         for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
237             switchConnectionPrv.setSwitchConnectionHandler(switchConnectionHandler);
238             ListenableFuture<Boolean> isOnlineFuture = switchConnectionPrv.startup();
239             starterChain.add(isOnlineFuture);
240         }
241         
242         Future<List<Boolean>> srvStarted = Futures.allAsList(starterChain);
243     }
244
245     /**
246      * @return wished connections configurations
247      */
248     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
249         // TODO:: get config from state manager
250         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
251         ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
252         return Lists.newArrayList(configuration, configurationLegacy);
253     }
254
255     /**
256      * Function called by the dependency manager before the services exported by
257      * the component are unregistered, this will be followed by a "destroy ()"
258      * calls
259      *
260      */
261     public void stop() {
262         LOG.debug("stopping");
263         List<ListenableFuture<Boolean>> stopChain = new ArrayList<>();
264         try {
265             for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
266                 ListenableFuture<Boolean> shutdown =  switchConnectionPrv.shutdown();
267                 stopChain.add(shutdown);
268             }
269             Futures.allAsList(stopChain).get(5000, TimeUnit.MILLISECONDS);
270         } catch (InterruptedException | ExecutionException | TimeoutException e) {
271             LOG.error(e.getMessage(), e);
272         }
273         close();
274     }
275
276     /**
277      * Function called by the dependency manager when at least one dependency
278      * become unsatisfied or when the component is shutting down because for
279      * example bundle is being stopped.
280      *
281      */
282     public void destroy() {
283         close();
284     }
285
286     @Override
287     public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
288         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
289
290         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
291         if (existingValues == null) {
292             existingValues = new LinkedHashSet<>();
293             messageTranslators.put(tKey, existingValues);
294         }
295         existingValues.add(translator);
296         LOG.debug("{} is now translated by {}", messageType, translator);
297     }
298
299     @Override
300     public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
301         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
302         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
303         if (values != null) {
304             values.remove(translator);
305             if (values.isEmpty()) {
306                 messageTranslators.remove(tKey);
307             }
308             LOG.debug("{} is now removed from translators", translator);
309          }
310     }
311
312     @Override
313     public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
314         Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
315         if (existingValues == null) {
316             existingValues = new LinkedHashSet<>();
317             popListeners.put(messageType, existingValues);
318         }
319         existingValues.add(popListener);
320         LOG.debug("{} is now popListened by {}", messageType, popListener);
321     }
322
323     @Override
324     public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
325         Collection<PopListener<DataObject>> values = popListeners.get(messageType);
326         if (values != null) {
327             values.remove(popListener);
328             if (values.isEmpty()) {
329                 popListeners.remove(messageType);
330             }
331             LOG.debug("{} is now removed from popListeners", popListener);
332          }
333     }
334
335     /**
336      * @param messageSpyCounter the messageSpyCounter to set
337      */
338     public void setMessageSpyCounter(
339             MessageSpy<OfHeader, DataObject> messageSpyCounter) {
340         this.messageSpyCounter = messageSpyCounter;
341     }
342     
343     @Override
344     public void close() {
345         LOG.debug("close");
346         messageSpyCounter = null;
347         messageTranslators = null;
348         popListeners = null;
349         for (SwitchConnectionProvider switchConnectionPrv : switchConnectionProviders) {
350             switchConnectionPrv.setSwitchConnectionHandler(null);
351         }
352         switchConnectionProviders = null;
353         OFSessionUtil.releaseSessionManager();
354         errorHandler.close();
355     }
356 }