Split out AbstractBuilder and rename it 13/36613/6
authorRobert Varga <rovarga@cisco.com>
Wed, 23 Mar 2016 15:06:13 +0000 (16:06 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Tue, 29 Mar 2016 22:08:30 +0000 (22:08 +0000)
This is a DTO-like class, not really a Builder with .build() method. Split it out
of its enclosing class and rename it to AbstractShardManagerCreator.

Change-Id: I891fc685c2d9776384294a708336b32eb34011b5
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStore.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManager.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerTest.java

index 3dc4562acbf4e99b2e05ddea8a7013aa270ee668..198c3b2edaa137e6a2b6c5d32d14a1b00798106b 100644 (file)
@@ -20,7 +20,7 @@ import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIde
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreInfoMXBeanImpl;
 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
-import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManager;
+import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerCreator;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
@@ -86,11 +86,11 @@ public class DistributedDataStore implements DOMStore, SchemaContextListener,
 
         PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
 
-        ShardManager.Builder builder = ShardManager.builder().cluster(cluster).configuration(configuration).
+        ShardManagerCreator creator = new ShardManagerCreator().cluster(cluster).configuration(configuration).
                 datastoreContextFactory(datastoreContextFactory).waitTillReadyCountdownLatch(waitTillReadyCountDownLatch).
                 primaryShardInfoCache(primaryShardInfoCache).restoreFromSnapshot(restoreFromSnapshot);
 
-        actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, builder, shardDispatcher,
+        actorContext = new ActorContext(actorSystem, createShardManager(actorSystem, creator, shardDispatcher,
                 shardManagerId), cluster, configuration, datastoreContextFactory.getBaseDatastoreContext(), primaryShardInfoCache);
 
         this.waitTillReadyTimeInMillis =
@@ -233,13 +233,13 @@ public class DistributedDataStore implements DOMStore, SchemaContextListener,
         }
     }
 
