2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.vpp.jvpp.core.examples;
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;
35 public class CallbackApiExample {
37 public static void main(String[] args) throws Exception {
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());
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);
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);
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);
63 System.out.println("Disconnecting...");
68 static class TestCallback implements GetNodeIndexReplyCallback, ShowVersionReplyCallback, SwInterfaceDetailsCallback {
71 public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
72 System.out.printf("Received GetNodeIndexReply: %s%n", msg);
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",
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);
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());