HONEYCOMB-10: Add specific methods for each request to Future facade
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / FutureApiTest.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.test;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.concurrent.Future;
23 import org.openvpp.jvpp.VppJNIConnection;
24 import org.openvpp.jvpp.dto.GetNodeIndex;
25 import org.openvpp.jvpp.dto.GetNodeIndexReply;
26 import org.openvpp.jvpp.dto.JVppReply;
27 import org.openvpp.jvpp.dto.ShowVersion;
28 import org.openvpp.jvpp.dto.ShowVersionReply;
29 import org.openvpp.jvpp.dto.SwInterfaceDetails;
30 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
31 import org.openvpp.jvpp.dto.SwInterfaceDump;
32 import org.openvpp.jvpp.future.FutureJVppFacade;
33 import org.openvpp.jvpp.future.FutureJVppFacadeCallback;
34
35 public class FutureApiTest {
36
37     private static void testShowVersion(final FutureJVppFacade jvpp) {
38         System.out.println("Sending ShowVersion request...");
39         try {
40             final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
41             final ShowVersionReply reply = replyFuture.get();
42             System.out.printf("Received ShowVersionReply: context=%d, retval=%d, program=%s, " +
43                             "version=%s, buildDate=%s, buildDirectory=%s\n",
44                     reply.context, reply.retval, new String(reply.program), new String(reply.version),
45                     new String(reply.buildDate), new String(reply.buildDirectory));
46         } catch (Exception e) {
47             System.err.printf("ShowVersion request failed:\n");
48             e.printStackTrace();
49         }
50     }
51
52     /**
53      * This test will fail with some error code if node 'node0' is not defined.
54      * TODO: consider adding error messages specific for given api calls
55      */
56     private static void testGetNodeIndex(final FutureJVppFacade jvpp) {
57         System.out.println("Sending GetNodeIndex request...");
58         try {
59             final GetNodeIndex request = new GetNodeIndex();
60             request.nodeName = "node0".getBytes();
61             final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
62             final GetNodeIndexReply reply = replyFuture.get();
63             System.out.printf("Received GetNodeIndexReply: context=%d, retval=%d, nodeIndex=%d\n",
64                     reply.context, reply.retval, reply.nodeIndex);
65         } catch (Exception e) {
66             System.err.printf("GetNodeIndex request failed:\n");
67             e.printStackTrace();
68         }
69     }
70
71     private static void testSwInterfaceDump(final FutureJVppFacade jvpp) {
72         System.out.println("Sending SwInterfaceDump request...");
73         try {
74             final SwInterfaceDump request = new SwInterfaceDump();
75             request.nameFilterValid = 0;
76             request.nameFilter = "".getBytes();
77             final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
78             final SwInterfaceDetailsReplyDump reply = replyFuture.get();
79
80             if (reply == null) {
81                 throw new IllegalStateException("SwInterfaceDetailsReplyDump is null!");
82             }
83             if (reply.swInterfaceDetails == null) {
84                 throw new IllegalStateException("SwInterfaceDetailsReplyDump.swInterfaceDetails is null!");
85             }
86
87             for (SwInterfaceDetails details : reply.swInterfaceDetails) {
88                 if (details == null) {
89                     throw new IllegalStateException("reply.swInterfaceDetails contains null element!");
90                 }
91
92                 System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " +
93                                 "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n",
94                         new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
95                         details.linkUpDown, details.linkSpeed, (int) details.linkMtu);
96             }
97         } catch (Exception e) {
98             System.err.printf("SwInterfaceDump request failed:\n");
99             e.printStackTrace();
100         }
101     }
102
103     private static void testFutureApi() throws Exception {
104         System.out.println("Testing Java future API");
105
106         final Map<Integer, CompletableFuture<? extends JVppReply<?>>>  map = new HashMap<>();
107         final org.openvpp.jvpp.JVppImpl impl =
108                 new org.openvpp.jvpp.JVppImpl(VppJNIConnection.create("FutureApiTest", new FutureJVppFacadeCallback(map)));
109         final FutureJVppFacade jvppFacade = new FutureJVppFacade(impl, map);
110         System.out.println("Successfully connected to VPP");
111
112         testShowVersion(jvppFacade);
113         testGetNodeIndex(jvppFacade);
114         testSwInterfaceDump(jvppFacade);
115
116         System.out.println("Disconnecting...");
117         // TODO we should consider adding jvpp.close(); to the facade
118         impl.close();
119     }
120
121     public static void main(String[] args) throws Exception {
122         testFutureApi();
123     }
124 }