2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openvpp.jvpp.test;
19 import java.util.HashMap;
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;
35 public class FutureApiTest {
37 private static void testShowVersion(final FutureJVppFacade jvpp) {
38 System.out.println("Sending ShowVersion request...");
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");
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
56 private static void testGetNodeIndex(final FutureJVppFacade jvpp) {
57 System.out.println("Sending GetNodeIndex request...");
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");
71 private static void testSwInterfaceDump(final FutureJVppFacade jvpp) {
72 System.out.println("Sending SwInterfaceDump request...");
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();
81 throw new IllegalStateException("SwInterfaceDetailsReplyDump is null!");
83 if (reply.swInterfaceDetails == null) {
84 throw new IllegalStateException("SwInterfaceDetailsReplyDump.swInterfaceDetails is null!");
87 for (SwInterfaceDetails details : reply.swInterfaceDetails) {
88 if (details == null) {
89 throw new IllegalStateException("reply.swInterfaceDetails contains null element!");
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);
97 } catch (Exception e) {
98 System.err.printf("SwInterfaceDump request failed:\n");
103 private static void testFutureApi() throws Exception {
104 System.out.println("Testing Java future API");
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");
112 testShowVersion(jvppFacade);
113 testGetNodeIndex(jvppFacade);
114 testSwInterfaceDump(jvppFacade);
116 System.out.println("Disconnecting...");
117 // TODO we should consider adding jvpp.close(); to the facade
121 public static void main(String[] args) throws Exception {