Repair Doxygen build infrastructure
[vpp.git] / src / vpp-api / java / jvpp-core / io / fd / vpp / jvpp / core / test / FutureApiTest.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.test;
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.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;
37
38 public class FutureApiTest {
39
40     private static final Logger LOG = Logger.getLogger(FutureApiTest.class.getName());
41
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();
46         LOG.info(
47             String.format(
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)));
51     }
52
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
57
58         final CompletableFuture<BridgeDomainDetailsReplyDump>
59             replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
60         final BridgeDomainDetailsReplyDump reply = replyFuture.get();
61
62         if (reply == null || reply.bridgeDomainDetails == null) {
63             LOG.severe("Received null response for empty dump: " + reply);
64         } else {
65             LOG.info(
66                 String.format(
67                     "Received empty bridge-domain dump reply with list of bridge-domains: %s, %s",
68                     reply.bridgeDomainDetails, reply.bridgeDomainSwIfDetails));
69         }
70     }
71
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();
77         try {
78             final GetNodeIndexReply reply = replyFuture.get();
79             LOG.info(
80                 String.format(
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);
84         }
85     }
86
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();
92
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!");
97             LOG.info(
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));
102         }
103     }
104
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");
110
111             testEmptyBridgeDomainDump(jvppFacade);
112             testShowVersion(jvppFacade);
113             testGetNodeIndex(jvppFacade);
114             testSwInterfaceDump(jvppFacade);
115
116             LOG.info("Disconnecting...");
117         }
118     }
119
120     public static void main(String[] args) throws Exception {
121         testFutureApi();
122     }
123 }