VPP-330 Track pending map-requests with a fifo
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / CallbackJVppFacadeNotificationTest.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.WantInterfaceEventsCallback;
24 import org.openvpp.jvpp.callfacade.CallbackJVppFacade;
25 import org.openvpp.jvpp.dto.WantInterfaceEventsReply;
26
27 public class CallbackJVppFacadeNotificationTest {
28
29     private static void testCallbackFacade() throws Exception {
30         System.out.println("Testing CallbackJVppFacade for notifications");
31
32         JVpp jvpp = new JVppImpl(new VppJNIConnection("CallbackApiTest"));
33
34         CallbackJVppFacade jvppCallbackFacade = new CallbackJVppFacade(jvpp);
35         System.out.println("Successfully connected to VPP");
36
37         final AutoCloseable notificationListenerReg =
38             jvppCallbackFacade.getNotificationRegistry().registerSwInterfaceSetFlagsNotificationCallback(
39                 NotificationUtils::printNotification
40             );
41
42         jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getEnableInterfaceNotificationsReq(),
43             new WantInterfaceEventsCallback() {
44                 @Override
45                 public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) {
46                     System.out.println("Interface events started");
47                 }
48
49                 @Override
50                 public void onError(final VppCallbackException ex) {
51                     System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n",
52                         ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
53                 }
54             });
55
56         System.out.println("Changing interface configuration");
57         NotificationUtils.getChangeInterfaceState().send(jvpp);
58
59         Thread.sleep(1000);
60
61         jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getDisableInterfaceNotificationsReq(),
62             new WantInterfaceEventsCallback() {
63                 @Override
64                 public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) {
65                     System.out.println("Interface events stopped");
66                 }
67
68                 @Override
69                 public void onError(final VppCallbackException ex) {
70                     System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n",
71                         ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
72                 }
73             });
74
75         notificationListenerReg.close();
76
77         Thread.sleep(2000);
78
79         System.out.println("Disconnecting...");
80         jvpp.close();
81         Thread.sleep(1000);
82     }
83
84     public static void main(String[] args) throws Exception {
85         testCallbackFacade();
86     }
87 }