jvpp: map VPP API enums to primitive types 63/10263/2
authorMarek Gradzki <mgradzki@cisco.com>
Wed, 24 Jan 2018 15:35:11 +0000 (16:35 +0100)
committerDamjan Marion <dmarion.lists@gmail.com>
Sat, 27 Jan 2018 10:50:52 +0000 (10:50 +0000)
Adding enum support (VPP-1153) requires
JVPP generator refactoring
(see: VPP-1154, VPP-1155, VPP-480)

As a workaround we just update all the mappings
used for VPP API definitions to JAVA and C/JNI translation.

Change-Id: I9dff83e5199039a1a46a3d4685ce57cdeeeb2014
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
src/vpp-api/java/jvpp/gen/jvpp_gen.py
src/vpp-api/java/jvpp/gen/jvppgen/enum_gen.py [new file with mode: 0644]

index 79708ab..2536b4e 100755 (executable)
@@ -29,6 +29,7 @@ from jvppgen import jvpp_future_facade_gen
 from jvppgen import jvpp_impl_gen
 from jvppgen import jvpp_c_gen
 from jvppgen import util
+from jvppgen import enum_gen
 
 # Invocation:
 # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp
@@ -88,6 +89,10 @@ os.chdir(work_dir)
 
 for inputfile in args.inputfiles:
     _cfg = json.load(open(cwd + "/" + inputfile, 'r'))
+    if 'enums' in cfg:
+        cfg['enums'].extend(_cfg['enums'])
+    else:
+        cfg['enums'] = _cfg['enums']
     if 'types' in cfg:
         cfg['types'].extend(_cfg['types'])
     else:
@@ -184,11 +189,10 @@ future_package = 'future'
 callback_facade_package = 'callfacade'
 
 types_list, types_name = get_definitions(cfg['types'])
-
-types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles, logger)
-
 func_list, func_name = get_definitions(cfg['messages'])
 
+enum_gen.generate_enums(cfg['enums'], args.inputfiles, logger)
+types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles, logger)
 dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles,
                       logger)
 jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles, logger)
diff --git a/src/vpp-api/java/jvpp/gen/jvppgen/enum_gen.py b/src/vpp-api/java/jvpp/gen/jvppgen/enum_gen.py
new file mode 100644 (file)
index 0000000..51a74e6
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 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:
+#
+#     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.
+
+import os
+import util
+import jni_gen
+
+
+def generate_enums(enum_list, inputfile, logger):
+    """
+    Generates Java representation of enum types defined in the provided JSON file.
+    """
+
+    if not enum_list:
+        logger.debug("Skipping enum generation (%s does not define enum types)." % inputfile)
+        return
+
+    logger.debug("Generating enums for %s" % inputfile)
+
+    for enum in enum_list:
+        enum_name = None
+        enum_type = None
+        for e in enum:
+            if isinstance(e, basestring):
+                enum_name = e
+            elif type(e) is dict and 'enumtype' in e:
+                enum_type = e['enumtype']
+
+        if not enum_name:
+            logger.warn("%s enum is missing name. Skipping" % enum)
+            continue
+
+        if not enum_type:
+            logger.warn("%s enum is missing value type. Skipping" % enum)
+            continue
+
+        # TODO(VPP-1153): add real enum support.
+        # But first refactor java api generation
+        # (either VPP-1154 or VPP-1155, see also VPP-480).
+
+        # As a workaround we just update all the mappings
+        # used for VPP API definitions to JAVA and C/JNI translation.
+        enum_array_type = enum_type + "[]"
+        type_name = "vl_api_" + enum_name + "_t"
+        array_type_name = type_name + "[]"
+        util.vpp_2_jni_type_mapping[type_name] = util.vpp_2_jni_type_mapping[enum_type]
+        util.vpp_2_jni_type_mapping[array_type_name] = util.vpp_2_jni_type_mapping[enum_array_type]
+        util.jni_2_java_type_mapping[type_name] = util.jni_2_java_type_mapping[enum_type]
+        util.jni_2_java_type_mapping[array_type_name] = util.jni_2_java_type_mapping[enum_array_type]
+        util.jni_2_signature_mapping[type_name] = util.jni_2_signature_mapping[enum_type]
+        util.jni_2_signature_mapping[array_type_name] = util.jni_2_signature_mapping[enum_array_type]
+        util.jni_field_accessors[type_name] = util.jni_field_accessors[enum_type]
+        util.jni_field_accessors[array_type_name] = util.jni_field_accessors[enum_array_type]
+        jni_gen.struct_setter_templates[type_name] = jni_gen.struct_setter_templates[enum_type]
+        jni_gen.struct_setter_templates[array_type_name] = jni_gen.struct_setter_templates[enum_array_type]
+        jni_gen.dto_field_setter_templates[type_name] = jni_gen.dto_field_setter_templates[enum_type]
+        jni_gen.dto_field_setter_templates[array_type_name] = jni_gen.dto_field_setter_templates[enum_array_type]