Logging related enhancements.
[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.List;
14 import java.util.Properties;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentMap;
17
18 import javax.transaction.HeuristicMixedException;
19 import javax.transaction.HeuristicRollbackException;
20 import javax.transaction.NotSupportedException;
21 import javax.transaction.RollbackException;
22 import javax.transaction.SystemException;
23 import javax.transaction.Transaction;
24 import javax.transaction.TransactionManager;
25
26 import org.opendaylight.controller.clustering.services.CacheConfigException;
27 import org.opendaylight.controller.clustering.services.CacheExistException;
28 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
29 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
30 import org.opendaylight.controller.clustering.services.IClusterServices;
31 import org.opendaylight.controller.clustering.services.IClusterServicesCommon;
32 import org.opendaylight.controller.clustering.services.ICoordinatorChangeAware;
33 import org.opendaylight.controller.clustering.services.IListenRoleChange;
34 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.Dictionary;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import org.apache.felix.dm.Component;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 abstract public class ClusterManagerCommon implements IClusterServicesCommon {
46     protected String containerName = null;
47     private IClusterServices clusterService = null;
48     protected static final Logger logger = LoggerFactory
49             .getLogger(ClusterManagerCommon.class);
50     private Set<ICacheUpdateAware> cacheUpdateAware = Collections
51             .synchronizedSet(new HashSet<ICacheUpdateAware>());
52     private Set<ICoordinatorChangeAware> coordinatorChangeAware = Collections
53             .synchronizedSet(new HashSet<ICoordinatorChangeAware>());
54     private ListenCoordinatorChange coordinatorChangeListener = null;
55
56     /**
57      * Class needed to listen to the role changes from the cluster
58      * manager and to pass it along to the other components that
59      * export the interface ICoordinatorChangeAware
60      */
61     class ListenCoordinatorChange implements IListenRoleChange {
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(ICacheUpdateAware s) {
89         if (this.cacheUpdateAware != null) {
90             this.cacheUpdateAware.add(s);
91         }
92     }
93
94     void unsetCacheUpdateAware(ICacheUpdateAware s) {
95         if (this.cacheUpdateAware != null) {
96             this.cacheUpdateAware.remove(s);
97         }
98     }
99
100     public void setClusterService(IClusterServices s) {
101         this.clusterService = s;
102     }
103
104     public void unsetClusterServices(IClusterServices s) {
105         if (this.clusterService == s) {
106             this.clusterService = null;
107         }
108     }
109
110     /**
111      * Function called by the dependency manager when all the required
112      * dependencies are satisfied
113      *
114      */
115     void init(Component c) {
116         Dictionary props = c.getServiceProperties();
117         if (props != null) {
118             this.containerName = (String) props.get("containerName");
119             logger.debug("Running containerName: {}", this.containerName);
120         } else {
121             // In the Global instance case the containerName is empty
122             this.containerName = "";
123         }
124         if (this.clusterService != null) {
125             this.coordinatorChangeListener = new ListenCoordinatorChange();
126             try {
127                 this.clusterService
128                         .listenRoleChange(this.coordinatorChangeListener);
129                 logger.debug("Coordinator change handler registered");
130             } catch (ListenRoleChangeAddException ex) {
131                 logger.error("Could not register coordinator change");
132             }
133         }
134     }
135
136     /**
137      * Function called by the dependency manager when any of the required
138      * dependencies are going away
139      *
140      */
141     void destroy() {
142         if (this.clusterService != null
143                 && this.coordinatorChangeListener != null) {
144             this.clusterService
145                     .unlistenRoleChange(this.coordinatorChangeListener);
146             this.coordinatorChangeListener = null;
147             logger.debug("Coordinator change handler UNregistered");
148         }
149     }
150
151     @Override
152     public ConcurrentMap<?, ?> createCache(String cacheName,
153             Set<IClusterServices.cacheMode> cMode) throws CacheExistException,
154             CacheConfigException {
155         if (this.clusterService != null) {
156             return this.clusterService.createCache(this.containerName,
157                     cacheName, cMode);
158         } else {
159             return null;
160         }
161     }
162
163     @Override
164     public ConcurrentMap<?, ?> getCache(String cacheName) {
165         if (this.clusterService != null) {
166             return this.clusterService.getCache(this.containerName, cacheName);
167         } else {
168             return null;
169         }
170     }
171
172     @Override
173     public void destroyCache(String cacheName) {
174         if (this.clusterService != null) {
175             this.clusterService.destroyCache(this.containerName, cacheName);
176         }
177     }
178
179     @Override
180     public boolean existCache(String cacheName) {
181         if (this.clusterService != null) {
182             return this.clusterService
183                     .existCache(this.containerName, cacheName);
184         } else {
185             return false;
186         }
187     }
188
189     @Override
190     public Set<String> getCacheList() {
191         if (this.clusterService != null) {
192             return this.clusterService.getCacheList(this.containerName);
193         } else {
194             return null;
195         }
196     }
197
198     @Override
199     public Properties getCacheProperties(String cacheName) {
200         if (this.clusterService != null) {
201             return this.clusterService.getCacheProperties(this.containerName,
202                     cacheName);
203         } else {
204             return null;
205         }
206     }
207
208     @Override
209     public void tbegin() throws NotSupportedException, SystemException {
210         if (this.clusterService != null) {
211             this.clusterService.tbegin();
212         } else {
213             throw new IllegalStateException();
214         }
215     }
216
217     @Override
218     public void tcommit() throws RollbackException, HeuristicMixedException,
219             HeuristicRollbackException, java.lang.SecurityException,
220             java.lang.IllegalStateException, SystemException {
221         if (this.clusterService != null) {
222             this.clusterService.tcommit();
223         } else {
224             throw new IllegalStateException();
225         }
226     }
227
228     @Override
229     public void trollback() throws java.lang.IllegalStateException,
230             java.lang.SecurityException, SystemException {
231         if (this.clusterService != null) {
232             this.clusterService.trollback();
233         } else {
234             throw new IllegalStateException();
235         }
236     }
237
238     @Override
239     public Transaction tgetTransaction() throws SystemException {
240         if (this.clusterService != null) {
241             return this.clusterService.tgetTransaction();
242         } else {
243             return null;
244         }
245     }
246
247     @Override
248     public List<InetAddress> getClusteredControllers() {
249         if (this.clusterService != null) {
250             return this.clusterService.getClusteredControllers();
251         } else {
252             return null;
253         }
254     }
255
256     @Override
257     public InetAddress getMyAddress() {
258         if (this.clusterService != null) {
259             return this.clusterService.getMyAddress();
260         } else {
261             return null;
262         }
263     }
264
265     @Override
266     public InetAddress getCoordinatorAddress() {
267         if (this.clusterService != null) {
268             return this.clusterService.getActiveAddress();
269         } else {
270             return null;
271         }
272     }
273
274     @Override
275     public boolean amICoordinator() {
276         if (this.clusterService != null) {
277             return (!this.clusterService.amIStandby());
278         } else {
279             return false;
280         }
281     }
282 }