jvpp: use Python's logging API
[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}EventRegistry extends $base_package.$notification_package.EventRegistry {
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}EventCallback$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}EventRegistryImpl implements ${plugin_name}EventRegistry, Global${plugin_name}EventCallback {
61
62     // TODO add a special NotificationCallback interface and only allow those to be registered
63     private final java.util.concurrent.ConcurrentMap<Class<?>, $base_package.$callback_package.JVppCallback> registeredCallbacks =
64         new java.util.concurrent.ConcurrentHashMap<>();
65     private static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
66
67     $register_callback_methods
68     $handler_methods
69
70     @Override
71     public void close() {
72         registeredCallbacks.clear();
73     }
74
75     @Override
76     public void onError(io.fd.vpp.jvpp.VppCallbackException ex) {
77         java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
78         LOG.log(java.util.logging.Level.WARNING, String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
79             ex.getCtxId(), ex.getErrorCode()), ex);
80     }
81 }
82 """)
83
84 register_callback_impl_template = Template("""
85     public java.lang.AutoCloseable register$callback(final $plugin_package.$callback_package.$callback callback){
86         if(null != registeredCallbacks.putIfAbsent($plugin_package.$dto_package.$notification.class, callback)){
87             throw new IllegalArgumentException("Callback for " + $plugin_package.$dto_package.$notification.class +
88                 "notification already registered");
89         }
90         return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
91     }
92 """)
93
94 handler_impl_template = Template("""
95     @Override
96     public void on$notification(
97         final $plugin_package.$dto_package.$notification_reply notification) {
98         if (LOG.isLoggable(java.util.logging.Level.FINE)) {
99             LOG.fine(String.format("Received $notification event message: %s", notification));
100         }
101         final $base_package.$callback_package.JVppCallback jVppCallback = registeredCallbacks.get($plugin_package.$dto_package.$notification.class);
102         if (null != jVppCallback) {
103             (($plugin_package.$callback_package.$callback) registeredCallbacks
104                 .get($plugin_package.$dto_package.$notification.class))
105                 .on$notification(notification);
106         }
107     }
108 """)
109
110 notification_provider_template = Template("""
111 package $plugin_package.$notification_package;
112
113  /**
114  * Provides ${plugin_name}EventRegistry.
115  * <br>The file was generated by notification_gen.py based on $inputfile
116  * <br>(python representation of api file generated by vppapigen).
117  */
118 public interface ${plugin_name}EventRegistryProvider extends $base_package.$notification_package.EventRegistryProvider {
119
120     @Override
121     public ${plugin_name}EventRegistry getEventRegistry();
122 }
123 """)
124
125
126 def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package,
127                                    callback_package, dto_package, inputfile, logger):
128     """ Generates notification registry interface and implementation """
129     logger.debug("Generating Notification interfaces and implementation for %s" % inputfile)
130
131     if not os.path.exists(notification_package):
132         os.mkdir(notification_package)
133
134     callbacks = []
135     register_callback_methods = []
136     register_callback_methods_impl = []
137     handler_methods = []
138     for func in func_list:
139         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
140
141         if util.is_control_ping(camel_case_name_with_suffix):
142             # Skip control ping managed by jvpp registry.
143             continue
144         if util.is_dump(func['name']) or util.is_request(func['name'], func_list):
145             continue
146
147         # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
148         notification_dto = camel_case_name_with_suffix
149         callback_ifc = camel_case_name_with_suffix + callback_gen.callback_suffix
150         fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
151         callbacks.append(fully_qualified_callback_ifc)
152
153         # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
154         # that the registration should be closed
155         register_callback_methods.append("java.lang.AutoCloseable register{0}({1} callback);"
156                                          .format(callback_ifc, fully_qualified_callback_ifc))
157         register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package,
158                                                                                          callback_package=callback_package,
159                                                                                          dto_package=dto_package,
160                                                                                          notification=camel_case_name_with_suffix,
161                                                                                          callback=callback_ifc))
162         handler_methods.append(handler_impl_template.substitute(base_package=base_package,
163                                                                 plugin_package=plugin_package,
164                                                                 callback_package=callback_package,
165                                                                 dto_package=dto_package,
166                                                                 notification=notification_dto,
167                                                                 notification_reply=camel_case_name_with_suffix,
168                                                                 callback=callback_ifc))
169
170     callback_file = open(os.path.join(notification_package, "%sEventRegistry.java" % plugin_name), 'w')
171     callback_file.write(notification_registry_template.substitute(inputfile=inputfile,
172                                                                 register_callback_methods="\n    ".join(register_callback_methods),
173                                                                 base_package=base_package,
174                                                                 plugin_package=plugin_package,
175                                                                 plugin_name=plugin_name,
176                                                                 notification_package=notification_package))
177     callback_file.flush()
178     callback_file.close()
179
180     callback_file = open(os.path.join(notification_package, "Global%sEventCallback.java" % plugin_name), 'w')
181
182     global_notification_callback_callbacks = ""
183     if callbacks:
184         global_notification_callback_callbacks = " extends " + ", ".join(callbacks)
185
186     callback_file.write(global_notification_callback_template.substitute(inputfile=inputfile,
187                                                                        callbacks=global_notification_callback_callbacks,
188                                                                        plugin_package=plugin_package,
189                                                                        plugin_name=plugin_name,
190                                                                        notification_package=notification_package))
191     callback_file.flush()
192     callback_file.close()
193
194     callback_file = open(os.path.join(notification_package, "%sEventRegistryImpl.java" % plugin_name), 'w')
195     callback_file.write(notification_registry_impl_template.substitute(inputfile=inputfile,
196                                                                      callback_package=callback_package,
197                                                                      dto_package=dto_package,
198                                                                      register_callback_methods="".join(register_callback_methods_impl),
199                                                                      handler_methods="".join(handler_methods),
200                                                                      base_package=base_package,
201                                                                      plugin_package=plugin_package,
202                                                                      plugin_name=plugin_name,
203                                                                      notification_package=notification_package))
204     callback_file.flush()
205     callback_file.close()
206
207     callback_file = open(os.path.join(notification_package, "%sEventRegistryProvider.java" % plugin_name), 'w')
208     callback_file.write(notification_provider_template.substitute(inputfile=inputfile,
209                                                                      base_package=base_package,
210                                                                      plugin_package=plugin_package,
211                                                                      plugin_name=plugin_name,
212                                                                      notification_package=notification_package))
213     callback_file.flush()
214     callback_file.close()
215