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.PacketInTranslator;
28 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.collect.Lists;
44 public class MDController implements IMDController {
46 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
48 private SwitchConnectionProvider switchConnectionProvider;
50 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
51 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
53 final private int OF10 = 1;
54 final private int OF13 = 4;
58 * @return translator mapping
60 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
61 return messageTranslators;
65 * provisioning of translator mapping
68 LOG.debug("Initializing!");
69 messageTranslators = new ConcurrentHashMap<>();
70 popListeners = new ConcurrentHashMap<>();
71 //TODO: move registration to factory
72 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
73 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
74 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
75 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
77 //TODO: move registration to factory
78 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
79 addMessagePopListener(PacketReceived.class,notificationPopListener);
80 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
82 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
83 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
84 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
88 * @param switchConnectionProvider
89 * the switchConnectionProvider to set
91 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
92 this.switchConnectionProvider = switchConnectionProvider;
96 * @param switchConnectionProviderToUnset
97 * the switchConnectionProvider to unset
99 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
100 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
101 this.switchConnectionProvider = null;
106 * Function called by dependency manager after "init ()" is called and after
107 * the services provided by the class are registered in the service registry
110 public void start() {
111 LOG.debug("starting ..");
112 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
114 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
115 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
116 // configure and startup library servers
117 switchConnectionProvider.configure(getConnectionConfiguration());
118 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
122 * @return wished connections configurations
124 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
125 // TODO:: get config from state manager
126 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
127 return Lists.newArrayList(configuration);
131 * Function called by the dependency manager before the services exported by
132 * the component are unregistered, this will be followed by a "destroy ()"
137 LOG.debug("stopping");
138 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
140 srvStopped.get(5000, TimeUnit.MILLISECONDS);
141 } catch (InterruptedException | ExecutionException | TimeoutException e) {
142 LOG.error(e.getMessage(), e);
147 * Function called by the dependency manager when at least one dependency
148 * become unsatisfied or when the component is shutting down because for
149 * example bundle is being stopped.
152 public void destroy() {
157 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
158 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
160 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
161 if (existingValues == null) {
162 existingValues = new LinkedHashSet<>();
163 messageTranslators.put(tKey, existingValues);
165 existingValues.add(translator);
166 LOG.debug("{} is now translated by {}", messageType, translator);
170 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
171 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
172 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
173 if (values != null) {
174 values.remove(translator);
175 if (values.isEmpty()) {
176 messageTranslators.remove(tKey);
178 LOG.debug("{} is now removed from translators", translator);
183 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
184 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
185 if (existingValues == null) {
186 existingValues = new LinkedHashSet<>();
187 popListeners.put(messageType, existingValues);
189 existingValues.add(popListener);
190 LOG.debug("{} is now popListened by {}", messageType, popListener);
194 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
195 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
196 if (values != null) {
197 values.remove(popListener);
198 if (values.isEmpty()) {
199 popListeners.remove(messageType);
201 LOG.debug("{} is now removed from popListeners", popListener);