Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 1 | """ |
| 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 | |
| 17 | load("//tools/build/bazel:generate_workspace.bzl", "CORE_DEPS", "ONOS_YANG") |
| 18 | load("//tools/build/bazel:osgi_java_library.bzl", "osgi_jar") |
| 19 | load("//tools/build/bazel:onos_app.bzl", "onos_app") |
| 20 | load( |
| 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 | |
| 30 | REGISTRATOR = \ |
| 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 | |
| 44 | REGISTRATOR_FILE = "src/org/onosproject/model/registrator/impl/YangModelRegistrator.java" |
| 45 | |
| 46 | # Implementation of the YANG library rule |
| 47 | def _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, |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 88 | generated_sources.path, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 89 | ], |
| 90 | command = "jar cf $1 -C $2 schema", |
| 91 | progress_message = "Assembling YANG compiled schema: %s" % ctx.attr.name, |
| 92 | ) |
| 93 | |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 94 | # Rule to generate YANG library from the specified set of YANG models. |
| 95 | _yang_library = rule( |
| 96 | attrs = { |
| 97 | "deps": attr.label_list(), |
| 98 | "srcs": attr.label_list(allow_files = True), |
| 99 | "model_id": attr.string(), |
| 100 | "_yang_compiler": attr.label( |
| 101 | executable = True, |
| 102 | cfg = "host", |
| 103 | allow_files = True, |
| 104 | default = Label("//tools/build/bazel:onos_yang_compiler"), |
| 105 | ), |
| 106 | }, |
| 107 | outputs = { |
| 108 | "srcjar": "model.srcjar", |
| 109 | "schema": "schema.jar", |
| 110 | }, |
| 111 | fragments = ["java"], |
| 112 | implementation = _yang_library_impl, |
| 113 | ) |
| 114 | |
| 115 | def yang_library( |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 116 | name = None, |
| 117 | deps = None, |
| 118 | yang_srcs = None, |
| 119 | java_srcs = None, |
| 120 | custom_registrator = False, |
| 121 | visibility = ["//visibility:public"]): |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 122 | if name == None: |
| 123 | name = "onos-" + native.package_name().replace("/", "-") |
| 124 | if yang_srcs == None: |
| 125 | yang_srcs = native.glob(["src/main/yang/**/*.yang"]) |
| 126 | if java_srcs == None: |
| 127 | java_srcs = native.glob(["src/main/java/**/*.java"]) |
| 128 | if deps == None: |
| 129 | deps = [] |
| 130 | |
| 131 | deps += CORE_DEPS + ONOS_YANG + [ |
| 132 | "@onos_yang_runtime//jar", |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 133 | "//apps/yang:onos-apps-yang", |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 134 | ] |
| 135 | |
| 136 | # Generate the Java sources from YANG model |
| 137 | _yang_library(name = name + "-generate", model_id = name, deps = deps, srcs = yang_srcs) |
| 138 | |
| 139 | srcs = [name + "-generate"] |
| 140 | |
| 141 | if len(java_srcs): |
| 142 | srcs += [name + "-srcjar"] |
| 143 | native.genrule( |
| 144 | name = name + "-srcjar", |
| 145 | srcs = java_srcs, |
| 146 | outs = [name + ".srcjar"], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 147 | cmd = "jar cf $(location %s.srcjar) $(SRCS)" % name, |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 148 | ) |
| 149 | |
| 150 | if not custom_registrator: |
| 151 | srcs += [name + "-registrator"] |
| 152 | native.genrule( |
| 153 | name = name + "-registrator", |
| 154 | outs = [REGISTRATOR_FILE], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 155 | cmd = "echo '%s' > $(location %s)" % (REGISTRATOR, REGISTRATOR_FILE), |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 156 | ) |
| 157 | |
| 158 | # Produce a Java library from the generated Java sources |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 159 | osgi_jar( |
| 160 | name = name, |
| 161 | srcs = srcs, |
| 162 | resource_jars = [name + "-generate"], |
| 163 | deps = deps, |
| 164 | visibility = ["//visibility:public"], |
| 165 | suppress_errorprone = True, |
| 166 | suppress_checkstyle = True, |
| 167 | suppress_javadocs = True, |
Thomas Vachuska | 0f7d7a4 | 2018-07-18 15:23:40 -0700 | [diff] [blame] | 168 | ) |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 169 | |
| 170 | def yang_model( |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 171 | name = None, |
| 172 | app_name = None, |
| 173 | title = None, |
| 174 | description = None, |
| 175 | url = "http://onosproject.org/", |
| 176 | custom_registrator = False, |
| 177 | deps = None, |
| 178 | yang_srcs = None, |
| 179 | java_srcs = None, |
| 180 | required_apps = [], |
| 181 | visibility = ["//visibility:public"]): |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 182 | if name == None: |
| 183 | name = "onos-" + native.package_name().replace("/", "-") |
| 184 | |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 185 | yang_library( |
| 186 | name = name, |
| 187 | deps = deps, |
| 188 | yang_srcs = yang_srcs, |
| 189 | java_srcs = java_srcs, |
| 190 | custom_registrator = custom_registrator, |
| 191 | visibility = ["//visibility:public"], |
| 192 | ) |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 193 | |
| 194 | onos_app( |
| 195 | app_name = app_name, |
| 196 | title = title, |
| 197 | description = description, |
| 198 | feature_name = name, |
| 199 | version = ONOS_VERSION, |
| 200 | url = url, |
| 201 | category = "Models", |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 202 | included_bundles = [name], |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 203 | required_apps = required_apps + ["org.onosproject.yang"], |
| 204 | visibility = ["//visibility:public"], |
Ray Milkey | 5063f5b | 2018-08-15 16:22:30 -0700 | [diff] [blame] | 205 | ) |