Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 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: |
| 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 | |
| 16 | import os |
| 17 | |
| 18 | import callback_gen |
| 19 | import util |
| 20 | from string import Template |
| 21 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 22 | notification_registry_template = Template(""" |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 23 | package $plugin_package.$notification_package; |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 24 | |
| 25 | /** |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 26 | * <p>Registry for notification callbacks defined in ${plugin_name}. |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 27 | * <br>It was generated by notification_gen.py based on $inputfile |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 28 | * <br>(python representation of api file generated by vppapigen). |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 29 | */ |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 30 | public interface ${plugin_name}NotificationRegistry extends $base_package.$notification_package.NotificationRegistry { |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 31 | |
| 32 | $register_callback_methods |
| 33 | |
| 34 | @Override |
| 35 | void close(); |
| 36 | } |
| 37 | """) |
| 38 | |
| 39 | global_notification_callback_template = Template(""" |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 40 | package $plugin_package.$notification_package; |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * <p>Aggregated callback interface for notifications only. |
| 44 | * <br>It was generated by notification_gen.py based on $inputfile |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 45 | * <br>(python representation of api file generated by vppapigen). |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 46 | */ |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 47 | public interface Global${plugin_name}NotificationCallback$callbacks { |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 48 | |
| 49 | } |
| 50 | """) |
| 51 | |
| 52 | notification_registry_impl_template = Template(""" |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 53 | package $plugin_package.$notification_package; |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * <p>Notification registry delegating notification processing to registered callbacks. |
| 57 | * <br>It was generated by notification_gen.py based on $inputfile |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 58 | * <br>(python representation of api file generated by vppapigen). |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 59 | */ |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 60 | public final class ${plugin_name}NotificationRegistryImpl implements ${plugin_name}NotificationRegistry, Global${plugin_name}NotificationCallback { |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 61 | |
| 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<>(); |
| 65 | |
| 66 | $register_callback_methods |
| 67 | $handler_methods |
| 68 | |
| 69 | @Override |
| 70 | public void close() { |
| 71 | registeredCallbacks.clear(); |
| 72 | } |
| 73 | } |
| 74 | """) |
| 75 | |
| 76 | register_callback_impl_template = Template(""" |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 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 + |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 80 | "notification already registered"); |
| 81 | } |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 82 | return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class); |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 83 | } |
| 84 | """) |
| 85 | |
| 86 | handler_impl_template = Template(""" |
| 87 | @Override |
| 88 | public void on$notification( |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 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)) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 94 | .on$notification(notification); |
| 95 | } |
| 96 | } |
| 97 | """) |
| 98 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 99 | notification_provider_template = Template(""" |
| 100 | package $plugin_package.$notification_package; |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 101 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 102 | /** |
| 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). |
| 106 | */ |
| 107 | public interface ${plugin_name}NotificationRegistryProvider extends $base_package.$notification_package.NotificationRegistryProvider { |
| 108 | |
| 109 | @Override |
| 110 | public ${plugin_name}NotificationRegistry getNotificationRegistry(); |
| 111 | } |
| 112 | """) |
| 113 | |
| 114 | |
| 115 | def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package, callback_package, dto_package, inputfile): |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 116 | """ Generates notification registry interface and implementation """ |
| 117 | print "Generating Notification interfaces and implementation" |
| 118 | |
| 119 | if not os.path.exists(notification_package): |
| 120 | raise Exception("%s folder is missing" % notification_package) |
| 121 | |
| 122 | callbacks = [] |
| 123 | register_callback_methods = [] |
| 124 | register_callback_methods_impl = [] |
| 125 | handler_methods = [] |
| 126 | for func in func_list: |
| 127 | |
| 128 | if not util.is_notification(func['name']): |
| 129 | continue |
| 130 | |
| 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 |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 134 | fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc) |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 135 | callbacks.append(fully_qualified_callback_ifc) |
| 136 | |
| 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)) |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 141 | register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package, |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 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, |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 147 | plugin_package=plugin_package, |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 148 | callback_package=callback_package, |
| 149 | dto_package=dto_package, |
| 150 | notification=notification_dto, |
| 151 | callback=callback_ifc)) |
| 152 | |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 153 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 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() |
| 163 | |
| 164 | callback_file = open(os.path.join(notification_package, "Global%sNotificationCallback.java" % plugin_name), 'w') |
| 165 | |
| 166 | global_notification_callback_callbacks = "" |
| 167 | if (callbacks): |
| 168 | global_notification_callback_callbacks = " extends " + ", ".join(callbacks) |
| 169 | |
| 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() |
| 177 | |
| 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() |
| 190 | |
| 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() |
| 199 | |