VPP-413 DTOs generated by JVpp improvements:
[vpp.git] / vpp-api / java / jvpp / gen / jvppgen / util.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2016 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, pprint
17 from os import removedirs
18
19
20 def underscore_to_camelcase(name):
21     name = name.title().replace("_", "")
22     return name[0].lower() + name[1:]
23
24
25 def underscore_to_camelcase_upper(name):
26     name = name.title().replace("_", "")
27     return name[0].upper() + name[1:]
28
29
30 def remove_folder(folder):
31     """ Remove folder with all its files """
32     for root, dirs, files in os.walk(folder, topdown=False):
33         for name in files:
34             os.remove(os.path.join(root, name))
35         removedirs(folder)
36
37
38 reply_suffixes = ("reply", "details", "l2fibtableentry")
39
40
41 def is_reply(name):
42     return name.lower().endswith(reply_suffixes)
43
44
45 def is_details(name):
46     return name.lower().endswith(reply_suffixes[1]) or name.lower().endswith(reply_suffixes[2])
47
48
49 def is_retval_field(name):
50     return name == 'retval'
51
52 dump_suffix = "dump"
53
54
55 def is_dump(name):
56     return name.lower().endswith(dump_suffix)
57
58
59 def get_reply_suffix(name):
60     for reply_suffix in reply_suffixes:
61         if name.lower().endswith(reply_suffix):
62             if reply_suffix == reply_suffixes[2]:
63                 # FIXME workaround for l2_fib_table_entry
64                 return 'entry'
65             else:
66                 return reply_suffix
67
68 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
69 jni_2_java_type_mapping = {'jbyte': 'byte',
70                            'jbyteArray': 'byte[]',
71                            'jchar': 'char',
72                            'jcharArray': 'char[]',
73                            'jshort': 'short',
74                            'jshortArray': 'short[]',
75                            'jint': 'int',
76                            'jintArray': 'int[]',
77                            'jlong': 'long',
78                            'jlongArray': 'long[]',
79                            'jdouble': 'double',
80                            'jdoubleArray': 'double[]',
81                            'jfloat': 'float',
82                            'jfloatArray': 'float[]',
83                            'void': 'void',
84                            'jstring': 'java.lang.String',
85                            'jobject': 'java.lang.Object',
86                            'jobjectArray': 'java.lang.Object[]'
87                            }
88
89 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
90 jni_2_signature_mapping = {'jbyte': 'B',
91                            'jbyteArray': '[B',
92                            'jchar': 'C',
93                            'jcharArray': '[C',
94                            'jshort': 'S',
95                            'jshortArray': '[S',
96                            'jint': 'I',
97                            'jintArray': '[I',
98                            'jlong': 'J',
99                            'jlongArray': '[J',
100                            'jdouble': 'D',
101                            'jdoubleArray': '[D',
102                            'jfloat': 'F',
103                            'jfloatArray': '[F'
104                            }
105
106 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines
107 jni_field_accessors = {
108     'jbyte': 'ByteField',
109     'jbyteArray': 'ObjectField',
110     'jchar': 'CharField',
111     'jcharArray': 'ObjectField',
112     'jshort': 'ShortField',
113     'jshortArray': 'ObjectField',
114     'jint': 'IntField',
115     'jintArray': 'ObjectField',
116     'jlong': 'LongField',
117     'jlongArray': 'ObjectField',
118     'jdouble': 'DoubleField',
119     'jdoubleArray': 'ObjectField',
120     'jfloat': 'FloatField',
121     'jfloatArray': 'ObjectField'
122 }
123
124 # Mapping according to:
125 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
126 #
127 # Unsigned types are converted to signed java types that have the same size.
128 # It is the API user responsibility to interpret them correctly.
129 vpp_2_jni_type_mapping = {'u8': 'jbyte',
130                           'i8': 'jbyte',
131                           'u16': 'jshort',
132                           'i16': 'jshort',
133                           'u32': 'jint',
134                           'i32': 'jint',
135                           'u64': 'jlong',
136                           'i64': 'jlong',
137                           'f64': 'jdouble'
138                           }
139
140 # vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping
141 # FIXME in vpe.api
142 unconventional_naming_rep_req = {
143                                  'cli_reply': 'cli_request',
144                                  'vnet_summary_stats_reply': 'vnet_get_summary_stats',
145                                  # This below is actually a sub-details callback. We cannot derive the mapping of dump request
146                                  # belonging to this sub-details from naming conventions. We need special mapping
147                                  'bridge_domain_sw_if_details': 'bridge_domain',
148                                  # This is standard dump call + details reply. However it's not called details but entry
149                                  'l2_fib_table_entry': 'l2_fib_table'
150                                  }
151
152 #
153 # FIXME no convention in the naming of events (notifications) in vpe.api
154 notifications_message_suffixes = ("event", "counters")
155 notification_messages_reused = ["sw_interface_set_flags"]
156
157 # messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api
158 # FIXME
159 ignored_messages = ["is_address_reachable"]
160
161
162 def is_notification(name):
163     """ Returns true if the structure is a notification regardless of its no other use """
164     return is_just_notification(name) or name.lower() in notification_messages_reused
165
166
167 def is_just_notification(name):
168     """ Returns true if the structure is just a notification and has no other use """
169     return name.lower().endswith(notifications_message_suffixes)
170
171
172 def is_ignored(param):
173     return param.lower() in ignored_messages
174
175
176 def remove_reply_suffix(camel_case_name_with_suffix):
177     return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix))
178
179
180 def remove_suffix(camel_case_name_with_suffix, suffix):
181     suffix_length = len(suffix)
182     return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix
183
184
185 def is_control_ping(camel_case_name_with_suffix):
186     return camel_case_name_with_suffix.lower().startswith("controlping");
187
188
189 def api_message_to_javadoc(api_message):
190     """ Converts vpe.api message description to javadoc """
191     str = pprint.pformat(api_message, indent=4, width=120, depth=None)
192     return " * " + str.replace("\n", "\n * ")
193
194
195 notification_dto_suffix = "Notification"
196
197
198 def add_notification_suffix(camel_case_dto_name):
199     camel_case_dto_name += notification_dto_suffix
200     return camel_case_dto_name
201
202
203 def is_array(java_type_as_string):
204     return java_type_as_string.endswith("[]")