diff options
Diffstat (limited to 'vpp-api/java/jvpp-core/org')
11 files changed, 1043 insertions, 0 deletions
diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackApiTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackApiTest.java new file mode 100644 index 00000000..cfa24560 --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackApiTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.callback.GetNodeIndexCallback; +import org.openvpp.jvpp.core.callback.ShowVersionCallback; +import org.openvpp.jvpp.core.callback.SwInterfaceCallback; +import org.openvpp.jvpp.core.dto.GetNodeIndex; +import org.openvpp.jvpp.core.dto.GetNodeIndexReply; +import org.openvpp.jvpp.core.dto.ShowVersion; +import org.openvpp.jvpp.core.dto.ShowVersionReply; +import org.openvpp.jvpp.core.dto.SwInterfaceDetails; +import org.openvpp.jvpp.core.dto.SwInterfaceDump; + +public class CallbackApiTest { + + static class TestCallback implements GetNodeIndexCallback, ShowVersionCallback, SwInterfaceCallback { + + @Override + public void onGetNodeIndexReply(final GetNodeIndexReply msg) { + System.out.printf("Received GetNodeIndexReply: context=%d, nodeIndex=%d\n", + msg.context, msg.nodeIndex); + } + + @Override + public void onShowVersionReply(final ShowVersionReply msg) { + System.out.printf("Received ShowVersionReply: context=%d, program=%s, version=%s, " + + "buildDate=%s, buildDirectory=%s\n", + msg.context, new String(msg.program), new String(msg.version), + new String(msg.buildDate), new String(msg.buildDirectory)); + } + + @Override + public void onSwInterfaceDetails(final SwInterfaceDetails msg) { + System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " + + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n", + new String(msg.interfaceName), msg.l2AddressLength, msg.adminUpDown, + msg.linkUpDown, msg.linkSpeed, (int) msg.linkMtu); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n", ex.getMethodName(), + ex.getCtxId(), ex.getErrorCode()); + } + } + + public static void main(String[] args) throws Exception { + testCallbackApi(); + } + + private static void testCallbackApi() throws Exception { + System.out.println("Testing Java callback API with JVppRegistry"); + JVppRegistry registry = new JVppRegistryImpl("CallbackApiTest"); + JVpp jvpp = new JVppCoreImpl(); + + registry.register(jvpp, new TestCallback()); + + System.out.println("Sending ShowVersion request..."); + final int result = jvpp.send(new ShowVersion()); + System.out.printf("ShowVersion send result = %d\n", result); + + System.out.println("Sending GetNodeIndex request..."); + GetNodeIndex getNodeIndexRequest = new GetNodeIndex(); + getNodeIndexRequest.nodeName = "non-existing-node".getBytes(); + jvpp.send(getNodeIndexRequest); + + System.out.println("Sending SwInterfaceDump request..."); + SwInterfaceDump swInterfaceDumpRequest = new SwInterfaceDump(); + swInterfaceDumpRequest.nameFilterValid = 0; + swInterfaceDumpRequest.nameFilter = "".getBytes(); + jvpp.send(swInterfaceDumpRequest); + + Thread.sleep(1000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeNotificationTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeNotificationTest.java new file mode 100644 index 00000000..542a561a --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeNotificationTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.core.JVppCore; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.callback.WantInterfaceEventsCallback; +import org.openvpp.jvpp.core.callfacade.CallbackJVppCoreFacade; +import org.openvpp.jvpp.core.dto.WantInterfaceEventsReply; + +public class CallbackJVppFacadeNotificationTest { + + private static void testCallbackFacade() throws Exception { + System.out.println("Testing CallbackJVppFacade for notifications"); + + final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest"); + final JVppCore jvpp = new JVppCoreImpl(); + + CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp); + System.out.println("Successfully connected to VPP"); + + final AutoCloseable notificationListenerReg = + jvppCallbackFacade.getNotificationRegistry().registerSwInterfaceSetFlagsNotificationCallback( + NotificationUtils::printNotification + ); + + jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getEnableInterfaceNotificationsReq(), + new WantInterfaceEventsCallback() { + @Override + public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) { + System.out.println("Interface events started"); + } + + @Override + public void onError(final VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n", + ex.getMethodName(), ex.getCtxId(), ex.getErrorCode()); + } + }); + + System.out.println("Changing interface configuration"); + NotificationUtils.getChangeInterfaceState().send(jvpp); + + Thread.sleep(1000); + + jvppCallbackFacade.wantInterfaceEvents(NotificationUtils.getDisableInterfaceNotificationsReq(), + new WantInterfaceEventsCallback() { + @Override + public void onWantInterfaceEventsReply(final WantInterfaceEventsReply reply) { + System.out.println("Interface events stopped"); + } + + @Override + public void onError(final VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, context=%d, retval=%d\n", + ex.getMethodName(), ex.getCtxId(), ex.getErrorCode()); + } + }); + + notificationListenerReg.close(); + + Thread.sleep(2000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testCallbackFacade(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeTest.java new file mode 100644 index 00000000..7499502b --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackJVppFacadeTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.core.JVppCore; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.callback.GetNodeIndexCallback; +import org.openvpp.jvpp.core.callback.ShowVersionCallback; +import org.openvpp.jvpp.core.callfacade.CallbackJVppCoreFacade; +import org.openvpp.jvpp.core.dto.GetNodeIndex; +import org.openvpp.jvpp.core.dto.GetNodeIndexReply; +import org.openvpp.jvpp.core.dto.ShowVersionReply; + +/** + * CallbackJVppFacade together with CallbackJVppFacadeCallback allow for setting different callback for each request. + * This is more convenient than the approach shown in CallbackApiTest. + */ +public class CallbackJVppFacadeTest { + + private static ShowVersionCallback showVersionCallback1 = new ShowVersionCallback() { + @Override + public void onShowVersionReply(final ShowVersionReply msg) { + System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s," + + "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program), + new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory)); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception in showVersionCallback1: call=%s, reply=%d, context=%d\n", + ex.getMethodName(), ex.getErrorCode(), ex.getCtxId()); + } + }; + + private static ShowVersionCallback showVersionCallback2 = new ShowVersionCallback() { + @Override + public void onShowVersionReply(final ShowVersionReply msg) { + System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s," + + "version=%s, buildDate=%s, buildDirectory=%s\n", msg.context, new String(msg.program), + new String(msg.version), new String(msg.buildDate), new String(msg.buildDirectory)); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception in showVersionCallback2: call=%s, reply=%d, context=%d\n", + ex.getMethodName(), ex.getErrorCode(), ex.getCtxId()); + } + + }; + + private static GetNodeIndexCallback getNodeIndexCallback = new GetNodeIndexCallback() { + @Override + public void onGetNodeIndexReply(final GetNodeIndexReply msg) { + System.out.printf("Received GetNodeIndexReply: context=%d, nodeIndex=%d\n", + msg.context, msg.nodeIndex); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n", + ex.getMethodName(), ex.getErrorCode(), ex.getCtxId()); + } + }; + + private static void testCallbackFacade() throws Exception { + System.out.println("Testing CallbackJVppFacade"); + + final JVppRegistry registry = new JVppRegistryImpl("CallbackFacadeTest"); + final JVppCore jvpp = new JVppCoreImpl(); + + CallbackJVppCoreFacade jvppCallbackFacade = new CallbackJVppCoreFacade(registry, jvpp); + System.out.println("Successfully connected to VPP"); + + jvppCallbackFacade.showVersion(showVersionCallback1); + jvppCallbackFacade.showVersion(showVersionCallback2); + + GetNodeIndex getNodeIndexRequest = new GetNodeIndex(); + getNodeIndexRequest.nodeName = "dummyNode".getBytes(); + jvppCallbackFacade.getNodeIndex(getNodeIndexRequest, getNodeIndexCallback); + + Thread.sleep(2000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testCallbackFacade(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackNotificationApiTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackNotificationApiTest.java new file mode 100644 index 00000000..a11cce60 --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CallbackNotificationApiTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import static org.openvpp.jvpp.core.test.NotificationUtils.getChangeInterfaceState; +import static org.openvpp.jvpp.core.test.NotificationUtils.getDisableInterfaceNotificationsReq; +import static org.openvpp.jvpp.core.test.NotificationUtils.getEnableInterfaceNotificationsReq; +import static org.openvpp.jvpp.core.test.NotificationUtils.printNotification; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsCallback; +import org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback; +import org.openvpp.jvpp.core.callback.WantInterfaceEventsCallback; +import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsNotification; +import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsReply; +import org.openvpp.jvpp.core.dto.WantInterfaceEventsReply; + +public class CallbackNotificationApiTest { + + private static class TestCallback implements SwInterfaceSetFlagsNotificationCallback, + WantInterfaceEventsCallback, SwInterfaceSetFlagsCallback { + + @Override + public void onSwInterfaceSetFlagsNotification( + final SwInterfaceSetFlagsNotification msg) { + printNotification(msg); + } + + @Override + public void onWantInterfaceEventsReply(final WantInterfaceEventsReply wantInterfaceEventsReply) { + System.out.println("Interface notification stream updated"); + } + + @Override + public void onSwInterfaceSetFlagsReply(final SwInterfaceSetFlagsReply swInterfaceSetFlagsReply) { + System.out.println("Interface flags set successfully"); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception in getNodeIndexCallback: call=%s, reply=%d, context=%d\n", + ex.getMethodName(), ex.getErrorCode(), ex.getCtxId()); + + } + } + + private static void testCallbackApi() throws Exception { + System.out.println("Testing Java callback API for notifications"); + JVppRegistry registry = new JVppRegistryImpl("CallbackNotificationTest"); + JVpp jvpp = new JVppCoreImpl(); + + registry.register(jvpp, new TestCallback()); + System.out.println("Successfully connected to VPP"); + + getEnableInterfaceNotificationsReq().send(jvpp); + System.out.println("Interface notifications started"); + // TODO test ifc dump which also triggers interface flags send + + System.out.println("Changing interface configuration"); + getChangeInterfaceState().send(jvpp); + + // Notifications are received + Thread.sleep(500); + + getDisableInterfaceNotificationsReq().send(jvpp); + System.out.println("Interface events stopped"); + + Thread.sleep(2000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testCallbackApi(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/ControlPingTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/ControlPingTest.java new file mode 100644 index 00000000..12ded2e0 --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/ControlPingTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.VppCallbackException; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.callback.ControlPingCallback; +import org.openvpp.jvpp.dto.ControlPing; +import org.openvpp.jvpp.dto.ControlPingReply; + +public class ControlPingTest { + + private static void testControlPing() throws Exception { + System.out.println("Testing ControlPing using Java callback API"); + JVppRegistry registry = new JVppRegistryImpl("ControlPingTest"); + JVpp jvpp = new JVppCoreImpl(); + + registry.register(jvpp, new ControlPingCallback() { + @Override + public void onControlPingReply(final ControlPingReply reply) { + System.out.printf("Received ControlPingReply: context=%d, clientIndex=%d vpePid=%d\n", + reply.context, reply.clientIndex, reply.vpePid); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, reply=%d, context=%d ", ex.getMethodName(), + ex.getErrorCode(), ex.getCtxId()); + } + + }); + System.out.println("Successfully connected to VPP"); + Thread.sleep(1000); + + System.out.println("Sending control ping using JVppRegistry"); + registry.controlPing(jvpp.getClass()); + + Thread.sleep(2000); + + System.out.println("Sending control ping using JVpp plugin"); + jvpp.send(new ControlPing()); + + Thread.sleep(2000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testControlPing(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CreateSubInterfaceTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CreateSubInterfaceTest.java new file mode 100644 index 00000000..9719a8cc --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/CreateSubInterfaceTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import static java.util.Objects.requireNonNull; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.dto.CreateSubif; +import org.openvpp.jvpp.core.dto.CreateSubifReply; +import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump; +import org.openvpp.jvpp.core.dto.SwInterfaceDump; +import org.openvpp.jvpp.core.future.FutureJVppCoreFacade; + +/** + * <p>Tests sub-interface creation.<br> Equivalent to:<br> + * + * <pre>{@code + * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any + * } + * </pre> + * + * To verify invoke:<br> + * <pre>{@code + * vpp_api_test json + * vat# sw_interface_dump + * } + */ +public class CreateSubInterfaceTest { + + private static SwInterfaceDump createSwInterfaceDumpRequest(final String ifaceName) { + SwInterfaceDump request = new SwInterfaceDump(); + request.nameFilter = ifaceName.getBytes(); + request.nameFilterValid = 1; + return request; + } + + private static void requireSingleIface(final SwInterfaceDetailsReplyDump response, final String ifaceName) { + if (response.swInterfaceDetails.size() != 1) { + throw new IllegalStateException( + String.format("Expected one interface matching filter %s but was %d", ifaceName, + response.swInterfaceDetails.size())); + } + } + + private static CreateSubif createSubifRequest(final int swIfIndex, final int subId) { + CreateSubif request = new CreateSubif(); + request.swIfIndex = swIfIndex; // super interface id + request.subId = subId; + request.noTags = 0; + request.oneTag = 0; + request.twoTags = 1; + request.dot1Ad = 0; + request.exactMatch = 1; + request.defaultSub = 0; + request.outerVlanIdAny = 0; + request.innerVlanIdAny = 1; + request.outerVlanId = 100; + request.innerVlanId = 0; + return request; + } + + private static void print(CreateSubifReply reply) { + System.out.printf("CreateSubifReply: context=%d, swIfIndex=%d\n", reply.context, reply.swIfIndex); + } + + private static void testCreateSubInterface() throws Exception { + System.out.println("Testing sub-interface creation using Java callback API"); + final JVppRegistry registry = new JVppRegistryImpl("CreateSubInterface"); + final JVpp jvpp = new JVppCoreImpl(); + final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp); + + System.out.println("Successfully connected to VPP"); + Thread.sleep(1000); + + final String ifaceName = "GigabitEthernet0/8/0"; + + final SwInterfaceDetailsReplyDump swInterfaceDetails = + jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get(); + + requireNonNull(swInterfaceDetails, "swInterfaceDump returned null"); + requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null"); + requireSingleIface(swInterfaceDetails, ifaceName); + + final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex; + final int subId = 1; + + final CreateSubifReply createSubifReply = + jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get(); + print(createSubifReply); + + final String subIfaceName = "GigabitEthernet0/8/0." + subId; + final SwInterfaceDetailsReplyDump subIface = + jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(subIfaceName)).toCompletableFuture().get(); + requireNonNull(swInterfaceDetails, "swInterfaceDump returned null"); + requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null"); + requireSingleIface(swInterfaceDetails, ifaceName); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testCreateSubInterface(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiNotificationTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiNotificationTest.java new file mode 100644 index 00000000..2decf6eb --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiNotificationTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import static org.openvpp.jvpp.core.test.NotificationUtils.getChangeInterfaceState; +import static org.openvpp.jvpp.core.test.NotificationUtils.getDisableInterfaceNotificationsReq; +import static org.openvpp.jvpp.core.test.NotificationUtils.getEnableInterfaceNotificationsReq; + +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.future.FutureJVppCoreFacade; + +public class FutureApiNotificationTest { + + private static void testFutureApi() throws Exception { + System.out.println("Testing Java future API for notifications"); + + final JVppRegistry registry = new JVppRegistryImpl("FutureApiNotificationTest"); + final JVpp jvpp = new JVppCoreImpl(); + final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp); + + System.out.println("Successfully connected to VPP"); + + final AutoCloseable notificationListenerReg = + jvppFacade.getNotificationRegistry() + .registerSwInterfaceSetFlagsNotificationCallback(NotificationUtils::printNotification); + + jvppFacade.wantInterfaceEvents(getEnableInterfaceNotificationsReq()).toCompletableFuture().get(); + System.out.println("Interface events started"); + + System.out.println("Changing interface configuration"); + jvppFacade.swInterfaceSetFlags(getChangeInterfaceState()).toCompletableFuture().get(); + + Thread.sleep(1000); + + jvppFacade.wantInterfaceEvents(getDisableInterfaceNotificationsReq()).toCompletableFuture().get(); + System.out.println("Interface events stopped"); + + notificationListenerReg.close(); + + System.out.println("Disconnecting..."); + registry.close(); + } + + public static void main(String[] args) throws Exception { + testFutureApi(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiTest.java new file mode 100644 index 00000000..7a671731 --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/FutureApiTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import java.util.Objects; +import java.util.concurrent.Future; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.dto.GetNodeIndex; +import org.openvpp.jvpp.core.dto.GetNodeIndexReply; +import org.openvpp.jvpp.core.dto.ShowVersion; +import org.openvpp.jvpp.core.dto.ShowVersionReply; +import org.openvpp.jvpp.core.dto.SwInterfaceDetails; +import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump; +import org.openvpp.jvpp.core.dto.SwInterfaceDump; +import org.openvpp.jvpp.core.future.FutureJVppCoreFacade; + +public class FutureApiTest { + + private static final Logger LOG = Logger.getLogger(FutureApiTest.class.getName()); + + private static void testShowVersion(final FutureJVppCoreFacade jvpp) throws Exception { + LOG.info("Sending ShowVersion request..."); + final Future<ShowVersionReply> replyFuture = jvpp.showVersion(new ShowVersion()).toCompletableFuture(); + final ShowVersionReply reply = replyFuture.get(); + LOG.info( + String.format( + "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s\n", + reply.context, new String(reply.program), new String(reply.version), new String(reply.buildDate), + new String(reply.buildDirectory))); + } + + private static void testGetNodeIndex(final FutureJVppCoreFacade jvpp) { + LOG.info("Sending GetNodeIndex request..."); + final GetNodeIndex request = new GetNodeIndex(); + request.nodeName = "non-existing-node".getBytes(); + final Future<GetNodeIndexReply> replyFuture = jvpp.getNodeIndex(request).toCompletableFuture(); + try { + final GetNodeIndexReply reply = replyFuture.get(); + LOG.info( + String.format( + "Received GetNodeIndexReply: context=%d, nodeIndex=%d\n", reply.context, reply.nodeIndex)); + } catch (Exception e) { + LOG.log(Level.SEVERE, "GetNodeIndex request failed", e); + } + } + + private static void testSwInterfaceDump(final FutureJVppCoreFacade jvpp) throws Exception { + LOG.info("Sending SwInterfaceDump request..."); + final SwInterfaceDump request = new SwInterfaceDump(); + request.nameFilterValid = 0; + request.nameFilter = "".getBytes(); + + final Future<SwInterfaceDetailsReplyDump> replyFuture = jvpp.swInterfaceDump(request).toCompletableFuture(); + final SwInterfaceDetailsReplyDump reply = replyFuture.get(); + for (SwInterfaceDetails details : reply.swInterfaceDetails) { + Objects.requireNonNull(details, "reply.swInterfaceDetails contains null element!"); + LOG.info( + String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " + + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d\n", + new String(details.interfaceName), details.l2AddressLength, details.adminUpDown, + details.linkUpDown, details.linkSpeed, (int) details.linkMtu)); + } + } + + private static void testFutureApi() throws Exception { + LOG.info("Testing Java future API"); + + final JVppRegistry registry = new JVppRegistryImpl("FutureApiTest"); + final JVpp jvpp = new JVppCoreImpl(); + final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp); + LOG.info("Successfully connected to VPP"); + + testShowVersion(jvppFacade); + testGetNodeIndex(jvppFacade); + testSwInterfaceDump(jvppFacade); + + LOG.info("Disconnecting..."); + registry.close(); + } + + public static void main(String[] args) throws Exception { + testFutureApi(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/L2AclTest.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/L2AclTest.java new file mode 100644 index 00000000..fc353f1d --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/L2AclTest.java @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import java.util.Arrays; +import javax.xml.bind.DatatypeConverter; +import org.openvpp.jvpp.JVpp; +import org.openvpp.jvpp.JVppRegistry; +import org.openvpp.jvpp.JVppRegistryImpl; +import org.openvpp.jvpp.core.JVppCoreImpl; +import org.openvpp.jvpp.core.dto.ClassifyAddDelSession; +import org.openvpp.jvpp.core.dto.ClassifyAddDelSessionReply; +import org.openvpp.jvpp.core.dto.ClassifyAddDelTable; +import org.openvpp.jvpp.core.dto.ClassifyAddDelTableReply; +import org.openvpp.jvpp.core.dto.ClassifySessionDetails; +import org.openvpp.jvpp.core.dto.ClassifySessionDetailsReplyDump; +import org.openvpp.jvpp.core.dto.ClassifySessionDump; +import org.openvpp.jvpp.core.dto.ClassifyTableByInterface; +import org.openvpp.jvpp.core.dto.ClassifyTableByInterfaceReply; +import org.openvpp.jvpp.core.dto.ClassifyTableIds; +import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply; +import org.openvpp.jvpp.core.dto.ClassifyTableInfo; +import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply; +import org.openvpp.jvpp.core.dto.InputAclSetInterface; +import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply; +import org.openvpp.jvpp.core.future.FutureJVppCoreFacade; + +/** + * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br> + * + * <pre>{@code + * vppctl classify table mask l2 src + * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06 + * vppctl set int input acl intfc local0 l2-table 0 + * vppctl sh class table verbose + * } + * </pre> + */ +public class L2AclTest { + + private static final int LOCAL0_IFACE_ID = 0; + + private static ClassifyAddDelTable createClassifyTable() { + ClassifyAddDelTable request = new ClassifyAddDelTable(); + request.isAdd = 1; + request.tableIndex = ~0; // default + request.nbuckets = 2; + request.memorySize = 2 << 20; + request.nextTableIndex = ~0; // default + request.missNextIndex = ~0; // default + request.skipNVectors = 0; + request.matchNVectors = 1; + request.mask = + new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00}; + return request; + } + + private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) { + ClassifyTableInfo request = new ClassifyTableInfo(); + request.tableId = tableId; + return request; + } + + private static ClassifyAddDelSession createClassifySession(final int tableIndex) { + ClassifyAddDelSession request = new ClassifyAddDelSession(); + request.isAdd = 1; + request.tableIndex = tableIndex; + request.hitNextIndex = 0; // deny + request.opaqueIndex = 0; + request.advance = 0; // default + // match 01:02:03:04:05:06 mac address + request.match = + new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, + (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00}; + return request; + } + + private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) { + ClassifySessionDump request = new ClassifySessionDump(); + request.tableId = newTableIndex; + return request; + } + + private static InputAclSetInterface aclSetInterface() { + InputAclSetInterface request = new InputAclSetInterface(); + request.isAdd = 1; + request.swIfIndex = LOCAL0_IFACE_ID; + request.ip4TableIndex = ~0; // skip + request.ip6TableIndex = ~0; // skip + request.l2TableIndex = 0; + return request; + } + + private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() { + ClassifyTableByInterface request = new ClassifyTableByInterface(); + request.swIfIndex = LOCAL0_IFACE_ID; + return request; + } + + private static void print(ClassifyAddDelTableReply reply) { + System.out.printf("ClassifyAddDelTableReply: context=%d, " + + "newTableIndex=%d, skipNVectors=%d, matchNVectors=%d\n", + reply.context, reply.newTableIndex, reply.skipNVectors, reply.matchNVectors); + } + + private static void print(ClassifyTableIdsReply reply) { + System.out.printf("ClassifyTableIdsReply: context=%d, count=%d, ids.length=%d\n", + reply.context, reply.count, reply.ids.length); + Arrays.stream(reply.ids).forEach(System.out::println); + } + + private static void print(final ClassifyTableInfoReply reply) { + final StringBuilder builder = new StringBuilder("ClassifyTableInfoReply:\n"); + builder.append("context: ").append(reply.context).append('\n'); + builder.append("tableId: ").append(reply.tableId).append('\n'); + builder.append("nbuckets: ").append(reply.nbuckets).append('\n'); + builder.append("matchNVectors: ").append(reply.matchNVectors).append('\n'); + builder.append("skipNVectors: ").append(reply.skipNVectors).append('\n'); + builder.append("activeSessions: ").append(reply.activeSessions).append('\n'); + builder.append("nextTableIndex: ").append(reply.nextTableIndex).append('\n'); + builder.append("missNextIndex: ").append(reply.missNextIndex).append('\n'); + builder.append("maskLength: ").append(reply.maskLength).append('\n'); + builder.append("mask: ").append(DatatypeConverter.printHexBinary(reply.mask)).append('\n'); + System.out.println(builder.toString()); + } + + private static void print(ClassifyAddDelSessionReply reply) { + System.out.printf("ClassifyAddDelSessionReply: context=%d\n", + reply.context); + } + + private static void print(final ClassifySessionDetailsReplyDump reply) { + if (reply.classifySessionDetails == null) { + System.out.println("ClassifySessionDetailsReplyDump: classifySessionDetails == NULL"); + } + for (final ClassifySessionDetails details : reply.classifySessionDetails) { + final StringBuilder builder = new StringBuilder("ClassifySessionDetails:\n"); + builder.append("context: ").append(details.context).append('\n'); + builder.append("tableId: ").append(details.tableId).append('\n'); + builder.append("hitNextIndex: ").append(details.hitNextIndex).append('\n'); + builder.append("advance: ").append(details.advance).append('\n'); + builder.append("opaqueIndex: ").append(details.opaqueIndex).append('\n'); + builder.append("matchLength: ").append(details.matchLength).append('\n'); + builder.append("match: ").append(DatatypeConverter.printHexBinary(details.match)).append('\n'); + System.out.println(builder.toString()); + } + } + + private static void print(final InputAclSetInterfaceReply reply) { + System.out.printf("InputAclSetInterfaceReply: context=%d\n", reply.context); + } + + private static void print(final ClassifyTableByInterfaceReply reply) { + System.out.printf("ClassifyAddDelTableReply: context=%d, swIfIndex=%d, l2TableId=%d, ip4TableId=%d," + + "ip6TableId=%d\n", reply.context, reply.swIfIndex, reply.l2TableId, reply.ip4TableId, reply.ip6TableId); + } + + private static void testL2Acl() throws Exception { + System.out.println("Testing L2 ACLs using Java callback API"); + final JVppRegistry registry = new JVppRegistryImpl("L2AclTest"); + final JVpp jvpp = new JVppCoreImpl(); + final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp); + + System.out.println("Successfully connected to VPP"); + Thread.sleep(1000); + + final ClassifyAddDelTableReply classifyAddDelTableReply = + jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get(); + print(classifyAddDelTableReply); + + final ClassifyTableIdsReply classifyTableIdsReply = + jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get(); + print(classifyTableIdsReply); + + final ClassifyTableInfoReply classifyTableInfoReply = + jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex)) + .toCompletableFuture().get(); + print(classifyTableInfoReply); + + final ClassifyAddDelSessionReply classifyAddDelSessionReply = + jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex)) + .toCompletableFuture().get(); + print(classifyAddDelSessionReply); + + final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump = + jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex)) + .toCompletableFuture().get(); + print(classifySessionDetailsReplyDump); + + final InputAclSetInterfaceReply inputAclSetInterfaceReply = + jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get(); + print(inputAclSetInterfaceReply); + + final ClassifyTableByInterfaceReply classifyTableByInterfaceReply = + jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get(); + print(classifyTableByInterfaceReply); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testL2Acl(); + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/NotificationUtils.java b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/NotificationUtils.java new file mode 100644 index 00000000..2e4d810c --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/NotificationUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openvpp.jvpp.core.test; + +import java.io.PrintStream; +import org.openvpp.jvpp.core.dto.SwInterfaceSetFlags; +import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsNotification; +import org.openvpp.jvpp.core.dto.WantInterfaceEvents; + +final class NotificationUtils { + + private NotificationUtils() {} + + static PrintStream printNotification(final SwInterfaceSetFlagsNotification msg) { + return System.out.printf("Received interface notification: ifc: %d, admin: %d, link: %d, deleted: %d\n", + msg.swIfIndex, msg.adminUpDown, msg.linkUpDown, msg.deleted); + } + + static SwInterfaceSetFlags getChangeInterfaceState() { + final SwInterfaceSetFlags swInterfaceSetFlags = new SwInterfaceSetFlags(); + swInterfaceSetFlags.swIfIndex = 0; + swInterfaceSetFlags.adminUpDown = 1; + swInterfaceSetFlags.deleted = 0; + return swInterfaceSetFlags; + } + + static WantInterfaceEvents getEnableInterfaceNotificationsReq() { + WantInterfaceEvents wantInterfaceEvents = new WantInterfaceEvents(); + wantInterfaceEvents.pid = 1; + wantInterfaceEvents.enableDisable = 1; + return wantInterfaceEvents; + } + + static WantInterfaceEvents getDisableInterfaceNotificationsReq() { + WantInterfaceEvents wantInterfaceEvents = new WantInterfaceEvents(); + wantInterfaceEvents.pid = 1; + wantInterfaceEvents.enableDisable = 0; + return wantInterfaceEvents; + } +} diff --git a/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/Readme.txt b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/Readme.txt new file mode 100644 index 00000000..016bde1c --- /dev/null +++ b/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/test/Readme.txt @@ -0,0 +1,16 @@ +This package contains basic tests for jvpp. To run the tests: + +- Make sure VPP is running +- From VPP's build-root/ folder execute: + - sudo java-cp build-vpp_debug-native/vpp-api/java/jvpp-registry-16.09.jar:build-vpp_debug-native/vpp-api/java/jvpp-core-16.09.jar org.openvpp.jvpp.core.test.[test name] + +Available tests: +CallbackApiTest - Similar to ControlPingTest, invokes more complex calls (e.g. interface dump) using low level JVpp APIs +CallbackJVppFacadeNotificationTest - Tests interface notifications using Callback based JVpp facade +CallbackJVppFacadeTest - Execution of more complex calls using Callback based JVpp facade +CallbackNotificationApiTest - Tests interface notifications using low level JVpp APIs +ControlPingTest - Simple test executing a single control ping using low level JVpp APIs +CreateSubInterfaceTest - Tests sub-interface creation +FutureApiNotificationTest - Tests interface notifications using Future based JVpp facade +FutureApiTest - Execution of more complex calls using Future based JVpp facade +L2AclTest - Tests L2 ACL creation |