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