blob: 3b123b253913e537f336156c6e918ae4cff2e21d [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" + \
35 "import org.apache.felix.scr.annotations.Component;\n" + \
36 "\n" + \
37 "@Component(immediate = true)\n" + \
38 "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
72 ctx.actions.run_shell(
73 inputs = [generated_sources],
74 outputs = [ctx.outputs.srcjar],
75 arguments = [
76 ctx.outputs.srcjar.path,
77 generated_sources.path,
78 ],
79 command = "jar cf $1 -C $2 src",
80 progress_message = "Assembling YANG Java sources: %s" % ctx.attr.name,
81 )
82
83 ctx.actions.run_shell(
84 inputs = [generated_sources],
85 outputs = [ctx.outputs.schema],
86 arguments = [
87 ctx.outputs.schema.path,
88 generated_sources.path
89 ],
90 command = "jar cf $1 -C $2 schema",
91 progress_message = "Assembling YANG compiled schema: %s" % ctx.attr.name,
92 )
93
94
95# Rule to generate YANG library from the specified set of YANG models.
96_yang_library = rule(
97 attrs = {
98 "deps": attr.label_list(),
99 "srcs": attr.label_list(allow_files = True),
100 "model_id": attr.string(),
101 "_yang_compiler": attr.label(
102 executable = True,
103 cfg = "host",
104 allow_files = True,
105 default = Label("//tools/build/bazel:onos_yang_compiler"),
106 ),
107 },
108 outputs = {
109 "srcjar": "model.srcjar",
110 "schema": "schema.jar",
111 },
112 fragments = ["java"],
113 implementation = _yang_library_impl,
114)
115
116def yang_library(
117 name = None,
118 deps = None,
119 yang_srcs = None,
120 java_srcs = None,
121 custom_registrator = False,
122 visibility = ["//visibility:public"]):
123
124 if name == None:
125 name = "onos-" + native.package_name().replace("/", "-")
126 if yang_srcs == None:
127 yang_srcs = native.glob(["src/main/yang/**/*.yang"])
128 if java_srcs == None:
129 java_srcs = native.glob(["src/main/java/**/*.java"])
130 if deps == None:
131 deps = []
132
133 deps += CORE_DEPS + ONOS_YANG + [
134 "@onos_yang_runtime//jar",
135 "//apps/yang:onos-apps-yang"
136 ]
137
138 # Generate the Java sources from YANG model
139 _yang_library(name = name + "-generate", model_id = name, deps = deps, srcs = yang_srcs)
140
141 srcs = [name + "-generate"]
142
143 if len(java_srcs):
144 srcs += [name + "-srcjar"]
145 native.genrule(
146 name = name + "-srcjar",
147 srcs = java_srcs,
148 outs = [name + ".srcjar"],
149 cmd = "jar cf $(location %s.srcjar) $(SRCS)" % name
150 )
151
152 if not custom_registrator:
153 srcs += [name + "-registrator"]
154 native.genrule(
155 name = name + "-registrator",
156 outs = [REGISTRATOR_FILE],
157 cmd = "echo '%s' > $(location %s)" % (REGISTRATOR, REGISTRATOR_FILE)
158 )
159
160 # Produce a Java library from the generated Java sources
161 osgi_jar(name = name, srcs = srcs,
162 resource_jars = [name + "-generate"], deps = deps,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700163 visibility = ["//visibility:public"],
Ray Milkey134d2922018-07-15 15:24:01 -0700164 suppress_errorprone = True,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700165 suppress_checkstyle = True,
166 suppress_javadocs = True,
167 )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700168
169def yang_model(
170 name = None,
171 app_name = None,
172 title = None,
173 description = None,
174 url = "http://onosproject.org/",
175 custom_registrator = False,
176 deps = None,
177 yang_srcs = None,
178 java_srcs = None,
179 required_apps = [],
180 visibility = ["//visibility:public"]):
181
182 if name == None:
183 name = "onos-" + native.package_name().replace("/", "-")
184
185 yang_library(name = name, deps = deps,
186 yang_srcs = yang_srcs, java_srcs = java_srcs,
187 custom_registrator = custom_registrator,
188 visibility = ["//visibility:public"])
189
190 onos_app(
191 app_name = app_name,
192 title = title,
193 description = description,
194 feature_name = name,
195 version = ONOS_VERSION,
196 url = url,
197 category = "Models",
198 included_bundles = [ name ],
199 required_apps = required_apps + ["org.onosproject.yang"],
200 visibility = ["//visibility:public"],
201 )