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.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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.collect.Lists;
41 public class MDController implements IMDController {
43 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
45 private SwitchConnectionProvider switchConnectionProvider;
47 private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
49 final private int OF10 = 1;
50 final private int OF13 = 4;
53 * @return translator mapping
55 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
56 return messageTranslators;
60 * provisioning of translator mapping
63 LOG.debug("Initializing!");
64 messageTranslators = new ConcurrentHashMap<>();
65 addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
66 addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
67 addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
68 addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
69 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
70 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
74 * @param switchConnectionProvider
75 * the switchConnectionProvider to set
77 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
78 this.switchConnectionProvider = switchConnectionProvider;
82 * @param switchConnectionProviderToUnset
83 * the switchConnectionProvider to unset
85 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
86 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
87 this.switchConnectionProvider = null;
92 * Function called by dependency manager after "init ()" is called and after
93 * the services provided by the class are registered in the service registry
97 LOG.debug("starting ..");
98 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
100 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
101 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
102 // configure and startup library servers
103 switchConnectionProvider.configure(getConnectionConfiguration());
104 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
108 * @return wished connections configurations
110 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
111 // TODO:: get config from state manager
112 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
113 return Lists.newArrayList(configuration);
117 * Function called by the dependency manager before the services exported by
118 * the component are unregistered, this will be followed by a "destroy ()"
123 LOG.debug("stopping");
124 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
126 srvStopped.get(5000, TimeUnit.MILLISECONDS);
127 } catch (InterruptedException | ExecutionException | TimeoutException e) {
128 LOG.error(e.getMessage(), e);
133 * Function called by the dependency manager when at least one dependency
134 * become unsatisfied or when the component is shutting down because for
135 * example bundle is being stopped.
138 public void destroy() {
143 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
144 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
146 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
147 if (existingValues == null) {
148 existingValues = new ArrayList<>();
149 messageTranslators.put(tKey, existingValues);
151 existingValues.add(translator);
152 LOG.debug("{} is now listened by {}", messageType, translator);
156 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
157 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
158 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
159 if (values != null) {
160 values.remove(translator);
161 if (values.isEmpty()) {
162 messageTranslators.remove(tKey);
164 LOG.debug("{} is now removed", translator);