Merge "To SAL conversion test."
[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.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.Collections;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17
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;
30
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>();
37
38     void setPluginService (Map props, IPluginInConnectionService s) {
39         String type = null;
40         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
41         if (value instanceof String) {
42             type = (String) value;
43         }
44         if (type == null) {
45             logger.error("Received a PluginInConnectionService without any "
46                     + "protocolPluginType provided");
47         } else {
48             this.pluginService.put(type, s);
49         }
50     }
51
52     void unsetPluginService(Map props, IPluginInConnectionService s) {
53         String type = null;
54
55         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
56         if (value instanceof String) {
57             type = (String) value;
58         }
59         if (type == null) {
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);
64         }
65     }
66
67     void setListener(IConnectionListener s) {
68         this.connectionListener = s;
69     }
70
71     void unsetListener(IConnectionListener s) {
72         if (this.connectionListener == s) {
73             this.connectionListener = null;
74         }
75     }
76
77     /**
78      * Function called by the dependency manager when all the required
79      * dependencies are satisfied
80      *
81      */
82     void init() {
83     }
84
85     /**
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.
89      *
90      */
91     void destroy() {
92         connectionListener = null;
93         if (this.pluginService != null) {
94             this.pluginService.clear();
95         }
96     }
97
98     /**
99      * Method to test if a node is local to a controller.
100      *
101      * @return true if node is local to this controller. false otherwise.
102      */
103     public boolean isLocal(Node node) {
104         if (this.connectionListener == null) return false;
105         return connectionListener.isLocal(node);
106     }
107
108     @Override
109     public ConnectionLocality getLocalityStatus(Node node) {
110         if (this.connectionListener == null) return ConnectionLocality.NOT_CONNECTED;
111         return connectionListener.getLocalityStatus(node);
112     }
113
114     @Override
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);
118         return null;
119     }
120
121     @Override
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;
128             }
129         }
130         return null;
131     }
132
133     @Override
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);
138     }
139
140     /**
141      * View Change notification
142      */
143     @Override
144     public void notifyClusterViewChanged() {
145         for (String pluginType : this.pluginService.keySet()) {
146             IPluginInConnectionService s = pluginService.get(pluginType);
147             s.notifyClusterViewChanged();
148         }
149     }
150
151     /**
152      * Node Disconnected from the node's master controller.
153      */
154     @Override
155     public void notifyNodeDisconnectFromMaster(Node node) {
156         for (String pluginType : this.pluginService.keySet()) {
157             IPluginInConnectionService s = pluginService.get(pluginType);
158             s.notifyNodeDisconnectFromMaster(node);
159         }
160     }
161 }