Merge "Reorder public/private modifiers as per JLS. (fixes sonar warnings)"
[controller.git] / opendaylight / clustering / services_implementation / src / main / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManagerCommon.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.clustering.services_implementation.internal;
11
12 import java.net.InetAddress;
13 import java.util.Collections;
14 import java.util.Dictionary;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Properties;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import javax.transaction.HeuristicMixedException;
23 import javax.transaction.HeuristicRollbackException;
24 import javax.transaction.NotSupportedException;
25 import javax.transaction.RollbackException;
26 import javax.transaction.SystemException;
27 import javax.transaction.Transaction;
28 import javax.transaction.TransactionManager;
29 import org.apache.felix.dm.Component;
30 import org.opendaylight.controller.clustering.services.CacheConfigException;
31 import org.opendaylight.controller.clustering.services.CacheExistException;
32 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
33 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
34 import org.opendaylight.controller.clustering.services.IClusterServices;
35 import org.opendaylight.controller.clustering.services.IClusterServicesCommon;
36 import org.opendaylight.controller.clustering.services.ICoordinatorChangeAware;
37 import org.opendaylight.controller.clustering.services.IListenRoleChange;
38 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public abstract class ClusterManagerCommon implements IClusterServicesCommon {
43     protected String containerName = null;
44     private IClusterServices clusterService = null;
45     protected static final Logger logger = LoggerFactory
46             .getLogger(ClusterManagerCommon.class);
47     private ConcurrentMap<String, GetUpdatesContainer> cacheUpdateAware =
48         new ConcurrentHashMap<String, GetUpdatesContainer>();
49     private Set<ICoordinatorChangeAware> coordinatorChangeAware = Collections
50             .synchronizedSet(new HashSet<ICoordinatorChangeAware>());
51     private ListenCoordinatorChange coordinatorChangeListener = null;
52
53     /**
54      * Class needed to listen to the role changes from the cluster
55      * manager and to pass it along to the other components that
56      * export the interface ICoordinatorChangeAware
57      */
58     class ListenCoordinatorChange implements IListenRoleChange {
59         public void newActiveAvailable() {
60             if (coordinatorChangeAware != null) {
61                 // Make sure to look the set while walking it
62                 synchronized (coordinatorChangeAware) {
63                     for (ICoordinatorChangeAware s : coordinatorChangeAware) {
64                         // Now walk every instance and signal that the
65                         // coordinator has changed
66                         s.coordinatorChanged();
67                     }
68                 }
69             }
70         }
71     }
72
73     void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
74         if (this.coordinatorChangeAware != null) {
75             this.coordinatorChangeAware.add(s);
76         }
77     }
78
79     void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
80         if (this.coordinatorChangeAware != null) {
81             this.coordinatorChangeAware.remove(s);
82         }
83     }
84
85     void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
86         logger.trace("CacheUpdateAware being set on container:{}",
87                      this.containerName);
88         if (this.cacheUpdateAware != null) {
89             Set<String> caches = (Set<String>)props.get("cachenames");
90             if (caches != null) {
91                 logger.trace("cachenames provided below:");
92                 for (String cache : caches) {
93                     if (this.cacheUpdateAware.get(cache) != null) {
94                         logger.error("cachename:{} on container:{} has " +
95                                      "already a listener", cache,
96                                      this.containerName);
97                     } else {
98                         GetUpdatesContainer<?, ?> up =
99                             new GetUpdatesContainer(s, this.containerName,
100                                                     cache);
101                         if (up != null) {
102                             try {
103                                 this.clusterService.addListener(this.containerName,
104                                                                 cache, up);
105                                 this.cacheUpdateAware.put(cache, up);
106                                 logger.trace("cachename:{} on container:{} has " +
107                                              "been registered", cache,
108                                              this.containerName);
109                             } catch (CacheListenerAddException exc) {
110                                 // Do nothing, the important is that
111                                 // we don't register the listener in
112                                 // the shadow, and we are not doing
113                                 // that.
114                             }
115                         }
116                     }
117                 }
118             }
119         }
120     }
121
122     void unsetCacheUpdateAware(Map props, ICacheUpdateAware s) {
123         logger.trace("CacheUpdateAware being unset on container:{}",
124                      this.containerName);
125         if (this.cacheUpdateAware != null) {
126             Set<String> caches = (Set<String>)props.get("cachenames");
127             if (caches != null) {
128                 logger.trace("cachenames provided below:");
129                 GetUpdatesContainer<?, ?> up = null;
130                 for (String cache : caches) {
131                     up = this.cacheUpdateAware.get(cache);
132                     if (up != null) {
133                         this.cacheUpdateAware.remove(cache);
134                         this.clusterService.removeListener(this.containerName,
135                                                            cache, up);
136                     }
137                 }
138             }
139         }
140     }
141
142     public void setClusterService(IClusterServices s) {
143         this.clusterService = s;
144     }
145
146     public void unsetClusterServices(IClusterServices s) {
147         if (this.clusterService == s) {
148             this.clusterService = null;
149         }
150     }
151
152     /**
153      * Function called by the dependency manager when all the required
154      * dependencies are satisfied
155      *
156      */
157     void init(Component c) {
158         Dictionary props = c.getServiceProperties();
159         if (props != null) {
160             this.containerName = (String) props.get("containerName");
161             logger.debug("Running containerName: {}", this.containerName);
162         } else {
163             // In the Global instance case the containerName is empty
164             this.containerName = "";
165         }
166         if (this.clusterService != null) {
167             this.coordinatorChangeListener = new ListenCoordinatorChange();
168             try {
169                 this.clusterService
170                         .listenRoleChange(this.coordinatorChangeListener);
171                 logger.debug("Coordinator change handler registered");
172             } catch (ListenRoleChangeAddException ex) {
173                 logger.error("Could not register coordinator change");
174             }
175         }
176     }
177
178     /**
179      * Function called by the dependency manager when any of the required
180      * dependencies are going away
181      *
182      */
183     void destroy() {
184         if (this.clusterService != null
185                 && this.coordinatorChangeListener != null) {
186             this.clusterService
187                     .unlistenRoleChange(this.coordinatorChangeListener);
188             this.coordinatorChangeListener = null;
189             logger.debug("Coordinator change handler UNregistered");
190         }
191     }
192
193     @Override
194     public ConcurrentMap<?, ?> createCache(String cacheName,
195             Set<IClusterServices.cacheMode> cMode) throws CacheExistException,
196             CacheConfigException {
197         if (this.clusterService != null) {
198             return this.clusterService.createCache(this.containerName,
199                     cacheName, cMode);
200         } else {
201             return null;
202         }
203     }
204
205     @Override
206     public ConcurrentMap<?, ?> getCache(String cacheName) {
207         if (this.clusterService != null) {
208             return this.clusterService.getCache(this.containerName, cacheName);
209         } else {
210             return null;
211         }
212     }
213
214     @Override
215     public void destroyCache(String cacheName) {
216         if (this.clusterService != null) {
217             this.clusterService.destroyCache(this.containerName, cacheName);
218         }
219     }
220
221     @Override
222     public boolean existCache(String cacheName) {
223         if (this.clusterService != null) {
224             return this.clusterService
225                     .existCache(this.containerName, cacheName);
226         } else {
227             return false;
228         }
229     }
230
231     @Override
232     public Set<String> getCacheList() {
233         if (this.clusterService != null) {
234             return this.clusterService.getCacheList(this.containerName);
235         } else {
236             return null;
237         }
238     }
239
240     @Override
241     public Properties getCacheProperties(String cacheName) {
242         if (this.clusterService != null) {
243             return this.clusterService.getCacheProperties(this.containerName,
244                     cacheName);
245         } else {
246             return null;
247         }
248     }
249
250     @Override
251     public void tbegin() throws NotSupportedException, SystemException {
252         if (this.clusterService != null) {
253             this.clusterService.tbegin();
254         } else {
255             throw new IllegalStateException();
256         }
257     }
258
259     @Override
260     public void tcommit() throws RollbackException, HeuristicMixedException,
261             HeuristicRollbackException, java.lang.SecurityException,
262             java.lang.IllegalStateException, SystemException {
263         if (this.clusterService != null) {
264             this.clusterService.tcommit();
265         } else {
266             throw new IllegalStateException();
267         }
268     }
269
270     @Override
271     public void trollback() throws java.lang.IllegalStateException,
272             java.lang.SecurityException, SystemException {
273         if (this.clusterService != null) {
274             this.clusterService.trollback();
275         } else {
276             throw new IllegalStateException();
277         }
278     }
279
280     @Override
281     public Transaction tgetTransaction() throws SystemException {
282         if (this.clusterService != null) {
283             return this.clusterService.tgetTransaction();
284         } else {
285             return null;
286         }
287     }
288
289     @Override
290     public List<InetAddress> getClusteredControllers() {
291         if (this.clusterService != null) {
292             return this.clusterService.getClusteredControllers();
293         } else {
294             return null;
295         }
296     }
297
298     @Override
299     public InetAddress getMyAddress() {
300         if (this.clusterService != null) {
301             return this.clusterService.getMyAddress();
302         } else {
303             return null;
304         }
305     }
306
307     @Override
308     public InetAddress getCoordinatorAddress() {
309         if (this.clusterService != null) {
310             return this.clusterService.getActiveAddress();
311         } else {
312             return null;
313         }
314     }
315
316     @Override
317     public boolean amICoordinator() {
318         if (this.clusterService != null) {
319             return (!this.clusterService.amIStandby());
320         } else {
321             return false;
322         }
323     }
324 }