blob: d96000f41fd4c94d70b4d90f2664d479d48ee36e [file] [log] [blame]
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -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", "CORE_DEPS", "ONOS_YANG")
18load("//tools/build/bazel:osgi_java_library.bzl", "osgi_jar")
19load("//tools/build/bazel:onos_app.bzl", "onos_app")
20load(
21 "//tools/build/bazel:variables.bzl",
22 "APP_PREFIX",
23 "DEFAULT_APP_CATEGORY",
24 "ONOS_ARTIFACT_BASE",
25 "ONOS_GROUP_ID",
26 "ONOS_ORIGIN",
27 "ONOS_VERSION",
28)
29
30REGISTRATOR = \
31 "// Auto-generated code\n" + \
32 "package org.onosproject.model.registrator.impl;\n" + \
33 "\n" + \
34 "import org.onosproject.yang.AbstractYangModelRegistrator;\n" + \
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035 "import org.osgi.service.component.annotations.Component;\n" + \
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070036 "\n" + \
Ray Milkey86ad7bb2018-09-27 12:32:28 -070037 "@Component(immediate = true, service = YangModelRegistrator.class)\n" + \
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070038 "public class YangModelRegistrator extends AbstractYangModelRegistrator {\n" + \
39 " public YangModelRegistrator() {\n" + \
40 " super(YangModelRegistrator.class);\n" + \
41 " }\n" + \
42 "}"
43
44REGISTRATOR_FILE = "src/org/onosproject/model/registrator/impl/YangModelRegistrator.java"
45
46# Implementation of the YANG library rule
47def _yang_library_impl(ctx):
48 generated_sources = ctx.actions.declare_directory("generated-sources")
49
50 arguments = [
51 ctx.attr.model_id,
52 generated_sources.path,
53 ]
54 inputs = []
55
56 for dep in ctx.files.deps:
57 arguments += ["-d", dep.path]
58 inputs += [dep]
59
60 for source in ctx.files.srcs:
61 arguments += [source.path]
62 inputs += [source]
63
64 ctx.actions.run(
65 inputs = inputs,
66 outputs = [generated_sources],
67 arguments = arguments,
68 progress_message = "Compiling YANG library: %s" % ctx.attr.name,
69 executable = ctx.executable._yang_compiler,
70 )
71
Carmelo Casconed33d3b42019-06-18 12:12:36 -070072 java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo]
73 jar_path = "%s/bin/jar" % java_runtime.java_home
74
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070075 ctx.actions.run_shell(
76 inputs = [generated_sources],
77 outputs = [ctx.outputs.srcjar],
78 arguments = [
79 ctx.outputs.srcjar.path,
80 generated_sources.path,
81 ],
Carmelo Casconed33d3b42019-06-18 12:12:36 -070082 tools = java_runtime.files,
83 command = "%s cf $1 -C $2 src" % jar_path,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070084 progress_message = "Assembling YANG Java sources: %s" % ctx.attr.name,
85 )
86
87 ctx.actions.run_shell(
88 inputs = [generated_sources],
89 outputs = [ctx.outputs.schema],
90 arguments = [
91 ctx.outputs.schema.path,
Ray Milkey5063f5b2018-08-15 16:22:30 -070092 generated_sources.path,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070093 ],
Carmelo Casconed33d3b42019-06-18 12:12:36 -070094 tools = java_runtime.files,
95 command = "%s cf $1 -C $2 schema" % jar_path,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070096 progress_message = "Assembling YANG compiled schema: %s" % ctx.attr.name,
97 )
98
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -070099# Rule to generate YANG library from the specified set of YANG models.
100_yang_library = rule(
101 attrs = {
102 "deps": attr.label_list(),
103 "srcs": attr.label_list(allow_files = True),
104 "model_id": attr.string(),
105 "_yang_compiler": attr.label(
106 executable = True,
107 cfg = "host",
108 allow_files = True,
109 default = Label("//tools/build/bazel:onos_yang_compiler"),
110 ),
Carmelo Casconed33d3b42019-06-18 12:12:36 -0700111 "_jdk": attr.label(
112 default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
113 providers = [java_common.JavaRuntimeInfo],
114 ),
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700115 },
116 outputs = {
117 "srcjar": "model.srcjar",
118 "schema": "schema.jar",
119 },
120 fragments = ["java"],
121 implementation = _yang_library_impl,
122)
123
124def yang_library(
Ray Milkey5063f5b2018-08-15 16:22:30 -0700125 name = None,
126 deps = None,
127 yang_srcs = None,
128 java_srcs = None,
129 custom_registrator = False,
130 visibility = ["//visibility:public"]):
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700131 if name == None:
132 name = "onos-" + native.package_name().replace("/", "-")
133 if yang_srcs == None:
134 yang_srcs = native.glob(["src/main/yang/**/*.yang"])
135 if java_srcs == None:
136 java_srcs = native.glob(["src/main/java/**/*.java"])
137 if deps == None:
138 deps = []
139
140 deps += CORE_DEPS + ONOS_YANG + [
141 "@onos_yang_runtime//jar",
Ray Milkey5063f5b2018-08-15 16:22:30 -0700142 "//apps/yang:onos-apps-yang",
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700143 ]
144
145 # Generate the Java sources from YANG model
146 _yang_library(name = name + "-generate", model_id = name, deps = deps, srcs = yang_srcs)
147
148 srcs = [name + "-generate"]
149
150 if len(java_srcs):
Carmelo Casconed33d3b42019-06-18 12:12:36 -0700151 srcs.extend(java_srcs)
152 # FIXME (carmelo): is this genrule really needed?
153 # srcs += [name + "-srcjar"]
154 # native.genrule(
155 # name = name + "-srcjar",
156 # srcs = java_srcs,
157 # outs = [name + ".srcjar"],
158 # cmd = "$(location //external:jar) cf $(location %s.srcjar) $(SRCS)" % name,
159 # tools = [
160 # "//external:jar",
161 # ]
162 # )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700163
164 if not custom_registrator:
165 srcs += [name + "-registrator"]
166 native.genrule(
167 name = name + "-registrator",
168 outs = [REGISTRATOR_FILE],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700169 cmd = "echo '%s' > $(location %s)" % (REGISTRATOR, REGISTRATOR_FILE),
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700170 )
171
172 # Produce a Java library from the generated Java sources
Ray Milkey5063f5b2018-08-15 16:22:30 -0700173 osgi_jar(
174 name = name,
175 srcs = srcs,
176 resource_jars = [name + "-generate"],
177 deps = deps,
178 visibility = ["//visibility:public"],
179 suppress_errorprone = True,
180 suppress_checkstyle = True,
181 suppress_javadocs = True,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700182 )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700183
184def yang_model(
Ray Milkey5063f5b2018-08-15 16:22:30 -0700185 name = None,
186 app_name = None,
187 title = None,
188 description = None,
189 url = "http://onosproject.org/",
190 custom_registrator = False,
191 deps = None,
192 yang_srcs = None,
193 java_srcs = None,
194 required_apps = [],
195 visibility = ["//visibility:public"]):
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700196 if name == None:
197 name = "onos-" + native.package_name().replace("/", "-")
198
Ray Milkey5063f5b2018-08-15 16:22:30 -0700199 yang_library(
200 name = name,
201 deps = deps,
202 yang_srcs = yang_srcs,
203 java_srcs = java_srcs,
204 custom_registrator = custom_registrator,
205 visibility = ["//visibility:public"],
206 )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700207
208 onos_app(
209 app_name = app_name,
210 title = title,
211 description = description,
212 feature_name = name,
213 version = ONOS_VERSION,
214 url = url,
215 category = "Models",
Ray Milkey5063f5b2018-08-15 16:22:30 -0700216 included_bundles = [name],
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700217 required_apps = required_apps + ["org.onosproject.yang"],
218 visibility = ["//visibility:public"],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700219 )