Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -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", "maven_coordinates") |
| 18 | load( |
| 19 | "//tools/build/bazel:variables.bzl", |
| 20 | "APP_PREFIX", |
| 21 | "DEFAULT_APP_CATEGORY", |
| 22 | "ONOS_ARTIFACT_BASE", |
| 23 | "ONOS_GROUP_ID", |
| 24 | "ONOS_ORIGIN", |
| 25 | "ONOS_VERSION", |
| 26 | ) |
| 27 | |
| 28 | # Implementation of the rule to build an ONOS application OAR file |
| 29 | def _onos_oar_impl(ctx): |
| 30 | app_xml_file = ctx.attr.app_xml.files.to_list()[0] |
| 31 | feature_xml_file = ctx.attr.feature_xml.files.to_list()[0] |
| 32 | feature_xml_coords = ctx.attr.feature_xml_coords |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 33 | |
| 34 | jar_file_args = [] |
| 35 | jar_files = [] |
| 36 | for bundle in ctx.attr.included_bundles: |
| 37 | jar_file = bundle.files.to_list()[0] |
| 38 | jar_files.append(jar_file) |
| 39 | jar_file_coords = maven_coordinates(bundle.label) |
| 40 | jar_file_args.append(jar_file.path) |
| 41 | jar_file_args.append(jar_file_coords) |
| 42 | |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 43 | arguments = [ |
| 44 | ctx.outputs.app_oar.path, |
| 45 | feature_xml_file.path, |
| 46 | feature_xml_coords, |
| 47 | app_xml_file.path, |
| 48 | "NONE", |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 49 | ] + jar_file_args |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 50 | |
| 51 | ctx.actions.run( |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 52 | inputs = [app_xml_file, feature_xml_file] + jar_files, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 53 | outputs = [ctx.outputs.app_oar], |
| 54 | arguments = arguments, |
| 55 | progress_message = "Running oar file generator: %s" % ctx.attr.name, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 56 | executable = ctx.executable._onos_app_bundler, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 57 | ) |
| 58 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 59 | # Implementation of the rule to build an app.xml desriptor file for ONOS app |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 60 | def _onos_app_xml_impl(ctx): |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 61 | # Build-up arguments for the app.xml and feature.xml generator |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 62 | arguments = [ |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 63 | "-n", ctx.attr.feature_coords, |
| 64 | "-a", ctx.attr.app_name, |
| 65 | "-o", ctx.attr.origin, |
| 66 | "-c", ctx.attr.category, |
| 67 | "-u", ctx.attr.url, |
| 68 | "-v", ctx.attr.version, |
| 69 | "-t", ctx.attr.title, |
| 70 | "-D", ctx.attr.description, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 71 | ] |
| 72 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 73 | for bundle in ctx.attr.included_bundles: |
| 74 | arguments += ["-b", maven_coordinates(bundle.label)] |
| 75 | for bundle in ctx.attr.excluded_bundles: |
| 76 | arguments += ["-e", maven_coordinates(bundle.label)] |
| 77 | for app in ctx.attr.required_apps: |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 78 | arguments += ["-d", app] |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 79 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 80 | if ctx.attr.security != "": |
| 81 | arguments += ["-s", ctx.attr.security] |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 82 | |
| 83 | ctx.actions.run( |
| 84 | inputs = [], |
| 85 | outputs = [ctx.outputs.app_xml], |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 86 | arguments = arguments + [ "-A", "-O", ctx.outputs.app_xml.path ], |
| 87 | progress_message = "Generating app.xml descriptor for app: %s" % ctx.attr.name, |
| 88 | executable = ctx.executable._onos_app_tools, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 89 | ) |
| 90 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 91 | # Implementation of the rule to build the feature.xml file for ONOS app |
| 92 | def _onos_feature_xml_impl(ctx): |
| 93 | # Build-up arguments |
| 94 | arguments = [ |
| 95 | "-n", ctx.attr.feature_coords, |
| 96 | "-a", ctx.attr.app_name, |
| 97 | "-u", ctx.attr.url, |
| 98 | "-v", ctx.attr.version, |
| 99 | "-t", ctx.attr.title, |
| 100 | "-D", ctx.attr.description, |
| 101 | ] |
| 102 | |
| 103 | for bundle in ctx.attr.included_bundles: |
| 104 | arguments += ["-b", maven_coordinates(bundle.label)] |
| 105 | for feature in ctx.attr.required_features: |
| 106 | arguments += ["-f", feature] |
| 107 | |
| 108 | ctx.actions.run( |
| 109 | inputs = [], |
| 110 | outputs = [ctx.outputs.feature_xml], |
| 111 | arguments = arguments + [ "-F", "-O", ctx.outputs.feature_xml.path ], |
| 112 | progress_message = "Generating feature.xml for app: %s" % ctx.attr.name, |
| 113 | executable = ctx.executable._onos_app_tools, |
| 114 | ) |
| 115 | |
| 116 | |
| 117 | # Rule to generate the ONOS app OAR file. |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 118 | _onos_oar = rule( |
| 119 | attrs = { |
| 120 | "deps": attr.label_list(), |
| 121 | "version": attr.string(), |
| 122 | "package_name_root": attr.string(), |
| 123 | "source": attr.label(), |
| 124 | "app_xml": attr.label(), |
| 125 | "feature_xml": attr.label(), |
| 126 | "feature_xml_coords": attr.string(), |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 127 | "included_bundles": attr.label_list(), |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 128 | "_onos_app_bundler": attr.label( |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 129 | executable = True, |
| 130 | cfg = "host", |
| 131 | allow_files = True, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 132 | default = Label("//tools/build/bazel:onos_app_bundler"), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 133 | ), |
| 134 | }, |
| 135 | outputs = { |
| 136 | "app_oar": "%{name}.oar", |
| 137 | }, |
| 138 | implementation = _onos_oar_impl, |
| 139 | ) |
| 140 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 141 | # Rule to generate app.xml descriptor for an ONOS application. |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 142 | _onos_app_xml = rule( |
| 143 | attrs = { |
| 144 | "app_name": attr.string(), |
| 145 | "origin": attr.string(), |
| 146 | "version": attr.string(), |
| 147 | "title": attr.string(), |
| 148 | "category": attr.string(), |
| 149 | "url": attr.string(), |
| 150 | "feature_coords": attr.string(), |
| 151 | "description": attr.string(), |
| 152 | "apps": attr.label_list(), |
| 153 | "included_bundles": attr.label_list(), |
| 154 | "excluded_bundles": attr.label_list(), |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 155 | "required_apps": attr.string_list(), |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 156 | "security": attr.string(), |
| 157 | "_onos_app_tools": attr.label( |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 158 | executable = True, |
| 159 | cfg = "host", |
| 160 | allow_files = True, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 161 | default = Label("//tools/build/bazel:onos_app_tools"), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 162 | ), |
| 163 | }, |
| 164 | outputs = { |
| 165 | "app_xml": "%{name}.xml", |
| 166 | }, |
| 167 | implementation = _onos_app_xml_impl, |
| 168 | ) |
| 169 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 170 | # Rule to generate feature.xml descriptor for an ONOS application. |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 171 | _onos_feature_xml = rule( |
| 172 | attrs = { |
| 173 | "app_name": attr.string(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 174 | "version": attr.string(), |
| 175 | "title": attr.string(), |
| 176 | "category": attr.string(), |
| 177 | "url": attr.string(), |
| 178 | "feature_coords": attr.string(), |
| 179 | "description": attr.string(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 180 | "included_bundles": attr.label_list(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 181 | "required_features": attr.string_list(), |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 182 | "_onos_app_tools": attr.label( |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 183 | executable = True, |
| 184 | cfg = "host", |
| 185 | allow_files = True, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 186 | default = Label("//tools/build/bazel:onos_app_tools"), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 187 | ), |
| 188 | }, |
| 189 | outputs = { |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 190 | "feature_xml": "%{name}.xml", |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 191 | }, |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 192 | implementation = _onos_feature_xml_impl, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 193 | ) |
| 194 | |
| 195 | def _basename(path): |
| 196 | paths = path.split("/") |
| 197 | return paths[len(paths) - 1] |
| 198 | |
| 199 | def _get_base_path(): |
| 200 | return native.package_name() |
| 201 | |
| 202 | def _get_name(): |
| 203 | base_path = _get_base_path() |
| 204 | return ONOS_ARTIFACT_BASE + base_path.replace("/", "-") |
| 205 | |
| 206 | def _get_app_name(): |
| 207 | base_path = _get_base_path() |
| 208 | return APP_PREFIX + _basename(base_path) |
| 209 | |
| 210 | def _local_label(name, suffix): |
| 211 | base_label_name = "//" + native.package_name() + ":" |
| 212 | return base_label_name + name + suffix |
| 213 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 214 | # Macro to build an ONOS application OAR file. |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 215 | def onos_app( |
| 216 | app_name = None, |
| 217 | name = None, |
| 218 | title = None, |
| 219 | version = ONOS_VERSION, |
| 220 | origin = ONOS_ORIGIN, |
| 221 | category = DEFAULT_APP_CATEGORY, |
| 222 | url = None, |
| 223 | description = None, |
| 224 | feature_coords = None, |
| 225 | required_features = ["onos-api"], |
| 226 | required_apps = [], |
| 227 | included_bundles = None, |
| 228 | excluded_bundles = [], |
| 229 | visibility = ["//visibility:public"], |
| 230 | security = None, |
| 231 | **kwargs): |
| 232 | if name == None: |
| 233 | name = _get_name() |
| 234 | |
| 235 | if app_name == None: |
| 236 | app_name = _get_app_name() |
| 237 | |
| 238 | maven_coords = "%s:%s:oar:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION) |
Thomas Vachuska | 1ae26c6 | 2018-07-09 13:40:16 -0700 | [diff] [blame] | 239 | feature_xml_coords = "mvn:%s:%s:xml:features:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION) |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 240 | |
| 241 | if title == None: |
| 242 | print("Missing title for %s" % _get_name()) |
| 243 | title = _get_app_name() |
| 244 | |
| 245 | if included_bundles == None: |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 246 | target = _local_label(name, "") |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 247 | included_bundles = [target] |
| 248 | |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 249 | if not feature_coords: |
Thomas Vachuska | 1ae26c6 | 2018-07-09 13:40:16 -0700 | [diff] [blame] | 250 | feature_coords = "mvn:%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION) |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 251 | |
| 252 | # TODO - intra app dependecies |
| 253 | apps = [] |
| 254 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 255 | # Generate the app.xml file |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 256 | _onos_app_xml( |
| 257 | name = name + "-app-xml", |
| 258 | app_name = app_name, |
| 259 | origin = origin, |
| 260 | version = version, |
| 261 | title = title, |
| 262 | category = category, |
| 263 | url = url, |
| 264 | feature_coords = feature_coords, |
| 265 | description = description, |
| 266 | apps = apps, |
| 267 | included_bundles = included_bundles, |
| 268 | excluded_bundles = excluded_bundles, |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 269 | required_apps = required_apps, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 270 | ) |
| 271 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 272 | # Generate feature.xml file |
| 273 | _onos_feature_xml( |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 274 | name = name + "-feature-xml", |
| 275 | app_name = app_name, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 276 | version = version, |
| 277 | title = title, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 278 | feature_coords = feature_coords, |
| 279 | description = description, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 280 | included_bundles = included_bundles, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 281 | required_features = required_features, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 282 | ) |
| 283 | |
Thomas Vachuska | 8e022a9 | 2018-07-10 14:47:38 -0700 | [diff] [blame] | 284 | # Generate the OAR file based on the app.xml, features.xml, and specified |
| 285 | # bundles to be included. |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 286 | _onos_oar( |
| 287 | name = name + "-oar", |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 288 | included_bundles = included_bundles, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 289 | app_xml = Label(_local_label(name, "-app-xml")), |
| 290 | feature_xml = Label(_local_label(name, "-feature-xml")), |
| 291 | feature_xml_coords = feature_xml_coords, |
| 292 | visibility = visibility, |
| 293 | ) |