Bug 7521: Move DatastoreSnapshotList et al to persisted package
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / AbstractShardManagerCreator.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.cluster.datastore.shardmanager;
9
10 import akka.actor.Props;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.CountDownLatch;
13 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
14 import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
15 import org.opendaylight.controller.cluster.datastore.config.Configuration;
16 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
17 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
18
19 public abstract class AbstractShardManagerCreator<T extends AbstractShardManagerCreator<T>> {
20     private ClusterWrapper cluster;
21     private Configuration configuration;
22     private DatastoreContextFactory datastoreContextFactory;
23     private CountDownLatch waitTillReadyCountDownLatch;
24     private PrimaryShardInfoFutureCache primaryShardInfoCache;
25     private DatastoreSnapshot restoreFromSnapshot;
26     private volatile boolean sealed;
27
28     AbstractShardManagerCreator() {
29         // Prevent outside instantiation
30     }
31
32     @SuppressWarnings("unchecked")
33     private T self() {
34         return (T) this;
35     }
36
37     protected final void checkSealed() {
38         Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
39     }
40
41     ClusterWrapper getCluster() {
42         return cluster;
43     }
44
45     public T cluster(ClusterWrapper newCluster) {
46         checkSealed();
47         this.cluster = newCluster;
48         return self();
49     }
50
51     Configuration getConfiguration() {
52         return configuration;
53     }
54
55     public T configuration(Configuration newConfiguration) {
56         checkSealed();
57         this.configuration = newConfiguration;
58         return self();
59     }
60
61     DatastoreContextFactory getDatastoreContextFactory() {
62         return datastoreContextFactory;
63     }
64
65     public T datastoreContextFactory(DatastoreContextFactory newDatastoreContextFactory) {
66         checkSealed();
67         this.datastoreContextFactory = newDatastoreContextFactory;
68         return self();
69     }
70
71     CountDownLatch getWaitTillReadyCountDownLatch() {
72         return waitTillReadyCountDownLatch;
73     }
74
75     public T waitTillReadyCountDownLatch(CountDownLatch newWaitTillReadyCountDownLatch) {
76         checkSealed();
77         this.waitTillReadyCountDownLatch = newWaitTillReadyCountDownLatch;
78         return self();
79     }
80
81     PrimaryShardInfoFutureCache getPrimaryShardInfoCache() {
82         return primaryShardInfoCache;
83     }
84
85     public T primaryShardInfoCache(PrimaryShardInfoFutureCache newPrimaryShardInfoCache) {
86         checkSealed();
87         this.primaryShardInfoCache = newPrimaryShardInfoCache;
88         return self();
89     }
90
91     DatastoreSnapshot getRestoreFromSnapshot() {
92         return restoreFromSnapshot;
93     }
94
95     public T restoreFromSnapshot(DatastoreSnapshot newRestoreFromSnapshot) {
96         checkSealed();
97         this.restoreFromSnapshot = newRestoreFromSnapshot;
98         return self();
99     }
100
101     protected void verify() {
102         sealed = true;
103         Preconditions.checkNotNull(cluster, "cluster should not be null");
104         Preconditions.checkNotNull(configuration, "configuration should not be null");
105         Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
106         Preconditions.checkNotNull(waitTillReadyCountDownLatch, "waitTillReadyCountdownLatch should not be null");
107         Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null");
108     }
109
110     public Props props() {
111         verify();
112         return Props.create(ShardManager.class, this);
113     }
114 }