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")
42 return name.lower().endswith(reply_suffixes)
46 return name.lower().endswith(reply_suffixes[1])
49 def is_retval_field(name):
50 return name == 'retval'
56 return name.lower().endswith(dump_suffix)
59 def get_reply_suffix(name):
60 for reply_suffix in reply_suffixes:
61 if name.lower().endswith(reply_suffix):
64 # Mapping according to:
65 # http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
67 # Unsigned types are converted to signed java types that have the same size.
68 # It is the API user responsibility to interpret them correctly.
69 jni_2_java_type_mapping = {'u8': 'byte',
89 vpp_2_jni_type_mapping = {'u8': 'jbyte',
94 'u16[]': 'jshortArray',
96 'i16[]': 'jshortArray',
100 'i32[]': 'jintArray',
102 'u64[]': 'jlongArray',
104 'i64[]': 'jlongArray',
106 'f64[]': 'jdoubleArray'
109 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures
110 jni_2_signature_mapping = {'u8': 'B',
130 # https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines
131 jni_field_accessors = {'u8': 'ByteField',
132 'u8[]': 'ObjectField',
134 'i8[]': 'ObjectField',
136 'u16[]': 'ObjectField',
138 'i16[]': 'ObjectField',
140 'u32[]': 'ObjectField',
142 'i32[]': 'ObjectField',
144 'u64[]': 'ObjectField',
146 'i64[]': 'ObjectField',
147 'f64': 'DoubleField',
148 'f64[]': 'ObjectField'
151 # FIXME no convention in the naming of events (notifications) in vpe.api
152 notifications_message_suffixes = ("event", "counters")
154 def is_notification(name):
155 """ Returns true if the structure is a notification regardless of its no other use """
156 return is_just_notification(name)
159 def is_just_notification(name):
160 """ Returns true if the structure is just a notification and has no other use """
161 return name.lower().endswith(notifications_message_suffixes)
164 def remove_reply_suffix(camel_case_name_with_suffix):
165 return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix))
168 def remove_suffix(camel_case_name_with_suffix, suffix):
170 return camel_case_name_with_suffix
171 suffix_length = len(suffix)
172 return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix
175 def is_control_ping(camel_case_name_with_suffix):
176 return camel_case_name_with_suffix.lower().startswith("controlping")
179 def api_message_to_javadoc(api_message):
180 """ Converts vpe.api message description to javadoc """
181 str = pprint.pformat(api_message, indent=4, width=120, depth=None)
182 return " * " + str.replace("\n", "\n * ")
185 notification_dto_suffix = "Notification"
188 def add_notification_suffix(camel_case_dto_name):
189 camel_case_dto_name += notification_dto_suffix
190 return camel_case_dto_name
193 def is_array(java_type_as_string):
194 return java_type_as_string.endswith("[]")
197 return name.startswith("want_")