Wired ClusteredDataStore with Configuration Subsytem
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / main / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreManager.java
index e6acdb0c3be0a0221c63a014bf11d7bcaeeb00ce..b0a099ff90e0e26f1fd408f6574c718529673590 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-
-
 package org.opendaylight.controller.datastore.internal;
 
+import java.util.Hashtable;
+
 import com.google.common.base.Preconditions;
-import org.apache.felix.dm.Component;
+
 import org.opendaylight.controller.clustering.services.CacheConfigException;
-import org.opendaylight.controller.clustering.services.CacheExistException;
 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
 import org.opendaylight.controller.datastore.ClusteredDataStore;
 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-public class ClusteredDataStoreManager implements ClusteredDataStore {
+public class ClusteredDataStoreManager implements //
+        ClusteredDataStore, //
+        ServiceTrackerCustomizer<IClusterGlobalServices, IClusterGlobalServices>, //
+        AutoCloseable {
 
-    private ClusteredDataStoreImpl clusteredDataStore = null;
+    private ClusteredDataStore clusteredDataStore = null;
     private IClusterGlobalServices clusterGlobalServices = null;
+    private BundleContext context;
+
+    private ServiceReference<IClusterGlobalServices> firstClusterGlobalReference;
+    private ServiceTracker<IClusterGlobalServices, IClusterGlobalServices> clusterTracker;
 
     @Override
-    public DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> requestCommit(DataModification<InstanceIdentifier<? extends Object>, Object> modification) {
+    public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
+            DataModification<InstanceIdentifier, CompositeNode> modification) {
         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
         return clusteredDataStore.requestCommit(modification);
     }
 
     @Override
-    public Object readOperationalData(InstanceIdentifier<? extends Object> path) {
+    public CompositeNode readOperationalData(InstanceIdentifier path) {
         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
         return clusteredDataStore.readOperationalData(path);
     }
 
     @Override
-    public Object readConfigurationData(InstanceIdentifier<? extends Object> path) {
+    public CompositeNode readConfigurationData(InstanceIdentifier path) {
         Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
         return clusteredDataStore.readConfigurationData(path);
     }
 
+    public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
+        Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
+        return clusteredDataStore.getStoredConfigurationPaths();
+    }
+
+    public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
+        Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
+        return clusteredDataStore.getStoredOperationalPaths();
+    }
 
-    public void setClusterGlobalServices(IClusterGlobalServices clusterGlobalServices){
+    public boolean containsConfigurationPath(InstanceIdentifier path) {
+        Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
+        return clusteredDataStore.containsConfigurationPath(path);
+    }
+
+    public boolean containsOperationalPath(InstanceIdentifier path) {
+        Preconditions.checkState(clusteredDataStore != null, "clusteredDataStore cannot be null");
+        return clusteredDataStore.containsOperationalPath(path);
+    }
+
+    public void setClusterGlobalServices(IClusterGlobalServices clusterGlobalServices) {
         this.clusterGlobalServices = clusterGlobalServices;
+        try {
+            // Adding creation of the clustered data store in its own method
+            // to make the method unit testable
+            clusteredDataStore = createClusteredDataStore();
+        } catch (CacheConfigException e) {
+            throw new IllegalStateException("could not construct clusteredDataStore");
+        }
     }
 
-    public void unsetClusterGlobalServices(IClusterGlobalServices clusterGlobalServices){
-        this.clusterGlobalServices = null;
-        this.clusteredDataStore = null;
+    @Override
+    public IClusterGlobalServices addingService(ServiceReference<IClusterGlobalServices> reference) {
+        if (clusterGlobalServices == null) {
+            setClusterGlobalServices(context.getService(reference));
+            return clusterGlobalServices;
+        }
+        return null;
+    }
+
+    @Override
+    public void modifiedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
+
+    }
+
+    @Override
+    public void removedService(ServiceReference<IClusterGlobalServices> reference, IClusterGlobalServices service) {
+        if (clusterGlobalServices == service) {
+            clusterGlobalServices = null;
+            clusteredDataStore = null;
+        }
     }
 
+    public BundleContext getContext() {
+        return context;
+    }
 
+    public void setContext(BundleContext context) {
+        this.context = context;
+    }
+    
+    
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
-     *
+     * 
      */
-    void init(Component c) {
-        try {
-               //Adding creation of the clustered data store in its own method to make the method unit testable
-            clusteredDataStore = createClusteredDataStore(c);
-        } catch (CacheExistException e) {
-            throw new IllegalStateException("could not construct clusteredDataStore");
-        } catch (CacheConfigException e) {
-            throw new IllegalStateException("could not construct clusteredDataStore");
+    public void start() {
+        if (context != null) {
+            clusterTracker = new ServiceTracker<>(context, IClusterGlobalServices.class, this);
+            clusterTracker.open();
+            
+            context.registerService(ClusteredDataStore.class, this, new Hashtable<String,Object>());
         }
     }
-    protected ClusteredDataStoreImpl createClusteredDataStore(Component c) throws CacheExistException,CacheConfigException{
-       return  new ClusteredDataStoreImpl(clusterGlobalServices);
+
+    protected ClusteredDataStore createClusteredDataStore() throws CacheConfigException {
+        return new ClusteredDataStoreImpl(clusterGlobalServices);
+    }
+
+    @Override
+    public void close() throws Exception {
+        clusterTracker.close();
     }
 }