Repair Doxygen build infrastructure
[vpp.git] / vpp-api / java / jvpp-registry / io / fd / vpp / jvpp / future / AbstractFutureJVppInvoker.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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
17 package io.fd.vpp.jvpp.future;
18
19
20 import java.util.Map;
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;
31
32 /**
33  * Future facade on top of JVpp
34  */
35 public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker {
36
37     private final JVpp jvpp;
38     private final JVppRegistry registry;
39
40     /**
41      * Guarded by self
42      */
43     private final Map<Integer, CompletableFuture<? extends JVppReply<?>>> requests;
44
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");
52     }
53
54     protected final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() {
55         synchronized (requests) {
56             return requests;
57         }
58     }
59
60     // TODO use Optional in Future, java8
61
62     @Override
63     @SuppressWarnings("unchecked")
64     public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>> CompletionStage<REPLY> send(REQ req) {
65         synchronized(requests) {
66             try {
67                 final CompletableFuture<REPLY> replyCompletableFuture;
68                 final int contextId = jvpp.send(req);
69
70                 if(req instanceof JVppDump) {
71                     throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls");
72                 }
73                 replyCompletableFuture = new CompletableFuture<>();
74                 requests.put(contextId, replyCompletableFuture);
75
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
79
80                 return replyCompletableFuture;
81             } catch (VppInvocationException ex) {
82                 final CompletableFuture<REPLY> replyCompletableFuture = new CompletableFuture<>();
83                 replyCompletableFuture.completeExceptionally(ex);
84                 return replyCompletableFuture;
85             }
86         }
87     }
88
89     @Override
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) {
94             try {
95                 final CompletableDumpFuture<DUMP> replyCompletableFuture;
96                 final int contextId = jvpp.send(req);
97
98                 if(!(req instanceof JVppDump)) {
99                     throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls");
100                 }
101                 replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump);
102
103                 requests.put(contextId, replyCompletableFuture);
104                 requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture);
105
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
109
110                 return replyCompletableFuture;
111             } catch (VppInvocationException ex) {
112                 final CompletableFuture<DUMP> replyCompletableFuture = new CompletableFuture<>();
113                 replyCompletableFuture.completeExceptionally(ex);
114                 return replyCompletableFuture;
115             }
116         }
117     }
118
119     public static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
120         private final T replyDump;
121         private final int contextId;
122
123         public CompletableDumpFuture(final int contextId, final T emptyDump) {
124             this.contextId = contextId;
125             this.replyDump = emptyDump;
126         }
127
128         public int getContextId() {
129             return contextId;
130         }
131
132         public T getReplyDump() {
133             return replyDump;
134         }
135     }
136
137     @Override
138     public void close() throws Exception {
139         jvpp.close();
140     }
141 }