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