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.core.test;
19 import java.util.Objects;
20 import java.util.concurrent.Future;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.openvpp.jvpp.JVpp;
24 import org.openvpp.jvpp.JVppRegistry;
25 import org.openvpp.jvpp.JVppRegistryImpl;
26 import org.openvpp.jvpp.core.JVppCoreImpl;
27 import org.openvpp.jvpp.core.dto.GetNodeIndex;
28 import org.openvpp.jvpp.core.dto.GetNodeIndexReply;
29 import org.openvpp.jvpp.core.dto.ShowVersion;
30 import org.openvpp.jvpp.core.dto.ShowVersionReply;
31 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
32 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
33 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
34 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
36 public class FutureApiTest {
38 private static final Logger LOG = Logger.getLogger(FutureApiTest.class.getName());
40 private static void testShowVersion(final FutureJVppCoreFacade jvpp) throws Exception {
41 LOG.info("Sending ShowVersion request...");
42 final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
43 final ShowVersionReply reply = replyFuture.get();
46 "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s\n",
47 reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate),
48 new String(reply.buildDirectory)));
51 private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) {
52 LOG.info("Sending GetNodeIndex request...");
53 final GetNodeIndex request = new GetNodeIndex();
54 request.nodeName = "non-existing-node".getBytes();
55 final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
57 final GetNodeIndexReply reply = replyFuture.get();
60 "Received GetNodeIndexReply: context=%d, nodeIndex=%d\n", reply.context, reply.nodeIndex));
61 } catch (Exception e) {
62 LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
66 private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception {
67 LOG.info("Sending SwInterfaceDump request...");
68 final SwInterfaceDump request = new SwInterfaceDump();
69 request.nameFilterValid = 0;
70 request.nameFilter = "".getBytes();
72 final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
73 final SwInterfaceDetailsReplyDump reply = replyFuture.get();
74 for (SwInterfaceDetails details : reply.swInterfaceDetails) {
75 Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
77 String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
78 + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n",
79 new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
80 details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
84 private static void testFutureApi() throws Exception {
85 LOG.info("Testing Java future API");
87 final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
88 final JVpp jvpp = new JVppCoreImpl();
89 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
90 LOG.info("Successfully connected to VPP");
92 testShowVersion(jvppFacade);
93 testGetNodeIndex(jvppFacade);
94 testSwInterfaceDump(jvppFacade);
96 LOG.info("Disconnecting...");
100 public static void main(String[] args) throws Exception {