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