blob: bb07140c34dfbb6ded45b762eb3da5675059c007 [file] [log] [blame]
Ray Milkey3275ae82018-05-29 15:35:36 -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
Ray Milkey7dac7da2017-08-01 16:56:05 -070017load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST")
18load("//tools/build/bazel:variables.bzl", "ONOS_VERSION")
19load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules")
20
Ray Milkey32ea35c2018-06-06 15:28:07 -070021def _all_java_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070022 return native.glob(["src/main/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070023
Ray Milkey32ea35c2018-06-06 15:28:07 -070024def _all_java_test_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070025 return native.glob(["src/test/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070026
Ray Milkey32ea35c2018-06-06 15:28:07 -070027def _all_test_resources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070028 return native.glob(["src/test/resources/**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070029
Ray Milkey32ea35c2018-06-06 15:28:07 -070030def _all_resources(resources_root):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070031 if resources_root == None:
32 return native.glob(["src/main/resources/**"])
33 else:
34 return native.glob([resources_root + "**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070035
Ray Milkey25747d82018-06-13 14:12:51 -070036def _webapp():
37 return native.glob(["src/main/webapp/**"])
38
Ray Milkey275af2c2018-06-15 13:05:08 -070039"""
40 Implementation of the rule to call bnd to make an OSGI jar file
41"""
Ray Milkey7dac7da2017-08-01 16:56:05 -070042def _bnd_impl(ctx):
Ray Milkey3275ae82018-05-29 15:35:36 -070043 if (len(ctx.files.source) == 1):
44 input_file = ctx.files.source[0]
45 else:
46 # this is a list of inputs. The one we want is the last one
47 # in the list that isn't a source jar
48 for file in reversed(ctx.files.source):
49 if ("-src" in file.path):
50 continue
51 else:
52 input_file = file
53 break
54
55 jar = input_file.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070056 output = ctx.outputs.osgi_jar.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070057 name = ctx.attr.source.label.name
Ray Milkey275af2c2018-06-15 13:05:08 -070058 group = ctx.attr.group
Ray Milkey0bcdfd12018-05-23 14:07:19 -070059 version = ctx.attr.version
60 license = ""
Ray Milkey12ae6ca2018-06-11 15:34:30 -070061 import_packages = ctx.attr.import_packages
Ray Milkey0bcdfd12018-05-23 14:07:19 -070062 exportPackages = "*"
63 includeResources = ""
Ray Milkey25747d82018-06-13 14:12:51 -070064 web_context = ctx.attr.web_context
65 if web_context == None or web_context == "":
66 web_context = "NONE"
67 web_xml = ctx.attr.web_xml
Ray Milkey0bcdfd12018-05-23 14:07:19 -070068 dynamicimportPackages = ""
Ray Milkey3275ae82018-05-29 15:35:36 -070069 cp = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070070
Ray Milkey3275ae82018-05-29 15:35:36 -070071 inputDependencies = [input_file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070072
Ray Milkey0bcdfd12018-05-23 14:07:19 -070073 # determine the dependencies and build the class path
74 for dep in ctx.attr.deps:
Ray Milkey25b785a2018-06-12 09:59:14 -070075 if java_common.provider in dep:
76 file = dep.files.to_list()[0]
Ray Milkey472d8392018-05-23 17:06:51 -070077
Ray Milkey25b785a2018-06-12 09:59:14 -070078 if cp:
79 cp += ":"
80 cp += file.path
81 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070082
Ray Milkey0bcdfd12018-05-23 14:07:19 -070083 # extract the class files for use by bnd
Ray Milkey3275ae82018-05-29 15:35:36 -070084 classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-"))
Ray Milkey0bcdfd12018-05-23 14:07:19 -070085 classesPath = classes.path
86 jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
87 ctx.actions.run_shell(
88 inputs = inputDependencies,
89 outputs = [classes],
90 command = jarCommand,
91 progress_message = "Expanding jar file: %s" % jar,
92 )
93 inputDependencies += [classes]
Ray Milkey25747d82018-06-13 14:12:51 -070094 web_xml_root_path = ""
95 if len(web_xml) != 0:
96 web_xml_root = web_xml[0].files.to_list()[0]
97 inputDependencies += [web_xml_root]
98 web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "")
Ray Milkey7dac7da2017-08-01 16:56:05 -070099
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700100 # call bnd to make the OSGI jar file
101 arguments = [
102 jar,
103 output,
104 cp,
105 name,
106 group,
107 version,
108 license,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700109 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700110 exportPackages,
111 includeResources,
Ray Milkey25747d82018-06-13 14:12:51 -0700112 web_context,
113 web_xml_root_path,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700114 dynamicimportPackages,
115 classesPath,
116 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700117
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700118 ctx.actions.run(
119 inputs = inputDependencies,
120 outputs = [ctx.outputs.osgi_jar],
121 arguments = arguments,
122 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
123 executable = ctx.executable._bnd_exe,
124 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700125
Ray Milkey25b785a2018-06-12 09:59:14 -0700126 deps = []
127 if java_common.provider in ctx.attr.source:
128 deps.append(ctx.attr.source[java_common.provider])
129 deps_provider = java_common.merge(deps)
130 return struct(
Ray Milkey15053f02018-06-13 10:00:45 -0700131 providers = [deps_provider],
Ray Milkey25b785a2018-06-12 09:59:14 -0700132 )
133
Ray Milkey275af2c2018-06-15 13:05:08 -0700134"""
135 Rule definition for calling bnd to make an OSGi jar file.
136"""
Ray Milkey32ea35c2018-06-06 15:28:07 -0700137_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700138 attrs = {
139 "deps": attr.label_list(),
140 "version": attr.string(),
Ray Milkey275af2c2018-06-15 13:05:08 -0700141 "group": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700142 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700143 "import_packages": attr.string(),
Ray Milkey25747d82018-06-13 14:12:51 -0700144 "web_context": attr.string(),
145 "web_xml": attr.label_list(allow_files = True),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700146 "_bnd_exe": attr.label(
147 executable = True,
148 cfg = "host",
149 allow_files = True,
150 default = Label("//utils/osgiwrap:osgi-jar"),
151 ),
152 },
153 fragments = ["java"],
154 outputs = {
155 "osgi_jar": "lib%{name}.jar",
156 },
157 implementation = _bnd_impl,
158)
159
Ray Milkey275af2c2018-06-15 13:05:08 -0700160"""
161 Implementation of the rule to call swagger generator to create the registrator java class source
162"""
163def _swagger_java_impl(ctx):
164 api_title = ctx.attr.api_title
165 api_version = ctx.attr.api_version
166 api_description = ctx.attr.api_description
167 api_package = ctx.attr.api_package
168 web_context = ctx.attr.web_context
169
170 output_java = ctx.outputs.swagger_java.path
171 output_dir = output_java [:output_java.find("generated-sources")]
172
173 package_name = ctx.attr.package_name
174
175 srcs_arg = ""
176 resources_arg = ""
177 input_dependencies = []
178
179 for file in ctx.files.srcs:
180 srcs_arg += file.path + ","
181 input_dependencies.append(file)
182
183 for resource in resources_arg:
184 resources_arg += resource.path + ","
185
186 # call swagger generator to make the swagger JSON and java files
187 arguments = [
188 srcs_arg,
189 resources_arg,
190 "",
191 package_name + "/src/main/resources",
192 output_dir,
193 output_dir,
194 web_context,
195 api_title,
196 api_version,
197 api_package,
198 api_description,
199 ]
200
201 ctx.actions.run(
202 inputs = ctx.files.srcs,
203 outputs = [ctx.outputs.swagger_java],
204 arguments = arguments,
205 progress_message = "Running swagger generator on: %s" % ctx.attr.name,
206 executable = ctx.executable._swagger_generator_exe,
207 )
208
209"""
210Implementation of the rule to call swagger generator for swagger.json file
211"""
212def _swagger_json_impl(ctx):
213 api_title = ctx.attr.api_title
214 api_version = ctx.attr.api_version
215 api_description = ctx.attr.api_description
216 api_package = ctx.attr.api_package
217 web_context = ctx.attr.web_context
218
219 output_json = ctx.outputs.swagger_json.path
220 output_dir = output_json[:output_json.find("swagger.json")]
221
222 package_name = ctx.attr.package_name
223
224 srcs_arg = ""
225 resources_arg = ""
226 input_dependencies = []
227
228 for file in ctx.files.srcs:
229 srcs_arg += file.path + ","
230 input_dependencies.append(file)
231
232 for resource in resources_arg:
233 resources_arg += resource.path + ","
234
235 # call swagger generator to make the swagger JSON and java files
236 arguments = [
237 srcs_arg,
238 resources_arg,
239 "",
240 package_name + "/src/main/resources",
241 output_dir,
242 output_dir,
243 web_context,
244 api_title,
245 api_version,
246 api_package,
247 api_description,
248 ]
249
250 ctx.actions.run(
251 inputs = ctx.files.srcs,
252 outputs = [ctx.outputs.swagger_json],
253 arguments = arguments,
254 progress_message = "Running swagger generator on: %s" % ctx.attr.name,
255 executable = ctx.executable._swagger_generator_exe,
256 )
257
258"""
259 Rule definition to call swagger generator to create the registrator java class source
260"""
261_swagger_java = rule(
262 attrs = {
263 "srcs": attr.label_list(allow_files = True),
264 "package_name": attr.string(),
265 "api_title": attr.string(),
266 "api_version": attr.string(),
267 "api_description": attr.string(),
268 "api_package": attr.string(),
269 "web_context": attr.string(),
270 "_swagger_generator_exe": attr.label(
271 executable = True,
272 cfg = "host",
273 allow_files = True,
274 default = Label("//tools/build/buck-plugin:swagger_generator"),
275 ),
276 "swagger_java": attr.output(),
277 },
278 fragments = ["java"],
279 implementation = _swagger_java_impl,
280)
281
282"""
283 Rule definition to call swagger generator to create the swagger JSON
284"""
285_swagger_json = rule(
286 attrs = {
287 "srcs": attr.label_list(allow_files = True),
288 "package_name": attr.string(),
289 "api_title": attr.string(),
290 "api_version": attr.string(),
291 "api_description": attr.string(),
292 "api_package": attr.string(),
293 "web_context": attr.string(),
294 "_swagger_generator_exe": attr.label(
295 executable = True,
296 cfg = "host",
297 allow_files = True,
298 default = Label("//tools/build/buck-plugin:swagger_generator"),
299 ),
300 "swagger_json": attr.output(),
301 },
302 fragments = ["java"],
303 implementation = _swagger_json_impl,
304)
305
306"""
307 Converts a jar file to an OSGI compatible jar file.
308
309 Args:
310 name: name of the rule to create the OSGI jar file - required
311 jar: jar file to convert - required target
312 deps: dependencies needed by the jar file - required list of targets
313 version: Version of the generated jar file. Optional, defaults to the current ONOS version
314 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
315 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
316 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to private
317"""
Ray Milkey25747d82018-06-13 14:12:51 -0700318def wrapped_osgi_jar(
319 name,
320 jar,
321 deps,
322 version = ONOS_VERSION,
Ray Milkey275af2c2018-06-15 13:05:08 -0700323 group = "org.onosproject",
Ray Milkey25747d82018-06-13 14:12:51 -0700324 import_packages = "*",
Ray Milkey25747d82018-06-13 14:12:51 -0700325 visibility = ["//visibility:private"]):
326 _bnd(
327 name = name,
328 source = jar,
329 deps = deps,
330 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700331 group = group,
Ray Milkey25747d82018-06-13 14:12:51 -0700332 visibility = visibility,
333 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700334 web_xml = None,
Ray Milkey25747d82018-06-13 14:12:51 -0700335 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700336
Ray Milkey275af2c2018-06-15 13:05:08 -0700337"""
338 Creates an OSGI jar and test jar file from a set of source and test files.
339 See osgi_jar() for a description of shared parameters.
340 Args:
341 test_srcs: Test source file(s) to compile. Optional list of targets, defaults to src/test/java/**/*.java
342 test_deps: Dependencies for the test jar. Optional list of targets, defaults to a common set of dependencies
343 test_resources: Resources to include in the test jar. Optional list of targets, defaults to src/test/resources/**
344 exclude_tests: Tests that should not be run. Useful for excluding things like test files without any @Test methods.
345 Optional ist of targets, defaults to []
346"""
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700347def osgi_jar_with_tests(
348 name = None,
349 deps = None,
350 test_deps = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700351 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700352 srcs = None,
353 resources_root = None,
354 resources = None,
355 test_srcs = None,
356 exclude_tests = None,
357 test_resources = None,
358 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700359 version = ONOS_VERSION,
Ray Milkey25747d82018-06-13 14:12:51 -0700360 web_context = None,
361 api_title = "",
362 api_version = "",
363 api_description = "",
364 api_package = "",
Ray Milkey15053f02018-06-13 10:00:45 -0700365 import_packages = None):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700366 if name == None:
367 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700368 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700369 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700370 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700371 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700372 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700373 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700374 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700375 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700376 if exclude_tests == None:
377 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700378 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700379 deps = COMPILE
380 if test_deps == None:
381 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700382 if import_packages == None:
383 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700384 tests_name = name + "-tests"
385 tests_jar_deps = list(depset(deps + test_deps)) + [name]
386 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey25747d82018-06-13 14:12:51 -0700387 web_xml = _webapp()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700388
Ray Milkey275af2c2018-06-15 13:05:08 -0700389 native_srcs = srcs
390 native_resources = resources
391 if web_context != None and api_title != None and len(resources) != 0:
392 # generate Swagger files if needed
393 _swagger_java(
394 name = name + "_swagger_java",
395 srcs = srcs + resources,
396 package_name = native.package_name(),
397 api_title = api_title,
398 api_version = api_version,
399 api_description = api_description,
400 web_context = web_context,
401 api_package = api_package,
402 swagger_java = ("src/main/resources/apidoc/generated-sources/" +
403 api_package.replace(".", "/") +
404 "/ApiDocRegistrator.java").replace("//", "/"),
405 )
406 _swagger_json(
407 name = name + "_swagger_json",
408 srcs = srcs + resources,
409 package_name = native.package_name(),
410 api_title = api_title,
411 api_version = api_version,
412 api_description = api_description,
413 web_context = web_context,
414 api_package = api_package,
415 swagger_json = "src/main/resources/apidoc/swagger.json",
416 )
417 native_resources = []
418 for r in resources:
419 if not "definitions" in r:
420 native_resources.append(r)
421 native_srcs = srcs + [ name + "_swagger_java" ]
422 native_resources.append(name + "_swagger_json");
423
Ray Milkey25747d82018-06-13 14:12:51 -0700424 # compile the Java code
Ray Milkey275af2c2018-06-15 13:05:08 -0700425 native.java_library(name = name + "-native", srcs = native_srcs, resources = native_resources, deps = deps, visibility = visibility)
Ray Milkey25747d82018-06-13 14:12:51 -0700426
Ray Milkey32ea35c2018-06-06 15:28:07 -0700427 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700428 name = name,
429 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700430 deps = deps,
431 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700432 group = group,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700433 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700434 import_packages = import_packages,
Ray Milkey25747d82018-06-13 14:12:51 -0700435 web_context = web_context,
436 web_xml = web_xml,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700437 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700438 if test_srcs != []:
439 native.java_library(
440 name = tests_name,
441 srcs = test_srcs,
442 resources = test_resources,
443 deps = tests_jar_deps,
444 visibility = visibility,
445 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700446
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700447 generate_test_rules(
448 name = name + "-tests-gen",
449 test_files = test_srcs,
450 exclude_tests = exclude_tests,
451 deps = all_test_deps,
452 )
453
Ray Milkey275af2c2018-06-15 13:05:08 -0700454"""
455 Creates an OSGI jar file from a set of source files.
456
457 Args:
458 name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree.
459 For example apps/mcast/app becomes onos-apps-mcast-app
460 deps: Dependencies of the generated jar file. Expressed as a list of targets
461 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
462 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
463 srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java
464 resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources
465 resources: Resources to include in the jar file. Optional list of targets, defaults to all files beneath resources_root
466 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public
467 version: Version of the generated jar file. Optional, defaults to the current ONOS version
468 web_context: Web context for a WAB file if needed. Only needed if the jar file provides a REST API. Optional string
469 api_title: Swagger API title. Optional string, only used if the jar file provides a REST API and has swagger annotations
470 api_version: Swagger API version. Optional string, only used if the jar file provides a REST API and has swagger annotations
471 api_description: Swagger API description. Optional string, only used if the jar file provides a REST API and has swagger annotations
472 api_package: Swagger API package name. Optional string, only used if the jar file provides a REST API and has swagger annotations
473"""
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700474def osgi_jar(
475 name = None,
476 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700477 import_packages = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700478 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700479 srcs = None,
480 resources_root = None,
481 resources = None,
482 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700483 version = ONOS_VERSION,
Ray Milkey25747d82018-06-13 14:12:51 -0700484 web_context = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700485 api_title = "",
486 api_version = "",
487 api_description = "",
Ray Milkey15053f02018-06-13 10:00:45 -0700488 api_package = ""):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700489 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700490 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700491 if deps == None:
492 deps = COMPILE
493
494 osgi_jar_with_tests(
495 name = name,
496 deps = deps,
497 test_deps = [],
Ray Milkey275af2c2018-06-15 13:05:08 -0700498 group = group,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700499 srcs = srcs,
500 resources = resources,
501 resources_root = resources_root,
502 test_srcs = [],
503 exclude_tests = [],
504 test_resources = [],
505 visibility = visibility,
506 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700507 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700508 api_title = api_title,
509 api_version = api_version,
510 api_description = api_description,
511 api_package = api_package,
Ray Milkey25747d82018-06-13 14:12:51 -0700512 web_context = web_context,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700513 )