Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [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.store.consistent.impl; |
| 17 | |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 18 | import org.onosproject.store.service.AtomicValue; |
| 19 | import org.onosproject.store.service.AtomicValueEvent; |
| 20 | import org.onosproject.store.service.AtomicValueEventListener; |
| 21 | import org.onosproject.store.service.ConsistentMap; |
| 22 | import org.onosproject.store.service.MapEvent; |
| 23 | import org.onosproject.store.service.MapEventListener; |
| 24 | import org.onosproject.store.service.Serializer; |
| 25 | import org.onosproject.store.service.Versioned; |
| 26 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | import java.util.concurrent.CopyOnWriteArraySet; |
| 29 | |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Default implementation of AtomicValue. |
| 32 | * |
| 33 | * @param <V> value type |
| 34 | */ |
| 35 | public class DefaultAtomicValue<V> implements AtomicValue<V> { |
| 36 | |
| 37 | private final Set<AtomicValueEventListener<V>> listeners = new CopyOnWriteArraySet<>(); |
| 38 | private final ConsistentMap<String, byte[]> valueMap; |
| 39 | private final String name; |
| 40 | private final Serializer serializer; |
| 41 | private final MapEventListener<String, byte[]> mapEventListener = new InternalMapEventListener(); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 42 | private final MeteringAgent monitor; |
| 43 | |
| 44 | private static final String COMPONENT_NAME = "atomicValue"; |
| 45 | private static final String GET = "get"; |
| 46 | private static final String GET_AND_SET = "getAndSet"; |
| 47 | private static final String COMPARE_AND_SET = "compareAndSet"; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 48 | |
| 49 | public DefaultAtomicValue(ConsistentMap<String, byte[]> valueMap, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 50 | String name, |
| 51 | boolean meteringEnabled, |
| 52 | Serializer serializer) { |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 53 | this.valueMap = valueMap; |
| 54 | this.name = name; |
| 55 | this.serializer = serializer; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 56 | this.monitor = new MeteringAgent(COMPONENT_NAME, name, meteringEnabled); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public boolean compareAndSet(V expect, V update) { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 61 | final MeteringAgent.Context newTimer = monitor.startTimer(COMPARE_AND_SET); |
| 62 | try { |
| 63 | if (expect == null) { |
| 64 | if (update == null) { |
| 65 | return true; |
| 66 | } |
| 67 | return valueMap.putIfAbsent(name, serializer.encode(update)) == null; |
| 68 | } else { |
| 69 | if (update == null) { |
| 70 | return valueMap.remove(name, serializer.encode(expect)); |
| 71 | } |
| 72 | return valueMap.replace(name, serializer.encode(expect), serializer.encode(update)); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 73 | } |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 74 | } finally { |
| 75 | newTimer.stop(); |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public V get() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 81 | final MeteringAgent.Context newTimer = monitor.startTimer(GET); |
| 82 | try { |
| 83 | Versioned<byte[]> rawValue = valueMap.get(name); |
| 84 | return rawValue == null ? null : serializer.decode(rawValue.value()); |
| 85 | } finally { |
| 86 | newTimer.stop(); |
| 87 | } |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public V getAndSet(V value) { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 92 | final MeteringAgent.Context newTimer = monitor.startTimer(GET_AND_SET); |
| 93 | try { |
| 94 | Versioned<byte[]> previousValue = value == null ? |
| 95 | valueMap.remove(name) : valueMap.put(name, serializer.encode(value)); |
| 96 | return previousValue == null ? null : serializer.decode(previousValue.value()); |
| 97 | } finally { |
| 98 | newTimer.stop(); |
| 99 | } |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void set(V value) { |
| 104 | getAndSet(value); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void addListener(AtomicValueEventListener<V> listener) { |
| 109 | synchronized (listeners) { |
| 110 | if (listeners.add(listener)) { |
| 111 | if (listeners.size() == 1) { |
| 112 | valueMap.addListener(mapEventListener); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void removeListener(AtomicValueEventListener<V> listener) { |
| 120 | synchronized (listeners) { |
| 121 | if (listeners.remove(listener)) { |
| 122 | if (listeners.size() == 0) { |
| 123 | valueMap.removeListener(mapEventListener); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private class InternalMapEventListener implements MapEventListener<String, byte[]> { |
| 130 | |
| 131 | @Override |
| 132 | public void event(MapEvent<String, byte[]> mapEvent) { |
| 133 | V newValue = mapEvent.type() == MapEvent.Type.REMOVE ? null : serializer.decode(mapEvent.value().value()); |
| 134 | AtomicValueEvent<V> atomicValueEvent = new AtomicValueEvent<>(name, AtomicValueEvent.Type.UPDATE, newValue); |
| 135 | listeners.forEach(l -> l.event(atomicValueEvent)); |
| 136 | } |
| 137 | } |
| 138 | } |