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