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