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}NotificationRegistry extends $base_package.$notification_package.NotificationRegistry {
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}NotificationCallback$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}NotificationRegistryImpl implements ${plugin_name}NotificationRegistry, Global${plugin_name}NotificationCallback {
62 // TODO add a special NotificationCallback interface and only allow those to be registered
63 private final java.util.concurrent.ConcurrentMap<Class<? extends $base_package.$dto_package.JVppNotification>, $base_package.$callback_package.JVppNotificationCallback> registeredCallbacks =
64 new java.util.concurrent.ConcurrentHashMap<>();
66 $register_callback_methods
71 registeredCallbacks.clear();
76 register_callback_impl_template = Template("""
77 public java.lang.AutoCloseable register$callback(final $plugin_package.$callback_package.$callback callback){
78 if(null != registeredCallbacks.putIfAbsent($plugin_package.$dto_package.$notification.class, callback)){
79 throw new IllegalArgumentException("Callback for " + $plugin_package.$dto_package.$notification.class +
80 "notification already registered");
82 return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
86 handler_impl_template = Template("""
88 public void on$notification(
89 final $plugin_package.$dto_package.$notification notification) {
90 final $base_package.$callback_package.JVppNotificationCallback jVppNotificationCallback = registeredCallbacks.get($plugin_package.$dto_package.$notification.class);
91 if (null != jVppNotificationCallback) {
92 (($plugin_package.$callback_package.$callback) registeredCallbacks
93 .get($plugin_package.$dto_package.$notification.class))
94 .on$notification(notification);
99 notification_provider_template = Template("""
100 package $plugin_package.$notification_package;
103 * Provides ${plugin_name}NotificationRegistry.
104 * <br>The file was generated by notification_gen.py based on $inputfile
105 * <br>(python representation of api file generated by vppapigen).
107 public interface ${plugin_name}NotificationRegistryProvider extends $base_package.$notification_package.NotificationRegistryProvider {
110 public ${plugin_name}NotificationRegistry getNotificationRegistry();
115 def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package, callback_package, dto_package, inputfile):
116 """ Generates notification registry interface and implementation """
117 print "Generating Notification interfaces and implementation"
119 if not os.path.exists(notification_package):
120 os.mkdir(notification_package)
123 register_callback_methods = []
124 register_callback_methods_impl = []
126 for func in func_list:
128 if not util.is_notification(func['name']):
131 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
132 notification_dto = util.add_notification_suffix(camel_case_name_with_suffix)
133 callback_ifc = notification_dto + callback_gen.callback_suffix
134 fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
135 callbacks.append(fully_qualified_callback_ifc)
137 # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
138 # that the registration should be closed
139 register_callback_methods.append("java.lang.AutoCloseable register{0}({1} callback);"
140 .format(callback_ifc, fully_qualified_callback_ifc))
141 register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package,
142 callback_package=callback_package,
143 dto_package=dto_package,
144 notification=notification_dto,
145 callback=callback_ifc))
146 handler_methods.append(handler_impl_template.substitute(base_package=base_package,
147 plugin_package=plugin_package,
148 callback_package=callback_package,
149 dto_package=dto_package,
150 notification=notification_dto,
151 callback=callback_ifc))
154 callback_file = open(os.path.join(notification_package, "%sNotificationRegistry.java" % plugin_name), 'w')
155 callback_file.write(notification_registry_template.substitute(inputfile=inputfile,
156 register_callback_methods="\n ".join(register_callback_methods),
157 base_package=base_package,
158 plugin_package=plugin_package,
159 plugin_name=plugin_name,
160 notification_package=notification_package))
161 callback_file.flush()
162 callback_file.close()
164 callback_file = open(os.path.join(notification_package, "Global%sNotificationCallback.java" % plugin_name), 'w')
166 global_notification_callback_callbacks = ""
168 global_notification_callback_callbacks = " extends " + ", ".join(callbacks)
170 callback_file.write(global_notification_callback_template.substitute(inputfile=inputfile,
171 callbacks=global_notification_callback_callbacks,
172 plugin_package=plugin_package,
173 plugin_name=plugin_name,
174 notification_package=notification_package))
175 callback_file.flush()
176 callback_file.close()
178 callback_file = open(os.path.join(notification_package, "%sNotificationRegistryImpl.java" % plugin_name), 'w')
179 callback_file.write(notification_registry_impl_template.substitute(inputfile=inputfile,
180 callback_package=callback_package,
181 dto_package=dto_package,
182 register_callback_methods="".join(register_callback_methods_impl),
183 handler_methods="".join(handler_methods),
184 base_package=base_package,
185 plugin_package=plugin_package,
186 plugin_name=plugin_name,
187 notification_package=notification_package))
188 callback_file.flush()
189 callback_file.close()
191 callback_file = open(os.path.join(notification_package, "%sNotificationRegistryProvider.java" % plugin_name), 'w')
192 callback_file.write(notification_provider_template.substitute(inputfile=inputfile,
193 base_package=base_package,
194 plugin_package=plugin_package,
195 plugin_name=plugin_name,
196 notification_package=notification_package))
197 callback_file.flush()
198 callback_file.close()