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.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.LinkedHashSet;
15 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
25 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
26 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
28 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
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.openflow.protocol.rev130731.ErrorMessage;
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.TransmitPacketInput;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.collect.Lists;
45 public class MDController implements IMDController {
47 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
49 private SwitchConnectionProvider switchConnectionProvider;
51 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
52 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
54 final private int OF10 = 1;
55 final private int OF13 = 4;
59 * @return translator mapping
61 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
62 return messageTranslators;
66 * provisioning of translator mapping
69 LOG.debug("Initializing!");
70 messageTranslators = new ConcurrentHashMap<>();
71 popListeners = new ConcurrentHashMap<>();
72 //TODO: move registration to factory
73 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
74 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
75 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
76 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
78 //TODO: move registration to factory
79 addMessagePopListener(TransmitPacketInput.class, new NotificationPopListener<DataObject>());
81 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
82 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
83 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
87 * @param switchConnectionProvider
88 * the switchConnectionProvider to set
90 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
91 this.switchConnectionProvider = switchConnectionProvider;
95 * @param switchConnectionProviderToUnset
96 * the switchConnectionProvider to unset
98 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
99 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
100 this.switchConnectionProvider = null;
105 * Function called by dependency manager after "init ()" is called and after
106 * the services provided by the class are registered in the service registry
109 public void start() {
110 LOG.debug("starting ..");
111 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
113 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
114 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
115 // configure and startup library servers
116 switchConnectionProvider.configure(getConnectionConfiguration());
117 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
121 * @return wished connections configurations
123 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
124 // TODO:: get config from state manager
125 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
126 return Lists.newArrayList(configuration);
130 * Function called by the dependency manager before the services exported by
131 * the component are unregistered, this will be followed by a "destroy ()"
136 LOG.debug("stopping");
137 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
139 srvStopped.get(5000, TimeUnit.MILLISECONDS);
140 } catch (InterruptedException | ExecutionException | TimeoutException e) {
141 LOG.error(e.getMessage(), e);
146 * Function called by the dependency manager when at least one dependency
147 * become unsatisfied or when the component is shutting down because for
148 * example bundle is being stopped.
151 public void destroy() {
156 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
157 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
159 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
160 if (existingValues == null) {
161 existingValues = new LinkedHashSet<>();
162 messageTranslators.put(tKey, existingValues);
164 existingValues.add(translator);
165 LOG.debug("{} is now translated by {}", messageType, translator);
169 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
170 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
171 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
172 if (values != null) {
173 values.remove(translator);
174 if (values.isEmpty()) {
175 messageTranslators.remove(tKey);
177 LOG.debug("{} is now removed from translators", translator);
182 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
183 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
184 if (existingValues == null) {
185 existingValues = new LinkedHashSet<>();
186 popListeners.put(messageType, existingValues);
188 existingValues.add(popListener);
189 LOG.debug("{} is now popListened by {}", messageType, popListener);
193 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
194 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
195 if (values != null) {
196 values.remove(popListener);
197 if (values.isEmpty()) {
198 popListeners.remove(messageType);
200 LOG.debug("{} is now removed from popListeners", popListener);