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.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.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.PortStatusMessageToNodeConnectorUpdatedTranslator;
36 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeakerPopListener;
37 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
64 import org.opendaylight.yangtools.yang.binding.DataObject;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.collect.Lists;
74 public class MDController implements IMDController {
76 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
78 private SwitchConnectionProvider switchConnectionProvider;
80 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
81 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
83 final private int OF10 = 1;
84 final private int OF13 = 4;
88 * @return translator mapping
90 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
91 return messageTranslators;
95 * provisioning of translator mapping
98 LOG.debug("Initializing!");
99 messageTranslators = new ConcurrentHashMap<>();
100 popListeners = new ConcurrentHashMap<>();
101 //TODO: move registration to factory
102 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
103 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
104 addMessageTranslator(FlowRemovedMessage.class, OF10, new FlowRemovedTranslator());
105 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
106 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
107 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
108 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
109 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
110 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
111 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
112 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
113 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
114 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
115 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
116 addMessageTranslator(GetFeaturesOutput.class,OF10, new FeaturesV10ToNodeConnectorUpdatedTranslator());
118 //TODO: move registration to factory
119 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
120 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
121 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
122 addMessagePopListener(PacketReceived.class,notificationPopListener);
123 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
124 addMessagePopListener(NodeUpdated.class, notificationPopListener);
126 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
127 addMessagePopListener(TableUpdated.class, notificationPopListener);
129 //Notification registration for flow statistics
130 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
131 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
133 //Notification registrations for group-statistics
134 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
135 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
136 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
138 //Notification registrations for meter-statistics
139 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
140 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
141 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
143 //Notification registration for port-statistics
144 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
146 //Notification registration for flow-table statistics
147 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
149 //Notification registration for queue-statistics
150 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
152 //Notification for LLDPSpeaker
153 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
154 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
156 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
157 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
158 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
162 * @param switchConnectionProvider
163 * the switchConnectionProvider to set
165 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
166 this.switchConnectionProvider = switchConnectionProvider;
170 * @param switchConnectionProviderToUnset
171 * the switchConnectionProvider to unset
173 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
174 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
175 this.switchConnectionProvider = null;
180 * Function called by dependency manager after "init ()" is called and after
181 * the services provided by the class are registered in the service registry
184 public void start() {
185 LOG.debug("starting ..");
186 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
188 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
189 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
190 // configure and startup library servers
191 switchConnectionProvider.configure(getConnectionConfiguration());
192 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
196 * @return wished connections configurations
198 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
199 // TODO:: get config from state manager
200 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
201 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
202 return Lists.newArrayList(configuration, configurationLegacy);
206 * Function called by the dependency manager before the services exported by
207 * the component are unregistered, this will be followed by a "destroy ()"
212 LOG.debug("stopping");
213 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
215 srvStopped.get(5000, TimeUnit.MILLISECONDS);
216 } catch (InterruptedException | ExecutionException | TimeoutException e) {
217 LOG.error(e.getMessage(), e);
222 * Function called by the dependency manager when at least one dependency
223 * become unsatisfied or when the component is shutting down because for
224 * example bundle is being stopped.
227 public void destroy() {
232 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
233 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
235 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
236 if (existingValues == null) {
237 existingValues = new LinkedHashSet<>();
238 messageTranslators.put(tKey, existingValues);
240 existingValues.add(translator);
241 LOG.debug("{} is now translated by {}", messageType, translator);
245 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
246 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
247 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
248 if (values != null) {
249 values.remove(translator);
250 if (values.isEmpty()) {
251 messageTranslators.remove(tKey);
253 LOG.debug("{} is now removed from translators", translator);
258 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
259 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
260 if (existingValues == null) {
261 existingValues = new LinkedHashSet<>();
262 popListeners.put(messageType, existingValues);
264 existingValues.add(popListener);
265 LOG.debug("{} is now popListened by {}", messageType, popListener);
269 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
270 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
271 if (values != null) {
272 values.remove(popListener);
273 if (values.isEmpty()) {
274 popListeners.remove(messageType);
276 LOG.debug("{} is now removed from popListeners", popListener);