Merge "BUG-2288: DOMNotification API"
[controller.git] / opendaylight / adsal / 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 import org.opendaylight.controller.sal.connection.ConnectionConstants;
15 import org.opendaylight.controller.sal.connection.ConnectionLocality;
16 import org.opendaylight.controller.sal.connection.IConnectionListener;
17 import org.opendaylight.controller.sal.connection.IConnectionService;
18 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
19 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.utils.GlobalConstants;
22 import org.opendaylight.controller.sal.utils.Status;
23 import org.opendaylight.controller.sal.utils.StatusCode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class ConnectionService implements IPluginOutConnectionService, IConnectionService {
28     protected static final Logger logger = LoggerFactory
29             .getLogger(ConnectionService.class);
30     private IConnectionListener connectionListener;
31     private ConcurrentMap<String, IPluginInConnectionService> pluginService =
32             new ConcurrentHashMap<String, IPluginInConnectionService>();
33
34     void setPluginService (Map<?, ?> props, IPluginInConnectionService s) {
35         String type = null;
36         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
37         if (value instanceof String) {
38             type = (String) value;
39         }
40         if (type == null) {
41             logger.error("Received a PluginInConnectionService without any "
42                     + "protocolPluginType provided");
43         } else {
44             this.pluginService.put(type, s);
45         }
46     }
47
48     void unsetPluginService(Map<?, ?> props, IPluginInConnectionService s) {
49         String type = null;
50
51         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
52         if (value instanceof String) {
53             type = (String) value;
54         }
55         if (type == null) {
56             logger.error("Received a PluginInConnectionService without any "
57                     + "protocolPluginType provided");
58         } else if (this.pluginService.get(type).equals(s)) {
59             this.pluginService.remove(type);
60         }
61     }
62
63     void setListener(IConnectionListener s) {
64         this.connectionListener = s;
65     }
66
67     void unsetListener(IConnectionListener s) {
68         if (this.connectionListener == s) {
69             this.connectionListener = null;
70         }
71     }
72
73     /**
74      * Function called by the dependency manager when all the required
75      * dependencies are satisfied
76      *
77      */
78     void init() {
79     }
80
81     /**
82      * Function called by the dependency manager when at least one dependency
83      * become unsatisfied or when the component is shutting down because for
84      * example bundle is being stopped.
85      *
86      */
87     void destroy() {
88         connectionListener = null;
89         if (this.pluginService != null) {
90             this.pluginService.clear();
91         }
92     }
93
94     /**
95      * Method to test if a node is local to a controller.
96      *
97      * @return true if node is local to this controller. false otherwise.
98      */
99     public boolean isLocal(Node node) {
100         if (this.connectionListener == null) return false;
101         return connectionListener.isLocal(node);
102     }
103
104     @Override
105     public ConnectionLocality getLocalityStatus(Node node) {
106         if (this.connectionListener == null) {
107             return ConnectionLocality.NOT_CONNECTED;
108         }
109         return connectionListener.getLocalityStatus(node);
110     }
111
112     @Override
113     public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
114         IPluginInConnectionService s = pluginService.get(type);
115         if (s != null) {
116             return s.connect(connectionIdentifier, params);
117         }
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) {
128                     return node;
129                 }
130             }
131         }
132         return null;
133     }
134
135     @Override
136     public Status disconnect(Node node) {
137         IPluginInConnectionService s = pluginService.get(node.getType());
138         if (s != null) {
139             return s.disconnect(node);
140         }
141         return new Status(StatusCode.NOTFOUND);
142     }
143
144     /**
145      * View Change notification
146      */
147     @Override
148     public void notifyClusterViewChanged() {
149         for (String pluginType : this.pluginService.keySet()) {
150             IPluginInConnectionService s = pluginService.get(pluginType);
151             s.notifyClusterViewChanged();
152         }
153     }
154
155     /**
156      * Node Disconnected from the node's master controller.
157      */
158     @Override
159     public void notifyNodeDisconnectFromMaster(Node node) {
160         IPluginInConnectionService s = pluginService.get(node.getType());
161         if (s != null) {
162              s.notifyNodeDisconnectFromMaster(node);
163         }
164     }
165 }