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