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