Madan Jampani | 762246d | 2015-07-21 15:40:59 -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 | */ |
| 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.TimeUnit; |
| 20 | import org.onosproject.store.service.AsyncAtomicValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 21 | import org.onosproject.store.service.AtomicValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.AtomicValueEventListener; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.StorageException; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 24 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 25 | import com.google.common.util.concurrent.Futures; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 26 | |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 27 | /** |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 28 | * Default implementation of {@link AtomicValue}. |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 29 | * |
| 30 | * @param <V> value type |
| 31 | */ |
| 32 | public class DefaultAtomicValue<V> implements AtomicValue<V> { |
| 33 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 34 | private static final int OPERATION_TIMEOUT_MILLIS = 5000; |
| 35 | private final AsyncAtomicValue<V> asyncValue; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 36 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 37 | public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) { |
| 38 | this.asyncValue = asyncValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public boolean compareAndSet(V expect, V update) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 43 | return complete(asyncValue.compareAndSet(expect, update)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public V get() { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 48 | return complete(asyncValue.get()); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public V getAndSet(V value) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 53 | return complete(asyncValue.getAndSet(value)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void set(V value) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 58 | complete(asyncValue.set(value)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void addListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 63 | complete(asyncValue.addListener(listener)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void removeListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 68 | complete(asyncValue.removeListener(listener)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 71 | private static <V> V complete(CompletableFuture<V> future) { |
| 72 | return Futures.getChecked(future, StorageException.class, OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 73 | } |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 74 | } |