blob: 836a6824cb936fbf670003362acd71ffdcce3298 [file] [log] [blame]
Jordan Halterman9bdc24f2017-04-19 23:45:12 -07001/*
2 * Copyright 2017-present 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.primitives.impl;
17
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20import java.util.concurrent.Executor;
21import java.util.function.Consumer;
22
23import com.google.common.collect.Maps;
24import org.onlab.util.Tools;
25import org.onosproject.store.service.DistributedPrimitive;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Base class for primitives that delegate asynchronous callbacks to a user provided {@link Executor}.
31 */
32public abstract class ExecutingDistributedPrimitive
33 extends DelegatingDistributedPrimitive {
34 private final DistributedPrimitive primitive;
Jordan Halterman046faeb2017-05-01 15:10:13 -070035 private final Executor orderedExecutor;
36 private final Executor threadPoolExecutor;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070037 private final Map<Consumer<Status>, Consumer<Status>> listenerMap = Maps.newConcurrentMap();
38
Jordan Halterman046faeb2017-05-01 15:10:13 -070039 protected ExecutingDistributedPrimitive(
40 DistributedPrimitive primitive, Executor orderedExecutor, Executor threadPoolExecutor) {
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070041 super(primitive);
42 this.primitive = primitive;
Jordan Halterman046faeb2017-05-01 15:10:13 -070043 this.orderedExecutor = checkNotNull(orderedExecutor);
44 this.threadPoolExecutor = checkNotNull(threadPoolExecutor);
45 }
46
47 /**
48 * Creates a future to be completed asynchronously on the provided ordered and thread pool executors.
49 *
50 * @param future the future to be completed asynchronously
51 * @param <T> future result type
52 * @return a new {@link CompletableFuture} to be completed asynchronously using the primitive thread model
53 */
54 protected <T> CompletableFuture<T> asyncFuture(CompletableFuture<T> future) {
55 return Tools.orderedFuture(future, orderedExecutor, threadPoolExecutor);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070056 }
57
58 @Override
59 public CompletableFuture<Void> destroy() {
Jordan Halterman046faeb2017-05-01 15:10:13 -070060 return asyncFuture(primitive.destroy());
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070061 }
62
63 @Override
64 public void addStatusChangeListener(Consumer<DistributedPrimitive.Status> listener) {
65 Consumer<DistributedPrimitive.Status> wrappedListener =
Jordan Halterman046faeb2017-05-01 15:10:13 -070066 status -> orderedExecutor.execute(() -> listener.accept(status));
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070067 listenerMap.put(listener, wrappedListener);
68 primitive.addStatusChangeListener(wrappedListener);
69 }
70
71 @Override
72 public void removeStatusChangeListener(Consumer<DistributedPrimitive.Status> listener) {
73 Consumer<DistributedPrimitive.Status> wrappedListener = listenerMap.remove(listener);
74 if (wrappedListener != null) {
75 primitive.removeStatusChangeListener(wrappedListener);
76 }
77 }
78}