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