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