blob: 38ceb0bc9542e38ee453b871d38050e98ac0f165 [file] [log] [blame]
Madan Jampanic27b6b22016-02-05 11:36:31 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampanic27b6b22016-02-05 11:36:31 -08003 *
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.vtnrsc.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;
32
33import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.*;
34import org.onosproject.store.service.EventuallyConsistentMapListener;
35import org.onosproject.store.service.EventuallyConsistentMapEvent;
36import org.onosproject.store.service.EventuallyConsistentMapBuilder;
37import org.onosproject.store.service.EventuallyConsistentMap;
38
39/**
40 * Testing version of an Eventually Consistent Map.
41 */
42
43public final class VtnEventuallyConsistentMapTest<K, V> extends VtnEventuallyConsistentMapAdapter<K, V> {
44
45 private final HashMap<K, V> map;
46 private final String mapName;
47 private final List<EventuallyConsistentMapListener<K, V>> listeners;
48 private final BiFunction<K, V, Collection<NodeId>> peerUpdateFunction;
49
50 private VtnEventuallyConsistentMapTest(String mapName,
51 BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
52 map = new HashMap<>();
53 listeners = new LinkedList<>();
54 this.mapName = mapName;
55 this.peerUpdateFunction = peerUpdateFunction;
56 }
57
58 /**
59 * Notify all listeners of an event.
60 */
61 private void notifyListeners(EventuallyConsistentMapEvent<K, V> event) {
62 listeners.forEach(
63 listener -> listener.event(event)
64 );
65 }
66
67 @Override
68 public int size() {
69 return map.size();
70 }
71
72 @Override
73 public boolean isEmpty() {
74 return map.isEmpty();
75 }
76
77 @Override
78 public boolean containsKey(K key) {
79 return map.containsKey(key);
80 }
81
82 @Override
83 public boolean containsValue(V value) {
84 return map.containsValue(value);
85 }
86
87 @Override
88 public V get(K key) {
89 return map.get(key);
90 }
91
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -080092 @SuppressWarnings("ReturnValueIgnored")
Madan Jampanic27b6b22016-02-05 11:36:31 -080093 @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
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700186 public EventuallyConsistentMapBuilder<K, V> withSerializer(KryoNamespace serializer) {
187 return this;
188 }
189
190 @Override
Madan Jampanic27b6b22016-02-05 11:36:31 -0800191 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 VtnEventuallyConsistentMapTest<>(name, peerUpdateFunction);
244 }
245 }
246
247}
248