Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [diff] [blame] | 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 | */ |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
| 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AtomicCounter; |
| 20 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 21 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 22 | import java.util.concurrent.ScheduledExecutorService; |
| 23 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of AtomicCounterBuilder. |
| 28 | */ |
| 29 | public 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 Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 35 | private boolean retryOnFailure = false; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 36 | private boolean metering = true; |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 37 | private ScheduledExecutorService retryExecutor = null; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 38 | |
| 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 Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 59 | validateInputs(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 60 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 61 | return new DefaultAtomicCounter(name, database, retryOnFailure, metering, retryExecutor); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public AsyncAtomicCounter buildAsyncCounter() { |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 66 | validateInputs(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 67 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 68 | return new DefaultAsyncAtomicCounter(name, database, retryOnFailure, metering, retryExecutor); |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public AtomicCounterBuilder withRetryOnFailure() { |
| 73 | retryOnFailure = true; |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | @Override |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 78 | public AtomicCounterBuilder withMeteringDisabled() { |
| 79 | metering = false; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | @Override |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 84 | 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 Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 95 | } |
| 96 | } |