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, |
| 56 | executable = ctx.executable._onos_app_oar_exe, |
| 57 | ) |
| 58 | |
| 59 | # Implementation of the rule to build an app.xml or features file for an application |
| 60 | def _onos_app_xml_impl(ctx): |
| 61 | output = ctx.outputs.app_xml.path |
| 62 | app_name = ctx.attr.app_name |
| 63 | origin = ctx.attr.origin |
| 64 | version = ctx.attr.version |
| 65 | title = ctx.attr.title |
| 66 | category = ctx.attr.category |
| 67 | url = ctx.attr.url |
| 68 | mode = ctx.attr.mode |
| 69 | feature_coords = ctx.attr.feature_coords |
| 70 | description = ctx.attr.description |
| 71 | apps = ctx.attr.apps |
| 72 | included_bundles = ctx.attr.included_bundles |
| 73 | excluded_bundles = ctx.attr.excluded_bundles |
| 74 | required_features = ctx.attr.required_features |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 75 | required_apps = ctx.attr.required_apps |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 76 | security = ctx.attr.security |
| 77 | artifacts_args = [] |
| 78 | |
| 79 | # call the app.xml generator |
| 80 | arguments = [ |
| 81 | "-O", |
| 82 | output, |
| 83 | "-n", |
| 84 | feature_coords, |
| 85 | "-a", |
| 86 | app_name, |
| 87 | "-o", |
| 88 | origin, |
| 89 | "-c", |
| 90 | category, |
| 91 | "-u", |
| 92 | url, |
| 93 | "-v", |
| 94 | version, |
| 95 | "-t", |
| 96 | title, |
| 97 | "-D", |
| 98 | description, |
| 99 | mode, |
| 100 | ] |
| 101 | |
| 102 | for bundle in included_bundles: |
Ray Milkey | 02a658b | 2018-06-13 08:52:39 -0700 | [diff] [blame] | 103 | arguments += ["-b", maven_coordinates(bundle.label).replace("mvn:", "")] |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 104 | for bundle in excluded_bundles: |
Ray Milkey | 02a658b | 2018-06-13 08:52:39 -0700 | [diff] [blame] | 105 | arguments += ["-e", maven_coordinates(bundle.label).replace("mvn:", "")] |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 106 | for feature in required_features: |
| 107 | arguments += ["-f", feature] |
Ray Milkey | 02a658b | 2018-06-13 08:52:39 -0700 | [diff] [blame] | 108 | for app in required_apps: |
Ray Milkey | 15053f0 | 2018-06-13 10:00:45 -0700 | [diff] [blame] | 109 | arguments += ["-d", app] |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 110 | |
| 111 | if security != "": |
| 112 | arguments += ["-s", security] |
| 113 | |
| 114 | ctx.actions.run( |
| 115 | inputs = [], |
| 116 | outputs = [ctx.outputs.app_xml], |
| 117 | arguments = arguments, |
| 118 | progress_message = "Running app xml generator: %s" % ctx.attr.name, |
| 119 | executable = ctx.executable._onos_app_writer_exe, |
| 120 | ) |
| 121 | |
| 122 | # OAR file rule |
| 123 | _onos_oar = rule( |
| 124 | attrs = { |
| 125 | "deps": attr.label_list(), |
| 126 | "version": attr.string(), |
| 127 | "package_name_root": attr.string(), |
| 128 | "source": attr.label(), |
| 129 | "app_xml": attr.label(), |
| 130 | "feature_xml": attr.label(), |
| 131 | "feature_xml_coords": attr.string(), |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 132 | "included_bundles": attr.label_list(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 133 | "_onos_app_oar_exe": attr.label( |
| 134 | executable = True, |
| 135 | cfg = "host", |
| 136 | allow_files = True, |
| 137 | default = Label("//tools/build/bazel:onos_app_oar"), |
| 138 | ), |
| 139 | }, |
| 140 | outputs = { |
| 141 | "app_oar": "%{name}.oar", |
| 142 | }, |
| 143 | implementation = _onos_oar_impl, |
| 144 | ) |
| 145 | |
| 146 | # app.xml rule |
| 147 | _onos_app_xml = rule( |
| 148 | attrs = { |
| 149 | "app_name": attr.string(), |
| 150 | "origin": attr.string(), |
| 151 | "version": attr.string(), |
| 152 | "title": attr.string(), |
| 153 | "category": attr.string(), |
| 154 | "url": attr.string(), |
| 155 | "feature_coords": attr.string(), |
| 156 | "description": attr.string(), |
| 157 | "apps": attr.label_list(), |
| 158 | "included_bundles": attr.label_list(), |
| 159 | "excluded_bundles": attr.label_list(), |
| 160 | "required_features": attr.string_list(), |
| 161 | "security": attr.string(), |
| 162 | "mode": attr.string(), |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 163 | "required_apps": attr.string_list(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 164 | "_onos_app_writer_exe": attr.label( |
| 165 | executable = True, |
| 166 | cfg = "host", |
| 167 | allow_files = True, |
| 168 | default = Label("//tools/build/bazel:onos_app_writer"), |
| 169 | ), |
| 170 | }, |
| 171 | outputs = { |
| 172 | "app_xml": "%{name}.xml", |
| 173 | }, |
| 174 | implementation = _onos_app_xml_impl, |
| 175 | ) |
| 176 | |
| 177 | # feature.xml rule |
| 178 | _onos_feature_xml = rule( |
| 179 | attrs = { |
| 180 | "app_name": attr.string(), |
| 181 | "origin": attr.string(), |
| 182 | "version": attr.string(), |
| 183 | "title": attr.string(), |
| 184 | "category": attr.string(), |
| 185 | "url": attr.string(), |
| 186 | "feature_coords": attr.string(), |
| 187 | "description": attr.string(), |
| 188 | "apps": attr.label_list(), |
| 189 | "included_bundles": attr.label_list(), |
| 190 | "excluded_bundles": attr.label_list(), |
| 191 | "required_features": attr.string_list(), |
| 192 | "security": attr.string(), |
| 193 | "mode": attr.string(), |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 194 | "required_apps": attr.string_list(), |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 195 | "_onos_app_writer_exe": attr.label( |
| 196 | executable = True, |
| 197 | cfg = "host", |
| 198 | allow_files = True, |
| 199 | default = Label("//tools/build/bazel:onos_app_writer"), |
| 200 | ), |
| 201 | }, |
| 202 | outputs = { |
| 203 | "app_xml": "%{name}.xml", |
| 204 | }, |
| 205 | implementation = _onos_app_xml_impl, |
| 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 | |
| 227 | # Rule to build an ONOS application OAR file |
| 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) |
| 252 | feature_xml_coords = "%s:%s:xml:features:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION) |
| 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 | |
| 262 | # TODO - have to implement this eventually |
| 263 | #if not feature_coords and len(included_bundles) == 1: |
| 264 | # feature_coords = '$(maven_coords %s)' % included_bundles[0] |
| 265 | |
| 266 | if not feature_coords: |
| 267 | feature_coords = "%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION) |
| 268 | |
| 269 | # TODO - intra app dependecies |
| 270 | apps = [] |
| 271 | |
| 272 | # rule that generates the app.xml |
| 273 | _onos_app_xml( |
| 274 | name = name + "-app-xml", |
| 275 | app_name = app_name, |
| 276 | origin = origin, |
| 277 | version = version, |
| 278 | title = title, |
| 279 | category = category, |
| 280 | url = url, |
| 281 | feature_coords = feature_coords, |
| 282 | description = description, |
| 283 | apps = apps, |
| 284 | included_bundles = included_bundles, |
| 285 | excluded_bundles = excluded_bundles, |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 286 | required_apps = required_apps, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 287 | mode = "-A", |
| 288 | ) |
| 289 | |
| 290 | # rule that generates the features.xml |
| 291 | # TODO - rename this |
| 292 | _onos_app_xml( |
| 293 | name = name + "-feature-xml", |
| 294 | app_name = app_name, |
| 295 | origin = origin, |
| 296 | version = version, |
| 297 | title = title, |
| 298 | category = category, |
| 299 | url = url, |
| 300 | feature_coords = feature_coords, |
| 301 | description = description, |
| 302 | apps = apps, |
| 303 | included_bundles = included_bundles, |
| 304 | excluded_bundles = excluded_bundles, |
| 305 | required_features = required_features, |
| 306 | mode = "-F", |
| 307 | ) |
| 308 | |
| 309 | # rule to generate the OAR file based on the app.xml, features.xml, and app jar file |
| 310 | _onos_oar( |
| 311 | name = name + "-oar", |
Ray Milkey | 1ccab71 | 2018-06-12 16:30:02 -0700 | [diff] [blame] | 312 | included_bundles = included_bundles, |
Ray Milkey | 1d52ddd | 2018-06-07 10:37:24 -0700 | [diff] [blame] | 313 | app_xml = Label(_local_label(name, "-app-xml")), |
| 314 | feature_xml = Label(_local_label(name, "-feature-xml")), |
| 315 | feature_xml_coords = feature_xml_coords, |
| 316 | visibility = visibility, |
| 317 | ) |