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 / CallbackJVppFacadeTest.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.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.VppCallbackException;
22 import io.fd.vpp.jvpp.core.JVppCore;
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.callfacade.CallbackJVppCoreFacade;
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.ShowVersionReply;
30
31 /**
32  * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request.
33  * This is more convenient than the approach shown in CallbackApiTest.
34  */
35 public class CallbackJVppFacadeTest {
36
37     private static ShowVersionCallback showVersionCallback1 = new ShowVersionCallback() {
38         @Override
39         public void onShowVersionReply(final ShowVersionReply msg) {
40             System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s,"
41                             + "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program),
42                     new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
43         }
44
45         @Override
46         public void onError(VppCallbackException ex) {
47             System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d\n",
48                     ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
49         }
50     };
51
52     private static ShowVersionCallback showVersionCallback2 = new ShowVersionCallback() {
53         @Override
54         public void onShowVersionReply(final ShowVersionReply msg) {
55             System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s,"
56                             + "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program),
57                     new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
58         }
59
60         @Override
61         public void onError(VppCallbackException ex) {
62             System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d\n",
63                     ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
64         }
65
66     };
67
68     private static GetNodeIndexCallback getNodeIndexCallback = new GetNodeIndexCallback() {
69         @Override
70         public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
71             System.out.printf("Received GetNodeIndexReply: %s\n", msg);
72         }
73
74         @Override
75         public void onError(VppCallbackException ex) {
76             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n",
77                     ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
78         }
79     };
80
81     private static void testCallbackFacade() throws Exception {
82         System.out.println("Testing CallbackJVppFacade");
83
84         final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
85         final JVppCore jvpp = new JVppCoreImpl();
86
87         CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp);
88         System.out.println("Successfully connected to VPP");
89
90         jvppCallbackFacade.showVersion(showVersionCallback1);
91         jvppCallbackFacade.showVersion(showVersionCallback2);
92
93         GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
94         getNodeIndexRequest.nodeName = "dummyNode".getBytes();
95         jvppCallbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback);
96
97         Thread.sleep(2000);
98
99         System.out.println("Disconnecting...");
100         registry.close();
101         Thread.sleep(1000);
102     }
103
104     public static void main(String[] args) throws Exception {
105         testCallbackFacade();
106     }
107 }