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.queue.PopListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.PortStatisticsUpdate;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdated;
61 import org.opendaylight.yangtools.yang.binding.DataObject;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.common.collect.Lists;
71 public class MDController implements IMDController {
73 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
75 private SwitchConnectionProvider switchConnectionProvider;
77 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
78 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
80 final private int OF10 = 1;
81 final private int OF13 = 4;
85 * @return translator mapping
87 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
88 return messageTranslators;
92 * provisioning of translator mapping
95 LOG.debug("Initializing!");
96 messageTranslators = new ConcurrentHashMap<>();
97 popListeners = new ConcurrentHashMap<>();
98 //TODO: move registration to factory
99 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
100 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
101 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
102 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
103 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
104 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
105 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
106 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
107 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
108 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
109 addMessageTranslator(MultipartReplyMessage.class,OF10, new MultipartReplyTranslator());
110 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
111 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultipartReplyTableFeaturesToTableUpdatedTranslator());
113 //TODO: move registration to factory
114 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
115 addMessagePopListener(NodeErrorNotification.class, notificationPopListener);
116 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
117 addMessagePopListener(PacketReceived.class,notificationPopListener);
118 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
119 addMessagePopListener(NodeUpdated.class, notificationPopListener);
121 addMessagePopListener(SwitchFlowRemoved.class, notificationPopListener);
122 addMessagePopListener(TableUpdated.class, notificationPopListener);
124 //Notification registration for flow statistics
125 addMessagePopListener(FlowsStatisticsUpdate.class, notificationPopListener);
126 addMessagePopListener(AggregateFlowStatisticsUpdate.class, notificationPopListener);
128 //Notification registrations for group-statistics
129 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
130 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
131 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
133 //Notification registrations for meter-statistics
134 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
135 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
136 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
138 //Notification registration for port-statistics
139 addMessagePopListener(PortStatisticsUpdate.class, notificationPopListener);
141 //Notification registration for flow-table statistics
142 addMessagePopListener(FlowTableStatisticsUpdate.class, notificationPopListener);
144 //Notification registration for queue-statistics
145 addMessagePopListener(QueueStatisticsUpdate.class, notificationPopListener);
147 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
148 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
149 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
153 * @param switchConnectionProvider
154 * the switchConnectionProvider to set
156 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
157 this.switchConnectionProvider = switchConnectionProvider;
161 * @param switchConnectionProviderToUnset
162 * the switchConnectionProvider to unset
164 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
165 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
166 this.switchConnectionProvider = null;
171 * Function called by dependency manager after "init ()" is called and after
172 * the services provided by the class are registered in the service registry
175 public void start() {
176 LOG.debug("starting ..");
177 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
179 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
180 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
181 // configure and startup library servers
182 switchConnectionProvider.configure(getConnectionConfiguration());
183 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
187 * @return wished connections configurations
189 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
190 // TODO:: get config from state manager
191 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
192 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
193 return Lists.newArrayList(configuration, configurationLegacy);
197 * Function called by the dependency manager before the services exported by
198 * the component are unregistered, this will be followed by a "destroy ()"
203 LOG.debug("stopping");
204 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
206 srvStopped.get(5000, TimeUnit.MILLISECONDS);
207 } catch (InterruptedException | ExecutionException | TimeoutException e) {
208 LOG.error(e.getMessage(), e);
213 * Function called by the dependency manager when at least one dependency
214 * become unsatisfied or when the component is shutting down because for
215 * example bundle is being stopped.
218 public void destroy() {
223 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
224 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
226 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
227 if (existingValues == null) {
228 existingValues = new LinkedHashSet<>();
229 messageTranslators.put(tKey, existingValues);
231 existingValues.add(translator);
232 LOG.debug("{} is now translated by {}", messageType, translator);
236 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
237 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
238 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
239 if (values != null) {
240 values.remove(translator);
241 if (values.isEmpty()) {
242 messageTranslators.remove(tKey);
244 LOG.debug("{} is now removed from translators", translator);
249 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
250 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
251 if (existingValues == null) {
252 existingValues = new LinkedHashSet<>();
253 popListeners.put(messageType, existingValues);
255 existingValues.add(popListener);
256 LOG.debug("{} is now popListened by {}", messageType, popListener);
260 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
261 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
262 if (values != null) {
263 values.remove(popListener);
264 if (values.isEmpty()) {
265 popListeners.remove(messageType);
267 LOG.debug("{} is now removed from popListeners", popListener);