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