3 # Copyright (c) 2016,2018 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.
15 from string import Template
17 from jvpp_model import is_control_ping, is_control_ping_reply, is_dump, is_request, is_details, is_reply, is_event
20 def generate_future_facade(work_dir, model, logger):
21 logger.debug("Generating JVpp future facade for %s" % model.json_api_files)
22 _generate_future_jvpp(work_dir, model),
23 _generate_future_jvpp_facade(work_dir, model)
24 _generate_future_jvpp_callback(work_dir, model)
27 def _generate_future_jvpp(work_dir, model):
28 with open("%s/FutureJVpp%s.java" % (work_dir, model.plugin_java_name), "w") as f:
29 f.write(_FUTURE_JVPP_TEMPLATE.substitute(
30 plugin_package=model.plugin_package,
31 json_filename=model.json_api_files,
32 plugin_name=model.plugin_java_name,
33 methods=_generate_future_jvpp_methods(model)
36 _FUTURE_JVPP_TEMPLATE = Template('''
37 package $plugin_package.future;
40 * <p>Async facade extension adding specific methods for each request invocation
41 * <br>It was generated by jvpp_future_facade_gen.py based on $json_filename.
43 public interface FutureJVpp${plugin_name} extends io.fd.vpp.jvpp.future.FutureJVppInvoker {
47 public $plugin_package.notification.${plugin_name}EventRegistry getEventRegistry();
53 def _generate_future_jvpp_methods(model):
55 for msg in model.messages:
56 if is_control_ping(msg) or is_control_ping_reply(msg):
57 # Skip control_ping managed by jvpp registry.
61 reply_name = msg.reply_java
63 # use reply dump wrappers
64 reply_name = "%sReplyDump" % msg.reply_java
68 methods.append(_FUTURE_JVPP_METHOD_TEMPLATE.substitute(
69 plugin_package=model.plugin_package,
70 method_name=msg.java_name_lower,
71 reply_name=reply_name,
72 request_name=msg.java_name_upper
74 return "".join(methods)
76 _FUTURE_JVPP_METHOD_TEMPLATE = Template('''
77 java.util.concurrent.CompletionStage<${plugin_package}.dto.${reply_name}> ${method_name}(${plugin_package}.dto.${request_name} request);
81 def _generate_future_jvpp_facade(work_dir, model):
82 with open("%s/FutureJVpp%sFacade.java" % (work_dir, model.plugin_java_name), "w") as f:
83 f.write(_FUTURE_JVPP_FACADE_TEMPLATE.substitute(
84 plugin_package=model.plugin_package,
85 json_filename=model.json_api_files,
86 plugin_name=model.plugin_java_name,
87 methods=_generate_future_jvpp_facade_methods(model)
90 _FUTURE_JVPP_FACADE_TEMPLATE = Template('''
91 package $plugin_package.future;
94 * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
95 * <br>It was generated by jvpp_future_facade_gen.py based on $json_filename.
97 public class FutureJVpp${plugin_name}Facade extends io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
99 private final $plugin_package.notification.${plugin_name}EventRegistryImpl eventRegistry = new $plugin_package.notification.${plugin_name}EventRegistryImpl();
102 * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
103 * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
104 * and then connects to provided JVpp instance
106 * @param jvpp provided io.fd.vpp.jvpp.JVpp instance
108 * @throws java.io.IOException in case instance cannot connect to JVPP
110 public FutureJVpp${plugin_name}Facade(final io.fd.vpp.jvpp.JVppRegistry registry, final io.fd.vpp.jvpp.JVpp jvpp) throws java.io.IOException {
111 super(jvpp, registry, new java.util.HashMap<>());
112 java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
113 registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), eventRegistry));
117 public $plugin_package.notification.${plugin_name}EventRegistry getEventRegistry() {
118 return eventRegistry;
126 def _generate_future_jvpp_facade_methods(model):
128 for msg in model.messages:
129 if is_control_ping(msg) or is_control_ping_reply(msg):
130 # Skip control_ping managed by jvpp registry.
134 template = _FUTURE_JVPP_FACADE_REQUEST_TEMPLATE
136 template = _FUTURE_JVPP_FACADE_DUMP_TEMPLATE
140 methods.append(template.substitute(
141 plugin_package=model.plugin_package,
142 method_name=msg.java_name_lower,
143 reply_name=msg.reply_java,
144 request_name=msg.java_name_upper
146 return "".join(methods)
148 _FUTURE_JVPP_FACADE_REQUEST_TEMPLATE = Template('''
150 public java.util.concurrent.CompletionStage<${plugin_package}.dto.${reply_name}> ${method_name}(${plugin_package}.dto.${request_name} request) {
151 return send(request);
155 _FUTURE_JVPP_FACADE_DUMP_TEMPLATE = Template('''
157 public java.util.concurrent.CompletionStage<${plugin_package}.dto.${reply_name}ReplyDump> ${method_name}(${plugin_package}.dto.${request_name} request) {
158 return send(request, new ${plugin_package}.dto.${reply_name}ReplyDump());
163 def _generate_future_jvpp_callback(work_dir, model):
164 with open("%s/FutureJVpp%sFacadeCallback.java" % (work_dir, model.plugin_java_name), "w") as f:
165 f.write(_FUTURE_JVPP_CALLBACK_TEMPLATE.substitute(
166 plugin_package=model.plugin_package,
167 json_filename=model.json_api_files,
168 plugin_name=model.plugin_java_name,
169 methods=_generate_future_jvpp_callback_methods(model)
172 _FUTURE_JVPP_CALLBACK_TEMPLATE = Template("""
173 package $plugin_package.future;
176 * <p>Async facade callback setting values to future objects
177 * <br>It was generated by jvpp_future_facade_gen.py based on $json_filename.
179 public final class FutureJVpp${plugin_name}FacadeCallback implements $plugin_package.callback.JVpp${plugin_name}GlobalCallback {
181 private final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends io.fd.vpp.jvpp.dto.JVppReply<?>>> requests;
182 private final $plugin_package.notification.Global${plugin_name}EventCallback notificationCallback;
183 private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(FutureJVpp${plugin_name}FacadeCallback.class.getName());
185 public FutureJVpp${plugin_name}FacadeCallback(
186 final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends io.fd.vpp.jvpp.dto.JVppReply<?>>> requestMap,
187 final $plugin_package.notification.Global${plugin_name}EventCallback notificationCallback) {
188 this.requests = requestMap;
189 this.notificationCallback = notificationCallback;
193 @SuppressWarnings("unchecked")
194 public void onError(io.fd.vpp.jvpp.VppCallbackException reply) {
195 final java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<?>> completableFuture;
197 synchronized(requests) {
198 completableFuture = (java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<?>>) requests.get(reply.getCtxId());
201 if(completableFuture != null) {
202 completableFuture.completeExceptionally(reply);
204 synchronized(requests) {
205 requests.remove(reply.getCtxId());
211 @SuppressWarnings("unchecked")
212 public void onControlPingReply(final io.fd.vpp.jvpp.dto.ControlPingReply reply) {
213 java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<?>> completableFuture;
215 final int replyId = reply.context;
216 synchronized(requests) {
217 completableFuture = (java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<?>>) requests.get(replyId);
219 if(completableFuture != null) {
221 if (completableFuture instanceof io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture) {
222 completableFuture.complete(((io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getReplyDump());
223 // Remove future mapped to dump call context id
224 requests.remove(((io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getContextId());
226 // reply to regular control ping, complete the future
227 completableFuture.complete(reply);
229 requests.remove(replyId);
231 // future not yet created by writer, create new future, complete it and put to map under ping id
232 completableFuture = new java.util.concurrent.CompletableFuture<>();
233 completableFuture.complete(reply);
234 requests.put(replyId, completableFuture);
244 def _generate_future_jvpp_callback_methods(model):
246 for msg in model.messages:
247 if is_control_ping(msg) or is_control_ping_reply(msg):
248 # Skip control_ping managed by jvpp registry.
250 if is_dump(msg) or is_request(msg):
253 # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
257 template = _FUTURE_JVPP_FACADE_DETAILS_CALLBACK_TEMPLATE
258 request_dto = msg.request_java
260 template = _FUTURE_JVPP_FACADE_REPLY_CALLBACK_TEMPLATE
261 request_dto = msg.request_java
263 template = _FUTURE_JVPP_FACADE_EVENT_CALLBACK_TEMPLATE
265 raise TypeError("Unknown message type %s", msg)
267 methods.append(template.substitute(
268 plugin_package=model.plugin_package,
269 callback_dto=msg.java_name_upper,
270 request_dto=request_dto,
271 callback_dto_field=msg.java_name_lower,
273 return "".join(methods)
276 _FUTURE_JVPP_FACADE_DETAILS_CALLBACK_TEMPLATE = Template("""
278 @SuppressWarnings("unchecked")
279 public void on$callback_dto(final $plugin_package.dto.$callback_dto reply) {
280 io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.dto.${callback_dto}ReplyDump> completableFuture;
281 final int replyId = reply.context;
282 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
283 LOG.fine(String.format("Received $callback_dto event message: %s", reply));
285 synchronized(requests) {
286 completableFuture = (io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.dto.${callback_dto}ReplyDump>) requests.get(replyId);
288 if(completableFuture == null) {
289 // reply received before writer created future,
290 // create new future, and put into map to notify sender that reply is already received,
291 // following details replies will add information to this future
292 completableFuture = new io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture<>(replyId,
293 new $plugin_package.dto.${callback_dto}ReplyDump());
294 requests.put(replyId, completableFuture);
296 completableFuture.getReplyDump().$callback_dto_field.add(reply);
301 _FUTURE_JVPP_FACADE_REPLY_CALLBACK_TEMPLATE = Template("""
303 @SuppressWarnings("unchecked")
304 public void on$callback_dto(final $plugin_package.dto.$callback_dto reply) {
305 java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<$plugin_package.dto.$request_dto>> completableFuture;
306 final int replyId = reply.context;
307 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
308 LOG.fine(String.format("Received $callback_dto event message: %s", reply));
310 synchronized(requests) {
312 (java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<$plugin_package.dto.$request_dto>>) requests.get(replyId);
314 if(completableFuture != null) {
315 // received reply on request, complete future created by sender and remove it from map
316 completableFuture.complete(reply);
317 requests.remove(replyId);
319 // reply received before writer created future,
320 // create new future, complete it and put into map to
321 // notify sender that reply is already received
322 completableFuture = new java.util.concurrent.CompletableFuture<>();
323 completableFuture.complete(reply);
324 requests.put(replyId, completableFuture);
330 _FUTURE_JVPP_FACADE_EVENT_CALLBACK_TEMPLATE = Template("""
332 public void on$callback_dto($plugin_package.dto.$callback_dto notification) {
333 if (LOG.isLoggable(java.util.logging.Level.FINE)) {
334 LOG.fine(String.format("Received $callback_dto event message: %s", notification));
336 notificationCallback.on$callback_dto(notification);