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.PacketInTranslator;
32 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
33 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import com.google.common.collect.Lists;
55 public class MDController implements IMDController {
57 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
59 private SwitchConnectionProvider switchConnectionProvider;
61 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
62 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
64 final private int OF10 = 1;
65 final private int OF13 = 4;
69 * @return translator mapping
71 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
72 return messageTranslators;
76 * provisioning of translator mapping
79 LOG.debug("Initializing!");
80 messageTranslators = new ConcurrentHashMap<>();
81 popListeners = new ConcurrentHashMap<>();
82 //TODO: move registration to factory
83 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
84 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
85 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
86 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
87 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
88 addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
89 addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
90 addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
91 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
92 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
94 //TODO: move registration to factory
95 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
96 addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
97 addMessagePopListener(PacketReceived.class,notificationPopListener);
98 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
99 addMessagePopListener(NodeUpdated.class, notificationPopListener);
101 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
102 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
103 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
107 * @param switchConnectionProvider
108 * the switchConnectionProvider to set
110 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
111 this.switchConnectionProvider = switchConnectionProvider;
115 * @param switchConnectionProviderToUnset
116 * the switchConnectionProvider to unset
118 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
119 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
120 this.switchConnectionProvider = null;
125 * Function called by dependency manager after "init ()" is called and after
126 * the services provided by the class are registered in the service registry
129 public void start() {
130 LOG.debug("starting ..");
131 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
133 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
134 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
135 // configure and startup library servers
136 switchConnectionProvider.configure(getConnectionConfiguration());
137 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
141 * @return wished connections configurations
143 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
144 // TODO:: get config from state manager
145 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
146 ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
147 return Lists.newArrayList(configuration, configurationLegacy);
151 * Function called by the dependency manager before the services exported by
152 * the component are unregistered, this will be followed by a "destroy ()"
157 LOG.debug("stopping");
158 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
160 srvStopped.get(5000, TimeUnit.MILLISECONDS);
161 } catch (InterruptedException | ExecutionException | TimeoutException e) {
162 LOG.error(e.getMessage(), e);
167 * Function called by the dependency manager when at least one dependency
168 * become unsatisfied or when the component is shutting down because for
169 * example bundle is being stopped.
172 public void destroy() {
177 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
178 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
180 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
181 if (existingValues == null) {
182 existingValues = new LinkedHashSet<>();
183 messageTranslators.put(tKey, existingValues);
185 existingValues.add(translator);
186 LOG.debug("{} is now translated by {}", messageType, translator);
190 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
191 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
192 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
193 if (values != null) {
194 values.remove(translator);
195 if (values.isEmpty()) {
196 messageTranslators.remove(tKey);
198 LOG.debug("{} is now removed from translators", translator);
203 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
204 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
205 if (existingValues == null) {
206 existingValues = new LinkedHashSet<>();
207 popListeners.put(messageType, existingValues);
209 existingValues.add(popListener);
210 LOG.debug("{} is now popListened by {}", messageType, popListener);
214 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
215 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
216 if (values != null) {
217 values.remove(popListener);
218 if (values.isEmpty()) {
219 popListeners.remove(messageType);
221 LOG.debug("{} is now removed from popListeners", popListener);