API: Use string type instead of u8.
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / FutureApiExample.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.vpp.jvpp.core.examples;
18
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;
38
39 public class FutureApiExample {
40
41     private static final Logger LOG = Logger.getLogger(FutureApiExample.class.getName());
42
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();
47         LOG.info(
48             String.format(
49                 "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n",
50                 reply.context, reply.program,
51                 reply.version,
52                 reply.buildDate,
53                 reply.buildDirectory));
54     }
55
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
60
61         final CompletableFuture<BridgeDomainDetailsReplyDump>
62             replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
63         final BridgeDomainDetailsReplyDump reply = replyFuture.get();
64
65         if (reply == null || reply.bridgeDomainDetails == null) {
66             LOG.severe("Received null response for empty dump: " + reply);
67         } else {
68             LOG.info(
69                 String.format(
70                     "Received bridge-domain dump reply with list of bridge-domains: %s",
71                     reply.bridgeDomainDetails));
72         }
73     }
74
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();
80         try {
81             final GetNodeIndexReply reply = replyFuture.get();
82             LOG.info(
83                 String.format(
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);
87         }
88     }
89
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);
95
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!");
100             LOG.info(
101                 String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
102                         + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
103                     details.interfaceName,
104                     details.l2AddressLength, details.adminUpDown,
105                     details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
106         }
107     }
108
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");
114
115             testEmptyBridgeDomainDump(jvppFacade);
116             testShowVersion(jvppFacade);
117             testGetNodeIndex(jvppFacade);
118             testSwInterfaceDump(jvppFacade);
119
120             LOG.info("Disconnecting...");
121         }
122     }
123
124     public static void main(String[] args) throws Exception {
125         testFutureApi();
126     }
127 }