VPP-330 Track pending map-requests with a fifo
[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 def is_retval_field(name):
49     return name == 'retval'
50
51 dump_suffix = "dump"
52
53
54 def is_dump(name):
55     return name.lower().endswith(dump_suffix)
56
57
58 def get_reply_suffix(name):
59     for reply_suffix in reply_suffixes:
60         if name.lower().endswith(reply_suffix):
61             if reply_suffix == reply_suffixes[2]:
62                 # FIXME workaround for l2_fib_table_entry
63                 return 'entry'
64             else:
65                 return reply_suffix
66
67 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
68 jni_2_java_type_mapping = {'jbyte': 'byte',
69                            'jbyteArray': 'byte[]',
70                            'jchar': 'char',
71                            'jcharArray': 'char[]',
72                            'jshort': 'short',
73                            'jshortArray': 'short[]',
74                            'jint': 'int',
75                            'jintArray': 'int[]',
76                            'jlong': 'long',
77                            'jlongArray': 'long[]',
78                            'jdouble': 'double',
79                            'jdoubleArray': 'double[]',
80                            'jfloat': 'float',
81                            'jfloatArray': 'float[]',
82                            'void': 'void',
83                            'jstring': 'java.lang.String',
84                            'jobject': 'java.lang.Object',
85                            'jobjectArray': 'java.lang.Object[]'
86                            }
87
88 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
89 jni_2_signature_mapping = {'jbyte': 'B',
90                            'jbyteArray': '[B',
91                            'jchar': 'C',
92                            'jcharArray': '[C',
93                            'jshort': 'S',
94                            'jshortArray': '[S',
95                            'jint': 'I',
96                            'jintArray': '[I',
97                            'jlong': 'J',
98                            'jlongArray': '[J',
99                            'jdouble': 'D',
100                            'jdoubleArray': '[D',
101                            'jfloat': 'F',
102                            'jfloatArray': '[F'
103                            }
104
105 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines
106 jni_field_accessors = {
107     'jbyte': 'ByteField',
108     'jbyteArray': 'ObjectField',
109     'jchar': 'CharField',
110     'jcharArray': 'ObjectField',
111     'jshort': 'ShortField',
112     'jshortArray': 'ObjectField',
113     'jint': 'IntField',
114     'jintArray': 'ObjectField',
115     'jlong': 'LongField',
116     'jlongArray': 'ObjectField',
117     'jdouble': 'DoubleField',
118     'jdoubleArray': 'ObjectField',
119     'jfloat': 'FloatField',
120     'jfloatArray': 'ObjectField'
121 }
122
123 # TODO watch out for unsigned types
124 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
125 vpp_2_jni_type_mapping = {'u8': 'jbyte',  # fixme
126                           'i8': 'jbyte',
127                           'u16': 'jshort',
128                           'i16': 'jshort',
129                           'u32': 'jint',  # fixme
130                           'i32': 'jint',
131                           'u64': 'jlong',  # fixme
132                           'i64': 'jlong',
133                           'f64': 'jdouble'
134                           }
135
136 # vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping
137 # FIXME in vpe.api
138 unconventional_naming_rep_req = {
139                                  'cli_reply': 'cli_request',
140                                  'vnet_summary_stats_reply': 'vnet_get_summary_stats',
141                                  # This below is actually a sub-details callback. We cannot derive the mapping of dump request
142                                  # belonging to this sub-details from naming conventions. We need special mapping
143                                  'bridge_domain_sw_if_details': 'bridge_domain',
144                                  # This is standard dump call + details reply. However it's not called details but entry
145                                  'l2_fib_table_entry': 'l2_fib_table'
146                                  }
147
148 #
149 # FIXME no convention in the naming of events (notifications) in vpe.api
150 notifications_message_suffixes = ("event", "counters")
151 notification_messages_reused = ["sw_interface_set_flags"]
152
153 # messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api
154 # FIXME
155 ignored_messages = ["is_address_reachable"]
156
157
158 def is_notification(name):
159     """ Returns true if the structure is a notification regardless of its no other use """
160     return is_just_notification(name) or name.lower() in notification_messages_reused
161
162
163 def is_just_notification(name):
164     """ Returns true if the structure is just a notification and has no other use """
165     return name.lower().endswith(notifications_message_suffixes)
166
167
168 def is_ignored(param):
169     return param.lower() in ignored_messages
170
171
172 def remove_reply_suffix(camel_case_name_with_suffix):
173     return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix))
174
175
176 def remove_suffix(camel_case_name_with_suffix, suffix):
177     suffix_length = len(suffix)
178     return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix
179
180
181 def is_control_ping(camel_case_name_with_suffix):
182     return "controlping" in camel_case_name_with_suffix.lower()
183
184 def api_message_to_javadoc(api_message):
185     """ Converts vpe.api message description to javadoc """
186     str = pprint.pformat(api_message, indent=4, width=120, depth=None)
187     return " * " + str.replace("\n", "\n * ")
188
189
190 notification_dto_suffix = "Notification"
191
192
193 def add_notification_suffix(camel_case_dto_name):
194     camel_case_dto_name += notification_dto_suffix
195     return camel_case_dto_name