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.examples;
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.nio.charset.StandardCharsets;
33 import java.util.Objects;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.Future;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 public class FutureApiExample {
41 private static final Logger LOG = Logger.getLogger(FutureApiExample.class.getName());
43 private static void testShowVersion(final FutureJVppCoreFacade jvpp) throws Exception {
44 LOG.info("Sending ShowVersion request...");
45 final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
46 final ShowVersionReply reply = replyFuture.get();
49 "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n",
50 reply.context, new String(reply.program, StandardCharsets.UTF_8),
51 new String(reply.version, StandardCharsets.UTF_8),
52 new String(reply.buildDate, StandardCharsets.UTF_8),
53 new String(reply.buildDirectory, StandardCharsets.UTF_8)));
56 private static void testEmptyBridgeDomainDump(final FutureJVppCoreFacade jvpp) throws Exception {
57 LOG.info("Sending ShowVersion request...");
58 final BridgeDomainDump request = new BridgeDomainDump();
59 request.bdId = -1; // dump call
61 final CompletableFuture<BridgeDomainDetailsReplyDump>
62 replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
63 final BridgeDomainDetailsReplyDump reply = replyFuture.get();
65 if (reply == null || reply.bridgeDomainDetails == null) {
66 LOG.severe("Received null response for empty dump: " + reply);
70 "Received bridge-domain dump reply with list of bridge-domains: %s",
71 reply.bridgeDomainDetails));
75 private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) {
76 LOG.info("Sending GetNodeIndex request...");
77 final GetNodeIndex request = new GetNodeIndex();
78 request.nodeName = "non-existing-node".getBytes(StandardCharsets.UTF_8);
79 final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
81 final GetNodeIndexReply reply = replyFuture.get();
84 "Received GetNodeIndexReply: context=%d, nodeIndex=%d%n", reply.context, reply.nodeIndex));
85 } catch (Exception e) {
86 LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
90 private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception {
91 LOG.info("Sending SwInterfaceDump request...");
92 final SwInterfaceDump request = new SwInterfaceDump();
93 request.nameFilterValid = 0;
94 request.nameFilter = "".getBytes(StandardCharsets.UTF_8);
96 final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
97 final SwInterfaceDetailsReplyDump reply = replyFuture.get();
98 for (SwInterfaceDetails details : reply.swInterfaceDetails) {
99 Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
101 String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
102 + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
103 new String(details.interfaceName, StandardCharsets.UTF_8),
104 details.l2AddressLength, details.adminUpDown,
105 details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
109 private static void testFutureApi() throws Exception {
110 LOG.info("Testing Java future API");
111 try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiExample");
112 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
113 LOG.info("Successfully connected to VPP");
115 testEmptyBridgeDomainDump(jvppFacade);
116 testShowVersion(jvppFacade);
117 testGetNodeIndex(jvppFacade);
118 testSwInterfaceDump(jvppFacade);
120 LOG.info("Disconnecting...");
124 public static void main(String[] args) throws Exception {