blob: 05bbb7b4a7c21fc40dc1a4dc2baf558b8c799718 [file] [log] [blame]
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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
93 @Override
94 public void put(K key, V value) {
95 map.put(key, value);
96 EventuallyConsistentMapEvent<K, V> addEvent =
97 new EventuallyConsistentMapEvent<>(mapName, PUT, key, value);
98 notifyListeners(addEvent);
99 if (peerUpdateFunction != null) {
100 peerUpdateFunction.apply(key, value);
101 }
102 }
103
104 @Override
105 public V remove(K key) {
106 V result = map.remove(key);
107 if (result != null) {
108 EventuallyConsistentMapEvent<K, V> removeEvent =
109 new EventuallyConsistentMapEvent<>(mapName, REMOVE,
110 key, map.get(key));
111 notifyListeners(removeEvent);
112 }
113 return result;
114 }
115
116 @Override
117 public void remove(K key, V value) {
118 boolean removed = map.remove(key, value);
119 if (removed) {
120 EventuallyConsistentMapEvent<K, V> removeEvent =
121 new EventuallyConsistentMapEvent<>(mapName, REMOVE, key, value);
122 notifyListeners(removeEvent);
123 }
124 }
125
126 @Override
127 public V compute(K key, BiFunction<K, V, V> recomputeFunction) {
128 return map.compute(key, recomputeFunction);
129 }
130
131 @Override
132 public void putAll(Map<? extends K, ? extends V> m) {
133 map.putAll(m);
134 }
135
136 @Override
137 public void clear() {
138 map.clear();
139 }
140
141 @Override
142 public Set<K> keySet() {
143 return map.keySet();
144 }
145
146 @Override
147 public Collection<V> values() {
148 return map.values();
149 }
150
151 @Override
152 public Set<Map.Entry<K, V>> entrySet() {
153 return map.entrySet();
154 }
155
156 public static <K, V> Builder<K, V> builder() {
157 return new Builder<>();
158 }
159
160 @Override
161 public void addListener(EventuallyConsistentMapListener<K, V> listener) {
162 listeners.add(listener);
163 }
164
165 @Override
166 public void removeListener(EventuallyConsistentMapListener<K, V> listener) {
167 listeners.remove(listener);
168 }
169
170 public static class Builder<K, V> implements EventuallyConsistentMapBuilder<K, V> {
171 private String name;
172 private BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
173
174 @Override
175 public EventuallyConsistentMapBuilder<K, V> withName(String name) {
176 this.name = name;
177 return this;
178 }
179
180 @Override
181 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace.Builder serializerBuilder) {
182 return this;
183 }
184
185 @Override
186 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
187 return this;
188 }
189
190 @Override
191 public EventuallyConsistentMapBuilder<K, V>
192 withTimestampProvider(BiFunction<K, V, Timestamp> timestampProvider) {
193 return this;
194 }
195
196 @Override
197 public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
198 return this;
199 }
200
201 @Override
202 public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(ExecutorService executor) {
203 return this;
204 }
205
206 @Override
207 public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
208 return this;
209 }
210
211 @Override
212 public EventuallyConsistentMapBuilder<K, V>
213 withPeerUpdateFunction(BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
214 this.peerUpdateFunction = peerUpdateFunction;
215 return this;
216 }
217
218 @Override
219 public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
220 return this;
221 }
222
223 @Override
224 public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
225 return this;
226 }
227
228 @Override
229 public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
230 return this;
231 }
232
233 @Override
234 public EventuallyConsistentMapBuilder<K, V> withPersistence() {
235 return this;
236 }
237
238 @Override
239 public EventuallyConsistentMap<K, V> build() {
240 if (name == null) {
241 name = "test";
242 }
243 return new TestEventuallyConsistentMap<>(name, peerUpdateFunction);
244 }
245 }
246
247}
248