docs: change code blocks from "shell" to "console"
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / notification_gen.py
index 66de7a5..69e870e 100644 (file)
@@ -1,6 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env python2
 #
-# Copyright (c) 2016 Cisco and/or its affiliates.
+# Copyright (c) 2016,2018 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+from string import Template
 
-import os
+from jvpp_model import is_control_ping, is_control_ping_reply, is_dump, is_request
+
+
+def generate_notifications(work_dir, model, logger):
+    """ Generates notification registry interface and implementation """
+    logger.debug("Generating Notification interfaces and implementation for %s" % model.json_api_files)
+    messages = filter(_notification_filter, model.messages)
+    _generate_global_event_callback(work_dir, model, messages)
+    _generate_event_registry(work_dir, model, messages)
+    _generate_event_registry_impl(work_dir, model, messages)
+    _generate_event_registry_provider(work_dir, model)
 
-import callback_gen
-import util
-from string import Template
 
-notification_registry_template = Template("""
-package $plugin_package.$notification_package;
+def _notification_filter(msg):
+    # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
+    # Also skip control ping managed by jvpp registry.
+    return (not is_control_ping(msg)) and \
+           (not is_control_ping_reply(msg)) and \
+           (not is_dump(msg)) and \
+           (not is_request(msg))
+
+
+def _generate_event_registry(work_dir, model, messages):
+    plugin_name = model.plugin_java_name
+    plugin_package = model.plugin_package
+
+    register_callback_methods = []
+    for msg in messages:
+        name = _callback_name(msg)
+        fqn_name = _fqn_callback_name(plugin_package, name)
+        # 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%s(%s callback);" % (name, fqn_name))
+
+    with open("%s/%sEventRegistry.java" % (work_dir, plugin_name), "w") as f:
+        f.write(_EVENT_REGISTRY_TEMPLATE.substitute(
+            plugin_package=plugin_package,
+            plugin_name=plugin_name,
+            json_filename=model.json_api_files,
+            register_callback_methods="\n".join(register_callback_methods)
+        ))
+
+_EVENT_REGISTRY_TEMPLATE = Template("""
+package $plugin_package.notification;
 
 /**
  * <p>Registry for notification callbacks defined in ${plugin_name}.
- * <br>It was generated by notification_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
+ * <br>It was generated by notification_gen.py based on $json_filename.
  */
-public interface ${plugin_name}NotificationRegistry extends $base_package.$notification_package.NotificationRegistry {
+public interface ${plugin_name}EventRegistry extends io.fd.vpp.jvpp.notification.EventRegistry {
 
-    $register_callback_methods
+$register_callback_methods
 
     @Override
     void close();
 }
 """)
 
-global_notification_callback_template = Template("""
-package $plugin_package.$notification_package;
 
-/**
- * <p>Aggregated callback interface for notifications only.
- * <br>It was generated by notification_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
- */
-public interface Global${plugin_name}NotificationCallback$callbacks {
+def _generate_event_registry_impl(work_dir, model, messages):
+    plugin_name = model.plugin_java_name
+    plugin_package = model.plugin_package
 
-}
+    register_callback_methods = []
+    handler_methods = []
+    for msg in messages:
+        notification = msg.java_name_upper
+        callback = "%sCallback" % notification
+        register_callback_methods.append(_REGISTER_CALLBACK_IMPL_TEMPLATE.substitute(
+            plugin_package=plugin_package,
+            notification=notification,
+            callback=callback
+        ))
+        handler_methods.append(_HANDLER_IMPL_TEMPLATE.substitute(
+            plugin_package=plugin_package,
+            notification=notification,
+            callback=callback
+        ))
+
+    with open("%s/%sEventRegistryImpl.java" % (work_dir, plugin_name), "w") as f:
+        f.write(_EVENT_REGISTRY_IMPL_TEMPLATE.substitute(
+            plugin_package=plugin_package,
+            plugin_name=plugin_name,
+            json_filename=model.json_api_files,
+            register_callback_methods="".join(register_callback_methods),
+            handler_methods="".join(handler_methods)
+        ))
+
+_REGISTER_CALLBACK_IMPL_TEMPLATE = Template("""
+    public java.lang.AutoCloseable register$callback(final $plugin_package.callback.$callback callback){
+        if(null != registeredCallbacks.putIfAbsent($plugin_package.dto.$notification.class, callback)){
+            throw new IllegalArgumentException("Callback for " + $plugin_package.dto.$notification.class +
+                "notification already registered");
+        }
+        return () -> registeredCallbacks.remove($plugin_package.dto.$notification.class);
+    }
 """)
 
-notification_registry_impl_template = Template("""
-package $plugin_package.$notification_package;
+_HANDLER_IMPL_TEMPLATE = Template("""
+    @Override
+    public void on$notification(
+        final $plugin_package.dto.$notification notification) {
+        if (LOG.isLoggable(java.util.logging.Level.FINE)) {
+            LOG.fine(String.format("Received $notification event message: %s", notification));
+        }
+        final io.fd.vpp.jvpp.callback.JVppCallback jVppCallback = registeredCallbacks.get($plugin_package.dto.$notification.class);
+        if (null != jVppCallback) {
+            (($plugin_package.callback.$callback) registeredCallbacks
+                .get($plugin_package.dto.$notification.class))
+                .on$notification(notification);
+        }
+    }
+""")
+
+_EVENT_REGISTRY_IMPL_TEMPLATE = Template("""
+package $plugin_package.notification;
 
 /**
  * <p>Notification registry delegating notification processing to registered callbacks.
- * <br>It was generated by notification_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
+ * <br>It was generated by notification_gen.py based on $json_filename.
  */
-public final class ${plugin_name}NotificationRegistryImpl implements ${plugin_name}NotificationRegistry, Global${plugin_name}NotificationCallback {
+public final class ${plugin_name}EventRegistryImpl implements ${plugin_name}EventRegistry, Global${plugin_name}EventCallback {
 
     // TODO add a special NotificationCallback interface and only allow those to be registered
-    private final java.util.concurrent.ConcurrentMap<Class<? extends $base_package.$dto_package.JVppNotification>, $base_package.$callback_package.JVppNotificationCallback> registeredCallbacks =
+    private final java.util.concurrent.ConcurrentMap<Class<?>, io.fd.vpp.jvpp.callback.JVppCallback> registeredCallbacks =
         new java.util.concurrent.ConcurrentHashMap<>();
-    private static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}NotificationRegistryImpl.class.getName());
+    private static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
 
     $register_callback_methods
     $handler_methods
@@ -71,133 +149,78 @@ public final class ${plugin_name}NotificationRegistryImpl implements ${plugin_na
     public void close() {
         registeredCallbacks.clear();
     }
-}
-""")
 
-register_callback_impl_template = Template("""
-    public java.lang.AutoCloseable register$callback(final $plugin_package.$callback_package.$callback callback){
-        if(null != registeredCallbacks.putIfAbsent($plugin_package.$dto_package.$notification.class, callback)){
-            throw new IllegalArgumentException("Callback for " + $plugin_package.$dto_package.$notification.class +
-                "notification already registered");
-        }
-        return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
-    }
-""")
-
-handler_impl_template = Template("""
     @Override
-    public void on$notification(
-        final $plugin_package.$dto_package.$notification notification) {
-        if (LOG.isLoggable(java.util.logging.Level.FINE)) {
-            LOG.fine(String.format("Received $notification event message: %s", notification));
-        }
-        final $base_package.$callback_package.JVppNotificationCallback jVppNotificationCallback = registeredCallbacks.get($plugin_package.$dto_package.$notification.class);
-        if (null != jVppNotificationCallback) {
-            (($plugin_package.$callback_package.$callback) registeredCallbacks
-                .get($plugin_package.$dto_package.$notification.class))
-                .on$notification(notification);
-        }
+    public void onError(io.fd.vpp.jvpp.VppCallbackException ex) {
+        java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
+        LOG.log(java.util.logging.Level.WARNING, String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
+            ex.getCtxId(), ex.getErrorCode()), ex);
     }
+}
 """)
 
-notification_provider_template = Template("""
-package $plugin_package.$notification_package;
 
- /**
- * Provides ${plugin_name}NotificationRegistry.
- * <br>The file was generated by notification_gen.py based on $inputfile
- * <br>(python representation of api file generated by vppapigen).
+def _generate_global_event_callback(work_dir, model, messages):
+    plugin_name = model.plugin_java_name
+    plugin_package = model.plugin_package
+
+    callbacks = ""
+    callback_list = []
+    for msg in messages:
+        fqn_name = _fqn_callback_name(plugin_package, _callback_name(msg))
+        callback_list.append(fqn_name)
+
+    if callback_list:
+        callbacks = " extends %s" % ", ".join(callback_list)
+
+    with open("%s/Global%sEventCallback.java" % (work_dir, plugin_name), "w") as f:
+        f.write(_GLOBAL_EVENT_CALLBACK_TEMPLATE.substitute(
+            plugin_package=plugin_package,
+            plugin_name=plugin_name,
+            json_filename=model.json_api_files,
+            callbacks=callbacks
+        ))
+
+_GLOBAL_EVENT_CALLBACK_TEMPLATE = Template("""
+package $plugin_package.notification;
+
+/**
+ * <p>Aggregated callback interface for notifications only.
+ * <br>It was generated by notification_gen.py based on $json_filename.
  */
-public interface ${plugin_name}NotificationRegistryProvider extends $base_package.$notification_package.NotificationRegistryProvider {
+public interface Global${plugin_name}EventCallback$callbacks {
 
-    @Override
-    public ${plugin_name}NotificationRegistry getNotificationRegistry();
 }
 """)
 
 
-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"
+def _generate_event_registry_provider(work_dir, model):
+    plugin_name = model.plugin_java_name
+    with open("%s/%sEventRegistryProvider.java" % (work_dir, plugin_name), "w") as f:
+        f.write(_EVENT_REGISTRY_PROVIDER_TEMPLATE.substitute(
+            plugin_package=model.plugin_package,
+            plugin_name=plugin_name,
+            json_filename=model.json_api_files
+        ))
 
-    if not os.path.exists(notification_package):
-        os.mkdir(notification_package)
+_EVENT_REGISTRY_PROVIDER_TEMPLATE = Template("""
+package $plugin_package.notification;
 
-    callbacks = []
-    register_callback_methods = []
-    register_callback_methods_impl = []
-    handler_methods = []
-    for func in func_list:
+ /**
+ * Provides ${plugin_name}EventRegistry.
+ * <br>The file was generated by notification_gen.py based on $json_filename.
+ */
+public interface ${plugin_name}EventRegistryProvider extends io.fd.vpp.jvpp.notification.EventRegistryProvider {
+
+    @Override
+    public ${plugin_name}EventRegistry getEventRegistry();
+}
+""")
 
-        if not util.is_notification(func['name']):
-            continue
 
-        camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
-        notification_dto = util.add_notification_suffix(camel_case_name_with_suffix)
-        callback_ifc = notification_dto + callback_gen.callback_suffix
-        fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
-        callbacks.append(fully_qualified_callback_ifc)
+def _callback_name(msg):
+    return "%sCallback" % msg.java_name_upper
 
-        # 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=notification_dto,
-                                                                                         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,
-                                                                callback=callback_ifc))
-
-
-    callback_file = open(os.path.join(notification_package, "%sNotificationRegistry.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%sNotificationCallback.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, "%sNotificationRegistryImpl.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, "%sNotificationRegistryProvider.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 _fqn_callback_name(plugin_package, callback_name):
+    return "%s.callback.%s" % (plugin_package, callback_name)