3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.clustering.services_implementation.internal;
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;
18 import java.util.Properties;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import java.util.concurrent.TimeUnit;
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;
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;
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;
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
60 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();
76 void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
77 if (this.coordinatorChangeAware != null) {
78 this.coordinatorChangeAware.add(s);
82 void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
83 if (this.coordinatorChangeAware != null) {
84 this.coordinatorChangeAware.remove(s);
88 void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
89 logger.trace("CacheUpdateAware being set on container:{}",
91 if (this.cacheUpdateAware != null) {
92 Set<String> caches = (Set<String>)props.get("cachenames");
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);
99 GetUpdatesContainer<?, ?> up = new GetUpdatesContainer(s, this.containerName, cache);
102 this.clusterService.addListener(this.containerName,
104 this.cacheUpdateAware.put(cache, up);
105 logger.trace("cachename:{} on container:{} has " +
106 "been registered", cache,
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
122 void unsetCacheUpdateAware(Map props, ICacheUpdateAware s) {
123 logger.trace("CacheUpdateAware being unset on container:{}",
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);
133 this.cacheUpdateAware.remove(cache);
134 this.clusterService.removeListener(this.containerName,
142 public void setClusterService(IClusterServices s) {
143 this.clusterService = s;
146 public void unsetClusterServices(IClusterServices s) {
147 if (this.clusterService == s) {
148 this.clusterService = null;
153 * Function called by the dependency manager when all the required
154 * dependencies are satisfied
157 void init(Component c) {
158 Dictionary props = c.getServiceProperties();
160 this.containerName = (String) props.get("containerName");
161 logger.debug("Running containerName: {}", this.containerName);
163 // In the Global instance case the containerName is empty
164 this.containerName = "";
166 if (this.clusterService != null) {
167 this.coordinatorChangeListener = new ListenCoordinatorChange();
170 .listenRoleChange(this.coordinatorChangeListener);
171 logger.debug("Coordinator change handler registered");
172 } catch (ListenRoleChangeAddException ex) {
173 logger.error("Could not register coordinator change");
179 * Function called by the dependency manager when any of the required
180 * dependencies are going away
184 if (this.clusterService != null
185 && this.coordinatorChangeListener != null) {
187 .unlistenRoleChange(this.coordinatorChangeListener);
188 this.coordinatorChangeListener = null;
189 logger.debug("Coordinator change handler UNregistered");
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,
206 public ConcurrentMap<?, ?> getCache(String cacheName) {
207 if (this.clusterService != null) {
208 return this.clusterService.getCache(this.containerName, cacheName);
215 public void destroyCache(String cacheName) {
216 if (this.clusterService != null) {
217 this.clusterService.destroyCache(this.containerName, cacheName);
222 public boolean existCache(String cacheName) {
223 if (this.clusterService != null) {
224 return this.clusterService
225 .existCache(this.containerName, cacheName);
232 public Set<String> getCacheList() {
233 if (this.clusterService != null) {
234 return this.clusterService.getCacheList(this.containerName);
241 public Properties getCacheProperties(String cacheName) {
242 if (this.clusterService != null) {
243 return this.clusterService.getCacheProperties(this.containerName,
251 public void tbegin() throws NotSupportedException, SystemException {
252 if (this.clusterService != null) {
253 this.clusterService.tbegin();
255 throw new IllegalStateException();
260 public void tbegin(long timeout, TimeUnit unit) throws NotSupportedException, SystemException {
261 if (this.clusterService != null) {
262 this.clusterService.tbegin(timeout, unit);
264 throw new IllegalStateException();
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();
275 throw new IllegalStateException();
280 public void trollback() throws java.lang.IllegalStateException,
281 java.lang.SecurityException, SystemException {
282 if (this.clusterService != null) {
283 this.clusterService.trollback();
285 throw new IllegalStateException();
290 public Transaction tgetTransaction() throws SystemException {
291 if (this.clusterService != null) {
292 return this.clusterService.tgetTransaction();
299 public List<InetAddress> getClusteredControllers() {
300 if (this.clusterService != null) {
301 return this.clusterService.getClusteredControllers();
308 public InetAddress getMyAddress() {
309 if (this.clusterService != null) {
310 return this.clusterService.getMyAddress();
317 public InetAddress getCoordinatorAddress() {
318 if (this.clusterService != null) {
319 return this.clusterService.getActiveAddress();
326 public boolean amICoordinator() {
327 if (this.clusterService != null) {
328 return (!this.clusterService.amIStandby());