Merge "Implement basic ShardTransactionChain#CloseTransactionChain"
[controller.git] / opendaylight / sal / connection / implementation / src / main / java / org / opendaylight / controller / sal / connection / implementation / internal / ConnectionService.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.connection.implementation.internal;
10
11 import java.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14
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;
27
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>();
34
35     void setPluginService (Map props, IPluginInConnectionService s) {
36         String type = null;
37         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
38         if (value instanceof String) {
39             type = (String) value;
40         }
41         if (type == null) {
42             logger.error("Received a PluginInConnectionService without any "
43                     + "protocolPluginType provided");
44         } else {
45             this.pluginService.put(type, s);
46         }
47     }
48
49     void unsetPluginService(Map props, IPluginInConnectionService s) {
50         String type = null;
51
52         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
53         if (value instanceof String) {
54             type = (String) value;
55         }
56         if (type == null) {
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);
61         }
62     }
63
64     void setListener(IConnectionListener s) {
65         this.connectionListener = s;
66     }
67
68     void unsetListener(IConnectionListener s) {
69         if (this.connectionListener == s) {
70             this.connectionListener = null;
71         }
72     }
73
74     /**
75      * Function called by the dependency manager when all the required
76      * dependencies are satisfied
77      *
78      */
79     void init() {
80     }
81
82     /**
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.
86      *
87      */
88     void destroy() {
89         connectionListener = null;
90         if (this.pluginService != null) {
91             this.pluginService.clear();
92         }
93     }
94
95     /**
96      * Method to test if a node is local to a controller.
97      *
98      * @return true if node is local to this controller. false otherwise.
99      */
100     public boolean isLocal(Node node) {
101         if (this.connectionListener == null) return false;
102         return connectionListener.isLocal(node);
103     }
104
105     @Override
106     public ConnectionLocality getLocalityStatus(Node node) {
107         if (this.connectionListener == null) return ConnectionLocality.NOT_CONNECTED;
108         return connectionListener.getLocalityStatus(node);
109     }
110
111     @Override
112     public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
113         IPluginInConnectionService s = pluginService.get(type);
114         if (s != null) {
115             return s.connect(connectionIdentifier, params);
116         }
117         return null;
118     }
119
120     @Override
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);
126                 if (node != null) {
127                     return node;
128                 }
129             }
130         }
131         return null;
132     }
133
134     @Override
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);
139     }
140
141     /**
142      * View Change notification
143      */
144     @Override
145     public void notifyClusterViewChanged() {
146         for (String pluginType : this.pluginService.keySet()) {
147             IPluginInConnectionService s = pluginService.get(pluginType);
148             s.notifyClusterViewChanged();
149         }
150     }
151
152     /**
153      * Node Disconnected from the node's master controller.
154      */
155     @Override
156     public void notifyNodeDisconnectFromMaster(Node node) {
157         IPluginInConnectionService s = pluginService.get(node.getType());
158         if (s != null) {
159              s.notifyNodeDisconnectFromMaster(node);
160         }
161     }
162 }