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