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