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}EventCallback notificationCallback;
34 private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(FutureJVpp${plugin_name}FacadeCallback.class.getName());
36 public FutureJVpp${plugin_name}FacadeCallback(
37 final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requestMap,
38 final $plugin_package.$notification_package.Global${plugin_name}EventCallback notificationCallback) {
39 this.requests = requestMap;
40 this.notificationCallback = notificationCallback;
44 @SuppressWarnings("unchecked")
45 public void onError($base_package.VppCallbackException reply) {
46 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
48 synchronized(requests) {
49 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.getCtxId());
52 if(completableFuture != null) {
53 completableFuture.completeExceptionally(reply);
55 synchronized(requests) {
56 requests.remove(reply.getCtxId());
62 @SuppressWarnings("unchecked")
63 public void onControlPingReply(final $base_package.$dto_package.ControlPingReply reply) {
64 java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
66 final int replyId = reply.context;
67 synchronized(requests) {
68 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(replyId);
70 if(completableFuture != null) {
72 if (completableFuture instanceof $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) {
73 completableFuture.complete((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getReplyDump());
74 // Remove future mapped to dump call context id
75 requests.remove((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getContextId());
77 // reply to regular control ping, complete the future
78 completableFuture.complete(reply);
80 requests.remove(replyId);
82 // future not yet created by writer, create new future, complete it and put to map under ping id
83 completableFuture = new java.util.concurrent.CompletableFuture<>();
84 completableFuture.complete(reply);
85 requests.put(replyId, completableFuture);
94 jvpp_facade_callback_method_template = Template("""
96 @SuppressWarnings("unchecked")
97 public void on$callback_dto(final $plugin_package.$dto_package.$callback_dto reply) {
98 java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<$plugin_package.$dto_package.$request_dto>> completableFuture;
99 final int replyId = reply.context;
100 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
101 LOG.fine(String.format("Received $callback_dto event message: %s", reply));
103 synchronized(requests) {
105 (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<$plugin_package.$dto_package.$request_dto>>) requests.get(replyId);
107 if(completableFuture != null) {
108 // received reply on request, complete future created by sender and remove it from map
109 completableFuture.complete(reply);
110 requests.remove(replyId);
112 // reply received before writer created future,
113 // create new future, complete it and put into map to
114 // notify sender that reply is already received
115 completableFuture = new java.util.concurrent.CompletableFuture<>();
116 completableFuture.complete(reply);
117 requests.put(replyId, completableFuture);
123 jvpp_facade_callback_notification_method_template = Template("""
125 public void on$callback_dto($plugin_package.$dto_package.$callback_dto notification) {
126 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
127 LOG.fine(String.format("Received $callback_dto event message: %s", notification));
129 notificationCallback.on$callback_dto(notification);
133 jvpp_facade_details_callback_method_template = Template("""
135 @SuppressWarnings("unchecked")
136 public void on$callback_dto(final $plugin_package.$dto_package.$callback_dto reply) {
137 $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump> completableFuture;
138 final int replyId = reply.context;
139 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
140 LOG.fine(String.format("Received $callback_dto event message: %s", reply));
142 synchronized(requests) {
143 completableFuture = ($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump>) requests.get(replyId);
145 if(completableFuture == null) {
146 // reply received before writer created future,
147 // create new future, and put into map to notify sender that reply is already received,
148 // following details replies will add information to this future
149 completableFuture = new $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<>(replyId,
150 new $plugin_package.$dto_package.$callback_dto_reply_dump());
151 requests.put(replyId, completableFuture);
153 completableFuture.getReplyDump().$callback_dto_field.add(reply);
159 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile):
160 """ Generates JVpp interface and JNI implementation """
161 print "Generating JVpp future facade"
163 if not os.path.exists(future_facade_package):
164 os.mkdir(future_facade_package)
169 for func in func_list:
170 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
172 if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
175 if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
178 camel_case_method_name = util.underscore_to_camelcase(func['name'])
180 if not util.is_notification(func["name"]):
181 camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
182 request_dto = util.remove_reply_suffix(util.underscore_to_camelcase_upper(func['name']))
183 if util.is_details(camel_case_name_with_suffix):
184 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
186 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
187 plugin_package=plugin_package,
188 dto_package=dto_package,
189 callback_dto=camel_case_name_with_suffix,
190 callback_dto_field=camel_case_method_name,
191 callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
192 future_package=future_facade_package))
194 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
195 dto_package=dto_package,
196 method_name=camel_case_request_method_name +
197 util.underscore_to_camelcase_upper(util.dump_suffix),
198 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
199 request_name=util.remove_reply_suffix(camel_case_reply_name) +
200 util.underscore_to_camelcase_upper(util.dump_suffix)))
201 methods_impl.append(future_jvpp_dump_method_impl_template.substitute(plugin_package=plugin_package,
202 dto_package=dto_package,
203 method_name=camel_case_request_method_name +
204 util.underscore_to_camelcase_upper(util.dump_suffix),
205 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
206 request_name=util.remove_reply_suffix(camel_case_reply_name) +
207 util.underscore_to_camelcase_upper(util.dump_suffix)))
209 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
210 if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
212 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
213 dto_package=dto_package,
214 method_name=camel_case_request_method_name,
215 reply_name=camel_case_name_with_suffix,
216 request_name=request_name))
217 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
218 dto_package=dto_package,
219 method_name=camel_case_request_method_name,
220 reply_name=camel_case_name_with_suffix,
221 request_name=request_name))
223 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
224 plugin_package=plugin_package,
225 dto_package=dto_package,
226 callback_dto=camel_case_name_with_suffix,
227 request_dto=request_dto))
229 if util.is_notification(func["name"]):
230 callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
231 dto_package=dto_package,
232 callback_dto=camel_case_name_with_suffix))
234 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w')
235 jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
236 base_package=base_package,
237 plugin_package=plugin_package,
238 plugin_name=plugin_name,
239 dto_package=dto_package,
240 notification_package=notification_package,
241 callback_package=callback_package,
242 methods="".join(callbacks),
243 future_package=future_facade_package))
247 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w')
248 jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
249 base_package=base_package,
250 plugin_package=plugin_package,
251 plugin_name=plugin_name,
252 notification_package=notification_package,
253 methods="".join(methods),
254 future_package=future_facade_package))
258 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w')
259 jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
260 base_package=base_package,
261 plugin_package=plugin_package,
262 plugin_name=plugin_name,
263 dto_package=dto_package,
264 notification_package=notification_package,
265 methods="".join(methods_impl),
266 future_package=future_facade_package))
271 future_jvpp_template = Template('''
272 package $plugin_package.$future_package;
275 * <p>Async facade extension adding specific methods for each request invocation
276 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
277 * <br>(python representation of api file generated by vppapigen).
279 public interface FutureJVpp${plugin_name} extends $base_package.$future_package.FutureJVppInvoker {
283 public $plugin_package.$notification_package.${plugin_name}EventRegistry getEventRegistry();
288 future_jvpp_method_template = Template('''
289 java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request);
293 future_jvpp_facade_template = Template('''
294 package $plugin_package.$future_package;
297 * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
298 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
299 * <br>(python representation of api file generated by vppapigen).
301 public class FutureJVpp${plugin_name}Facade extends $base_package.$future_package.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
303 private final $plugin_package.$notification_package.${plugin_name}EventRegistryImpl eventRegistry = new $plugin_package.$notification_package.${plugin_name}EventRegistryImpl();
306 * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
307 * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
308 * and then connects to provided JVpp instance
310 * @param jvpp provided $base_package.JVpp instance
312 * @throws java.io.IOException in case instance cannot connect to JVPP
314 public FutureJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $base_package.JVpp jvpp) throws java.io.IOException {
315 super(jvpp, registry, new java.util.HashMap<>());
316 java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
317 registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), eventRegistry));
321 public $plugin_package.$notification_package.${plugin_name}EventRegistry getEventRegistry() {
322 return eventRegistry;
329 future_jvpp_method_impl_template = Template('''
331 public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
332 return send(request);
336 future_jvpp_dump_method_impl_template = Template('''
338 public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
339 return send(request, new $plugin_package.$dto_package.$reply_name());
344 # Returns request name or special one from unconventional_naming_rep_req map
345 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
346 # FIXME this is a hotfix for sub-details callbacks
347 # FIXME also for L2FibTableEntry
348 # It's all because unclear mapping between
350 # dump -> reply, details,
351 # notification_start -> reply, notifications
353 # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
354 suffix = func_name.split("_")[-1]
355 return util.underscore_to_camelcase_upper(
356 util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
357 else camel_case_dto_name