BUG-2470: add trace of failed modification
[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) return ConnectionLocality.NOT_CONNECTED;
107         return connectionListener.getLocalityStatus(node);
108     }
109
110     @Override
111     public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
112         IPluginInConnectionService s = pluginService.get(type);
113         if (s != null) {
114             return s.connect(connectionIdentifier, params);
115         }
116         return null;
117     }
118
119     @Override
120     public Node connect (String connectionIdentifier, Map<ConnectionConstants, String> params) {
121         synchronized (this.pluginService) {
122             for (String pluginType : this.pluginService.keySet()) {
123                 IPluginInConnectionService s = pluginService.get(pluginType);
124                 Node node = s.connect(connectionIdentifier, params);
125                 if (node != null) {
126                     return node;
127                 }
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         IPluginInConnectionService s = pluginService.get(node.getType());
157         if (s != null) {
158              s.notifyNodeDisconnectFromMaster(node);
159         }
160     }
161 }