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