blob: c5563b150cc0a0dacbcadc93f551505331933cb9 [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 = [
Thomas Vachuska8e022a92018-07-10 14:47:38 -070063 "-n", ctx.attr.feature_coords,
64 "-a", ctx.attr.app_name,
65 "-o", ctx.attr.origin,
66 "-c", ctx.attr.category,
67 "-u", ctx.attr.url,
68 "-v", ctx.attr.version,
69 "-t", ctx.attr.title,
70 "-D", ctx.attr.description,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070071 ]
72
Thomas Vachuska8e022a92018-07-10 14:47:38 -070073 for bundle in ctx.attr.included_bundles:
74 arguments += ["-b", maven_coordinates(bundle.label)]
75 for bundle in ctx.attr.excluded_bundles:
76 arguments += ["-e", maven_coordinates(bundle.label)]
77 for app in ctx.attr.required_apps:
Ray Milkey15053f02018-06-13 10:00:45 -070078 arguments += ["-d", app]
Ray Milkey1d52ddd2018-06-07 10:37:24 -070079
Thomas Vachuska8e022a92018-07-10 14:47:38 -070080 if ctx.attr.security != "":
81 arguments += ["-s", ctx.attr.security]
Ray Milkey1d52ddd2018-06-07 10:37:24 -070082
83 ctx.actions.run(
84 inputs = [],
85 outputs = [ctx.outputs.app_xml],
Thomas Vachuska8e022a92018-07-10 14:47:38 -070086 arguments = arguments + [ "-A", "-O", ctx.outputs.app_xml.path ],
87 progress_message = "Generating app.xml descriptor for app: %s" % ctx.attr.name,
88 executable = ctx.executable._onos_app_tools,
Ray Milkey1d52ddd2018-06-07 10:37:24 -070089 )
90
Thomas Vachuska8e022a92018-07-10 14:47:38 -070091# Implementation of the rule to build the feature.xml file for ONOS app
92def _onos_feature_xml_impl(ctx):
93 # Build-up arguments
94 arguments = [
95 "-n", ctx.attr.feature_coords,
96 "-a", ctx.attr.app_name,
97 "-u", ctx.attr.url,
98 "-v", ctx.attr.version,
99 "-t", ctx.attr.title,
100 "-D", ctx.attr.description,
101 ]
102
103 for bundle in ctx.attr.included_bundles:
104 arguments += ["-b", maven_coordinates(bundle.label)]
105 for feature in ctx.attr.required_features:
106 arguments += ["-f", feature]
107
108 ctx.actions.run(
109 inputs = [],
110 outputs = [ctx.outputs.feature_xml],
111 arguments = arguments + [ "-F", "-O", ctx.outputs.feature_xml.path ],
112 progress_message = "Generating feature.xml for app: %s" % ctx.attr.name,
113 executable = ctx.executable._onos_app_tools,
114 )
115
116
117# Rule to generate the ONOS app OAR file.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700118_onos_oar = rule(
119 attrs = {
120 "deps": attr.label_list(),
121 "version": attr.string(),
122 "package_name_root": attr.string(),
123 "source": attr.label(),
124 "app_xml": attr.label(),
125 "feature_xml": attr.label(),
126 "feature_xml_coords": attr.string(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700127 "included_bundles": attr.label_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700128 "_onos_app_bundler": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700129 executable = True,
130 cfg = "host",
131 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700132 default = Label("//tools/build/bazel:onos_app_bundler"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700133 ),
134 },
135 outputs = {
136 "app_oar": "%{name}.oar",
137 },
138 implementation = _onos_oar_impl,
139)
140
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700141# Rule to generate app.xml descriptor for an ONOS application.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700142_onos_app_xml = rule(
143 attrs = {
144 "app_name": attr.string(),
145 "origin": attr.string(),
146 "version": attr.string(),
147 "title": attr.string(),
148 "category": attr.string(),
149 "url": attr.string(),
150 "feature_coords": attr.string(),
151 "description": attr.string(),
152 "apps": attr.label_list(),
153 "included_bundles": attr.label_list(),
154 "excluded_bundles": attr.label_list(),
Ray Milkey1ccab712018-06-12 16:30:02 -0700155 "required_apps": attr.string_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700156 "security": attr.string(),
157 "_onos_app_tools": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700158 executable = True,
159 cfg = "host",
160 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700161 default = Label("//tools/build/bazel:onos_app_tools"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700162 ),
163 },
164 outputs = {
165 "app_xml": "%{name}.xml",
166 },
167 implementation = _onos_app_xml_impl,
168)
169
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700170# Rule to generate feature.xml descriptor for an ONOS application.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700171_onos_feature_xml = rule(
172 attrs = {
173 "app_name": attr.string(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700174 "version": attr.string(),
175 "title": attr.string(),
176 "category": attr.string(),
177 "url": attr.string(),
178 "feature_coords": attr.string(),
179 "description": attr.string(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700180 "included_bundles": attr.label_list(),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700181 "required_features": attr.string_list(),
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700182 "_onos_app_tools": attr.label(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700183 executable = True,
184 cfg = "host",
185 allow_files = True,
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700186 default = Label("//tools/build/bazel:onos_app_tools"),
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700187 ),
188 },
189 outputs = {
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700190 "feature_xml": "%{name}.xml",
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700191 },
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700192 implementation = _onos_feature_xml_impl,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700193)
194
195def _basename(path):
196 paths = path.split("/")
197 return paths[len(paths) - 1]
198
199def _get_base_path():
200 return native.package_name()
201
202def _get_name():
203 base_path = _get_base_path()
204 return ONOS_ARTIFACT_BASE + base_path.replace("/", "-")
205
206def _get_app_name():
207 base_path = _get_base_path()
208 return APP_PREFIX + _basename(base_path)
209
210def _local_label(name, suffix):
211 base_label_name = "//" + native.package_name() + ":"
212 return base_label_name + name + suffix
213
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700214# Macro to build an ONOS application OAR file.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700215def onos_app(
216 app_name = None,
217 name = None,
218 title = None,
219 version = ONOS_VERSION,
220 origin = ONOS_ORIGIN,
221 category = DEFAULT_APP_CATEGORY,
222 url = None,
223 description = None,
224 feature_coords = None,
225 required_features = ["onos-api"],
226 required_apps = [],
227 included_bundles = None,
228 excluded_bundles = [],
229 visibility = ["//visibility:public"],
230 security = None,
231 **kwargs):
232 if name == None:
233 name = _get_name()
234
235 if app_name == None:
236 app_name = _get_app_name()
237
238 maven_coords = "%s:%s:oar:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Thomas Vachuska1ae26c62018-07-09 13:40:16 -0700239 feature_xml_coords = "mvn:%s:%s:xml:features:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700240
241 if title == None:
242 print("Missing title for %s" % _get_name())
243 title = _get_app_name()
244
245 if included_bundles == None:
Ray Milkey1ccab712018-06-12 16:30:02 -0700246 target = _local_label(name, "")
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700247 included_bundles = [target]
248
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700249 if not feature_coords:
Thomas Vachuska1ae26c62018-07-09 13:40:16 -0700250 feature_coords = "mvn:%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700251
252 # TODO - intra app dependecies
253 apps = []
254
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700255 # Generate the app.xml file
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700256 _onos_app_xml(
257 name = name + "-app-xml",
258 app_name = app_name,
259 origin = origin,
260 version = version,
261 title = title,
262 category = category,
263 url = url,
264 feature_coords = feature_coords,
265 description = description,
266 apps = apps,
267 included_bundles = included_bundles,
268 excluded_bundles = excluded_bundles,
Ray Milkey1ccab712018-06-12 16:30:02 -0700269 required_apps = required_apps,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700270 )
271
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700272 # Generate feature.xml file
273 _onos_feature_xml(
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700274 name = name + "-feature-xml",
275 app_name = app_name,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700276 version = version,
277 title = title,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700278 feature_coords = feature_coords,
279 description = description,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700280 included_bundles = included_bundles,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700281 required_features = required_features,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700282 )
283
Thomas Vachuska8e022a92018-07-10 14:47:38 -0700284 # Generate the OAR file based on the app.xml, features.xml, and specified
285 # bundles to be included.
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700286 _onos_oar(
287 name = name + "-oar",
Ray Milkey1ccab712018-06-12 16:30:02 -0700288 included_bundles = included_bundles,
Ray Milkey1d52ddd2018-06-07 10:37:24 -0700289 app_xml = Label(_local_label(name, "-app-xml")),
290 feature_xml = Label(_local_label(name, "-feature-xml")),
291 feature_xml_coords = feature_xml_coords,
292 visibility = visibility,
293 )