API: Use string type instead of u8.
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackApiExample.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.JVpp;
20 import io.fd.vpp.jvpp.JVppRegistry;
21 import io.fd.vpp.jvpp.JVppRegistryImpl;
22 import io.fd.vpp.jvpp.VppCallbackException;
23 import io.fd.vpp.jvpp.core.JVppCoreImpl;
24 import io.fd.vpp.jvpp.core.callback.GetNodeIndexReplyCallback;
25 import io.fd.vpp.jvpp.core.callback.ShowVersionReplyCallback;
26 import io.fd.vpp.jvpp.core.callback.SwInterfaceDetailsCallback;
27 import io.fd.vpp.jvpp.core.dto.GetNodeIndex;
28 import io.fd.vpp.jvpp.core.dto.GetNodeIndexReply;
29 import io.fd.vpp.jvpp.core.dto.ShowVersion;
30 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
31 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
32 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
33 import java.nio.charset.StandardCharsets;
34
35 public class CallbackApiExample {
36
37     public static void main(String[] args) throws Exception {
38         testCallbackApi();
39     }
40
41     private static void testCallbackApi() throws Exception {
42         System.out.println("Testing Java callback API with JVppRegistry");
43         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackApiExample");
44              final JVpp jvpp = new JVppCoreImpl()) {
45             registry.register(jvpp, new TestCallback());
46
47             System.out.println("Sending ShowVersion request...");
48             final int result = jvpp.send(new ShowVersion());
49             System.out.printf("ShowVersion send result = %d%n", result);
50
51             System.out.println("Sending GetNodeIndex request...");
52             GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
53             getNodeIndexRequest.nodeName = "non-existing-node".getBytes(StandardCharsets.UTF_8);
54             jvpp.send(getNodeIndexRequest);
55
56             System.out.println("Sending SwInterfaceDump request...");
57             SwInterfaceDump swInterfaceDumpRequest = new SwInterfaceDump();
58             swInterfaceDumpRequest.nameFilterValid = 0;
59             swInterfaceDumpRequest.nameFilter = "".getBytes(StandardCharsets.UTF_8);
60             jvpp.send(swInterfaceDumpRequest);
61
62             Thread.sleep(1000);
63             System.out.println("Disconnecting...");
64         }
65         Thread.sleep(1000);
66     }
67
68     static class TestCallback implements GetNodeIndexReplyCallback, ShowVersionReplyCallback, SwInterfaceDetailsCallback {
69
70         @Override
71         public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
72             System.out.printf("Received GetNodeIndexReply: %s%n", msg);
73         }
74
75         @Override
76         public void onShowVersionReply(final ShowVersionReply msg) {
77             System.out.printf("Received ShowVersionReply: context=%d, program=%s, version=%s, "
78                     + "buildDate=%s, buildDirectory=%s%n",
79                 msg.context,
80                 msg.program,
81                 msg.version,
82                 msg.buildDate,
83                 msg.buildDirectory);
84         }
85
86         @Override
87         public void onSwInterfaceDetails(final SwInterfaceDetails msg) {
88             System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, "
89                     + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n",
90                 msg.interfaceName, msg.l2AddressLength, msg.adminUpDown,
91                 msg.linkUpDown, msg.linkSpeed, (int) msg.linkMtu);
92         }
93
94         @Override
95         public void onError(VppCallbackException ex) {
96             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
97                 ex.getCtxId(), ex.getErrorCode());
98         }
99     }
100 }