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.
17 from string import Template
22 jvpp_facade_callback_template = Template("""
23 package $plugin_package.$future_package;
26 * <p>Async facade callback setting values to future objects
27 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
28 * <br>(python representation of api file generated by vppapigen).
30 public final class FutureJVpp${plugin_name}FacadeCallback implements $plugin_package.$callback_package.JVpp${plugin_name}GlobalCallback {
32 private final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requests;
33 private final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback;
35 public FutureJVpp${plugin_name}FacadeCallback(
36 final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requestMap,
37 final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback) {
38 this.requests = requestMap;
39 this.notificationCallback = notificationCallback;
43 @SuppressWarnings("unchecked")
44 public void onError($base_package.VppCallbackException reply) {
45 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
47 synchronized(requests) {
48 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.getCtxId());
51 if(completableFuture != null) {
52 completableFuture.completeExceptionally(reply);
54 synchronized(requests) {
55 requests.remove(reply.getCtxId());
61 @SuppressWarnings("unchecked")
62 public void onControlPingReply($base_package.$dto_package.ControlPingReply reply) {
63 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
65 synchronized(requests) {
66 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context);
69 if(completableFuture != null) {
71 if (completableFuture instanceof $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) {
72 completableFuture.complete((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getReplyDump());
73 // Remove future mapped to dump call context id
74 synchronized(requests) {
75 requests.remove((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getContextId());
78 completableFuture.complete(reply);
81 synchronized(requests) {
82 requests.remove(reply.context);
91 jvpp_facade_callback_method_template = Template("""
93 @SuppressWarnings("unchecked")
94 public void on$callback_dto($plugin_package.$dto_package.$callback_dto reply) {
95 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
97 synchronized(requests) {
98 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context);
101 if(completableFuture != null) {
102 completableFuture.complete(reply);
104 synchronized(requests) {
105 requests.remove(reply.context);
111 jvpp_facade_callback_notification_method_template = Template("""
113 public void on$callback_dto($plugin_package.$dto_package.$callback_dto notification) {
114 notificationCallback.on$callback_dto(notification);
118 jvpp_facade_details_callback_method_template = Template("""
120 @SuppressWarnings("unchecked")
121 public void on$callback_dto($plugin_package.$dto_package.$callback_dto reply) {
122 final $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump> completableFuture;
124 synchronized(requests) {
125 completableFuture = ($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump>) requests.get(reply.context);
128 if(completableFuture != null) {
129 $plugin_package.$dto_package.$callback_dto_reply_dump replyDump = completableFuture.getReplyDump();
130 if(replyDump == null) {
131 replyDump = new $plugin_package.$dto_package.$callback_dto_reply_dump();
132 completableFuture.setReplyDump(replyDump);
135 replyDump.$callback_dto_field.add(reply);
141 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile):
142 """ Generates JVpp interface and JNI implementation """
143 print "Generating JVpp future facade"
145 if not os.path.exists(future_facade_package):
146 raise Exception("%s folder is missing" % future_facade_package)
151 for func in func_list:
152 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
154 if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
157 if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
160 camel_case_method_name = util.underscore_to_camelcase(func['name'])
162 if not util.is_notification(func["name"]):
163 camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
164 if util.is_details(camel_case_name_with_suffix):
165 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
167 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
168 plugin_package=plugin_package,
169 dto_package=dto_package,
170 callback_dto=camel_case_name_with_suffix,
171 callback_dto_field=camel_case_method_name,
172 callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
173 future_package=future_facade_package))
175 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
176 dto_package=dto_package,
177 method_name=camel_case_request_method_name +
178 util.underscore_to_camelcase_upper(util.dump_suffix),
179 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
180 request_name=util.remove_reply_suffix(camel_case_reply_name) +
181 util.underscore_to_camelcase_upper(util.dump_suffix)))
182 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
183 dto_package=dto_package,
184 method_name=camel_case_request_method_name +
185 util.underscore_to_camelcase_upper(util.dump_suffix),
186 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
187 request_name=util.remove_reply_suffix(camel_case_reply_name) +
188 util.underscore_to_camelcase_upper(util.dump_suffix)))
190 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
191 if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
193 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
194 dto_package=dto_package,
195 method_name=camel_case_request_method_name,
196 reply_name=camel_case_name_with_suffix,
197 request_name=request_name))
198 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
199 dto_package=dto_package,
200 method_name=camel_case_request_method_name,
201 reply_name=camel_case_name_with_suffix,
202 request_name=request_name))
204 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
205 plugin_package=plugin_package,
206 dto_package=dto_package,
207 callback_dto=camel_case_name_with_suffix))
209 if util.is_notification(func["name"]):
210 callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
211 dto_package=dto_package,
212 callback_dto=util.add_notification_suffix(camel_case_name_with_suffix)))
214 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w')
215 jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
216 base_package=base_package,
217 plugin_package=plugin_package,
218 plugin_name=plugin_name,
219 dto_package=dto_package,
220 notification_package=notification_package,
221 callback_package=callback_package,
222 methods="".join(callbacks),
223 future_package=future_facade_package))
227 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w')
228 jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
229 base_package=base_package,
230 plugin_package=plugin_package,
231 plugin_name=plugin_name,
232 notification_package=notification_package,
233 methods="".join(methods),
234 future_package=future_facade_package))
238 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w')
239 jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
240 base_package=base_package,
241 plugin_package=plugin_package,
242 plugin_name=plugin_name,
243 dto_package=dto_package,
244 notification_package=notification_package,
245 methods="".join(methods_impl),
246 future_package=future_facade_package))
251 future_jvpp_template = Template('''
252 package $plugin_package.$future_package;
255 * <p>Async facade extension adding specific methods for each request invocation
256 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
257 * <br>(python representation of api file generated by vppapigen).
259 public interface FutureJVpp${plugin_name} extends $base_package.$future_package.FutureJVppInvoker {
263 public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry();
268 future_jvpp_method_template = Template('''
269 java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request);
273 future_jvpp_facade_template = Template('''
274 package $plugin_package.$future_package;
277 * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
278 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
279 * <br>(python representation of api file generated by vppapigen).
281 public class FutureJVpp${plugin_name}Facade extends $base_package.$future_package.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
283 private final $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl notificationRegistry = new $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl();
286 * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
287 * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
288 * and then connects to provided JVpp instance
290 * @param jvpp provided $base_package.JVpp instance
292 * @throws java.io.IOException in case instance cannot connect to JVPP
294 public FutureJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $base_package.JVpp jvpp) throws java.io.IOException {
295 super(jvpp, registry, new java.util.HashMap<>());
296 java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
297 registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), notificationRegistry));
301 public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry() {
302 return notificationRegistry;
309 future_jvpp_method_impl_template = Template('''
311 public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
312 return send(request);
316 # Returns request name or special one from unconventional_naming_rep_req map
317 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
318 # FIXME this is a hotfix for sub-details callbacks
319 # FIXME also for L2FibTableEntry
320 # It's all because unclear mapping between
322 # dump -> reply, details,
323 # notification_start -> reply, notifications
325 # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
326 suffix = func_name.split("_")[-1]
327 return util.underscore_to_camelcase_upper(
328 util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
329 else camel_case_dto_name