blob: df6407f845d6baa0d33de7689e5333e36ce54b2d [file] [log] [blame]
Maros Marsalek7becd082016-05-31 17:45:16 +02001#!/usr/bin/env python
2#
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:
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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
16import os
17
18import callback_gen
19import util
20from string import Template
21
22from util import remove_suffix
23
24notification_registry_template = Template("""
25package $base_package.$notification_package;
26
27/**
28 * <p>Registry for notification callbacks.
29 * <br>It was generated by notification_gen.py based on $inputfile
30 * <br>(python representation of vpe.api generated by vppapigen).
31 */
32public interface NotificationRegistry extends java.lang.AutoCloseable {
33
34 $register_callback_methods
35
36 @Override
37 void close();
38}
39""")
40
41global_notification_callback_template = Template("""
42package $base_package.$notification_package;
43
44/**
45 * <p>Aggregated callback interface for notifications only.
46 * <br>It was generated by notification_gen.py based on $inputfile
47 * <br>(python representation of vpe.api generated by vppapigen).
48 */
49public interface GlobalNotificationCallback extends $callbacks {
50
51}
52""")
53
54notification_registry_impl_template = Template("""
55package $base_package.$notification_package;
56
57/**
58 * <p>Notification registry delegating notification processing to registered callbacks.
59 * <br>It was generated by notification_gen.py based on $inputfile
60 * <br>(python representation of vpe.api generated by vppapigen).
61 */
62public final class NotificationRegistryImpl implements NotificationRegistry, GlobalNotificationCallback {
63
64 // TODO add a special NotificationCallback interface and only allow those to be registered
65 private final java.util.concurrent.ConcurrentMap<Class<? extends $base_package.$dto_package.JVppNotification>, $base_package.$callback_package.JVppNotificationCallback> registeredCallbacks =
66 new java.util.concurrent.ConcurrentHashMap<>();
67
68 $register_callback_methods
69 $handler_methods
70
71 @Override
72 public void close() {
73 registeredCallbacks.clear();
74 }
75}
76""")
77
78register_callback_impl_template = Template("""
79 public java.lang.AutoCloseable register$callback(final $base_package.$callback_package.$callback callback){
80 if(null != registeredCallbacks.putIfAbsent($base_package.$dto_package.$notification.class, callback)){
81 throw new IllegalArgumentException("Callback for " + $base_package.$dto_package.$notification.class +
82 "notification already registered");
83 }
84 return () -> registeredCallbacks.remove($base_package.$dto_package.$notification.class);
85 }
86""")
87
88handler_impl_template = Template("""
89 @Override
90 public void on$notification(
91 final $base_package.$dto_package.$notification notification) {
92 final $base_package.$callback_package.JVppNotificationCallback JVppNotificationCallback = registeredCallbacks.get($base_package.$dto_package.$notification.class);
93 if (null != JVppNotificationCallback) {
94 (($base_package.$callback_package.$callback) registeredCallbacks
95 .get($base_package.$dto_package.$notification.class))
96 .on$notification(notification);
97 }
98 }
99""")
100
101
102def generate_notification_registry(func_list, base_package, notification_package, callback_package, dto_package, inputfile):
103 """ Generates notification registry interface and implementation """
104 print "Generating Notification interfaces and implementation"
105
106 if not os.path.exists(notification_package):
107 raise Exception("%s folder is missing" % notification_package)
108
109 callbacks = []
110 register_callback_methods = []
111 register_callback_methods_impl = []
112 handler_methods = []
113 for func in func_list:
114
115 if not util.is_notification(func['name']):
116 continue
117
118 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
119 notification_dto = util.add_notification_suffix(camel_case_name_with_suffix)
120 callback_ifc = notification_dto + callback_gen.callback_suffix
121 fully_qualified_callback_ifc = "{0}.{1}.{2}".format(base_package, callback_package, callback_ifc)
122 callbacks.append(fully_qualified_callback_ifc)
123
124 # TODO create NotificationListenerRegistration and return that instead of AutoCloseable to better indicate
125 # that the registration should be closed
126 register_callback_methods.append("java.lang.AutoCloseable register{0}({1} callback);"
127 .format(callback_ifc, fully_qualified_callback_ifc))
128 register_callback_methods_impl.append(register_callback_impl_template.substitute(base_package=base_package,
129 callback_package=callback_package,
130 dto_package=dto_package,
131 notification=notification_dto,
132 callback=callback_ifc))
133 handler_methods.append(handler_impl_template.substitute(base_package=base_package,
134 callback_package=callback_package,
135 dto_package=dto_package,
136 notification=notification_dto,
137 callback=callback_ifc))
Ed Warnickeadeb7492016-07-11 10:29:41 -0700138 if(callbacks):
139 callback_file = open(os.path.join(notification_package, "NotificationRegistry.java"), 'w')
140 callback_file.write(notification_registry_template.substitute(inputfile=inputfile,
141 register_callback_methods="\n ".join(register_callback_methods),
142 base_package=base_package,
143 notification_package=notification_package))
144 callback_file.flush()
145 callback_file.close()
Maros Marsalek7becd082016-05-31 17:45:16 +0200146
Ed Warnickeadeb7492016-07-11 10:29:41 -0700147 callback_file = open(os.path.join(notification_package, "GlobalNotificationCallback.java"), 'w')
148 callback_file.write(global_notification_callback_template.substitute(inputfile=inputfile,
149 callbacks=", ".join(callbacks),
150 base_package=base_package,
151 notification_package=notification_package))
152 callback_file.flush()
153 callback_file.close()
Maros Marsalek7becd082016-05-31 17:45:16 +0200154
Ed Warnickeadeb7492016-07-11 10:29:41 -0700155 callback_file = open(os.path.join(notification_package, "NotificationRegistryImpl.java"), 'w')
156 callback_file.write(notification_registry_impl_template.substitute(inputfile=inputfile,
157 callback_package=callback_package,
158 dto_package=dto_package,
159 register_callback_methods="".join(register_callback_methods_impl),
160 handler_methods="".join(handler_methods),
Maros Marsalek7becd082016-05-31 17:45:16 +0200161 base_package=base_package,
162 notification_package=notification_package))
Ed Warnickeadeb7492016-07-11 10:29:41 -0700163 callback_file.flush()
164 callback_file.close()