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 javax.transaction.HeuristicMixedException;
23 import javax.transaction.HeuristicRollbackException;
24 import javax.transaction.NotSupportedException;
25 import javax.transaction.RollbackException;
26 import javax.transaction.SystemException;
27 import javax.transaction.Transaction;
28 import javax.transaction.TransactionManager;
29 import org.apache.felix.dm.Component;
30 import org.opendaylight.controller.clustering.services.CacheConfigException;
31 import org.opendaylight.controller.clustering.services.CacheExistException;
32 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
33 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
34 import org.opendaylight.controller.clustering.services.IClusterServices;
35 import org.opendaylight.controller.clustering.services.IClusterServicesCommon;
36 import org.opendaylight.controller.clustering.services.ICoordinatorChangeAware;
37 import org.opendaylight.controller.clustering.services.IListenRoleChange;
38 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public abstract class ClusterManagerCommon implements IClusterServicesCommon {
43 protected String containerName = null;
44 protected IClusterServices clusterService = null;
45 protected static final Logger logger = LoggerFactory
46 .getLogger(ClusterManagerCommon.class);
47 private ConcurrentMap<String, GetUpdatesContainer> cacheUpdateAware =
48 new ConcurrentHashMap<String, GetUpdatesContainer>();
49 private Set<ICoordinatorChangeAware> coordinatorChangeAware = Collections
50 .synchronizedSet(new HashSet<ICoordinatorChangeAware>());
51 private ListenCoordinatorChange coordinatorChangeListener = null;
54 * Class needed to listen to the role changes from the cluster
55 * manager and to pass it along to the other components that
56 * export the interface ICoordinatorChangeAware
58 class ListenCoordinatorChange implements IListenRoleChange {
59 public void newActiveAvailable() {
60 if (coordinatorChangeAware != null) {
61 // Make sure to look the set while walking it
62 synchronized (coordinatorChangeAware) {
63 for (ICoordinatorChangeAware s : coordinatorChangeAware) {
64 // Now walk every instance and signal that the
65 // coordinator has changed
66 s.coordinatorChanged();
73 void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
74 if (this.coordinatorChangeAware != null) {
75 this.coordinatorChangeAware.add(s);
79 void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
80 if (this.coordinatorChangeAware != null) {
81 this.coordinatorChangeAware.remove(s);
85 void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
86 logger.trace("CacheUpdateAware being set on container:{}",
88 if (this.cacheUpdateAware != null) {
89 Set<String> caches = (Set<String>)props.get("cachenames");
91 logger.trace("cachenames provided below:");
92 for (String cache : caches) {
93 if (this.cacheUpdateAware.get(cache) != null) {
94 logger.error("cachename:{} on container:{} has " +
95 "already a listener", cache,
98 GetUpdatesContainer<?, ?> up =
99 new GetUpdatesContainer(s, this.containerName,
103 this.clusterService.addListener(this.containerName,
105 this.cacheUpdateAware.put(cache, up);
106 logger.trace("cachename:{} on container:{} has " +
107 "been registered", cache,
109 } catch (CacheListenerAddException exc) {
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 tcommit() throws RollbackException, HeuristicMixedException,
261 HeuristicRollbackException, java.lang.SecurityException,
262 java.lang.IllegalStateException, SystemException {
263 if (this.clusterService != null) {
264 this.clusterService.tcommit();
266 throw new IllegalStateException();
271 public void trollback() throws java.lang.IllegalStateException,
272 java.lang.SecurityException, SystemException {
273 if (this.clusterService != null) {
274 this.clusterService.trollback();
276 throw new IllegalStateException();
281 public Transaction tgetTransaction() throws SystemException {
282 if (this.clusterService != null) {
283 return this.clusterService.tgetTransaction();
290 public List<InetAddress> getClusteredControllers() {
291 if (this.clusterService != null) {
292 return this.clusterService.getClusteredControllers();
299 public InetAddress getMyAddress() {
300 if (this.clusterService != null) {
301 return this.clusterService.getMyAddress();
308 public InetAddress getCoordinatorAddress() {
309 if (this.clusterService != null) {
310 return this.clusterService.getActiveAddress();
317 public boolean amICoordinator() {
318 if (this.clusterService != null) {
319 return (!this.clusterService.amIStandby());