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;
11 import java.util.HashSet;
14 import java.util.Collections;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
18 import org.opendaylight.controller.sal.connection.ConnectionConstants;
19 import org.opendaylight.controller.sal.connection.ConnectionLocality;
20 import org.opendaylight.controller.sal.connection.IConnectionListener;
21 import org.opendaylight.controller.sal.connection.IConnectionService;
22 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
23 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.utils.GlobalConstants;
26 import org.opendaylight.controller.sal.utils.Status;
27 import org.opendaylight.controller.sal.utils.StatusCode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class ConnectionService implements IPluginOutConnectionService, IConnectionService {
32 protected static final Logger logger = LoggerFactory
33 .getLogger(ConnectionService.class);
34 private IConnectionListener connectionListener;
35 private ConcurrentMap<String, IPluginInConnectionService> pluginService =
36 new ConcurrentHashMap<String, IPluginInConnectionService>();
38 void setPluginService (Map props, IPluginInConnectionService s) {
40 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
41 if (value instanceof String) {
42 type = (String) value;
45 logger.error("Received a PluginInConnectionService without any "
46 + "protocolPluginType provided");
48 this.pluginService.put(type, s);
52 void unsetPluginService(Map props, IPluginInConnectionService s) {
55 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
56 if (value instanceof String) {
57 type = (String) value;
60 logger.error("Received a PluginInConnectionService without any "
61 + "protocolPluginType provided");
62 } else if (this.pluginService.get(type).equals(s)) {
63 this.pluginService.remove(type);
67 void setListener(IConnectionListener s) {
68 this.connectionListener = s;
71 void unsetListener(IConnectionListener s) {
72 if (this.connectionListener == s) {
73 this.connectionListener = null;
78 * Function called by the dependency manager when all the required
79 * dependencies are satisfied
86 * Function called by the dependency manager when at least one dependency
87 * become unsatisfied or when the component is shutting down because for
88 * example bundle is being stopped.
92 connectionListener = null;
93 if (this.pluginService != null) {
94 this.pluginService.clear();
99 * Method to test if a node is local to a controller.
101 * @return true if node is local to this controller. false otherwise.
103 public boolean isLocal(Node node) {
104 if (this.connectionListener == null) return false;
105 return connectionListener.isLocal(node);
109 public ConnectionLocality getLocalityStatus(Node node) {
110 if (this.connectionListener == null) return ConnectionLocality.NOT_CONNECTED;
111 return connectionListener.getLocalityStatus(node);
115 public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
116 IPluginInConnectionService s = pluginService.get(type);
117 if (s != null) return s.connect(connectionIdentifier, params);
122 public Node connect (String connectionIdentifier, Map<ConnectionConstants, String> params) {
123 synchronized (this.pluginService) {
124 for (String pluginType : this.pluginService.keySet()) {
125 IPluginInConnectionService s = pluginService.get(pluginType);
126 Node node = s.connect(connectionIdentifier, params);
127 if (node != null) return node;
134 public Status disconnect(Node node) {
135 IPluginInConnectionService s = pluginService.get(node.getType());
136 if (s != null) return s.disconnect(node);
137 return new Status(StatusCode.NOTFOUND);
141 * View Change notification
144 public void notifyClusterViewChanged() {
145 for (String pluginType : this.pluginService.keySet()) {
146 IPluginInConnectionService s = pluginService.get(pluginType);
147 s.notifyClusterViewChanged();
152 * Node Disconnected from the node's master controller.
155 public void notifyNodeDisconnectFromMaster(Node node) {
156 for (String pluginType : this.pluginService.keySet()) {
157 IPluginInConnectionService s = pluginService.get(pluginType);
158 s.notifyNodeDisconnectFromMaster(node);