Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 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 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 16 | import os, pprint |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 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 | |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 48 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame] | 49 | def is_retval_field(name): |
| 50 | return name == 'retval' |
| 51 | |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 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 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 68 | # Mapping according to: |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 69 | # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 70 | # |
| 71 | # Unsigned types are converted to signed java types that have the same size. |
| 72 | # It is the API user responsibility to interpret them correctly. |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 73 | jni_2_java_type_mapping = {'u8': 'byte', |
| 74 | 'u8[]': 'byte[]', |
| 75 | 'i8': 'byte', |
| 76 | 'i8[]': 'byte[]', |
| 77 | 'u16': 'short', |
| 78 | 'u16[]': 'short[]', |
| 79 | 'i16': 'short', |
| 80 | 'i16[]': 'short[]', |
| 81 | 'u32': 'int', |
| 82 | 'u32[]': 'int[]', |
| 83 | 'i32': 'int', |
| 84 | 'i32[]': 'int[]', |
| 85 | 'u64': 'long', |
| 86 | 'u64[]': 'long[]', |
| 87 | 'i64': 'long', |
| 88 | 'i64[]': 'long[]', |
| 89 | 'f64': 'double', |
| 90 | 'f64[]': 'double[]' |
| 91 | } |
| 92 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 93 | vpp_2_jni_type_mapping = {'u8': 'jbyte', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 94 | 'u8[]': 'jbyteArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 95 | 'i8': 'jbyte', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 96 | 'u8[]': 'jbyteArray', |
Jan Srnicek | 2e95f5a | 2016-07-06 13:19:12 +0200 | [diff] [blame] | 97 | 'u16': 'jshort', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 98 | 'u16[]': 'jshortArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 99 | 'i16': 'jshort', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 100 | 'i16[]': 'jshortArray', |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 101 | 'u32': 'jint', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 102 | 'u32[]': 'jintArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 103 | 'i32': 'jint', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 104 | 'i32[]': 'jintArray', |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 105 | 'u64': 'jlong', |
Maros Marsalek | 275b146 | 2016-11-07 11:37:49 +0100 | [diff] [blame] | 106 | 'u64[]': 'jlongArray', |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 107 | 'i64': 'jlong', |
Maros Marsalek | 275b146 | 2016-11-07 11:37:49 +0100 | [diff] [blame] | 108 | 'i64[]': 'jlongArray', |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 109 | 'f64': 'jdouble', |
| 110 | 'f64[]': 'jdoubleArray' |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 113 | # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures |
| 114 | jni_2_signature_mapping = {'u8': 'B', |
| 115 | 'u8[]': '[B', |
| 116 | 'i8': 'B', |
| 117 | 'i8[]': '[B', |
| 118 | 'u16': 'S', |
| 119 | 'u16[]': '[S', |
| 120 | 'i16': 'S', |
| 121 | 'i16[]': '[S', |
| 122 | 'u32': 'I', |
| 123 | 'u32[]': '[I', |
| 124 | 'i32': 'I', |
| 125 | 'i32[]': '[I', |
| 126 | 'u64': 'J', |
| 127 | 'u64[]': '[J', |
| 128 | 'i64': 'J', |
| 129 | 'i64[]': '[J', |
| 130 | 'f64': 'D', |
| 131 | 'f64[]': '[D' |
| 132 | } |
| 133 | |
| 134 | # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines |
| 135 | jni_field_accessors = {'u8': 'ByteField', |
| 136 | 'u8[]': 'ObjectField', |
| 137 | 'i8': 'ByteField', |
| 138 | 'i8[]': 'ObjectField', |
| 139 | 'u16': 'ShortField', |
| 140 | 'u16[]': 'ObjectField', |
| 141 | 'i16': 'ShortField', |
| 142 | 'i16[]': 'ObjectField', |
| 143 | 'u32': 'IntField', |
| 144 | 'u32[]': 'ObjectField', |
| 145 | 'i32': 'IntField', |
| 146 | 'i32[]': 'ObjectField', |
| 147 | 'u64': 'LongField', |
| 148 | 'u64[]': 'ObjectField', |
| 149 | 'i64': 'LongField', |
| 150 | 'i64[]': 'ObjectField', |
| 151 | 'f64': 'DoubleField', |
| 152 | 'f64[]': 'ObjectField' |
| 153 | } |
| 154 | |
| 155 | |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 156 | # vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping |
| 157 | # FIXME in vpe.api |
| 158 | unconventional_naming_rep_req = { |
| 159 | 'cli_reply': 'cli_request', |
| 160 | 'vnet_summary_stats_reply': 'vnet_get_summary_stats', |
| 161 | # This below is actually a sub-details callback. We cannot derive the mapping of dump request |
| 162 | # belonging to this sub-details from naming conventions. We need special mapping |
| 163 | 'bridge_domain_sw_if_details': 'bridge_domain', |
| 164 | # This is standard dump call + details reply. However it's not called details but entry |
| 165 | 'l2_fib_table_entry': 'l2_fib_table' |
| 166 | } |
| 167 | |
| 168 | # |
| 169 | # FIXME no convention in the naming of events (notifications) in vpe.api |
| 170 | notifications_message_suffixes = ("event", "counters") |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 171 | notification_messages_reused = ["sw_interface_set_flags"] |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 172 | |
| 173 | # messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api |
| 174 | # FIXME |
| 175 | ignored_messages = ["is_address_reachable"] |
| 176 | |
| 177 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 178 | def is_notification(name): |
| 179 | """ Returns true if the structure is a notification regardless of its no other use """ |
| 180 | return is_just_notification(name) or name.lower() in notification_messages_reused |
| 181 | |
| 182 | |
| 183 | def is_just_notification(name): |
| 184 | """ Returns true if the structure is just a notification and has no other use """ |
| 185 | return name.lower().endswith(notifications_message_suffixes) |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 186 | |
| 187 | |
| 188 | def is_ignored(param): |
| 189 | return param.lower() in ignored_messages |
| 190 | |
| 191 | |
| 192 | def remove_reply_suffix(camel_case_name_with_suffix): |
| 193 | return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix)) |
| 194 | |
| 195 | |
| 196 | def remove_suffix(camel_case_name_with_suffix, suffix): |
| 197 | suffix_length = len(suffix) |
| 198 | return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix |
| 199 | |
| 200 | |
| 201 | def is_control_ping(camel_case_name_with_suffix): |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 202 | return camel_case_name_with_suffix.lower().startswith("controlping"); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 203 | |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 204 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 205 | def api_message_to_javadoc(api_message): |
| 206 | """ Converts vpe.api message description to javadoc """ |
| 207 | str = pprint.pformat(api_message, indent=4, width=120, depth=None) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 208 | return " * " + str.replace("\n", "\n * ") |
| 209 | |
| 210 | |
| 211 | notification_dto_suffix = "Notification" |
| 212 | |
| 213 | |
| 214 | def add_notification_suffix(camel_case_dto_name): |
| 215 | camel_case_dto_name += notification_dto_suffix |
Jan Srnicek | 2e95f5a | 2016-07-06 13:19:12 +0200 | [diff] [blame] | 216 | return camel_case_dto_name |
Maros Marsalek | a53b0e2 | 2016-09-16 16:17:58 +0200 | [diff] [blame] | 217 | |
| 218 | |
| 219 | def is_array(java_type_as_string): |
| 220 | return java_type_as_string.endswith("[]") |