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.List;
14 import java.util.Properties;
16 import java.util.concurrent.ConcurrentMap;
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;
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;
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;
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;
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
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();
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(ICacheUpdateAware s) {
89 if (this.cacheUpdateAware != null) {
90 this.cacheUpdateAware.add(s);
94 void unsetCacheUpdateAware(ICacheUpdateAware s) {
95 if (this.cacheUpdateAware != null) {
96 this.cacheUpdateAware.remove(s);
100 public void setClusterService(IClusterServices s) {
101 this.clusterService = s;
104 public void unsetClusterServices(IClusterServices s) {
105 if (this.clusterService == s) {
106 this.clusterService = null;
111 * Function called by the dependency manager when all the required
112 * dependencies are satisfied
115 void init(Component c) {
116 Dictionary props = c.getServiceProperties();
118 this.containerName = (String) props.get("containerName");
119 logger.debug("Running containerName:" + this.containerName);
121 // In the Global instance case the containerName is empty
122 this.containerName = "";
124 if (this.clusterService != null) {
125 this.coordinatorChangeListener = new ListenCoordinatorChange();
128 .listenRoleChange(this.coordinatorChangeListener);
129 logger.debug("Coordinator change handler registered");
130 } catch (ListenRoleChangeAddException ex) {
131 logger.error("Could not register coordinator change");
137 * Function called by the dependency manager when any of the required
138 * dependencies are going away
142 if (this.clusterService != null
143 && this.coordinatorChangeListener != null) {
145 .unlistenRoleChange(this.coordinatorChangeListener);
146 this.coordinatorChangeListener = null;
147 logger.debug("Coordinator change handler UNregistered");
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,
164 public ConcurrentMap<?, ?> getCache(String cacheName) {
165 if (this.clusterService != null) {
166 return this.clusterService.getCache(this.containerName, cacheName);
173 public void destroyCache(String cacheName) {
174 if (this.clusterService != null) {
175 this.clusterService.destroyCache(this.containerName, cacheName);
180 public boolean existCache(String cacheName) {
181 if (this.clusterService != null) {
182 return this.clusterService
183 .existCache(this.containerName, cacheName);
190 public Set<String> getCacheList() {
191 if (this.clusterService != null) {
192 return this.clusterService.getCacheList(this.containerName);
199 public Properties getCacheProperties(String cacheName) {
200 if (this.clusterService != null) {
201 return this.clusterService.getCacheProperties(this.containerName,
209 public void tbegin() throws NotSupportedException, SystemException {
210 if (this.clusterService != null) {
211 this.clusterService.tbegin();
213 throw new IllegalStateException();
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();
224 throw new IllegalStateException();
229 public void trollback() throws java.lang.IllegalStateException,
230 java.lang.SecurityException, SystemException {
231 if (this.clusterService != null) {
232 this.clusterService.trollback();
234 throw new IllegalStateException();
239 public Transaction tgetTransaction() throws SystemException {
240 if (this.clusterService != null) {
241 return this.clusterService.tgetTransaction();
248 public List<InetAddress> getClusteredControllers() {
249 if (this.clusterService != null) {
250 return this.clusterService.getClusteredControllers();
257 public InetAddress getMyAddress() {
258 if (this.clusterService != null) {
259 return this.clusterService.getMyAddress();
266 public InetAddress getCoordinatorAddress() {
267 if (this.clusterService != null) {
268 return this.clusterService.getActiveAddress();
275 public boolean amICoordinator() {
276 if (this.clusterService != null) {
277 return (!this.clusterService.amIStandby());