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, OF10, new FlowRemovedTranslator());
103 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
104 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
105 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
106 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
107 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
108 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
109 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
110 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
111 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
112 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
113 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
115 //TODO: move registration to factory
116 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
117 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
118 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
119 addMessagePopListener(PacketReceived.class,notificationPopListener);
120 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
121 addMessagePopListener(NodeUpdated.class, notificationPopListener);
123 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
124 addMessagePopListener(TableUpdated.class, notificationPopListener);
126 //Notification registration for flow statistics
127 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
128 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
130 //Notification registrations for group-statistics
131 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
132 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
133 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
135 //Notification registrations for meter-statistics
136 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
137 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
138 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
140 //Notification registration for port-statistics
141 addMessagePopListener(NodeConnectorStatisticsUpdate.class, notificationPopListener);
143 //Notification registration for flow-table statistics
144 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
146 //Notification registration for queue-statistics
147 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
149 //Notification for LLDPSpeaker
150 LLDPSpeakerPopListener<DataObject> lldpPopListener = new LLDPSpeakerPopListener<DataObject>();
151 addMessagePopListener(NodeConnectorUpdated.class,lldpPopListener);
153 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
154 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
155 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
159 * @param switchConnectionProvider
160 * the switchConnectionProvider to set
162 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
163 this.switchConnectionProvider = switchConnectionProvider;
167 * @param switchConnectionProviderToUnset
168 * the switchConnectionProvider to unset
170 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
171 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
172 this.switchConnectionProvider = null;
177 * Function called by dependency manager after "init ()" is called and after
178 * the services provided by the class are registered in the service registry
181 public void start() {
182 LOG.debug("starting ..");
183 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
185 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
186 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
187 // configure and startup library servers
188 switchConnectionProvider.configure(getConnectionConfiguration());
189 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
193 * @return wished connections configurations
195 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
196 // TODO:: get config from state manager
197 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
198 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
199 return Lists.newArrayList(configuration, configurationLegacy);
203 * Function called by the dependency manager before the services exported by
204 * the component are unregistered, this will be followed by a "destroy ()"
209 LOG.debug("stopping");
210 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
212 srvStopped.get(5000, TimeUnit.MILLISECONDS);
213 } catch (InterruptedException | ExecutionException | TimeoutException e) {
214 LOG.error(e.getMessage(), e);
219 * Function called by the dependency manager when at least one dependency
220 * become unsatisfied or when the component is shutting down because for
221 * example bundle is being stopped.
224 public void destroy() {
229 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
230 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
232 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
233 if (existingValues == null) {
234 existingValues = new LinkedHashSet<>();
235 messageTranslators.put(tKey, existingValues);
237 existingValues.add(translator);
238 LOG.debug("{} is now translated by {}", messageType, translator);
242 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
243 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
244 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
245 if (values != null) {
246 values.remove(translator);
247 if (values.isEmpty()) {
248 messageTranslators.remove(tKey);
250 LOG.debug("{} is now removed from translators", translator);
255 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
256 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
257 if (existingValues == null) {
258 existingValues = new LinkedHashSet<>();
259 popListeners.put(messageType, existingValues);
261 existingValues.add(popListener);
262 LOG.debug("{} is now popListened by {}", messageType, popListener);
266 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
267 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
268 if (values != null) {
269 values.remove(popListener);
270 if (values.isEmpty()) {
271 popListeners.remove(messageType);
273 LOG.debug("{} is now removed from popListeners", popListener);