From b731e2f1dd0972409b136aebc7b463dd72c9cfad Mon Sep 17 00:00:00 2001 From: CNlucius Date: Tue, 13 Sep 2016 11:40:12 +0800 Subject: ONOSFW-171 O/S-SFC-ONOS scenario documentation Change-Id: I51ae1cf736ea24ab6680f8edca1b2bf5dd598365 Signed-off-by: CNlucius --- .../store/service/TestConsistentMap.java | 287 --------------------- 1 file changed, 287 deletions(-) delete mode 100644 framework/src/onos/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java (limited to 'framework/src/onos/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java') diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java b/framework/src/onos/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java deleted file mode 100644 index 0136a94c..00000000 --- a/framework/src/onos/core/api/src/test/java/org/onosproject/store/service/TestConsistentMap.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.store.service; - -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -import org.onosproject.core.ApplicationId; -import static org.onosproject.store.service.MapEvent.Type; -import static org.onosproject.store.service.MapEvent.Type.*; - -/** - * Test implementation of the consistent map. - */ -public final class TestConsistentMap extends ConsistentMapAdapter { - - private final List> listeners; - private final HashMap map; - private final String mapName; - private final AtomicLong counter = new AtomicLong(0); - - private TestConsistentMap(String mapName) { - map = new HashMap<>(); - listeners = new LinkedList<>(); - this.mapName = mapName; - } - - private Versioned version(V v) { - return new Versioned<>(v, counter.incrementAndGet(), System.currentTimeMillis()); - } - - /** - * Notify all listeners of an event. - */ - private void notifyListeners(String mapName, Type type, - K key, Versioned value) { - MapEvent event = new MapEvent<>(mapName, type, key, value); - listeners.forEach( - listener -> listener.event(event) - ); - } - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean isEmpty() { - return map.isEmpty(); - } - - @Override - public boolean containsKey(K key) { - return map.containsKey(key); - } - - @Override - public boolean containsValue(V value) { - return map.containsValue(value); - } - - @Override - public Versioned get(K key) { - V value = map.get(key); - if (value != null) { - return version(value); - } else { - return null; - } - } - - @Override - public Versioned computeIfAbsent(K key, Function mappingFunction) { - Versioned result = version(map.computeIfAbsent(key, mappingFunction)); - notifyListeners(mapName, INSERT, key, result); - return result; - } - - @Override - public Versioned compute(K key, BiFunction remappingFunction) { - return version(map.compute(key, remappingFunction)); - } - - @Override - public Versioned computeIfPresent(K key, BiFunction remappingFunction) { - return version(map.computeIfPresent(key, remappingFunction)); - } - - @Override - public Versioned computeIf(K key, Predicate condition, - BiFunction remappingFunction) { - return null; - } - - @Override - public Versioned put(K key, V value) { - Versioned result = version(value); - if (map.put(key, value) == null) { - notifyListeners(mapName, INSERT, key, result); - } else { - notifyListeners(mapName, UPDATE, key, result); - } - return result; - } - - @Override - public Versioned putAndGet(K key, V value) { - Versioned result = version(map.put(key, value)); - notifyListeners(mapName, UPDATE, key, result); - return result; - } - - @Override - public Versioned remove(K key) { - Versioned result = version(map.remove(key)); - notifyListeners(mapName, REMOVE, key, result); - return result; - } - - @Override - public void clear() { - map.clear(); - } - - @Override - public Set keySet() { - return map.keySet(); - } - - @Override - public Collection> values() { - return map - .values() - .stream() - .map(this::version) - .collect(Collectors.toList()); - } - - @Override - public Set>> entrySet() { - return super.entrySet(); - } - - @Override - public Versioned putIfAbsent(K key, V value) { - Versioned result = version(map.putIfAbsent(key, value)); - if (map.get(key).equals(value)) { - notifyListeners(mapName, INSERT, key, result); - } - return result; - } - - @Override - public boolean remove(K key, V value) { - boolean removed = map.remove(key, value); - if (removed) { - notifyListeners(mapName, REMOVE, key, null); - } - return removed; - } - - @Override - public boolean remove(K key, long version) { - boolean removed = map.remove(key, version); - if (removed) { - notifyListeners(mapName, REMOVE, key, null); - } - return removed; - } - - @Override - public boolean replace(K key, V oldValue, V newValue) { - boolean replaced = map.replace(key, oldValue, newValue); - if (replaced) { - notifyListeners(mapName, REMOVE, key, null); - } - return replaced; - } - - @Override - public boolean replace(K key, long oldVersion, V newValue) { - boolean replaced = map.replace(key, map.get(key), newValue); - if (replaced) { - notifyListeners(mapName, REMOVE, key, null); - } - return replaced; - } - - @Override - public void addListener(MapEventListener listener) { - listeners.add(listener); - } - - @Override - public void removeListener(MapEventListener listener) { - listeners.remove(listener); - } - - @Override - public Map asJavaMap() { - return map; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder implements ConsistentMapBuilder { - String mapName = "map"; - - @Override - public ConsistentMapBuilder withName(String mapName) { - this.mapName = mapName; - return this; - } - - @Override - public ConsistentMapBuilder withApplicationId(ApplicationId id) { - return this; - } - - @Override - public ConsistentMapBuilder withSerializer(Serializer serializer) { - return this; - } - - @Override - public ConsistentMapBuilder withPartitionsDisabled() { - return this; - } - - @Override - public ConsistentMapBuilder withUpdatesDisabled() { - return this; - } - - @Override - public ConsistentMapBuilder withPurgeOnUninstall() { - return this; - } - - @Override - public ConsistentMapBuilder withRelaxedReadConsistency() { - return this; - } - - @Override - public ConsistentMapBuilder withMeteringDisabled() { - return this; - } - - @Override - public ConsistentMap build() { - return new TestConsistentMap<>(mapName); - } - - @Override - public AsyncConsistentMap buildAsyncMap() { - return null; - } - - } - -} -- cgit 1.2.3-korg