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