blob: 1ba58f7533ae8f5b40e00ebf2edb86e8755f2bbd [file] [log] [blame]
Madan Jampani762246d2015-07-21 15:40:59 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani762246d2015-07-21 15:40:59 -07003 *
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 Jampanie17d3282016-02-03 15:30:57 -080016package org.onosproject.store.primitives;
Madan Jampani762246d2015-07-21 15:40:59 -070017
Madan Jampanidfde6ba2016-01-13 21:36:09 -080018import java.util.concurrent.CompletableFuture;
Madan Jampanie17d3282016-02-03 15:30:57 -080019import java.util.concurrent.ExecutionException;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080020import java.util.concurrent.TimeUnit;
Madan Jampanie17d3282016-02-03 15:30:57 -080021import java.util.concurrent.TimeoutException;
Madan Jampania090a112016-01-18 16:38:17 -080022
Madan Jampanidfde6ba2016-01-13 21:36:09 -080023import org.onosproject.store.service.AsyncAtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070024import org.onosproject.store.service.AtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070025import org.onosproject.store.service.AtomicValueEventListener;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080026import org.onosproject.store.service.StorageException;
Madan Jampania090a112016-01-18 16:38:17 -080027import org.onosproject.store.service.Synchronous;
Madan Jampani762246d2015-07-21 15:40:59 -070028
29/**
Madan Jampanie17d3282016-02-03 15:30:57 -080030 * Default implementation for a {@code AtomicValue} backed by a {@link AsyncAtomicValue}.
Madan Jampani762246d2015-07-21 15:40:59 -070031 *
32 * @param <V> value type
33 */
Madan Jampania090a112016-01-18 16:38:17 -080034public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> {
Madan Jampani762246d2015-07-21 15:40:59 -070035
Madan Jampanidfde6ba2016-01-13 21:36:09 -080036 private final AsyncAtomicValue<V> asyncValue;
Madan Jampanie17d3282016-02-03 15:30:57 -080037 private final long operationTimeoutMillis;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070038
Madan Jampanie17d3282016-02-03 15:30:57 -080039 public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue, long operationTimeoutMillis) {
Madan Jampania090a112016-01-18 16:38:17 -080040 super(asyncValue);
Madan Jampanidfde6ba2016-01-13 21:36:09 -080041 this.asyncValue = asyncValue;
Madan Jampanie17d3282016-02-03 15:30:57 -080042 this.operationTimeoutMillis = operationTimeoutMillis;
Madan Jampani762246d2015-07-21 15:40:59 -070043 }
44
45 @Override
46 public boolean compareAndSet(V expect, V update) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080047 return complete(asyncValue.compareAndSet(expect, update));
Madan Jampani762246d2015-07-21 15:40:59 -070048 }
49
50 @Override
51 public V get() {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080052 return complete(asyncValue.get());
Madan Jampani762246d2015-07-21 15:40:59 -070053 }
54
55 @Override
56 public V getAndSet(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080057 return complete(asyncValue.getAndSet(value));
Madan Jampani762246d2015-07-21 15:40:59 -070058 }
59
60 @Override
61 public void set(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080062 complete(asyncValue.set(value));
Madan Jampani762246d2015-07-21 15:40:59 -070063 }
64
65 @Override
66 public void addListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080067 complete(asyncValue.addListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070068 }
69
70 @Override
71 public void removeListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080072 complete(asyncValue.removeListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070073 }
74
Madan Jampanie17d3282016-02-03 15:30:57 -080075 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 Jampani762246d2015-07-21 15:40:59 -070086 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -080087}