BUG-509: reorganize data tree abstractions.
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / main / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreManager.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.datastore.internal;
10
11 import java.util.Hashtable;
12
13 import com.google.common.base.Preconditions;
14
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;
25
26 public class ClusteredDataStoreManager implements //
27         ClusteredDataStore, //
28         ServiceTrackerCustomizer<IClusterGlobalServices, IClusterGlobalServices>, //
29         AutoCloseable {
30
31     private ClusteredDataStore clusteredDataStore = null;
32     private IClusterGlobalServices clusterGlobalServices = null;
33     private BundleContext context;
34
35     private ServiceReference<IClusterGlobalServices> firstClusterGlobalReference;
36     private ServiceTracker<IClusterGlobalServices, IClusterGlobalServices> clusterTracker;
37
38     @Override
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);
43     }
44
45     @Override
46     public CompositeNode readOperationalData(InstanceIdentifier path) {
47         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
48         return clusteredDataStore.readOperationalData(path);
49     }
50
51     @Override
52     public CompositeNode readConfigurationData(InstanceIdentifier path) {
53         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
54         return clusteredDataStore.readConfigurationData(path);
55     }
56
57     public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
58         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
59         return clusteredDataStore.getStoredConfigurationPaths();
60     }
61
62     public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
63         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
64         return clusteredDataStore.getStoredOperationalPaths();
65     }
66
67     public boolean containsConfigurationPath(InstanceIdentifier path) {
68         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
69         return clusteredDataStore.containsConfigurationPath(path);
70     }
71
72     public boolean containsOperationalPath(InstanceIdentifier path) {
73         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
74         return clusteredDataStore.containsOperationalPath(path);
75     }
76
77     public void setClusterGlobalServices(IClusterGlobalServices clusterGlobalServices) {
78         this.clusterGlobalServices = clusterGlobalServices;
79         try {
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");
85         }
86     }
87
88     @Override
89     public IClusterGlobalServices addingService(ServiceReference<IClusterGlobalServices> reference) {
90         if (clusterGlobalServices == null) {
91             setClusterGlobalServices(context.getService(reference));
92             return clusterGlobalServices;
93         }
94         return null;
95     }
96
97     @Override
98     public void modifiedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
99
100     }
101
102     @Override
103     public void removedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
104         if (clusterGlobalServices == service) {
105             clusterGlobalServices = null;
106             clusteredDataStore = null;
107         }
108     }
109
110     public BundleContext getContext() {
111         return context;
112     }
113
114     public void setContext(BundleContext context) {
115         this.context = context;
116     }
117     
118     
119     /**
120      * Function called by the dependency manager when all the required
121      * dependencies are satisfied
122      * 
123      */
124     public void start() {
125         if (context != null) {
126             clusterTracker = new ServiceTracker<>(context, IClusterGlobalServices.class, this);
127             clusterTracker.open();
128             
129             context.registerService(ClusteredDataStore.class, this, new Hashtable<String,Object>());
130         }
131     }
132
133     protected ClusteredDataStore createClusteredDataStore() throws CacheConfigException {
134         return new ClusteredDataStoreImpl(clusterGlobalServices);
135     }
136
137     @Override
138     public void close() throws Exception {
139         clusterTracker.close();
140     }
141 }