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 {
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();
75 void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
76 if (this.coordinatorChangeAware != null) {
77 this.coordinatorChangeAware.add(s);
81 void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
82 if (this.coordinatorChangeAware != null) {
83 this.coordinatorChangeAware.remove(s);
87 void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
88 logger.trace("CacheUpdateAware being set on container:{}",
90 if (this.cacheUpdateAware != null) {
91 Set<String> caches = (Set<String>)props.get("cachenames");
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,
100 GetUpdatesContainer<?, ?> up =
101 new GetUpdatesContainer(s, this.containerName,
105 this.clusterService.addListener(this.containerName,
107 this.cacheUpdateAware.put(cache, up);
108 logger.trace("cachename:{} on container:{} has " +
109 "been registered", cache,
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
124 void unsetCacheUpdateAware(Map props, ICacheUpdateAware s) {
125 logger.trace("CacheUpdateAware being unset on container:{}",
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);
135 this.cacheUpdateAware.remove(cache);
136 this.clusterService.removeListener(this.containerName,
144 public void setClusterService(IClusterServices s) {
145 this.clusterService = s;
148 public void unsetClusterServices(IClusterServices s) {
149 if (this.clusterService == s) {
150 this.clusterService = null;
155 * Function called by the dependency manager when all the required
156 * dependencies are satisfied
159 void init(Component c) {
160 Dictionary props = c.getServiceProperties();
162 this.containerName = (String) props.get("containerName");
163 logger.debug("Running containerName: {}", this.containerName);
165 // In the Global instance case the containerName is empty
166 this.containerName = "";
168 if (this.clusterService != null) {
169 this.coordinatorChangeListener = new ListenCoordinatorChange();
172 .listenRoleChange(this.coordinatorChangeListener);
173 logger.debug("Coordinator change handler registered");
174 } catch (ListenRoleChangeAddException ex) {
175 logger.error("Could not register coordinator change");
181 * Function called by the dependency manager when any of the required
182 * dependencies are going away
186 if (this.clusterService != null
187 && this.coordinatorChangeListener != null) {
189 .unlistenRoleChange(this.coordinatorChangeListener);
190 this.coordinatorChangeListener = null;
191 logger.debug("Coordinator change handler UNregistered");
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,
208 public ConcurrentMap<?, ?> getCache(String cacheName) {
209 if (this.clusterService != null) {
210 return this.clusterService.getCache(this.containerName, cacheName);
217 public void destroyCache(String cacheName) {
218 if (this.clusterService != null) {
219 this.clusterService.destroyCache(this.containerName, cacheName);
224 public boolean existCache(String cacheName) {
225 if (this.clusterService != null) {
226 return this.clusterService
227 .existCache(this.containerName, cacheName);
234 public Set<String> getCacheList() {
235 if (this.clusterService != null) {
236 return this.clusterService.getCacheList(this.containerName);
243 public Properties getCacheProperties(String cacheName) {
244 if (this.clusterService != null) {
245 return this.clusterService.getCacheProperties(this.containerName,
253 public void tbegin() throws NotSupportedException, SystemException {
254 if (this.clusterService != null) {
255 this.clusterService.tbegin();
257 throw new IllegalStateException();
262 public void tbegin(long timeout, TimeUnit unit) throws NotSupportedException, SystemException {
263 if (this.clusterService != null) {
264 this.clusterService.tbegin(timeout, unit);
266 throw new IllegalStateException();
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();
277 throw new IllegalStateException();
282 public void trollback() throws java.lang.IllegalStateException,
283 java.lang.SecurityException, SystemException {
284 if (this.clusterService != null) {
285 this.clusterService.trollback();
287 throw new IllegalStateException();
292 public Transaction tgetTransaction() throws SystemException {
293 if (this.clusterService != null) {
294 return this.clusterService.tgetTransaction();
301 public List<InetAddress> getClusteredControllers() {
302 if (this.clusterService != null) {
303 return this.clusterService.getClusteredControllers();
310 public InetAddress getMyAddress() {
311 if (this.clusterService != null) {
312 return this.clusterService.getMyAddress();
319 public InetAddress getCoordinatorAddress() {
320 if (this.clusterService != null) {
321 return this.clusterService.getActiveAddress();
328 public boolean amICoordinator() {
329 if (this.clusterService != null) {
330 return (!this.clusterService.amIStandby());