5bf2b212f73e6e34f1003f4688c564be787c0d51
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / CallbackNotificationApiTest.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 static org.openvpp.jvpp.test.NotificationUtils.getChangeInterfaceState;
20 import static org.openvpp.jvpp.test.NotificationUtils.getDisableInterfaceNotificationsReq;
21 import static org.openvpp.jvpp.test.NotificationUtils.getEnableInterfaceNotificationsReq;
22 import static org.openvpp.jvpp.test.NotificationUtils.printNotification;
23
24 import org.openvpp.jvpp.JVpp;
25 import org.openvpp.jvpp.JVppImpl;
26 import org.openvpp.jvpp.VppCallbackException;
27 import org.openvpp.jvpp.VppJNIConnection;
28 import org.openvpp.jvpp.callback.SwInterfaceSetFlagsCallback;
29 import org.openvpp.jvpp.callback.SwInterfaceSetFlagsNotificationCallback;
30 import org.openvpp.jvpp.callback.WantInterfaceEventsCallback;
31 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsNotification;
32 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsReply;
33 import org.openvpp.jvpp.dto.WantInterfaceEventsReply;
34
35 public class CallbackNotificationApiTest {
36
37     private static class TestCallback implements SwInterfaceSetFlagsNotificationCallback,
38         WantInterfaceEventsCallback, SwInterfaceSetFlagsCallback {
39
40         @Override
41         public void onSwInterfaceSetFlagsNotification(
42             final SwInterfaceSetFlagsNotification msg) {
43             printNotification(msg);
44         }
45
46         @Override
47         public void onWantInterfaceEventsReply(final WantInterfaceEventsReply wantInterfaceEventsReply) {
48             System.out.println("Interface notification stream updated");
49         }
50
51         @Override
52         public void onSwInterfaceSetFlagsReply(final SwInterfaceSetFlagsReply swInterfaceSetFlagsReply) {
53             System.out.println("Interface flags set successfully");
54         }
55
56         @Override
57         public void onError(VppCallbackException ex) {
58             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n",
59                 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
60
61         }
62     }
63
64     private static void testCallbackApi() throws Exception {
65         System.out.println("Testing Java callback API for notifications");
66         JVpp jvpp = new JVppImpl( new VppJNIConnection("CallbackApiTest"));
67         jvpp.connect( new TestCallback());
68         System.out.println("Successfully connected to VPP");
69
70         getEnableInterfaceNotificationsReq().send(jvpp);
71         System.out.println("Interface notifications started");
72         // TODO test ifc dump which also triggers interface flags send
73
74         System.out.println("Changing interface configuration");
75         getChangeInterfaceState().send(jvpp);
76
77         // Notification is received
78         Thread.sleep(500);
79
80         getDisableInterfaceNotificationsReq().send(jvpp);
81         System.out.println("Interface events stopped");
82
83         Thread.sleep(2000);
84
85         System.out.println("Disconnecting...");
86         jvpp.close();
87         Thread.sleep(1000);
88     }
89
90     public static void main(String[] args) throws Exception {
91         testCallbackApi();
92     }
93 }