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 io.fd.vpp.jvpp.future;
21 import java.util.Objects;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.CompletionStage;
24 import io.fd.vpp.jvpp.JVpp;
25 import io.fd.vpp.jvpp.JVppRegistry;
26 import io.fd.vpp.jvpp.VppInvocationException;
27 import io.fd.vpp.jvpp.dto.JVppDump;
28 import io.fd.vpp.jvpp.dto.JVppReply;
29 import io.fd.vpp.jvpp.dto.JVppReplyDump;
30 import io.fd.vpp.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() {
55 synchronized (requests) {
60 // TODO use Optional in Future, java8
63 @SuppressWarnings("unchecked")
64 public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>> CompletionStage<REPLY> send(REQ req) {
65 synchronized(requests) {
67 final CompletableFuture<REPLY> replyCompletableFuture;
68 final int contextId = jvpp.send(req);
70 if(req instanceof JVppDump) {
71 throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls");
73 replyCompletableFuture = new CompletableFuture<>();
74 requests.put(contextId, replyCompletableFuture);
76 // TODO in case of timeouts/missing replies, requests from the map are not removed
77 // consider adding cancel method, that would remove requests from the map and cancel
78 // associated replyCompletableFuture
80 return replyCompletableFuture;
81 } catch (VppInvocationException ex) {
82 final CompletableFuture<REPLY> replyCompletableFuture = new CompletableFuture<>();
83 replyCompletableFuture.completeExceptionally(ex);
84 return replyCompletableFuture;
90 @SuppressWarnings("unchecked")
91 public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>, DUMP extends JVppReplyDump<REQ, REPLY>> CompletionStage<DUMP> send(
92 REQ req, DUMP emptyReplyDump) {
93 synchronized(requests) {
95 final CompletableDumpFuture<DUMP> replyCompletableFuture;
96 final int contextId = jvpp.send(req);
98 if(!(req instanceof JVppDump)) {
99 throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls");
101 replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump);
103 requests.put(contextId, replyCompletableFuture);
104 requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture);
106 // TODO in case of timeouts/missing replies, requests from the map are not removed
107 // consider adding cancel method, that would remove requests from the map and cancel
108 // associated replyCompletableFuture
110 return replyCompletableFuture;
111 } catch (VppInvocationException ex) {
112 final CompletableFuture<DUMP> replyCompletableFuture = new CompletableFuture<>();
113 replyCompletableFuture.completeExceptionally(ex);
114 return replyCompletableFuture;
119 public static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
120 private final T replyDump;
121 private final int contextId;
123 public CompletableDumpFuture(final int contextId, final T emptyDump) {
124 this.contextId = contextId;
125 this.replyDump = emptyDump;
128 public int getContextId() {
132 public T getReplyDump() {
138 public void close() throws Exception {