aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2016-07-26 15:28:22 +0200
committerDave Wallace <dwallacelf@gmail.com>2016-08-16 21:26:19 +0000
commit66ea26b1bc7bbc8d54a3498dbd3d0919c4712fa8 (patch)
tree16c46e75723099ad859128af6069055e68897300 /vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py
parentf4691cd7befd4cf31a63adffc204d71b1f1548e1 (diff)
VPP-205: jvpp plugin support.
Splits jvpp into two jars jvpp-registry.jar - base jvpp functionality jvpp-core.jar - Java wrapper for vpe.api Plugins can be generated the same way jvpp-core.jar is. Example (nsh): https://gerrit.fd.io/r/#/c/2118/ Change-Id: I2254f90b2c3e423563bb91bf70877979f1e90a7d Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py')
-rw-r--r--vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py143
1 files changed, 95 insertions, 48 deletions
diff --git a/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py b/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py
index 93ffd0fb359..41df4f2ae18 100644
--- a/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py
+++ b/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py
@@ -17,25 +17,14 @@ import os, util
from string import Template
jvpp_ifc_template = Template("""
-package $base_package;
-
+package $plugin_package;
/**
- * <p>Java representation of vpe.api.
+ * <p>Java representation of plugin's api file.
* <br>It was generated by jvpp_impl_gen.py based on $inputfile
- * <br>(python representation of vpe.api generated by vppapigen).
+ * <br>(python representation of api file generated by vppapigen).
*/
-public interface JVpp extends java.lang.AutoCloseable {
-
- /**
- * Generic connect with $base_package.callback.JVppCallback callback handler
- * providing connecting to VPP
- *
- * @param callback JVppCallback instance providing callback handling
- *
- * @throws java.io.IOException if connection cannot be initiated
- */
- void connect($base_package.callback.JVppCallback callback) throws java.io.IOException;
+public interface JVpp${plugin_name} extends $base_package.JVpp {
/**
* Generic dispatch method for sending requests to VPP
@@ -44,52 +33,110 @@ public interface JVpp extends java.lang.AutoCloseable {
*/
int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException;
- @Override
- void close();
-
$methods
}
""")
jvpp_impl_template = Template("""
-package $base_package;
+package $plugin_package;
+
+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.Logger;
+import $base_package.callback.JVppCallback;
+import $base_package.VppConnection;
+import $base_package.JVppRegistry;
/**
* <p>Default implementation of JVpp interface.
* <br>It was generated by jvpp_impl_gen.py based on $inputfile
- * <br>(python representation of vpe.api generated by vppapigen).
+ * <br>(python representation of api file generated by vppapigen).
*/
-public final class JVppImpl implements $base_package.JVpp {
+public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugin_name} {
+
+ private final static Logger LOG = Logger.getLogger(JVpp${plugin_name}Impl.class.getName());
+ private static final String LIBNAME = "libjvpp_${plugin_name_underscore}.so.0.0.0";
+
+ // FIXME using NativeLibraryLoader makes load fail could not find (WantInterfaceEventsReply).
+ static {
+ try {
+ loadLibrary();
+ } catch (Exception e) {
+ LOG.severe("Can't find jvpp jni library: " + LIBNAME);
+ throw new ExceptionInInitializerError(e);
+ }
+ }
- private final $base_package.VppConnection connection;
+ private static void loadStream(final InputStream is) throws IOException {
+ final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
+ final Path p = Files.createTempFile(LIBNAME, null, PosixFilePermissions.asFileAttribute(perms));
+ try {
+ Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
+
+ try {
+ Runtime.getRuntime().load(p.toString());
+ } catch (UnsatisfiedLinkError e) {
+ throw new IOException("Failed to load library " + p, e);
+ }
+ } finally {
+ try {
+ Files.deleteIfExists(p);
+ } catch (IOException e) {
+ }
+ }
+ }
- public JVppImpl(final $base_package.VppConnection connection) {
- this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
+ private static void loadLibrary() throws IOException {
+ try (final InputStream is = JVpp${plugin_name}Impl.class.getResourceAsStream('/' + LIBNAME)) {
+ if (is == null) {
+ throw new IOException("Failed to open library resource " + LIBNAME);
+ }
+ loadStream(is);
+ }
}
+ private VppConnection connection;
+ private JVppRegistry registry;
+
+ private static native void init0(final JVppCallback callback, final long queueAddress, final int clientIndex);
@Override
- public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException {
- connection.connect(callback);
+ public void init(final JVppRegistry registry, final JVppCallback callback, final long queueAddress, final int clientIndex) {
+ this.registry = java.util.Objects.requireNonNull(registry, "registry should not be null");
+ this.connection = java.util.Objects.requireNonNull(registry.getConnection(), "connection should not be null");
+ connection.checkActive();
+ init0(callback, queueAddress, clientIndex);
}
+ private static native void close0();
@Override
public void close() {
- connection.close();
+ close0();
}
@Override
- public int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException {
+ public int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException {
return request.send(this);
}
+ @Override
+ public final int controlPing(final org.openvpp.jvpp.dto.ControlPing controlPing) throws org.openvpp.jvpp.VppInvocationException {
+ return registry.controlPing(JVpp${plugin_name}Impl.class);
+ }
+
$methods
}
""")
-method_template = Template(""" int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""")
+method_template = Template(""" int $name($plugin_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""")
method_native_template = Template(
- """ private static native int ${name}0($base_package.$dto_package.$request request);""")
-method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException {
+ """ private static native int ${name}0($plugin_package.$dto_package.$request request);""")
+method_impl_template = Template(""" public final int $name($plugin_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException {
java.util.Objects.requireNonNull(request,"Null request object");
connection.checkActive();
int result=${name}0(request);
@@ -113,9 +160,10 @@ no_arg_method_impl_template = Template(""" public final int $name() throws or
""")
-def generate_jvpp(func_list, base_package, dto_package, inputfile):
+def generate_jvpp(func_list, base_package, plugin_package, plugin_name_underscore, control_ping_class, dto_package, inputfile):
""" Generates JVpp interface and JNI implementation """
print "Generating JVpp"
+ plugin_name = util.underscore_to_camelcase_upper(plugin_name_underscore)
methods = []
methods_impl = []
@@ -131,43 +179,42 @@ def generate_jvpp(func_list, base_package, dto_package, inputfile):
continue
if len(func['args']) == 0:
- methods.append(no_arg_method_template.substitute(name=camel_case_name,
- base_package=base_package,
- dto_package=dto_package))
- methods_impl.append(
- no_arg_method_native_template.substitute(name=camel_case_name,
- base_package=base_package,
- dto_package=dto_package))
- methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
- base_package=base_package,
- dto_package=dto_package))
+ methods.append(no_arg_method_template.substitute(name=camel_case_name))
+ methods_impl.append(no_arg_method_native_template.substitute(name=camel_case_name))
+ methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name))
else:
methods.append(method_template.substitute(name=camel_case_name,
request=camel_case_name_upper,
- base_package=base_package,
+ plugin_package=plugin_package,
dto_package=dto_package))
methods_impl.append(method_native_template.substitute(name=camel_case_name,
request=camel_case_name_upper,
- base_package=base_package,
+ plugin_package=plugin_package,
dto_package=dto_package))
methods_impl.append(method_impl_template.substitute(name=camel_case_name,
request=camel_case_name_upper,
- base_package=base_package,
+ plugin_package=plugin_package,
dto_package=dto_package))
- jvpp_file = open("JVpp.java", 'w')
+ jvpp_file = open("JVpp%s.java" % plugin_name, 'w')
jvpp_file.write(
jvpp_ifc_template.substitute(inputfile=inputfile,
methods="\n".join(methods),
base_package=base_package,
+ plugin_package=plugin_package,
+ plugin_name=plugin_name,
dto_package=dto_package))
jvpp_file.flush()
jvpp_file.close()
- jvpp_file = open("JVppImpl.java", 'w')
+ jvpp_file = open("JVpp%sImpl.java" % plugin_name, 'w')
jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
methods="\n".join(methods_impl),
base_package=base_package,
- dto_package=dto_package))
+ plugin_package=plugin_package,
+ plugin_name=plugin_name,
+ plugin_name_underscore=plugin_name_underscore,
+ dto_package=dto_package,
+ control_ping_class=control_ping_class))
jvpp_file.flush()
jvpp_file.close()
class="n">clib_mem_alloc (2ULL << 30); void *h; uword *objects = 0; int i; f64 before, after; clib_time_init (&clib_time); vec_validate (objects, 2000000 - 1); h = mheap_alloc (h_mem, (uword) (2 << 30)); before = clib_time_now (&clib_time); for (i = 0; i < vec_len (objects); i++) { h = mheap_get_aligned (h, 24 /* size */ , 64 /* align */ , 16 /* align at offset */ , &objects[i]); } after = clib_time_now (&clib_time); fformat (stdout, "alloc: %u objects in %.2f seconds, %.2f objects/second\n", vec_len (objects), (after - before), ((f64) vec_len (objects)) / (after - before)); return 0; } int test_mheap_main (unformat_input_t * input) { int i, j, k, n_iterations; void *h, *h_mem; uword *objects = 0; u32 objects_used, really_verbose, n_objects, max_object_size; u32 check_mask, seed, trace, use_vm; u32 print_every = 0; u32 *data; mheap_t *mh; /* Validation flags. */ check_mask = 0; #define CHECK_VALIDITY 1 #define CHECK_DATA 2 #define CHECK_ALIGN 4 #define TEST1 8 n_iterations = 10; seed = 0; max_object_size = 100; n_objects = 1000; trace = 0; really_verbose = 0; use_vm = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (0 == unformat (input, "iter %d", &n_iterations) && 0 == unformat (input, "count %d", &n_objects) && 0 == unformat (input, "size %d", &max_object_size) && 0 == unformat (input, "seed %d", &seed) && 0 == unformat (input, "print %d", &print_every) && 0 == unformat (input, "validdata %|", &check_mask, CHECK_DATA | CHECK_VALIDITY) && 0 == unformat (input, "valid %|", &check_mask, CHECK_VALIDITY) && 0 == unformat (input, "verbose %=", &really_verbose, 1) && 0 == unformat (input, "trace %=", &trace, 1) && 0 == unformat (input, "vm %=", &use_vm, 1) && 0 == unformat (input, "align %|", &check_mask, CHECK_ALIGN) && 0 == unformat (input, "test1 %|", &check_mask, TEST1)) { clib_warning ("unknown input `%U'", format_unformat_error, input); return 1; } } /* Zero seed means use default. */ if (!seed) seed = random_default_seed (); if (check_mask & TEST1) { return test1 (); } if_verbose ("testing %d iterations, %d %saligned objects, max. size %d, seed %d", n_iterations, n_objects, (check_mask & CHECK_ALIGN) ? "randomly " : "un", max_object_size, seed); vec_resize (objects, n_objects); if (vec_bytes (objects) > 0) /* stupid warning be gone */ clib_memset (objects, ~0, vec_bytes (objects)); objects_used = 0; /* Allocate initial heap. */ { uword size = max_pow2 (2 * n_objects * max_object_size * sizeof (data[0])); h_mem = clib_mem_alloc (size); if (!h_mem) return 0; h = mheap_alloc (h_mem, size); } if (trace) mheap_trace (h, trace); mh = mheap_header (h); if (use_vm) mh->flags &= ~MHEAP_FLAG_DISABLE_VM; else mh->flags |= MHEAP_FLAG_DISABLE_VM; if (check_mask & CHECK_VALIDITY) mh->flags |= MHEAP_FLAG_VALIDATE; for (i = 0; i < n_iterations; i++) { while (1) { j = random_u32 (&seed) % vec_len (objects); if (objects[j] != ~0 || i + objects_used < n_iterations) break; } if (objects[j] != ~0) { mheap_put (h, objects[j]); objects_used--; objects[j] = ~0; } else { uword size, align, align_offset; size = (random_u32 (&seed) % max_object_size) * sizeof (data[0]); align = align_offset = 0; if (check_mask & CHECK_ALIGN) { align = 1 << (random_u32 (&seed) % 10); align_offset = round_pow2 (random_u32 (&seed) & (align - 1), sizeof (u32)); } h = mheap_get_aligned (h, size, align, align_offset, &objects[j]); if (align > 0) ASSERT (0 == ((objects[j] + align_offset) & (align - 1))); ASSERT (objects[j] != ~0); objects_used++; /* Set newly allocated object with test data. */ if (check_mask & CHECK_DATA) { uword len; data = (void *) h + objects[j]; len = mheap_len (h, data); ASSERT (size <= mheap_data_bytes (h, objects[j])); data[0] = len; for (k = 1; k < len; k++) data[k] = objects[j] + k; } } /* Verify that all used objects have correct test data. */ if (check_mask & 2) { for (j = 0; j < vec_len (objects); j++) if (objects[j] != ~0) { u32 *data = h + objects[j]; uword len = data[0]; for (k = 1; k < len; k++) ASSERT (data[k] == objects[j] + k); } } if (print_every != 0 && i > 0 && (i % print_every) == 0) fformat (stderr, "iteration %d: %U\n", i, format_mheap, h, really_verbose); } if (verbose) fformat (stderr, "%U\n", format_mheap, h, really_verbose); mheap_free (h); clib_mem_free (h_mem); vec_free (objects); return 0; } #ifdef CLIB_UNIX int main (int argc, char *argv[]) { unformat_input_t i; int ret; clib_mem_init (0, 3ULL << 30); verbose = (argc > 1); unformat_init_command_line (&i, argv); ret = test_mheap_main (&i); unformat_free (&i); return ret; } #endif /* CLIB_UNIX */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */