def generate_msg_handlers(func_list, plugin_name, inputfile): handlers = [] for f in func_list: handler_name = f['name'] dto_name = util.underscore_to_camelcase_upper(handler_name) ref_name = util.underscore_to_camelcase(handler_name) if is_manually_generated(handler_name, plugin_name) or util.is_ignored(handler_name): continue if not util.is_reply(dto_name) and not util.is_notification( handler_name): continue if util.is_notification(handler_name): dto_name = util.add_notification_suffix(dto_name) ref_name = util.add_notification_suffix(ref_name) dto_setters = '' err_handler = '' # dto setters for t in zip(f['types'], f['args'], f['lengths']): c_name = t[1] java_name = util.underscore_to_camelcase(c_name) field_length = t[2][0] is_variable_len_array = t[2][1] length_field_type = None if is_variable_len_array: length_field_type = f['types'][f['args'].index(field_length)] dto_setters += jni_gen.jni_reply_handler_for_type( handler_name=handler_name, ref_name=ref_name, field_type=t[0], c_name=t[1], field_reference_name=java_name, field_name=java_name, field_length=field_length, is_variable_len_array=is_variable_len_array, length_field_type=length_field_type) # for retval don't generate setters and generate retval check if util.is_retval_field(c_name): err_handler = callback_err_handler_template.substitute( handler_name=handler_name) continue handlers.append( msg_handler_template.substitute( inputfile=inputfile, api_data=util.api_message_to_javadoc(f), handler_name=handler_name, plugin_name=plugin_name, dto_name=dto_name, class_ref_name=ref_name, dto_setters=dto_setters, err_handler=err_handler)) return "\n".join(handlers)
def generate_callback(func_list, base_package, dto_package, callback_package, callback_facade_package, inputfile): callbacks = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) if not util.is_reply(camel_case_name_with_suffix): continue callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix, callback_dto=camel_case_name_with_suffix)) jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacadeCallback.java"), 'w') jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile, base_package=base_package, dto_package=dto_package, callback_package=callback_package, methods="".join(callbacks), callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close()
def generate_class_cache(func_list): class_references = [] find_class_invocations = [] for f in func_list: c_name = f['name'] class_name = util.underscore_to_camelcase_upper(c_name) ref_name = util.underscore_to_camelcase(c_name) if util.is_ignored(c_name): continue if util.is_reply(class_name): class_references.append(class_reference_template.substitute( ref_name=ref_name)) find_class_invocations.append(find_class_invocation_template.substitute( ref_name=ref_name, class_name=class_name)) elif util.is_notification(c_name): class_references.append(class_reference_template.substitute( ref_name=util.add_notification_suffix(ref_name))) find_class_invocations.append(find_class_invocation_template.substitute( ref_name=util.add_notification_suffix(ref_name), class_name=util.add_notification_suffix(class_name))) # add exception class to class cache ref_name = 'callbackException' class_name = 'org/openvpp/jvpp/VppCallbackException' class_references.append(class_reference_template.substitute( ref_name=ref_name)) find_class_invocations.append(find_class_template.substitute( ref_name=ref_name, class_name=class_name)) return class_cache_template.substitute( class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))
def generate_callback(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile): callbacks = [] for func in func_list: if util.is_ignored(func['name']): continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) if util.is_reply(camel_case_name_with_suffix): callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix, callback_dto=camel_case_name_with_suffix)) if util.is_notification(func["name"]): with_notification_suffix = util.add_notification_suffix(camel_case_name_with_suffix) callbacks.append(jvpp_facade_callback_notification_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=with_notification_suffix + callback_gen.callback_suffix, callback_dto=with_notification_suffix)) jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacadeCallback.java"), 'w') jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile, base_package=base_package, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, methods="".join(callbacks), callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close()
def generate_dtos(func_list, base_package, plugin_package, plugin_name, dto_package, inputfile): """ Generates dto objects in a dedicated package """ print "Generating DTOs" if not os.path.exists(dto_package): os.mkdir(dto_package) for func in func_list: camel_case_dto_name = util.underscore_to_camelcase_upper(func['name']) camel_case_method_name = util.underscore_to_camelcase(func['name']) dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") if util.is_ignored(func['name']) or util.is_control_ping(camel_case_dto_name): continue fields = generate_dto_fields(camel_case_dto_name, func) methods = generate_dto_base_methods(camel_case_dto_name, func) base_type = "" # Generate request/reply or dump/dumpReply even if structure can be used as notification if not util.is_just_notification(func["name"]): if util.is_reply(camel_case_dto_name): description = "reply DTO" request_dto_name = get_request_name(camel_case_dto_name, func['name']) if util.is_details(camel_case_dto_name): # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name + "Dump") generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name, camel_case_method_name, func) else: base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name) else: args = "" if fields is "" else "this" methods += send_template.substitute(method_name=camel_case_method_name, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, args=args) if util.is_dump(camel_case_dto_name): base_type += "JVppDump" description = "dump request DTO" else: base_type += "JVppRequest" description = "request DTO" write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func, inputfile, methods) # for structures that are also used as notifications, generate dedicated notification DTO if util.is_notification(func["name"]): base_type = "JVppNotification" description = "notification DTO" camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name) dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") methods = generate_dto_base_methods(camel_case_dto_name, func) write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func, inputfile, methods) flush_dump_reply_dtos(inputfile)
def generate_callbacks(func_list, base_package, plugin_package, plugin_name, callback_package, dto_package, inputfile): """ Generates callback interfaces """ print "Generating Callback interfaces" if not os.path.exists(callback_package): os.mkdir(callback_package) callbacks = [] for func in func_list: camel_case_name_with_suffix = util.underscore_to_camelcase_upper( func['name']) if util.is_ignored(func['name']) or util.is_control_ping( camel_case_name_with_suffix): continue if not util.is_reply(camel_case_name_with_suffix ) and not util.is_notification(func['name']): continue callback_type = "JVppCallback" callbacks.append("{0}.{1}.{2}".format( plugin_package, callback_package, camel_case_name_with_suffix + callback_suffix)) callback_path = os.path.join( callback_package, camel_case_name_with_suffix + callback_suffix + ".java") callback_file = open(callback_path, 'w') reply_type = "%s.%s.%s" % (plugin_package, dto_package, camel_case_name_with_suffix) method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type) callback_file.write( callback_template.substitute( inputfile=inputfile, docs=util.api_message_to_javadoc(func), cls_name=camel_case_name_with_suffix + callback_suffix, callback_method=method, base_package=base_package, plugin_package=plugin_package, callback_package=callback_package, callback_type=callback_type)) callback_file.flush() callback_file.close() callback_file = open( os.path.join(callback_package, "JVpp%sGlobalCallback.java" % plugin_name), 'w') callback_file.write( global_callback_template.substitute(inputfile=inputfile, callbacks=", ".join(callbacks), base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, callback_package=callback_package)) callback_file.flush() callback_file.close()
def generate_jni_impl(func_list, inputfile): jni_impl = [] for f in func_list: f_name = f['name'] camel_case_function_name = util.underscore_to_camelcase(f_name) if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \ or util.is_ignored(f_name) or util.is_notification(f_name): continue arguments = '' request_class = '' field_identifiers = '' struct_setters = '' f_name_uppercase = f_name.upper() if f['args']: arguments = ', jobject request' camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name) request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper) # field identifiers for t in zip(f['types'], f['args']): jni_type = t[0] java_field_name = util.underscore_to_camelcase(t[1]) jni_signature = util.jni_2_signature_mapping[jni_type] jni_getter = util.jni_field_accessors[jni_type] field_identifiers += request_field_identifier_template.substitute( jni_type=jni_type, java_name=java_field_name, jni_signature=jni_signature, jni_getter=jni_getter) # field setters for t in zip(f['c_types'], f['args']): c_type = t[0] c_name = t[1] java_field_name = util.underscore_to_camelcase(c_name) struct_setter_template = struct_setter_templates[c_type] struct_setters += struct_setter_template.substitute( c_name=c_name, java_name=java_field_name) jni_impl.append(jni_impl_template.substitute( inputfile=inputfile, api_data=util.api_message_to_javadoc(f), java_name=camel_case_function_name, c_name_uppercase=f_name_uppercase, c_name=f_name, request_class=request_class, field_identifiers=field_identifiers, struct_setters=struct_setters, args=arguments)) return "\n".join(jni_impl)
def generate_callback(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile): callbacks = [] for func in func_list: camel_case_name_with_suffix = util.underscore_to_camelcase_upper( func['name']) if util.is_ignored(func['name']) or util.is_control_ping( camel_case_name_with_suffix): continue if util.is_reply(camel_case_name_with_suffix): callbacks.append( jvpp_facade_callback_method_template.substitute( plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=util.remove_reply_suffix( camel_case_name_with_suffix) + callback_gen.callback_suffix, callback_dto=camel_case_name_with_suffix)) if util.is_notification(func["name"]): with_notification_suffix = util.add_notification_suffix( camel_case_name_with_suffix) callbacks.append( jvpp_facade_callback_notification_method_template.substitute( plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=with_notification_suffix + callback_gen.callback_suffix, callback_dto=with_notification_suffix)) jvpp_file = open( os.path.join(callback_facade_package, "CallbackJVpp%sFacadeCallback.java" % plugin_name), 'w') jvpp_file.write( jvpp_facade_callback_template.substitute( inputfile=inputfile, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, methods="".join(callbacks), callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close()
def generate_callbacks(func_list, base_package, callback_package, dto_package, inputfile): """ Generates callback interfaces """ print "Generating Callback interfaces" if not os.path.exists(callback_package): raise Exception("%s folder is missing" % callback_package) callbacks = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # FIXME handle notifications continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper( func['name']) if not util.is_reply(camel_case_name_with_suffix): continue camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix) callbacks.append("{0}.{1}.{2}".format( base_package, callback_package, camel_case_name + callback_suffix)) callback_path = os.path.join( callback_package, camel_case_name + callback_suffix + ".java") callback_file = open(callback_path, 'w') reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix) method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type) callback_file.write( callback_template.substitute( inputfile=inputfile, docs=util.api_message_to_javadoc(func), cls_name=camel_case_name + callback_suffix, callback_method=method, base_package=base_package, callback_package=callback_package)) callback_file.flush() callback_file.close() callback_file = open( os.path.join(callback_package, "JVppGlobalCallback.java"), 'w') callback_file.write( global_callback_template.substitute(inputfile=inputfile, callbacks=", ".join(callbacks), base_package=base_package, callback_package=callback_package)) callback_file.flush() callback_file.close()
def generate_handler_registration(func_list): handler_registration = ["#define foreach_vpe_api_msg \\\n"] for f in func_list: name = f['name'] camelcase_name = util.underscore_to_camelcase(f['name']) if (not util.is_reply(camelcase_name) and not util.is_notification(name)) or util.is_ignored(name): continue handler_registration.append(handler_registration_template.substitute( name=name, upercase_name=name.upper())) return "".join(handler_registration)
def generate_handler_registration(func_list): handler_registration = ["#define foreach_api_reply_handler \\\n"] for f in func_list: name = f['name'] camelcase_name = util.underscore_to_camelcase(f['name']) if (not util.is_reply(camelcase_name) and not util.is_notification(name)) or util.is_ignored(name) \ or util.is_control_ping(camelcase_name): continue handler_registration.append( handler_registration_template.substitute(name=name, crc=f['crc'])) return "".join(handler_registration)
def generate_class_cache(func_list, plugin_name): class_references = [] find_class_invocations = [] delete_class_invocations = [] for f in func_list: c_name = f['name'] class_name = util.underscore_to_camelcase_upper(c_name) ref_name = util.underscore_to_camelcase(c_name) if util.is_ignored(c_name) or util.is_control_ping(class_name): continue if util.is_reply(class_name): class_references.append( class_reference_template.substitute(ref_name=ref_name)) find_class_invocations.append( find_class_invocation_template.substitute( plugin_name=plugin_name, ref_name=ref_name, class_name=class_name)) delete_class_invocations.append( delete_class_invocation_template.substitute(ref_name=ref_name)) elif util.is_notification(c_name): class_references.append( class_reference_template.substitute( ref_name=util.add_notification_suffix(ref_name))) find_class_invocations.append( find_class_invocation_template.substitute( plugin_name=plugin_name, ref_name=util.add_notification_suffix(ref_name), class_name=util.add_notification_suffix(class_name))) delete_class_invocations.append( delete_class_invocation_template.substitute( ref_name=util.add_notification_suffix(ref_name))) # add exception class to class cache ref_name = 'callbackException' class_name = 'io/fd/vpp/jvpp/VppCallbackException' class_references.append( class_reference_template.substitute(ref_name=ref_name)) find_class_invocations.append( find_class_template.substitute(ref_name=ref_name, class_name=class_name)) delete_class_invocations.append( delete_class_invocation_template.substitute(ref_name=ref_name)) return class_cache_template.substitute( class_references="".join(class_references), find_class_invocations="".join(find_class_invocations), delete_class_invocations="".join(delete_class_invocations))
def generate_callbacks(func_list, base_package, callback_package, dto_package, inputfile): """ Generates callback interfaces """ print "Generating Callback interfaces" if not os.path.exists(callback_package): raise Exception("%s folder is missing" % callback_package) callbacks = [] for func in func_list: if util.is_ignored(func['name']): continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']): continue if util.is_reply(camel_case_name_with_suffix): camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix) callback_type = "JVppCallback" else: camel_case_name_with_suffix = util.add_notification_suffix(camel_case_name_with_suffix) camel_case_name = camel_case_name_with_suffix callback_type = "JVppNotificationCallback" callbacks.append("{0}.{1}.{2}".format(base_package, callback_package, camel_case_name + callback_suffix)) callback_path = os.path.join(callback_package, camel_case_name + callback_suffix + ".java") callback_file = open(callback_path, 'w') reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix) method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type) callback_file.write( callback_template.substitute(inputfile=inputfile, docs=util.api_message_to_javadoc(func), cls_name=camel_case_name + callback_suffix, callback_method=method, base_package=base_package, callback_package=callback_package, callback_type=callback_type)) callback_file.flush() callback_file.close() callback_file = open(os.path.join(callback_package, "JVppGlobalCallback.java"), 'w') callback_file.write(global_callback_template.substitute(inputfile=inputfile, callbacks=", ".join(callbacks), base_package=base_package, callback_package=callback_package)) callback_file.flush() callback_file.close()
def generate_msg_handlers(func_list, inputfile): handlers = [] for f in func_list: handler_name = f['name'] dto_name = util.underscore_to_camelcase_upper(handler_name) ref_name = util.underscore_to_camelcase(handler_name) if is_manually_generated(handler_name) or not util.is_reply( dto_name) or util.is_ignored( handler_name) or util.is_notification(handler_name): # TODO handle notifications continue dto_setters = '' # dto setters for t in zip(f['c_types'], f['types'], f['args'], f['lengths']): c_type = t[0] jni_type = t[1] c_name = t[2] field_length = t[3] java_field_name = util.underscore_to_camelcase(c_name) jni_signature = util.jni_2_signature_mapping[jni_type] jni_setter = util.jni_field_accessors[jni_type] dto_setters += dto_field_id_template.substitute( java_name=java_field_name, class_ref_name=ref_name, jni_signature=jni_signature) dto_setter_template = dto_field_setter_templates[c_type] dto_setters += dto_setter_template.substitute( java_name=java_field_name, jni_signature=jni_signature, c_name=c_name, jni_setter=jni_setter, field_length=field_length) handlers.append( msg_handler_template.substitute( inputfile=inputfile, api_data=util.api_message_to_javadoc(f), handler_name=handler_name, dto_name=dto_name, class_ref_name=ref_name, dto_setters=dto_setters)) return "\n".join(handlers)
def generate_handler_registration(func_list): handler_registration = ["#define foreach_vpe_api_msg \\\n"] for f in func_list: name = f['name'] camelcase_name = util.underscore_to_camelcase(f['name']) if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name): # TODO handle notifications continue handler_registration.append(handler_registration_template.substitute( name=name, upercase_name=name.upper())) return "".join(handler_registration)
def generate_class_cache(func_list): class_references = [] find_class_invocations = [] for f in func_list: c_name = f['name'] class_name = util.underscore_to_camelcase_upper(c_name) ref_name = util.underscore_to_camelcase(c_name) if not util.is_reply(class_name) or util.is_ignored(c_name) or util.is_notification(c_name): # TODO handle notifications continue class_references.append(class_reference_template.substitute( ref_name=ref_name)) find_class_invocations.append(find_class_invocation_template.substitute( ref_name=ref_name, class_name=class_name)) return class_cache_template.substitute( class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))
def generate_msg_handlers(func_list, plugin_name, inputfile): handlers = [] for f in func_list: handler_name = f['name'] dto_name = util.underscore_to_camelcase_upper(handler_name) ref_name = util.underscore_to_camelcase(handler_name) if is_manually_generated(handler_name, plugin_name) or util.is_ignored(handler_name): continue if not util.is_reply(dto_name) and not util.is_notification( handler_name): continue if util.is_notification(handler_name): dto_name = util.add_notification_suffix(dto_name) ref_name = util.add_notification_suffix(ref_name) dto_setters = '' err_handler = '' # dto setters for t in zip(f['c_types'], f['types'], f['args'], f['lengths']): c_type = t[0] jni_type = t[1] c_name = t[2] field_length = t[3][0] if jni_type.endswith('Array') and field_length == '0': raise Exception( 'Variable array \'%s\' defined in message \'%s\' ' 'should have defined length (e.g. \'%s[%s_length]\'' % (c_name, handler_name, c_name, c_name)) # check if we are processing variable length array if t[3][1]: length_var_name = t[3][0] length_field_type = f['c_types'][f['args'].index( length_var_name)] field_length = variable_length_array_value_template.substitute( length_var_name=length_var_name) if length_field_type != 'u8': # we need net to host conversion: field_length = variable_length_array_template.substitute( length_field_type=length_field_type, value=field_length) # for retval don't generate setters and generate retval check if util.is_retval_field(c_name): err_handler = callback_err_handler_template.substitute( handler_name=handler_name) continue java_field_name = util.underscore_to_camelcase(c_name) jni_signature = util.jni_2_signature_mapping[jni_type] jni_setter = util.jni_field_accessors[jni_type] dto_setters += dto_field_id_template.substitute( java_name=java_field_name, class_ref_name=ref_name, jni_signature=jni_signature) dto_setter_template = dto_field_setter_templates[c_type] dto_setters += dto_setter_template.substitute( java_name=java_field_name, jni_signature=jni_signature, c_name=c_name, jni_setter=jni_setter, field_length=field_length) handlers.append( msg_handler_template.substitute( inputfile=inputfile, api_data=util.api_message_to_javadoc(f), handler_name=handler_name, plugin_name=plugin_name, dto_name=dto_name, class_ref_name=ref_name, dto_setters=dto_setters, err_handler=err_handler)) return "\n".join(handlers)
def generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile): """ Generates callback facade """ print "Generating JVpp callback facade" if os.path.exists(callback_facade_package): util.remove_folder(callback_facade_package) os.mkdir(callback_facade_package) methods = [] methods_impl = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue camel_case_name = util.underscore_to_camelcase(func['name']) camel_case_name_upper = util.underscore_to_camelcase_upper(func['name']) if util.is_reply(camel_case_name): continue # Strip suffix for dump calls callback_type = get_request_name(camel_case_name_upper, func['name']) + callback_gen.callback_suffix if len(func['args']) == 0: methods.append(no_arg_method_template.substitute(name=camel_case_name, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) else: methods.append(method_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append(method_impl_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) join = os.path.join(callback_facade_package, "CallbackJVpp.java") jvpp_file = open(join, 'w') jvpp_file.write( jvpp_ifc_template.substitute(inputfile=inputfile, methods="\n".join(methods), base_package=base_package, dto_package=dto_package, notification_package=notification_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacade.java"), 'w') jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile, methods="\n".join(methods_impl), base_package=base_package, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() generate_callback(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile)
def generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, future_facade_package, inputfile): """ Generates JVpp interface and JNI implementation """ print "Generating JVpp future facade" if not os.path.exists(future_facade_package): raise Exception("%s folder is missing" % future_facade_package) methods = [] methods_impl = [] callbacks = [] for func in func_list: if util.is_ignored(func['name']): continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']): continue camel_case_method_name = util.underscore_to_camelcase(func['name']) if not util.is_notification(func["name"]): camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name'])) if util.is_details(camel_case_name_with_suffix): camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']), func['name']) callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_dto=camel_case_name_with_suffix, callback_dto_field=camel_case_method_name, callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix, future_package=future_facade_package)) methods.append(future_jvpp_method_template.substitute(base_package=base_package, dto_package=dto_package, method_name=camel_case_request_method_name + util.underscore_to_camelcase_upper(util.dump_suffix), reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix, request_name=util.remove_reply_suffix(camel_case_reply_name) + util.underscore_to_camelcase_upper(util.dump_suffix))) methods_impl.append(future_jvpp_method_impl_template.substitute(base_package=base_package, dto_package=dto_package, method_name=camel_case_request_method_name + util.underscore_to_camelcase_upper(util.dump_suffix), reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix, request_name=util.remove_reply_suffix(camel_case_reply_name) + util.underscore_to_camelcase_upper(util.dump_suffix))) else: request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \ if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix) methods.append(future_jvpp_method_template.substitute(base_package=base_package, dto_package=dto_package, method_name=camel_case_request_method_name, reply_name=camel_case_name_with_suffix, request_name=request_name)) methods_impl.append(future_jvpp_method_impl_template.substitute(base_package=base_package, dto_package=dto_package, method_name=camel_case_request_method_name, reply_name=camel_case_name_with_suffix, request_name=request_name)) # Callback handler is a bit special and a different template has to be used if util.is_control_ping(camel_case_name_with_suffix): callbacks.append(jvpp_facade_control_ping_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_dto=camel_case_name_with_suffix, future_package=future_facade_package)) else: callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_dto=camel_case_name_with_suffix)) if util.is_notification(func["name"]): callbacks.append(jvpp_facade_callback_notification_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_dto=util.add_notification_suffix(camel_case_name_with_suffix))) jvpp_file = open(os.path.join(future_facade_package, "FutureJVppFacadeCallback.java"), 'w') jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile, base_package=base_package, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, methods="".join(callbacks), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp.java"), 'w') jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile, base_package=base_package, methods="".join(methods), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open(os.path.join(future_facade_package, "FutureJVppFacade.java"), 'w') jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile, base_package=base_package, dto_package=dto_package, methods="".join(methods_impl), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close()
def generate_dtos(func_list, base_package, dto_package, inputfile): """ Generates dto objects in a dedicated package """ print "Generating DTOs" if not os.path.exists(dto_package): raise Exception("%s folder is missing" % dto_package) for func in func_list: camel_case_dto_name = util.underscore_to_camelcase_upper(func['name']) camel_case_method_name = util.underscore_to_camelcase(func['name']) dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") if util.is_ignored(func['name']): continue fields = "" for t in zip(func['types'], func['args']): # for retval don't generate dto field in Reply field_name = util.underscore_to_camelcase(t[1]) if util.is_reply(camel_case_dto_name) and util.is_retval_field(field_name): continue fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]], name=field_name) methods = "" base_type = "" # Generate request/reply or dump/dumpReply even if structure can be used as notification if not util.is_just_notification(func["name"]): if util.is_reply(camel_case_dto_name): description = "vpe.api reply DTO" request_dto_name = get_request_name(camel_case_dto_name, func['name']) if util.is_details(camel_case_dto_name): # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name + "Dump") generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name, func) else: base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name) else: args = "" if fields is "" else "this" methods = send_template.substitute(method_name=camel_case_method_name, base_package=base_package, args=args) if util.is_dump(camel_case_dto_name): base_type += "JVppDump" description = "vpe.api dump request DTO" else: base_type += "JVppRequest" description = "vpe.api request DTO" write_dto_file(base_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func, inputfile, methods) # for structures that are also used as notifications, generate dedicated notification DTO if util.is_notification(func["name"]): base_type = "JVppNotification" description = "vpe.api notification DTO" camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name) methods = "" dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") write_dto_file(base_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func, inputfile, methods) flush_dump_reply_dtos(inputfile)
def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package, callback_package, dto_package, inputfile): """ Generates notification registry interface and implementation """ print "Generating Notification interfaces and implementation" if not os.path.exists(notification_package): os.mkdir(notification_package) callbacks = [] register_callback_methods = [] register_callback_methods_impl = [] handler_methods = [] for func in func_list: if not util.is_reply(func['name']) and not util.is_notification( func['name']): continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper( func['name']) if util.is_control_ping(camel_case_name_with_suffix): continue notification_dto = camel_case_name_with_suffix callback_ifc = camel_case_name_with_suffix + callback_gen.callback_suffix fully_qualified_callback_ifc = "{0}.{1}.{2}".format( plugin_package, callback_package, callback_ifc) callbacks.append(fully_qualified_callback_ifc) # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate # that the registration should be closed register_callback_methods.append( "java.lang.AutoCloseable register{0}({1} callback);".format( callback_ifc, fully_qualified_callback_ifc)) register_callback_methods_impl.append( register_callback_impl_template.substitute( plugin_package=plugin_package, callback_package=callback_package, dto_package=dto_package, notification=camel_case_name_with_suffix, callback=callback_ifc)) handler_methods.append( handler_impl_template.substitute( base_package=base_package, plugin_package=plugin_package, callback_package=callback_package, dto_package=dto_package, notification=notification_dto, notification_reply=camel_case_name_with_suffix, callback=callback_ifc)) callback_file = open( os.path.join(notification_package, "%sEventRegistry.java" % plugin_name), 'w') callback_file.write( notification_registry_template.substitute( inputfile=inputfile, register_callback_methods="\n ".join(register_callback_methods), base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, notification_package=notification_package)) callback_file.flush() callback_file.close() callback_file = open( os.path.join(notification_package, "Global%sEventCallback.java" % plugin_name), 'w') global_notification_callback_callbacks = "" if (callbacks): global_notification_callback_callbacks = " extends " + ", ".join( callbacks) callback_file.write( global_notification_callback_template.substitute( inputfile=inputfile, callbacks=global_notification_callback_callbacks, plugin_package=plugin_package, plugin_name=plugin_name, notification_package=notification_package)) callback_file.flush() callback_file.close() callback_file = open( os.path.join(notification_package, "%sEventRegistryImpl.java" % plugin_name), 'w') callback_file.write( notification_registry_impl_template.substitute( inputfile=inputfile, callback_package=callback_package, dto_package=dto_package, register_callback_methods="".join(register_callback_methods_impl), handler_methods="".join(handler_methods), base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, notification_package=notification_package)) callback_file.flush() callback_file.close() callback_file = open( os.path.join(notification_package, "%sEventRegistryProvider.java" % plugin_name), 'w') callback_file.write( notification_provider_template.substitute( inputfile=inputfile, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, notification_package=notification_package)) callback_file.flush() callback_file.close()
def generate_msg_handlers(func_list, inputfile): handlers = [] for f in func_list: handler_name = f['name'] dto_name = util.underscore_to_camelcase_upper(handler_name) ref_name = util.underscore_to_camelcase(handler_name) if is_manually_generated(handler_name) or util.is_ignored(handler_name): continue if not util.is_reply(dto_name) and not util.is_notification(handler_name): continue if util.is_notification(handler_name): dto_name = util.add_notification_suffix(dto_name) ref_name = util.add_notification_suffix(ref_name) dto_setters = '' err_handler = '' # dto setters for t in zip(f['c_types'], f['types'], f['args'], f['lengths']): c_type = t[0] jni_type = t[1] c_name = t[2] field_length = t[3][0] # check if we are processing variable length array if t[3][1]: length_var_name = t[3][0] length_field_type = f['c_types'][f['args'].index(length_var_name)] field_length = variable_length_array_value_template.substitute(length_var_name=length_var_name) if length_field_type != 'u8': # we need net to host conversion: field_length = variable_length_array_template.substitute( length_field_type=length_field_type, value=field_length) # for retval don't generate setters and generate retval check if util.is_retval_field(c_name): err_handler = callback_err_handler_template.substitute( handler_name=handler_name ) continue java_field_name = util.underscore_to_camelcase(c_name) jni_signature = util.jni_2_signature_mapping[jni_type] jni_setter = util.jni_field_accessors[jni_type] dto_setters += dto_field_id_template.substitute( java_name=java_field_name, class_ref_name=ref_name, jni_signature=jni_signature) dto_setter_template = dto_field_setter_templates[c_type] dto_setters += dto_setter_template.substitute( java_name=java_field_name, jni_signature=jni_signature, c_name=c_name, jni_setter=jni_setter, field_length=field_length) handlers.append(msg_handler_template.substitute( inputfile=inputfile, api_data=util.api_message_to_javadoc(f), handler_name=handler_name, dto_name=dto_name, class_ref_name=ref_name, dto_setters=dto_setters, err_handler=err_handler)) return "\n".join(handlers)
def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile): """ Generates callback facade """ print "Generating JVpp callback facade" if os.path.exists(callback_facade_package): util.remove_folder(callback_facade_package) os.mkdir(callback_facade_package) methods = [] methods_impl = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): continue camel_case_name = util.underscore_to_camelcase(func['name']) camel_case_name_upper = util.underscore_to_camelcase_upper( func['name']) if util.is_reply(camel_case_name) or util.is_control_ping( camel_case_name): continue # Strip suffix for dump calls callback_type = get_request_name( camel_case_name_upper, func['name']) + callback_gen.callback_suffix if len(func['args']) == 0: methods.append( no_arg_method_template.substitute( name=camel_case_name, base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append( no_arg_method_impl_template.substitute( name=camel_case_name, base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) else: methods.append( method_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append( method_impl_template.substitute( name=camel_case_name, request=camel_case_name_upper, base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) join = os.path.join(callback_facade_package, "CallbackJVpp%s.java" % plugin_name) jvpp_file = open(join, 'w') jvpp_file.write( jvpp_ifc_template.substitute( inputfile=inputfile, methods="\n".join(methods), base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, dto_package=dto_package, notification_package=notification_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open( os.path.join(callback_facade_package, "CallbackJVpp%sFacade.java" % plugin_name), 'w') jvpp_file.write( jvpp_impl_template.substitute( inputfile=inputfile, methods="\n".join(methods_impl), base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() generate_callback(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile)
def generate_jvpp(func_list, base_package, dto_package, inputfile): """ Generates JVpp interface and JNI implementation """ print "Generating JVpp" methods = [] methods_impl = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue camel_case_name = util.underscore_to_camelcase(func['name']) camel_case_name_upper = util.underscore_to_camelcase_upper( func['name']) if util.is_reply(camel_case_name): continue if len(func['args']) == 0: methods.append( no_arg_method_template.substitute(name=camel_case_name, base_package=base_package, dto_package=dto_package)) methods_impl.append( no_arg_method_native_template.substitute( name=camel_case_name, base_package=base_package, dto_package=dto_package)) methods_impl.append( no_arg_method_impl_template.substitute( name=camel_case_name, base_package=base_package, dto_package=dto_package)) else: methods.append( method_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package)) methods_impl.append( method_native_template.substitute( name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package)) methods_impl.append( method_impl_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package)) jvpp_file = open("JVpp.java", 'w') jvpp_file.write( jvpp_ifc_template.substitute(inputfile=inputfile, methods="\n".join(methods), base_package=base_package, dto_package=dto_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open("JVppImpl.java", 'w') jvpp_file.write( jvpp_impl_template.substitute(inputfile=inputfile, methods="\n".join(methods_impl), base_package=base_package, dto_package=dto_package)) jvpp_file.flush() jvpp_file.close()
def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile): """ Generates JVpp interface and JNI implementation """ print "Generating JVpp future facade" if not os.path.exists(future_facade_package): os.mkdir(future_facade_package) methods = [] methods_impl = [] callbacks = [] for func in func_list: camel_case_name_with_suffix = util.underscore_to_camelcase_upper( func['name']) if util.is_ignored(func['name']) or util.is_control_ping( camel_case_name_with_suffix): continue if not util.is_reply(camel_case_name_with_suffix ) and not util.is_notification(func['name']): continue camel_case_method_name = util.underscore_to_camelcase(func['name']) if not util.is_notification(func["name"]): camel_case_request_method_name = util.remove_reply_suffix( util.underscore_to_camelcase(func['name'])) if util.is_details(camel_case_name_with_suffix): camel_case_reply_name = get_standard_dump_reply_name( util.underscore_to_camelcase_upper(func['name']), func['name']) callbacks.append( jvpp_facade_details_callback_method_template.substitute( base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_dto=camel_case_name_with_suffix, callback_dto_field=camel_case_method_name, callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix, future_package=future_facade_package)) methods.append( future_jvpp_method_template.substitute( plugin_package=plugin_package, dto_package=dto_package, method_name=camel_case_request_method_name + util.underscore_to_camelcase_upper(util.dump_suffix), reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix, request_name=util.remove_reply_suffix( camel_case_reply_name) + util.underscore_to_camelcase_upper(util.dump_suffix))) methods_impl.append( future_jvpp_dump_method_impl_template.substitute( plugin_package=plugin_package, dto_package=dto_package, method_name=camel_case_request_method_name + util.underscore_to_camelcase_upper(util.dump_suffix), reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix, request_name=util.remove_reply_suffix( camel_case_reply_name) + util.underscore_to_camelcase_upper(util.dump_suffix))) else: request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \ if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix) methods.append( future_jvpp_method_template.substitute( plugin_package=plugin_package, dto_package=dto_package, method_name=camel_case_request_method_name, reply_name=camel_case_name_with_suffix, request_name=request_name)) methods_impl.append( future_jvpp_method_impl_template.substitute( plugin_package=plugin_package, dto_package=dto_package, method_name=camel_case_request_method_name, reply_name=camel_case_name_with_suffix, request_name=request_name)) callbacks.append( jvpp_facade_callback_method_template.substitute( base_package=base_package, plugin_package=plugin_package, dto_package=dto_package, callback_dto=camel_case_name_with_suffix)) if util.is_notification(func["name"]): callbacks.append( jvpp_facade_callback_notification_method_template.substitute( plugin_package=plugin_package, dto_package=dto_package, callback_dto=util.add_notification_suffix( camel_case_name_with_suffix))) jvpp_file = open( os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w') jvpp_file.write( jvpp_facade_callback_template.substitute( inputfile=inputfile, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, dto_package=dto_package, notification_package=notification_package, callback_package=callback_package, methods="".join(callbacks), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open( os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w') jvpp_file.write( future_jvpp_template.substitute( inputfile=inputfile, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, notification_package=notification_package, methods="".join(methods), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open( os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w') jvpp_file.write( future_jvpp_facade_template.substitute( inputfile=inputfile, base_package=base_package, plugin_package=plugin_package, plugin_name=plugin_name, dto_package=dto_package, notification_package=notification_package, methods="".join(methods_impl), future_package=future_facade_package)) jvpp_file.flush() jvpp_file.close()
def generate_dtos(func_list, base_package, dto_package, inputfile): """ Generates dto objects in a dedicated package """ print "Generating DTOs" if not os.path.exists(dto_package): raise Exception("%s folder is missing" % dto_package) for func in func_list: camel_case_dto_name = util.underscore_to_camelcase_upper(func['name']) camel_case_method_name = util.underscore_to_camelcase(func['name']) dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue fields = "" for t in zip(func['types'], func['args']): fields += field_template.substitute( type=util.jni_2_java_type_mapping[t[0]], name=util.underscore_to_camelcase(t[1])) methods = "" base_type = "" if util.is_reply(camel_case_dto_name): description = "vpe.api reply DTO" request_dto_name = get_request_name(camel_case_dto_name, func['name']) if util.is_details(camel_case_dto_name): # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api base_type += "JVppReply<%s.%s.%s>" % ( base_package, dto_package, request_dto_name + "Dump") generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name, func) else: base_type += "JVppReply<%s.%s.%s>" % ( base_package, dto_package, request_dto_name) else: args = "" if fields is "" else "this" methods = send_template.substitute( method_name=camel_case_method_name, base_package=base_package, args=args) if util.is_dump(camel_case_dto_name): base_type += "JVppDump" description = "vpe.api dump request DTO" else: base_type += "JVppRequest" description = "vpe.api request DTO" dto_file = open(dto_path, 'w') dto_file.write( dto_template.substitute(inputfile=inputfile, description=description, docs=util.api_message_to_javadoc(func), cls_name=camel_case_dto_name, fields=fields, methods=methods, base_package=base_package, base_type=base_type, dto_package=dto_package)) dto_file.flush() dto_file.close() flush_dump_reply_dtos(inputfile)