jvpp: added logs for sending and receiving event messages (VPP-982)
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / notification_gen.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17
18 import callback_gen
19 import util
20 from string import Template
21
22 notification_registry_template = Template("""
23 package $plugin_package.$notification_package;
24
25 /**
26  * <p>Registry for notification callbacks defined in ${plugin_name}.
27  * <br>It was generated by notification_gen.py based on $inputfile
28  * <br>(python representation of api file generated by vppapigen).
29  */
30 public interface ${plugin_name}NotificationRegistry extends $base_package.$notification_package.NotificationRegistry {
31
32     $register_callback_methods
33
34     @Override
35     void close();
36 }
37 """)
38
39 global_notification_callback_template = Template("""
40 package $plugin_package.$notification_package;
41
42 /**
43  * <p>Aggregated callback interface for notifications only.
44  * <br>It was generated by notification_gen.py based on $inputfile
45  * <br>(python representation of api file generated by vppapigen).
46  */
47 public interface Global${plugin_name}NotificationCallback$callbacks {
48
49 }
50 """)
51
52 notification_registry_impl_template = Template("""
53 package $plugin_package.$notification_package;
54
55 /**
56  * <p>Notification registry delegating notification processing to registered callbacks.
57  * <br>It was generated by notification_gen.py based on $inputfile
58  * <br>(python representation of api file generated by vppapigen).
59  */
60 public final class ${plugin_name}NotificationRegistryImpl implements ${plugin_name}NotificationRegistry, Global${plugin_name}NotificationCallback {
61
62     // TODO add a special NotificationCallback interface and only allow those to be registered
63     private final java.util.concurrent.ConcurrentMap<Class<? extends $base_package.$dto_package.JVppNotification>, $base_package.$callback_package.JVppNotificationCallback> registeredCallbacks =
64         new java.util.concurrent.ConcurrentHashMap<>();
65     private static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}NotificationRegistryImpl.class.getName());
66
67     $register_callback_methods
68     $handler_methods
69
70     @Override
71     public void close() {
72         registeredCallbacks.clear();
73     }
74 }
75 """)
76
77 register_callback_impl_template = Template("""
78     public java.lang.AutoCloseable register$callback(final $plugin_package.$callback_package.$callback callback){
79         if(null != registeredCallbacks.putIfAbsent($plugin_package.$dto_package.$notification.class, callback)){
80             throw new IllegalArgumentException("Callback for " + $plugin_package.$dto_package.$notification.class +
81                 "notification already registered");
82         }
83         return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
84     }
85 """)
86
87 handler_impl_template = Template("""
88     @Override
89     public void on$notification(
90         final $plugin_package.$dto_package.$notification notification) {
91         if (LOG.isLoggable(java.util.logging.Level.FINE)) {
92             LOG.fine(String.format("Received $notification event message: %s", notification));
93         }
94         final $base_package.$callback_package.JVppNotificationCallback jVppNotificationCallback = registeredCallbacks.get($plugin_package.$dto_package.$notification.class);
95         if (null != jVppNotificationCallback) {
96             (($plugin_package.$callback_package.$callback) registeredCallbacks
97                 .get($plugin_package.$dto_package.$notification.class))
98                 .on$notification(notification);
99         }
100     }
101 """)
102
103 notification_provider_template = Template("""
104 package $plugin_package.$notification_package;
105
106  /**
107  * Provides ${plugin_name}NotificationRegistry.
108  * <br>The file was generated by notification_gen.py based on $inputfile
109  * <br>(python representation of api file generated by vppapigen).
110  */
111 public interface ${plugin_name}NotificationRegistryProvider extends $base_package.$notification_package.NotificationRegistryProvider {
112
113     @Override
114     public ${plugin_name}NotificationRegistry getNotificationRegistry();
115 }
116 """)
117
118
119 def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package, callback_package, dto_package, inputfile):
120     """ Generates notification registry interface and implementation """
121     print "Generating Notification interfaces and implementation"
122
123     if not os.path.exists(notification_package):
124         os.mkdir(notification_package)
125
126     callbacks = []
127     register_callback_methods = []
128     register_callback_methods_impl = []
129     handler_methods = []
130     for func in func_list:
131
132         if not util.is_notification(func['name']):
133             continue
134
135         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
136         notification_dto = util.add_notification_suffix(camel_case_name_with_suffix)
137         callback_ifc = notification_dto + callback_gen.callback_suffix
138         fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
139         callbacks.append(fully_qualified_callback_ifc)
140
141         # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
142         # that the registration should be closed
143         register_callback_methods.append("java.lang.AutoCloseable register{0}({1} callback);"
144                                          .format(callback_ifc, fully_qualified_callback_ifc))
145         register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package,
146                                                                                          callback_package=callback_package,
147                                                                                          dto_package=dto_package,
148                                                                                          notification=notification_dto,
149                                                                                          callback=callback_ifc))
150         handler_methods.append(handler_impl_template.substitute(base_package=base_package,
151                                                                 plugin_package=plugin_package,
152                                                                 callback_package=callback_package,
153                                                                 dto_package=dto_package,
154                                                                 notification=notification_dto,
155                                                                 callback=callback_ifc))
156
157
158     callback_file = open(os.path.join(notification_package, "%sNotificationRegistry.java" % plugin_name), 'w')
159     callback_file.write(notification_registry_template.substitute(inputfile=inputfile,
160                                                                 register_callback_methods="\n    ".join(register_callback_methods),
161                                                                 base_package=base_package,
162                                                                 plugin_package=plugin_package,
163                                                                 plugin_name=plugin_name,
164                                                                 notification_package=notification_package))
165     callback_file.flush()
166     callback_file.close()
167
168     callback_file = open(os.path.join(notification_package, "Global%sNotificationCallback.java" % plugin_name), 'w')
169
170     global_notification_callback_callbacks = ""
171     if (callbacks):
172         global_notification_callback_callbacks = " extends " + ", ".join(callbacks)
173
174     callback_file.write(global_notification_callback_template.substitute(inputfile=inputfile,
175                                                                        callbacks=global_notification_callback_callbacks,
176                                                                        plugin_package=plugin_package,
177                                                                        plugin_name=plugin_name,
178                                                                        notification_package=notification_package))
179     callback_file.flush()
180     callback_file.close()
181
182     callback_file = open(os.path.join(notification_package, "%sNotificationRegistryImpl.java" % plugin_name), 'w')
183     callback_file.write(notification_registry_impl_template.substitute(inputfile=inputfile,
184                                                                      callback_package=callback_package,
185                                                                      dto_package=dto_package,
186                                                                      register_callback_methods="".join(register_callback_methods_impl),
187                                                                      handler_methods="".join(handler_methods),
188                                                                      base_package=base_package,
189                                                                      plugin_package=plugin_package,
190                                                                      plugin_name=plugin_name,
191                                                                      notification_package=notification_package))
192     callback_file.flush()
193     callback_file.close()
194
195     callback_file = open(os.path.join(notification_package, "%sNotificationRegistryProvider.java" % plugin_name), 'w')
196     callback_file.write(notification_provider_template.substitute(inputfile=inputfile,
197                                                                      base_package=base_package,
198                                                                      plugin_package=plugin_package,
199                                                                      plugin_name=plugin_name,
200                                                                      notification_package=notification_package))
201     callback_file.flush()
202     callback_file.close()
203