Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 16 | |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 17 | package org.onosproject.store.primitives.impl; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 18 | |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.CompletableFuture; |
| 23 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 24 | import org.onosproject.store.service.AsyncAtomicValue; |
| 25 | import org.onosproject.store.service.AsyncConsistentMap; |
| 26 | import org.onosproject.store.service.AtomicValueEvent; |
| 27 | import org.onosproject.store.service.AtomicValueEventListener; |
| 28 | import org.onosproject.store.service.MapEvent; |
| 29 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 30 | import org.onosproject.store.service.Serializer; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 31 | import org.onosproject.store.service.Versioned; |
| 32 | |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 33 | import com.google.common.base.Throwables; |
| 34 | import com.google.common.collect.Maps; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 35 | |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 36 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 37 | public class DefaultAsyncAtomicValue<V> implements AsyncAtomicValue<V> { |
| 38 | |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 39 | private final String name; |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 40 | private final Serializer serializer; |
| 41 | private final AsyncConsistentMap<String, byte[]> backingMap; |
| 42 | private final Map<AtomicValueEventListener<V>, MapEventListener<String, byte[]>> listeners = |
| 43 | Maps.newIdentityHashMap(); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 44 | private final MeteringAgent monitor; |
| 45 | |
| 46 | private static final String COMPONENT_NAME = "atomicValue"; |
| 47 | private static final String GET = "get"; |
| 48 | private static final String GET_AND_SET = "getAndSet"; |
| 49 | private static final String SET = "set"; |
| 50 | private static final String COMPARE_AND_SET = "compareAndSet"; |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 51 | private static final String ADD_LISTENER = "addListener"; |
| 52 | private static final String REMOVE_LISTENER = "removeListener"; |
| 53 | private static final String NOTIFY_LISTENER = "notifyListener"; |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 54 | |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 55 | public DefaultAsyncAtomicValue(String name, Serializer serializer, AsyncConsistentMap<String, byte[]> backingMap) { |
| 56 | this.name = checkNotNull(name, "name must not be null"); |
| 57 | this.serializer = checkNotNull(serializer, "serializer must not be null"); |
| 58 | this.backingMap = checkNotNull(backingMap, "backingMap must not be null"); |
| 59 | this.monitor = new MeteringAgent(COMPONENT_NAME, name, true); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 63 | public String name() { |
| 64 | return name; |
| 65 | } |
| 66 | |
| 67 | @Override |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 68 | public CompletableFuture<Boolean> compareAndSet(V expect, V update) { |
| 69 | final MeteringAgent.Context newTimer = monitor.startTimer(COMPARE_AND_SET); |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 70 | return backingMap.replace(name, serializer.encode(expect), serializer.encode(update)) |
| 71 | .whenComplete((r, e) -> newTimer.stop(e)); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public CompletableFuture<V> get() { |
| 76 | final MeteringAgent.Context newTimer = monitor.startTimer(GET); |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 77 | return backingMap.get(name) |
| 78 | .thenApply(Versioned::valueOrNull) |
| 79 | .thenApply(v -> v == null ? null : serializer.<V>decode(v)) |
| 80 | .whenComplete((r, e) -> newTimer.stop(e)); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<V> getAndSet(V value) { |
| 85 | final MeteringAgent.Context newTimer = monitor.startTimer(GET_AND_SET); |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 86 | if (value == null) { |
| 87 | return backingMap.remove(name) |
| 88 | .thenApply(Versioned::valueOrNull) |
| 89 | .thenApply(v -> v == null ? null : serializer.<V>decode(v)) |
| 90 | .whenComplete((r, e) -> newTimer.stop(e)); |
| 91 | } |
| 92 | return backingMap.put(name, serializer.encode(value)) |
| 93 | .thenApply(Versioned::valueOrNull) |
| 94 | .thenApply(v -> v == null ? null : serializer.<V>decode(v)) |
| 95 | .whenComplete((r, e) -> newTimer.stop(e)); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Void> set(V value) { |
| 100 | final MeteringAgent.Context newTimer = monitor.startTimer(SET); |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 101 | if (value == null) { |
| 102 | return backingMap.remove(name) |
| 103 | .whenComplete((r, e) -> newTimer.stop(e)) |
| 104 | .thenApply(v -> null); |
| 105 | |
| 106 | } |
| 107 | return backingMap.put(name, serializer.encode(value)) |
| 108 | .whenComplete((r, e) -> newTimer.stop(e)) |
| 109 | .thenApply(v -> null); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 114 | checkNotNull(listener, "listener must not be null"); |
| 115 | final MeteringAgent.Context newTimer = monitor.startTimer(ADD_LISTENER); |
| 116 | MapEventListener<String, byte[]> mapListener = |
| 117 | listeners.computeIfAbsent(listener, key -> new InternalMapValueEventListener(listener)); |
| 118 | return backingMap.addListener(mapListener).whenComplete((r, e) -> newTimer.stop(e)); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener) { |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 123 | checkNotNull(listener, "listener must not be null"); |
| 124 | final MeteringAgent.Context newTimer = monitor.startTimer(REMOVE_LISTENER); |
| 125 | MapEventListener<String, byte[]> mapListener = listeners.remove(listener); |
| 126 | if (mapListener != null) { |
| 127 | return backingMap.removeListener(mapListener) |
| 128 | .whenComplete((r, e) -> newTimer.stop(e)); |
| 129 | } else { |
| 130 | newTimer.stop(null); |
| 131 | return CompletableFuture.completedFuture(null); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private class InternalMapValueEventListener implements MapEventListener<String, byte[]> { |
| 136 | |
| 137 | private final AtomicValueEventListener<V> listener; |
| 138 | |
| 139 | InternalMapValueEventListener(AtomicValueEventListener<V> listener) { |
| 140 | this.listener = listener; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void event(MapEvent<String, byte[]> event) { |
| 145 | if (event.key().equals(name)) { |
| 146 | final MeteringAgent.Context newTimer = monitor.startTimer(NOTIFY_LISTENER); |
| 147 | byte[] rawNewValue = Versioned.valueOrNull(event.newValue()); |
| 148 | byte[] rawOldValue = Versioned.valueOrNull(event.oldValue()); |
| 149 | try { |
| 150 | listener.event(new AtomicValueEvent<>(name, |
| 151 | rawNewValue == null ? null : serializer.decode(rawNewValue), |
| 152 | rawOldValue == null ? null : serializer.decode(rawOldValue))); |
| 153 | newTimer.stop(null); |
| 154 | } catch (Exception e) { |
| 155 | newTimer.stop(e); |
| 156 | Throwables.propagate(e); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 160 | } |
Madan Jampani | 533ef98 | 2016-02-01 00:16:35 -0800 | [diff] [blame^] | 161 | } |