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