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.controller.sal.connection.implementation.internal;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.controller.sal.connection.ConnectionConstants;
16 import org.opendaylight.controller.sal.connection.ConnectionLocality;
17 import org.opendaylight.controller.sal.connection.IConnectionListener;
18 import org.opendaylight.controller.sal.connection.IConnectionService;
19 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
20 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.utils.GlobalConstants;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.controller.sal.utils.StatusCode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class ConnectionService implements IPluginOutConnectionService, IConnectionService {
29 protected static final Logger logger = LoggerFactory
30 .getLogger(ConnectionService.class);
31 private IConnectionListener connectionListener;
32 private ConcurrentMap<String, IPluginInConnectionService> pluginService =
33 new ConcurrentHashMap<String, IPluginInConnectionService>();
35 void setPluginService (Map props, IPluginInConnectionService s) {
37 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
38 if (value instanceof String) {
39 type = (String) value;
42 logger.error("Received a PluginInConnectionService without any "
43 + "protocolPluginType provided");
45 this.pluginService.put(type, s);
49 void unsetPluginService(Map props, IPluginInConnectionService s) {
52 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
53 if (value instanceof String) {
54 type = (String) value;
57 logger.error("Received a PluginInConnectionService without any "
58 + "protocolPluginType provided");
59 } else if (this.pluginService.get(type).equals(s)) {
60 this.pluginService.remove(type);
64 void setListener(IConnectionListener s) {
65 this.connectionListener = s;
68 void unsetListener(IConnectionListener s) {
69 if (this.connectionListener == s) {
70 this.connectionListener = null;
75 * Function called by the dependency manager when all the required
76 * dependencies are satisfied
83 * Function called by the dependency manager when at least one dependency
84 * become unsatisfied or when the component is shutting down because for
85 * example bundle is being stopped.
89 connectionListener = null;
90 if (this.pluginService != null) {
91 this.pluginService.clear();
96 * Method to test if a node is local to a controller.
98 * @return true if node is local to this controller. false otherwise.
100 public boolean isLocal(Node node) {
101 if (this.connectionListener == null) return false;
102 return connectionListener.isLocal(node);
106 public ConnectionLocality getLocalityStatus(Node node) {
107 if (this.connectionListener == null) return ConnectionLocality.NOT_CONNECTED;
108 return connectionListener.getLocalityStatus(node);
112 public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
113 IPluginInConnectionService s = pluginService.get(type);
115 return s.connect(connectionIdentifier, params);
121 public Node connect (String connectionIdentifier, Map<ConnectionConstants, String> params) {
122 synchronized (this.pluginService) {
123 for (String pluginType : this.pluginService.keySet()) {
124 IPluginInConnectionService s = pluginService.get(pluginType);
125 Node node = s.connect(connectionIdentifier, params);
135 public Status disconnect(Node node) {
136 IPluginInConnectionService s = pluginService.get(node.getType());
137 if (s != null) return s.disconnect(node);
138 return new Status(StatusCode.NOTFOUND);
142 * View Change notification
145 public void notifyClusterViewChanged() {
146 for (String pluginType : this.pluginService.keySet()) {
147 IPluginInConnectionService s = pluginService.get(pluginType);
148 s.notifyClusterViewChanged();
153 * Node Disconnected from the node's master controller.
156 public void notifyNodeDisconnectFromMaster(Node node) {
157 IPluginInConnectionService s = pluginService.get(node.getType());
159 s.notifyNodeDisconnectFromMaster(node);