Esempio n. 1
0
def generate_callbacks(func_list, base_package, plugin_package, plugin_name,
                       callback_package, dto_package, inputfile, logger):
    """ Generates callback interfaces """
    logger.debug("Generating Callback interfaces for %s" % inputfile)

    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_control_ping(camel_case_name_with_suffix):
            # Skip control_ping managed by jvpp registry.
            continue
        if util.is_dump(func['name']) or util.is_request(
                func['name'], func_list):
            continue

        # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
        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()
Esempio n. 2
0
def generate_dtos(func_list, base_package, plugin_package, plugin_name,
                  dto_package, inputfile, logger):
    """ Generates dto objects in a dedicated package """
    logger.debug("Generating DTOs for %s" % inputfile)

    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_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 = ""

        if util.is_reply(camel_case_dto_name):
            description = "reply DTO"
            request_dto_name = util.remove_reply_suffix(camel_case_dto_name)
            if util.is_details(camel_case_dto_name):
                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)
        elif util.is_dump(camel_case_dto_name) or util.is_request(
                func['name'], func_list):
            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"
        else:
            description = "event DTO"
            dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")

        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)
Esempio n. 3
0
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):
            # Skip control ping managed by jvpp registry.
            continue
        if util.is_dump(handler_name) or util.is_request(
                handler_name, func_list):
            continue

        # Generate msg handlers for all messages except for dumps and requests (handled by vpp, not client).
        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, 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_control_ping(camel_case_name_with_suffix):
            # Skip control ping managed by jvpp registry.
            continue
        if util.is_dump(func['name']) or util.is_request(
                func['name'], func_list):
            continue

        # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
        if util.is_reply(camel_case_name_with_suffix):
            request_method = 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=camel_case_name_with_suffix +
                    callback_gen.callback_suffix,
                    callback_dto=request_method))
        else:
            callbacks.append(
                jvpp_facade_callback_notification_method_template.substitute(
                    plugin_package=plugin_package,
                    dto_package=dto_package,
                    callback_package=callback_package,
                    callback=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,
                     "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()
Esempio n. 5
0
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(name)

        if util.is_control_ping(camelcase_name):
            # Skip control ping managed by registry.
            continue
        if util.is_dump(name) or util.is_request(name, func_list):
            continue

        # Generate msg handler registration for all messages except for dumps and requests.
        handler_registration.append(
            handler_registration_template.substitute(name=name, crc=f['crc']))

    return "".join(handler_registration)
Esempio n. 6
0
def generate_jni_impl(func_list, plugin_name, 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):
            # Skip control ping managed by jvpp registry.
            continue
        if not (util.is_dump(f_name) or util.is_request(f_name, func_list)):
            continue

        # Generate jni bindings for sending dump and request messages.
        arguments = ''
        request_class = ''
        jni_identifiers = ''
        msg_initialization = ''
        f_name_uppercase = f_name.upper()
        msg_size = 'sizeof(*mp)'

        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,
                plugin_name=plugin_name)

            for t in zip(f['types'], f['args'], f['lengths'], f['arg_types']):
                field_name = util.underscore_to_camelcase(t[1])
                is_variable_len_array = t[2][1]
                if is_variable_len_array:
                    msg_size += jni_msg_size_template.substitute(
                        array_length=util.underscore_to_camelcase(t[2][0]),
                        element_type=t[3])
                jni_identifiers += jni_gen.jni_request_identifiers_for_type(
                    field_type=t[0],
                    field_reference_name=field_name,
                    field_name=field_name)
                msg_initialization += jni_gen.jni_request_binding_for_type(
                    field_type=t[0],
                    c_name=t[1],
                    field_reference_name=field_name,
                    field_length=t[2][0],
                    is_variable_len_array=is_variable_len_array)

        jni_impl.append(
            jni_impl_template.substitute(
                inputfile=inputfile,
                api_data=util.api_message_to_javadoc(f),
                field_reference_name=camel_case_function_name,
                field_name=camel_case_function_name,
                c_name_uppercase=f_name_uppercase,
                c_name=f_name,
                crc=f['crc'],
                plugin_name=plugin_name,
                java_plugin_name=plugin_name.title(),
                request_class=request_class,
                jni_identifiers=jni_identifiers,
                msg_size=msg_size,
                msg_initialization=msg_initialization,
                args=arguments))

    return "\n".join(jni_impl)
Esempio n. 7
0
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:
        camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])

        if util.is_control_ping(camel_case_name_with_suffix):
            # Skip control ping managed by jvpp registry.
            continue
        if util.is_dump(func['name']) or util.is_request(func['name'], func_list):
            continue

        # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
        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()
Esempio n. 8
0
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 = []

    # Generate methods for sending messages.
    for func in func_list:
        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)
        if util.is_dump(camel_case_name_upper):
            callback_type += "Details"
        elif util.is_request(func['name'], func_list):
            callback_type += "Reply"
        else:
            # Skip messages that do not not have replies (e.g events/counters).
            continue
        callback_type += 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)