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