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_ifc_template = Template("""
23 package $base_package.$callback_facade_package;
26 * <p>Callback Java API representation of vpe.api.
27 * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
28 * <br>(python representation of vpe.api generated by vppapigen).
30 public interface CallbackJVpp extends $base_package.$notification_package.NotificationRegistryProvider, java.lang.AutoCloseable {
41 jvpp_impl_template = Template("""
42 package $base_package.$callback_facade_package;
45 * <p>Default implementation of CallbackJVpp interface.
46 * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
47 * <br>(python representation of vpe.api generated by vppapigen).
49 public final class CallbackJVppFacade extends $base_package.$notification_package.NotificationRegistryProviderContext implements $base_package.$callback_facade_package.CallbackJVpp {
51 private final $base_package.JVpp jvpp;
52 private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> callbacks;
55 * <p>Create CallbackJVppFacade object for provided JVpp instance.
56 * Constructor internally creates CallbackJVppFacadeCallback class for processing callbacks
57 * and then connects to provided JVpp instance
59 * @param jvpp provided $base_package.JVpp instance
61 * @throws java.io.IOException in case instance cannot connect to JVPP
63 public CallbackJVppFacade(final $base_package.JVpp jvpp) throws java.io.IOException {
64 this.jvpp = java.util.Objects.requireNonNull(jvpp,"jvpp is null");
65 this.callbacks = new java.util.HashMap<>();
66 this.jvpp.connect(new CallbackJVppFacadeCallback(this.callbacks, getNotificationCallback()));
79 method_template = Template(
80 """ void $name($base_package.$dto_package.$request request, $base_package.$callback_package.$callback callback) throws org.openvpp.jvpp.VppInvocationException;""")
82 method_impl_template = Template(""" public final void $name($base_package.$dto_package.$request request, $base_package.$callback_package.$callback callback) throws org.openvpp.jvpp.VppInvocationException {
83 synchronized (callbacks) {
84 callbacks.put(jvpp.$name(request), callback);
89 no_arg_method_template = Template(""" void $name($base_package.$callback_package.$callback callback) throws org.openvpp.jvpp.VppInvocationException;""")
90 no_arg_method_impl_template = Template(""" public final void $name($base_package.$callback_package.$callback callback) throws org.openvpp.jvpp.VppInvocationException {
91 synchronized (callbacks) {
92 callbacks.put(jvpp.$name(), callback);
98 def generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile):
99 """ Generates callback facade """
100 print "Generating JVpp callback facade"
102 if os.path.exists(callback_facade_package):
103 util.remove_folder(callback_facade_package)
105 os.mkdir(callback_facade_package)
109 for func in func_list:
111 if util.is_notification(func['name']) or util.is_ignored(func['name']):
112 # TODO handle notifications
115 camel_case_name = util.underscore_to_camelcase(func['name'])
116 camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
117 if util.is_reply(camel_case_name):
120 # Strip suffix for dump calls
121 callback_type = get_request_name(camel_case_name_upper, func['name']) + callback_gen.callback_suffix
123 if len(func['args']) == 0:
124 methods.append(no_arg_method_template.substitute(name=camel_case_name,
125 base_package=base_package,
126 dto_package=dto_package,
127 callback_package=callback_package,
128 callback=callback_type))
129 methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
130 base_package=base_package,
131 dto_package=dto_package,
132 callback_package=callback_package,
133 callback=callback_type))
135 methods.append(method_template.substitute(name=camel_case_name,
136 request=camel_case_name_upper,
137 base_package=base_package,
138 dto_package=dto_package,
139 callback_package=callback_package,
140 callback=callback_type))
141 methods_impl.append(method_impl_template.substitute(name=camel_case_name,
142 request=camel_case_name_upper,
143 base_package=base_package,
144 dto_package=dto_package,
145 callback_package=callback_package,
146 callback=callback_type))
148 join = os.path.join(callback_facade_package, "CallbackJVpp.java")
149 jvpp_file = open(join, 'w')
151 jvpp_ifc_template.substitute(inputfile=inputfile,
152 methods="\n".join(methods),
153 base_package=base_package,
154 dto_package=dto_package,
155 notification_package=notification_package,
156 callback_facade_package=callback_facade_package))
160 jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacade.java"), 'w')
161 jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
162 methods="\n".join(methods_impl),
163 base_package=base_package,
164 dto_package=dto_package,
165 notification_package=notification_package,
166 callback_package=callback_package,
167 callback_facade_package=callback_facade_package))
171 generate_callback(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile)
174 jvpp_facade_callback_template = Template("""
175 package $base_package.$callback_facade_package;
178 * <p>Implementation of JVppGlobalCallback interface for Java Callback API.
179 * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
180 * <br>(python representation of vpe.api generated by vppapigen).
182 public final class CallbackJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback {
184 private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requests;
185 private final $base_package.$notification_package.GlobalNotificationCallback notificationCallback;
186 private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(CallbackJVppFacadeCallback.class.getName());
188 public CallbackJVppFacadeCallback(final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requestMap,
189 final $base_package.$notification_package.GlobalNotificationCallback notificationCallback) {
190 this.requests = requestMap;
191 this.notificationCallback = notificationCallback;
195 public void onError(org.openvpp.jvpp.VppCallbackException reply) {
197 $base_package.$callback_package.JVppCallback failedCall;
198 synchronized(requests) {
199 failedCall = requests.remove(reply.getCtxId());
202 if(failedCall != null) {
204 failedCall.onError(reply);
205 } catch(RuntimeException ex) {
206 ex.addSuppressed(reply);
207 LOG.log(java.util.logging.Level.WARNING, String.format("Callback: %s failed while handling exception: %s", failedCall, reply), ex);
216 jvpp_facade_callback_method_template = Template("""
218 @SuppressWarnings("unchecked")
219 public void on$callback_dto($base_package.$dto_package.$callback_dto reply) {
221 $base_package.$callback_package.$callback callback;
222 synchronized(requests) {
223 callback = ($base_package.$callback_package.$callback) requests.remove(reply.context);
226 if(callback != null) {
227 callback.on$callback_dto(reply);
232 jvpp_facade_callback_notification_method_template = Template("""
234 @SuppressWarnings("unchecked")
235 public void on$callback_dto($base_package.$dto_package.$callback_dto notification) {
236 notificationCallback.on$callback_dto(notification);
241 def generate_callback(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, inputfile):
243 for func in func_list:
245 if util.is_ignored(func['name']):
248 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
250 if util.is_reply(camel_case_name_with_suffix):
251 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
252 dto_package=dto_package,
253 callback_package=callback_package,
254 callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix,
255 callback_dto=camel_case_name_with_suffix))
257 if util.is_notification(func["name"]):
258 with_notification_suffix = util.add_notification_suffix(camel_case_name_with_suffix)
259 callbacks.append(jvpp_facade_callback_notification_method_template.substitute(base_package=base_package,
260 dto_package=dto_package,
261 callback_package=callback_package,
262 callback=with_notification_suffix + callback_gen.callback_suffix,
263 callback_dto=with_notification_suffix))
265 jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacadeCallback.java"), 'w')
266 jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
267 base_package=base_package,
268 dto_package=dto_package,
269 notification_package=notification_package,
270 callback_package=callback_package,
271 methods="".join(callbacks),
272 callback_facade_package=callback_facade_package))
277 # Returns request name or special one from unconventional_naming_rep_req map
278 def get_request_name(camel_case_dto_name, func_name):
279 if func_name in reverse_dict(util.unconventional_naming_rep_req):
280 request_name = util.underscore_to_camelcase_upper(reverse_dict(util.unconventional_naming_rep_req)[func_name])
282 request_name = camel_case_dto_name
283 return remove_suffix(request_name)
286 def reverse_dict(map):
287 return dict((v, k) for k, v in map.iteritems())
290 def remove_suffix(name):
291 if util.is_reply(name):
292 return util.remove_reply_suffix(name)
294 if util.is_dump(name):
295 return util.remove_suffix(name, util.dump_suffix)