BUG-7965 Switch distributed-data backend to a separate shard
[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.AbstractDataStore;
14 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
15 import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
16 import org.opendaylight.controller.cluster.datastore.config.Configuration;
17 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
18 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
19
20 public abstract class AbstractShardManagerCreator<T extends AbstractShardManagerCreator<T>> {
21     private ClusterWrapper cluster;
22     private Configuration configuration;
23     private DatastoreContextFactory datastoreContextFactory;
24     private AbstractDataStore distributedDataStore;
25     private CountDownLatch waitTillReadyCountDownLatch;
26     private PrimaryShardInfoFutureCache primaryShardInfoCache;
27     private DatastoreSnapshot restoreFromSnapshot;
28     private volatile boolean sealed;
29
30     AbstractShardManagerCreator() {
31         // Prevent outside instantiation
32     }
33
34     @SuppressWarnings("unchecked")
35     private T self() {
36         return (T) this;
37     }
38
39     protected final void checkSealed() {
40         Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");
41     }
42
43     ClusterWrapper getCluster() {
44         return cluster;
45     }
46
47     public T cluster(ClusterWrapper newCluster) {
48         checkSealed();
49         this.cluster = newCluster;
50         return self();
51     }
52
53     Configuration getConfiguration() {
54         return configuration;
55     }
56
57     public T configuration(Configuration newConfiguration) {
58         checkSealed();
59         this.configuration = newConfiguration;
60         return self();
61     }
62
63     DatastoreContextFactory getDatastoreContextFactory() {
64         return datastoreContextFactory;
65     }
66
67     public T datastoreContextFactory(final DatastoreContextFactory newDatastoreContextFactory) {
68         checkSealed();
69         this.datastoreContextFactory = Preconditions.checkNotNull(newDatastoreContextFactory);
70         return self();
71     }
72
73     AbstractDataStore getDistributedDataStore() {
74         return distributedDataStore;
75     }
76
77     public T distributedDataStore(final AbstractDataStore distributedDataStore) {
78         checkSealed();
79         this.distributedDataStore = distributedDataStore;
80         return self();
81     }
82
83     CountDownLatch getWaitTillReadyCountDownLatch() {
84         return waitTillReadyCountDownLatch;
85     }
86
87     public T waitTillReadyCountDownLatch(CountDownLatch newWaitTillReadyCountDownLatch) {
88         checkSealed();
89         this.waitTillReadyCountDownLatch = newWaitTillReadyCountDownLatch;
90         return self();
91     }
92
93     PrimaryShardInfoFutureCache getPrimaryShardInfoCache() {
94         return primaryShardInfoCache;
95     }
96
97     public T primaryShardInfoCache(PrimaryShardInfoFutureCache newPrimaryShardInfoCache) {
98         checkSealed();
99         this.primaryShardInfoCache = newPrimaryShardInfoCache;
100         return self();
101     }
102
103     DatastoreSnapshot getRestoreFromSnapshot() {
104         return restoreFromSnapshot;
105     }
106
107     public T restoreFromSnapshot(DatastoreSnapshot newRestoreFromSnapshot) {
108         checkSealed();
109         this.restoreFromSnapshot = newRestoreFromSnapshot;
110         return self();
111     }
112
113     protected void verify() {
114         sealed = true;
115         Preconditions.checkNotNull(cluster, "cluster should not be null");
116         Preconditions.checkNotNull(configuration, "configuration should not be null");
117         Preconditions.checkNotNull(datastoreContextFactory, "datastoreContextFactory should not be null");
118         Preconditions.checkNotNull(distributedDataStore, "distributedDataStore should not be null");
119         Preconditions.checkNotNull(waitTillReadyCountDownLatch, "waitTillReadyCountdownLatch should not be null");
120         Preconditions.checkNotNull(primaryShardInfoCache, "primaryShardInfoCache should not be null");
121     }
122
123     public Props props() {
124         verify();
125         return Props.create(ShardManager.class, this);
126     }
127 }