53a445e697706c80f37b2fd3d81a5a227c6cda68
[vpp.git] / vpp-api / java / jvpp-registry / org / openvpp / 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 org.openvpp.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 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;
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         return this.requests;
56     }
57
58     // TODO use Optional in Future, java8
59
60     @Override
61     @SuppressWarnings("unchecked")
62     public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>> CompletionStage<REPLY> send(REQ req) {
63         synchronized(requests) {
64             try {
65                 final CompletableFuture<REPLY> replyCompletableFuture;
66                 final int contextId = jvpp.send(req);
67
68                 if(req instanceof JVppDump) {
69                     throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls");
70                 }
71                 replyCompletableFuture = new CompletableFuture<>();
72                 requests.put(contextId, replyCompletableFuture);
73
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
77
78                 return replyCompletableFuture;
79             } catch (VppInvocationException ex) {
80                 final CompletableFuture<REPLY> replyCompletableFuture = new CompletableFuture<>();
81                 replyCompletableFuture.completeExceptionally(ex);
82                 return replyCompletableFuture;
83             }
84         }
85     }
86
87     @Override
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) {
92             try {
93                 final CompletableDumpFuture<DUMP> replyCompletableFuture;
94                 final int contextId = jvpp.send(req);
95
96                 if(!(req instanceof JVppDump)) {
97                     throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls");
98                 }
99                 replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump);
100
101                 requests.put(contextId, replyCompletableFuture);
102                 requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture);
103
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
107
108                 return replyCompletableFuture;
109             } catch (VppInvocationException ex) {
110                 final CompletableFuture<DUMP> replyCompletableFuture = new CompletableFuture<>();
111                 replyCompletableFuture.completeExceptionally(ex);
112                 return replyCompletableFuture;
113             }
114         }
115     }
116
117     public static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
118         private final T replyDump;
119         private final long contextId;
120
121         public CompletableDumpFuture(final long contextId, final T emptyDump) {
122             this.contextId = contextId;
123             this.replyDump = emptyDump;
124         }
125
126         public long getContextId() {
127             return contextId;
128         }
129
130         public T getReplyDump() {
131             return replyDump;
132         }
133     }
134
135     @Override
136     public void close() throws Exception {
137         jvpp.close();
138     }
139 }