Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.Executor; |
| 21 | import java.util.function.Consumer; |
| 22 | |
| 23 | import com.google.common.collect.Maps; |
| 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.store.service.DistributedPrimitive; |
| 26 | |
| 27 | import 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 | */ |
| 32 | public abstract class ExecutingDistributedPrimitive |
| 33 | extends DelegatingDistributedPrimitive { |
| 34 | private final DistributedPrimitive primitive; |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 35 | private final Executor orderedExecutor; |
| 36 | private final Executor threadPoolExecutor; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 37 | private final Map<Consumer<Status>, Consumer<Status>> listenerMap = Maps.newConcurrentMap(); |
| 38 | |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 39 | protected ExecutingDistributedPrimitive( |
| 40 | DistributedPrimitive primitive, Executor orderedExecutor, Executor threadPoolExecutor) { |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 41 | super(primitive); |
| 42 | this.primitive = primitive; |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 43 | 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 Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public CompletableFuture<Void> destroy() { |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 60 | return asyncFuture(primitive.destroy()); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void addStatusChangeListener(Consumer<DistributedPrimitive.Status> listener) { |
| 65 | Consumer<DistributedPrimitive.Status> wrappedListener = |
Jordan Halterman | 046faeb | 2017-05-01 15:10:13 -0700 | [diff] [blame] | 66 | status -> orderedExecutor.execute(() -> listener.accept(status)); |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 67 | 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 | } |