docs: change code blocks from "shell" to "console"
[vpp.git] / extras / japi / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackNotificationApiExample.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 import static io.fd.vpp.jvpp.core.examples.NotificationUtils.printNotification;
23
24 import io.fd.vpp.jvpp.JVpp;
25 import io.fd.vpp.jvpp.JVppRegistry;
26 import io.fd.vpp.jvpp.JVppRegistryImpl;
27 import io.fd.vpp.jvpp.VppCallbackException;
28 import io.fd.vpp.jvpp.core.JVppCoreImpl;
29 import io.fd.vpp.jvpp.core.callback.SwInterfaceEventCallback;
30 import io.fd.vpp.jvpp.core.callback.WantInterfaceEventsReplyCallback;
31 import io.fd.vpp.jvpp.core.dto.SwInterfaceEvent;
32 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
33 import io.fd.vpp.jvpp.core.dto.WantInterfaceEventsReply;
34
35 public class CallbackNotificationApiExample {
36
37     private static void testCallbackApi() throws Exception {
38         System.out.println("Testing Java callback API for notifications");
39         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackNotificationApiExample");
40              final JVpp jvpp = new JVppCoreImpl()) {
41             registry.register(jvpp, new TestCallback());
42             System.out.println("Successfully connected to VPP");
43
44             getEnableInterfaceNotificationsReq().send(jvpp);
45             System.out.println("Interface notifications started");
46             // TODO test ifc dump which also triggers interface flags send
47
48             System.out.println("Changing interface configuration");
49             getChangeInterfaceState().send(jvpp);
50
51             // Notifications are received
52             Thread.sleep(500);
53
54             getDisableInterfaceNotificationsReq().send(jvpp);
55             System.out.println("Interface events stopped");
56
57             Thread.sleep(2000);
58             System.out.println("Disconnecting...");
59         }
60         Thread.sleep(1000);
61     }
62
63     public static void main(String[] args) throws Exception {
64         testCallbackApi();
65     }
66
67     private static class TestCallback implements SwInterfaceEventCallback,
68             WantInterfaceEventsReplyCallback {
69
70         @Override
71         public void onSwInterfaceEvent(
72             final SwInterfaceEvent msg) {
73             printNotification(msg);
74         }
75
76         @Override
77         public void onWantInterfaceEventsReply(final WantInterfaceEventsReply wantInterfaceEventsReply) {
78             System.out.println("Interface notification stream updated");
79         }
80
81         @Override
82         public void onError(VppCallbackException ex) {
83             System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d%n",
84                 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
85
86         }
87     }
88 }