blob: eb380fc8ea31b14378e5de9660ddeaac4780a8b9 [file] [log] [blame]
Maros Marsalek7becd082016-05-31 17:45:16 +02001#!/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
16import os
17
18import callback_gen
19import util
20from string import Template
21
Maros Marsalek7becd082016-05-31 17:45:16 +020022notification_registry_template = Template("""
Marek Gradzki66ea26b2016-07-26 15:28:22 +020023package $plugin_package.$notification_package;
Maros Marsalek7becd082016-05-31 17:45:16 +020024
25/**
Marek Gradzki66ea26b2016-07-26 15:28:22 +020026 * <p>Registry for notification callbacks defined in ${plugin_name}.
Maros Marsalek7becd082016-05-31 17:45:16 +020027 * <br>It was generated by notification_gen.py based on $inputfile
Marek Gradzki66ea26b2016-07-26 15:28:22 +020028 * <br>(python representation of api file generated by vppapigen).
Maros Marsalek7becd082016-05-31 17:45:16 +020029 */
Marek Gradzki66ea26b2016-07-26 15:28:22 +020030public interface ${plugin_name}NotificationRegistry extends $base_package.$notification_package.NotificationRegistry {
Maros Marsalek7becd082016-05-31 17:45:16 +020031
32 $register_callback_methods
33
34 @Override
35 void close();
36}
37""")
38
39global_notification_callback_template = Template("""
Marek Gradzki66ea26b2016-07-26 15:28:22 +020040package $plugin_package.$notification_package;
Maros Marsalek7becd082016-05-31 17:45:16 +020041
42/**
43 * <p>Aggregated callback interface for notifications only.
44 * <br>It was generated by notification_gen.py based on $inputfile
Marek Gradzki66ea26b2016-07-26 15:28:22 +020045 * <br>(python representation of api file generated by vppapigen).
Maros Marsalek7becd082016-05-31 17:45:16 +020046 */
Marek Gradzki66ea26b2016-07-26 15:28:22 +020047public interface Global${plugin_name}NotificationCallback$callbacks {
Maros Marsalek7becd082016-05-31 17:45:16 +020048
49}
50""")
51
52notification_registry_impl_template = Template("""
Marek Gradzki66ea26b2016-07-26 15:28:22 +020053package $plugin_package.$notification_package;
Maros Marsalek7becd082016-05-31 17:45:16 +020054
55/**
56 * <p>Notification registry delegating notification processing to registered callbacks.
57 * <br>It was generated by notification_gen.py based on $inputfile
Marek Gradzki66ea26b2016-07-26 15:28:22 +020058 * <br>(python representation of api file generated by vppapigen).
Maros Marsalek7becd082016-05-31 17:45:16 +020059 */
Marek Gradzki66ea26b2016-07-26 15:28:22 +020060public final class ${plugin_name}NotificationRegistryImpl implements ${plugin_name}NotificationRegistry, Global${plugin_name}NotificationCallback {
Maros Marsalek7becd082016-05-31 17:45:16 +020061
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
76register_callback_impl_template = Template("""
Marek Gradzki66ea26b2016-07-26 15:28:22 +020077 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 Marsalek7becd082016-05-31 17:45:16 +020080 "notification already registered");
81 }
Marek Gradzki66ea26b2016-07-26 15:28:22 +020082 return () -> registeredCallbacks.remove($plugin_package.$dto_package.$notification.class);
Maros Marsalek7becd082016-05-31 17:45:16 +020083 }
84""")
85
86handler_impl_template = Template("""
87 @Override
88 public void on$notification(
Marek Gradzki66ea26b2016-07-26 15:28:22 +020089 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 Marsalek7becd082016-05-31 17:45:16 +020094 .on$notification(notification);
95 }
96 }
97""")
98
Marek Gradzki66ea26b2016-07-26 15:28:22 +020099notification_provider_template = Template("""
100package $plugin_package.$notification_package;
Maros Marsalek7becd082016-05-31 17:45:16 +0200101
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200102 /**
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 */
107public interface ${plugin_name}NotificationRegistryProvider extends $base_package.$notification_package.NotificationRegistryProvider {
108
109 @Override
110 public ${plugin_name}NotificationRegistry getNotificationRegistry();
111}
112""")
113
114
115def generate_notification_registry(func_list, base_package, plugin_package, plugin_name, notification_package, callback_package, dto_package, inputfile):
Maros Marsalek7becd082016-05-31 17:45:16 +0200116 """ 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 Gradzki66ea26b2016-07-26 15:28:22 +0200134 fully_qualified_callback_ifc = "{0}.{1}.{2}".format(plugin_package, callback_package, callback_ifc)
Maros Marsalek7becd082016-05-31 17:45:16 +0200135 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 Gradzki66ea26b2016-07-26 15:28:22 +0200141 register_callback_methods_impl.append(register_callback_impl_template.substitute(plugin_package=plugin_package,
Maros Marsalek7becd082016-05-31 17:45:16 +0200142 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 Gradzki66ea26b2016-07-26 15:28:22 +0200147 plugin_package=plugin_package,
Maros Marsalek7becd082016-05-31 17:45:16 +0200148 callback_package=callback_package,
149 dto_package=dto_package,
150 notification=notification_dto,
151 callback=callback_ifc))
152
Maros Marsalek7becd082016-05-31 17:45:16 +0200153
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200154 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