a553ffff387e0eb112b1fc23113d2159483ce39e
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.store.ecmap;
17
18 import org.onlab.util.KryoNamespace;
19 import org.onosproject.cluster.ClusterService;
20 import org.onosproject.cluster.NodeId;
21 import org.onosproject.store.Timestamp;
22 import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
23 import org.onosproject.store.service.EventuallyConsistentMap;
24 import org.onosproject.store.service.EventuallyConsistentMapBuilder;
25
26 import java.util.Collection;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import java.util.function.BiFunction;
31
32 import static com.google.common.base.Preconditions.checkArgument;
33 import static com.google.common.base.Preconditions.checkNotNull;
34
35 /**
36  * Eventually consistent map builder.
37  */
38 public class EventuallyConsistentMapBuilderImpl<K, V>
39         implements EventuallyConsistentMapBuilder<K, V> {
40     private final ClusterService clusterService;
41     private final ClusterCommunicationService clusterCommunicator;
42
43     private String name;
44     private KryoNamespace.Builder serializerBuilder;
45     private ExecutorService eventExecutor;
46     private ExecutorService communicationExecutor;
47     private ScheduledExecutorService backgroundExecutor;
48     private BiFunction<K, V, Timestamp> timestampProvider;
49     private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
50     private boolean tombstonesDisabled = false;
51     private long antiEntropyPeriod = 5;
52     private TimeUnit antiEntropyTimeUnit = TimeUnit.SECONDS;
53     private boolean convergeFaster = false;
54     private boolean persistent = false;
55
56     /**
57      * Creates a new eventually consistent map builder.
58      *
59      * @param clusterService cluster service
60      * @param clusterCommunicator cluster communication service
61      */
62     public EventuallyConsistentMapBuilderImpl(ClusterService clusterService,
63                                               ClusterCommunicationService clusterCommunicator) {
64         this.clusterService = checkNotNull(clusterService);
65         this.clusterCommunicator = checkNotNull(clusterCommunicator);
66     }
67
68     @Override
69     public EventuallyConsistentMapBuilder<K, V> withName(String name) {
70         this.name = checkNotNull(name);
71         return this;
72     }
73
74     @Override
75     public EventuallyConsistentMapBuilder<K, V> withSerializer(
76             KryoNamespace.Builder serializerBuilder) {
77         this.serializerBuilder = checkNotNull(serializerBuilder);
78         return this;
79     }
80
81     @Override
82     public EventuallyConsistentMapBuilder<K, V> withTimestampProvider(
83             BiFunction<K, V, Timestamp> timestampProvider) {
84         this.timestampProvider = checkNotNull(timestampProvider);
85         return this;
86     }
87
88     @Override
89     public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
90         this.eventExecutor = checkNotNull(executor);
91         return this;
92     }
93
94     @Override
95     public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(
96             ExecutorService executor) {
97         communicationExecutor = checkNotNull(executor);
98         return this;
99     }
100
101     @Override
102     public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
103         this.backgroundExecutor = checkNotNull(executor);
104         return this;
105     }
106
107     @Override
108     public EventuallyConsistentMapBuilder<K, V> withPeerUpdateFunction(
109             BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
110         this.peerUpdateFunction = checkNotNull(peerUpdateFunction);
111         return this;
112     }
113
114     @Override
115     public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
116         tombstonesDisabled = true;
117         return this;
118     }
119
120     @Override
121     public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
122         checkArgument(period > 0, "anti-entropy period must be greater than 0");
123         antiEntropyPeriod = period;
124         antiEntropyTimeUnit = checkNotNull(unit);
125         return this;
126     }
127
128     @Override
129     public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
130         convergeFaster = true;
131         return this;
132     }
133
134     @Override
135     public EventuallyConsistentMapBuilder<K, V> withPersistence() {
136         persistent = true;
137         return this;
138     }
139
140     @Override
141     public EventuallyConsistentMap<K, V> build() {
142         checkNotNull(name, "name is a mandatory parameter");
143         checkNotNull(serializerBuilder, "serializerBuilder is a mandatory parameter");
144         checkNotNull(timestampProvider, "timestampProvider is a mandatory parameter");
145
146         return new EventuallyConsistentMapImpl<>(name,
147                                                  clusterService,
148                                                  clusterCommunicator,
149                                                  serializerBuilder,
150                                                  timestampProvider,
151                                                  peerUpdateFunction,
152                                                  eventExecutor,
153                                                  communicationExecutor,
154                                                  backgroundExecutor,
155                                                  tombstonesDisabled,
156                                                  antiEntropyPeriod,
157                                                  antiEntropyTimeUnit,
158                                                  convergeFaster,
159                                                  persistent);
160     }
161 }