Changed JVPP interface for construction and connectivity
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / future / FutureJVppInvokerFacade.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.dto.ControlPing;
26 import org.openvpp.jvpp.dto.JVppDump;
27 import org.openvpp.jvpp.dto.JVppReply;
28 import org.openvpp.jvpp.dto.JVppReplyDump;
29 import org.openvpp.jvpp.dto.JVppRequest;
30
31 /**
32 * Future facade on top of JVpp
33 */
34 public class FutureJVppInvokerFacade implements FutureJVppInvoker {
35
36     private final JVpp jvpp;
37
38     /**
39      * Guarded by self
40      */
41     private final Map<Integer, CompletableFuture<? extends JVppReply<?>>> requests;
42
43     public FutureJVppInvokerFacade(final JVpp jvpp,
44                      final Map<Integer, CompletableFuture<? extends JVppReply<?>>> requestMap) {
45         this.jvpp =  Objects.requireNonNull(jvpp, "Null jvpp");
46         // Request map represents the shared state between this facade and it's callback
47         // where facade puts futures in and callback completes + removes them
48         // TODO what if the call never completes ?
49         this.requests = Objects.requireNonNull(requestMap, "Null requestMap");
50     }
51
52     protected final Map<Integer, CompletableFuture<? extends JVppReply<?>>> getRequests() {
53         return this.requests;
54     }
55
56     // TODO use Optional in Future, java8
57
58     @Override
59     @SuppressWarnings("unchecked")
60     public <REQ extends JVppRequest, REPLY extends JVppReply<REQ>> CompletionStage<REPLY> send(REQ req) {
61         synchronized(requests) {
62             final int contextId = jvpp.send(req);
63
64             final CompletableFuture<REPLY> replyCompletableFuture;
65             if(req instanceof JVppDump) {
66                 replyCompletableFuture = (CompletableFuture<REPLY>) new CompletableDumpFuture<>(contextId);
67             } else {
68                 replyCompletableFuture = new CompletableFuture<>();
69             }
70
71             requests.put(contextId, replyCompletableFuture);
72             if(req instanceof JVppDump) {
73                 requests.put(jvpp.send(new ControlPing()), replyCompletableFuture);
74             }
75             return replyCompletableFuture;
76         }
77     }
78
79     static final class CompletableDumpFuture<T extends JVppReplyDump<?, ?>> extends CompletableFuture<T> {
80         // The reason why this is not final is the instantiation of ReplyDump DTOs
81         // Their instantiation must be generated, so currently the DTOs are created in callback and set when first dump reponses
82         // is handled in the callback.
83         private T replyDump;
84         private final long contextId;
85
86         CompletableDumpFuture(final long contextId) {
87             this.contextId = contextId;
88         }
89
90         long getContextId() {
91             return contextId;
92         }
93
94         T getReplyDump() {
95             return replyDump;
96         }
97
98         void setReplyDump(final T replyDump) {
99             this.replyDump = replyDump;
100         }
101     }
102
103     @Override
104     public void close() throws Exception {
105         // NOOP
106     }
107 }