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 $base_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 vpe.api generated by vppapigen).
30 public final class FutureJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback {
32 private final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requests;
33 private final $base_package.$notification_package.GlobalNotificationCallback notificationCallback;
35 public FutureJVppFacadeCallback(final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requestMap,
36 final $base_package.$notification_package.GlobalNotificationCallback notificationCallback) {
37 this.requests = requestMap;
38 this.notificationCallback = notificationCallback;
42 @SuppressWarnings("unchecked")
43 public void onError(org.openvpp.jvpp.VppCallbackException reply) {
44 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
46 synchronized(requests) {
47 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.getCtxId());
50 if(completableFuture != null) {
51 completableFuture.completeExceptionally(reply);
53 synchronized(requests) {
54 requests.remove(reply.getCtxId());
63 jvpp_facade_callback_method_template = Template("""
65 @SuppressWarnings("unchecked")
66 public void on$callback_dto($base_package.$dto_package.$callback_dto reply) {
67 final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
69 synchronized(requests) {
70 completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context);
73 if(completableFuture != null) {
74 completableFuture.complete(reply);
76 synchronized(requests) {
77 requests.remove(reply.context);
83 jvpp_facade_callback_notification_method_template = Template("""
85 public void on$callback_dto($base_package.$dto_package.$callback_dto notification) {
86 notificationCallback.on$callback_dto(notification);
90 # TODO reuse common parts with generic method callback
91 jvpp_facade_control_ping_method_template = Template("""
93 @SuppressWarnings("unchecked")
94 public void on$callback_dto($base_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) {
103 if (completableFuture instanceof $base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) {
104 completableFuture.complete((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getReplyDump());
105 // Remove future mapped to dump call context id
106 synchronized(requests) {
107 requests.remove((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getContextId());
110 completableFuture.complete(reply);
113 synchronized(requests) {
114 requests.remove(reply.context);
120 jvpp_facade_details_callback_method_template = Template("""
122 @SuppressWarnings("unchecked")
123 public void on$callback_dto($base_package.$dto_package.$callback_dto reply) {
124 final FutureJVppFacade.CompletableDumpFuture<$base_package.$dto_package.$callback_dto_reply_dump> completableFuture;
126 synchronized(requests) {
127 completableFuture = ($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture<$base_package.$dto_package.$callback_dto_reply_dump>) requests.get(reply.context);
130 if(completableFuture != null) {
131 $base_package.$dto_package.$callback_dto_reply_dump replyDump = completableFuture.getReplyDump();
132 if(replyDump == null) {
133 replyDump = new $base_package.$dto_package.$callback_dto_reply_dump();
134 completableFuture.setReplyDump(replyDump);
137 replyDump.$callback_dto_field.add(reply);
143 def generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, future_facade_package, inputfile):
144 """ Generates JVpp interface and JNI implementation """
145 print "Generating JVpp future facade"
147 if not os.path.exists(future_facade_package):
148 raise Exception("%s folder is missing" % future_facade_package)
153 for func in func_list:
155 if util.is_ignored(func['name']):
158 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
159 if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
162 camel_case_method_name = util.underscore_to_camelcase(func['name'])
164 if not util.is_notification(func["name"]):
165 camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
166 if util.is_details(camel_case_name_with_suffix):
167 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
169 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
170 dto_package=dto_package,
171 callback_dto=camel_case_name_with_suffix,
172 callback_dto_field=camel_case_method_name,
173 callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
174 future_package=future_facade_package))
176 methods.append(future_jvpp_method_template.substitute(base_package=base_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)))
183 methods_impl.append(future_jvpp_method_impl_template.substitute(base_package=base_package,
184 dto_package=dto_package,
185 method_name=camel_case_request_method_name +
186 util.underscore_to_camelcase_upper(util.dump_suffix),
187 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
188 request_name=util.remove_reply_suffix(camel_case_reply_name) +
189 util.underscore_to_camelcase_upper(util.dump_suffix)))
191 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
192 if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
194 methods.append(future_jvpp_method_template.substitute(base_package=base_package,
195 dto_package=dto_package,
196 method_name=camel_case_request_method_name,
197 reply_name=camel_case_name_with_suffix,
198 request_name=request_name))
199 methods_impl.append(future_jvpp_method_impl_template.substitute(base_package=base_package,
200 dto_package=dto_package,
201 method_name=camel_case_request_method_name,
202 reply_name=camel_case_name_with_suffix,
203 request_name=request_name))
205 # Callback handler is a bit special and a different template has to be used
206 if util.is_control_ping(camel_case_name_with_suffix):
207 callbacks.append(jvpp_facade_control_ping_method_template.substitute(base_package=base_package,
208 dto_package=dto_package,
209 callback_dto=camel_case_name_with_suffix,
210 future_package=future_facade_package))
212 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
213 dto_package=dto_package,
214 callback_dto=camel_case_name_with_suffix))
216 if util.is_notification(func["name"]):
217 callbacks.append(jvpp_facade_callback_notification_method_template.substitute(base_package=base_package,
218 dto_package=dto_package,
219 callback_dto=util.add_notification_suffix(camel_case_name_with_suffix)))
221 jvpp_file = open(os.path.join(future_facade_package, "FutureJVppFacadeCallback.java"), 'w')
222 jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
223 base_package=base_package,
224 dto_package=dto_package,
225 notification_package=notification_package,
226 callback_package=callback_package,
227 methods="".join(callbacks),
228 future_package=future_facade_package))
232 jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp.java"), 'w')
233 jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
234 base_package=base_package,
235 methods="".join(methods),
236 future_package=future_facade_package))
240 jvpp_file = open(os.path.join(future_facade_package, "FutureJVppFacade.java"), 'w')
241 jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
242 base_package=base_package,
243 dto_package=dto_package,
244 methods="".join(methods_impl),
245 future_package=future_facade_package))
250 future_jvpp_template = Template('''
251 package $base_package.$future_package;
254 * <p>Async facade extension adding specific methods for each request invocation
255 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
256 * <br>(python representation of vpe.api generated by vppapigen).
258 public interface FutureJVpp extends FutureJVppInvoker {
263 future_jvpp_method_template = Template('''
264 java.util.concurrent.CompletionStage<$base_package.$dto_package.$reply_name> $method_name($base_package.$dto_package.$request_name request);
268 future_jvpp_facade_template = Template('''
269 package $base_package.$future_package;
272 * <p>Implementation of FutureJVpp based on FutureJVppInvokerFacade
273 * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
274 * <br>(python representation of vpe.api generated by vppapigen).
276 public class FutureJVppFacade extends FutureJVppInvokerFacade implements FutureJVpp {
279 * <p>Create FutureJVppFacade object for provided JVpp instance.
280 * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
281 * and then connects to provided JVpp instance
283 * @param jvpp provided $base_package.JVpp instance
285 * @throws java.io.IOException in case instance cannot connect to JVPP
287 public FutureJVppFacade(final $base_package.JVpp jvpp) throws java.io.IOException {
288 super(jvpp, new java.util.HashMap<>());
289 jvpp.connect(new FutureJVppFacadeCallback(getRequests(), getNotificationCallback()));
295 future_jvpp_method_impl_template = Template('''
297 public java.util.concurrent.CompletionStage<$base_package.$dto_package.$reply_name> $method_name($base_package.$dto_package.$request_name request) {
298 return send(request);
302 # Returns request name or special one from unconventional_naming_rep_req map
303 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
304 # FIXME this is a hotfix for sub-details callbacks
305 # FIXME also for L2FibTableEntry
306 # It's all because unclear mapping between
308 # dump -> reply, details,
309 # notification_start -> reply, notifications
311 # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
312 suffix = func_name.split("_")[-1]
313 return util.underscore_to_camelcase_upper(
314 util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
315 else camel_case_dto_name