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.PacketInTranslator;
29 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.collect.Lists;
46 public class MDController implements IMDController {
48 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
50 private SwitchConnectionProvider switchConnectionProvider;
52 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
53 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
55 final private int OF10 = 1;
56 final private int OF13 = 4;
60 * @return translator mapping
62 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
63 return messageTranslators;
67 * provisioning of translator mapping
70 LOG.debug("Initializing!");
71 messageTranslators = new ConcurrentHashMap<>();
72 popListeners = new ConcurrentHashMap<>();
73 //TODO: move registration to factory
74 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
75 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
76 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
77 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
78 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
80 //TODO: move registration to factory
81 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
82 addMessagePopListener(PacketReceived.class,notificationPopListener);
83 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
85 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
86 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
87 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
91 * @param switchConnectionProvider
92 * the switchConnectionProvider to set
94 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
95 this.switchConnectionProvider = switchConnectionProvider;
99 * @param switchConnectionProviderToUnset
100 * the switchConnectionProvider to unset
102 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
103 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
104 this.switchConnectionProvider = null;
109 * Function called by dependency manager after "init ()" is called and after
110 * the services provided by the class are registered in the service registry
113 public void start() {
114 LOG.debug("starting ..");
115 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
117 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
118 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
119 // configure and startup library servers
120 switchConnectionProvider.configure(getConnectionConfiguration());
121 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
125 * @return wished connections configurations
127 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
128 // TODO:: get config from state manager
129 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
130 return Lists.newArrayList(configuration);
134 * Function called by the dependency manager before the services exported by
135 * the component are unregistered, this will be followed by a "destroy ()"
140 LOG.debug("stopping");
141 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
143 srvStopped.get(5000, TimeUnit.MILLISECONDS);
144 } catch (InterruptedException | ExecutionException | TimeoutException e) {
145 LOG.error(e.getMessage(), e);
150 * Function called by the dependency manager when at least one dependency
151 * become unsatisfied or when the component is shutting down because for
152 * example bundle is being stopped.
155 public void destroy() {
160 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
161 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
163 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
164 if (existingValues == null) {
165 existingValues = new LinkedHashSet<>();
166 messageTranslators.put(tKey, existingValues);
168 existingValues.add(translator);
169 LOG.debug("{} is now translated by {}", messageType, translator);
173 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
174 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
175 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
176 if (values != null) {
177 values.remove(translator);
178 if (values.isEmpty()) {
179 messageTranslators.remove(tKey);
181 LOG.debug("{} is now removed from translators", translator);
186 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
187 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
188 if (existingValues == null) {
189 existingValues = new LinkedHashSet<>();
190 popListeners.put(messageType, existingValues);
192 existingValues.add(popListener);
193 LOG.debug("{} is now popListened by {}", messageType, popListener);
197 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
198 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
199 if (values != null) {
200 values.remove(popListener);
201 if (values.isEmpty()) {
202 popListeners.remove(messageType);
204 LOG.debug("{} is now removed from popListeners", popListener);