docs: change code blocks from "shell" to "console"
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / jvpp_impl_gen.py
old mode 100644 (file)
new mode 100755 (executable)
index 7af70be..aff8d7f
@@ -1,6 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env python2
 #
-# Copyright (c) 2016 Cisco and/or its affiliates.
+# Copyright (c) 2016,2018 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:
 # 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.
-
-import os, util
+#
 from string import Template
 
-jvpp_ifc_template = Template("""
-package $plugin_package;
+from jvpp_model import is_request, is_dump, is_event
 
-/**
- * <p>Java representation of plugin's api file.
- * <br>It was generated by jvpp_impl_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
- */
-public interface JVpp${plugin_name} extends $base_package.JVpp {
 
-    /**
-     * Generic dispatch method for sending requests to VPP
-     *
-     * @throws io.fd.vpp.jvpp.VppInvocationException if send request had failed
-     */
-    int send($base_package.$dto_package.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException;
+def generate_java_impl(work_dir, model, logger):
+    logger.debug("Generating JVpp implementation for %s" % model.json_api_files)
+    messages = filter(_jvpp_impl_filter, model.messages)
+    plugin_package = model.plugin_package
+    methods = []
+    for msg in messages:
+        if msg.has_fields:
+            methods.append(_JVPP_IMPL_METHOD_TEMPLATE.substitute(
+                name=msg.java_name_lower,
+                plugin_package=plugin_package,
+                type=msg.java_name_upper))
+        else:
+            methods.append(_JVPP_IMPL_NO_ARG_METHOD_TEMPLATE.substitute(
+                name=msg.java_name_lower,
+                type=msg.java_name_upper))
+
+    plugin_name = model.plugin_java_name
+    jvpp_impl = _JVPP_IMPL_TEMPLATE.substitute(
+        plugin_package=plugin_package,
+        json_filename=model.json_api_files,
+        plugin_name=model.plugin_java_name,
+        plugin_name_underscore=model.plugin_name,
+        methods="\n".join(methods))
 
-$methods
-}
-""")
+    with open("%s/JVpp%sImpl.java" % (work_dir, plugin_name), "w") as f:
+        f.write(jvpp_impl)
+
+
+def _jvpp_impl_filter(msg):
+    return is_request(msg) or is_dump(msg) or is_event(msg)
 
-jvpp_impl_template = Template("""
-package $plugin_package;
+
+_JVPP_IMPL_TEMPLATE = Template("""package $plugin_package;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -50,14 +62,14 @@ import java.nio.file.attribute.PosixFilePermissions;
 import java.util.Set;
 import java.util.logging.Logger;
 import java.util.logging.Level;
-import $base_package.callback.JVppCallback;
-import $base_package.VppConnection;
-import $base_package.JVppRegistry;
+import io.fd.vpp.jvpp.callback.JVppCallback;
+import io.fd.vpp.jvpp.VppConnection;
+import io.fd.vpp.jvpp.JVppRegistry;
 
 /**
  * <p>Default implementation of JVpp interface.
- * <br>It was generated by jvpp_impl_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
+ * <br>It was generated by jvpp_impl_gen.py based on $json_filename.
+ * <br>(python representation of api file generated by vppapigen)
  */
 public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugin_name} {
 
@@ -121,7 +133,7 @@ public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugi
     }
 
     @Override
-    public int send($base_package.$dto_package.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException {
+    public int send(io.fd.vpp.jvpp.dto.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException {
         return request.send(this);
     }
 
@@ -129,96 +141,33 @@ public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugi
     public final int controlPing(final io.fd.vpp.jvpp.dto.ControlPing controlPing) throws io.fd.vpp.jvpp.VppInvocationException {
         return registry.controlPing(JVpp${plugin_name}Impl.class);
     }
-
 $methods
 }
 """)
 
-method_template = Template("""    int $name($plugin_package.$dto_package.$request request) throws io.fd.vpp.jvpp.VppInvocationException;""")
-method_native_template = Template(
-    """    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 io.fd.vpp.jvpp.VppInvocationException {
-        java.util.Objects.requireNonNull(request,"Null request object");
+_JVPP_IMPL_METHOD_TEMPLATE = Template("""
+    private static native int ${name}0($plugin_package.dto.$type request);
+    public final int $name($plugin_package.dto.$type request) throws io.fd.vpp.jvpp.VppInvocationException {
+        java.util.Objects.requireNonNull(request, "Null request object");
         connection.checkActive();
-        if(LOG.isLoggable(Level.FINE)) {
-            LOG.fine(String.format("Sending $name event message: %s", request));
+        if (LOG.isLoggable(Level.FINE)) {
+            LOG.fine(String.format("Sending $type event message: %s", request));
         }
         int result=${name}0(request);
-        if(result<0){
-            throw new io.fd.vpp.jvpp.VppInvocationException("${name}",result);
+        if (result<0){
+            throw new io.fd.vpp.jvpp.VppInvocationException("${name}", result);
         }
         return result;
-    }
-""")
+    }""")
 
-no_arg_method_template = Template("""    int $name() throws io.fd.vpp.jvpp.VppInvocationException;""")
-no_arg_method_native_template = Template("""    private static native int ${name}0() throws io.fd.vpp.jvpp.VppInvocationException;""")
-no_arg_method_impl_template = Template("""    public final int $name() throws io.fd.vpp.jvpp.VppInvocationException {
+_JVPP_IMPL_NO_ARG_METHOD_TEMPLATE = Template("""
+    private static native int ${name}0() throws io.fd.vpp.jvpp.VppInvocationException;
+    public final int $name() throws io.fd.vpp.jvpp.VppInvocationException {
         connection.checkActive();
-        LOG.fine("Sending $name event message");
+        LOG.fine("Sending $type event message");
         int result=${name}0();
         if(result<0){
-            throw new io.fd.vpp.jvpp.VppInvocationException("${name}",result);
+            throw new io.fd.vpp.jvpp.VppInvocationException("${name}", result);
         }
         return result;
-    }
-""")
-
-
-def generate_jvpp(func_list, base_package, plugin_package, plugin_name_underscore, 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 = []
-    for func in func_list:
-
-        # Skip structures that are used only as notifications
-        if util.is_just_notification(func['name']) or util.is_ignored(func['name']):
-            continue
-
-        camel_case_name = util.underscore_to_camelcase(func['name'])
-        camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
-        if util.is_reply(camel_case_name):
-            continue
-
-        if len(func['args']) == 0:
-            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,
-                                                      plugin_package=plugin_package,
-                                                      dto_package=dto_package))
-            methods_impl.append(method_native_template.substitute(name=camel_case_name,
-                                                                  request=camel_case_name_upper,
-                                                                  plugin_package=plugin_package,
-                                                                  dto_package=dto_package))
-            methods_impl.append(method_impl_template.substitute(name=camel_case_name,
-                                                                request=camel_case_name_upper,
-                                                                plugin_package=plugin_package,
-                                                                dto_package=dto_package))
-
-    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("JVpp%sImpl.java" % plugin_name, 'w')
-    jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
-                                                  methods="\n".join(methods_impl),
-                                                  base_package=base_package,
-                                                  plugin_package=plugin_package,
-                                                  plugin_name=plugin_name,
-                                                  plugin_name_underscore=plugin_name_underscore,
-                                                  dto_package=dto_package))
-    jvpp_file.flush()
-    jvpp_file.close()
+    }""")