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.yangtools.yang.binding.DataObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.collect.Lists;
36 public class MDController implements IMDController {
38 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
40 private SwitchConnectionProvider switchConnectionProvider;
42 private ConcurrentMap<Class<? extends DataObject>, Collection<IMDMessageListener>> messageListeners;
44 public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getMessageListeners() {
45 return messageListeners;
50 LOG.debug("Initializing!");
51 this.messageListeners = new ConcurrentHashMap<Class<? extends DataObject>, Collection<IMDMessageListener>>();
52 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
53 OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
57 * @param switchConnectionProvider
58 * the switchConnectionProvider to set
60 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
61 this.switchConnectionProvider = switchConnectionProvider;
65 * @param switchConnectionProviderToUnset
66 * the switchConnectionProvider to unset
68 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
69 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
70 this.switchConnectionProvider = null;
75 * Function called by dependency manager after "init ()" is called and after
76 * the services provided by the class are registered in the service registry
80 LOG.debug("starting ..");
81 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
83 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
84 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
85 // configure and startup library servers
86 switchConnectionProvider.configure(getConnectionConfiguration());
87 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
91 * @return wished connections configurations
93 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
94 // TODO:: get config from state manager
95 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
96 return Lists.newArrayList(configuration);
100 * Function called by the dependency manager before the services exported by
101 * the component are unregistered, this will be followed by a "destroy ()"
106 LOG.debug("stopping");
107 Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
109 srvStopped.get(5000, TimeUnit.MILLISECONDS);
110 } catch (InterruptedException | ExecutionException | TimeoutException e) {
111 LOG.error(e.getMessage(), e);
116 * Function called by the dependency manager when at least one dependency
117 * become unsatisfied or when the component is shutting down because for
118 * example bundle is being stopped.
121 public void destroy() {
126 public void addMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
128 Collection<IMDMessageListener> existingValues = messageListeners.get(messageType);
129 if (existingValues == null) {
130 existingValues = new ArrayList<IMDMessageListener>();
131 messageListeners.put(messageType, existingValues);
133 existingValues.add(listener);
134 LOG.debug("{} is now listened by {}", messageType, listener);
138 public void removeMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
140 Collection<IMDMessageListener> values = messageListeners.get(messageType);
141 if (values != null) {
142 values.remove(listener);
143 if (values.size() == 0) {
144 messageListeners.remove(messageType);
146 LOG.debug("{} is now removed", listener);