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