blob: 7bbd9d3c576ddbf11ec9b2817387080ac715aa81 [file] [log] [blame]
Madan Jampanidfde6ba2016-01-13 21:36:09 -08001/*
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 Jampani533ef982016-02-01 00:16:35 -080016
Madan Jampanif4c88502016-01-21 12:35:36 -080017package org.onosproject.store.primitives.impl;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080018
Madan Jampani533ef982016-02-01 00:16:35 -080019import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Map;
22import java.util.concurrent.CompletableFuture;
23
Madan Jampanidfde6ba2016-01-13 21:36:09 -080024import org.onosproject.store.service.AsyncAtomicValue;
25import org.onosproject.store.service.AsyncConsistentMap;
26import org.onosproject.store.service.AtomicValueEvent;
27import org.onosproject.store.service.AtomicValueEventListener;
28import org.onosproject.store.service.MapEvent;
29import org.onosproject.store.service.MapEventListener;
Madan Jampani533ef982016-02-01 00:16:35 -080030import org.onosproject.store.service.Serializer;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080031import org.onosproject.store.service.Versioned;
32
Madan Jampani533ef982016-02-01 00:16:35 -080033import com.google.common.base.Throwables;
34import com.google.common.collect.Maps;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080035
Madan Jampani533ef982016-02-01 00:16:35 -080036
Madan Jampanidfde6ba2016-01-13 21:36:09 -080037public class DefaultAsyncAtomicValue<V> implements AsyncAtomicValue<V> {
38
Madan Jampanidfde6ba2016-01-13 21:36:09 -080039 private final String name;
Madan Jampani533ef982016-02-01 00:16:35 -080040 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 Jampanidfde6ba2016-01-13 21:36:09 -080044 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 Jampani533ef982016-02-01 00:16:35 -080051 private static final String ADD_LISTENER = "addListener";
52 private static final String REMOVE_LISTENER = "removeListener";
53 private static final String NOTIFY_LISTENER = "notifyListener";
Madan Jampanidfde6ba2016-01-13 21:36:09 -080054
Madan Jampani533ef982016-02-01 00:16:35 -080055 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 Jampanidfde6ba2016-01-13 21:36:09 -080060 }
61
62 @Override
Madan Jampania090a112016-01-18 16:38:17 -080063 public String name() {
64 return name;
65 }
66
67 @Override
Madan Jampanidfde6ba2016-01-13 21:36:09 -080068 public CompletableFuture<Boolean> compareAndSet(V expect, V update) {
69 final MeteringAgent.Context newTimer = monitor.startTimer(COMPARE_AND_SET);
Madan Jampani533ef982016-02-01 00:16:35 -080070 return backingMap.replace(name, serializer.encode(expect), serializer.encode(update))
71 .whenComplete((r, e) -> newTimer.stop(e));
Madan Jampanidfde6ba2016-01-13 21:36:09 -080072 }
73
74 @Override
75 public CompletableFuture<V> get() {
76 final MeteringAgent.Context newTimer = monitor.startTimer(GET);
Madan Jampani533ef982016-02-01 00:16:35 -080077 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 Jampanidfde6ba2016-01-13 21:36:09 -080081 }
82
83 @Override
84 public CompletableFuture<V> getAndSet(V value) {
85 final MeteringAgent.Context newTimer = monitor.startTimer(GET_AND_SET);
Madan Jampani533ef982016-02-01 00:16:35 -080086 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 Jampanidfde6ba2016-01-13 21:36:09 -080096 }
97
98 @Override
99 public CompletableFuture<Void> set(V value) {
100 final MeteringAgent.Context newTimer = monitor.startTimer(SET);
Madan Jampani533ef982016-02-01 00:16:35 -0800101 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 Jampanidfde6ba2016-01-13 21:36:09 -0800110 }
111
112 @Override
113 public CompletableFuture<Void> addListener(AtomicValueEventListener<V> listener) {
Madan Jampani533ef982016-02-01 00:16:35 -0800114 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 Jampanidfde6ba2016-01-13 21:36:09 -0800119 }
120
121 @Override
122 public CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener) {
Madan Jampani533ef982016-02-01 00:16:35 -0800123 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 Jampanidfde6ba2016-01-13 21:36:09 -0800157 }
158 }
159 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800160 }
Madan Jampani533ef982016-02-01 00:16:35 -0800161}