2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.datastore.internal;
11 import java.util.Hashtable;
13 import com.google.common.base.Preconditions;
15 import org.opendaylight.controller.clustering.services.CacheConfigException;
16 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
17 import org.opendaylight.controller.datastore.ClusteredDataStore;
18 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23 import org.osgi.util.tracker.ServiceTracker;
24 import org.osgi.util.tracker.ServiceTrackerCustomizer;
26 public class ClusteredDataStoreManager implements //
27 ClusteredDataStore, //
28 ServiceTrackerCustomizer<IClusterGlobalServices, IClusterGlobalServices>, //
31 private ClusteredDataStore clusteredDataStore = null;
32 private IClusterGlobalServices clusterGlobalServices = null;
33 private BundleContext context;
35 private ServiceReference<IClusterGlobalServices> firstClusterGlobalReference;
36 private ServiceTracker<IClusterGlobalServices, IClusterGlobalServices> clusterTracker;
39 public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
40 DataModification<InstanceIdentifier, CompositeNode> modification) {
41 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
42 return clusteredDataStore.requestCommit(modification);
46 public CompositeNode readOperationalData(InstanceIdentifier path) {
47 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
48 return clusteredDataStore.readOperationalData(path);
52 public CompositeNode readConfigurationData(InstanceIdentifier path) {
53 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
54 return clusteredDataStore.readConfigurationData(path);
57 public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
58 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
59 return clusteredDataStore.getStoredConfigurationPaths();
62 public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
63 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
64 return clusteredDataStore.getStoredOperationalPaths();
67 public boolean containsConfigurationPath(InstanceIdentifier path) {
68 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
69 return clusteredDataStore.containsConfigurationPath(path);
72 public boolean containsOperationalPath(InstanceIdentifier path) {
73 Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
74 return clusteredDataStore.containsOperationalPath(path);
77 public void setClusterGlobalServices(IClusterGlobalServices clusterGlobalServices) {
78 this.clusterGlobalServices = clusterGlobalServices;
80 // Adding creation of the clustered data store in its own method
81 // to make the method unit testable
82 clusteredDataStore = createClusteredDataStore();
83 } catch (CacheConfigException e) {
84 throw new IllegalStateException("could not construct clusteredDataStore");
89 public IClusterGlobalServices addingService(ServiceReference<IClusterGlobalServices> reference) {
90 if (clusterGlobalServices == null) {
91 setClusterGlobalServices(context.getService(reference));
92 return clusterGlobalServices;
98 public void modifiedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
103 public void removedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
104 if (clusterGlobalServices == service) {
105 clusterGlobalServices = null;
106 clusteredDataStore = null;
110 public BundleContext getContext() {
114 public void setContext(BundleContext context) {
115 this.context = context;
120 * Function called by the dependency manager when all the required
121 * dependencies are satisfied
124 public void start() {
125 if (context != null) {
126 clusterTracker = new ServiceTracker<>(context, IClusterGlobalServices.class, this);
127 clusterTracker.open();
129 context.registerService(ClusteredDataStore.class, this, new Hashtable<String,Object>());
133 protected ClusteredDataStore createClusteredDataStore() throws CacheConfigException {
134 return new ClusteredDataStoreImpl(clusterGlobalServices);
138 public void close() throws Exception {
139 clusterTracker.close();