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