blob: e8c93f31bca77b681840e194f46d8e0a56efcd2e [file] [log] [blame]
Madan Jampani762246d2015-07-21 15:40:59 -07001/*
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 */
16package org.onosproject.store.consistent.impl;
17
Madan Jampani762246d2015-07-21 15:40:59 -070018import org.onosproject.store.service.AtomicValue;
19import org.onosproject.store.service.AtomicValueEvent;
20import org.onosproject.store.service.AtomicValueEventListener;
21import org.onosproject.store.service.ConsistentMap;
22import org.onosproject.store.service.MapEvent;
23import org.onosproject.store.service.MapEventListener;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.Versioned;
26
Flavio Castro41b1f3a2015-07-31 13:51:32 -070027import java.util.Set;
28import java.util.concurrent.CopyOnWriteArraySet;
29
Madan Jampani762246d2015-07-21 15:40:59 -070030/**
31 * Default implementation of AtomicValue.
32 *
33 * @param <V> value type
34 */
35public 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 Castro41b1f3a2015-07-31 13:51:32 -070042 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 Jampani762246d2015-07-21 15:40:59 -070048
49 public DefaultAtomicValue(ConsistentMap<String, byte[]> valueMap,
Flavio Castro41b1f3a2015-07-31 13:51:32 -070050 String name,
51 boolean meteringEnabled,
52 Serializer serializer) {
Madan Jampani762246d2015-07-21 15:40:59 -070053 this.valueMap = valueMap;
54 this.name = name;
55 this.serializer = serializer;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070056 this.monitor = new MeteringAgent(COMPONENT_NAME, name, meteringEnabled);
Madan Jampani762246d2015-07-21 15:40:59 -070057 }
58
59 @Override
60 public boolean compareAndSet(V expect, V update) {
Flavio Castro41b1f3a2015-07-31 13:51:32 -070061 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 Jampani762246d2015-07-21 15:40:59 -070073 }
Flavio Castro41b1f3a2015-07-31 13:51:32 -070074 } finally {
Flavio Castro6e044612015-08-13 14:13:58 -070075 newTimer.stop(null);
Madan Jampani762246d2015-07-21 15:40:59 -070076 }
77 }
78
79 @Override
80 public V get() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -070081 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 {
Flavio Castro6e044612015-08-13 14:13:58 -070086 newTimer.stop(null);
Flavio Castro41b1f3a2015-07-31 13:51:32 -070087 }
Madan Jampani762246d2015-07-21 15:40:59 -070088 }
89
90 @Override
91 public V getAndSet(V value) {
Flavio Castro41b1f3a2015-07-31 13:51:32 -070092 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 {
Flavio Castro6e044612015-08-13 14:13:58 -070098 newTimer.stop(null);
Flavio Castro41b1f3a2015-07-31 13:51:32 -070099 }
Madan Jampani762246d2015-07-21 15:40:59 -0700100 }
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}