HONEYCOMB-67 Introduce exception handling into JVPP
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / CallbackApiTest.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.test;
18
19 import org.openvpp.jvpp.JVpp;
20 import org.openvpp.jvpp.JVppImpl;
21 import org.openvpp.jvpp.VppCallbackException;
22 import org.openvpp.jvpp.VppJNIConnection;
23 import org.openvpp.jvpp.callback.GetNodeIndexCallback;
24 import org.openvpp.jvpp.callback.ShowVersionCallback;
25 import org.openvpp.jvpp.callback.SwInterfaceCallback;
26 import org.openvpp.jvpp.dto.*;
27
28 public class CallbackApiTest {
29
30     private static class TestCallback implements GetNodeIndexCallback, ShowVersionCallback, SwInterfaceCallback {
31
32         @Override
33         public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
34             System.out.printf("Received GetNodeIndexReply: context=%d, nodeIndex=%d\n",
35                     msg.context, msg.nodeIndex);
36         }
37         @Override
38         public void onShowVersionReply(final ShowVersionReply msg)  {
39             System.out.printf("Received ShowVersionReply: context=%d, program=%s, version=%s, " +
40                     "buildDate=%s, buildDirectory=%s\n",
41                     msg.context, new String(msg.program), new String(msg.version),
42                     new String(msg.buildDate), new String(msg.buildDirectory));
43         }
44
45         @Override
46         public void onSwInterfaceDetails(final SwInterfaceDetails msg) {
47             System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " +
48                     "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n",
49                     new String(msg.interfaceName), msg.l2AddressLength, msg.adminUpDown,
50                     msg.linkUpDown, msg.linkSpeed, (int)msg.linkMtu);
51         }
52
53         @Override
54         public void onError(VppCallbackException ex) {
55             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n", ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
56         }
57     }
58
59     private static void testCallbackApi() throws Exception {
60         System.out.println("Testing Java callback API");
61         JVpp jvpp = new JVppImpl(new VppJNIConnection("CallbackApiTest"));
62         jvpp.connect(new TestCallback());
63         System.out.println("Successfully connected to VPP");
64
65         System.out.println("Sending ShowVersion request...");
66         jvpp.send(new ShowVersion());
67
68         System.out.println("Sending GetNodeIndex request...");
69         GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
70         getNodeIndexRequest.nodeName = "node0".getBytes();
71         jvpp.send(getNodeIndexRequest);
72
73         System.out.println("Sending SwInterfaceDump request...");
74         SwInterfaceDump swInterfaceDumpRequest = new SwInterfaceDump();
75         swInterfaceDumpRequest.nameFilterValid = 0;
76         swInterfaceDumpRequest.nameFilter = "".getBytes();
77         jvpp.send(swInterfaceDumpRequest);
78
79         Thread.sleep(5000);
80
81         System.out.println("Disconnecting...");
82         jvpp.close();
83         Thread.sleep(1000);
84     }
85
86     public static void main(String[] args) throws Exception {
87         testCallbackApi();
88     }
89 }