blob: 6bf8df34d66395541485519d92aa90d8dd1950f3 [file] [log] [blame]
Ray Milkey1d52ddd2018-06-07 10:37:24 -07001"""
2 Copyright 2018-present Open Networking Foundation
3
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"""
16
17load("//tools/build/bazel:generate_workspace.bzl", "maven_coordinates")
18load(
19 "//tools/build/bazel:variables.bzl",
20 "APP_PREFIX",
21 "DEFAULT_APP_CATEGORY",
22 "ONOS_ARTIFACT_BASE",
23 "ONOS_GROUP_ID",
24 "ONOS_ORIGIN",
25 "ONOS_VERSION",
26)
27
28# Implementation of the rule to build an ONOS application OAR file
29def _onos_oar_impl(ctx):
30 app_xml_file = ctx.attr.app_xml.files.to_list()[0]
31 feature_xml_file = ctx.attr.feature_xml.files.to_list()[0]
32 feature_xml_coords = ctx.attr.feature_xml_coords
Ray Milkey1ccab712018-06-12 16:30:02 -070033
34 jar_file_args = []
35 jar_files = []
36 for bundle in ctx.attr.included_bundles:
37 jar_file = bundle.files.to_list()[0]
38 jar_files.append(jar_file)
39 jar_file_coords = maven_coordinates(bundle.label)
40 jar_file_args.append(jar_file.path)
41 jar_file_args.append(jar_file_coords)
42
Ray Milkey1d52ddd2018-06-07 10:37:24 -070043 arguments = [
44 ctx.outputs.app_oar.path,
45 feature_xml_file.path,
46 feature_xml_coords,
47 app_xml_file.path,
48 "NONE",
Ray Milkey1ccab712018-06-12 16:30:02 -070049 ] + jar_file_args
Ray Milkey1d52ddd2018-06-07 10:37:24 -070050
51 ctx.actions.run(
Ray Milkey1ccab712018-06-12 16:30:02 -070052 inputs = [app_xml_file, feature_xml_file] + jar_files,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070053 outputs = [ctx.outputs.app_oar],
54 arguments = arguments,
55 progress_message = "Running oar file generator: %s" % ctx.attr.name,
Thomas Vachuska8e022a92018-07-10 14:47:38 -070056 executable = ctx.executable._onos_app_bundler,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070057 )
58
Thomas Vachuska8e022a92018-07-10 14:47:38 -070059# Implementation of the rule to build an app.xml desriptor file for ONOS app
Ray Milkey1d52ddd2018-06-07 10:37:24 -070060def _onos_app_xml_impl(ctx):
Thomas Vachuska8e022a92018-07-10 14:47:38 -070061 # Build-up arguments for the app.xml and feature.xml generator
Ray Milkey1d52ddd2018-06-07 10:37:24 -070062 arguments = [
Ray Milkey5063f5b2018-08-15 16:22:30 -070063 "-n",
64 ctx.attr.feature_coords,
65 "-a",
66 ctx.attr.app_name,
67 "-o",
68 ctx.attr.origin,
69 "-c",
70 ctx.attr.category,
71 "-u",
72 ctx.attr.url,
73 "-v",
74 ctx.attr.version,
75 "-t",
76 ctx.attr.title,
77 "-D",
78 ctx.attr.description,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070079 ]
80
Thomas Vachuska8e022a92018-07-10 14:47:38 -070081 for bundle in ctx.attr.included_bundles:
82 arguments += ["-b", maven_coordinates(bundle.label)]
83 for bundle in ctx.attr.excluded_bundles:
84 arguments += ["-e", maven_coordinates(bundle.label)]
85 for app in ctx.attr.required_apps:
Ray Milkey15053f02018-06-13 10:00:45 -070086 arguments += ["-d", app]
Ray Milkey1d52ddd2018-06-07 10:37:24 -070087
Thomas Vachuska8e022a92018-07-10 14:47:38 -070088 if ctx.attr.security != "":
89 arguments += ["-s", ctx.attr.security]
Ray Milkey1d52ddd2018-06-07 10:37:24 -070090
91 ctx.actions.run(
92 inputs = [],
93 outputs = [ctx.outputs.app_xml],
Ray Milkey5063f5b2018-08-15 16:22:30 -070094 arguments = arguments + ["-A", "-O", ctx.outputs.app_xml.path],
Thomas Vachuska8e022a92018-07-10 14:47:38 -070095 progress_message = "Generating app.xml descriptor for app: %s" % ctx.attr.name,
96 executable = ctx.executable._onos_app_tools,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070097 )
98
Thomas Vachuska8e022a92018-07-10 14:47:38 -070099# Implementation of the rule to build the feature.xml file for ONOS app
100def _onos_feature_xml_impl(ctx):
101 # Build-up arguments
102 arguments = [
Ray Milkey5063f5b2018-08-15 16:22:30 -0700103 "-n",
104 ctx.attr.feature_coords,
105 "-a",
106 ctx.attr.app_name,
107 "-u",
108 ctx.attr.url,
109 "-v",
110 ctx.attr.version,
111 "-t",
112 ctx.attr.title,
113 "-D",
114 ctx.attr.description,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700115 ]
116
117 for bundle in ctx.attr.included_bundles:
118 arguments += ["-b", maven_coordinates(bundle.label)]
119 for feature in ctx.attr.required_features:
120 arguments += ["-f", feature]
121
122 ctx.actions.run(
123 inputs = [],
124 outputs = [ctx.outputs.feature_xml],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700125 arguments = arguments + ["-F", "-O", ctx.outputs.feature_xml.path],
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700126 progress_message = "Generating feature.xml for app: %s" % ctx.attr.name,
127 executable = ctx.executable._onos_app_tools,
128 )
129
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700130# Rule to generate the ONOS app OAR file.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700131_onos_oar = rule(
132 attrs = {
133 "deps": attr.label_list(),
134 "version": attr.string(),
135 "package_name_root": attr.string(),
136 "source": attr.label(),
137 "app_xml": attr.label(),
138 "feature_xml": attr.label(),
139 "feature_xml_coords": attr.string(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700140 "included_bundles": attr.label_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700141 "_onos_app_bundler": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700142 executable = True,
143 cfg = "host",
144 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700145 default = Label("//tools/build/bazel:onos_app_bundler"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700146 ),
147 },
148 outputs = {
149 "app_oar": "%{name}.oar",
150 },
151 implementation = _onos_oar_impl,
152)
153
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700154# Rule to generate app.xml descriptor for an ONOS application.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700155_onos_app_xml = rule(
156 attrs = {
157 "app_name": attr.string(),
158 "origin": attr.string(),
159 "version": attr.string(),
160 "title": attr.string(),
161 "category": attr.string(),
162 "url": attr.string(),
163 "feature_coords": attr.string(),
164 "description": attr.string(),
165 "apps": attr.label_list(),
166 "included_bundles": attr.label_list(),
167 "excluded_bundles": attr.label_list(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700168 "required_apps": attr.string_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700169 "security": attr.string(),
170 "_onos_app_tools": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700171 executable = True,
172 cfg = "host",
173 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700174 default = Label("//tools/build/bazel:onos_app_tools"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700175 ),
176 },
177 outputs = {
178 "app_xml": "%{name}.xml",
179 },
180 implementation = _onos_app_xml_impl,
181)
182
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700183# Rule to generate feature.xml descriptor for an ONOS application.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700184_onos_feature_xml = rule(
185 attrs = {
186 "app_name": attr.string(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700187 "version": attr.string(),
188 "title": attr.string(),
189 "category": attr.string(),
190 "url": attr.string(),
191 "feature_coords": attr.string(),
192 "description": attr.string(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700193 "included_bundles": attr.label_list(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700194 "required_features": attr.string_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700195 "_onos_app_tools": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700196 executable = True,
197 cfg = "host",
198 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700199 default = Label("//tools/build/bazel:onos_app_tools"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700200 ),
201 },
202 outputs = {
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700203 "feature_xml": "%{name}.xml",
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700204 },
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700205 implementation = _onos_feature_xml_impl,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700206)
207
208def _basename(path):
209 paths = path.split("/")
210 return paths[len(paths) - 1]
211
212def _get_base_path():
213 return native.package_name()
214
215def _get_name():
216 base_path = _get_base_path()
217 return ONOS_ARTIFACT_BASE + base_path.replace("/", "-")
218
219def _get_app_name():
220 base_path = _get_base_path()
221 return APP_PREFIX + _basename(base_path)
222
223def _local_label(name, suffix):
224 base_label_name = "//" + native.package_name() + ":"
225 return base_label_name + name + suffix
226
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700227# Macro to build an ONOS application OAR file.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700228def onos_app(
229 app_name = None,
230 name = None,
231 title = None,
232 version = ONOS_VERSION,
233 origin = ONOS_ORIGIN,
234 category = DEFAULT_APP_CATEGORY,
235 url = None,
236 description = None,
237 feature_coords = None,
238 required_features = ["onos-api"],
239 required_apps = [],
240 included_bundles = None,
241 excluded_bundles = [],
242 visibility = ["//visibility:public"],
243 security = None,
244 **kwargs):
245 if name == None:
246 name = _get_name()
247
248 if app_name == None:
249 app_name = _get_app_name()
250
251 maven_coords = "%s:%s:oar:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Thomas Vachuska1ae26c62018-07-09 13:40:16 -0700252 feature_xml_coords = "mvn:%s:%s:xml:features:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700253
254 if title == None:
255 print("Missing title for %s" % _get_name())
256 title = _get_app_name()
257
258 if included_bundles == None:
Ray Milkey1ccab712018-06-12 16:30:02 -0700259 target = _local_label(name, "")
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700260 included_bundles = [target]
261
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700262 if not feature_coords:
Thomas Vachuska1ae26c62018-07-09 13:40:16 -0700263 feature_coords = "mvn:%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700264
265 # TODO - intra app dependecies
266 apps = []
267
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700268 # Generate the app.xml file
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700269 _onos_app_xml(
270 name = name + "-app-xml",
271 app_name = app_name,
272 origin = origin,
273 version = version,
274 title = title,
275 category = category,
276 url = url,
277 feature_coords = feature_coords,
278 description = description,
279 apps = apps,
280 included_bundles = included_bundles,
281 excluded_bundles = excluded_bundles,
Ray Milkey1ccab712018-06-12 16:30:02 -0700282 required_apps = required_apps,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700283 )
284
Ray Milkey5063f5b2018-08-15 16:22:30 -0700285 # Generate feature.xml file
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700286 _onos_feature_xml(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700287 name = name + "-feature-xml",
288 app_name = app_name,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700289 version = version,
290 title = title,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700291 feature_coords = feature_coords,
292 description = description,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700293 included_bundles = included_bundles,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700294 required_features = required_features,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700295 )
296
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700297 # Generate the OAR file based on the app.xml, features.xml, and specified
298 # bundles to be included.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700299 _onos_oar(
300 name = name + "-oar",
Ray Milkey1ccab712018-06-12 16:30:02 -0700301 included_bundles = included_bundles,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700302 app_xml = Label(_local_label(name, "-app-xml")),
303 feature_xml = Label(_local_label(name, "-feature-xml")),
304 feature_xml_coords = feature_xml_coords,
305 visibility = visibility,
306 )