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