Fix coverity issues in jvpp (newlines + CompletableDumpFuture.ctxId)
[vpp.git] / 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 java.util.Objects;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.Future;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import io.fd.vpp.jvpp.JVpp;
25 import io.fd.vpp.jvpp.JVppRegistry;
26 import io.fd.vpp.jvpp.JVppRegistryImpl;
27 import io.fd.vpp.jvpp.core.JVppCoreImpl;
28 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.BridgeDomainDump;
30 import io.fd.vpp.jvpp.core.dto.GetNodeIndex;
31 import io.fd.vpp.jvpp.core.dto.GetNodeIndexReply;
32 import io.fd.vpp.jvpp.core.dto.ShowVersion;
33 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
34 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
35 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
36 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
37 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
38
39 public class FutureApiTest {
40
41     private static final Logger LOG = Logger.getLogger(FutureApiTest.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, new String(reply.program), new String(reply.version), new String(reply.buildDate),
51                         new String(reply.buildDirectory)));
52     }
53
54     private static void testEmptyBridgeDomainDump(final FutureJVppCoreFacade jvpp) throws Exception {
55         LOG.info("Sending ShowVersion request...");
56         final BridgeDomainDump request = new BridgeDomainDump();
57         request.bdId = -1; // dump call
58
59         final CompletableFuture<BridgeDomainDetailsReplyDump>
60                 replyFuture = jvpp.bridgeDomainDump(request).toCompletableFuture();
61         final BridgeDomainDetailsReplyDump reply = replyFuture.get();
62
63         if (reply == null || reply.bridgeDomainDetails == null) {
64             LOG.severe("Received null response for empty dump: " + reply);
65         } else {
66             LOG.info(
67                     String.format(
68                             "Received empty bridge-domain dump reply with list of bridge-domains: %s, %s",
69                             reply.bridgeDomainDetails, reply.bridgeDomainSwIfDetails));
70         }
71     }
72
73     private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) {
74         LOG.info("Sending GetNodeIndex request...");
75         final GetNodeIndex request = new GetNodeIndex();
76         request.nodeName = "non-existing-node".getBytes();
77         final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
78         try {
79             final GetNodeIndexReply reply = replyFuture.get();
80             LOG.info(
81                     String.format(
82                             "Received GetNodeIndexReply: context=%d, nodeIndex=%d%n", reply.context, reply.nodeIndex));
83         } catch (Exception e) {
84             LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
85         }
86     }
87
88     private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception {
89         LOG.info("Sending SwInterfaceDump request...");
90         final SwInterfaceDump request = new SwInterfaceDump();
91         request.nameFilterValid = 0;
92         request.nameFilter = "".getBytes();
93
94         final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
95         final SwInterfaceDetailsReplyDump reply = replyFuture.get();
96         for (SwInterfaceDetails details : reply.swInterfaceDetails) {
97             Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
98             LOG.info(
99                     String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
100                                     + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
101                             new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
102                             details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
103         }
104     }
105
106     private static void testFutureApi() throws Exception {
107         LOG.info("Testing Java future API");
108
109         final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
110         final JVpp jvpp = new JVppCoreImpl();
111         final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
112         LOG.info("Successfully connected to VPP");
113
114         testEmptyBridgeDomainDump(jvppFacade);
115         testShowVersion(jvppFacade);
116         testGetNodeIndex(jvppFacade);
117         testSwInterfaceDump(jvppFacade);
118
119         LOG.info("Disconnecting...");
120         registry.close();
121     }
122
123     public static void main(String[] args) throws Exception {
124         testFutureApi();
125     }
126 }