2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openvpp.jvpp.future;
21 import java.util.Objects;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.CompletionStage;
24 import org.openvpp.jvpp.JVpp;
25 import org.openvpp.jvpp.JVppRegistry;
26 import org.openvpp.jvpp.VppInvocationException;
27 import org.openvpp.jvpp.dto.JVppDump;
28 import org.openvpp.jvpp.dto.JVppReply;
29 import org.openvpp.jvpp.dto.JVppReplyDump;
30 import org.openvpp.jvpp.dto.JVppRequest;
33 * Future facade on top of JVpp
35 public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker {
37 private final JVpp jvpp;
38 private final JVppRegistry registry;
43 private final Map<Integer, CompletableFuture<? extends JVppReply<?>>> requests;
45 protected AbstractFutureJVppInvoker(final JVpp jvpp, final JVppRegistry registry,
46 final Map<Integer, CompletableFuture<? extends JVppReply<?>>> requestMap) {
47 this.jvpp = Objects.requireNonNull(jvpp, "jvpp should not be null");
48 this.registry = Objects.requireNonNull(registry, "registry should not be null");
49 // Request map represents the shared state between this facade and it's callback
50 // where facade puts futures in and callback completes + removes them
51 this.requests = Objects.requireNonNull(requestMap, "Null requestMap");
54 protected final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() {
58 // TODO use Optional in Future, java8
61 @SuppressWarnings("unchecked")
62 public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>> CompletionStage<REPLY> send(REQ req) {
63 synchronized(requests) {
65 final CompletableFuture<REPLY> replyCompletableFuture;
66 final int contextId = jvpp.send(req);
68 if(req instanceof JVppDump) {
69 replyCompletableFuture = (CompletableFuture<REPLY>) new CompletableDumpFuture<>(contextId);
71 replyCompletableFuture = new CompletableFuture<>();
74 requests.put(contextId, replyCompletableFuture);
75 if(req instanceof JVppDump) {
76 requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture);
79 // TODO in case of timeouts/missing replies, requests from the map are not removed
80 // consider adding cancel method, that would remove requests from the map and cancel
81 // associated replyCompletableFuture
83 return replyCompletableFuture;
84 } catch (VppInvocationException ex) {
85 final CompletableFuture<REPLY> replyCompletableFuture = new CompletableFuture<>();
86 replyCompletableFuture.completeExceptionally(ex);
87 return replyCompletableFuture;
92 public static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
93 // The reason why this is not final is the instantiation of ReplyDump DTOs
94 // Their instantiation must be generated, so currently the DTOs are created in callback and set when first dump reponses
95 // is handled in the callback.
97 private final long contextId;
99 public CompletableDumpFuture(final long contextId) {
100 this.contextId = contextId;
103 public long getContextId() {
107 public T getReplyDump() {
111 public void setReplyDump(final T replyDump) {
112 this.replyDump = replyDump;
117 public void close() throws Exception {