docs: change code blocks from "shell" to "console"
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / FutureApiNotificationExample.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.examples;
18
19 import static io.fd.vpp.jvpp.core.examples.NotificationUtils.getChangeInterfaceState;
20 import static io.fd.vpp.jvpp.core.examples.NotificationUtils.getDisableInterfaceNotificationsReq;
21 import static io.fd.vpp.jvpp.core.examples.NotificationUtils.getEnableInterfaceNotificationsReq;
22
23 import io.fd.vpp.jvpp.JVppRegistry;
24 import io.fd.vpp.jvpp.JVppRegistryImpl;
25 import io.fd.vpp.jvpp.core.JVppCoreImpl;
26 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
27 import io.fd.vpp.jvpp.core.callback.SwInterfaceEventCallback;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceEvent;
29 import io.fd.vpp.jvpp.VppCallbackException;
30
31 public class FutureApiNotificationExample {
32
33     private static void testFutureApi() throws Exception {
34         System.out.println("Testing Java future API for notifications");
35         try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiNotificationExample");
36              final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl());
37              final AutoCloseable notificationListenerReg =
38                  jvppFacade.getEventRegistry()
39                      .registerSwInterfaceEventCallback(new SwInterfaceEventCallback() {
40                          public void onSwInterfaceEvent(SwInterfaceEvent reply) {
41                              System.out.printf("Received interface notification: ifc: %s%n", reply);
42                          }
43
44                          public void onError (VppCallbackException ex) {
45                              System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n",
46                                      ex.getMethodName(), ex.getCtxId(), ex.getErrorCode());
47                          }
48                      })) {
49             System.out.println("Successfully connected to VPP");
50             jvppFacade.wantInterfaceEvents(getEnableInterfaceNotificationsReq()).toCompletableFuture().get();
51             System.out.println("Interface events started");
52
53             System.out.println("Changing interface configuration");
54             jvppFacade.swInterfaceSetFlags(getChangeInterfaceState()).toCompletableFuture().get();
55
56             Thread.sleep(1000);
57
58             jvppFacade.wantInterfaceEvents(getDisableInterfaceNotificationsReq()).toCompletableFuture().get();
59             System.out.println("Interface events stopped");
60             System.out.println("Disconnecting...");
61         }
62     }
63
64     public static void main(String[] args) throws Exception {
65         testFutureApi();
66     }
67 }