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 completableFuture.getReplyDump().$callback_dto_field.add(reply);
135 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile):
136 """ Generates JVpp interface and JNI implementation """
137 print "Generating JVpp future facade"
139 if not os.path.exists(future_facade_package):
140 os.mkdir(future_facade_package)
145 for func in func_list:
146 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
148 if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
151 if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
154 camel_case_method_name = util.underscore_to_camelcase(func['name'])
156 if not util.is_notification(func["name"]):
157 camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
158 if util.is_details(camel_case_name_with_suffix):
159 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
161 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
162 plugin_package=plugin_package,
163 dto_package=dto_package,
164 callback_dto=camel_case_name_with_suffix,
165 callback_dto_field=camel_case_method_name,
166 callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
167 future_package=future_facade_package))
169 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
170 dto_package=dto_package,
171 method_name=camel_case_request_method_name +
172 util.underscore_to_camelcase_upper(util.dump_suffix),
173 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
174 request_name=util.remove_reply_suffix(camel_case_reply_name) +
175 util.underscore_to_camelcase_upper(util.dump_suffix)))
176 methods_impl.append(future_jvpp_dump_method_impl_template.substitute(plugin_package=plugin_package,
177 dto_package=dto_package,
178 method_name=camel_case_request_method_name +
179 util.underscore_to_camelcase_upper(util.dump_suffix),
180 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
181 request_name=util.remove_reply_suffix(camel_case_reply_name) +
182 util.underscore_to_camelcase_upper(util.dump_suffix)))
184 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
185 if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
187 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
188 dto_package=dto_package,
189 method_name=camel_case_request_method_name,
190 reply_name=camel_case_name_with_suffix,
191 request_name=request_name))
192 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
193 dto_package=dto_package,
194 method_name=camel_case_request_method_name,
195 reply_name=camel_case_name_with_suffix,
196 request_name=request_name))
198 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
199 plugin_package=plugin_package,
200 dto_package=dto_package,
201 callback_dto=camel_case_name_with_suffix))
203 if util.is_notification(func["name"]):
204 callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
205 dto_package=dto_package,
206 callback_dto=util.add_notification_suffix(camel_case_name_with_suffix)))
208 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w')
209 jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
210 base_package=base_package,
211 plugin_package=plugin_package,
212 plugin_name=plugin_name,
213 dto_package=dto_package,
214 notification_package=notification_package,
215 callback_package=callback_package,
216 methods="".join(callbacks),
217 future_package=future_facade_package))
221 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w')
222 jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
223 base_package=base_package,
224 plugin_package=plugin_package,
225 plugin_name=plugin_name,
226 notification_package=notification_package,
227 methods="".join(methods),
228 future_package=future_facade_package))
232 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w')
233 jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
234 base_package=base_package,
235 plugin_package=plugin_package,
236 plugin_name=plugin_name,
237 dto_package=dto_package,
238 notification_package=notification_package,
239 methods="".join(methods_impl),
240 future_package=future_facade_package))
245 future_jvpp_template = Template('''
246 package $plugin_package.$future_package;
249 * <p>Async facade extension adding specific methods for each request invocation
250 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
251 * <br>(python representation of api file generated by vppapigen).
253 public interface FutureJVpp${plugin_name} extends $base_package.$future_package.FutureJVppInvoker {
257 public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry();
262 future_jvpp_method_template = Template('''
263 java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request);
267 future_jvpp_facade_template = Template('''
268 package $plugin_package.$future_package;
271 * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
272 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
273 * <br>(python representation of api file generated by vppapigen).
275 public class FutureJVpp${plugin_name}Facade extends $base_package.$future_package.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
277 private final $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl notificationRegistry = new $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl();
280 * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
281 * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
282 * and then connects to provided JVpp instance
284 * @param jvpp provided $base_package.JVpp instance
286 * @throws java.io.IOException in case instance cannot connect to JVPP
288 public FutureJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $base_package.JVpp jvpp) throws java.io.IOException {
289 super(jvpp, registry, new java.util.HashMap<>());
290 java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
291 registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), notificationRegistry));
295 public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry() {
296 return notificationRegistry;
303 future_jvpp_method_impl_template = Template('''
305 public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
306 return send(request);
310 future_jvpp_dump_method_impl_template = Template('''
312 public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
313 return send(request, new $plugin_package.$dto_package.$reply_name());
318 # Returns request name or special one from unconventional_naming_rep_req map
319 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
320 # FIXME this is a hotfix for sub-details callbacks
321 # FIXME also for L2FibTableEntry
322 # It's all because unclear mapping between
324 # dump -> reply, details,
325 # notification_start -> reply, notifications
327 # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
328 suffix = func_name.split("_")[-1]
329 return util.underscore_to_camelcase_upper(
330 util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
331 else camel_case_dto_name