-    private static ActorRef createShardManager(ActorSystem actorSystem, ShardManager.Builder builder,
+    private static ActorRef createShardManager(ActorSystem actorSystem, ShardManagerCreator creator,
             String shardDispatcher, String shardManagerId) {
         Exception lastException = null;
 
         for(int i=0;i<100;i++) {
             try {
-                return actorSystem.actorOf(builder.props().withDispatcher(shardDispatcher).withMailbox(
+                return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher).withMailbox(
                         ActorContext.BOUNDED_MAILBOX), shardManagerId);
             } catch (Exception e){
                 lastException = e;
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/AbstractShardManagerCreator.java
new file mode 100644 (file)
index 0000000..a5804aa
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.shardmanager;
+
+import akka.actor.Props;
+import com.google.common.base.Preconditions;
+import java.util.concurrent.CountDownLatch;
+import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
+import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
+import org.opendaylight.controller.cluster.datastore.config.Configuration;
+import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
+import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
+
+public abstract class AbstractShardManagerCreator<T extends AbstractShardManagerCreator<T>> {
+    private ClusterWrapper cluster;
+    private Configuration configuration;
+    private DatastoreContextFactory datastoreContextFactory;
+    private CountDownLatch waitTillReadyCountdownLatch;
+    private PrimaryShardInfoFutureCache primaryShardInfoCache;
+    private DatastoreSnapshot restoreFromSnapshot;
+    private volatile boolean sealed;
+
+    AbstractShardManagerCreator() {
+        // Prevent outside instantiation
+    }
+
+    @SuppressWarnings("unchecked")
+    private T self() {
+        return (T) this;
+    }
+
+    protected final void checkSealed() {
+        Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
+    }
+
+    ClusterWrapper getCluster() {
+        return cluster;
+    }
+
+    public T cluster(ClusterWrapper cluster) {
+        checkSealed();
+        this.cluster = cluster;
+        return self();
+    }
+
+    Configuration getConfiguration() {
+        return configuration;
+    }
+
+    public T configuration(Configuration configuration) {
+        checkSealed();
+        this.configuration = configuration;
+        return self();
+    }
+
+    DatastoreContextFactory getDdatastoreContextFactory() {
+        return datastoreContextFactory;
+    }
+
+    public T datastoreContextFactory(DatastoreContextFactory datastoreContextFactory) {
+        checkSealed();
+        this.datastoreContextFactory = datastoreContextFactory;
+        return self();
+    }
+
+    CountDownLatch getWaitTillReadyCountdownLatch() {
+        return waitTillReadyCountdownLatch;
+    }
+
+    public T waitTillReadyCountdownLatch(CountDownLatch waitTillReadyCountdownLatch) {
+        checkSealed();
+        this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
+        return self();
+    }
+
+    PrimaryShardInfoFutureCache getPrimaryShardInfoCache() {
+        return primaryShardInfoCache;
+    }
+
+    public T primaryShardInfoCache(PrimaryShardInfoFutureCache primaryShardInfoCache) {
+        checkSealed();
+        this.primaryShardInfoCache = primaryShardInfoCache;
+        return self();
+    }
+
+    DatastoreSnapshot getRestoreFromSnapshot() {
+        return restoreFromSnapshot;
+    }
+
+    public T restoreFromSnapshot(DatastoreSnapshot restoreFromSnapshot) {
+        checkSealed();
+        this.restoreFromSnapshot = restoreFromSnapshot;
+        return self();
+    }
+
+    protected void verify() {
+        sealed = true;
+        Preconditions.checkNotNull(cluster, "cluster should not be null");
+        Preconditions.checkNotNull(configuration, "configuration should not be null");
+        Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
+        Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
+        Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null");
+    }
+
+    public Props props() {
+        verify();
+        return Props.create(ShardManager.class, this);
+    }
+}
\ No newline at end of file
index 197ae827b0a71786f036decba43548c0f983f644..9bf023d211163657fe4d849c6a5f5461af50b58f 100644 (file)
@@ -117,7 +117,7 @@ import scala.concurrent.duration.FiniteDuration;
  * <li> Monitor the cluster members and store their addresses
  * <ul>
  */
-public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
+class ShardManager extends AbstractUntypedPersistentActorWithMetering {
 
     private static final Logger LOG = LoggerFactory.getLogger(ShardManager.class);
 
@@ -156,19 +156,16 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
 
     private final String persistenceId;
 
-    /**
-     */
-    protected ShardManager(AbstractBuilder<?> builder) {
-
-        this.cluster = builder.cluster;
-        this.configuration = builder.configuration;
-        this.datastoreContextFactory = builder.datastoreContextFactory;
-        this.type = builder.datastoreContextFactory.getBaseDatastoreContext().getDataStoreName();
+    ShardManager(AbstractShardManagerCreator<?> builder) {
+        this.cluster = builder.getCluster();
+        this.configuration = builder.getConfiguration();
+        this.datastoreContextFactory = builder.getDdatastoreContextFactory();
+        this.type = datastoreContextFactory.getBaseDatastoreContext().getDataStoreName();
         this.shardDispatcherPath =
                 new Dispatchers(context().system().dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
-        this.waitTillReadyCountdownLatch = builder.waitTillReadyCountdownLatch;
-        this.primaryShardInfoCache = builder.primaryShardInfoCache;
-        this.restoreFromSnapshot = builder.restoreFromSnapshot;
+        this.waitTillReadyCountdownLatch = builder.getWaitTillReadyCountdownLatch();
+        this.primaryShardInfoCache = builder.getPrimaryShardInfoCache();
+        this.restoreFromSnapshot = builder.getRestoreFromSnapshot();
 
         String possiblePersistenceId = datastoreContextFactory.getBaseDatastoreContext().getShardManagerPersistenceId();
         persistenceId = possiblePersistenceId != null ? possiblePersistenceId : "shard-manager-" + type;
@@ -1592,82 +1589,6 @@ public class ShardManager extends AbstractUntypedPersistentActorWithMetering {
         }
     }
 
-    public static Builder builder() {
-        return new Builder();
-    }
-
-    public static abstract class AbstractBuilder<T extends AbstractBuilder<T>> {
-        private ClusterWrapper cluster;
-        private Configuration configuration;
-        private DatastoreContextFactory datastoreContextFactory;
-        private CountDownLatch waitTillReadyCountdownLatch;
-        private PrimaryShardInfoFutureCache primaryShardInfoCache;
-        private DatastoreSnapshot restoreFromSnapshot;
-        private volatile boolean sealed;
-
-        @SuppressWarnings("unchecked")
-        private T self() {
-            return (T) this;
-        }
-
-        protected void checkSealed() {
-            Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
-        }
-
-        public T cluster(ClusterWrapper cluster) {
-            checkSealed();
-            this.cluster = cluster;
-            return self();
-        }
-
-        public T configuration(Configuration configuration) {
-            checkSealed();
-            this.configuration = configuration;
-            return self();
-        }
-
-        public T datastoreContextFactory(DatastoreContextFactory datastoreContextFactory) {
-            checkSealed();
-            this.datastoreContextFactory = datastoreContextFactory;
-            return self();
-        }
-
-        public T waitTillReadyCountdownLatch(CountDownLatch waitTillReadyCountdownLatch) {
-            checkSealed();
-            this.waitTillReadyCountdownLatch = waitTillReadyCountdownLatch;
-            return self();
-        }
-
-        public T primaryShardInfoCache(PrimaryShardInfoFutureCache primaryShardInfoCache) {
-            checkSealed();
-            this.primaryShardInfoCache = primaryShardInfoCache;
-            return self();
-        }
-
-        public T restoreFromSnapshot(DatastoreSnapshot restoreFromSnapshot) {
-            checkSealed();
-            this.restoreFromSnapshot = restoreFromSnapshot;
-            return self();
-        }
-
-        protected void verify() {
-            sealed = true;
-            Preconditions.checkNotNull(cluster, "cluster should not be null");
-            Preconditions.checkNotNull(configuration, "configuration should not be null");
-            Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
-            Preconditions.checkNotNull(waitTillReadyCountdownLatch, "waitTillReadyCountdownLatch should not be null");
-            Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null");
-        }
-
-        public Props props() {
-            verify();
-            return Props.create(ShardManager.class, this);
-        }
-    }
-
-    public static class Builder extends AbstractBuilder<Builder> {
-    }
-
     private void findPrimary(final String shardName, final FindPrimaryResponseHandler handler) {
         Timeout findPrimaryTimeout = new Timeout(datastoreContextFactory.getBaseDatastoreContext().
                 getShardInitializationTimeout().duration().$times(2));
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardmanager/ShardManagerCreator.java
new file mode 100644 (file)
index 0000000..308f06a
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.shardmanager;
+
+public final class ShardManagerCreator extends AbstractShardManagerCreator<ShardManagerCreator> {
+
+}
index 39158e0c1877cfaec95076d6752c2975cd24353f..8883a5e10b18b018e8ff7a48b91baedeb8f7fafb 100644 (file)
@@ -312,8 +312,8 @@ public class ShardManagerTest extends AbstractActorTest {
         final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
         final CountDownLatch newShardActorLatch = new CountDownLatch(2);
         class LocalShardManager extends ShardManager {
-            public LocalShardManager(AbstractBuilder<?> builder) {
-                super(builder);
+            public LocalShardManager(AbstractShardManagerCreator<?> creator) {
+                super(creator);
             }
 
             @Override
@@ -334,7 +334,7 @@ public class ShardManagerTest extends AbstractActorTest {
             private static final long serialVersionUID = 1L;
             @Override
             public ShardManager create() throws Exception {
-                return new LocalShardManager(new GenericBuilder<LocalShardManager>(LocalShardManager.class).
+                return new LocalShardManager(new GenericCreator<LocalShardManager>(LocalShardManager.class).
                         datastoreContextFactory(mockFactory).primaryShardInfoCache(primaryShardInfoCache).
                         configuration(mockConfig));
             }
@@ -2085,7 +2085,7 @@ public class ShardManagerTest extends AbstractActorTest {
             return new Builder(datastoreContextBuilder);
         }
 
-        private static class Builder extends AbstractGenericBuilder<Builder, TestShardManager> {
+        private static class Builder extends AbstractGenericCreator<Builder, TestShardManager> {
             private ActorRef shardActor;
             private final Map<String, ActorRef> shardActors = new HashMap<>();
 
@@ -2132,11 +2132,11 @@ public class ShardManagerTest extends AbstractActorTest {
         }
     }
 
-    private static abstract class AbstractGenericBuilder<T extends AbstractGenericBuilder<T, ?>, C extends ShardManager>
-                                                     extends ShardManager.AbstractBuilder<T> {
+    private static abstract class AbstractGenericCreator<T extends AbstractGenericCreator<T, ?>, C extends ShardManager>
+                                                     extends AbstractShardManagerCreator<T> {
         private final Class<C> shardManagerClass;
 
-        AbstractGenericBuilder(Class<C> shardManagerClass) {
+        AbstractGenericCreator(Class<C> shardManagerClass) {
             this.shardManagerClass = shardManagerClass;
             cluster(new MockClusterWrapper()).configuration(new MockConfiguration()).
                     waitTillReadyCountdownLatch(ready).primaryShardInfoCache(new PrimaryShardInfoFutureCache());
@@ -2149,8 +2149,8 @@ public class ShardManagerTest extends AbstractActorTest {
         }
     }
 
-    private static class GenericBuilder<C extends ShardManager> extends AbstractGenericBuilder<GenericBuilder<C>, C> {
-        GenericBuilder(Class<C> shardManagerClass) {
+    private static class GenericCreator<C extends ShardManager> extends AbstractGenericCreator<GenericCreator<C>, C> {
+        GenericCreator(Class<C> shardManagerClass) {
             super(shardManagerClass);
         }
     }