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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from jvpp_model import is_control_ping, is_control_ping_reply, is_dump, is_request
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)
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 \
39 def _generate_event_registry(work_dir, model, messages):
40 plugin_name = model.plugin_java_name
41 plugin_package = model.plugin_package
43 register_callback_methods = []
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))
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)
59 _EVENT_REGISTRY_TEMPLATE = Template("""
60 package $plugin_package.notification;
63 * <p>Registry for notification callbacks defined in ${plugin_name}.
64 * <br>It was generated by notification_gen.py based on $json_filename.
66 public interface ${plugin_name}EventRegistry extends io.fd.vpp.jvpp.notification.EventRegistry {
68 $register_callback_methods
76 def _generate_event_registry_impl(work_dir, model, messages):
77 plugin_name = model.plugin_java_name
78 plugin_package = model.plugin_package
80 register_callback_methods = []
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,
90 handler_methods.append(_HANDLER_IMPL_TEMPLATE.substitute(
91 plugin_package=plugin_package,
92 notification=notification,
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)
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");
111 return () -> registeredCallbacks.remove($plugin_package.dto.$notification.class);
115 _HANDLER_IMPL_TEMPLATE = Template("""
117 public void on$notification(
118 final $plugin_package.dto.$notification notification) {
119 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
120 LOG.fine(String.format("Received $notification event message: %s", notification));
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);
131 _EVENT_REGISTRY_IMPL_TEMPLATE = Template("""
132 package $plugin_package.notification;
135 * <p>Notification registry delegating notification processing to registered callbacks.
136 * <br>It was generated by notification_gen.py based on $json_filename.
138 public final class ${plugin_name}EventRegistryImpl implements ${plugin_name}EventRegistry, Global${plugin_name}EventCallback {
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());
145 $register_callback_methods
149 public void close() {
150 registeredCallbacks.clear();
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, String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
157 ex.getCtxId(), ex.getErrorCode()), ex);
163 def _generate_global_event_callback(work_dir, model, messages):
164 plugin_name = model.plugin_java_name
165 plugin_package = model.plugin_package
170 fqn_name = _fqn_callback_name(plugin_package, _callback_name(msg))
171 callback_list.append(fqn_name)
174 callbacks = " extends %s" % ", ".join(callback_list)
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,
184 _GLOBAL_EVENT_CALLBACK_TEMPLATE = Template("""
185 package $plugin_package.notification;
188 * <p>Aggregated callback interface for notifications only.
189 * <br>It was generated by notification_gen.py based on $json_filename.
191 public interface Global${plugin_name}EventCallback$callbacks {
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
206 _EVENT_REGISTRY_PROVIDER_TEMPLATE = Template("""
207 package $plugin_package.notification;
210 * Provides ${plugin_name}EventRegistry.
211 * <br>The file was generated by notification_gen.py based on $json_filename.
213 public interface ${plugin_name}EventRegistryProvider extends io.fd.vpp.jvpp.notification.EventRegistryProvider {
216 public ${plugin_name}EventRegistry getEventRegistry();
221 def _callback_name(msg):
222 return "%sCallback" % msg.java_name_upper
225 def _fqn_callback_name(plugin_package, callback_name):
226 return "%s.callback.%s" % (plugin_package, callback_name)