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