blob: a6d653607de3f9a598b727c2a3d63f227e2dfbbb [file] [log] [blame]
Madan Jampani13f65152016-08-17 13:14:53 -07001/*
Thomas Vachuskab6d31672018-07-27 17:03:46 -07002 * Copyright 2018-present Open Networking Foundation
Madan Jampani13f65152016-08-17 13:14:53 -07003 *
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 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Madan Jampani13f65152016-08-17 13:14:53 -070017
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
Madan Jampani03fb8b22016-08-22 09:08:42 -070020import java.util.concurrent.Executor;
Madan Jampani13f65152016-08-17 13:14:53 -070021import java.util.function.Consumer;
22
Jordan Halterman00e92da2018-05-22 23:05:52 -070023import com.google.common.collect.Maps;
24import io.atomix.core.value.AsyncAtomicValue;
25import io.atomix.core.value.AtomicValueEventListener;
Madan Jampani13f65152016-08-17 13:14:53 -070026import org.onosproject.store.service.DistributedPrimitive;
27import org.onosproject.store.service.Topic;
28
Jordan Halterman6cf60c32018-08-15 01:22:51 -070029import static org.onosproject.store.atomix.primitives.impl.AtomixFutures.adaptFuture;
30
Madan Jampani13f65152016-08-17 13:14:53 -070031/**
32 * Default implementation of {@link Topic}.
33 *
34 * @param <T> topic message type.
35 */
Jordan Halterman00e92da2018-05-22 23:05:52 -070036public class AtomixDistributedTopic<T> implements Topic<T> {
Madan Jampani13f65152016-08-17 13:14:53 -070037
Jordan Halterman00e92da2018-05-22 23:05:52 -070038 private final AsyncAtomicValue<T> atomixValue;
Madan Jampani13f65152016-08-17 13:14:53 -070039 private final Map<Consumer<T>, AtomicValueEventListener<T>> callbacks = Maps.newIdentityHashMap();
40
Jordan Halterman00e92da2018-05-22 23:05:52 -070041 AtomixDistributedTopic(AsyncAtomicValue<T> atomixValue) {
42 this.atomixValue = atomixValue;
Madan Jampani13f65152016-08-17 13:14:53 -070043 }
44
45 @Override
46 public String name() {
Jordan Halterman00e92da2018-05-22 23:05:52 -070047 return atomixValue.name();
Madan Jampani13f65152016-08-17 13:14:53 -070048 }
49
50 @Override
51 public Type primitiveType() {
52 return DistributedPrimitive.Type.TOPIC;
53 }
54
55 @Override
Madan Jampani13f65152016-08-17 13:14:53 -070056 public CompletableFuture<Void> publish(T message) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070057 return adaptFuture(atomixValue.set(message));
Madan Jampani13f65152016-08-17 13:14:53 -070058 }
59
60 @Override
Madan Jampani03fb8b22016-08-22 09:08:42 -070061 public CompletableFuture<Void> subscribe(Consumer<T> callback, Executor executor) {
62 AtomicValueEventListener<T> valueListener =
63 event -> executor.execute(() -> callback.accept(event.newValue()));
Madan Jampani13f65152016-08-17 13:14:53 -070064 if (callbacks.putIfAbsent(callback, valueListener) == null) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070065 return adaptFuture(atomixValue.addListener(valueListener));
Madan Jampani13f65152016-08-17 13:14:53 -070066 }
67 return CompletableFuture.completedFuture(null);
68 }
69
70 @Override
71 public CompletableFuture<Void> unsubscribe(Consumer<T> callback) {
72 AtomicValueEventListener<T> valueListener = callbacks.remove(callback);
73 if (valueListener != null) {
Jordan Halterman6cf60c32018-08-15 01:22:51 -070074 return adaptFuture(atomixValue.removeListener(valueListener));
Madan Jampani13f65152016-08-17 13:14:53 -070075 }
76 return CompletableFuture.completedFuture(null);
77 }
Jordan Halterman00e92da2018-05-22 23:05:52 -070078
79 @Override
80 public CompletableFuture<Void> destroy() {
81 return atomixValue.close();
82 }
Madan Jampani13f65152016-08-17 13:14:53 -070083}