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.Future;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
20 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
21 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.collect.Lists;
33 public class MDController implements IMDController {
35 private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
37 private SwitchConnectionProvider switchConnectionProvider;
39 private ConcurrentMap<Class<? extends DataObject>, Collection<IMDMessageListener>> messageListeners;
41 public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getMessageListeners() {
42 return messageListeners;
47 LOG.debug("Initializing!");
48 this.messageListeners = new ConcurrentHashMap<Class<? extends DataObject>, Collection<IMDMessageListener>>();
52 * @param switchConnectionProvider
53 * the switchConnectionProvider to set
55 public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
56 this.switchConnectionProvider = switchConnectionProvider;
60 * @param switchConnectionProviderToUnset
61 * the switchConnectionProvider to unset
63 public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
64 if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
65 this.switchConnectionProvider = null;
70 * Function called by dependency manager after "init ()" is called and after
71 * the services provided by the class are registered in the service registry
75 LOG.debug("starting ..");
76 LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
78 SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
79 switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
80 // configure and startup library servers
81 switchConnectionProvider.configure(getConnectionConfiguration());
82 Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
86 * @return wished connections configurations
88 private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
89 // TODO:: get config from state manager
90 ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
91 return Lists.newArrayList(configuration);
95 * Function called by the dependency manager before the services exported by
96 * the component are unregistered, this will be followed by a "destroy ()"
101 LOG.debug("stopping");
105 * Function called by the dependency manager when at least one dependency
106 * become unsatisfied or when the component is shutting down because for
107 * example bundle is being stopped.
110 public void destroy() {
115 public void addMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
117 Collection<IMDMessageListener> existingValues = messageListeners.get(messageType);
118 if (existingValues == null) {
119 existingValues = new ArrayList<IMDMessageListener>();
120 messageListeners.put(messageType, existingValues);
122 existingValues.add(listener);
123 // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
124 OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
125 LOG.debug("{} is now listened by {}", messageType, listener);
129 public void removeMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
131 Collection<IMDMessageListener> values = messageListeners.get(messageType);
132 if (values != null) {
133 values.remove(listener);
134 if (values.size() == 0) {
135 messageListeners.remove(messageType);
137 //Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
138 OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
139 LOG.debug("{} is now removed", listener);