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.MultiPartMessageDescToNodeUpdatedTranslator;
29 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
30 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.collect.Lists;
49 public class MDController implements IMDController {
51 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
53 private SwitchConnectionProvider switchConnectionProvider;
55 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
56 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
58 final private int OF10 = 1;
59 final private int OF13 = 4;
63 * @return translator mapping
65 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
66 return messageTranslators;
70 * provisioning of translator mapping
73 LOG.debug("Initializing!");
74 messageTranslators = new ConcurrentHashMap<>();
75 popListeners = new ConcurrentHashMap<>();
76 //TODO: move registration to factory
77 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
78 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
79 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
80 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
81 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
82 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
84 //TODO: move registration to factory
85 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
86 addMessagePopListener(PacketReceived.class,notificationPopListener);
87 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
88 addMessagePopListener(NodeUpdated.class, notificationPopListener);
90 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
91 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
92 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
96 * @param switchConnectionProvider
97 * the switchConnectionProvider to set
99 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
100 this.switchConnectionProvider = switchConnectionProvider;
104 * @param switchConnectionProviderToUnset
105 * the switchConnectionProvider to unset
107 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
108 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
109 this.switchConnectionProvider = null;
114 * Function called by dependency manager after "init ()" is called and after
115 * the services provided by the class are registered in the service registry
118 public void start() {
119 LOG.debug("starting ..");
120 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
122 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
123 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
124 // configure and startup library servers
125 switchConnectionProvider.configure(getConnectionConfiguration());
126 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
130 * @return wished connections configurations
132 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
133 // TODO:: get config from state manager
134 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
135 return Lists.newArrayList(configuration);
139 * Function called by the dependency manager before the services exported by
140 * the component are unregistered, this will be followed by a "destroy ()"
145 LOG.debug("stopping");
146 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
148 srvStopped.get(5000, TimeUnit.MILLISECONDS);
149 } catch (InterruptedException | ExecutionException | TimeoutException e) {
150 LOG.error(e.getMessage(), e);
155 * Function called by the dependency manager when at least one dependency
156 * become unsatisfied or when the component is shutting down because for
157 * example bundle is being stopped.
160 public void destroy() {
165 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
166 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
168 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
169 if (existingValues == null) {
170 existingValues = new LinkedHashSet<>();
171 messageTranslators.put(tKey, existingValues);
173 existingValues.add(translator);
174 LOG.debug("{} is now translated by {}", messageType, translator);
178 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
179 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
180 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
181 if (values != null) {
182 values.remove(translator);
183 if (values.isEmpty()) {
184 messageTranslators.remove(tKey);
186 LOG.debug("{} is now removed from translators", translator);
191 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
192 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
193 if (existingValues == null) {
194 existingValues = new LinkedHashSet<>();
195 popListeners.put(messageType, existingValues);
197 existingValues.add(popListener);
198 LOG.debug("{} is now popListened by {}", messageType, popListener);
202 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
203 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
204 if (values != null) {
205 values.remove(popListener);
206 if (values.isEmpty()) {
207 popListeners.remove(messageType);
209 LOG.debug("{} is now removed from popListeners", popListener);