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