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.IConnectionListener;
20 import org.opendaylight.controller.sal.connection.IConnectionService;
21 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
22 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.utils.GlobalConstants;
25 import org.opendaylight.controller.sal.utils.Status;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class ConnectionService implements IPluginOutConnectionService, IConnectionService {
31 protected static final Logger logger = LoggerFactory
32 .getLogger(ConnectionService.class);
33 private IConnectionListener connectionListener;
34 private ConcurrentMap<String, IPluginInConnectionService> pluginService =
35 new ConcurrentHashMap<String, IPluginInConnectionService>();
37 void setPluginService (Map props, IPluginInConnectionService s) {
39 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
40 if (value instanceof String) {
41 type = (String) value;
44 logger.error("Received a PluginInConnectionService without any "
45 + "protocolPluginType provided");
47 this.pluginService.put(type, s);
51 void unsetPluginService(Map props, IPluginInConnectionService s) {
54 Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
55 if (value instanceof String) {
56 type = (String) value;
59 logger.error("Received a PluginInConnectionService without any "
60 + "protocolPluginType provided");
61 } else if (this.pluginService.get(type).equals(s)) {
62 this.pluginService.remove(type);
66 void setListener(IConnectionListener s) {
67 this.connectionListener = s;
70 void unsetListener(IConnectionListener s) {
71 if (this.connectionListener == s) {
72 this.connectionListener = null;
77 * Function called by the dependency manager when all the required
78 * dependencies are satisfied
85 * Function called by the dependency manager when at least one dependency
86 * become unsatisfied or when the component is shutting down because for
87 * example bundle is being stopped.
91 connectionListener = null;
92 if (this.pluginService != null) {
93 this.pluginService.clear();
98 * Method to test if a node is local to a controller.
100 * @return true if node is local to this controller. false otherwise.
102 public boolean isLocal(Node node) {
103 if (this.connectionListener == null) return false;
104 return connectionListener.isLocal(node);
108 public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
109 IPluginInConnectionService s = pluginService.get(type);
110 if (s != null) return s.connect(connectionIdentifier, params);
115 public Node connect (String connectionIdentifier, Map<ConnectionConstants, String> params) {
116 synchronized (this.pluginService) {
117 for (String pluginType : this.pluginService.keySet()) {
118 IPluginInConnectionService s = pluginService.get(pluginType);
119 Node node = s.connect(connectionIdentifier, params);
120 if (node != null) return node;
127 public Status disconnect(Node node) {
128 IPluginInConnectionService s = pluginService.get(node.getType());
129 if (s != null) return s.disconnect(node);
130 return new Status(StatusCode.NOTFOUND);
134 * View Change notification
137 public void notifyClusterViewChanged() {
138 for (String pluginType : this.pluginService.keySet()) {
139 IPluginInConnectionService s = pluginService.get(pluginType);
140 s.notifyClusterViewChanged();
145 * Node Disconnected from the node's master controller.
148 public void notifyNodeDisconnectFromMaster(Node node) {
149 for (String pluginType : this.pluginService.keySet()) {
150 IPluginInConnectionService s = pluginService.get(pluginType);
151 s.notifyNodeDisconnectFromMaster(node);