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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from os import removedirs
20 def underscore_to_camelcase(name):
21 name = name.title().replace("_", "")
22 return name[0].lower() + name[1:]
25 def underscore_to_camelcase_upper(name):
26 name = name.title().replace("_", "")
27 return name[0].upper() + name[1:]
30 def remove_folder(folder):
31 """ Remove folder with all its files """
32 for root, dirs, files in os.walk(folder, topdown=False):
34 os.remove(os.path.join(root, name))
38 reply_suffixes = ("reply", "details", "l2fibtableentry")
42 return name.lower().endswith(reply_suffixes)
44 details_suffix = "details"
47 return name.lower().endswith(reply_suffixes[1]) or name.lower().endswith(reply_suffixes[2])
50 def is_retval_field(name):
51 return name == 'retval'
57 return name.lower().endswith(dump_suffix)
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
69 # Mapping according to:
70 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
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',
94 vpp_2_jni_type_mapping = {'u8': 'jbyte',
99 'u16[]': 'jshortArray',
101 'i16[]': 'jshortArray',
103 'u32[]': 'jintArray',
105 'i32[]': 'jintArray',
107 'u64[]': 'jlongArray',
109 'i64[]': 'jlongArray',
111 'f64[]': 'jdoubleArray'
114 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
115 jni_2_signature_mapping = {'u8': 'B',
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',
139 'i8[]': 'ObjectField',
141 'u16[]': 'ObjectField',
143 'i16[]': 'ObjectField',
145 'u32[]': 'ObjectField',
147 'i32[]': 'ObjectField',
149 'u64[]': 'ObjectField',
151 'i64[]': 'ObjectField',
152 'f64': 'DoubleField',
153 'f64[]': 'ObjectField'
157 # vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping
159 unconventional_naming_rep_req = {
163 # FIXME no convention in the naming of events (notifications) in vpe.api
164 notifications_message_suffixes = ("event", "counters")
166 # messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api
168 ignored_messages = []
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)
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)
181 def is_ignored(param):
182 return param.lower() in ignored_messages
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))
189 def remove_suffix(camel_case_name_with_suffix, 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
196 def is_control_ping(camel_case_name_with_suffix):
197 return camel_case_name_with_suffix.lower().startswith("controlping");
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 * ")
206 notification_dto_suffix = "Notification"
209 def add_notification_suffix(camel_case_dto_name):
210 camel_case_dto_name += notification_dto_suffix
211 return camel_case_dto_name
214 def is_array(java_type_as_string):
215 return java_type_as_string.endswith("[]")
218 return name.startswith("want_")