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