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, |
| 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 | |
| 116 | def 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 Vachuska | 5b9ff6a | 2018-07-13 11:00:50 -0700 | [diff] [blame] | 163 | visibility = ["//visibility:public"], |
Ray Milkey | 134d292 | 2018-07-15 15:24:01 -0700 | [diff] [blame] | 164 | suppress_errorprone = True, |
| 165 | suppress_checkstyle = True) |
Thomas Vachuska | f8c8cb9 | 2018-07-11 17:12:43 -0700 | [diff] [blame] | 166 | |
| 167 | def yang_model( |
| 168 | name = None, |
| 169 | app_name = None, |
| 170 | title = None, |
| 171 | description = None, |
| 172 | url = "http://onosproject.org/", |
| 173 | custom_registrator = False, |
| 174 | deps = None, |
| 175 | yang_srcs = None, |
| 176 | java_srcs = None, |
| 177 | required_apps = [], |
| 178 | visibility = ["//visibility:public"]): |
| 179 | |
| 180 | if name == None: |
| 181 | name = "onos-" + native.package_name().replace("/", "-") |
| 182 | |
| 183 | yang_library(name = name, deps = deps, |
| 184 | yang_srcs = yang_srcs, java_srcs = java_srcs, |
| 185 | custom_registrator = custom_registrator, |
| 186 | visibility = ["//visibility:public"]) |
| 187 | |
| 188 | onos_app( |
| 189 | app_name = app_name, |
| 190 | title = title, |
| 191 | description = description, |
| 192 | feature_name = name, |
| 193 | version = ONOS_VERSION, |
| 194 | url = url, |
| 195 | category = "Models", |
| 196 | included_bundles = [ name ], |
| 197 | required_apps = required_apps + ["org.onosproject.yang"], |
| 198 | visibility = ["//visibility:public"], |
| 199 | ) |