jvpp: adding callbacks for all messages (VPP-914)
[vpp.git] / src / 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 details_suffix = "details"
45
46 def is_details(name):
47     return name.lower().endswith(reply_suffixes[1]) or name.lower().endswith(reply_suffixes[2])
48
49
50 def is_retval_field(name):
51     return name == 'retval'
52
53 dump_suffix = "dump"
54
55
56 def is_dump(name):
57     return name.lower().endswith(dump_suffix)
58
59
60 def get_reply_suffix(name):
61     for reply_suffix in reply_suffixes:
62         if name.lower().endswith(reply_suffix):
63             if reply_suffix == reply_suffixes[2]:
64                 # FIXME workaround for l2_fib_table_entry
65                 return 'entry'
66             else:
67                 return reply_suffix
68
69 # Mapping according to:
70 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
71 #
72 # Unsigned types are converted to signed java types that have the same size.
73 # It is the API user responsibility to interpret them correctly.
74 jni_2_java_type_mapping = {'u8': 'byte',
75                            'u8[]': 'byte[]',
76                            'i8': 'byte',
77                            'i8[]': 'byte[]',
78                            'u16': 'short',
79                            'u16[]': 'short[]',
80                            'i16': 'short',
81                            'i16[]': 'short[]',
82                            'u32': 'int',
83                            'u32[]': 'int[]',
84                            'i32': 'int',
85                            'i32[]': 'int[]',
86                            'u64': 'long',
87                            'u64[]': 'long[]',
88                            'i64': 'long',
89                            'i64[]': 'long[]',
90                            'f64': 'double',
91                            'f64[]': 'double[]'
92                            }
93
94 vpp_2_jni_type_mapping = {'u8': 'jbyte',
95                           'u8[]': 'jbyteArray',
96                           'i8': 'jbyte',
97                           'u8[]': 'jbyteArray',
98                           'u16': 'jshort',
99                           'u16[]': 'jshortArray',
100                           'i16': 'jshort',
101                           'i16[]': 'jshortArray',
102                           'u32': 'jint',
103                           'u32[]': 'jintArray',
104                           'i32': 'jint',
105                           'i32[]': 'jintArray',
106                           'u64': 'jlong',
107                           'u64[]': 'jlongArray',
108                           'i64': 'jlong',
109                           'i64[]': 'jlongArray',
110                           'f64': 'jdouble',
111                           'f64[]': 'jdoubleArray'
112                           }
113
114 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
115 jni_2_signature_mapping = {'u8': 'B',
116                            'u8[]': '[B',
117                            'i8': 'B',
118                            'i8[]': '[B',
119                            'u16': 'S',
120                            'u16[]': '[S',
121                            'i16': 'S',
122                            'i16[]': '[S',
123                            'u32': 'I',
124                            'u32[]': '[I',
125                            'i32': 'I',
126                            'i32[]': '[I',
127                            'u64': 'J',
128                            'u64[]': '[J',
129                            'i64': 'J',
130                            'i64[]': '[J',
131                            'f64': 'D',
132                            'f64[]': '[D'
133                            }
134
135 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines
136 jni_field_accessors =  {'u8': 'ByteField',
137                         'u8[]': 'ObjectField',
138                         'i8': 'ByteField',
139                         'i8[]': 'ObjectField',
140                         'u16': 'ShortField',
141                         'u16[]': 'ObjectField',
142                         'i16': 'ShortField',
143                         'i16[]': 'ObjectField',
144                         'u32': 'IntField',
145                         'u32[]': 'ObjectField',
146                         'i32': 'IntField',
147                         'i32[]': 'ObjectField',
148                         'u64': 'LongField',
149                         'u64[]': 'ObjectField',
150                         'i64': 'LongField',
151                         'i64[]': 'ObjectField',
152                         'f64': 'DoubleField',
153                         'f64[]': 'ObjectField'
154                         }
155
156
157 # vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping
158 # FIXME in vpe.api
159 unconventional_naming_rep_req = {
160                                  }
161
162 #
163 # FIXME no convention in the naming of events (notifications) in vpe.api
164 notifications_message_suffixes = ("event", "counters")
165
166 # messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api
167 # FIXME
168 ignored_messages = []
169
170
171 def is_notification(name):
172     """ Returns true if the structure is a notification regardless of its no other use """
173     return is_just_notification(name)
174
175
176 def is_just_notification(name):
177     """ Returns true if the structure is just a notification and has no other use """
178     return name.lower().endswith(notifications_message_suffixes)
179
180
181 def is_ignored(param):
182     return param.lower() in ignored_messages
183
184
185 def remove_reply_suffix(camel_case_name_with_suffix):
186     return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix))
187
188
189 def remove_suffix(camel_case_name_with_suffix, suffix):
190     if not suffix:
191         return camel_case_name_with_suffix
192     suffix_length = len(suffix)
193     return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix
194
195
196 def is_control_ping(camel_case_name_with_suffix):
197     return camel_case_name_with_suffix.lower().startswith("controlping");
198
199
200 def api_message_to_javadoc(api_message):
201     """ Converts vpe.api message description to javadoc """
202     str = pprint.pformat(api_message, indent=4, width=120, depth=None)
203     return " * " + str.replace("\n", "\n * ")
204
205
206 notification_dto_suffix = "Notification"
207
208
209 def add_notification_suffix(camel_case_dto_name):
210     camel_case_dto_name += notification_dto_suffix
211     return camel_case_dto_name
212
213
214 def is_array(java_type_as_string):
215     return java_type_as_string.endswith("[]")
216
217 def is_want(name):
218     return name.startswith("want_")