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