Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.pcelabelstore.util; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | import java.util.concurrent.ScheduledExecutorService; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.function.BiFunction; |
| 28 | |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.store.Timestamp; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMap; |
Ray Milkey | d1c34da | 2018-06-22 18:10:53 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.EventuallyConsistentMapAdapter; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 34 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 35 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 37 | |
| 38 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 39 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 40 | |
| 41 | /** |
| 42 | * Testing version of an Eventually Consistent Map. |
| 43 | */ |
| 44 | |
| 45 | public 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 HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 94 | @SuppressWarnings("ReturnValueIgnored") |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 95 | @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 | |