blob: 280cffbe3adb854789a37df3a2fff8756e6fb954 [file] [log] [blame]
Ray Milkey35958232015-07-29 11:19:28 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey35958232015-07-29 11:19:28 -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 */
16package org.onosproject.store.service;
17
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23import java.util.Set;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.ScheduledExecutorService;
26import java.util.concurrent.TimeUnit;
27import java.util.function.BiFunction;
28
29import org.onlab.util.KryoNamespace;
30import org.onosproject.cluster.NodeId;
31import org.onosproject.store.Timestamp;
32
Aaron Kruglikov66cf0b92015-10-26 15:46:54 -070033import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
34import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
Ray Milkey35958232015-07-29 11:19:28 -070035
36/**
37 * Testing version of an Eventually Consistent Map.
38 */
39
40public final class TestEventuallyConsistentMap<K, V> extends EventuallyConsistentMapAdapter<K, V> {
41
42 private final HashMap<K, V> map;
43 private final String mapName;
44 private final List<EventuallyConsistentMapListener<K, V>> listeners;
45 private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
46
47 private TestEventuallyConsistentMap(String mapName,
48 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
49 map = new HashMap<>();
50 listeners = new LinkedList<>();
51 this.mapName = mapName;
52 this.peerUpdateFunction = peerUpdateFunction;
53 }
54
55 /**
56 * Notify all listeners of an event.
57 */
58 private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) {
59 listeners.forEach(
60 listener -> listener.event(event)
61 );
62 }
63
64 @Override
65 public int size() {
66 return map.size();
67 }
68
69 @Override
70 public boolean isEmpty() {
71 return map.isEmpty();
72 }
73
74 @Override
75 public boolean containsKey(K key) {
76 return map.containsKey(key);
77 }
78
79 @Override
80 public boolean containsValue(V value) {
81 return map.containsValue(value);
82 }
83
84 @Override
85 public V get(K key) {
86 return map.get(key);
87 }
88
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080089 @SuppressWarnings("ReturnValueIgnored")
Ray Milkey35958232015-07-29 11:19:28 -070090 @Override
91 public void put(K key, V value) {
92 map.put(key, value);
93 EventuallyConsistentMapEvent<K, V> addEvent =
94 new EventuallyConsistentMapEvent<>(mapName, PUT, key, value);
95 notifyListeners(addEvent);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070096 if (peerUpdateFunction != null) {
97 peerUpdateFunction.apply(key, value);
98 }
Ray Milkey35958232015-07-29 11:19:28 -070099 }
100
101 @Override
102 public V remove(K key) {
103 V result = map.remove(key);
104 if (result != null) {
105 EventuallyConsistentMapEvent<K, V> removeEvent =
106 new EventuallyConsistentMapEvent<>(mapName, REMOVE,
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200107 key, result);
Ray Milkey35958232015-07-29 11:19:28 -0700108 notifyListeners(removeEvent);
109 }
110 return result;
111 }
112
113 @Override
114 public void remove(K key, V value) {
115 boolean removed = map.remove(key, value);
116 if (removed) {
117 EventuallyConsistentMapEvent<K, V> removeEvent =
118 new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, value);
119 notifyListeners(removeEvent);
120 }
121 }
122
123 @Override
124 public V compute(K key, BiFunction<K, V, V> recomputeFunction) {
125 return map.compute(key, recomputeFunction);
126 }
127
128 @Override
129 public void putAll(Map<? extends K, ? extends V> m) {
130 map.putAll(m);
131 }
132
133 @Override
134 public void clear() {
135 map.clear();
136 }
137
138 @Override
139 public Set<K> keySet() {
140 return map.keySet();
141 }
142
143 @Override
144 public Collection<V> values() {
145 return map.values();
146 }
147
148 @Override
149 public Set<Map.Entry<K, V>> entrySet() {
150 return map.entrySet();
151 }
152
153 public static <K, V> Builder<K, V> builder() {
154 return new Builder<>();
155 }
156
157 @Override
158 public void addListener(EventuallyConsistentMapListener<K, V> listener) {
159 listeners.add(listener);
160 }
161
162 @Override
163 public void removeListener(EventuallyConsistentMapListener<K, V> listener) {
164 listeners.remove(listener);
165 }
166
167 public static class Builder<K, V> implements EventuallyConsistentMapBuilder<K, V> {
168 private String name;
169 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
170
171 @Override
172 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
173 this.name = name;
174 return this;
175 }
176
177 @Override
178 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace.Builder serializerBuilder) {
179 return this;
180 }
181
182 @Override
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700183 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
184 return this;
185 }
186
187 @Override
Ray Milkey35958232015-07-29 11:19:28 -0700188 public EventuallyConsistentMapBuilder<K, V>
189 withTimestampProvider(BiFunction<K, V, Timestamp> timestampProvider) {
190 return this;
191 }
192
193 @Override
194 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
195 return this;
196 }
197
198 @Override
199 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(ExecutorService executor) {
200 return this;
201 }
202
203 @Override
204 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
205 return this;
206 }
207
208 @Override
209 public EventuallyConsistentMapBuilder<K, V>
210 withPeerUpdateFunction(BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
211 this.peerUpdateFunction = peerUpdateFunction;
212 return this;
213 }
214
215 @Override
216 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
217 return this;
218 }
219
220 @Override
221 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
222 return this;
223 }
224
225 @Override
226 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
227 return this;
228 }
229
230 @Override
231 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
232 return this;
233 }
234
235 @Override
236 public EventuallyConsistentMap<K, V> build() {
237 if (name == null) {
238 name = "test";
239 }
240 return new TestEventuallyConsistentMap<>(name, peerUpdateFunction);
241 }
242 }
243
244}
245