Adding Bazel support for YANG models.
Change-Id: Icdb620bdb99ae9eeb042a84a8ca7563588c813c1
diff --git a/tools/build/bazel/BUILD b/tools/build/bazel/BUILD
index 09dd187..62534a0 100644
--- a/tools/build/bazel/BUILD
+++ b/tools/build/bazel/BUILD
@@ -18,3 +18,10 @@
main = "onos_app_bundler.py",
visibility = ["//visibility:public"],
)
+
+java_binary(
+ name = "onos_yang_compiler",
+ main_class = "org.onosproject.yang.compiler.main.YangCompilerMain",
+ visibility = ["//visibility:public"],
+ runtime_deps = ["@onos_yang_compiler_main//jar"],
+)
\ No newline at end of file
diff --git a/tools/build/bazel/generate_workspace.bzl b/tools/build/bazel/generate_workspace.bzl
index 0e7c50f..93a5182 100644
--- a/tools/build/bazel/generate_workspace.bzl
+++ b/tools/build/bazel/generate_workspace.bzl
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Wed, 11 Jul 2018 05:19:34 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Wed, 11 Jul 2018 21:35:55 GMT. Do not edit this file manually. *****
# ***** Use onos-lib-gen *****
load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION")
@@ -926,6 +926,12 @@
)
native.maven_jar(
+ name = "onos_yang_compiler_main",
+ artifact = "org.onosproject:onos-yang-compiler-main:2.5",
+ sha1 = "8048db59dab2c813cfbc3f7c176eb12b1c8b2548",
+ )
+
+ native.maven_jar(
name = "onos_yang_runtime",
artifact = "org.onosproject:onos-yang-runtime:2.5",
sha1 = "9d621709f34fd7da4d1954d868ac0eddeba725d2",
@@ -1917,6 +1923,12 @@
)
native.java_library(
+ name = "onos_yang_compiler_main",
+ visibility = ["//visibility:public"],
+ exports = ["@onos_yang_compiler_main//jar"],
+ )
+
+ native.java_library(
name = "onos_yang_runtime",
visibility = ["//visibility:public"],
exports = ["@onos_yang_runtime//jar"],
@@ -2249,6 +2261,7 @@
artifact_map[str(Label("@snakeyaml//jar"))] = "mvn:org.yaml:snakeyaml:jar:1.15"
artifact_map[str(Label("@onos_yang_model//jar"))] = "mvn:org.onosproject:onos-yang-model:jar:2.5"
artifact_map[str(Label("@onos_yang_compiler_api//jar"))] = "mvn:org.onosproject:onos-yang-compiler-api:jar:2.5"
+artifact_map[str(Label("@onos_yang_compiler_main//jar"))] = "mvn:org.onosproject:onos-yang-compiler-main:jar:2.5"
artifact_map[str(Label("@onos_yang_runtime//jar"))] = "mvn:org.onosproject:onos-yang-runtime:jar:2.5"
artifact_map[str(Label("@onos_yang_serializers_json//jar"))] = "mvn:org.onosproject:onos-yang-serializers-json:jar:2.5"
artifact_map[str(Label("@onos_yang_serializers_xml//jar"))] = "mvn:org.onosproject:onos-yang-serializers-xml:jar:2.5"
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index b75b367..becc270 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -365,6 +365,7 @@
srcs = None,
resources_root = None,
resources = None,
+ resource_jars = [],
include_resources = {},
test_srcs = None,
exclude_tests = None,
@@ -436,7 +437,12 @@
native_resources.append(name + "_swagger_json")
# compile the Java code
- native.java_library(name = name + "-native", srcs = native_srcs, resources = native_resources, deps = deps, visibility = visibility)
+ if len(resource_jars) > 0:
+ native.java_library(name = name + "-native", srcs = native_srcs, resource_jars = resource_jars,
+ deps = deps, visibility = visibility)
+ else:
+ native.java_library(name = name + "-native", srcs = native_srcs, resources = native_resources,
+ deps = deps, visibility = visibility)
_bnd(
name = name,
@@ -500,6 +506,7 @@
srcs = None,
resources_root = None,
resources = None,
+ resource_jars = [],
include_resources = {},
visibility = ["//visibility:public"],
version = ONOS_VERSION,
@@ -521,6 +528,7 @@
srcs = srcs,
resources = resources,
resources_root = resources_root,
+ resource_jars = resource_jars,
test_srcs = [],
exclude_tests = [],
test_resources = [],
diff --git a/tools/build/bazel/yang.bzl b/tools/build/bazel/yang.bzl
new file mode 100644
index 0000000..c0d77fd
--- /dev/null
+++ b/tools/build/bazel/yang.bzl
@@ -0,0 +1,197 @@
+"""
+ Copyright 2018-present Open Networking Foundation
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+"""
+
+load("//tools/build/bazel:generate_workspace.bzl", "CORE_DEPS", "ONOS_YANG")
+load("//tools/build/bazel:osgi_java_library.bzl", "osgi_jar")
+load("//tools/build/bazel:onos_app.bzl", "onos_app")
+load(
+ "//tools/build/bazel:variables.bzl",
+ "APP_PREFIX",
+ "DEFAULT_APP_CATEGORY",
+ "ONOS_ARTIFACT_BASE",
+ "ONOS_GROUP_ID",
+ "ONOS_ORIGIN",
+ "ONOS_VERSION",
+)
+
+REGISTRATOR = \
+ "// Auto-generated code\n" + \
+ "package org.onosproject.model.registrator.impl;\n" + \
+ "\n" + \
+ "import org.onosproject.yang.AbstractYangModelRegistrator;\n" + \
+ "import org.apache.felix.scr.annotations.Component;\n" + \
+ "\n" + \
+ "@Component(immediate = true)\n" + \
+ "public class YangModelRegistrator extends AbstractYangModelRegistrator {\n" + \
+ " public YangModelRegistrator() {\n" + \
+ " super(YangModelRegistrator.class);\n" + \
+ " }\n" + \
+ "}"
+
+REGISTRATOR_FILE = "src/org/onosproject/model/registrator/impl/YangModelRegistrator.java"
+
+# Implementation of the YANG library rule
+def _yang_library_impl(ctx):
+ generated_sources = ctx.actions.declare_directory("generated-sources")
+
+ arguments = [
+ ctx.attr.model_id,
+ generated_sources.path,
+ ]
+ inputs = []
+
+ for dep in ctx.files.deps:
+ arguments += ["-d", dep.path]
+ inputs += [dep]
+
+ for source in ctx.files.srcs:
+ arguments += [source.path]
+ inputs += [source]
+
+ ctx.actions.run(
+ inputs = inputs,
+ outputs = [generated_sources],
+ arguments = arguments,
+ progress_message = "Compiling YANG library: %s" % ctx.attr.name,
+ executable = ctx.executable._yang_compiler,
+ )
+
+ ctx.actions.run_shell(
+ inputs = [generated_sources],
+ outputs = [ctx.outputs.srcjar],
+ arguments = [
+ ctx.outputs.srcjar.path,
+ generated_sources.path,
+ ],
+ command = "jar cf $1 -C $2 src",
+ progress_message = "Assembling YANG Java sources: %s" % ctx.attr.name,
+ )
+
+ ctx.actions.run_shell(
+ inputs = [generated_sources],
+ outputs = [ctx.outputs.schema],
+ arguments = [
+ ctx.outputs.schema.path,
+ generated_sources.path
+ ],
+ command = "jar cf $1 -C $2 schema",
+ progress_message = "Assembling YANG compiled schema: %s" % ctx.attr.name,
+ )
+
+
+# Rule to generate YANG library from the specified set of YANG models.
+_yang_library = rule(
+ attrs = {
+ "deps": attr.label_list(),
+ "srcs": attr.label_list(allow_files = True),
+ "model_id": attr.string(),
+ "_yang_compiler": attr.label(
+ executable = True,
+ cfg = "host",
+ allow_files = True,
+ default = Label("//tools/build/bazel:onos_yang_compiler"),
+ ),
+ },
+ outputs = {
+ "srcjar": "model.srcjar",
+ "schema": "schema.jar",
+ },
+ fragments = ["java"],
+ implementation = _yang_library_impl,
+)
+
+def yang_library(
+ name = None,
+ deps = None,
+ yang_srcs = None,
+ java_srcs = None,
+ custom_registrator = False,
+ visibility = ["//visibility:public"]):
+
+ if name == None:
+ name = "onos-" + native.package_name().replace("/", "-")
+ if yang_srcs == None:
+ yang_srcs = native.glob(["src/main/yang/**/*.yang"])
+ if java_srcs == None:
+ java_srcs = native.glob(["src/main/java/**/*.java"])
+ if deps == None:
+ deps = []
+
+ deps += CORE_DEPS + ONOS_YANG + [
+ "@onos_yang_runtime//jar",
+ "//apps/yang:onos-apps-yang"
+ ]
+
+ # Generate the Java sources from YANG model
+ _yang_library(name = name + "-generate", model_id = name, deps = deps, srcs = yang_srcs)
+
+ srcs = [name + "-generate"]
+
+ if len(java_srcs):
+ srcs += [name + "-srcjar"]
+ native.genrule(
+ name = name + "-srcjar",
+ srcs = java_srcs,
+ outs = [name + ".srcjar"],
+ cmd = "jar cf $(location %s.srcjar) $(SRCS)" % name
+ )
+
+ if not custom_registrator:
+ srcs += [name + "-registrator"]
+ native.genrule(
+ name = name + "-registrator",
+ outs = [REGISTRATOR_FILE],
+ cmd = "echo '%s' > $(location %s)" % (REGISTRATOR, REGISTRATOR_FILE)
+ )
+
+ # Produce a Java library from the generated Java sources
+ osgi_jar(name = name, srcs = srcs,
+ resource_jars = [name + "-generate"], deps = deps,
+ visibility = ["//visibility:public"])
+
+def yang_model(
+ name = None,
+ app_name = None,
+ title = None,
+ description = None,
+ url = "http://onosproject.org/",
+ custom_registrator = False,
+ deps = None,
+ yang_srcs = None,
+ java_srcs = None,
+ required_apps = [],
+ visibility = ["//visibility:public"]):
+
+ if name == None:
+ name = "onos-" + native.package_name().replace("/", "-")
+
+ yang_library(name = name, deps = deps,
+ yang_srcs = yang_srcs, java_srcs = java_srcs,
+ custom_registrator = custom_registrator,
+ visibility = ["//visibility:public"])
+
+ onos_app(
+ app_name = app_name,
+ title = title,
+ description = description,
+ feature_name = name,
+ version = ONOS_VERSION,
+ url = url,
+ category = "Models",
+ included_bundles = [ name ],
+ required_apps = required_apps + ["org.onosproject.yang"],
+ visibility = ["//visibility:public"],
+ )
\ No newline at end of file