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, callback_package, dto_package, inputfile):
127 """ Generates notification registry interface and implementation """
128 print "Generating Notification interfaces and implementation"
130 if not os.path.exists(notification_package):
131 os.mkdir(notification_package)
134 register_callback_methods = []
135 register_callback_methods_impl = []
137 for func in func_list:
138 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
140 if util.is_control_ping(camel_case_name_with_suffix):
141 # Skip control ping managed by jvpp registry.
143 if util.is_dump(func['name']) or util.is_request(func['name'], func_list):
146 # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
147 notification_dto = camel_case_name_with_suffix
148 callback_ifc = camel_case_name_with_suffix + callback_gen.callback_suffix
149 fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
150 callbacks.append(fully_qualified_callback_ifc)
152 # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
153 # that the registration should be closed
154 register_callback_methods.append("java.lang.AutoCloseable register{0}({1} callback);"
155 .format(callback_ifc, fully_qualified_callback_ifc))
156 register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package,
157 callback_package=callback_package,
158 dto_package=dto_package,
159 notification=camel_case_name_with_suffix,
160 callback=callback_ifc))
161 handler_methods.append(handler_impl_template.substitute(base_package=base_package,
162 plugin_package=plugin_package,
163 callback_package=callback_package,
164 dto_package=dto_package,
165 notification=notification_dto,
166 notification_reply=camel_case_name_with_suffix,
167 callback=callback_ifc))
169 callback_file = open(os.path.join(notification_package, "%sEventRegistry.java" % plugin_name), 'w')
170 callback_file.write(notification_registry_template.substitute(inputfile=inputfile,
171 register_callback_methods="\n ".join(register_callback_methods),
172 base_package=base_package,
173 plugin_package=plugin_package,
174 plugin_name=plugin_name,
175 notification_package=notification_package))
176 callback_file.flush()
177 callback_file.close()
179 callback_file = open(os.path.join(notification_package, "Global%sEventCallback.java" % plugin_name), 'w')
181 global_notification_callback_callbacks = ""
183 global_notification_callback_callbacks = " extends " + ", ".join(callbacks)
185 callback_file.write(global_notification_callback_template.substitute(inputfile=inputfile,
186 callbacks=global_notification_callback_callbacks,
187 plugin_package=plugin_package,
188 plugin_name=plugin_name,
189 notification_package=notification_package))
190 callback_file.flush()
191 callback_file.close()
193 callback_file = open(os.path.join(notification_package, "%sEventRegistryImpl.java" % plugin_name), 'w')
194 callback_file.write(notification_registry_impl_template.substitute(inputfile=inputfile,
195 callback_package=callback_package,
196 dto_package=dto_package,
197 register_callback_methods="".join(register_callback_methods_impl),
198 handler_methods="".join(handler_methods),
199 base_package=base_package,
200 plugin_package=plugin_package,
201 plugin_name=plugin_name,
202 notification_package=notification_package))
203 callback_file.flush()
204 callback_file.close()
206 callback_file = open(os.path.join(notification_package, "%sEventRegistryProvider.java" % plugin_name), 'w')
207 callback_file.write(notification_provider_template.substitute(inputfile=inputfile,
208 base_package=base_package,
209 plugin_package=plugin_package,
210 plugin_name=plugin_name,
211 notification_package=notification_package))
212 callback_file.flush()
213 callback_file.close()