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