blob: 4f0a296d7030470c6ef930501e5f2dc617f9dba8 [file] [log] [blame]
Madan Jampani619453b2015-07-22 23:47:09 -07001/*
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 */
Madan Jampanib5d72d52015-04-03 16:53:50 -070016package org.onosproject.store.consistent.impl;
17
18import org.onosproject.store.service.AsyncAtomicCounter;
19import org.onosproject.store.service.AtomicCounter;
20import org.onosproject.store.service.AtomicCounterBuilder;
21
Flavio Castro41b1f3a2015-07-31 13:51:32 -070022import java.util.concurrent.ScheduledExecutorService;
23
Madan Jampanib5d72d52015-04-03 16:53:50 -070024import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * Default implementation of AtomicCounterBuilder.
28 */
29public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder {
30
31 private String name;
32 private boolean partitionsEnabled = true;
33 private final Database partitionedDatabase;
34 private final Database inMemoryDatabase;
Madan Jampanif4d58f32015-06-05 17:38:22 -070035 private boolean retryOnFailure = false;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070036 private boolean metering = true;
Madan Jampanif4d58f32015-06-05 17:38:22 -070037 private ScheduledExecutorService retryExecutor = null;
Madan Jampanib5d72d52015-04-03 16:53:50 -070038
39 public DefaultAtomicCounterBuilder(Database inMemoryDatabase, Database partitionedDatabase) {
40 this.inMemoryDatabase = inMemoryDatabase;
41 this.partitionedDatabase = partitionedDatabase;
42 }
43
44 @Override
45 public AtomicCounterBuilder withName(String name) {
46 checkArgument(name != null && !name.isEmpty());
47 this.name = name;
48 return this;
49 }
50
51 @Override
52 public AtomicCounterBuilder withPartitionsDisabled() {
53 partitionsEnabled = false;
54 return this;
55 }
56
57 @Override
58 public AtomicCounter build() {
Madan Jampanif4d58f32015-06-05 17:38:22 -070059 validateInputs();
Madan Jampanib5d72d52015-04-03 16:53:50 -070060 Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070061 return new DefaultAtomicCounter(name, database, retryOnFailure, metering, retryExecutor);
Madan Jampanib5d72d52015-04-03 16:53:50 -070062 }
63
64 @Override
65 public AsyncAtomicCounter buildAsyncCounter() {
Madan Jampanif4d58f32015-06-05 17:38:22 -070066 validateInputs();
Madan Jampanib5d72d52015-04-03 16:53:50 -070067 Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070068 return new DefaultAsyncAtomicCounter(name, database, retryOnFailure, metering, retryExecutor);
Madan Jampanif4d58f32015-06-05 17:38:22 -070069 }
70
71 @Override
72 public AtomicCounterBuilder withRetryOnFailure() {
73 retryOnFailure = true;
74 return this;
75 }
76
77 @Override
Flavio Castro41b1f3a2015-07-31 13:51:32 -070078 public AtomicCounterBuilder withMeteringDisabled() {
79 metering = false;
80 return this;
81 }
82
83 @Override
Madan Jampanif4d58f32015-06-05 17:38:22 -070084 public AtomicCounterBuilder withRetryExecutor(ScheduledExecutorService executor) {
85 this.retryExecutor = executor;
86 return this;
87 }
88
89 private void validateInputs() {
90 if (retryOnFailure) {
91 if (retryExecutor == null) {
92 throw new IllegalArgumentException("RetryExecutor must be specified when retries are enabled");
93 }
94 }
Madan Jampanib5d72d52015-04-03 16:53:50 -070095 }
96}