From 2388db4347cf0c4a1032cb945db2fdb1011d74d7 Mon Sep 17 00:00:00 2001 From: Michal Cmarada Date: Thu, 28 Feb 2019 10:21:59 +0100 Subject: Update package names updating package names from io.fd.vpp.jvpp* to io.fd.jvpp* Change-Id: Ia1634db8f0875ff6ecfeddc281041cd49c4aacc2 Signed-off-by: Michal Cmarada --- .../io/fd/jvpp/AbstractCallbackApiTest.java | 63 +++++++ java/jvpp-registry/io/fd/jvpp/Assertions.java | 32 ++++ java/jvpp-registry/io/fd/jvpp/JVpp.java | 56 ++++++ java/jvpp-registry/io/fd/jvpp/JVppRegistry.java | 76 +++++++++ .../jvpp-registry/io/fd/jvpp/JVppRegistryImpl.java | 187 +++++++++++++++++++++ .../io/fd/jvpp/NativeLibraryLoader.java | 73 ++++++++ .../io/fd/jvpp/VppBaseCallException.java | 79 +++++++++ .../io/fd/jvpp/VppCallbackException.java | 48 ++++++ java/jvpp-registry/io/fd/jvpp/VppConnection.java | 45 +++++ .../io/fd/jvpp/VppInvocationException.java | 33 ++++ .../jvpp-registry/io/fd/jvpp/VppJNIConnection.java | 152 +++++++++++++++++ .../io/fd/jvpp/callback/ControlPingCallback.java | 29 ++++ .../io/fd/jvpp/callback/JVppCallback.java | 29 ++++ .../fd/jvpp/callback/JVppNotificationCallback.java | 24 +++ .../io/fd/jvpp/coverity/SuppressFBWarnings.java | 40 +++++ java/jvpp-registry/io/fd/jvpp/dto/ControlPing.java | 34 ++++ .../io/fd/jvpp/dto/ControlPingReply.java | 58 +++++++ java/jvpp-registry/io/fd/jvpp/dto/JVppDump.java | 24 +++ java/jvpp-registry/io/fd/jvpp/dto/JVppReply.java | 24 +++ .../io/fd/jvpp/dto/JVppReplyDump.java | 25 +++ java/jvpp-registry/io/fd/jvpp/dto/JVppRequest.java | 34 ++++ .../fd/jvpp/future/AbstractFutureJVppInvoker.java | 180 ++++++++++++++++++++ .../io/fd/jvpp/future/FutureJVppInvoker.java | 49 ++++++ .../io/fd/jvpp/notification/EventRegistry.java | 25 +++ .../jvpp/notification/EventRegistryProvider.java | 28 +++ .../io/fd/jvpp/test/ConnectionTest.java | 44 +++++ .../io/fd/vpp/jvpp/AbstractCallbackApiTest.java | 63 ------- java/jvpp-registry/io/fd/vpp/jvpp/Assertions.java | 32 ---- java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java | 56 ------ .../jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java | 76 --------- .../io/fd/vpp/jvpp/JVppRegistryImpl.java | 187 --------------------- .../io/fd/vpp/jvpp/NativeLibraryLoader.java | 73 -------- .../io/fd/vpp/jvpp/VppBaseCallException.java | 79 --------- .../io/fd/vpp/jvpp/VppCallbackException.java | 48 ------ .../io/fd/vpp/jvpp/VppConnection.java | 45 ----- .../io/fd/vpp/jvpp/VppInvocationException.java | 33 ---- .../io/fd/vpp/jvpp/VppJNIConnection.java | 152 ----------------- .../fd/vpp/jvpp/callback/ControlPingCallback.java | 29 ---- .../io/fd/vpp/jvpp/callback/JVppCallback.java | 29 ---- .../jvpp/callback/JVppNotificationCallback.java | 24 --- .../fd/vpp/jvpp/coverity/SuppressFBWarnings.java | 40 ----- .../io/fd/vpp/jvpp/dto/ControlPing.java | 34 ---- .../io/fd/vpp/jvpp/dto/ControlPingReply.java | 58 ------- .../jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java | 24 --- .../io/fd/vpp/jvpp/dto/JVppReply.java | 24 --- .../io/fd/vpp/jvpp/dto/JVppReplyDump.java | 25 --- .../io/fd/vpp/jvpp/dto/JVppRequest.java | 34 ---- .../vpp/jvpp/future/AbstractFutureJVppInvoker.java | 180 -------------------- .../io/fd/vpp/jvpp/future/FutureJVppInvoker.java | 49 ------ .../io/fd/vpp/jvpp/notification/EventRegistry.java | 25 --- .../jvpp/notification/EventRegistryProvider.java | 28 --- .../io/fd/vpp/jvpp/test/ConnectionTest.java | 44 ----- java/jvpp-registry/jvpp_registry.c | 18 +- 53 files changed, 1500 insertions(+), 1500 deletions(-) create mode 100644 java/jvpp-registry/io/fd/jvpp/AbstractCallbackApiTest.java create mode 100644 java/jvpp-registry/io/fd/jvpp/Assertions.java create mode 100644 java/jvpp-registry/io/fd/jvpp/JVpp.java create mode 100644 java/jvpp-registry/io/fd/jvpp/JVppRegistry.java create mode 100644 java/jvpp-registry/io/fd/jvpp/JVppRegistryImpl.java create mode 100644 java/jvpp-registry/io/fd/jvpp/NativeLibraryLoader.java create mode 100644 java/jvpp-registry/io/fd/jvpp/VppBaseCallException.java create mode 100644 java/jvpp-registry/io/fd/jvpp/VppCallbackException.java create mode 100644 java/jvpp-registry/io/fd/jvpp/VppConnection.java create mode 100644 java/jvpp-registry/io/fd/jvpp/VppInvocationException.java create mode 100644 java/jvpp-registry/io/fd/jvpp/VppJNIConnection.java create mode 100644 java/jvpp-registry/io/fd/jvpp/callback/ControlPingCallback.java create mode 100644 java/jvpp-registry/io/fd/jvpp/callback/JVppCallback.java create mode 100644 java/jvpp-registry/io/fd/jvpp/callback/JVppNotificationCallback.java create mode 100644 java/jvpp-registry/io/fd/jvpp/coverity/SuppressFBWarnings.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/ControlPing.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/ControlPingReply.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/JVppDump.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/JVppReply.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/JVppReplyDump.java create mode 100644 java/jvpp-registry/io/fd/jvpp/dto/JVppRequest.java create mode 100644 java/jvpp-registry/io/fd/jvpp/future/AbstractFutureJVppInvoker.java create mode 100644 java/jvpp-registry/io/fd/jvpp/future/FutureJVppInvoker.java create mode 100644 java/jvpp-registry/io/fd/jvpp/notification/EventRegistry.java create mode 100644 java/jvpp-registry/io/fd/jvpp/notification/EventRegistryProvider.java create mode 100644 java/jvpp-registry/io/fd/jvpp/test/ConnectionTest.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/AbstractCallbackApiTest.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/Assertions.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/coverity/SuppressFBWarnings.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistry.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistryProvider.java delete mode 100644 java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java (limited to 'java/jvpp-registry') diff --git a/java/jvpp-registry/io/fd/jvpp/AbstractCallbackApiTest.java b/java/jvpp-registry/io/fd/jvpp/AbstractCallbackApiTest.java new file mode 100644 index 0000000..a2e38e1 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/AbstractCallbackApiTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2017 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 io.fd.jvpp; + +import io.fd.jvpp.callback.ControlPingCallback; +import io.fd.jvpp.dto.ControlPing; +import io.fd.jvpp.dto.ControlPingReply; + +public abstract class AbstractCallbackApiTest { + + private static int receivedPingCount = 0; + private static int errorPingCount = 0; + + public static void testControlPing(String shm_prefix, JVpp jvpp) throws Exception { + try (JVppRegistry registry = new JVppRegistryImpl("CallbackApiTest", shm_prefix)) { + + registry.register(jvpp, new ControlPingCallback() { + @Override + public void onControlPingReply(final ControlPingReply reply) { + System.out.printf("Received ControlPingReply: %s%n", reply); + receivedPingCount++; + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, reply=%d, context=%d ", ex.getMethodName(), + ex.getErrorCode(), ex.getCtxId()); + errorPingCount++; + } + + }); + 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..."); + Assertions.assertEquals(2, receivedPingCount); + Assertions.assertEquals(0, errorPingCount); + } + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/Assertions.java b/java/jvpp-registry/io/fd/jvpp/Assertions.java new file mode 100644 index 0000000..2a403ad --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/Assertions.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 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 io.fd.jvpp; + +public class Assertions { + + public static void assertEquals(final int expected, final int actual) { + if (expected != actual) { + throw new IllegalArgumentException(String.format("Expected[%s]/Actual[%s]", expected, actual)); + } + } + + public static void assertNotNull(final Object value) { + if (value == null) { + throw new IllegalArgumentException("Variable is null"); + } + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/JVpp.java b/java/jvpp-registry/io/fd/jvpp/JVpp.java new file mode 100644 index 0000000..251b0a7 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/JVpp.java @@ -0,0 +1,56 @@ +/* + * 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 io.fd.jvpp; + +import io.fd.jvpp.callback.JVppCallback; +import io.fd.jvpp.dto.ControlPing; +import io.fd.jvpp.dto.JVppRequest; + +/** + * Base interface for plugin's Java API. + */ +public interface JVpp extends AutoCloseable { + + /** + * Sends request to vpp. + * + * @param request request to be sent + * @return unique identifer of message in message queue + * @throws VppInvocationException when message could not be sent + */ + int send(final JVppRequest request) throws VppInvocationException; + + /** + * Initializes plugin's Java API. + * + * @param registry plugin registry + * @param callback called by vpe.api message handlers + * @param queueAddress address of vpp shared memory queue + * @param clientIndex vpp client identifier + */ + void init(final JVppRegistry registry, final JVppCallback callback, final long queueAddress, + final int clientIndex); + + /** + * Sends control_ping message. + * + * @param controlPing request DTO + * @return unique identifer of message in message queue + * @throws VppInvocationException when message could not be sent + */ + int controlPing(final ControlPing controlPing) throws VppInvocationException; +} diff --git a/java/jvpp-registry/io/fd/jvpp/JVppRegistry.java b/java/jvpp-registry/io/fd/jvpp/JVppRegistry.java new file mode 100644 index 0000000..f182a0a --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/JVppRegistry.java @@ -0,0 +1,76 @@ +/* + * 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 io.fd.jvpp; + +import io.fd.jvpp.callback.JVppCallback; + +/** + * Manages VPP connection and stores plugin callbacks. + */ +public interface JVppRegistry extends AutoCloseable { + + /** + * Vpp connection managed by the registry. + * + * @return representation of vpp connection + */ + VppConnection getConnection(); + + /** + * Registers callback and initializes Java API for given plugin. + * + * @param jvpp plugin name + * @param callback callback provided by the plugin + * @throws NullPointerException if name or callback is null + * @throws IllegalArgumentException if plugin was already registered + */ + void register(final JVpp jvpp, final JVppCallback callback); + + /** + * Unregisters callback for the given plugin. + * + * @param name plugin name + * @throws NullPointerException if name is null + * @throws IllegalArgumentException if plugin was not registered + */ + void unregister(final String name); + + /** + * Returns callback registered for the plugin. + * + * @param name plugin name + * @return callback provided by the plugin + * @throws NullPointerException if name is null + * @throws IllegalArgumentException if plugin was not registered + */ + JVppCallback get(final String name); + + /** + * Sends control ping. Reply handler calls callback registered for give plugin. + * + * Control ping is used for initial RX thread to Java thread attachment + * that takes place in the plugin's JNI lib + * and to wrap dump message replies in one list. + * + * VPP plugins don't have to provide special control ping, therefore + * it is necessary to providing control ping support in JVppRegistry. + + * @param clazz identifies plugin that should receive ping callback + * @return unique identifier of message in message queue + */ + int controlPing(final Class clazz) throws VppInvocationException; +} diff --git a/java/jvpp-registry/io/fd/jvpp/JVppRegistryImpl.java b/java/jvpp-registry/io/fd/jvpp/JVppRegistryImpl.java new file mode 100644 index 0000000..7caa64c --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/JVppRegistryImpl.java @@ -0,0 +1,187 @@ +/* + * 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 io.fd.jvpp; + +import static java.util.Objects.requireNonNull; + +import io.fd.jvpp.callback.ControlPingCallback; +import io.fd.jvpp.callback.JVppCallback; +import io.fd.jvpp.dto.ControlPingReply; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Default implementation of JVppRegistry. + */ +public final class JVppRegistryImpl implements JVppRegistry, ControlPingCallback { + + private static final Logger LOG = Logger.getLogger(JVppRegistryImpl.class.getName()); + + private final VppJNIConnection connection; + // Unguarded concurrent map, no race conditions expected on top of that + private final Map pluginRegistry; + // Guarded by self + private final Map pingCalls; + + public JVppRegistryImpl(final String clientName) throws IOException { + connection = new VppJNIConnection(clientName); + connection.connect(); + pluginRegistry = new ConcurrentHashMap<>(); + pingCalls = new HashMap<>(); + } + + public JVppRegistryImpl(final String clientName, final String shmPrefix) throws IOException { + connection = new VppJNIConnection(clientName, shmPrefix); + connection.connect(); + pluginRegistry = new ConcurrentHashMap<>(); + pingCalls = new HashMap<>(); + } + + @Override + public VppConnection getConnection() { + return connection; + } + + @Override + public void register(final JVpp jvpp, final JVppCallback callback) { + requireNonNull(jvpp, "jvpp should not be null"); + requireNonNull(callback, "Callback should not be null"); + final String name = jvpp.getClass().getName(); + if (pluginRegistry.containsKey(name)) { + throw new IllegalArgumentException( + String.format("Callback for plugin %s was already registered", name)); + } + jvpp.init(this, callback, connection.getConnectionInfo().queueAddress, + connection.getConnectionInfo().clientIndex); + pluginRegistry.put(name, callback); + } + + @Override + public void unregister(final String name) { + requireNonNull(name, "Plugin name should not be null"); + final JVppCallback previous = pluginRegistry.remove(name); + assertPluginWasRegistered(name, previous); + } + + @Override + public JVppCallback get(final String name) { + requireNonNull(name, "Plugin name should not be null"); + JVppCallback value = pluginRegistry.get(name); + assertPluginWasRegistered(name, value); + return value; + } + + private native int controlPing0() throws VppInvocationException; + + @Override + public int controlPing(final Class clazz) throws VppInvocationException { + connection.checkActive(); + final String name = clazz.getName(); + + final ControlPingCallback callback = (ControlPingCallback) pluginRegistry.get(clazz.getName()); + assertPluginWasRegistered(name, callback); + + // controlPing0 is sending function and can go to waiting in case of e. g. full queue + // because of that it cant be in same synchronization block as used by reply handler function + int context = controlPing0(); + if (context < 0) { + throw new VppInvocationException("controlPing", context); + } + + synchronized (pingCalls) { + // if callback is in map it's because reply was already received + EarlyControlPingReply earlyReplyCallback = (EarlyControlPingReply) pingCalls.remove(context); + if(earlyReplyCallback == null) { + pingCalls.put(context, callback); + } else { + callback.onControlPingReply(earlyReplyCallback.getReply()); + } + } + + return context; + } + + @Override + public void onControlPingReply(final ControlPingReply reply) { + final ControlPingCallback callback; + synchronized (pingCalls) { + callback = pingCalls.remove(reply.context); + if (callback == null) { + // reply received early, because we don't know callback to call + // we wrap the reply and let the sender to call it + pingCalls.put(reply.context, new EarlyControlPingReply(reply)); + return; + } + } + // pass the reply to the callback registered by the ping caller + callback.onControlPingReply(reply); + } + + @Override + public void onError(final VppCallbackException ex) { + final int ctxId = ex.getCtxId(); + final ControlPingCallback callback; + + synchronized (pingCalls) { + callback = pingCalls.get(ctxId); + } + if (callback == null) { + LOG.log(Level.WARNING, "No callback was registered for reply id={0} ", ctxId); + return; + } + // pass the error to the callback registered by the ping caller + callback.onError(ex); + } + + private static void assertPluginWasRegistered(final String name, final JVppCallback value) { + if (value == null) { + throw new IllegalArgumentException(String.format("Callback for plugin %s is not registered", name)); + } + } + + @Override + public void close() throws Exception { + connection.close(); + } + + private static class EarlyControlPingReply implements ControlPingCallback { + + private final ControlPingReply reply; + + public EarlyControlPingReply(final ControlPingReply reply) { + this.reply = reply; + } + + public ControlPingReply getReply() { + return reply; + } + + @Override + public void onError(VppCallbackException ex) { + throw new IllegalStateException("Calling onError in EarlyControlPingReply"); + } + + @Override + public void onControlPingReply(ControlPingReply reply) { + throw new IllegalStateException("Calling onControlPingReply in EarlyControlPingReply"); + } + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/NativeLibraryLoader.java b/java/jvpp-registry/io/fd/jvpp/NativeLibraryLoader.java new file mode 100644 index 0000000..54097f1 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/NativeLibraryLoader.java @@ -0,0 +1,73 @@ +/* + * 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 io.fd.jvpp; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Utility class for loading JNI libraries. + */ +public final class NativeLibraryLoader { + + private static final Logger LOG = Logger.getLogger(NativeLibraryLoader.class.getName()); + + private NativeLibraryLoader() { + throw new UnsupportedOperationException("This utility class cannot be instantiated."); + } + + /** + * Loads JNI library using class loader of the given class. + * + * @param libName name of the library to be loaded + */ + public static void loadLibrary(final String libName, final Class clazz) throws IOException { + java.util.Objects.requireNonNull(libName, "libName should not be null"); + java.util.Objects.requireNonNull(clazz, "clazz should not be null"); + try (final InputStream is = clazz.getResourceAsStream('/' + libName)) { + if (is == null) { + throw new IOException("Failed to open library resource " + libName); + } + loadStream(libName, is); + } + } + + private static void loadStream(final String libName, final InputStream is) throws IOException { + final Set perms = PosixFilePermissions.fromString("rwxr-x---"); + final Path p = Files.createTempFile(libName, null, PosixFilePermissions.asFileAttribute(perms)); + try { + Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING); + Runtime.getRuntime().load(p.toString()); + } catch (Exception e) { + throw new IOException("Failed to load library " + p, e); + } finally { + try { + Files.deleteIfExists(p); + } catch (IOException e) { + LOG.log(Level.WARNING, String.format("Failed to delete temporary file %s.", p), e); + } + } + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/VppBaseCallException.java b/java/jvpp-registry/io/fd/jvpp/VppBaseCallException.java new file mode 100644 index 0000000..1fbe579 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/VppBaseCallException.java @@ -0,0 +1,79 @@ +/* + * 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 io.fd.jvpp; + + +/** + * Base exception representing failed operation of JVpp request call + */ +public abstract class VppBaseCallException extends Exception { + private final String methodName; + private final int errorCode; + + /** + * Constructs an VppCallbackException with the specified api method name and error code. + * + * @param methodName name of a method, which invocation or execution failed + * @param errorCode negative error code value associated with this failure + * @throws NullPointerException if apiMethodName is null + */ + public VppBaseCallException(final String methodName, final int errorCode) { + super(String.format("vppApi.%s failed with error code: %d", methodName, errorCode)); + this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!"); + this.errorCode = errorCode; + if(errorCode >= 0) { + throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode + + " for " + methodName ); + } + } + + /** + * Constructs an VppCallbackException with the specified api method name, error description and error code. + * + * @param methodName name of a method, which invocation or execution failed + * @param message description of error reason + * @param errorCode negative error code value associated with this failure + * @throws NullPointerException if apiMethodName is null + */ + public VppBaseCallException(final String methodName, final String message, final int errorCode) { + super(String.format("vppApi.%s failed: %s (error code: %d)", methodName,message, errorCode)); + this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!"); + this.errorCode = errorCode; + if(errorCode >= 0) { + throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode + + " for " + methodName ); + } + } + + /** + * Returns name of a method, which invocation failed. + * + * @return method name + */ + public String getMethodName() { + return methodName; + } + + /** + * Returns the error code associated with this failure. + * + * @return a negative integer error code + */ + public int getErrorCode() { + return errorCode; + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/VppCallbackException.java b/java/jvpp-registry/io/fd/jvpp/VppCallbackException.java new file mode 100644 index 0000000..2cd6a45 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/VppCallbackException.java @@ -0,0 +1,48 @@ +/* + * 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 io.fd.jvpp; + +/** + * Callback Exception representing failed operation of JVpp request call + */ +public class VppCallbackException extends VppBaseCallException { + private final int ctxId; + + /** + * Constructs an VppCallbackException with the specified api method name and error code. + * + * @param methodName name of a method, which invocation failed. + * @param message description of error reason + * @param ctxId api request context identifier + * @param errorCode negative error code value associated with this failure + * @throws NullPointerException if apiMethodName is null + */ + public VppCallbackException(final String methodName, final String message, final int ctxId, final int errorCode ){ + super(methodName, message, errorCode); + this.ctxId = ctxId; + } + + /** + * Returns api request context identifier. + * + * @return value of context identifier + */ + public int getCtxId() { + return ctxId; + } + +} diff --git a/java/jvpp-registry/io/fd/jvpp/VppConnection.java b/java/jvpp-registry/io/fd/jvpp/VppConnection.java new file mode 100644 index 0000000..5d7c03a --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/VppConnection.java @@ -0,0 +1,45 @@ +/* + * 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 io.fd.jvpp; + +import java.io.IOException; + +/** + * Representation of a management connection to VPP. + */ +public interface VppConnection extends AutoCloseable { + + /** + * Opens VppConnection for communication with VPP. + * + * @throws IOException if connection is not established + */ + void connect() throws IOException; + + /** + * Checks if this instance connection is active. + * + * @throws IllegalStateException if this instance was disconnected. + */ + void checkActive(); + + /** + * Closes Vpp connection. + */ + @Override + void close(); +} diff --git a/java/jvpp-registry/io/fd/jvpp/VppInvocationException.java b/java/jvpp-registry/io/fd/jvpp/VppInvocationException.java new file mode 100644 index 0000000..8125eb6 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/VppInvocationException.java @@ -0,0 +1,33 @@ +/* + * 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 io.fd.jvpp; + +/** + * Exception thrown when Vpp jAPI method invocation failed. + */ +public class VppInvocationException extends VppBaseCallException { + /** + * Constructs an VppApiInvocationFailedException with the specified api method name and error code. + * + * @param methodName name of a method, which invocation failed. + * @param errorCode negative error code value associated with this failure + * @throws NullPointerException if apiMethodName is null + */ + public VppInvocationException(final String methodName, final int errorCode) { + super(methodName, errorCode); + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/VppJNIConnection.java b/java/jvpp-registry/io/fd/jvpp/VppJNIConnection.java new file mode 100644 index 0000000..ff0eb1e --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/VppJNIConnection.java @@ -0,0 +1,152 @@ +/* + * 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 io.fd.jvpp; + +import static io.fd.jvpp.NativeLibraryLoader.loadLibrary; +import static java.lang.String.format; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * JNI based representation of a management connection to VPP. + */ +public final class VppJNIConnection implements VppConnection { + private static final Logger LOG = Logger.getLogger(VppJNIConnection.class.getName()); + private static final String DEFAULT_SHM_PREFIX = "/vpe-api"; + + static { + final String libName = "libjvpp_registry.so"; + try { + loadLibrary(libName, VppJNIConnection.class); + } catch (IOException e) { + LOG.log(Level.SEVERE, format("Can't find vpp jni library: %s", libName), e); + throw new ExceptionInInitializerError(e); + } + } + + private ConnectionInfo connectionInfo; + + private final String clientName; + private final String shmPrefix; + private volatile boolean disconnected = false; + + /** + * Create VPPJNIConnection instance for client connecting to VPP. + * + * @param clientName client name instance to be used for communication. Single connection per clientName is + * allowed. + */ + public VppJNIConnection(final String clientName) { + this.clientName = Objects.requireNonNull(clientName, "Null clientName"); + this.shmPrefix = DEFAULT_SHM_PREFIX; + } + + public VppJNIConnection(final String clientName, final String shmPrefix) { + this.clientName = Objects.requireNonNull(clientName, "Null clientName"); + this.shmPrefix = Objects.requireNonNull(shmPrefix, "Null shmPrefix"); + } + + /** + * Guarded by VppJNIConnection.class + */ + private static final Map connections = new HashMap<>(); + + /** + * Initiate VPP connection for current instance + * + * Multiple instances are allowed since this class is not a singleton (VPP allows multiple management connections). + * + * However only a single connection per clientName is allowed. + * + * @throws IOException in case the connection could not be established + */ + + @Override + public void connect() throws IOException { + _connect(shmPrefix); + } + + private void _connect(final String shmPrefix) throws IOException { + Objects.requireNonNull(shmPrefix, "Shared memory prefix must be defined"); + + synchronized (VppJNIConnection.class) { + if (connections.containsKey(clientName)) { + throw new IOException("Client " + clientName + " already connected"); + } + + connectionInfo = clientConnect(shmPrefix, clientName); + if (connectionInfo.status != 0) { + throw new IOException("Connection returned error " + connectionInfo.status); + } + connections.put(clientName, this); + } + } + + @Override + public final void checkActive() { + if (disconnected) { + throw new IllegalStateException("Disconnected client " + clientName); + } + } + + @Override + public final synchronized void close() { + if (!disconnected) { + disconnected = true; + try { + clientDisconnect(); + } finally { + synchronized (VppJNIConnection.class) { + connections.remove(clientName); + } + } + } + } + + public ConnectionInfo getConnectionInfo() { + return connectionInfo; + } + + /** + * VPP connection information used by plugins to reuse the connection. + */ + public static final class ConnectionInfo { + public final long queueAddress; + public final int clientIndex; + public final int status; // FIXME throw exception instead + public final int pid; + + public ConnectionInfo(long queueAddress, int clientIndex, int status, int pid) { + this.queueAddress = queueAddress; + this.clientIndex = clientIndex; + this.status = status; + this.pid = pid; + } + } + + private static native ConnectionInfo clientConnect(String shmPrefix, String clientName); + + private static native void clientDisconnect(); + +} diff --git a/java/jvpp-registry/io/fd/jvpp/callback/ControlPingCallback.java b/java/jvpp-registry/io/fd/jvpp/callback/ControlPingCallback.java new file mode 100644 index 0000000..f2cca3c --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/callback/ControlPingCallback.java @@ -0,0 +1,29 @@ +/* + * 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 io.fd.jvpp.callback; + +import io.fd.jvpp.dto.ControlPingReply; + +/** + * Represents callback for control_ping message. + */ +public interface ControlPingCallback extends JVppCallback { + + void onControlPingReply(ControlPingReply reply); + +} + diff --git a/java/jvpp-registry/io/fd/jvpp/callback/JVppCallback.java b/java/jvpp-registry/io/fd/jvpp/callback/JVppCallback.java new file mode 100644 index 0000000..00e85c9 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/callback/JVppCallback.java @@ -0,0 +1,29 @@ +/* + * 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 io.fd.jvpp.callback; +import io.fd.jvpp.VppCallbackException; + +/** + * Base JVppCallback interface + */ +public interface JVppCallback { + /** + * onError callback handler used to report failing operation + * @param ex VppCallbackException object containing details about failing operation + */ + void onError(VppCallbackException ex); +} diff --git a/java/jvpp-registry/io/fd/jvpp/callback/JVppNotificationCallback.java b/java/jvpp-registry/io/fd/jvpp/callback/JVppNotificationCallback.java new file mode 100644 index 0000000..7193f4d --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/callback/JVppNotificationCallback.java @@ -0,0 +1,24 @@ +/* + * 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 io.fd.jvpp.callback; + +/** +* Notification callback +*/ +public interface JVppNotificationCallback { + +} diff --git a/java/jvpp-registry/io/fd/jvpp/coverity/SuppressFBWarnings.java b/java/jvpp-registry/io/fd/jvpp/coverity/SuppressFBWarnings.java new file mode 100644 index 0000000..7f9724d --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/coverity/SuppressFBWarnings.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 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 io.fd.jvpp.coverity; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Used to suppress FindBugs warnings found by Coverity.
+ * We don't want extra dependency, so we define our own annotation version. + * + * @see Findbugs sourceforge + */ +@Retention(RetentionPolicy.CLASS) +public @interface SuppressFBWarnings { + /** + * The set of FindBugs warnings that are to be suppressed in annotated element. The value can be a bug category, + * kind or pattern. + */ + String[] value() default {}; + + /** + * Optional documentation of the reason why the warning is suppressed + */ + String justification() default ""; +} diff --git a/java/jvpp-registry/io/fd/jvpp/dto/ControlPing.java b/java/jvpp-registry/io/fd/jvpp/dto/ControlPing.java new file mode 100644 index 0000000..e2a7c26 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/ControlPing.java @@ -0,0 +1,34 @@ +/* + * 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 io.fd.jvpp.dto; + +import io.fd.jvpp.JVpp; +import io.fd.jvpp.VppInvocationException; + +/** + * Represents request DTO for control_ping message. + */ +public final class ControlPing implements JVppRequest { + + @Override + public int send(final JVpp jvpp) throws VppInvocationException { + return jvpp.controlPing(this); + } + +} + + diff --git a/java/jvpp-registry/io/fd/jvpp/dto/ControlPingReply.java b/java/jvpp-registry/io/fd/jvpp/dto/ControlPingReply.java new file mode 100644 index 0000000..68d5a2d --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/ControlPingReply.java @@ -0,0 +1,58 @@ +/* + * 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 io.fd.jvpp.dto; + +import java.util.Objects; + +/** + * Represents reply DTO for control_ping message. + */ +public final class ControlPingReply implements JVppReply { + + public int context; + public int clientIndex; + public int vpePid; + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final ControlPingReply that = (ControlPingReply) o; + return context == that.context && + clientIndex == that.clientIndex && + vpePid == that.vpePid; + } + + @Override + public int hashCode() { + return Objects.hash(context, clientIndex, vpePid); + } + + @Override + public String toString() { + return "ControlPingReply{" + + "context=" + context + + ", clientIndex=" + clientIndex + + ", vpePid=" + vpePid + + '}'; + } +} + diff --git a/java/jvpp-registry/io/fd/jvpp/dto/JVppDump.java b/java/jvpp-registry/io/fd/jvpp/dto/JVppDump.java new file mode 100644 index 0000000..9f8db7f --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/JVppDump.java @@ -0,0 +1,24 @@ +/* + * 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 io.fd.jvpp.dto; + +/** +* Base interface for all dump requests +*/ +public interface JVppDump extends JVppRequest { + +} diff --git a/java/jvpp-registry/io/fd/jvpp/dto/JVppReply.java b/java/jvpp-registry/io/fd/jvpp/dto/JVppReply.java new file mode 100644 index 0000000..9088ade --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/JVppReply.java @@ -0,0 +1,24 @@ +/* + * 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 io.fd.jvpp.dto; + +/** +* Base interface for all reply DTOs +*/ +public interface JVppReply { + +} diff --git a/java/jvpp-registry/io/fd/jvpp/dto/JVppReplyDump.java b/java/jvpp-registry/io/fd/jvpp/dto/JVppReplyDump.java new file mode 100644 index 0000000..a24b120 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/JVppReplyDump.java @@ -0,0 +1,25 @@ +/* + * 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 io.fd.jvpp.dto; + +/** +* Base interface for all dump replies +*/ +public interface JVppReplyDump> + extends JVppReply { + +} diff --git a/java/jvpp-registry/io/fd/jvpp/dto/JVppRequest.java b/java/jvpp-registry/io/fd/jvpp/dto/JVppRequest.java new file mode 100644 index 0000000..f82f5a3 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/dto/JVppRequest.java @@ -0,0 +1,34 @@ +/* + * 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 io.fd.jvpp.dto; + +import io.fd.jvpp.JVpp; +import io.fd.jvpp.VppInvocationException; + +/** +* Base interface for all request DTOs +*/ +public interface JVppRequest { + + /** + * Invoke current operation asynchronously on VPP + * + * @return context id of this request. Can be used to track incoming response + */ + int send(JVpp jvpp) throws VppInvocationException; + +} diff --git a/java/jvpp-registry/io/fd/jvpp/future/AbstractFutureJVppInvoker.java b/java/jvpp-registry/io/fd/jvpp/future/AbstractFutureJVppInvoker.java new file mode 100644 index 0000000..406ccfb --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/future/AbstractFutureJVppInvoker.java @@ -0,0 +1,180 @@ +/* + * 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 io.fd.jvpp.future; + + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; + +import io.fd.jvpp.JVpp; +import io.fd.jvpp.JVppRegistry; +import io.fd.jvpp.VppInvocationException; +import io.fd.jvpp.dto.JVppDump; +import io.fd.jvpp.dto.JVppReply; +import io.fd.jvpp.dto.JVppReplyDump; +import io.fd.jvpp.dto.JVppRequest; + +/** + * Future facade on top of JVpp + */ +public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { + + private final JVpp jvpp; + private final JVppRegistry registry; + + /** + * Guarded by self + */ + private final Map>> requests; + + protected AbstractFutureJVppInvoker(final JVpp jvpp, final JVppRegistry registry, + final Map>> requestMap) { + this.jvpp = Objects.requireNonNull(jvpp, "jvpp should not be null"); + this.registry = Objects.requireNonNull(registry, "registry should not be null"); + // Request map represents the shared state between this facade and it's callback + // where facade puts futures in and callback completes + removes them + this.requests = Objects.requireNonNull(requestMap, "Null requestMap"); + } + + protected final Map>> getRequests() { + synchronized (requests) { + return requests; + } + } + + // TODO use Optional in Future, java8 + + @Override + @SuppressWarnings("unchecked") + public > CompletionStage send(REQ req) { + try { + // jvpp.send() can go to waiting state if sending queue is full, putting it into same + // synchronization block as used by receiving part (synchronized(requests)) can lead + // to deadlock between these two sides or at least slowing sending process by slow + // reader + final CompletableFuture replyCompletableFuture; + final int contextId = jvpp.send(req); + + if(req instanceof JVppDump) { + throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls"); + } + + synchronized(requests) { + CompletableFuture> replyFuture = requests.get(contextId); + if (replyFuture == null) { + // reply not yet received, put new future into map + replyCompletableFuture = new CompletableFuture<>(); + requests.put(contextId, replyCompletableFuture); + } else { + // reply already received (should be completed by reader), + // remove future from map and return it to caller + replyCompletableFuture = (CompletableFuture) replyFuture; + requests.remove(contextId); + } + } + + // TODO in case of timeouts/missing replies, requests from the map are not removed + // consider adding cancel method, that would remove requests from the map and cancel + // associated replyCompletableFuture + + return replyCompletableFuture; + } catch (VppInvocationException ex) { + final CompletableFuture replyCompletableFuture = new CompletableFuture<>(); + replyCompletableFuture.completeExceptionally(ex); + return replyCompletableFuture; + } + } + + @Override + @SuppressWarnings("unchecked") + public , DUMP extends JVppReplyDump> CompletionStage send( + REQ req, DUMP emptyReplyDump) { + try { + // jvpp.send() and registry.controlPing() can go to waiting state if sending queue is full, + // putting it into same synchronization block as used by receiving part (synchronized(requests)) + // can lead to deadlock between these two sides or at least slowing sending process by slow reader + final CompletableDumpFuture replyDumpFuture; + final int contextId = jvpp.send(req); + + if(!(req instanceof JVppDump)) { + throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls"); + } + + synchronized(requests) { + CompletableFuture> replyFuture = requests.get(contextId); + if (replyFuture == null) { + // reply not received yet, put new future to map + replyDumpFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump); + requests.put(contextId, replyDumpFuture); + } else { + // reply already received, save existing future + replyDumpFuture = (CompletableDumpFuture) replyFuture; + } + } + + final int pingId = registry.controlPing(jvpp.getClass()); + + synchronized(requests) { + if (requests.remove(pingId) == null) { + // reply not received yet, put future into map under pingId + requests.put(pingId, replyDumpFuture); + } else { + // reply already received, complete future + // ping reply couldn't complete the future because it is not in map under + // ping id + replyDumpFuture.complete(replyDumpFuture.getReplyDump()); + requests.remove(contextId); + } + } + + // TODO in case of timeouts/missing replies, requests from the map are not removed + // consider adding cancel method, that would remove requests from the map and cancel + // associated replyCompletableFuture + + return replyDumpFuture; + } catch (VppInvocationException ex) { + final CompletableFuture replyCompletableFuture = new CompletableFuture<>(); + replyCompletableFuture.completeExceptionally(ex); + return replyCompletableFuture; + } + } + + public static final class CompletableDumpFuture> extends CompletableFuture { + private final T replyDump; + private final int contextId; + + public CompletableDumpFuture(final int contextId, final T emptyDump) { + this.contextId = contextId; + this.replyDump = emptyDump; + } + + public int getContextId() { + return contextId; + } + + public T getReplyDump() { + return replyDump; + } + } + + @Override + public void close() throws Exception { + jvpp.close(); + } +} diff --git a/java/jvpp-registry/io/fd/jvpp/future/FutureJVppInvoker.java b/java/jvpp-registry/io/fd/jvpp/future/FutureJVppInvoker.java new file mode 100644 index 0000000..f421f5a --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/future/FutureJVppInvoker.java @@ -0,0 +1,49 @@ +/* + * 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 io.fd.jvpp.future; + + +import io.fd.jvpp.dto.JVppReply; +import io.fd.jvpp.dto.JVppReplyDump; +import io.fd.jvpp.dto.JVppRequest; + +import java.util.concurrent.CompletionStage; +import io.fd.jvpp.notification.EventRegistryProvider; + +/** +* Future facade on top of JVpp +*/ +public interface FutureJVppInvoker extends EventRegistryProvider, AutoCloseable { + + /** + * Invoke asynchronous operation on VPP + * + * @return CompletionStage with future result of an async VPP call + * @throws io.fd.jvpp.VppInvocationException when send request failed with details + */ + > CompletionStage send(REQ req); + + + /** + * Invoke asynchronous dump operation on VPP + * + * @return CompletionStage with aggregated future result of an async VPP dump call + * @throws io.fd.jvpp.VppInvocationException when send request failed with details + */ + , DUMP extends JVppReplyDump> CompletionStage send( + REQ req, DUMP emptyReplyDump); +} diff --git a/java/jvpp-registry/io/fd/jvpp/notification/EventRegistry.java b/java/jvpp-registry/io/fd/jvpp/notification/EventRegistry.java new file mode 100644 index 0000000..f25b038 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/notification/EventRegistry.java @@ -0,0 +1,25 @@ +/* + * 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 io.fd.jvpp.notification; + +/** + * Base registry for notification callbacks. + */ +public interface EventRegistry extends AutoCloseable { + + void close(); +} diff --git a/java/jvpp-registry/io/fd/jvpp/notification/EventRegistryProvider.java b/java/jvpp-registry/io/fd/jvpp/notification/EventRegistryProvider.java new file mode 100644 index 0000000..a1702da --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/notification/EventRegistryProvider.java @@ -0,0 +1,28 @@ +/* + * 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 io.fd.jvpp.notification; + +/** + * Provides notification registry + */ +public interface EventRegistryProvider { + + /** + * Get current notification registry instance + */ + EventRegistry getEventRegistry(); +} diff --git a/java/jvpp-registry/io/fd/jvpp/test/ConnectionTest.java b/java/jvpp-registry/io/fd/jvpp/test/ConnectionTest.java new file mode 100644 index 0000000..b982d11 --- /dev/null +++ b/java/jvpp-registry/io/fd/jvpp/test/ConnectionTest.java @@ -0,0 +1,44 @@ +/* + * 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 io.fd.jvpp.test; + +import io.fd.jvpp.JVppRegistry; +import io.fd.jvpp.JVppRegistryImpl; + +/** + * Run using: + * sudo java -cp build-vpp-native/vpp-api/java/jvpp-registry-16.09.jar io.fd.jvpp.test.ConnectionTest + */ +public class ConnectionTest { + + private static void testConnect() throws Exception { + System.out.println("Testing JNI connection with JVppRegistry"); + final JVppRegistry registry = new JVppRegistryImpl("ConnectionTest"); + try { + System.out.println("Successfully connected to vpp"); + Thread.sleep(5000); + System.out.println("Disconnecting..."); + Thread.sleep(1000); + } finally { + registry.close(); + } + } + + public static void main(String[] args) throws Exception { + testConnect(); + } +} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/AbstractCallbackApiTest.java b/java/jvpp-registry/io/fd/vpp/jvpp/AbstractCallbackApiTest.java deleted file mode 100644 index d221d1e..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/AbstractCallbackApiTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2017 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 io.fd.vpp.jvpp; - -import io.fd.vpp.jvpp.callback.ControlPingCallback; -import io.fd.vpp.jvpp.dto.ControlPing; -import io.fd.vpp.jvpp.dto.ControlPingReply; - -public abstract class AbstractCallbackApiTest { - - private static int receivedPingCount = 0; - private static int errorPingCount = 0; - - public static void testControlPing(String shm_prefix, JVpp jvpp) throws Exception { - try (JVppRegistry registry = new JVppRegistryImpl("CallbackApiTest", shm_prefix)) { - - registry.register(jvpp, new ControlPingCallback() { - @Override - public void onControlPingReply(final ControlPingReply reply) { - System.out.printf("Received ControlPingReply: %s%n", reply); - receivedPingCount++; - } - - @Override - public void onError(VppCallbackException ex) { - System.out.printf("Received onError exception: call=%s, reply=%d, context=%d ", ex.getMethodName(), - ex.getErrorCode(), ex.getCtxId()); - errorPingCount++; - } - - }); - 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..."); - Assertions.assertEquals(2, receivedPingCount); - Assertions.assertEquals(0, errorPingCount); - } - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/Assertions.java b/java/jvpp-registry/io/fd/vpp/jvpp/Assertions.java deleted file mode 100644 index f8b591f..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/Assertions.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2017 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 io.fd.vpp.jvpp; - -public class Assertions { - - public static void assertEquals(final int expected, final int actual) { - if (expected != actual) { - throw new IllegalArgumentException(String.format("Expected[%s]/Actual[%s]", expected, actual)); - } - } - - public static void assertNotNull(final Object value) { - if (value == null) { - throw new IllegalArgumentException("Variable is null"); - } - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java b/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java deleted file mode 100644 index 55f25a7..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import io.fd.vpp.jvpp.callback.JVppCallback; -import io.fd.vpp.jvpp.dto.ControlPing; -import io.fd.vpp.jvpp.dto.JVppRequest; - -/** - * Base interface for plugin's Java API. - */ -public interface JVpp extends AutoCloseable { - - /** - * Sends request to vpp. - * - * @param request request to be sent - * @return unique identifer of message in message queue - * @throws VppInvocationException when message could not be sent - */ - int send(final JVppRequest request) throws VppInvocationException; - - /** - * Initializes plugin's Java API. - * - * @param registry plugin registry - * @param callback called by vpe.api message handlers - * @param queueAddress address of vpp shared memory queue - * @param clientIndex vpp client identifier - */ - void init(final JVppRegistry registry, final JVppCallback callback, final long queueAddress, - final int clientIndex); - - /** - * Sends control_ping message. - * - * @param controlPing request DTO - * @return unique identifer of message in message queue - * @throws VppInvocationException when message could not be sent - */ - int controlPing(final ControlPing controlPing) throws VppInvocationException; -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java b/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java deleted file mode 100644 index 6535db0..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import io.fd.vpp.jvpp.callback.JVppCallback; - -/** - * Manages VPP connection and stores plugin callbacks. - */ -public interface JVppRegistry extends AutoCloseable { - - /** - * Vpp connection managed by the registry. - * - * @return representation of vpp connection - */ - VppConnection getConnection(); - - /** - * Registers callback and initializes Java API for given plugin. - * - * @param jvpp plugin name - * @param callback callback provided by the plugin - * @throws NullPointerException if name or callback is null - * @throws IllegalArgumentException if plugin was already registered - */ - void register(final JVpp jvpp, final JVppCallback callback); - - /** - * Unregisters callback for the given plugin. - * - * @param name plugin name - * @throws NullPointerException if name is null - * @throws IllegalArgumentException if plugin was not registered - */ - void unregister(final String name); - - /** - * Returns callback registered for the plugin. - * - * @param name plugin name - * @return callback provided by the plugin - * @throws NullPointerException if name is null - * @throws IllegalArgumentException if plugin was not registered - */ - JVppCallback get(final String name); - - /** - * Sends control ping. Reply handler calls callback registered for give plugin. - * - * Control ping is used for initial RX thread to Java thread attachment - * that takes place in the plugin's JNI lib - * and to wrap dump message replies in one list. - * - * VPP plugins don't have to provide special control ping, therefore - * it is necessary to providing control ping support in JVppRegistry. - - * @param clazz identifies plugin that should receive ping callback - * @return unique identifier of message in message queue - */ - int controlPing(final Class clazz) throws VppInvocationException; -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java b/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java deleted file mode 100644 index baef14c..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import static java.util.Objects.requireNonNull; - -import io.fd.vpp.jvpp.callback.ControlPingCallback; -import io.fd.vpp.jvpp.callback.JVppCallback; -import io.fd.vpp.jvpp.dto.ControlPingReply; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Default implementation of JVppRegistry. - */ -public final class JVppRegistryImpl implements JVppRegistry, ControlPingCallback { - - private static final Logger LOG = Logger.getLogger(JVppRegistryImpl.class.getName()); - - private final VppJNIConnection connection; - // Unguarded concurrent map, no race conditions expected on top of that - private final Map pluginRegistry; - // Guarded by self - private final Map pingCalls; - - public JVppRegistryImpl(final String clientName) throws IOException { - connection = new VppJNIConnection(clientName); - connection.connect(); - pluginRegistry = new ConcurrentHashMap<>(); - pingCalls = new HashMap<>(); - } - - public JVppRegistryImpl(final String clientName, final String shmPrefix) throws IOException { - connection = new VppJNIConnection(clientName, shmPrefix); - connection.connect(); - pluginRegistry = new ConcurrentHashMap<>(); - pingCalls = new HashMap<>(); - } - - @Override - public VppConnection getConnection() { - return connection; - } - - @Override - public void register(final JVpp jvpp, final JVppCallback callback) { - requireNonNull(jvpp, "jvpp should not be null"); - requireNonNull(callback, "Callback should not be null"); - final String name = jvpp.getClass().getName(); - if (pluginRegistry.containsKey(name)) { - throw new IllegalArgumentException( - String.format("Callback for plugin %s was already registered", name)); - } - jvpp.init(this, callback, connection.getConnectionInfo().queueAddress, - connection.getConnectionInfo().clientIndex); - pluginRegistry.put(name, callback); - } - - @Override - public void unregister(final String name) { - requireNonNull(name, "Plugin name should not be null"); - final JVppCallback previous = pluginRegistry.remove(name); - assertPluginWasRegistered(name, previous); - } - - @Override - public JVppCallback get(final String name) { - requireNonNull(name, "Plugin name should not be null"); - JVppCallback value = pluginRegistry.get(name); - assertPluginWasRegistered(name, value); - return value; - } - - private native int controlPing0() throws VppInvocationException; - - @Override - public int controlPing(final Class clazz) throws VppInvocationException { - connection.checkActive(); - final String name = clazz.getName(); - - final ControlPingCallback callback = (ControlPingCallback) pluginRegistry.get(clazz.getName()); - assertPluginWasRegistered(name, callback); - - // controlPing0 is sending function and can go to waiting in case of e. g. full queue - // because of that it cant be in same synchronization block as used by reply handler function - int context = controlPing0(); - if (context < 0) { - throw new VppInvocationException("controlPing", context); - } - - synchronized (pingCalls) { - // if callback is in map it's because reply was already received - EarlyControlPingReply earlyReplyCallback = (EarlyControlPingReply) pingCalls.remove(context); - if(earlyReplyCallback == null) { - pingCalls.put(context, callback); - } else { - callback.onControlPingReply(earlyReplyCallback.getReply()); - } - } - - return context; - } - - @Override - public void onControlPingReply(final ControlPingReply reply) { - final ControlPingCallback callback; - synchronized (pingCalls) { - callback = pingCalls.remove(reply.context); - if (callback == null) { - // reply received early, because we don't know callback to call - // we wrap the reply and let the sender to call it - pingCalls.put(reply.context, new EarlyControlPingReply(reply)); - return; - } - } - // pass the reply to the callback registered by the ping caller - callback.onControlPingReply(reply); - } - - @Override - public void onError(final VppCallbackException ex) { - final int ctxId = ex.getCtxId(); - final ControlPingCallback callback; - - synchronized (pingCalls) { - callback = pingCalls.get(ctxId); - } - if (callback == null) { - LOG.log(Level.WARNING, "No callback was registered for reply id={0} ", ctxId); - return; - } - // pass the error to the callback registered by the ping caller - callback.onError(ex); - } - - private static void assertPluginWasRegistered(final String name, final JVppCallback value) { - if (value == null) { - throw new IllegalArgumentException(String.format("Callback for plugin %s is not registered", name)); - } - } - - @Override - public void close() throws Exception { - connection.close(); - } - - private static class EarlyControlPingReply implements ControlPingCallback { - - private final ControlPingReply reply; - - public EarlyControlPingReply(final ControlPingReply reply) { - this.reply = reply; - } - - public ControlPingReply getReply() { - return reply; - } - - @Override - public void onError(VppCallbackException ex) { - throw new IllegalStateException("Calling onError in EarlyControlPingReply"); - } - - @Override - public void onControlPingReply(ControlPingReply reply) { - throw new IllegalStateException("Calling onControlPingReply in EarlyControlPingReply"); - } - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java b/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java deleted file mode 100644 index ce6d1bf..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.PosixFilePermission; -import java.nio.file.attribute.PosixFilePermissions; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Utility class for loading JNI libraries. - */ -public final class NativeLibraryLoader { - - private static final Logger LOG = Logger.getLogger(NativeLibraryLoader.class.getName()); - - private NativeLibraryLoader() { - throw new UnsupportedOperationException("This utility class cannot be instantiated."); - } - - /** - * Loads JNI library using class loader of the given class. - * - * @param libName name of the library to be loaded - */ - public static void loadLibrary(final String libName, final Class clazz) throws IOException { - java.util.Objects.requireNonNull(libName, "libName should not be null"); - java.util.Objects.requireNonNull(clazz, "clazz should not be null"); - try (final InputStream is = clazz.getResourceAsStream('/' + libName)) { - if (is == null) { - throw new IOException("Failed to open library resource " + libName); - } - loadStream(libName, is); - } - } - - private static void loadStream(final String libName, final InputStream is) throws IOException { - final Set perms = PosixFilePermissions.fromString("rwxr-x---"); - final Path p = Files.createTempFile(libName, null, PosixFilePermissions.asFileAttribute(perms)); - try { - Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().load(p.toString()); - } catch (Exception e) { - throw new IOException("Failed to load library " + p, e); - } finally { - try { - Files.deleteIfExists(p); - } catch (IOException e) { - LOG.log(Level.WARNING, String.format("Failed to delete temporary file %s.", p), e); - } - } - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java b/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java deleted file mode 100644 index 7fc1682..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - - -/** - * Base exception representing failed operation of JVpp request call - */ -public abstract class VppBaseCallException extends Exception { - private final String methodName; - private final int errorCode; - - /** - * Constructs an VppCallbackException with the specified api method name and error code. - * - * @param methodName name of a method, which invocation or execution failed - * @param errorCode negative error code value associated with this failure - * @throws NullPointerException if apiMethodName is null - */ - public VppBaseCallException(final String methodName, final int errorCode) { - super(String.format("vppApi.%s failed with error code: %d", methodName, errorCode)); - this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!"); - this.errorCode = errorCode; - if(errorCode >= 0) { - throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode + - " for " + methodName ); - } - } - - /** - * Constructs an VppCallbackException with the specified api method name, error description and error code. - * - * @param methodName name of a method, which invocation or execution failed - * @param message description of error reason - * @param errorCode negative error code value associated with this failure - * @throws NullPointerException if apiMethodName is null - */ - public VppBaseCallException(final String methodName, final String message, final int errorCode) { - super(String.format("vppApi.%s failed: %s (error code: %d)", methodName,message, errorCode)); - this.methodName = java.util.Objects.requireNonNull(methodName, "apiMethodName is null!"); - this.errorCode = errorCode; - if(errorCode >= 0) { - throw new IllegalArgumentException("Error code must be < 0. Was " + errorCode + - " for " + methodName ); - } - } - - /** - * Returns name of a method, which invocation failed. - * - * @return method name - */ - public String getMethodName() { - return methodName; - } - - /** - * Returns the error code associated with this failure. - * - * @return a negative integer error code - */ - public int getErrorCode() { - return errorCode; - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java b/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java deleted file mode 100644 index adcc5d2..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -/** - * Callback Exception representing failed operation of JVpp request call - */ -public class VppCallbackException extends VppBaseCallException { - private final int ctxId; - - /** - * Constructs an VppCallbackException with the specified api method name and error code. - * - * @param methodName name of a method, which invocation failed. - * @param message description of error reason - * @param ctxId api request context identifier - * @param errorCode negative error code value associated with this failure - * @throws NullPointerException if apiMethodName is null - */ - public VppCallbackException(final String methodName, final String message, final int ctxId, final int errorCode ){ - super(methodName, message, errorCode); - this.ctxId = ctxId; - } - - /** - * Returns api request context identifier. - * - * @return value of context identifier - */ - public int getCtxId() { - return ctxId; - } - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java b/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java deleted file mode 100644 index e6fd3bd..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import java.io.IOException; - -/** - * Representation of a management connection to VPP. - */ -public interface VppConnection extends AutoCloseable { - - /** - * Opens VppConnection for communication with VPP. - * - * @throws IOException if connection is not established - */ - void connect() throws IOException; - - /** - * Checks if this instance connection is active. - * - * @throws IllegalStateException if this instance was disconnected. - */ - void checkActive(); - - /** - * Closes Vpp connection. - */ - @Override - void close(); -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java b/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java deleted file mode 100644 index a7ccb19..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -/** - * Exception thrown when Vpp jAPI method invocation failed. - */ -public class VppInvocationException extends VppBaseCallException { - /** - * Constructs an VppApiInvocationFailedException with the specified api method name and error code. - * - * @param methodName name of a method, which invocation failed. - * @param errorCode negative error code value associated with this failure - * @throws NullPointerException if apiMethodName is null - */ - public VppInvocationException(final String methodName, final int errorCode) { - super(methodName, errorCode); - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java b/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java deleted file mode 100644 index 6a414f3..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp; - -import static io.fd.vpp.jvpp.NativeLibraryLoader.loadLibrary; -import static java.lang.String.format; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * JNI based representation of a management connection to VPP. - */ -public final class VppJNIConnection implements VppConnection { - private static final Logger LOG = Logger.getLogger(VppJNIConnection.class.getName()); - private static final String DEFAULT_SHM_PREFIX = "/vpe-api"; - - static { - final String libName = "libjvpp_registry.so"; - try { - loadLibrary(libName, VppJNIConnection.class); - } catch (IOException e) { - LOG.log(Level.SEVERE, format("Can't find vpp jni library: %s", libName), e); - throw new ExceptionInInitializerError(e); - } - } - - private ConnectionInfo connectionInfo; - - private final String clientName; - private final String shmPrefix; - private volatile boolean disconnected = false; - - /** - * Create VPPJNIConnection instance for client connecting to VPP. - * - * @param clientName client name instance to be used for communication. Single connection per clientName is - * allowed. - */ - public VppJNIConnection(final String clientName) { - this.clientName = Objects.requireNonNull(clientName, "Null clientName"); - this.shmPrefix = DEFAULT_SHM_PREFIX; - } - - public VppJNIConnection(final String clientName, final String shmPrefix) { - this.clientName = Objects.requireNonNull(clientName, "Null clientName"); - this.shmPrefix = Objects.requireNonNull(shmPrefix, "Null shmPrefix"); - } - - /** - * Guarded by VppJNIConnection.class - */ - private static final Map connections = new HashMap<>(); - - /** - * Initiate VPP connection for current instance - * - * Multiple instances are allowed since this class is not a singleton (VPP allows multiple management connections). - * - * However only a single connection per clientName is allowed. - * - * @throws IOException in case the connection could not be established - */ - - @Override - public void connect() throws IOException { - _connect(shmPrefix); - } - - private void _connect(final String shmPrefix) throws IOException { - Objects.requireNonNull(shmPrefix, "Shared memory prefix must be defined"); - - synchronized (VppJNIConnection.class) { - if (connections.containsKey(clientName)) { - throw new IOException("Client " + clientName + " already connected"); - } - - connectionInfo = clientConnect(shmPrefix, clientName); - if (connectionInfo.status != 0) { - throw new IOException("Connection returned error " + connectionInfo.status); - } - connections.put(clientName, this); - } - } - - @Override - public final void checkActive() { - if (disconnected) { - throw new IllegalStateException("Disconnected client " + clientName); - } - } - - @Override - public final synchronized void close() { - if (!disconnected) { - disconnected = true; - try { - clientDisconnect(); - } finally { - synchronized (VppJNIConnection.class) { - connections.remove(clientName); - } - } - } - } - - public ConnectionInfo getConnectionInfo() { - return connectionInfo; - } - - /** - * VPP connection information used by plugins to reuse the connection. - */ - public static final class ConnectionInfo { - public final long queueAddress; - public final int clientIndex; - public final int status; // FIXME throw exception instead - public final int pid; - - public ConnectionInfo(long queueAddress, int clientIndex, int status, int pid) { - this.queueAddress = queueAddress; - this.clientIndex = clientIndex; - this.status = status; - this.pid = pid; - } - } - - private static native ConnectionInfo clientConnect(String shmPrefix, String clientName); - - private static native void clientDisconnect(); - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java b/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java deleted file mode 100644 index efddfdb..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.callback; - -import io.fd.vpp.jvpp.dto.ControlPingReply; - -/** - * Represents callback for control_ping message. - */ -public interface ControlPingCallback extends JVppCallback { - - void onControlPingReply(ControlPingReply reply); - -} - diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java b/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java deleted file mode 100644 index ae02063..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.callback; -import io.fd.vpp.jvpp.VppCallbackException; - -/** - * Base JVppCallback interface - */ -public interface JVppCallback { - /** - * onError callback handler used to report failing operation - * @param ex VppCallbackException object containing details about failing operation - */ - void onError(VppCallbackException ex); -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java b/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java deleted file mode 100644 index 8ab0cb2..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.callback; - -/** -* Notification callback -*/ -public interface JVppNotificationCallback { - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/coverity/SuppressFBWarnings.java b/java/jvpp-registry/io/fd/vpp/jvpp/coverity/SuppressFBWarnings.java deleted file mode 100644 index 1e780bb..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/coverity/SuppressFBWarnings.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2017 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 io.fd.vpp.jvpp.coverity; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * Used to suppress FindBugs warnings found by Coverity.
- * We don't want extra dependency, so we define our own annotation version. - * - * @see Findbugs sourceforge - */ -@Retention(RetentionPolicy.CLASS) -public @interface SuppressFBWarnings { - /** - * The set of FindBugs warnings that are to be suppressed in annotated element. The value can be a bug category, - * kind or pattern. - */ - String[] value() default {}; - - /** - * Optional documentation of the reason why the warning is suppressed - */ - String justification() default ""; -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java deleted file mode 100644 index 984e167..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -import io.fd.vpp.jvpp.JVpp; -import io.fd.vpp.jvpp.VppInvocationException; - -/** - * Represents request DTO for control_ping message. - */ -public final class ControlPing implements JVppRequest { - - @Override - public int send(final JVpp jvpp) throws VppInvocationException { - return jvpp.controlPing(this); - } - -} - - diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java deleted file mode 100644 index 61e4d0e..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -import java.util.Objects; - -/** - * Represents reply DTO for control_ping message. - */ -public final class ControlPingReply implements JVppReply { - - public int context; - public int clientIndex; - public int vpePid; - - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - final ControlPingReply that = (ControlPingReply) o; - return context == that.context && - clientIndex == that.clientIndex && - vpePid == that.vpePid; - } - - @Override - public int hashCode() { - return Objects.hash(context, clientIndex, vpePid); - } - - @Override - public String toString() { - return "ControlPingReply{" + - "context=" + context + - ", clientIndex=" + clientIndex + - ", vpePid=" + vpePid + - '}'; - } -} - diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java deleted file mode 100644 index 60b9898..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -/** -* Base interface for all dump requests -*/ -public interface JVppDump extends JVppRequest { - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java deleted file mode 100644 index 73f512d..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -/** -* Base interface for all reply DTOs -*/ -public interface JVppReply { - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java deleted file mode 100644 index 1511139..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -/** -* Base interface for all dump replies -*/ -public interface JVppReplyDump> - extends JVppReply { - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java b/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java deleted file mode 100644 index 9b301da..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.dto; - -import io.fd.vpp.jvpp.JVpp; -import io.fd.vpp.jvpp.VppInvocationException; - -/** -* Base interface for all request DTOs -*/ -public interface JVppRequest { - - /** - * Invoke current operation asynchronously on VPP - * - * @return context id of this request. Can be used to track incoming response - */ - int send(JVpp jvpp) throws VppInvocationException; - -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java b/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java deleted file mode 100644 index ac85f53..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.future; - - -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; - -import io.fd.vpp.jvpp.JVpp; -import io.fd.vpp.jvpp.JVppRegistry; -import io.fd.vpp.jvpp.VppInvocationException; -import io.fd.vpp.jvpp.dto.JVppDump; -import io.fd.vpp.jvpp.dto.JVppReply; -import io.fd.vpp.jvpp.dto.JVppReplyDump; -import io.fd.vpp.jvpp.dto.JVppRequest; - -/** - * Future facade on top of JVpp - */ -public abstract class AbstractFutureJVppInvoker implements FutureJVppInvoker { - - private final JVpp jvpp; - private final JVppRegistry registry; - - /** - * Guarded by self - */ - private final Map>> requests; - - protected AbstractFutureJVppInvoker(final JVpp jvpp, final JVppRegistry registry, - final Map>> requestMap) { - this.jvpp = Objects.requireNonNull(jvpp, "jvpp should not be null"); - this.registry = Objects.requireNonNull(registry, "registry should not be null"); - // Request map represents the shared state between this facade and it's callback - // where facade puts futures in and callback completes + removes them - this.requests = Objects.requireNonNull(requestMap, "Null requestMap"); - } - - protected final Map>> getRequests() { - synchronized (requests) { - return requests; - } - } - - // TODO use Optional in Future, java8 - - @Override - @SuppressWarnings("unchecked") - public > CompletionStage send(REQ req) { - try { - // jvpp.send() can go to waiting state if sending queue is full, putting it into same - // synchronization block as used by receiving part (synchronized(requests)) can lead - // to deadlock between these two sides or at least slowing sending process by slow - // reader - final CompletableFuture replyCompletableFuture; - final int contextId = jvpp.send(req); - - if(req instanceof JVppDump) { - throw new IllegalArgumentException("Send with empty reply dump has to be used in case of dump calls"); - } - - synchronized(requests) { - CompletableFuture> replyFuture = requests.get(contextId); - if (replyFuture == null) { - // reply not yet received, put new future into map - replyCompletableFuture = new CompletableFuture<>(); - requests.put(contextId, replyCompletableFuture); - } else { - // reply already received (should be completed by reader), - // remove future from map and return it to caller - replyCompletableFuture = (CompletableFuture) replyFuture; - requests.remove(contextId); - } - } - - // TODO in case of timeouts/missing replies, requests from the map are not removed - // consider adding cancel method, that would remove requests from the map and cancel - // associated replyCompletableFuture - - return replyCompletableFuture; - } catch (VppInvocationException ex) { - final CompletableFuture replyCompletableFuture = new CompletableFuture<>(); - replyCompletableFuture.completeExceptionally(ex); - return replyCompletableFuture; - } - } - - @Override - @SuppressWarnings("unchecked") - public , DUMP extends JVppReplyDump> CompletionStage send( - REQ req, DUMP emptyReplyDump) { - try { - // jvpp.send() and registry.controlPing() can go to waiting state if sending queue is full, - // putting it into same synchronization block as used by receiving part (synchronized(requests)) - // can lead to deadlock between these two sides or at least slowing sending process by slow reader - final CompletableDumpFuture replyDumpFuture; - final int contextId = jvpp.send(req); - - if(!(req instanceof JVppDump)) { - throw new IllegalArgumentException("Send without empty reply dump has to be used in case of regular calls"); - } - - synchronized(requests) { - CompletableFuture> replyFuture = requests.get(contextId); - if (replyFuture == null) { - // reply not received yet, put new future to map - replyDumpFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump); - requests.put(contextId, replyDumpFuture); - } else { - // reply already received, save existing future - replyDumpFuture = (CompletableDumpFuture) replyFuture; - } - } - - final int pingId = registry.controlPing(jvpp.getClass()); - - synchronized(requests) { - if (requests.remove(pingId) == null) { - // reply not received yet, put future into map under pingId - requests.put(pingId, replyDumpFuture); - } else { - // reply already received, complete future - // ping reply couldn't complete the future because it is not in map under - // ping id - replyDumpFuture.complete(replyDumpFuture.getReplyDump()); - requests.remove(contextId); - } - } - - // TODO in case of timeouts/missing replies, requests from the map are not removed - // consider adding cancel method, that would remove requests from the map and cancel - // associated replyCompletableFuture - - return replyDumpFuture; - } catch (VppInvocationException ex) { - final CompletableFuture replyCompletableFuture = new CompletableFuture<>(); - replyCompletableFuture.completeExceptionally(ex); - return replyCompletableFuture; - } - } - - public static final class CompletableDumpFuture> extends CompletableFuture { - private final T replyDump; - private final int contextId; - - public CompletableDumpFuture(final int contextId, final T emptyDump) { - this.contextId = contextId; - this.replyDump = emptyDump; - } - - public int getContextId() { - return contextId; - } - - public T getReplyDump() { - return replyDump; - } - } - - @Override - public void close() throws Exception { - jvpp.close(); - } -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java b/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java deleted file mode 100644 index 65250ed..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.future; - - -import io.fd.vpp.jvpp.dto.JVppReply; -import io.fd.vpp.jvpp.dto.JVppReplyDump; -import io.fd.vpp.jvpp.dto.JVppRequest; - -import java.util.concurrent.CompletionStage; -import io.fd.vpp.jvpp.notification.EventRegistryProvider; - -/** -* Future facade on top of JVpp -*/ -public interface FutureJVppInvoker extends EventRegistryProvider, AutoCloseable { - - /** - * Invoke asynchronous operation on VPP - * - * @return CompletionStage with future result of an async VPP call - * @throws io.fd.vpp.jvpp.VppInvocationException when send request failed with details - */ - > CompletionStage send(REQ req); - - - /** - * Invoke asynchronous dump operation on VPP - * - * @return CompletionStage with aggregated future result of an async VPP dump call - * @throws io.fd.vpp.jvpp.VppInvocationException when send request failed with details - */ - , DUMP extends JVppReplyDump> CompletionStage send( - REQ req, DUMP emptyReplyDump); -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistry.java b/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistry.java deleted file mode 100644 index 12515a5..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistry.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.notification; - -/** - * Base registry for notification callbacks. - */ -public interface EventRegistry extends AutoCloseable { - - void close(); -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistryProvider.java b/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistryProvider.java deleted file mode 100644 index 1ac5d55..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/notification/EventRegistryProvider.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.notification; - -/** - * Provides notification registry - */ -public interface EventRegistryProvider { - - /** - * Get current notification registry instance - */ - EventRegistry getEventRegistry(); -} diff --git a/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java b/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java deleted file mode 100644 index 27b4d29..0000000 --- a/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 io.fd.vpp.jvpp.test; - -import io.fd.vpp.jvpp.JVppRegistry; -import io.fd.vpp.jvpp.JVppRegistryImpl; - -/** - * Run using: - * sudo java -cp build-vpp-native/vpp-api/java/jvpp-registry-16.09.jar io.fd.vpp.jvpp.test.ConnectionTest - */ -public class ConnectionTest { - - private static void testConnect() throws Exception { - System.out.println("Testing JNI connection with JVppRegistry"); - final JVppRegistry registry = new JVppRegistryImpl("ConnectionTest"); - try { - System.out.println("Successfully connected to vpp"); - Thread.sleep(5000); - System.out.println("Disconnecting..."); - Thread.sleep(1000); - } finally { - registry.close(); - } - } - - public static void main(String[] args) throws Exception { - testConnect(); - } -} diff --git a/java/jvpp-registry/jvpp_registry.c b/java/jvpp-registry/jvpp_registry.c index bbe9719..a8dfa60 100644 --- a/java/jvpp-registry/jvpp_registry.c +++ b/java/jvpp-registry/jvpp_registry.c @@ -22,8 +22,8 @@ #include #include -#include "io_fd_vpp_jvpp_VppJNIConnection.h" -#include "io_fd_vpp_jvpp_JVppRegistryImpl.h" +#include "io_fd_jvpp_VppJNIConnection.h" +#include "io_fd_jvpp_JVppRegistryImpl.h" #include #define vl_typedefs /* define message structures */ @@ -145,7 +145,7 @@ static void vl_api_control_ping_reply_t_handler( rm->controlPingReplyClass, "", "()V"); jmethodID callbackMethod = (*env)->GetMethodID(env, rm->registryClass, "onControlPingReply", - "(Lio/fd/vpp/jvpp/dto/ControlPingReply;)V"); + "(Lio/fd/jvpp/dto/ControlPingReply;)V"); jobject dto = (*env)->NewObject(env, rm->controlPingReplyClass, constructor); @@ -276,7 +276,7 @@ static int connect_to_vpe(char *shm_prefix, char *name) { return send_initial_control_ping(); } -JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect( +JNIEXPORT jobject JNICALL Java_io_fd_jvpp_VppJNIConnection_clientConnect( JNIEnv *env, jclass obj, jstring shmPrefix, jstring clientName) { /* * TODO introducing memory prefix as variable can be used in hc2vpp @@ -291,7 +291,7 @@ JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect( jvpp_registry_main_t * rm = &jvpp_registry_main; jclass connectionInfoClass = (*env)->FindClass(env, - "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo"); + "io/fd/jvpp/VppJNIConnection$ConnectionInfo"); jmethodID connectionInfoConstructor = (*env)->GetMethodID(env, connectionInfoClass, "", "(JIII)V"); @@ -327,7 +327,7 @@ JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect( (jint) jm->my_client_index, (jint) rv, (jint) rm->vpe_pid); } -JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0( +JNIEXPORT jint JNICALL Java_io_fd_jvpp_JVppRegistryImpl_controlPing0( JNIEnv *env, jobject regstryObject) { jvpp_main_t * jm = &jvpp_main; vl_api_control_ping_t * mp; @@ -353,7 +353,7 @@ JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0( return my_context_id; } -JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientDisconnect( +JNIEXPORT void JNICALL Java_io_fd_jvpp_VppJNIConnection_clientDisconnect( JNIEnv *env, jclass clazz) { jvpp_registry_main_t * rm = &jvpp_registry_main; rm->is_connected = 0; // TODO make thread safe @@ -380,7 +380,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { } rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env, - (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply")); + (*env)->FindClass(env, "io/fd/jvpp/dto/ControlPingReply")); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); clib_warning("Failed to cache class references\n"); @@ -388,7 +388,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { } rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env, - (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException")); + (*env)->FindClass(env, "io/fd/jvpp/VppCallbackException")); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); return JNI_ERR; -- cgit 1.2.3-korg