Fix Bug 271
[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,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
112         addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
113         addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
114         addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
115         addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
116         addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
117
118         //TODO: move registration to factory
119         NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
120         addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
121         addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
122         addMessagePopListener(PacketReceived.class,notificationPopListener);
123         addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
124         addMessagePopListener(NodeUpdated.class, notificationPopListener);
125
126         addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
127         addMessagePopListener(TableUpdated.class, notificationPopListener);
128         
129         //Notification registration for flow statistics
130         addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
131         addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
132         
133         //Notification registrations for group-statistics
134         addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
135         addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
136         addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
137
138         //Notification registrations for meter-statistics
139         addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
140         addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
141         addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
142
143         //Notification registration for port-statistics
144         addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
145         
146         //Notification registration for flow-table statistics
147         addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
148         
149         //Notification registration for queue-statistics
150         addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
151
152         //Notification for LLDPSpeaker
153         LLDPSpeakerPopListener<DataObject> lldpPopListener  = new LLDPSpeakerPopListener<DataObject>();
154         addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
155         
156         // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
157         OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
158         OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
159     }
160
161     /**
162      * @param switchConnectionProvider
163      *            the switchConnectionProvider to set
164      */
165     public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
166         this.switchConnectionProvider = switchConnectionProvider;
167     }
168
169     /**
170      * @param switchConnectionProviderToUnset
171      *            the switchConnectionProvider to unset
172      */
173     public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
174         if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
175             this.switchConnectionProvider = null;
176         }
177     }
178
179     /**
180      * Function called by dependency manager after "init ()" is called and after
181      * the services provided by the class are registered in the service registry
182      *
183      */
184     public void start() {
185         LOG.debug("starting ..");
186         LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
187         // setup handler
188         SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
189         switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
190         // configure and startup library servers
191         switchConnectionProvider.configure(getConnectionConfiguration());
192         Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
193     }
194
195     /**
196      * @return wished connections configurations
197      */
198     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
199         // TODO:: get config from state manager
200         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
201         ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
202         return Lists.newArrayList(configuration, configurationLegacy);
203     }
204
205     /**
206      * Function called by the dependency manager before the services exported by
207      * the component are unregistered, this will be followed by a "destroy ()"
208      * calls
209      *
210      */
211     public void stop() {
212         LOG.debug("stopping");
213         Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
214         try {
215             srvStopped.get(5000, TimeUnit.MILLISECONDS);
216         } catch (InterruptedException | ExecutionException | TimeoutException e) {
217             LOG.error(e.getMessage(), e);
218         }
219     }
220
221     /**
222      * Function called by the dependency manager when at least one dependency
223      * become unsatisfied or when the component is shutting down because for
224      * example bundle is being stopped.
225      *
226      */
227     public void destroy() {
228         // do nothing
229     }
230
231     @Override
232     public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
233         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
234
235         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
236         if (existingValues == null) {
237             existingValues = new LinkedHashSet<>();
238             messageTranslators.put(tKey, existingValues);
239         }
240         existingValues.add(translator);
241         LOG.debug("{} is now translated by {}", messageType, translator);
242     }
243
244     @Override
245     public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
246         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
247         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
248         if (values != null) {
249             values.remove(translator);
250             if (values.isEmpty()) {
251                 messageTranslators.remove(tKey);
252             }
253             LOG.debug("{} is now removed from translators", translator);
254          }
255     }
256
257     @Override
258     public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
259         Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
260         if (existingValues == null) {
261             existingValues = new LinkedHashSet<>();
262             popListeners.put(messageType, existingValues);
263         }
264         existingValues.add(popListener);
265         LOG.debug("{} is now popListened by {}", messageType, popListener);
266     }
267
268     @Override
269     public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
270         Collection<PopListener<DataObject>> values = popListeners.get(messageType);
271         if (values != null) {
272             values.remove(popListener);
273             if (values.isEmpty()) {
274                 popListeners.remove(messageType);
275             }
276             LOG.debug("{} is now removed from popListeners", popListener);
277          }
278     }
279
280
281 }