VPP-330 Track pending map-requests with a fifo
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / 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 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.callfacade.CallbackJVppFacade;
26 import org.openvpp.jvpp.dto.GetNodeIndex;
27 import org.openvpp.jvpp.dto.GetNodeIndexReply;
28 import org.openvpp.jvpp.dto.ShowVersionReply;
29
30 /**
31  * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request.
32  * This is more convenient than the approach shown in CallbackApiTest.
33  */
34 public class CallbackJVppFacadeTest {
35
36     private static ShowVersionCallback showVersionCallback1;
37     private static ShowVersionCallback showVersionCallback2;
38     private static GetNodeIndexCallback getNodeIndexCallback;
39
40     static {
41         getNodeIndexCallback = new GetNodeIndexCallback() {
42             @Override
43             public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
44                 System.out.printf("Received GetNodeIndexReply: context=%d, nodeIndex=%d\n",
45                         msg.context, msg.nodeIndex);
46             }
47
48             @Override
49             public void onError(VppCallbackException ex) {
50                 System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n", ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
51             }
52         };
53         showVersionCallback2 = new ShowVersionCallback() {
54             @Override
55             public void onShowVersionReply(final ShowVersionReply msg) {
56                 System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s," +
57                                 "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program),
58                         new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
59             }
60
61             @Override
62             public void onError(VppCallbackException ex) {
63                 System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d\n", ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
64             }
65
66         };
67         showVersionCallback1 = new ShowVersionCallback() {
68             @Override
69             public void onShowVersionReply(final ShowVersionReply msg) {
70                 System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s," +
71                                 "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program),
72                         new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
73             }
74
75             @Override
76             public void onError(VppCallbackException ex) {
77                 System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d\n", ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
78             }
79         };
80     }
81
82     private static void testCallbackFacade() throws Exception {
83         System.out.println("Testing CallbackJVppFacade");
84
85         JVpp jvpp = new JVppImpl(new VppJNIConnection("CallbackApiTest"));
86
87         CallbackJVppFacade jvppCallbackFacade = new CallbackJVppFacade(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         jvpp.close();
101         Thread.sleep(1000);
102     }
103
104     public static void main(String[] args) throws Exception {
105         testCallbackFacade();
106     }
107 }