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 org.apache.felix.dm.Component;
29 import org.opendaylight.controller.clustering.services.CacheConfigException;
30 import org.opendaylight.controller.clustering.services.CacheExistException;
31 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
32 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
33 import org.opendaylight.controller.clustering.services.IClusterServices;
34 import org.opendaylight.controller.clustering.services.IClusterServicesCommon;
35 import org.opendaylight.controller.clustering.services.ICoordinatorChangeAware;
36 import org.opendaylight.controller.clustering.services.IListenRoleChange;
37 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public abstract class ClusterManagerCommon implements IClusterServicesCommon {
42 protected String containerName = null;
43 protected IClusterServices clusterService = null;
44 protected static final Logger logger = LoggerFactory
45 .getLogger(ClusterManagerCommon.class);
46 private ConcurrentMap<String, GetUpdatesContainer> cacheUpdateAware =
47 new ConcurrentHashMap<String, GetUpdatesContainer>();
48 private Set<ICoordinatorChangeAware> coordinatorChangeAware = Collections
49 .synchronizedSet(new HashSet<ICoordinatorChangeAware>());
50 private ListenCoordinatorChange coordinatorChangeListener = null;
53 * Class needed to listen to the role changes from the cluster
54 * manager and to pass it along to the other components that
55 * export the interface ICoordinatorChangeAware
57 class ListenCoordinatorChange implements IListenRoleChange {
58 public void newActiveAvailable() {
59 if (coordinatorChangeAware != null) {
60 // Make sure to look the set while walking it
61 synchronized (coordinatorChangeAware) {
62 for (ICoordinatorChangeAware s : coordinatorChangeAware) {
63 // Now walk every instance and signal that the
64 // coordinator has changed
65 s.coordinatorChanged();
72 void setCoordinatorChangeAware(ICoordinatorChangeAware s) {
73 if (this.coordinatorChangeAware != null) {
74 this.coordinatorChangeAware.add(s);
78 void unsetCoordinatorChangeAware(ICoordinatorChangeAware s) {
79 if (this.coordinatorChangeAware != null) {
80 this.coordinatorChangeAware.remove(s);
84 void setCacheUpdateAware(Map props, ICacheUpdateAware s) {
85 logger.trace("CacheUpdateAware being set on container:{}",
87 if (this.cacheUpdateAware != null) {
88 Set<String> caches = (Set<String>)props.get("cachenames");
90 logger.trace("cachenames provided below:");
91 for (String cache : caches) {
92 if (this.cacheUpdateAware.get(cache) != null) {
93 logger.error("cachename:{} on container:{} has " +
94 "already a listener", cache,
97 GetUpdatesContainer<?, ?> up =
98 new GetUpdatesContainer(s, this.containerName,
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 // Do nothing, the important is that
110 // we don't register the listener in
111 // the shadow, and we are not doing
121 void unsetCacheUpdateAware(Map props, ICacheUpdateAware s) {
122 logger.trace("CacheUpdateAware being unset on container:{}",
124 if (this.cacheUpdateAware != null) {
125 Set<String> caches = (Set<String>)props.get("cachenames");
126 if (caches != null) {
127 logger.trace("cachenames provided below:");
128 GetUpdatesContainer<?, ?> up = null;
129 for (String cache : caches) {
130 up = this.cacheUpdateAware.get(cache);
132 this.cacheUpdateAware.remove(cache);
133 this.clusterService.removeListener(this.containerName,
141 public void setClusterService(IClusterServices s) {
142 this.clusterService = s;
145 public void unsetClusterServices(IClusterServices s) {
146 if (this.clusterService == s) {
147 this.clusterService = null;
152 * Function called by the dependency manager when all the required
153 * dependencies are satisfied
156 void init(Component c) {
157 Dictionary props = c.getServiceProperties();
159 this.containerName = (String) props.get("containerName");
160 logger.debug("Running containerName: {}", this.containerName);
162 // In the Global instance case the containerName is empty
163 this.containerName = "";
165 if (this.clusterService != null) {
166 this.coordinatorChangeListener = new ListenCoordinatorChange();
169 .listenRoleChange(this.coordinatorChangeListener);
170 logger.debug("Coordinator change handler registered");
171 } catch (ListenRoleChangeAddException ex) {
172 logger.error("Could not register coordinator change");
178 * Function called by the dependency manager when any of the required
179 * dependencies are going away
183 if (this.clusterService != null
184 && this.coordinatorChangeListener != null) {
186 .unlistenRoleChange(this.coordinatorChangeListener);
187 this.coordinatorChangeListener = null;
188 logger.debug("Coordinator change handler UNregistered");
193 public ConcurrentMap<?, ?> createCache(String cacheName,
194 Set<IClusterServices.cacheMode> cMode) throws CacheExistException,
195 CacheConfigException {
196 if (this.clusterService != null) {
197 return this.clusterService.createCache(this.containerName,
205 public ConcurrentMap<?, ?> getCache(String cacheName) {
206 if (this.clusterService != null) {
207 return this.clusterService.getCache(this.containerName, cacheName);
214 public void destroyCache(String cacheName) {
215 if (this.clusterService != null) {
216 this.clusterService.destroyCache(this.containerName, cacheName);
221 public boolean existCache(String cacheName) {
222 if (this.clusterService != null) {
223 return this.clusterService
224 .existCache(this.containerName, cacheName);
231 public Set<String> getCacheList() {
232 if (this.clusterService != null) {
233 return this.clusterService.getCacheList(this.containerName);
240 public Properties getCacheProperties(String cacheName) {
241 if (this.clusterService != null) {
242 return this.clusterService.getCacheProperties(this.containerName,
250 public void tbegin() throws NotSupportedException, SystemException {
251 if (this.clusterService != null) {
252 this.clusterService.tbegin();
254 throw new IllegalStateException();
259 public void tcommit() throws RollbackException, HeuristicMixedException,
260 HeuristicRollbackException, java.lang.SecurityException,
261 java.lang.IllegalStateException, SystemException {
262 if (this.clusterService != null) {
263 this.clusterService.tcommit();
265 throw new IllegalStateException();
270 public void trollback() throws java.lang.IllegalStateException,
271 java.lang.SecurityException, SystemException {
272 if (this.clusterService != null) {
273 this.clusterService.trollback();
275 throw new IllegalStateException();
280 public Transaction tgetTransaction() throws SystemException {
281 if (this.clusterService != null) {
282 return this.clusterService.tgetTransaction();
289 public List<InetAddress> getClusteredControllers() {
290 if (this.clusterService != null) {
291 return this.clusterService.getClusteredControllers();
298 public InetAddress getMyAddress() {
299 if (this.clusterService != null) {
300 return this.clusterService.getMyAddress();
307 public InetAddress getCoordinatorAddress() {
308 if (this.clusterService != null) {
309 return this.clusterService.getActiveAddress();
316 public boolean amICoordinator() {
317 if (this.clusterService != null) {
318 return (!this.clusterService.amIStandby());