VPP-205: jvpp plugin support.
[vpp.git] / vpp-api / java / jvpp-core / org / openvpp / 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 org.openvpp.jvpp.core.test;
18
19 import java.util.Objects;
20 import java.util.concurrent.Future;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.openvpp.jvpp.JVpp;
24 import org.openvpp.jvpp.JVppRegistry;
25 import org.openvpp.jvpp.JVppRegistryImpl;
26 import org.openvpp.jvpp.core.JVppCoreImpl;
27 import org.openvpp.jvpp.core.dto.GetNodeIndex;
28 import org.openvpp.jvpp.core.dto.GetNodeIndexReply;
29 import org.openvpp.jvpp.core.dto.ShowVersion;
30 import org.openvpp.jvpp.core.dto.ShowVersionReply;
31 import org.openvpp.jvpp.core.dto.SwInterfaceDetails;
32 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
33 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
34 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
35
36 public class FutureApiTest {
37
38     private static final Logger LOG = Logger.getLogger(FutureApiTest.class.getName());
39
40     private static void testShowVersion(final FutureJVppCoreFacade jvpp) throws Exception {
41         LOG.info("Sending ShowVersion request...");
42         final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture();
43         final ShowVersionReply reply = replyFuture.get();
44         LOG.info(
45             String.format(
46                 "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s\n",
47                 reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate),
48                 new String(reply.buildDirectory)));
49     }
50
51     private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) {
52         LOG.info("Sending GetNodeIndex request...");
53         final GetNodeIndex request = new GetNodeIndex();
54         request.nodeName = "non-existing-node".getBytes();
55         final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture();
56         try {
57             final GetNodeIndexReply reply = replyFuture.get();
58             LOG.info(
59                 String.format(
60                     "Received GetNodeIndexReply: context=%d, nodeIndex=%d\n", reply.context, reply.nodeIndex));
61         } catch (Exception e) {
62             LOG.log(Level.SEVERE, "GetNodeIndex request failed", e);
63         }
64     }
65
66     private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception {
67         LOG.info("Sending SwInterfaceDump request...");
68         final SwInterfaceDump request = new SwInterfaceDump();
69         request.nameFilterValid = 0;
70         request.nameFilter = "".getBytes();
71
72         final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture();
73         final SwInterfaceDetailsReplyDump reply = replyFuture.get();
74         for (SwInterfaceDetails details : reply.swInterfaceDetails) {
75             Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!");
76             LOG.info(
77                 String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
78                         + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n",
79                     new String(details.interfaceName), details.l2AddressLength, details.adminUpDown,
80                     details.linkUpDown, details.linkSpeed, (int) details.linkMtu));
81         }
82     }
83
84     private static void testFutureApi() throws Exception {
85         LOG.info("Testing Java future API");
86
87         final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest");
88         final JVpp jvpp = new JVppCoreImpl();
89         final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
90         LOG.info("Successfully connected to VPP");
91
92         testShowVersion(jvppFacade);
93         testGetNodeIndex(jvppFacade);
94         testSwInterfaceDump(jvppFacade);
95
96         LOG.info("Disconnecting...");
97         registry.close();
98     }
99
100     public static void main(String[] args) throws Exception {
101         testFutureApi();
102     }
103 }