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.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;
31 public class FutureApiTest {
33 private static void testShowVersion(final FutureJVppFacade jvpp) {
34 System.out.println("Sending ShowVersion request...");
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");
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
55 private static void testGetNodeIndex(final FutureJVppFacade jvpp) {
56 System.out.println("Sending GetNodeIndex request...");
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");
73 private static void testSwInterfaceDump(final FutureJVppFacade jvpp) {
74 System.out.println("Sending SwInterfaceDump request...");
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);
91 } catch(NullPointerException e) {
92 throw new IllegalStateException(e.getMessage());
93 } catch (Exception e) {
94 System.err.printf("SwInterfaceDump request failed:\n");
99 private static void testFutureApi() throws Exception {
100 System.out.println("Testing Java future API");
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");
107 testShowVersion(jvppFacade);
108 testGetNodeIndex(jvppFacade);
109 testSwInterfaceDump(jvppFacade);
111 System.out.println("Disconnecting...");
112 // TODO we should consider adding jvpp.close(); to the facade
116 public static void main(String[] args) throws Exception {