jvpp: map VPP API enums to primitive types
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / enum_gen.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2018 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17 import util
18 import jni_gen
19
20
21 def generate_enums(enum_list, inputfile, logger):
22     """
23     Generates Java representation of enum types defined in the provided JSON file.
24     """
25
26     if not enum_list:
27         logger.debug("Skipping enum generation (%s does not define enum types)." % inputfile)
28         return
29
30     logger.debug("Generating enums for %s" % inputfile)
31
32     for enum in enum_list:
33         enum_name = None
34         enum_type = None
35         for e in enum:
36             if isinstance(e, basestring):
37                 enum_name = e
38             elif type(e) is dict and 'enumtype' in e:
39                 enum_type = e['enumtype']
40
41         if not enum_name:
42             logger.warn("%s enum is missing name. Skipping" % enum)
43             continue
44
45         if not enum_type:
46             logger.warn("%s enum is missing value type. Skipping" % enum)
47             continue
48
49         # TODO(VPP-1153): add real enum support.
50         # But first refactor java api generation
51         # (either VPP-1154 or VPP-1155, see also VPP-480).
52
53         # As a workaround we just update all the mappings
54         # used for VPP API definitions to JAVA and C/JNI translation.
55         enum_array_type = enum_type + "[]"
56         type_name = "vl_api_" + enum_name + "_t"
57         array_type_name = type_name + "[]"
58         util.vpp_2_jni_type_mapping[type_name] = util.vpp_2_jni_type_mapping[enum_type]
59         util.vpp_2_jni_type_mapping[array_type_name] = util.vpp_2_jni_type_mapping[enum_array_type]
60         util.jni_2_java_type_mapping[type_name] = util.jni_2_java_type_mapping[enum_type]
61         util.jni_2_java_type_mapping[array_type_name] = util.jni_2_java_type_mapping[enum_array_type]
62         util.jni_2_signature_mapping[type_name] = util.jni_2_signature_mapping[enum_type]
63         util.jni_2_signature_mapping[array_type_name] = util.jni_2_signature_mapping[enum_array_type]
64         util.jni_field_accessors[type_name] = util.jni_field_accessors[enum_type]
65         util.jni_field_accessors[array_type_name] = util.jni_field_accessors[enum_array_type]
66         jni_gen.struct_setter_templates[type_name] = jni_gen.struct_setter_templates[enum_type]
67         jni_gen.struct_setter_templates[array_type_name] = jni_gen.struct_setter_templates[enum_array_type]
68         jni_gen.dto_field_setter_templates[type_name] = jni_gen.dto_field_setter_templates[enum_type]
69         jni_gen.dto_field_setter_templates[array_type_name] = jni_gen.dto_field_setter_templates[enum_array_type]