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() {
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 throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls");
71 replyCompletableFuture = new CompletableFuture<>();
72 requests.put(contextId, replyCompletableFuture);
74 // TODO in case of timeouts/missing replies, requests from the map are not removed
75 // consider adding cancel method, that would remove requests from the map and cancel
76 // associated replyCompletableFuture
78 return replyCompletableFuture;
79 } catch (VppInvocationException ex) {
80 final CompletableFuture<REPLY> replyCompletableFuture = new CompletableFuture<>();
81 replyCompletableFuture.completeExceptionally(ex);
82 return replyCompletableFuture;
88 @SuppressWarnings("unchecked")
89 public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>, DUMP extends JVppReplyDump<REQ, REPLY>> CompletionStage<DUMP> send(
90 REQ req, DUMP emptyReplyDump) {
91 synchronized(requests) {
93 final CompletableDumpFuture<DUMP> replyCompletableFuture;
94 final int contextId = jvpp.send(req);
96 if(!(req instanceof JVppDump)) {
97 throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls");
99 replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump);
101 requests.put(contextId, replyCompletableFuture);
102 requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture);
104 // TODO in case of timeouts/missing replies, requests from the map are not removed
105 // consider adding cancel method, that would remove requests from the map and cancel
106 // associated replyCompletableFuture
108 return replyCompletableFuture;
109 } catch (VppInvocationException ex) {
110 final CompletableFuture<DUMP> replyCompletableFuture = new CompletableFuture<>();
111 replyCompletableFuture.completeExceptionally(ex);
112 return replyCompletableFuture;
117 public static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
118 private final T replyDump;
119 private final int contextId;
121 public CompletableDumpFuture(final int contextId, final T emptyDump) {
122 this.contextId = contextId;
123 this.replyDump = emptyDump;
126 public int getContextId() {
130 public T getReplyDump() {
136 public void close() throws Exception {