From e85581cd07cf70d19870d8f858420f252ced3405 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Wed, 28 Sep 2016 10:12:04 +0200 Subject: VPP-378: rename jvpp package to the same as groupId of deployed jars Related changes: - NSH: https://gerrit.fd.io/r/#/c/3181/ - Honeycomb: https://gerrit.fd.io/r/#/c/3182 Change-Id: Ifdd6b8b575916fdf99794618dbe604c2e17e8e82 Signed-off-by: Marek Gradzki --- .../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 | 133 ++++++++++++++++++++ .../io/fd/vpp/jvpp/NativeLibraryLoader.java | 73 +++++++++++ .../io/fd/vpp/jvpp/VppBaseCallException.java | 60 +++++++++ .../io/fd/vpp/jvpp/VppCallbackException.java | 47 +++++++ .../io/fd/vpp/jvpp/VppConnection.java | 45 +++++++ .../io/fd/vpp/jvpp/VppInvocationException.java | 33 +++++ .../io/fd/vpp/jvpp/VppJNIConnection.java | 137 ++++++++++++++++++++ .../fd/vpp/jvpp/callback/ControlPingCallback.java | 29 +++++ .../io/fd/vpp/jvpp/callback/JVppCallback.java | 29 +++++ .../jvpp/callback/JVppNotificationCallback.java | 24 ++++ .../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/JVppNotification.java | 23 ++++ .../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 | 139 +++++++++++++++++++++ .../io/fd/vpp/jvpp/future/FutureJVppInvoker.java | 49 ++++++++ .../jvpp/notification/NotificationRegistry.java | 25 ++++ .../notification/NotificationRegistryProvider.java | 28 +++++ .../io/fd/vpp/jvpp/test/ConnectionTest.java | 43 +++++++ vpp-api/java/jvpp-registry/jvpp_registry.c | 18 +-- .../java/jvpp-registry/org/openvpp/jvpp/JVpp.java | 56 --------- .../org/openvpp/jvpp/JVppRegistry.java | 76 ----------- .../org/openvpp/jvpp/JVppRegistryImpl.java | 133 -------------------- .../org/openvpp/jvpp/NativeLibraryLoader.java | 73 ----------- .../org/openvpp/jvpp/VppBaseCallException.java | 60 --------- .../org/openvpp/jvpp/VppCallbackException.java | 47 ------- .../org/openvpp/jvpp/VppConnection.java | 45 ------- .../org/openvpp/jvpp/VppInvocationException.java | 33 ----- .../org/openvpp/jvpp/VppJNIConnection.java | 137 -------------------- .../openvpp/jvpp/callback/ControlPingCallback.java | 29 ----- .../org/openvpp/jvpp/callback/JVppCallback.java | 29 ----- .../jvpp/callback/JVppNotificationCallback.java | 24 ---- .../org/openvpp/jvpp/dto/ControlPing.java | 34 ----- .../org/openvpp/jvpp/dto/ControlPingReply.java | 58 --------- .../org/openvpp/jvpp/dto/JVppDump.java | 24 ---- .../org/openvpp/jvpp/dto/JVppNotification.java | 23 ---- .../org/openvpp/jvpp/dto/JVppReply.java | 24 ---- .../org/openvpp/jvpp/dto/JVppReplyDump.java | 25 ---- .../org/openvpp/jvpp/dto/JVppRequest.java | 34 ----- .../jvpp/future/AbstractFutureJVppInvoker.java | 139 --------------------- .../org/openvpp/jvpp/future/FutureJVppInvoker.java | 49 -------- .../jvpp/notification/NotificationRegistry.java | 25 ---- .../notification/NotificationRegistryProvider.java | 28 ----- .../org/openvpp/jvpp/test/ConnectionTest.java | 43 ------- 49 files changed, 1257 insertions(+), 1257 deletions(-) create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppNotification.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistry.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistryProvider.java create mode 100644 vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVpp.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistry.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistryImpl.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/NativeLibraryLoader.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppBaseCallException.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppCallbackException.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppConnection.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppInvocationException.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppJNIConnection.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/ControlPingCallback.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppCallback.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppNotificationCallback.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPing.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPingReply.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppDump.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppNotification.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReply.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReplyDump.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppRequest.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistry.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistryProvider.java delete mode 100644 vpp-api/java/jvpp-registry/org/openvpp/jvpp/test/ConnectionTest.java (limited to 'vpp-api/java/jvpp-registry') diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVpp.java new file mode 100644 index 00000000..55f25a7b --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistry.java new file mode 100644 index 00000000..6535db02 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java new file mode 100644 index 00000000..fb2ef1c0 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/JVppRegistryImpl.java @@ -0,0 +1,133 @@ +/* + * 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 java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import io.fd.vpp.jvpp.callback.ControlPingCallback; +import io.fd.vpp.jvpp.callback.JVppCallback; +import io.fd.vpp.jvpp.dto.ControlPingReply; + +/** + * 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; + private final ConcurrentMap pluginRegistry; + private final ConcurrentMap pingCalls; + + public JVppRegistryImpl(final String clientName) throws IOException { + connection = new VppJNIConnection(clientName); + connection.connect(); + pluginRegistry = new ConcurrentHashMap<>(); + pingCalls = new ConcurrentHashMap<>(); + } + + @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.putIfAbsent(name, callback) != null) { + throw new IllegalArgumentException(String.format("Callback for plugin %s was already registered", name)); + } + jvpp.init(this, callback, connection.getConnectionInfo().queueAddress, + connection.getConnectionInfo().clientIndex); + } + + @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); + + int context = controlPing0(); + if (context < 0) { + throw new VppInvocationException("controlPing", context); + } + + pingCalls.put(context, callback); + return context; + } + + + @Override + public void onControlPingReply(final ControlPingReply reply) { + final ControlPingCallback callback = pingCalls.get(reply.context); + if (callback == null) { + LOG.log(Level.WARNING, "No callback was registered for reply id={0} ", reply.context); + 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 = 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(); + } +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/NativeLibraryLoader.java new file mode 100644 index 00000000..ce6d1bfc --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java new file mode 100644 index 00000000..d71e3055 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppBaseCallException.java @@ -0,0 +1,60 @@ +/* + * 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 ); + } + } + + /** + * 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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java new file mode 100644 index 00000000..ccfcbd3c --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppCallbackException.java @@ -0,0 +1,47 @@ +/* + * 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 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 int ctxId, final int errorCode ){ + super(methodName, errorCode); + this.ctxId = ctxId; + } + + /** + * Returns api request context identifier. + * + * @return value of context identifier + */ + public int getCtxId() { + return ctxId; + } + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppConnection.java new file mode 100644 index 00000000..e6fd3bdb --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppInvocationException.java new file mode 100644 index 00000000..a7ccb197 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java new file mode 100644 index 00000000..7178bcf7 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/VppJNIConnection.java @@ -0,0 +1,137 @@ +/* + * 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 java.io.IOException; +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()); + + static { + final String libName = "libjvpp_registry.so.0.0.0"; + try { + loadLibrary(libName, VppJNIConnection.class); + } catch (IOException e) { + LOG.log(Level.SEVERE, String.format("Can't find vpp jni library: %s", libName), e); + throw new ExceptionInInitializerError(e); + } + } + + private ConnectionInfo connectionInfo; + + private final String clientName; + 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"); + } + + /** + * 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(); + } + + private void _connect() throws IOException { + synchronized (VppJNIConnection.class) { + if (connections.containsKey(clientName)) { + throw new IOException("Client " + clientName + " already connected"); + } + + connectionInfo = clientConnect(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 ConnectionInfo(long queueAddress, int clientIndex, int status) { + this.queueAddress = queueAddress; + this.clientIndex = clientIndex; + this.status = status; + } + } + + private static native ConnectionInfo clientConnect(String clientName); + + private static native void clientDisconnect(); + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/ControlPingCallback.java new file mode 100644 index 00000000..efddfdbb --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppCallback.java new file mode 100644 index 00000000..ae02063b --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/callback/JVppNotificationCallback.java new file mode 100644 index 00000000..8ab0cb21 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.vpp.jvpp.callback; + +/** +* Notification callback +*/ +public interface JVppNotificationCallback { + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPing.java new file mode 100644 index 00000000..984e1674 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/ControlPingReply.java new file mode 100644 index 00000000..61e4d0e4 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppDump.java new file mode 100644 index 00000000..60b98984 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.vpp.jvpp.dto; + +/** +* Base interface for all dump requests +*/ +public interface JVppDump extends JVppRequest { + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppNotification.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppNotification.java new file mode 100644 index 00000000..5554f501 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppNotification.java @@ -0,0 +1,23 @@ +/* + * 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 notification DTOs +*/ +public interface JVppNotification { +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReply.java new file mode 100644 index 00000000..73f512d4 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.vpp.jvpp.dto; + +/** +* Base interface for all reply DTOs +*/ +public interface JVppReply { + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppReplyDump.java new file mode 100644 index 00000000..15111395 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.vpp.jvpp.dto; + +/** +* Base interface for all dump replies +*/ +public interface JVppReplyDump> + extends JVppReply { + +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/dto/JVppRequest.java new file mode 100644 index 00000000..9b301da2 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java new file mode 100644 index 00000000..2067a91a --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/AbstractFutureJVppInvoker.java @@ -0,0 +1,139 @@ +/* + * 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() { + return this.requests; + } + + // TODO use Optional in Future, java8 + + @Override + @SuppressWarnings("unchecked") + public > CompletionStage send(REQ req) { + synchronized(requests) { + try { + 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"); + } + replyCompletableFuture = new CompletableFuture<>(); + requests.put(contextId, replyCompletableFuture); + + // 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) { + synchronized(requests) { + try { + final CompletableDumpFuture replyCompletableFuture; + 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"); + } + replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump); + + requests.put(contextId, replyCompletableFuture); + requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture); + + // 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; + } + } + } + + public static final class CompletableDumpFuture> extends CompletableFuture { + private final T replyDump; + private final long contextId; + + public CompletableDumpFuture(final long contextId, final T emptyDump) { + this.contextId = contextId; + this.replyDump = emptyDump; + } + + public long getContextId() { + return contextId; + } + + public T getReplyDump() { + return replyDump; + } + } + + @Override + public void close() throws Exception { + jvpp.close(); + } +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/future/FutureJVppInvoker.java new file mode 100644 index 00000000..7a48e418 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/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.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.NotificationRegistryProvider; + +/** +* Future facade on top of JVpp +*/ +public interface FutureJVppInvoker extends NotificationRegistryProvider, 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/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistry.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistry.java new file mode 100644 index 00000000..3c72ff79 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistry.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.vpp.jvpp.notification; + +/** + * Base registry for notification callbacks. + */ +public interface NotificationRegistry extends AutoCloseable { + + void close(); +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistryProvider.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistryProvider.java new file mode 100644 index 00000000..4a6e06b7 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/notification/NotificationRegistryProvider.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.vpp.jvpp.notification; + +/** + * Provides notification registry + */ +public interface NotificationRegistryProvider { + + /** + * Get current notification registry instance + */ + NotificationRegistry getNotificationRegistry(); +} diff --git a/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java new file mode 100644 index 00000000..ea1d8647 --- /dev/null +++ b/vpp-api/java/jvpp-registry/io/fd/vpp/jvpp/test/ConnectionTest.java @@ -0,0 +1,43 @@ +/* + * 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"); + JVppRegistry registry = new JVppRegistryImpl("ConnectionTest"); + System.out.println("Successfully connected to vpp"); + + Thread.sleep(5000); + + System.out.println("Disconnecting..."); + registry.close(); + Thread.sleep(1000); + } + + public static void main(String[] args) throws Exception { + testConnect(); + } +} diff --git a/vpp-api/java/jvpp-registry/jvpp_registry.c b/vpp-api/java/jvpp-registry/jvpp_registry.c index 1c9871bb..af20916e 100644 --- a/vpp-api/java/jvpp-registry/jvpp_registry.c +++ b/vpp-api/java/jvpp-registry/jvpp_registry.c @@ -21,8 +21,8 @@ #include #include -#include "org_openvpp_jvpp_VppJNIConnection.h" -#include "org_openvpp_jvpp_JVppRegistryImpl.h" +#include "io_fd_vpp_jvpp_VppJNIConnection.h" +#include "io_fd_vpp_jvpp_JVppRegistryImpl.h" #include #define vl_typedefs /* define message structures */ @@ -135,7 +135,7 @@ static void vl_api_control_ping_reply_t_handler( rm->controlPingReplyClass, "", "()V"); jmethodID callbackMethod = (*env)->GetMethodID(env, rm->registryClass, "onControlPingReply", - "(Lorg/openvpp/jvpp/dto/ControlPingReply;)V"); + "(Lio/fd/vpp/jvpp/dto/ControlPingReply;)V"); jobject dto = (*env)->NewObject(env, rm->controlPingReplyClass, constructor); @@ -220,7 +220,7 @@ static int connect_to_vpe(char *name) { return 0; } -JNIEXPORT jobject JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientConnect( +JNIEXPORT jobject JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientConnect( JNIEnv *env, jclass obj, jstring clientName) { int rv; const char *client_name; @@ -229,7 +229,7 @@ JNIEXPORT jobject JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientConnect( jvpp_registry_main_t * rm = &jvpp_registry_main; jclass connectionInfoClass = (*env)->FindClass(env, - "org/openvpp/jvpp/VppJNIConnection$ConnectionInfo"); + "io/fd/vpp/jvpp/VppJNIConnection$ConnectionInfo"); jmethodID connectionInfoConstructor = (*env)->GetMethodID(env, connectionInfoClass, "", "(JII)V"); @@ -266,7 +266,7 @@ JNIEXPORT jobject JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientConnect( (jint) jm->my_client_index, (jint) rv); } -JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppRegistryImpl_controlPing0( +JNIEXPORT jint JNICALL Java_io_fd_vpp_jvpp_JVppRegistryImpl_controlPing0( JNIEnv *env, jobject regstryObject) { jvpp_main_t * jm = &jvpp_main; vl_api_control_ping_t * mp; @@ -292,7 +292,7 @@ JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppRegistryImpl_controlPing0( return my_context_id; } -JNIEXPORT void JNICALL Java_org_openvpp_jvpp_VppJNIConnection_clientDisconnect( +JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_VppJNIConnection_clientDisconnect( JNIEnv *env, jclass clazz) { jvpp_registry_main_t * rm = &jvpp_registry_main; rm->is_connected = 0; // TODO make thread safe @@ -319,7 +319,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { } rm->controlPingReplyClass = (jclass) (*env)->NewGlobalRef(env, - (*env)->FindClass(env, "org/openvpp/jvpp/dto/ControlPingReply")); + (*env)->FindClass(env, "io/fd/vpp/jvpp/dto/ControlPingReply")); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); clib_warning("Failed to cache class references\n"); @@ -327,7 +327,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { } rm->callbackExceptionClass = (jclass) (*env)->NewGlobalRef(env, - (*env)->FindClass(env, "org/openvpp/jvpp/VppCallbackException")); + (*env)->FindClass(env, "io/fd/vpp/jvpp/VppCallbackException")); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); return JNI_ERR; diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVpp.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVpp.java deleted file mode 100644 index 53bae04c..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp; - -import org.openvpp.jvpp.callback.JVppCallback; -import org.openvpp.jvpp.dto.ControlPing; -import org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistry.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistry.java deleted file mode 100644 index 4515bb3f..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp; - -import org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistryImpl.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistryImpl.java deleted file mode 100644 index bb6730f4..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/JVppRegistryImpl.java +++ /dev/null @@ -1,133 +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 org.openvpp.jvpp; - -import static java.util.Objects.requireNonNull; - -import java.io.IOException; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.openvpp.jvpp.callback.ControlPingCallback; -import org.openvpp.jvpp.callback.JVppCallback; -import org.openvpp.jvpp.dto.ControlPingReply; - -/** - * 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; - private final ConcurrentMap pluginRegistry; - private final ConcurrentMap pingCalls; - - public JVppRegistryImpl(final String clientName) throws IOException { - connection = new VppJNIConnection(clientName); - connection.connect(); - pluginRegistry = new ConcurrentHashMap<>(); - pingCalls = new ConcurrentHashMap<>(); - } - - @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.putIfAbsent(name, callback) != null) { - throw new IllegalArgumentException(String.format("Callback for plugin %s was already registered", name)); - } - jvpp.init(this, callback, connection.getConnectionInfo().queueAddress, - connection.getConnectionInfo().clientIndex); - } - - @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); - - int context = controlPing0(); - if (context < 0) { - throw new VppInvocationException("controlPing", context); - } - - pingCalls.put(context, callback); - return context; - } - - - @Override - public void onControlPingReply(final ControlPingReply reply) { - final ControlPingCallback callback = pingCalls.get(reply.context); - if (callback == null) { - LOG.log(Level.WARNING, "No callback was registered for reply id={0} ", reply.context); - 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 = 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(); - } -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/NativeLibraryLoader.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/NativeLibraryLoader.java deleted file mode 100644 index d00c41d5..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppBaseCallException.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppBaseCallException.java deleted file mode 100644 index 792af2c6..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppBaseCallException.java +++ /dev/null @@ -1,60 +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 org.openvpp.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 ); - } - } - - /** - * 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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppCallbackException.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppCallbackException.java deleted file mode 100644 index 3d2a1cb2..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppCallbackException.java +++ /dev/null @@ -1,47 +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 org.openvpp.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 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 int ctxId, final int errorCode ){ - super(methodName, errorCode); - this.ctxId = ctxId; - } - - /** - * Returns api request context identifier. - * - * @return value of context identifier - */ - public int getCtxId() { - return ctxId; - } - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppConnection.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppConnection.java deleted file mode 100644 index e7055f90..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppInvocationException.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppInvocationException.java deleted file mode 100644 index 298bcd0a..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppJNIConnection.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppJNIConnection.java deleted file mode 100644 index f3529874..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/VppJNIConnection.java +++ /dev/null @@ -1,137 +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 org.openvpp.jvpp; - -import static org.openvpp.jvpp.NativeLibraryLoader.loadLibrary; - -import java.io.IOException; -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()); - - static { - final String libName = "libjvpp_registry.so.0.0.0"; - try { - loadLibrary(libName, VppJNIConnection.class); - } catch (IOException e) { - LOG.log(Level.SEVERE, String.format("Can't find vpp jni library: %s", libName), e); - throw new ExceptionInInitializerError(e); - } - } - - private ConnectionInfo connectionInfo; - - private final String clientName; - 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"); - } - - /** - * 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(); - } - - private void _connect() throws IOException { - synchronized (VppJNIConnection.class) { - if (connections.containsKey(clientName)) { - throw new IOException("Client " + clientName + " already connected"); - } - - connectionInfo = clientConnect(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 ConnectionInfo(long queueAddress, int clientIndex, int status) { - this.queueAddress = queueAddress; - this.clientIndex = clientIndex; - this.status = status; - } - } - - private static native ConnectionInfo clientConnect(String clientName); - - private static native void clientDisconnect(); - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/ControlPingCallback.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/ControlPingCallback.java deleted file mode 100644 index 529ea220..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.callback; - -import org.openvpp.jvpp.dto.ControlPingReply; - -/** - * Represents callback for control_ping message. - */ -public interface ControlPingCallback extends JVppCallback { - - void onControlPingReply(ControlPingReply reply); - -} - diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppCallback.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppCallback.java deleted file mode 100644 index f681e379..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.callback; -import org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppNotificationCallback.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/callback/JVppNotificationCallback.java deleted file mode 100644 index 72a75c83..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.callback; - -/** -* Notification callback -*/ -public interface JVppNotificationCallback { - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPing.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPing.java deleted file mode 100644 index cc59836d..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.dto; - -import org.openvpp.jvpp.JVpp; -import org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPingReply.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/ControlPingReply.java deleted file mode 100644 index 6377fcdf..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppDump.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppDump.java deleted file mode 100644 index 295bbba8..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.dto; - -/** -* Base interface for all dump requests -*/ -public interface JVppDump extends JVppRequest { - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppNotification.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppNotification.java deleted file mode 100644 index 7d0fecb7..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppNotification.java +++ /dev/null @@ -1,23 +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 org.openvpp.jvpp.dto; - -/** -* Base interface for all notification DTOs -*/ -public interface JVppNotification { -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReply.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReply.java deleted file mode 100644 index 2f4964c4..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.dto; - -/** -* Base interface for all reply DTOs -*/ -public interface JVppReply { - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReplyDump.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppReplyDump.java deleted file mode 100644 index 4aecedc1..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.dto; - -/** -* Base interface for all dump replies -*/ -public interface JVppReplyDump> - extends JVppReply { - -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppRequest.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/dto/JVppRequest.java deleted file mode 100644 index 1216edf8..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.dto; - -import org.openvpp.jvpp.JVpp; -import org.openvpp.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/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java deleted file mode 100644 index 53a445e6..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/AbstractFutureJVppInvoker.java +++ /dev/null @@ -1,139 +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 org.openvpp.jvpp.future; - - -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import org.openvpp.jvpp.JVpp; -import org.openvpp.jvpp.JVppRegistry; -import org.openvpp.jvpp.VppInvocationException; -import org.openvpp.jvpp.dto.JVppDump; -import org.openvpp.jvpp.dto.JVppReply; -import org.openvpp.jvpp.dto.JVppReplyDump; -import org.openvpp.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() { - return this.requests; - } - - // TODO use Optional in Future, java8 - - @Override - @SuppressWarnings("unchecked") - public > CompletionStage send(REQ req) { - synchronized(requests) { - try { - 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"); - } - replyCompletableFuture = new CompletableFuture<>(); - requests.put(contextId, replyCompletableFuture); - - // 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) { - synchronized(requests) { - try { - final CompletableDumpFuture replyCompletableFuture; - 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"); - } - replyCompletableFuture = new CompletableDumpFuture<>(contextId, emptyReplyDump); - - requests.put(contextId, replyCompletableFuture); - requests.put(registry.controlPing(jvpp.getClass()), replyCompletableFuture); - - // 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; - } - } - } - - public static final class CompletableDumpFuture> extends CompletableFuture { - private final T replyDump; - private final long contextId; - - public CompletableDumpFuture(final long contextId, final T emptyDump) { - this.contextId = contextId; - this.replyDump = emptyDump; - } - - public long getContextId() { - return contextId; - } - - public T getReplyDump() { - return replyDump; - } - } - - @Override - public void close() throws Exception { - jvpp.close(); - } -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/future/FutureJVppInvoker.java deleted file mode 100644 index 721f95c9..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/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 org.openvpp.jvpp.future; - - -import org.openvpp.jvpp.dto.JVppReply; -import org.openvpp.jvpp.dto.JVppReplyDump; -import org.openvpp.jvpp.dto.JVppRequest; - -import java.util.concurrent.CompletionStage; -import org.openvpp.jvpp.notification.NotificationRegistryProvider; - -/** -* Future facade on top of JVpp -*/ -public interface FutureJVppInvoker extends NotificationRegistryProvider, AutoCloseable { - - /** - * Invoke asynchronous operation on VPP - * - * @return CompletionStage with future result of an async VPP call - * @throws org.openvpp.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 org.openvpp.jvpp.VppInvocationException when send request failed with details - */ - , DUMP extends JVppReplyDump> CompletionStage send( - REQ req, DUMP emptyReplyDump); -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistry.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistry.java deleted file mode 100644 index 27349f28..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistry.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 org.openvpp.jvpp.notification; - -/** - * Base registry for notification callbacks. - */ -public interface NotificationRegistry extends AutoCloseable { - - void close(); -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistryProvider.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistryProvider.java deleted file mode 100644 index 46c534a0..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/notification/NotificationRegistryProvider.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 org.openvpp.jvpp.notification; - -/** - * Provides notification registry - */ -public interface NotificationRegistryProvider { - - /** - * Get current notification registry instance - */ - NotificationRegistry getNotificationRegistry(); -} diff --git a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/test/ConnectionTest.java b/vpp-api/java/jvpp-registry/org/openvpp/jvpp/test/ConnectionTest.java deleted file mode 100644 index d134a1c1..00000000 --- a/vpp-api/java/jvpp-registry/org/openvpp/jvpp/test/ConnectionTest.java +++ /dev/null @@ -1,43 +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 org.openvpp.jvpp.test; - -import org.openvpp.jvpp.JVppRegistry; -import org.openvpp.jvpp.JVppRegistryImpl; - -/** - * Run using: - * sudo java -cp build-vpp-native/vpp-api/java/jvpp-registry-16.09.jar org.openvpp.jvpp.test.ConnectionTest - */ -public class ConnectionTest { - - private static void testConnect() throws Exception { - System.out.println("Testing JNI connection with JVppRegistry"); - JVppRegistry registry = new JVppRegistryImpl("ConnectionTest"); - System.out.println("Successfully connected to vpp"); - - Thread.sleep(5000); - - System.out.println("Disconnecting..."); - registry.close(); - Thread.sleep(1000); - } - - public static void main(String[] args) throws Exception { - testConnect(); - } -} -- cgit 1.2.3-korg