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.PacketInTranslator;
31 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.collect.Lists;
51 public class MDController implements IMDController {
53 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
55 private SwitchConnectionProvider switchConnectionProvider;
57 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
58 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
60 final private int OF10 = 1;
61 final private int OF13 = 4;
65 * @return translator mapping
67 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
68 return messageTranslators;
72 * provisioning of translator mapping
75 LOG.debug("Initializing!");
76 messageTranslators = new ConcurrentHashMap<>();
77 popListeners = new ConcurrentHashMap<>();
78 //TODO: move registration to factory
79 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
80 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
81 addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
82 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
83 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
84 addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
85 addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
87 //TODO: move registration to factory
88 NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
89 addMessagePopListener(PacketReceived.class,notificationPopListener);
90 addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
91 addMessagePopListener(NodeUpdated.class, notificationPopListener);
93 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
94 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
95 OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
99 * @param switchConnectionProvider
100 * the switchConnectionProvider to set
102 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
103 this.switchConnectionProvider = switchConnectionProvider;
107 * @param switchConnectionProviderToUnset
108 * the switchConnectionProvider to unset
110 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
111 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
112 this.switchConnectionProvider = null;
117 * Function called by dependency manager after "init ()" is called and after
118 * the services provided by the class are registered in the service registry
121 public void start() {
122 LOG.debug("starting ..");
123 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
125 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
126 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
127 // configure and startup library servers
128 switchConnectionProvider.configure(getConnectionConfiguration());
129 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
133 * @return wished connections configurations
135 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
136 // TODO:: get config from state manager
137 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
138 return Lists.newArrayList(configuration);
142 * Function called by the dependency manager before the services exported by
143 * the component are unregistered, this will be followed by a "destroy ()"
148 LOG.debug("stopping");
149 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
151 srvStopped.get(5000, TimeUnit.MILLISECONDS);
152 } catch (InterruptedException | ExecutionException | TimeoutException e) {
153 LOG.error(e.getMessage(), e);
158 * Function called by the dependency manager when at least one dependency
159 * become unsatisfied or when the component is shutting down because for
160 * example bundle is being stopped.
163 public void destroy() {
168 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
169 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
171 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
172 if (existingValues == null) {
173 existingValues = new LinkedHashSet<>();
174 messageTranslators.put(tKey, existingValues);
176 existingValues.add(translator);
177 LOG.debug("{} is now translated by {}", messageType, translator);
181 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
182 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
183 Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
184 if (values != null) {
185 values.remove(translator);
186 if (values.isEmpty()) {
187 messageTranslators.remove(tKey);
189 LOG.debug("{} is now removed from translators", translator);
194 public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
195 Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
196 if (existingValues == null) {
197 existingValues = new LinkedHashSet<>();
198 popListeners.put(messageType, existingValues);
200 existingValues.add(popListener);
201 LOG.debug("{} is now popListened by {}", messageType, popListener);
205 public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
206 Collection<PopListener<DataObject>> values = popListeners.get(messageType);
207 if (values != null) {
208 values.remove(popListener);
209 if (values.isEmpty()) {
210 popListeners.remove(messageType);
212 LOG.debug("{} is now removed from popListeners", popListener);