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.MultipartReplyTranslator;
32 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
33 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
34 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
52 import org.opendaylight.yangtools.yang.binding.DataObject;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.common.collect.Lists;
62 public class MDController implements IMDController {
64 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
66 private SwitchConnectionProvider switchConnectionProvider;
68 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
69 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
71 final private int OF10 = 1;
72 final private int OF13 = 4;
76 * @return translator mapping
78 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
79 return messageTranslators;
83 * provisioning of translator mapping
86 LOG.debug("Initializing!");
87 messageTranslators = new ConcurrentHashMap<>();
88 popListeners = new ConcurrentHashMap<>();
89 //TODO: move registration to factory
90 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
91 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
92 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
93 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
94 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
95 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
96 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
97 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
98 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
99 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
100 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultipartReplyTranslator());
102 //TODO: move registration to factory
103 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
104 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
105 addMessagePopListener(PacketReceived.class,notificationPopListener);
106 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
107 addMessagePopListener(NodeUpdated.class, notificationPopListener);
109 //Notification registrations for group-statistics
110 addMessagePopListener(GroupStatisticsUpdated.class, notificationPopListener);
111 addMessagePopListener(GroupFeaturesUpdated.class, notificationPopListener);
112 addMessagePopListener(GroupDescStatsUpdated.class, notificationPopListener);
114 //Notification registrations for meter-statistics
115 addMessagePopListener(MeterStatisticsUpdated.class, notificationPopListener);
116 addMessagePopListener(MeterConfigStatsUpdated.class, notificationPopListener);
117 addMessagePopListener(MeterFeaturesUpdated.class, notificationPopListener);
120 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
121 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
122 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
126 * @param switchConnectionProvider
127 * the switchConnectionProvider to set
129 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
130 this.switchConnectionProvider = switchConnectionProvider;
134 * @param switchConnectionProviderToUnset
135 * the switchConnectionProvider to unset
137 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
138 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
139 this.switchConnectionProvider = null;
144 * Function called by dependency manager after "init ()" is called and after
145 * the services provided by the class are registered in the service registry
148 public void start() {
149 LOG.debug("starting ..");
150 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
152 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
153 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
154 // configure and startup library servers
155 switchConnectionProvider.configure(getConnectionConfiguration());
156 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
160 * @return wished connections configurations
162 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
163 // TODO:: get config from state manager
164 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
165 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
166 return Lists.newArrayList(configuration, configurationLegacy);
170 * Function called by the dependency manager before the services exported by
171 * the component are unregistered, this will be followed by a "destroy ()"
176 LOG.debug("stopping");
177 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
179 srvStopped.get(5000, TimeUnit.MILLISECONDS);
180 } catch (InterruptedException | ExecutionException | TimeoutException e) {
181 LOG.error(e.getMessage(), e);
186 * Function called by the dependency manager when at least one dependency
187 * become unsatisfied or when the component is shutting down because for
188 * example bundle is being stopped.
191 public void destroy() {
196 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
197 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
199 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
200 if (existingValues == null) {
201 existingValues = new LinkedHashSet<>();
202 messageTranslators.put(tKey, existingValues);
204 existingValues.add(translator);
205 LOG.debug("{} is now translated by {}", messageType, translator);
209 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
210 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
211 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
212 if (values != null) {
213 values.remove(translator);
214 if (values.isEmpty()) {
215 messageTranslators.remove(tKey);
217 LOG.debug("{} is now removed from translators", translator);
222 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
223 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
224 if (existingValues == null) {
225 existingValues = new LinkedHashSet<>();
226 popListeners.put(messageType, existingValues);
228 existingValues.add(popListener);
229 LOG.debug("{} is now popListened by {}", messageType, popListener);
233 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
234 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
235 if (values != null) {
236 values.remove(popListener);
237 if (values.isEmpty()) {
238 popListeners.remove(messageType);
240 LOG.debug("{} is now removed from popListeners", popListener);