blob: 793ecc44132952198b44c32f8a1cfab141bd78f6 [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,
56 executable = ctx.executable._onos_app_oar_exe,
57 )
58
59# Implementation of the rule to build an app.xml or features file for an application
60def _onos_app_xml_impl(ctx):
61 output = ctx.outputs.app_xml.path
62 app_name = ctx.attr.app_name
63 origin = ctx.attr.origin
64 version = ctx.attr.version
65 title = ctx.attr.title
66 category = ctx.attr.category
67 url = ctx.attr.url
68 mode = ctx.attr.mode
69 feature_coords = ctx.attr.feature_coords
70 description = ctx.attr.description
71 apps = ctx.attr.apps
72 included_bundles = ctx.attr.included_bundles
73 excluded_bundles = ctx.attr.excluded_bundles
74 required_features = ctx.attr.required_features
Ray Milkey1ccab712018-06-12 16:30:02 -070075 required_apps = ctx.attr.required_apps
Ray Milkey1d52ddd2018-06-07 10:37:24 -070076 security = ctx.attr.security
77 artifacts_args = []
78
79 # call the app.xml generator
80 arguments = [
81 "-O",
82 output,
83 "-n",
84 feature_coords,
85 "-a",
86 app_name,
87 "-o",
88 origin,
89 "-c",
90 category,
91 "-u",
92 url,
93 "-v",
94 version,
95 "-t",
96 title,
97 "-D",
98 description,
99 mode,
100 ]
101
102 for bundle in included_bundles:
103 arguments += ["-b", maven_coordinates(bundle.label)]
104 for bundle in excluded_bundles:
105 arguments += ["-e", maven_coordinates(bundle.label)]
106 for feature in required_features:
107 arguments += ["-f", feature]
108
109 if security != "":
110 arguments += ["-s", security]
111
112 ctx.actions.run(
113 inputs = [],
114 outputs = [ctx.outputs.app_xml],
115 arguments = arguments,
116 progress_message = "Running app xml generator: %s" % ctx.attr.name,
117 executable = ctx.executable._onos_app_writer_exe,
118 )
119
120# OAR file rule
121_onos_oar = rule(
122 attrs = {
123 "deps": attr.label_list(),
124 "version": attr.string(),
125 "package_name_root": attr.string(),
126 "source": attr.label(),
127 "app_xml": attr.label(),
128 "feature_xml": attr.label(),
129 "feature_xml_coords": attr.string(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700130 "included_bundles": attr.label_list(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700131 "_onos_app_oar_exe": attr.label(
132 executable = True,
133 cfg = "host",
134 allow_files = True,
135 default = Label("//tools/build/bazel:onos_app_oar"),
136 ),
137 },
138 outputs = {
139 "app_oar": "%{name}.oar",
140 },
141 implementation = _onos_oar_impl,
142)
143
144# app.xml rule
145_onos_app_xml = rule(
146 attrs = {
147 "app_name": attr.string(),
148 "origin": attr.string(),
149 "version": attr.string(),
150 "title": attr.string(),
151 "category": attr.string(),
152 "url": attr.string(),
153 "feature_coords": attr.string(),
154 "description": attr.string(),
155 "apps": attr.label_list(),
156 "included_bundles": attr.label_list(),
157 "excluded_bundles": attr.label_list(),
158 "required_features": attr.string_list(),
159 "security": attr.string(),
160 "mode": attr.string(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700161 "required_apps": attr.string_list(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700162 "_onos_app_writer_exe": attr.label(
163 executable = True,
164 cfg = "host",
165 allow_files = True,
166 default = Label("//tools/build/bazel:onos_app_writer"),
167 ),
168 },
169 outputs = {
170 "app_xml": "%{name}.xml",
171 },
172 implementation = _onos_app_xml_impl,
173)
174
175# feature.xml rule
176_onos_feature_xml = rule(
177 attrs = {
178 "app_name": attr.string(),
179 "origin": attr.string(),
180 "version": attr.string(),
181 "title": attr.string(),
182 "category": attr.string(),
183 "url": attr.string(),
184 "feature_coords": attr.string(),
185 "description": attr.string(),
186 "apps": attr.label_list(),
187 "included_bundles": attr.label_list(),
188 "excluded_bundles": attr.label_list(),
189 "required_features": attr.string_list(),
190 "security": attr.string(),
191 "mode": attr.string(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700192 "required_apps": attr.string_list(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700193 "_onos_app_writer_exe": attr.label(
194 executable = True,
195 cfg = "host",
196 allow_files = True,
197 default = Label("//tools/build/bazel:onos_app_writer"),
198 ),
199 },
200 outputs = {
201 "app_xml": "%{name}.xml",
202 },
203 implementation = _onos_app_xml_impl,
204)
205
206def _basename(path):
207 paths = path.split("/")
208 return paths[len(paths) - 1]
209
210def _get_base_path():
211 return native.package_name()
212
213def _get_name():
214 base_path = _get_base_path()
215 return ONOS_ARTIFACT_BASE + base_path.replace("/", "-")
216
217def _get_app_name():
218 base_path = _get_base_path()
219 return APP_PREFIX + _basename(base_path)
220
221def _local_label(name, suffix):
222 base_label_name = "//" + native.package_name() + ":"
223 return base_label_name + name + suffix
224
225# Rule to build an ONOS application OAR file
226def onos_app(
227 app_name = None,
228 name = None,
229 title = None,
230 version = ONOS_VERSION,
231 origin = ONOS_ORIGIN,
232 category = DEFAULT_APP_CATEGORY,
233 url = None,
234 description = None,
235 feature_coords = None,
236 required_features = ["onos-api"],
237 required_apps = [],
238 included_bundles = None,
239 excluded_bundles = [],
240 visibility = ["//visibility:public"],
241 security = None,
242 **kwargs):
243 if name == None:
244 name = _get_name()
245
246 if app_name == None:
247 app_name = _get_app_name()
248
249 maven_coords = "%s:%s:oar:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
250 feature_xml_coords = "%s:%s:xml:features:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
251
252 if title == None:
253 print("Missing title for %s" % _get_name())
254 title = _get_app_name()
255
256 if included_bundles == None:
Ray Milkey1ccab712018-06-12 16:30:02 -0700257 target = _local_label(name, "")
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700258 included_bundles = [target]
259
260 # TODO - have to implement this eventually
261 #if not feature_coords and len(included_bundles) == 1:
262 # feature_coords = '$(maven_coords %s)' % included_bundles[0]
263
264 if not feature_coords:
265 feature_coords = "%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
266
267 # TODO - intra app dependecies
268 apps = []
269
270 # rule that generates the app.xml
271 _onos_app_xml(
272 name = name + "-app-xml",
273 app_name = app_name,
274 origin = origin,
275 version = version,
276 title = title,
277 category = category,
278 url = url,
279 feature_coords = feature_coords,
280 description = description,
281 apps = apps,
282 included_bundles = included_bundles,
283 excluded_bundles = excluded_bundles,
Ray Milkey1ccab712018-06-12 16:30:02 -0700284 required_apps = required_apps,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700285 mode = "-A",
286 )
287
288 # rule that generates the features.xml
289 # TODO - rename this
290 _onos_app_xml(
291 name = name + "-feature-xml",
292 app_name = app_name,
293 origin = origin,
294 version = version,
295 title = title,
296 category = category,
297 url = url,
298 feature_coords = feature_coords,
299 description = description,
300 apps = apps,
301 included_bundles = included_bundles,
302 excluded_bundles = excluded_bundles,
303 required_features = required_features,
304 mode = "-F",
305 )
306
307 # rule to generate the OAR file based on the app.xml, features.xml, and app jar file
308 _onos_oar(
309 name = name + "-oar",
Ray Milkey1ccab712018-06-12 16:30:02 -0700310 included_bundles = included_bundles,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700311 app_xml = Label(_local_label(name, "-app-xml")),
312 feature_xml = Label(_local_label(name, "-feature-xml")),
313 feature_xml_coords = feature_xml_coords,
314 visibility = visibility,
315 )