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.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.lldp.LLDPSpeakerPopListener;
36 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
62 import org.opendaylight.yangtools.yang.binding.DataObject;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 import com.google.common.collect.Lists;
72 public class MDController implements IMDController {
74 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
76 private SwitchConnectionProvider switchConnectionProvider;
78 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
79 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
81 final private int OF10 = 1;
82 final private int OF13 = 4;
86 * @return translator mapping
88 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
89 return messageTranslators;
93 * provisioning of translator mapping
96 LOG.debug("Initializing!");
97 messageTranslators = new ConcurrentHashMap<>();
98 popListeners = new ConcurrentHashMap<>();
99 //TODO: move registration to factory
100 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
101 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
102 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
103 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
104 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
105 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
106 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
107 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
108 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
109 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
110 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
111 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
112 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
114 //TODO: move registration to factory
115 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
116 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
117 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
118 addMessagePopListener(PacketReceived.class,notificationPopListener);
119 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
120 addMessagePopListener(NodeUpdated.class, notificationPopListener);
122 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
123 addMessagePopListener(TableUpdated.class, notificationPopListener);
125 //Notification registration for flow statistics
126 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
127 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
129 //Notification registrations for group-statistics
130 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
131 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
132 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
134 //Notification registrations for meter-statistics
135 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
136 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
137 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
139 //Notification registration for port-statistics
140 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
142 //Notification registration for flow-table statistics
143 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
145 //Notification registration for queue-statistics
146 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
148 //Notification for LLDPSpeaker
149 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
150 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
152 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
153 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
154 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
158 * @param switchConnectionProvider
159 * the switchConnectionProvider to set
161 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
162 this.switchConnectionProvider = switchConnectionProvider;
166 * @param switchConnectionProviderToUnset
167 * the switchConnectionProvider to unset
169 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
170 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
171 this.switchConnectionProvider = null;
176 * Function called by dependency manager after "init ()" is called and after
177 * the services provided by the class are registered in the service registry
180 public void start() {
181 LOG.debug("starting ..");
182 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
184 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
185 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
186 // configure and startup library servers
187 switchConnectionProvider.configure(getConnectionConfiguration());
188 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
192 * @return wished connections configurations
194 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
195 // TODO:: get config from state manager
196 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
197 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
198 return Lists.newArrayList(configuration, configurationLegacy);
202 * Function called by the dependency manager before the services exported by
203 * the component are unregistered, this will be followed by a "destroy ()"
208 LOG.debug("stopping");
209 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
211 srvStopped.get(5000, TimeUnit.MILLISECONDS);
212 } catch (InterruptedException | ExecutionException | TimeoutException e) {
213 LOG.error(e.getMessage(), e);
218 * Function called by the dependency manager when at least one dependency
219 * become unsatisfied or when the component is shutting down because for
220 * example bundle is being stopped.
223 public void destroy() {
228 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
229 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
231 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
232 if (existingValues == null) {
233 existingValues = new LinkedHashSet<>();
234 messageTranslators.put(tKey, existingValues);
236 existingValues.add(translator);
237 LOG.debug("{} is now translated by {}", messageType, translator);
241 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
242 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
243 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
244 if (values != null) {
245 values.remove(translator);
246 if (values.isEmpty()) {
247 messageTranslators.remove(tKey);
249 LOG.debug("{} is now removed from translators", translator);
254 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
255 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
256 if (existingValues == null) {
257 existingValues = new LinkedHashSet<>();
258 popListeners.put(messageType, existingValues);
260 existingValues.add(popListener);
261 LOG.debug("{} is now popListened by {}", messageType, popListener);
265 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
266 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
267 if (values != null) {
268 values.remove(popListener);
269 if (values.isEmpty()) {
270 popListeners.remove(messageType);
272 LOG.debug("{} is now removed from popListeners", popListener);