20f8ae4dd8783d7bcde0e9e65953b5d53c84394b
[vpp.git] / vpp-api / java / jvpp-core / org / openvpp / jvpp / core / test / FutureApiNotificationTest.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.core.test;
18
19 import static org.openvpp.jvpp.core.test.NotificationUtils.getChangeInterfaceState;
20 import static org.openvpp.jvpp.core.test.NotificationUtils.getDisableInterfaceNotificationsReq;
21 import static org.openvpp.jvpp.core.test.NotificationUtils.getEnableInterfaceNotificationsReq;
22
23 import org.openvpp.jvpp.JVpp;
24 import org.openvpp.jvpp.JVppRegistry;
25 import org.openvpp.jvpp.JVppRegistryImpl;
26 import org.openvpp.jvpp.core.JVppCoreImpl;
27 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
28
29 public class FutureApiNotificationTest {
30
31     private static void testFutureApi() throws Exception {
32         System.out.println("Testing Java future API for notifications");
33
34         final JVppRegistry registry = new JVppRegistryImpl("FutureApiNotificationTest");
35         final JVpp jvpp = new JVppCoreImpl();
36         final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
37
38         System.out.println("Successfully connected to VPP");
39
40         final AutoCloseable notificationListenerReg =
41                 jvppFacade.getNotificationRegistry()
42                         .registerSwInterfaceSetFlagsNotificationCallback(NotificationUtils::printNotification);
43
44         jvppFacade.wantInterfaceEvents(getEnableInterfaceNotificationsReq()).toCompletableFuture().get();
45         System.out.println("Interface events started");
46
47         System.out.println("Changing interface configuration");
48         jvppFacade.swInterfaceSetFlags(getChangeInterfaceState()).toCompletableFuture().get();
49
50         Thread.sleep(1000);
51
52         jvppFacade.wantInterfaceEvents(getDisableInterfaceNotificationsReq()).toCompletableFuture().get();
53         System.out.println("Interface events stopped");
54
55         notificationListenerReg.close();
56
57         System.out.println("Disconnecting...");
58         registry.close();
59     }
60
61     public static void main(String[] args) throws Exception {
62         testFutureApi();
63     }
64 }