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