Mahesh Poojary S | eb045b4 | 2015-11-19 19:17:29 +0530 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright 2015 Open Networking Laboratory
|
| 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.vtnrsc.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 |
|
| 33 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.*;
|
| 34 | import org.onosproject.store.service.EventuallyConsistentMapListener;
|
| 35 | import org.onosproject.store.service.EventuallyConsistentMapEvent;
|
| 36 | import org.onosproject.store.service.EventuallyConsistentMapBuilder;
|
| 37 | import org.onosproject.store.service.EventuallyConsistentMap;
|
| 38 |
|
| 39 | /**
|
| 40 | * Testing version of an Eventually Consistent Map.
|
| 41 | */
|
| 42 |
|
| 43 | public 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
|
| 185 | public EventuallyConsistentMapBuilder<K, V>
|
| 186 | withTimestampProvider(BiFunction<K, V, Timestamp> timestampProvider) {
|
| 187 | return this;
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public EventuallyConsistentMapBuilder<K, V> withEventExecutor(ExecutorService executor) {
|
| 192 | return this;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public EventuallyConsistentMapBuilder<K, V> withCommunicationExecutor(ExecutorService executor) {
|
| 197 | return this;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public EventuallyConsistentMapBuilder<K, V> withBackgroundExecutor(ScheduledExecutorService executor) {
|
| 202 | return this;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public EventuallyConsistentMapBuilder<K, V>
|
| 207 | withPeerUpdateFunction(BiFunction<K, V, Collection<NodeId>> peerUpdateFunction) {
|
| 208 | this.peerUpdateFunction = peerUpdateFunction;
|
| 209 | return this;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public EventuallyConsistentMapBuilder<K, V> withTombstonesDisabled() {
|
| 214 | return this;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public EventuallyConsistentMapBuilder<K, V> withAntiEntropyPeriod(long period, TimeUnit unit) {
|
| 219 | return this;
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public EventuallyConsistentMapBuilder<K, V> withFasterConvergence() {
|
| 224 | return this;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public EventuallyConsistentMapBuilder<K, V> withPersistence() {
|
| 229 | return this;
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public EventuallyConsistentMap<K, V> build() {
|
| 234 | if (name == null) {
|
| 235 | name = "test";
|
| 236 | }
|
| 237 | return new VtnEventuallyConsistentMapTest<>(name, peerUpdateFunction);
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | }
|
| 242 |
|