API: Use string type instead of u8.
[vpp.git] / extras / japi / java / jvpp / gen / jvppgen / notification_gen.py
1 #!/usr/bin/env python2
2 #
3 # Copyright (c) 2016,2018 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 from string import Template
16
17 from jvpp_model import is_control_ping, is_control_ping_reply, is_dump, is_request
18
19
20 def generate_notifications(work_dir, model, logger):
21     """ Generates notification registry interface and implementation """
22     logger.debug("Generating Notification interfaces and implementation for %s" % model.json_api_files)
23     messages = filter(_notification_filter, model.messages)
24     _generate_global_event_callback(work_dir, model, messages)
25     _generate_event_registry(work_dir, model, messages)
26     _generate_event_registry_impl(work_dir, model, messages)
27     _generate_event_registry_provider(work_dir, model)
28
29
30 def _notification_filter(msg):
31     # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
32     # Also skip control ping managed by jvpp registry.
33     return (not is_control_ping(msg)) and \
34            (not is_control_ping_reply(msg)) and \
35            (not is_dump(msg)) and \
36            (not is_request(msg))
37
38
39 def _generate_event_registry(work_dir, model, messages):
40     plugin_name = model.plugin_java_name
41     plugin_package = model.plugin_package
42
43     register_callback_methods = []
44     for msg in messages:
45         name = _callback_name(msg)
46         fqn_name = _fqn_callback_name(plugin_package, name)
47         # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
48         # that the registration should be closed
49         register_callback_methods.append("    java.lang.AutoCloseable register%s(%s callback);" % (name, fqn_name))
50
51     with open("%s/%sEventRegistry.java" % (work_dir, plugin_name), "w") as f:
52         f.write(_EVENT_REGISTRY_TEMPLATE.substitute(
53             plugin_package=plugin_package,
54             plugin_name=plugin_name,
55             json_filename=model.json_api_files,
56             register_callback_methods="\n".join(register_callback_methods)
57         ))
58
59 _EVENT_REGISTRY_TEMPLATE = Template("""
60 package $plugin_package.notification;
61
62 /**
63  * <p>Registry for notification callbacks defined in ${plugin_name}.
64  * <br>It was generated by notification_gen.py based on $json_filename.
65  */
66 public interface ${plugin_name}EventRegistry extends io.fd.vpp.jvpp.notification.EventRegistry {
67
68 $register_callback_methods
69
70     @Override
71     void close();
72 }
73 """)
74
75
76 def _generate_event_registry_impl(work_dir, model, messages):
77     plugin_name = model.plugin_java_name
78     plugin_package = model.plugin_package
79
80     register_callback_methods = []
81     handler_methods = []
82     for msg in messages:
83         notification = msg.java_name_upper
84         callback = "%sCallback" % notification
85         register_callback_methods.append(_REGISTER_CALLBACK_IMPL_TEMPLATE.substitute(
86             plugin_package=plugin_package,
87             notification=notification,
88             callback=callback
89         ))
90         handler_methods.append(_HANDLER_IMPL_TEMPLATE.substitute(
91             plugin_package=plugin_package,
92             notification=notification,
93             callback=callback
94         ))
95
96     with open("%s/%sEventRegistryImpl.java" % (work_dir, plugin_name), "w") as f:
97         f.write(_EVENT_REGISTRY_IMPL_TEMPLATE.substitute(
98             plugin_package=plugin_package,
99             plugin_name=plugin_name,
100             json_filename=model.json_api_files,
101             register_callback_methods="".join(register_callback_methods),
102             handler_methods="".join(handler_methods)
103         ))
104
105 _REGISTER_CALLBACK_IMPL_TEMPLATE = Template("""
106     public java.lang.AutoCloseable register$callback(final $plugin_package.callback.$callback callback){
107         if(null != registeredCallbacks.putIfAbsent($plugin_package.dto.$notification.class, callback)){
108             throw new IllegalArgumentException("Callback for " + $plugin_package.dto.$notification.class +
109                 "notification already registered");
110         }
111         return () -> registeredCallbacks.remove($plugin_package.dto.$notification.class);
112     }
113 """)
114
115 _HANDLER_IMPL_TEMPLATE = Template("""
116     @Override
117     public void on$notification(
118         final $plugin_package.dto.$notification notification) {
119         if (LOG.isLoggable(java.util.logging.Level.FINE)) {
120             LOG.fine(java.lang.String.format("Received $notification event message: %s", notification));
121         }
122         final io.fd.vpp.jvpp.callback.JVppCallback jVppCallback = registeredCallbacks.get($plugin_package.dto.$notification.class);
123         if (null != jVppCallback) {
124             (($plugin_package.callback.$callback) registeredCallbacks
125                 .get($plugin_package.dto.$notification.class))
126                 .on$notification(notification);
127         }
128     }
129 """)
130
131 _EVENT_REGISTRY_IMPL_TEMPLATE = Template("""
132 package $plugin_package.notification;
133
134 /**
135  * <p>Notification registry delegating notification processing to registered callbacks.
136  * <br>It was generated by notification_gen.py based on $json_filename.
137  */
138 public final class ${plugin_name}EventRegistryImpl implements ${plugin_name}EventRegistry, Global${plugin_name}EventCallback {
139
140     // TODO add a special NotificationCallback interface and only allow those to be registered
141     private final java.util.concurrent.ConcurrentMap<Class<?>, io.fd.vpp.jvpp.callback.JVppCallback> registeredCallbacks =
142         new java.util.concurrent.ConcurrentHashMap<>();
143     private static java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
144
145     $register_callback_methods
146     $handler_methods
147
148     @Override
149     public void close() {
150         registeredCallbacks.clear();
151     }
152
153     @Override
154     public void onError(io.fd.vpp.jvpp.VppCallbackException ex) {
155         java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName());
156         LOG.log(java.util.logging.Level.WARNING, java.lang.String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
157             ex.getCtxId(), ex.getErrorCode()), ex);
158     }
159 }
160 """)
161
162
163 def _generate_global_event_callback(work_dir, model, messages):
164     plugin_name = model.plugin_java_name
165     plugin_package = model.plugin_package
166
167     callbacks = ""
168     callback_list = []
169     for msg in messages:
170         fqn_name = _fqn_callback_name(plugin_package, _callback_name(msg))
171         callback_list.append(fqn_name)
172
173     if callback_list:
174         callbacks = " extends %s" % ", ".join(callback_list)
175
176     with open("%s/Global%sEventCallback.java" % (work_dir, plugin_name), "w") as f:
177         f.write(_GLOBAL_EVENT_CALLBACK_TEMPLATE.substitute(
178             plugin_package=plugin_package,
179             plugin_name=plugin_name,
180             json_filename=model.json_api_files,
181             callbacks=callbacks
182         ))
183
184 _GLOBAL_EVENT_CALLBACK_TEMPLATE = Template("""
185 package $plugin_package.notification;
186
187 /**
188  * <p>Aggregated callback interface for notifications only.
189  * <br>It was generated by notification_gen.py based on $json_filename.
190  */
191 public interface Global${plugin_name}EventCallback$callbacks {
192
193 }
194 """)
195
196
197 def _generate_event_registry_provider(work_dir, model):
198     plugin_name = model.plugin_java_name
199     with open("%s/%sEventRegistryProvider.java" % (work_dir, plugin_name), "w") as f:
200         f.write(_EVENT_REGISTRY_PROVIDER_TEMPLATE.substitute(
201             plugin_package=model.plugin_package,
202             plugin_name=plugin_name,
203             json_filename=model.json_api_files
204         ))
205
206 _EVENT_REGISTRY_PROVIDER_TEMPLATE = Template("""
207 package $plugin_package.notification;
208
209  /**
210  * Provides ${plugin_name}EventRegistry.
211  * <br>The file was generated by notification_gen.py based on $json_filename.
212  */
213 public interface ${plugin_name}EventRegistryProvider extends io.fd.vpp.jvpp.notification.EventRegistryProvider {
214
215     @Override
216     public ${plugin_name}EventRegistry getEventRegistry();
217 }
218 """)
219
220
221 def _callback_name(msg):
222     return "%sCallback" % msg.java_name_upper
223
224
225 def _fqn_callback_name(plugin_package, callback_name):
226     return "%s.callback.%s" % (plugin_package, callback_name)