blob: 48645246596952af4d99fdd789cb735abdbd2cfb [file] [log] [blame]
Maros Marsalek45a42b52016-04-28 12:29:33 +02001#!/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 Gradzkid85036f2016-04-26 12:09:05 +020016import os, pprint
Maros Marsalek45a42b52016-04-28 12:29:33 +020017from os import removedirs
18
19
20def underscore_to_camelcase(name):
21 name = name.title().replace("_", "")
22 return name[0].lower() + name[1:]
23
24
25def underscore_to_camelcase_upper(name):
26 name = name.title().replace("_", "")
27 return name[0].upper() + name[1:]
28
29
30def 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
38reply_suffixes = ("reply", "details", "l2fibtableentry")
39
40
41def is_reply(name):
42 return name.lower().endswith(reply_suffixes)
43
Matej Perinaac1a7282017-09-11 10:11:51 +020044details_suffix = "details"
Maros Marsalek45a42b52016-04-28 12:29:33 +020045
46def is_details(name):
47 return name.lower().endswith(reply_suffixes[1]) or name.lower().endswith(reply_suffixes[2])
48
Maros Marsaleka53b0e22016-09-16 16:17:58 +020049
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020050def is_retval_field(name):
51 return name == 'retval'
52
Maros Marsalek45a42b52016-04-28 12:29:33 +020053dump_suffix = "dump"
54
55
56def is_dump(name):
57 return name.lower().endswith(dump_suffix)
58
59
60def 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
Marek Gradzki66ea26b2016-07-26 15:28:22 +020069# Mapping according to:
Maros Marsalek45a42b52016-04-28 12:29:33 +020070# http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
Marek Gradzki66ea26b2016-07-26 15:28:22 +020071#
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.
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020074jni_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
Marek Gradzki66ea26b2016-07-26 15:28:22 +020094vpp_2_jni_type_mapping = {'u8': 'jbyte',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020095 'u8[]': 'jbyteArray',
Maros Marsalek45a42b52016-04-28 12:29:33 +020096 'i8': 'jbyte',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020097 'u8[]': 'jbyteArray',
Jan Srnicek2e95f5a2016-07-06 13:19:12 +020098 'u16': 'jshort',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020099 'u16[]': 'jshortArray',
Maros Marsalek45a42b52016-04-28 12:29:33 +0200100 'i16': 'jshort',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200101 'i16[]': 'jshortArray',
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200102 'u32': 'jint',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200103 'u32[]': 'jintArray',
Maros Marsalek45a42b52016-04-28 12:29:33 +0200104 'i32': 'jint',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200105 'i32[]': 'jintArray',
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200106 'u64': 'jlong',
Maros Marsalek275b1462016-11-07 11:37:49 +0100107 'u64[]': 'jlongArray',
Maros Marsalek45a42b52016-04-28 12:29:33 +0200108 'i64': 'jlong',
Maros Marsalek275b1462016-11-07 11:37:49 +0100109 'i64[]': 'jlongArray',
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200110 'f64': 'jdouble',
111 'f64[]': 'jdoubleArray'
Maros Marsalek45a42b52016-04-28 12:29:33 +0200112 }
113
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200114# https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
115jni_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
136jni_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
Maros Marsalek45a42b52016-04-28 12:29:33 +0200157# 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
159unconventional_naming_rep_req = {
Maros Marsalek45a42b52016-04-28 12:29:33 +0200160 }
161
162#
163# FIXME no convention in the naming of events (notifications) in vpe.api
164notifications_message_suffixes = ("event", "counters")
Maros Marsalek45a42b52016-04-28 12:29:33 +0200165
166# messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api
167# FIXME
Ole Troan01384fe2017-05-12 11:55:35 +0200168ignored_messages = []
Maros Marsalek45a42b52016-04-28 12:29:33 +0200169
170
Maros Marsalek7becd082016-05-31 17:45:16 +0200171def is_notification(name):
172 """ Returns true if the structure is a notification regardless of its no other use """
Neale Rannsa07bd702017-08-07 07:53:49 -0700173 return is_just_notification(name)
Maros Marsalek7becd082016-05-31 17:45:16 +0200174
175
176def 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)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200179
180
181def is_ignored(param):
182 return param.lower() in ignored_messages
183
184
185def 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
189def remove_suffix(camel_case_name_with_suffix, suffix):
Matej Perinaac1a7282017-09-11 10:11:51 +0200190 if not suffix:
191 return camel_case_name_with_suffix
Maros Marsalek45a42b52016-04-28 12:29:33 +0200192 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
196def is_control_ping(camel_case_name_with_suffix):
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200197 return camel_case_name_with_suffix.lower().startswith("controlping");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200198
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200199
Marek Gradzkid85036f2016-04-26 12:09:05 +0200200def 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)
Maros Marsalek7becd082016-05-31 17:45:16 +0200203 return " * " + str.replace("\n", "\n * ")
204
205
206notification_dto_suffix = "Notification"
207
208
209def add_notification_suffix(camel_case_dto_name):
210 camel_case_dto_name += notification_dto_suffix
Jan Srnicek2e95f5a2016-07-06 13:19:12 +0200211 return camel_case_dto_name
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200212
213
214def is_array(java_type_as_string):
215 return java_type_as_string.endswith("[]")
Matej Perinaac1a7282017-09-11 10:11:51 +0200216
217def is_want(name):
218 return name.startswith("want_")