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 io.fd.vpp.jvpp.core.test;
19 import io.fd.vpp.jvpp.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.core.JVppCoreImpl;
22 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
23 import io.fd.vpp.jvpp.core.dto.BridgeDomainDump;
24 import io.fd.vpp.jvpp.core.dto.GetNodeIndex;
25 import io.fd.vpp.jvpp.core.dto.GetNodeIndexReply;
26 import io.fd.vpp.jvpp.core.dto.ShowVersion;
27 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
30 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
32 import java.util.Objects;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.Future;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 public class FutureApiTest {
40 private static final Logger LOG = Logger.getLogger(FutureApiTest.class.getName());
42 private static void testShowVersion(final FutureJVppCoreFacade jvpp) throws Exception {
43 LOG.info("Sending ShowVersion request...");
44 final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
45 final ShowVersionReply reply = replyFuture.get();
48 "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n",
49 reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate),
50 new String(reply.buildDirectory)));
53 private static void testEmptyBridgeDomainDump(final FutureJVppCoreFacade jvpp) throws Exception {
54 LOG.info("Sending ShowVersion request...");
55 final BridgeDomainDump request = new BridgeDomainDump();
56 request.bdId = -1; // dump call
58 final CompletableFuture<BridgeDomainDetailsReplyDump>
59 replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
60 final BridgeDomainDetailsReplyDump reply = replyFuture.get();
62 if (reply == null || reply.bridgeDomainDetails == null) {
63 LOG.severe("Received null response for empty dump: " + reply);
67 "Received empty bridge-domain dump reply with list of bridge-domains: %s, %s",
68 reply.bridgeDomainDetails, reply.bridgeDomainSwIfDetails));
72 private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) {
73 LOG.info("Sending GetNodeIndex request...");
74 final GetNodeIndex request = new GetNodeIndex();
75 request.nodeName = "non-existing-node".getBytes();
76 final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
78 final GetNodeIndexReply reply = replyFuture.get();
81 "Received GetNodeIndexReply: context=%d, nodeIndex=%d%n", reply.context, reply.nodeIndex));
82 } catch (Exception e) {
83 LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
87 private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception {
88 LOG.info("Sending SwInterfaceDump request...");
89 final SwInterfaceDump request = new SwInterfaceDump();
90 request.nameFilterValid = 0;
91 request.nameFilter = "".getBytes();
93 final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
94 final SwInterfaceDetailsReplyDump reply = replyFuture.get();
95 for (SwInterfaceDetails details : reply.swInterfaceDetails) {
96 Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
98 String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
99 + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
100 new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
101 details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
105 private static void testFutureApi() throws Exception {
106 LOG.info("Testing Java future API");
107 try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
108 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
109 LOG.info("Successfully connected to VPP");
111 testEmptyBridgeDomainDump(jvppFacade);
112 testShowVersion(jvppFacade);
113 testGetNodeIndex(jvppFacade);
114 testSwInterfaceDump(jvppFacade);
116 LOG.info("Disconnecting...");
120 public static void main(String[] args) throws Exception {