2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openvpp.jvpp.core.test;
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 import static org.openvpp.jvpp.core.test.NotificationUtils.printNotification;
24 import org.openvpp.jvpp.JVpp;
25 import org.openvpp.jvpp.JVppRegistry;
26 import org.openvpp.jvpp.JVppRegistryImpl;
27 import org.openvpp.jvpp.VppCallbackException;
28 import org.openvpp.jvpp.core.JVppCoreImpl;
29 import org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsCallback;
30 import org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback;
31 import org.openvpp.jvpp.core.callback.WantInterfaceEventsCallback;
32 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsNotification;
33 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
34 import org.openvpp.jvpp.core.dto.WantInterfaceEventsReply;
36 public class CallbackNotificationApiTest {
38 private static class TestCallback implements SwInterfaceSetFlagsNotificationCallback,
39 WantInterfaceEventsCallback, SwInterfaceSetFlagsCallback {
42 public void onSwInterfaceSetFlagsNotification(
43 final SwInterfaceSetFlagsNotification msg) {
44 printNotification(msg);
48 public void onWantInterfaceEventsReply(final WantInterfaceEventsReply wantInterfaceEventsReply) {
49 System.out.println("Interface notification stream updated");
53 public void onSwInterfaceSetFlagsReply(final SwInterfaceSetFlagsReply swInterfaceSetFlagsReply) {
54 System.out.println("Interface flags set successfully");
58 public void onError(VppCallbackException ex) {
59 System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n",
60 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
65 private static void testCallbackApi() throws Exception {
66 System.out.println("Testing Java callback API for notifications");
67 JVppRegistry registry = new JVppRegistryImpl("CallbackNotificationTest");
68 JVpp jvpp = new JVppCoreImpl();
70 registry.register(jvpp, new TestCallback());
71 System.out.println("Successfully connected to VPP");
73 getEnableInterfaceNotificationsReq().send(jvpp);
74 System.out.println("Interface notifications started");
75 // TODO test ifc dump which also triggers interface flags send
77 System.out.println("Changing interface configuration");
78 getChangeInterfaceState().send(jvpp);
80 // Notifications are received
83 getDisableInterfaceNotificationsReq().send(jvpp);
84 System.out.println("Interface events stopped");
88 System.out.println("Disconnecting...");
93 public static void main(String[] args) throws Exception {