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 io.fd.vpp.jvpp.core.test;
19 import io.fd.vpp.jvpp.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.VppCallbackException;
22 import io.fd.vpp.jvpp.core.JVppCoreImpl;
23 import io.fd.vpp.jvpp.core.callback.GetNodeIndexCallback;
24 import io.fd.vpp.jvpp.core.callback.ShowVersionCallback;
25 import io.fd.vpp.jvpp.core.callfacade.CallbackJVppCoreFacade;
26 import io.fd.vpp.jvpp.core.dto.GetNodeIndex;
27 import io.fd.vpp.jvpp.core.dto.GetNodeIndexReply;
28 import io.fd.vpp.jvpp.core.dto.ShowVersionReply;
31 * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request.
32 * This is more convenient than the approach shown in CallbackApiTest.
34 public class CallbackJVppFacadeTest {
36 private static ShowVersionCallback showVersionCallback1 = new ShowVersionCallback() {
38 public void onShowVersionReply(final ShowVersionReply msg) {
39 System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s,"
40 + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
41 new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
45 public void onError(VppCallbackException ex) {
46 System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d%n",
47 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
51 private static ShowVersionCallback showVersionCallback2 = new ShowVersionCallback() {
53 public void onShowVersionReply(final ShowVersionReply msg) {
54 System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s,"
55 + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, new String(msg.program),
56 new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory));
60 public void onError(VppCallbackException ex) {
61 System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d%n",
62 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
67 private static GetNodeIndexCallback getNodeIndexCallback = new GetNodeIndexCallback() {
69 public void onGetNodeIndexReply(final GetNodeIndexReply msg) {
70 System.out.printf("Received GetNodeIndexReply: %s%n", msg);
74 public void onError(VppCallbackException ex) {
75 System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d%n",
76 ex.getMethodName(), ex.getErrorCode(), ex.getCtxId());
80 private static void testCallbackFacade() throws Exception {
81 System.out.println("Testing CallbackJVppFacade");
83 try (final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest");
84 final CallbackJVppCoreFacade callbackFacade = new CallbackJVppCoreFacade(registry, new JVppCoreImpl())) {
85 System.out.println("Successfully connected to VPP");
87 callbackFacade.showVersion(showVersionCallback1);
88 callbackFacade.showVersion(showVersionCallback2);
90 GetNodeIndex getNodeIndexRequest = new GetNodeIndex();
91 getNodeIndexRequest.nodeName = "dummyNode".getBytes();
92 callbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback);
95 System.out.println("Disconnecting...");
100 public static void main(String[] args) throws Exception {
101 testCallbackFacade();