Adding feature file generation via Bazel

Change-Id: Ifa9cdf01c3129e5cd6cb7b8f4aee956eef9055f8
diff --git a/tools/build/bazel/BUILD b/tools/build/bazel/BUILD
index 6c4e6ec..faaf6bc 100644
--- a/tools/build/bazel/BUILD
+++ b/tools/build/bazel/BUILD
@@ -6,21 +6,21 @@
 )
 
 py_binary(
-    name = "onos-app-oar",
+    name = "onos_app_oar",
     srcs = ["onos_oar.py"],
     main = "onos_oar.py",
     visibility = ["//visibility:public"],
 )
 
 py_binary(
-    name = "onos-stage",
+    name = "onos_stage",
     srcs = ["onos_stage.py"],
     main = "onos_stage.py",
     visibility = ["//visibility:public"],
 )
 
 py_binary(
-    name = "onos-feature",
+    name = "onos_feature",
     srcs = ["onos_feature.py"],
     main = "onos_feature.py",
     visibility = ["//visibility:public"],
diff --git a/tools/build/bazel/onos_app.py b/tools/build/bazel/onos_app.py
index bbb053c..ebb6bb7 100755
--- a/tools/build/bazel/onos_app.py
+++ b/tools/build/bazel/onos_app.py
@@ -134,11 +134,18 @@
     output += APP_FOOTER
     return output
 
+def write(name, msg):
+    if name is not None:
+        with open(name, "w") as file:
+            file.write(msg)
+    else:
+        print msg
 
 if __name__ == '__main__':
     import sys, optparse
 
     parser = optparse.OptionParser()
+    parser.add_option("-O", "--output",   dest="output",       help="Output file")
     parser.add_option("-n", "--name",     dest="feature_coords", help="Feature MVN Coords")
     parser.add_option("-a", "--app",      dest="app_name",     help="App Name")
     parser.add_option("-o", "--origin",   dest="origin",       help="Origin")
@@ -206,14 +213,15 @@
                               **values)
 
     if options.write_feature:
-        print feature
+        write(options.output, feature)
 
     if options.write_features:
-        print generateFeatureFile(features=[ feature ],
-                                  **values)
+        write(options.output,
+              generateFeatureFile(features=[ feature ], **values))
 
     if options.write_app:
-        print generateAppFile(artifacts=options.included_bundles,
+        write(options.output,
+              generateAppFile(artifacts=options.included_bundles,
                               apps=options.apps,
                               security=options.security,
-                              **values)
\ No newline at end of file
+                              **values))
\ No newline at end of file
diff --git a/tools/build/bazel/osgi_features.bzl b/tools/build/bazel/osgi_features.bzl
new file mode 100644
index 0000000..bc6ec4f
--- /dev/null
+++ b/tools/build/bazel/osgi_features.bzl
@@ -0,0 +1,114 @@
+"""
+ Copyright 2018-present Open Networking Foundation
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+"""
+
+load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST", "maven_coordinates")
+load("//tools/build/bazel:variables.bzl", "ONOS_VERSION", "ONOS_GROUP_ID")
+
+def dump(obj):
+  for attr in dir(obj):
+    print("obj.%s = %r" % (attr, getattr(obj, attr)))
+
+# Implementation of a rule to produce an OSGi feature XML snippet
+def _osgi_feature_impl(ctx):
+    output = ctx.outputs.feature_xml
+
+    args = [ "-O", output.path,
+             "-n", ctx.attr.name,
+             "-v", ctx.attr.version,
+             "-t", ctx.attr.description,
+            ]
+
+    inputs = []
+    for dep in ctx.attr.included_bundles:
+        args += [ "-b", maven_coordinates(dep.label) ]
+        for f in dep.java.outputs.jars:
+          inputs += [ f.class_jar ]
+
+    for dep in ctx.attr.excluded_bundles:
+        args += [ "-e", maven_coordinates(dep.label) ]
+        for f in dep.java.outputs.jars:
+          inputs += [ f.class_jar ]
+
+    for f in ctx.attr.required_features:
+      args += [ "-f", f ]
+
+    args += [ "-F" if ctx.attr.generate_file else "-E" ]
+
+    ctx.actions.run(
+        inputs = inputs,
+        outputs = [ output ],
+        arguments = args,
+        progress_message = "Generating feature %s" % ctx.attr.name,
+        executable = ctx.executable._writer
+    )
+
+osgi_feature = rule(
+    attrs = {
+        "description": attr.string(),
+        "version": attr.string(default = ONOS_VERSION),
+        "required_features": attr.string_list(default = [ "onos-api" ]),
+        "included_bundles": attr.label_list(),
+        "excluded_bundles": attr.label_list(default = []),
+        "generate_file": attr.bool(default = False),
+        "_writer": attr.label(executable=True, cfg="host", allow_files=True,
+                              default=Label("//tools/build/bazel:onos_app_writer")),
+    },
+    outputs = {
+        "feature_xml": "feature-%{name}.xml",
+    },
+    implementation = _osgi_feature_impl,
+)
+
+# OSGi feature XML header & footer constants
+FEATURES_HEADER = '''\
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0"
+          name="onos-%s">
+    <repository>mvn:org.apache.karaf.features/standard/3.0.8/xml/features</repository>
+''' % ONOS_VERSION
+
+FEATURES_FOOTER = '</features>'
+
+# Implementation of a rule to produce an OSGi feature repo XML file
+def _osgi_feature_repo_impl(ctx):
+    output = ctx.outputs.feature_repo_xml
+
+    cmd = "(echo '%s';" % FEATURES_HEADER
+    inputs = []
+    for dep in ctx.attr.exported_features:
+        for f in dep.files.to_list():
+            inputs += [ f ]
+            cmd += "cat %s;" % f.path
+    cmd += "echo '%s') > %s;" % (FEATURES_FOOTER, output.path)
+
+    ctx.actions.run_shell(
+        inputs = inputs,
+        outputs = [ output ],
+        progress_message = "Generating feature repo %s" % ctx.attr.name,
+        command = cmd,
+    )
+
+osgi_feature_repo = rule(
+    attrs = {
+        "description": attr.string(),
+        "version": attr.string(default = ONOS_VERSION),
+        "exported_features": attr.label_list(),
+    },
+    outputs = {
+        "feature_repo_xml": "feature-repo-%{name}.xml",
+    },
+    implementation = _osgi_feature_repo_impl,
+)