Refactor vpp-japi
vlib does not allow concurrent connections, hence make sure the Java API
does not allow the user to instantiate them.
Also hide native methods from direct user invocation behind defensive
methods also detect if a particular connection has been terminated.
Disconnect is hidden befind standard close() method, as specified by
AutoCloseable.
Change-Id: Ib5079200ae4216cad84358a2174a41e90271a30b
Signed-off-by: Robert Varga <nite@hq.sk>
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java b/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java
index 5c35897..df5b953 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppApiCallbacks.java
@@ -14,9 +14,14 @@
*/
package org.openvpp.vppjapi;
+
+import java.io.IOException;
import org.openvpp.vppjapi.vppApi;
public abstract class vppApiCallbacks extends vppApi {
+ public vppApiCallbacks(String clientName) throws IOException {
+ super(clientName);
+ }
/* Disabled!
*
* public abstract void interfaceDetails(
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java
index 01fe11d..db859a0 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainDetails.java
@@ -17,7 +17,7 @@
import org.openvpp.vppjapi.vppBridgeDomainInterfaceDetails;
-public class vppBridgeDomainDetails {
+public final class vppBridgeDomainDetails {
public String name;
public int bdId;
public boolean flood;
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java
index f17c062..ab99ee4 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppBridgeDomainInterfaceDetails.java
@@ -15,7 +15,7 @@
package org.openvpp.vppjapi;
-public class vppBridgeDomainInterfaceDetails {
+public final class vppBridgeDomainInterfaceDetails {
public String interfaceName;
public byte splitHorizonGroup;
}
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java b/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java
index cd95780..8de806d 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppConn.java
@@ -23,6 +23,7 @@
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.openvpp.vppjapi.vppVersion;
import org.openvpp.vppjapi.vppInterfaceDetails;
@@ -32,7 +33,7 @@
import org.openvpp.vppjapi.vppIPv6Address;
import org.openvpp.vppjapi.vppVxlanTunnelDetails;
-public class vppConn {
+public class vppConn implements AutoCloseable {
private static final String LIBNAME = "libvppjni.so.0.0.0";
static {
@@ -40,6 +41,7 @@
loadLibrary();
} catch (IOException | RuntimeException e) {
System.out.printf ("Can't find vpp jni library: %s\n", LIBNAME);
+ throw new ExceptionInInitializerError(e);
}
}
@@ -72,25 +74,163 @@
}
}
- public native int clientConnect(String clientName);
- public native void clientDisconnect();
- public native int getRetval(int context, int release);
- public native String getInterfaceList (String nameFilter);
- public native int swIfIndexFromName (String interfaceName);
- public native String interfaceNameFromSwIfIndex (int swIfIndex);
- public native void clearInterfaceTable ();
- public native vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter);
- public native int bridgeDomainIdFromName(String bridgeDomain);
- public native int findOrAddBridgeDomainId(String bridgeDomain);
- public native vppVersion getVppVersion();
- public native vppInterfaceCounters getInterfaceCounters(int swIfIndex);
- public native int[] bridgeDomainDump(int bdId);
- public native vppBridgeDomainDetails getBridgeDomainDetails(int bdId);
- public native vppL2Fib[] l2FibTableDump(int bdId);
- public native int bridgeDomainIdFromInterfaceName(String interfaceName);
- public native vppIPv4Address[] ipv4AddressDump(String interfaceName);
- public native vppIPv6Address[] ipv6AddressDump(String interfaceName);
- public native vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex);
- public native int setInterfaceDescription (String ifName, String ifDesc);
- public native String getInterfaceDescription (String ifName);
+ private static vppConn currentConnection = null;
+ private final AtomicBoolean disconnected = new AtomicBoolean(false);
+ private final String clientName;
+
+ // Hidden on purpose to prevent external instantiation
+ vppConn(final String clientName) throws IOException {
+ this.clientName = clientName;
+
+ synchronized (vppConn.class) {
+ if (currentConnection != null) {
+ throw new IOException("Already connected as " + currentConnection.clientName);
+ }
+
+ final int ret = clientConnect(clientName);
+ if (ret != 0) {
+ throw new IOException("Connection returned error " + ret);
+ }
+
+ currentConnection = this;
+ }
+ }
+
+ @Override
+ public final void close() {
+ if (disconnected.compareAndSet(false, true)) {
+ synchronized (vppConn.class) {
+ clientDisconnect();
+ currentConnection = null;
+ }
+ }
+ }
+
+ /**
+ * Check if this instance is connected.
+ *
+ * @throws IllegalStateException if this instance was disconnected.
+ */
+ protected final void checkConnected() {
+ if (disconnected.get()) {
+ throw new IllegalStateException("Disconnected client " + clientName);
+ }
+ }
+
+ public final int getRetval(int context, int release) {
+ checkConnected();
+ return getRetval0(context, release);
+ }
+
+ public final String getInterfaceList (String nameFilter) {
+ checkConnected();
+ return getInterfaceList0(nameFilter);
+ }
+
+ public final int swIfIndexFromName (String interfaceName) {
+ checkConnected();
+ return swIfIndexFromName0(interfaceName);
+ }
+
+ public final String interfaceNameFromSwIfIndex (int swIfIndex) {
+ checkConnected();
+ return interfaceNameFromSwIfIndex0(swIfIndex);
+ }
+
+ public final void clearInterfaceTable () {
+ checkConnected();
+ clearInterfaceTable0();
+ }
+
+ public final vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter) {
+ checkConnected();
+ return swInterfaceDump0(nameFilterValid, nameFilter);
+ }
+
+ public final int bridgeDomainIdFromName(String bridgeDomain) {
+ checkConnected();
+ return bridgeDomainIdFromName0(bridgeDomain);
+ }
+
+ public final int findOrAddBridgeDomainId(String bridgeDomain) {
+ checkConnected();
+ return findOrAddBridgeDomainId0(bridgeDomain);
+ }
+
+ public final vppVersion getVppVersion() {
+ checkConnected();
+ return getVppVersion0();
+ }
+
+ public final vppInterfaceCounters getInterfaceCounters(int swIfIndex) {
+ checkConnected();
+ return getInterfaceCounters0(swIfIndex);
+ }
+
+ public final int[] bridgeDomainDump(int bdId) {
+ checkConnected();
+ return bridgeDomainDump0(bdId);
+ }
+
+ public final vppBridgeDomainDetails getBridgeDomainDetails(int bdId) {
+ checkConnected();
+ return getBridgeDomainDetails0(bdId);
+ }
+
+ public final vppL2Fib[] l2FibTableDump(int bdId) {
+ checkConnected();
+ return l2FibTableDump0(bdId);
+ }
+
+ public final int bridgeDomainIdFromInterfaceName(String interfaceName) {
+ checkConnected();
+ return bridgeDomainIdFromInterfaceName0(interfaceName);
+ }
+
+ public final vppIPv4Address[] ipv4AddressDump(String interfaceName) {
+ checkConnected();
+ return ipv4AddressDump0(interfaceName);
+ }
+
+ public final vppIPv6Address[] ipv6AddressDump(String interfaceName) {
+ checkConnected();
+ return ipv6AddressDump0(interfaceName);
+ }
+
+ public final vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex) {
+ checkConnected();
+ return vxlanTunnelDump0(swIfIndex);
+ }
+
+ public final int setInterfaceDescription(String ifName, String ifDesc) {
+ checkConnected();
+ return setInterfaceDescription0(ifName, ifDesc);
+ }
+
+ public final String getInterfaceDescription(String ifName) {
+ checkConnected();
+ return getInterfaceDescription0(ifName);
+ }
+
+ private static native int clientConnect(String clientName);
+ private static native void clientDisconnect();
+ private static native int getRetval0(int context, int release);
+ private static native String getInterfaceList0(String nameFilter);
+ private static native int swIfIndexFromName0(String interfaceName);
+ private static native String interfaceNameFromSwIfIndex0(int swIfIndex);
+ private static native void clearInterfaceTable0();
+ private static native vppInterfaceDetails[] swInterfaceDump0(byte nameFilterValid, byte [] nameFilter);
+ private static native int bridgeDomainIdFromName0(String bridgeDomain);
+ private static native int findOrAddBridgeDomainId0(String bridgeDomain);
+ private static native vppVersion getVppVersion0();
+ private static native vppInterfaceCounters getInterfaceCounters0(int swIfIndex);
+ private static native int[] bridgeDomainDump0(int bdId);
+ private static native vppBridgeDomainDetails getBridgeDomainDetails0(int bdId);
+ private static native vppL2Fib[] l2FibTableDump0(int bdId);
+ private static native int bridgeDomainIdFromInterfaceName0(String interfaceName);
+ private static native vppIPv4Address[] ipv4AddressDump0(String interfaceName);
+ private static native vppIPv6Address[] ipv6AddressDump0(String interfaceName);
+ private static native vppVxlanTunnelDetails[] vxlanTunnelDump0(int swIfIndex);
+ private static native int setInterfaceDescription0(String ifName, String ifDesc);
+ private static native String getInterfaceDescription0(String ifName);
}
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java b/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java
index 7bf10f2..6690a5d 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppIPv6Address.java
@@ -20,8 +20,8 @@
public final byte[] ip;
public final byte prefixLength;
- public vppIPv6Address(byte[] _ip, byte _prefixLength) {
- ip = _ip;
- prefixLength = _prefixLength;
+ public vppIPv6Address(byte[] ip, byte prefixLength) {
+ this.ip = ip;
+ this.prefixLength = prefixLength;
}
}
diff --git a/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java b/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java
index 8a02ad0..b2687fb 100644
--- a/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java
+++ b/vpp-japi/japi/org/openvpp/vppjapi/vppInterfaceCounters.java
@@ -15,7 +15,7 @@
package org.openvpp.vppjapi;
-public class vppInterfaceCounters {
+public final class vppInterfaceCounters {
public final long rxOctets;
public final long rxIp4;
diff --git a/vpp-japi/japi/test/demo.java b/vpp-japi/japi/test/demo.java
index ef419d7..ea1db84 100644
--- a/vpp-japi/japi/test/demo.java
+++ b/vpp-japi/japi/test/demo.java
@@ -15,9 +15,11 @@
import org.openvpp.vppjapi.*;
-public class demo extends vppApi {
+public class demo {
public static void main (String[] args) throws Exception {
- vppApi api = new vppApi ();
+ vppApi api = new vppApi ("JavaTest");
+ System.out.printf ("Connected OK...");
+
String intlist;
int [] contexts;
int i, limit;
@@ -25,15 +27,6 @@
int rv, errors, saved_error;
long before, after;
- rv = api.clientConnect ("JavaTest");
- if (rv == 0)
- System.out.printf ("Connected OK...");
- else
- {
- System.out.printf ("clientConnect returned %d\n", rv);
- System.exit (1);
- }
-
if (false)
{
intlist = api.getInterfaceList ("");
@@ -171,7 +164,7 @@
limit, after - before,
limit / (after - before));
- api.clientDisconnect();
+ api.close();
System.out.printf ("Done...\n");
}
}
diff --git a/vpp-japi/japi/test/vppApi.java b/vpp-japi/japi/test/vppApi.java
index 96ba4a2..87af329 100644
--- a/vpp-japi/japi/test/vppApi.java
+++ b/vpp-japi/japi/test/vppApi.java
@@ -16,13 +16,13 @@
import java.net.InetAddress;
import org.openvpp.vppjapi.*;
-public class vppApi extends vppConn {
+public class vppApi {
native int controlPing();
native void test (byte[] array, byte[] array2);
public static void main (String[] args) throws Exception {
- vppApi api = new vppApi ();
+ vppConn api = new vppConn ();
String ipv6 = "db01::feed";
String ipv4 = "192.168.1.1";
InetAddress addr6 = InetAddress.getByName(ipv6);