2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core;
11 import java.util.Collection;
12 import java.util.LinkedHashSet;
13 import java.util.List;
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;
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;
71 import com.google.common.collect.Lists;
77 public class MDController implements IMDController, AutoCloseable {
79 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
81 private SwitchConnectionProvider switchConnectionProvider;
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;
87 final private int OF10 = OFConstants.OFP_VERSION_1_0;
88 final private int OF13 = OFConstants.OFP_VERSION_1_3;
92 * @return translator mapping
94 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
95 return messageTranslators;
99 * provisioning of translator mapping
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());
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);
131 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
132 addMessagePopListener(TableUpdated.class, notificationPopListener);
134 //Notification registration for flow statistics
135 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
136 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
138 //Notification registrations for group-statistics
139 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
140 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
141 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
143 //Notification registrations for meter-statistics
144 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
145 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
146 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
148 //Notification registration for port-statistics
149 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
151 //Notification registration for flow-table statistics
152 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
154 //Notification registration for queue-statistics
155 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
157 //Notification for LLDPSpeaker
158 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
159 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
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);
167 * @param switchConnectionProvider
168 * the switchConnectionProvider to set
170 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
171 this.switchConnectionProvider = switchConnectionProvider;
175 * @param switchConnectionProviderToUnset
176 * the switchConnectionProvider to unset
178 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
179 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
180 this.switchConnectionProvider = null;
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
189 public void start() {
190 LOG.debug("starting ..");
191 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
193 SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl();
194 switchConnectionHandler.setMessageSpy(messageSpyCounter);
195 switchConnectionHandler.init();
197 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
199 // configure and startup library servers
200 switchConnectionProvider.configure(getConnectionConfiguration());
201 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
205 * @return wished connections configurations
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);
215 * Function called by the dependency manager before the services exported by
216 * the component are unregistered, this will be followed by a "destroy ()"
221 LOG.debug("stopping");
222 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
224 srvStopped.get(5000, TimeUnit.MILLISECONDS);
225 } catch (InterruptedException | ExecutionException | TimeoutException e) {
226 LOG.error(e.getMessage(), e);
232 * Function called by the dependency manager when at least one dependency
233 * become unsatisfied or when the component is shutting down because for
234 * example bundle is being stopped.
237 public void destroy() {
242 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
243 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
245 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
246 if (existingValues == null) {
247 existingValues = new LinkedHashSet<>();
248 messageTranslators.put(tKey, existingValues);
250 existingValues.add(translator);
251 LOG.debug("{} is now translated by {}", messageType, translator);
255 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
256 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
257 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
258 if (values != null) {
259 values.remove(translator);
260 if (values.isEmpty()) {
261 messageTranslators.remove(tKey);
263 LOG.debug("{} is now removed from translators", translator);
268 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
269 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
270 if (existingValues == null) {
271 existingValues = new LinkedHashSet<>();
272 popListeners.put(messageType, existingValues);
274 existingValues.add(popListener);
275 LOG.debug("{} is now popListened by {}", messageType, popListener);
279 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
280 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
281 if (values != null) {
282 values.remove(popListener);
283 if (values.isEmpty()) {
284 popListeners.remove(messageType);
286 LOG.debug("{} is now removed from popListeners", popListener);
291 * @param messageSpyCounter the messageSpyCounter to set
293 public void setMessageSpyCounter(
294 MessageSpy<OfHeader, DataObject> messageSpyCounter) {
295 this.messageSpyCounter = messageSpyCounter;
299 public void close() {
301 messageSpyCounter = null;
302 messageTranslators = null;
304 switchConnectionProvider.setSwitchConnectionHandler(null);
305 switchConnectionProvider = null;
306 OFSessionUtil.releaseSessionManager();