3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 * Connection Manager provides south-bound connectivity services.
12 * The APIs are currently focused towards Active-Active Clustering support
13 * wherein the node can connect to any of the Active Controller in the Cluster.
14 * This component can also host the necessary logic for south-bound connectivity
15 * when partial cluster is identified during Partition scenarios.
17 * But this (and its corresponding implementation) component can also be used for
18 * basic connectivity mechansims for various south-bound plugins.
21 package org.opendaylight.controller.connectionmanager.internal;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.util.HashSet;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ConcurrentMap;
31 import java.util.concurrent.LinkedBlockingQueue;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.eclipse.osgi.framework.console.CommandInterpreter;
37 import org.eclipse.osgi.framework.console.CommandProvider;
38 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
39 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
40 import org.opendaylight.controller.clustering.services.ICoordinatorChangeAware;
41 import org.opendaylight.controller.connectionmanager.ConnectionMgmtScheme;
42 import org.opendaylight.controller.connectionmanager.IConnectionManager;
43 import org.opendaylight.controller.connectionmanager.scheme.AbstractScheme;
44 import org.opendaylight.controller.connectionmanager.scheme.SchemeFactory;
45 import org.opendaylight.controller.sal.connection.ConnectionConstants;
46 import org.opendaylight.controller.sal.connection.IConnectionListener;
47 import org.opendaylight.controller.sal.connection.IConnectionService;
48 import org.opendaylight.controller.sal.core.Node;
49 import org.opendaylight.controller.sal.core.NodeConnector;
50 import org.opendaylight.controller.sal.core.Property;
51 import org.opendaylight.controller.sal.core.UpdateType;
52 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
53 import org.opendaylight.controller.sal.utils.Status;
54 import org.opendaylight.controller.sal.utils.StatusCode;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.FrameworkUtil;
58 public class ConnectionManager implements IConnectionManager, IConnectionListener,
59 ICoordinatorChangeAware, IListenInventoryUpdates,
60 ICacheUpdateAware<Node, Set<InetAddress>>,
62 private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
63 private ConnectionMgmtScheme activeScheme = ConnectionMgmtScheme.ANY_CONTROLLER_ONE_MASTER;
64 private IClusterGlobalServices clusterServices;
65 private ConcurrentMap<ConnectionMgmtScheme, AbstractScheme> schemes;
66 private IConnectionService connectionService;
67 private Thread connectionEventThread;
68 private BlockingQueue<ConnectionMgmtEvent> connectionEvents;
70 public void setClusterServices(IClusterGlobalServices i) {
71 this.clusterServices = i;
74 public void unsetClusterServices(IClusterGlobalServices i) {
75 if (this.clusterServices == i) {
76 this.clusterServices = null;
80 public void setConnectionService(IConnectionService i) {
81 this.connectionService = i;
84 public void unsetConnectionService(IConnectionService i) {
85 if (this.connectionService == i) {
86 this.connectionService = null;
90 public void started() {
91 connectionEventThread = new Thread(new EventHandler(), "ConnectionEvent Thread");
92 connectionEventThread.start();
94 registerWithOSGIConsole();
95 notifyClusterViewChanged();
99 this.connectionEvents = new LinkedBlockingQueue<ConnectionMgmtEvent>();
100 schemes = new ConcurrentHashMap<ConnectionMgmtScheme, AbstractScheme>();
101 for (ConnectionMgmtScheme scheme : ConnectionMgmtScheme.values()) {
102 AbstractScheme schemeImpl = SchemeFactory.getScheme(scheme, clusterServices);
103 if (schemeImpl != null) schemes.put(scheme, schemeImpl);
108 connectionEventThread.interrupt();
109 Set<Node> localNodes = getLocalNodes();
110 if (localNodes != null) {
111 AbstractScheme scheme = schemes.get(activeScheme);
112 for (Node localNode : localNodes) {
113 connectionService.disconnect(localNode);
114 if (scheme != null) scheme.removeNode(localNode);
120 public ConnectionMgmtScheme getActiveScheme() {
125 public Set<Node> getNodes(InetAddress controller) {
126 AbstractScheme scheme = schemes.get(activeScheme);
127 if (scheme == null) return null;
128 return scheme.getNodes(controller);
132 public Set<Node> getLocalNodes() {
133 AbstractScheme scheme = schemes.get(activeScheme);
134 if (scheme == null) return null;
135 return scheme.getNodes();
139 public boolean isLocal(Node node) {
140 AbstractScheme scheme = schemes.get(activeScheme);
141 if (scheme == null) return false;
142 return scheme.isLocal(node);
146 public void updateNode(Node node, UpdateType type, Set<Property> props) {
147 logger.debug("updateNode: {} type {} props {}", node, type, props);
148 AbstractScheme scheme = schemes.get(activeScheme);
149 if (scheme == null) return;
152 scheme.addNode(node);
155 scheme.removeNode(node);
163 public void updateNodeConnector(NodeConnector nodeConnector,
164 UpdateType type, Set<Property> props) {
165 logger.debug("updateNodeConnector: {} type {} props {}", nodeConnector, type, props);
166 AbstractScheme scheme = schemes.get(activeScheme);
167 if (scheme == null) return;
170 scheme.addNode(nodeConnector.getNode());
178 public void coordinatorChanged() {
179 notifyClusterViewChanged();
183 public Node connect(String connectionIdentifier, Map<ConnectionConstants, String> params) {
184 if (connectionService == null) return null;
185 return connectionService.connect(connectionIdentifier, params);
189 public Node connect(String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
190 if (connectionService == null) return null;
191 return connectionService.connect(type, connectionIdentifier, params);
195 public Status disconnect (Node node) {
196 if (connectionService == null) return new Status(StatusCode.NOSERVICE);
197 return connectionService.disconnect(node);
201 public void entryCreated(Node key, String cacheName, boolean originLocal) {
202 if (originLocal) return;
206 * Clustering Services' doesnt provide the existing states in the cache update callbacks.
207 * Hence, using a scratch local cache to maintain the existing state.
210 private ConcurrentMap<Node, Set<InetAddress>> existingConnections = new ConcurrentHashMap<Node, Set<InetAddress>>();
213 public void entryUpdated(Node node, Set<InetAddress> newControllers, String cacheName, boolean originLocal) {
214 if (originLocal) return;
215 Set<InetAddress> existingControllers = existingConnections.get(node);
216 if (existingControllers != null) {
217 logger.debug("Processing Update for : {} NewControllers : {} existingControllers : {}", node,
218 newControllers.toString(), existingControllers.toString());
219 if (newControllers.size() < existingControllers.size()) {
220 Set<InetAddress> removed = new HashSet<InetAddress>(existingControllers);
221 if (removed.removeAll(newControllers)) {
222 logger.debug("notifyNodeDisconnectFromMaster({})", node);
223 notifyNodeDisconnectedEvent(node);
227 logger.debug("Ignoring the Update for : {} NewControllers : {}", node, newControllers.toString());
229 existingConnections.put(node, newControllers);
233 public void entryDeleted(Node key, String cacheName, boolean originLocal) {
234 if (originLocal) return;
235 logger.debug("Deleted : {} cache : {}", key, cacheName);
236 notifyNodeDisconnectedEvent(key);
239 private void enqueueConnectionEvent(ConnectionMgmtEvent event) {
241 if (!connectionEvents.contains(event)) {
242 this.connectionEvents.put(event);
244 } catch (InterruptedException e) {
245 logger.debug("enqueueConnectionEvent caught Interrupt Exception for event {}", event);
249 private void notifyClusterViewChanged() {
250 ConnectionMgmtEvent event = new ConnectionMgmtEvent(ConnectionMgmtEventType.CLUSTER_VIEW_CHANGED, null);
251 enqueueConnectionEvent(event);
254 private void notifyNodeDisconnectedEvent(Node node) {
255 ConnectionMgmtEvent event = new ConnectionMgmtEvent(ConnectionMgmtEventType.NODE_DISCONNECTED_FROM_MASTER, node);
256 enqueueConnectionEvent(event);
260 * this thread monitors the connectionEvent queue for new incoming events from
262 private class EventHandler implements Runnable {
268 ConnectionMgmtEvent ev = connectionEvents.take();
269 ConnectionMgmtEventType eType = ev.getEvent();
271 case NODE_DISCONNECTED_FROM_MASTER:
272 Node node = (Node)ev.getData();
273 connectionService.notifyNodeDisconnectFromMaster(node);
275 case CLUSTER_VIEW_CHANGED:
276 AbstractScheme scheme = schemes.get(activeScheme);
277 if (scheme == null) return;
278 scheme.handleClusterViewChanged();
279 connectionService.notifyClusterViewChanged();
282 logger.error("Unknown Connection event {}", eType.ordinal());
284 } catch (InterruptedException e) {
285 connectionEvents.clear();
292 private void registerWithOSGIConsole() {
293 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
295 bundleContext.registerService(CommandProvider.class.getName(), this,
299 public void _scheme (CommandInterpreter ci) {
300 String schemeStr = ci.nextArgument();
301 if (schemeStr == null) {
302 ci.println("Please enter valid Scheme name");
303 ci.println("Current Scheme : " + activeScheme.name());
306 ConnectionMgmtScheme scheme = ConnectionMgmtScheme.valueOf(schemeStr);
307 if (scheme == null) {
308 ci.println("Please enter a valid Scheme name");
311 activeScheme = scheme;
314 public void _printNodes (CommandInterpreter ci) {
315 String controller = ci.nextArgument();
316 if (controller == null) {
317 ci.println("Nodes connected to this controller : ");
318 if (this.getLocalNodes() == null) ci.println("None");
319 else ci.println(this.getLocalNodes().toString());
323 InetAddress address = InetAddress.getByName(controller);
324 ci.println("Nodes connected to controller "+controller);
325 if (this.getNodes(address) == null) ci.println("None");
326 else ci.println(this.getNodes(address).toString());
328 } catch (UnknownHostException e) {
334 public String getHelp() {
335 StringBuffer help = new StringBuffer();
336 help.append("---Connection Manager---\n");
337 help.append("\t scheme [<name>] - Print / Set scheme\n");
338 help.append("\t printNodes [<controller>] - Print connected nodes\n");
339 return help.toString();