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:
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.
20 from string import Template
22 notification_registry_template = Template("""
23 package $plugin_package.$notification_package;
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).
30 public interface ${plugin_name}EventRegistry extends $base_package.$notification_package.EventRegistry {
32 $register_callback_methods
39 global_notification_callback_template = Template("""
40 package $plugin_package.$notification_package;
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).
47 public interface Global${plugin_name}EventCallback$callbacks {
52 notification_registry_impl_template = Template("""
53 package $plugin_package.$notification_package;
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).
60 public final class ${plugin_name}EventRegistryImpl implements ${plugin_name}EventRegistry, Global${plugin_name}EventCallback {
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());
67 $register_callback_methods
72 registeredCallbacks.clear();
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);
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");
90 return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
94 handler_impl_template = Template("""
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));
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);
110 notification_provider_template = Template("""
111 package $plugin_package.$notification_package;
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).
118 public interface ${plugin_name}EventRegistryProvider extends $base_package.$notification_package.EventRegistryProvider {
121 public ${plugin_name}EventRegistry getEventRegistry();
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)
131 if not os.path.exists(notification_package):
132 os.mkdir(notification_package)
135 register_callback_methods = []
136 register_callback_methods_impl = []
138 for func in func_list:
139 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
141 if util.is_control_ping(camel_case_name_with_suffix):
142 # Skip control ping managed by jvpp registry.
144 if util.is_dump(func['name']) or util.is_request(func['name'], func_list):
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)
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))
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()
180 callback_file = open(os.path.join(notification_package, "Global%sEventCallback.java" % plugin_name), 'w')
182 global_notification_callback_callbacks = ""
184 global_notification_callback_callbacks = " extends " + ", ".join(callbacks)
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()
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()
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()