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.Collections;
14 import java.util.List;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
24 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
25 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
26 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
27 import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
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.yangtools.yang.binding.DataObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.collect.Collections2;
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;
50 * @return translator mapping
52 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
53 return messageTranslators;
57 * provisioning of translator mapping
60 LOG.debug("Initializing!");
61 messageTranslators = new ConcurrentHashMap<>();
62 addMessageTranslator(ErrorMessage.class, 4, new ErrorTranslator());
63 addMessageTranslator(ErrorMessage.class, 1, new ErrorTranslator());
65 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
66 OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
70 * @param switchConnectionProvider
71 * the switchConnectionProvider to set
73 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
74 this.switchConnectionProvider = switchConnectionProvider;
78 * @param switchConnectionProviderToUnset
79 * the switchConnectionProvider to unset
81 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
82 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
83 this.switchConnectionProvider = null;
88 * Function called by dependency manager after "init ()" is called and after
89 * the services provided by the class are registered in the service registry
93 LOG.debug("starting ..");
94 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
96 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
97 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
98 // configure and startup library servers
99 switchConnectionProvider.configure(getConnectionConfiguration());
100 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
104 * @return wished connections configurations
106 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
107 // TODO:: get config from state manager
108 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
109 return Lists.newArrayList(configuration);
113 * Function called by the dependency manager before the services exported by
114 * the component are unregistered, this will be followed by a "destroy ()"
119 LOG.debug("stopping");
120 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
122 srvStopped.get(5000, TimeUnit.MILLISECONDS);
123 } catch (InterruptedException | ExecutionException | TimeoutException e) {
124 LOG.error(e.getMessage(), e);
129 * Function called by the dependency manager when at least one dependency
130 * become unsatisfied or when the component is shutting down because for
131 * example bundle is being stopped.
134 public void destroy() {
139 public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
140 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
142 Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
143 if (existingValues == null) {
144 existingValues = new ArrayList<>();
145 messageTranslators.put(tKey, existingValues);
147 existingValues.add(translator);
148 LOG.debug("{} is now listened by {}", messageType, translator);
152 public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
153 TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
154 Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
155 if (values != null) {
156 values.remove(translator);
157 if (values.isEmpty()) {
158 messageTranslators.remove(tKey);
160 LOG.debug("{} is now removed", translator);