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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.