Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 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 | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 17 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 18 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 19 | import java.util.concurrent.ExecutionException; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 20 | import java.util.concurrent.TimeUnit; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 21 | import java.util.concurrent.TimeoutException; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 22 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.AsyncAtomicValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.AtomicValue; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 25 | import org.onosproject.store.service.AtomicValueEventListener; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 26 | import org.onosproject.store.service.StorageException; |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 27 | import org.onosproject.store.service.Synchronous; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 30 | * Default implementation for a {@code AtomicValue} backed by a {@link AsyncAtomicValue}. |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 31 | * |
| 32 | * @param <V> value type |
| 33 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 34 | public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> { |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 35 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 36 | private final AsyncAtomicValue<V> asyncValue; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 37 | private final long operationTimeoutMillis; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 38 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 39 | public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue, long operationTimeoutMillis) { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 40 | super(asyncValue); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 41 | this.asyncValue = asyncValue; |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 42 | this.operationTimeoutMillis = operationTimeoutMillis; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean compareAndSet(V expect, V update) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 47 | return complete(asyncValue.compareAndSet(expect, update)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public V get() { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 52 | return complete(asyncValue.get()); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public V getAndSet(V value) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 57 | return complete(asyncValue.getAndSet(value)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void set(V value) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 62 | complete(asyncValue.set(value)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void addListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 67 | complete(asyncValue.addListener(listener)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void removeListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 72 | complete(asyncValue.removeListener(listener)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 75 | private <T> T complete(CompletableFuture<T> future) { |
| 76 | try { |
| 77 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
| 78 | } catch (InterruptedException e) { |
| 79 | Thread.currentThread().interrupt(); |
| 80 | throw new StorageException.Interrupted(); |
| 81 | } catch (TimeoutException e) { |
| 82 | throw new StorageException.Timeout(); |
| 83 | } catch (ExecutionException e) { |
| 84 | throw new StorageException(e.getCause()); |
| 85 | } |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 86 | } |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 87 | } |