BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / adsal / 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         @Override
62         public void newActiveAvailable() {
63             if (coordinatorChangeAware != null) {
64                 // Make sure to look the set while walking it
65                 synchronized (coordinatorChangeAware) {
66                     for (ICoordinatorChangeAware s : coordinatorChangeAware) {
67                         // Now walk every instance and signal that the
68                         // coordinator has changed
69                         s.coordinatorChanged();
70                     }
71                 }
72             }
73         }
74     }
75
76     void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
77         if (this.coordinatorChangeAware != null) {
78             this.coordinatorChangeAware.add(s);
79         }
80     }
81
82     void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
83         if (this.coordinatorChangeAware != null) {
84             this.coordinatorChangeAware.remove(s);
85         }
86     }
87
88     void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
89         logger.trace("CacheUpdateAware being set on container:{}",
90                      this.containerName);
91         if (this.cacheUpdateAware != null) {
92             Set<String> caches = (Set<String>)props.get("cachenames");
93             if (caches != null) {
94                 logger.trace("cachenames provided below:");
95                 for (String cache : caches) {
96                     if (this.cacheUpdateAware.get(cache) != null) {
97                         logger.error("cachename:{} on container:{} has already a listener", cache, this.containerName);
98                     } else {
99                         GetUpdatesContainer<?, ?> up = new GetUpdatesContainer(s, this.containerName, 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                                 logger.debug("Cache {} didn't exist when {} tried to register to its updates", cache, s);
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 tbegin(long timeout, TimeUnit unit) throws NotSupportedException, SystemException {
261         if (this.clusterService != null) {
262             this.clusterService.tbegin(timeout, unit);
263         } else {
264             throw new IllegalStateException();
265         }
266     }
267
268     @Override
269     public void tcommit() throws RollbackException, HeuristicMixedException,
270             HeuristicRollbackException, java.lang.SecurityException,
271             java.lang.IllegalStateException, SystemException {
272         if (this.clusterService != null) {
273             this.clusterService.tcommit();
274         } else {
275             throw new IllegalStateException();
276         }
277     }
278
279     @Override
280     public void trollback() throws java.lang.IllegalStateException,
281             java.lang.SecurityException, SystemException {
282         if (this.clusterService != null) {
283             this.clusterService.trollback();
284         } else {
285             throw new IllegalStateException();
286         }
287     }
288
289     @Override
290     public Transaction tgetTransaction() throws SystemException {
291         if (this.clusterService != null) {
292             return this.clusterService.tgetTransaction();
293         } else {
294             return null;
295         }
296     }
297
298     @Override
299     public List<InetAddress> getClusteredControllers() {
300         if (this.clusterService != null) {
301             return this.clusterService.getClusteredControllers();
302         } else {
303             return null;
304         }
305     }
306
307     @Override
308     public InetAddress getMyAddress() {
309         if (this.clusterService != null) {
310             return this.clusterService.getMyAddress();
311         } else {
312             return null;
313         }
314     }
315
316     @Override
317     public InetAddress getCoordinatorAddress() {
318         if (this.clusterService != null) {
319             return this.clusterService.getActiveAddress();
320         } else {
321             return null;
322         }
323     }
324
325     @Override
326     public boolean amICoordinator() {
327         if (this.clusterService != null) {
328             return (!this.clusterService.amIStandby());
329         } else {
330             return false;
331         }
332     }
333 }