Fixing Bazel build & packaging.

Change-Id: I96ba75c395b9a780e4c8233a088f8d8a8abeb874
diff --git a/BUILD b/BUILD
index beaf1c1..3e36aa6 100644
--- a/BUILD
+++ b/BUILD
@@ -82,7 +82,7 @@
 # Runs ONOS as a single instance from the /tmp directory
 # FIXME: Still work in progress
 genrule(
-    name = "onos-run",
+    name = "onos-local",
     srcs = [
         ":onos-package",
         "tools/package/onos-run-karaf",
diff --git a/apps/optical-model/BUILD b/apps/optical-model/BUILD
index ac2a540..f57c3b5 100644
--- a/apps/optical-model/BUILD
+++ b/apps/optical-model/BUILD
@@ -15,9 +15,6 @@
     app_name = "org.onosproject.optical-model",
     category = "Optical",
     description = "ONOS optical information model.",
-    # Is there a way to change BUCK target name properly?
-    #name = 'onos-optical-model',
-    feature_coords = "org.onosproject:onos-optical-model:%s" % (ONOS_VERSION),
     required_apps = [],
     title = "Optical Network Model",
     url = "https://wiki.onosproject.org/x/C4m",
diff --git a/modules.bzl b/modules.bzl
index 8b98c9b..c47d449 100644
--- a/modules.bzl
+++ b/modules.bzl
@@ -168,7 +168,7 @@
     #"//apps/openstacknetworking:onos-apps-openstacknetworking-oar",
     "//apps/mobility:onos-apps-mobility-oar",
     #"//apps/newoptical:onos-apps-newoptical-oar",
-    #"//apps/optical-model:onos-apps-optical-model-oar",
+    "//apps/optical-model:onos-apps-optical-model-oar",
     #"//apps/optical-rest:onos-apps-optical-rest-oar",
     #"//apps/pathpainter:onos-apps-pathpainter-oar",
     "//apps/pcep-api:onos-apps-pcep-api-oar",
diff --git a/tools/build/bazel/BUILD b/tools/build/bazel/BUILD
index faaf6bc..09dd187 100644
--- a/tools/build/bazel/BUILD
+++ b/tools/build/bazel/BUILD
@@ -1,27 +1,20 @@
 py_binary(
-    name = "onos_app_writer",
-    srcs = ["onos_app.py"],
-    main = "onos_app.py",
+    name = "osgi_feature_bundler",
+    srcs = ["osgi_feature_bundler.py"],
+    main = "osgi_feature_bundler.py",
     visibility = ["//visibility:public"],
 )
 
 py_binary(
-    name = "onos_app_oar",
-    srcs = ["onos_oar.py"],
-    main = "onos_oar.py",
+    name = "onos_app_tools",
+    srcs = ["onos_app_tools.py"],
+    main = "onos_app_tools.py",
     visibility = ["//visibility:public"],
 )
 
 py_binary(
-    name = "onos_stage",
-    srcs = ["onos_stage.py"],
-    main = "onos_stage.py",
-    visibility = ["//visibility:public"],
-)
-
-py_binary(
-    name = "onos_feature",
-    srcs = ["onos_feature.py"],
-    main = "onos_feature.py",
+    name = "onos_app_bundler",
+    srcs = ["onos_app_bundler.py"],
+    main = "onos_app_bundler.py",
     visibility = ["//visibility:public"],
 )
diff --git a/tools/build/bazel/onos_app.bzl b/tools/build/bazel/onos_app.bzl
index a5d3f82..c5563b1 100644
--- a/tools/build/bazel/onos_app.bzl
+++ b/tools/build/bazel/onos_app.bzl
@@ -53,73 +53,68 @@
         outputs = [ctx.outputs.app_oar],
         arguments = arguments,
         progress_message = "Running oar file generator: %s" % ctx.attr.name,
-        executable = ctx.executable._onos_app_oar_exe,
+        executable = ctx.executable._onos_app_bundler,
     )
 
-# Implementation of the rule to build an app.xml or features file for an application
+# Implementation of the rule to build an app.xml desriptor file for ONOS app
 def _onos_app_xml_impl(ctx):
-    output = ctx.outputs.app_xml.path
-    app_name = ctx.attr.app_name
-    origin = ctx.attr.origin
-    version = ctx.attr.version
-    title = ctx.attr.title
-    category = ctx.attr.category
-    url = ctx.attr.url
-    mode = ctx.attr.mode
-    feature_coords = ctx.attr.feature_coords
-    description = ctx.attr.description
-    apps = ctx.attr.apps
-    included_bundles = ctx.attr.included_bundles
-    excluded_bundles = ctx.attr.excluded_bundles
-    required_features = ctx.attr.required_features
-    required_apps = ctx.attr.required_apps
-    security = ctx.attr.security
-    artifacts_args = []
-
-    # call the app.xml generator
+    # Build-up arguments for the app.xml and feature.xml generator
     arguments = [
-        "-O",
-        output,
-        "-n",
-        feature_coords,
-        "-a",
-        app_name,
-        "-o",
-        origin,
-        "-c",
-        category,
-        "-u",
-        url,
-        "-v",
-        version,
-        "-t",
-        title,
-        "-D",
-        description,
-        mode,
+        "-n", ctx.attr.feature_coords,
+        "-a", ctx.attr.app_name,
+        "-o", ctx.attr.origin,
+        "-c", ctx.attr.category,
+        "-u", ctx.attr.url,
+        "-v", ctx.attr.version,
+        "-t", ctx.attr.title,
+        "-D", ctx.attr.description,
     ]
 
-    for bundle in included_bundles:
-        arguments += ["-b", maven_coordinates(bundle.label).replace("mvn:", "")]
-    for bundle in excluded_bundles:
-        arguments += ["-e", maven_coordinates(bundle.label).replace("mvn:", "")]
-    for feature in required_features:
-        arguments += ["-f", feature]
-    for app in required_apps:
+    for bundle in ctx.attr.included_bundles:
+        arguments += ["-b", maven_coordinates(bundle.label)]
+    for bundle in ctx.attr.excluded_bundles:
+        arguments += ["-e", maven_coordinates(bundle.label)]
+    for app in ctx.attr.required_apps:
         arguments += ["-d", app]
 
-    if security != "":
-        arguments += ["-s", security]
+    if ctx.attr.security != "":
+        arguments += ["-s", ctx.attr.security]
 
     ctx.actions.run(
         inputs = [],
         outputs = [ctx.outputs.app_xml],
-        arguments = arguments,
-        progress_message = "Running app xml generator: %s" % ctx.attr.name,
-        executable = ctx.executable._onos_app_writer_exe,
+        arguments = arguments + [ "-A", "-O", ctx.outputs.app_xml.path ],
+        progress_message = "Generating app.xml descriptor for app: %s" % ctx.attr.name,
+        executable = ctx.executable._onos_app_tools,
     )
 
-# OAR file rule
+# Implementation of the rule to build the feature.xml file for ONOS app
+def _onos_feature_xml_impl(ctx):
+    # Build-up arguments
+    arguments = [
+        "-n", ctx.attr.feature_coords,
+        "-a", ctx.attr.app_name,
+        "-u", ctx.attr.url,
+        "-v", ctx.attr.version,
+        "-t", ctx.attr.title,
+        "-D", ctx.attr.description,
+    ]
+
+    for bundle in ctx.attr.included_bundles:
+        arguments += ["-b", maven_coordinates(bundle.label)]
+    for feature in ctx.attr.required_features:
+        arguments += ["-f", feature]
+
+    ctx.actions.run(
+        inputs = [],
+        outputs = [ctx.outputs.feature_xml],
+        arguments = arguments + [ "-F", "-O", ctx.outputs.feature_xml.path ],
+        progress_message = "Generating feature.xml for app: %s" % ctx.attr.name,
+        executable = ctx.executable._onos_app_tools,
+    )
+
+
+# Rule to generate the ONOS app OAR file.
 _onos_oar = rule(
     attrs = {
         "deps": attr.label_list(),
@@ -130,11 +125,11 @@
         "feature_xml": attr.label(),
         "feature_xml_coords": attr.string(),
         "included_bundles": attr.label_list(),
-        "_onos_app_oar_exe": attr.label(
+        "_onos_app_bundler": attr.label(
             executable = True,
             cfg = "host",
             allow_files = True,
-            default = Label("//tools/build/bazel:onos_app_oar"),
+            default = Label("//tools/build/bazel:onos_app_bundler"),
         ),
     },
     outputs = {
@@ -143,7 +138,7 @@
     implementation = _onos_oar_impl,
 )
 
-# app.xml rule
+# Rule to generate app.xml descriptor for an ONOS application.
 _onos_app_xml = rule(
     attrs = {
         "app_name": attr.string(),
@@ -157,15 +152,13 @@
         "apps": attr.label_list(),
         "included_bundles": attr.label_list(),
         "excluded_bundles": attr.label_list(),
-        "required_features": attr.string_list(),
-        "security": attr.string(),
-        "mode": attr.string(),
         "required_apps": attr.string_list(),
-        "_onos_app_writer_exe": attr.label(
+        "security": attr.string(),
+        "_onos_app_tools": attr.label(
             executable = True,
             cfg = "host",
             allow_files = True,
-            default = Label("//tools/build/bazel:onos_app_writer"),
+            default = Label("//tools/build/bazel:onos_app_tools"),
         ),
     },
     outputs = {
@@ -174,35 +167,29 @@
     implementation = _onos_app_xml_impl,
 )
 
-# feature.xml rule
+# Rule to generate feature.xml descriptor for an ONOS application.
 _onos_feature_xml = rule(
     attrs = {
         "app_name": attr.string(),
-        "origin": attr.string(),
         "version": attr.string(),
         "title": attr.string(),
         "category": attr.string(),
         "url": attr.string(),
         "feature_coords": attr.string(),
         "description": attr.string(),
-        "apps": attr.label_list(),
         "included_bundles": attr.label_list(),
-        "excluded_bundles": attr.label_list(),
         "required_features": attr.string_list(),
-        "security": attr.string(),
-        "mode": attr.string(),
-        "required_apps": attr.string_list(),
-        "_onos_app_writer_exe": attr.label(
+        "_onos_app_tools": attr.label(
             executable = True,
             cfg = "host",
             allow_files = True,
-            default = Label("//tools/build/bazel:onos_app_writer"),
+            default = Label("//tools/build/bazel:onos_app_tools"),
         ),
     },
     outputs = {
-        "app_xml": "%{name}.xml",
+        "feature_xml": "%{name}.xml",
     },
-    implementation = _onos_app_xml_impl,
+    implementation = _onos_feature_xml_impl,
 )
 
 def _basename(path):
@@ -224,7 +211,7 @@
     base_label_name = "//" + native.package_name() + ":"
     return base_label_name + name + suffix
 
-# Rule to build an ONOS application OAR file
+# Macro to build an ONOS application OAR file.
 def onos_app(
         app_name = None,
         name = None,
@@ -259,17 +246,13 @@
         target = _local_label(name, "")
         included_bundles = [target]
 
-    # TODO - have to implement this eventually
-    #if not feature_coords and len(included_bundles) == 1:
-    #    feature_coords = '$(maven_coords %s)' % included_bundles[0]
-
     if not feature_coords:
         feature_coords = "mvn:%s:%s:%s" % (ONOS_GROUP_ID, name, ONOS_VERSION)
 
     # TODO - intra app dependecies
     apps = []
 
-    # rule that generates the app.xml
+    # Generate the app.xml file
     _onos_app_xml(
         name = name + "-app-xml",
         app_name = app_name,
@@ -284,29 +267,22 @@
         included_bundles = included_bundles,
         excluded_bundles = excluded_bundles,
         required_apps = required_apps,
-        mode = "-A",
     )
 
-    # rule that generates the features.xml
-    # TODO - rename this
-    _onos_app_xml(
+   # Generate feature.xml file
+    _onos_feature_xml(
         name = name + "-feature-xml",
         app_name = app_name,
-        origin = origin,
         version = version,
         title = title,
-        category = category,
-        url = url,
         feature_coords = feature_coords,
         description = description,
-        apps = apps,
         included_bundles = included_bundles,
-        excluded_bundles = excluded_bundles,
         required_features = required_features,
-        mode = "-F",
     )
 
-    # rule to generate the OAR file based on the app.xml, features.xml, and app jar file
+    # Generate the OAR file based on the app.xml, features.xml, and specified
+    # bundles to be included.
     _onos_oar(
         name = name + "-oar",
         included_bundles = included_bundles,
diff --git a/tools/build/bazel/onos_oar.py b/tools/build/bazel/onos_app_bundler.py
similarity index 68%
rename from tools/build/bazel/onos_oar.py
rename to tools/build/bazel/onos_app_bundler.py
index e5915f8..afac671 100755
--- a/tools/build/bazel/onos_oar.py
+++ b/tools/build/bazel/onos_app_bundler.py
@@ -1,11 +1,26 @@
 #!/usr/bin/env python
-#FIXME Add license
+"""
+ 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.
+"""
 
 from zipfile import ZipFile, ZipInfo
-import os
+
+# Utility to write out the ONOS OAR file bundle containing the artifacts
+# required to install and activate an ONOS application.
 
 def generateOar(output, files=[]):
-    # Note this is not a compressed zip
     with ZipFile(output, 'w') as zip:
         for file, mvnCoords in files:
             mvnCoords = mvnCoords.replace("mvn:", "")
diff --git a/tools/build/bazel/onos_app.py b/tools/build/bazel/onos_app_tools.py
similarity index 92%
rename from tools/build/bazel/onos_app.py
rename to tools/build/bazel/onos_app_tools.py
index 36552a0..fb975a4 100755
--- a/tools/build/bazel/onos_app.py
+++ b/tools/build/bazel/onos_app_tools.py
@@ -1,5 +1,19 @@
 #!/usr/bin/env python
-#FIXME Add license
+"""
+ 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.
+"""
 
 ##### Templates for features.xml
 FEATURES_HEADER = '''\
@@ -179,7 +193,7 @@
     values = {}
     if options.feature_coords and options.version and options.title:
         parts = options.feature_coords.split(':')
-        values['feature_name'] = parts[1] if len(parts) > 1 else parts[0]
+        values['feature_name'] = parts[2] if len(parts) > 2 else parts[0]
         values['version'] = options.version
         values['title'] = options.title
     else:
diff --git a/tools/build/bazel/onos_stage.py b/tools/build/bazel/onos_stage.py
deleted file mode 100644
index e69de29..0000000
--- a/tools/build/bazel/onos_stage.py
+++ /dev/null
diff --git a/tools/build/bazel/onos_feature.py b/tools/build/bazel/osgi_feature_bundler.py
similarity index 63%
rename from tools/build/bazel/onos_feature.py
rename to tools/build/bazel/osgi_feature_bundler.py
index 368d281..40e944a 100755
--- a/tools/build/bazel/onos_feature.py
+++ b/tools/build/bazel/osgi_feature_bundler.py
@@ -1,10 +1,26 @@
 #!/usr/bin/env python
-#FIXME Add license
+"""
+ 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.
+"""
 
 from zipfile import ZipFile, ZipInfo
 
-def generateOar(output, files=[]):
-    # Note this is not a compressed zip
+# Utility to write out the ZIP bundle containing the artifacts required by an
+# OSGI feature.
+
+def writeFeatureBundle(output, files=[]):
     with ZipFile(output, 'w') as zip:
         for file, mvnCoords in files:
             filename = file.split('/')[-1]
@@ -41,4 +57,4 @@
         sys.exit(2)
 
     files = zip(*[iter(args)]*2)
-    generateOar(output, files)
+    writeFeatureBundle(output, files)
diff --git a/tools/build/bazel/osgi_features.bzl b/tools/build/bazel/osgi_features.bzl
index 424e1a7..3ae3ce6 100644
--- a/tools/build/bazel/osgi_features.bzl
+++ b/tools/build/bazel/osgi_features.bzl
@@ -33,7 +33,7 @@
         "-t",
         ctx.attr.description,
     ]
-    bundleArgs = [ctx.outputs.bundle_zip.path]
+    bundleArgs = [ctx.outputs.feature_zip.path]
     inputs = []
 
     for dep in ctx.attr.included_bundles:
@@ -58,15 +58,15 @@
         outputs = [ctx.outputs.feature_xml],
         arguments = xmlArgs,
         progress_message = "Generating feature %s XML" % ctx.attr.name,
-        executable = ctx.executable._writer,
+        executable = ctx.executable._feature_writer,
     )
 
     ctx.actions.run(
         inputs = inputs,
-        outputs = [ctx.outputs.bundle_zip],
+        outputs = [ctx.outputs.feature_zip],
         arguments = bundleArgs,
         progress_message = "Generating feature %s bundle" % ctx.attr.name,
-        executable = ctx.executable._bundler,
+        executable = ctx.executable._feature_bundler,
     )
 
 osgi_feature = rule(
@@ -77,22 +77,22 @@
         "included_bundles": attr.label_list(),
         "excluded_bundles": attr.label_list(default = []),
         "generate_file": attr.bool(default = False),
-        "_writer": attr.label(
+        "_feature_writer": attr.label(
             executable = True,
             cfg = "host",
             allow_files = True,
-            default = Label("//tools/build/bazel:onos_app_writer"),
+            default = Label("//tools/build/bazel:onos_app_tools"),
         ),
-        "_bundler": attr.label(
+        "_feature_bundler": attr.label(
             executable = True,
             cfg = "host",
             allow_files = True,
-            default = Label("//tools/build/bazel:onos_feature"),
+            default = Label("//tools/build/bazel:osgi_feature_bundler"),
         ),
     },
     outputs = {
         "feature_xml": "feature-%{name}.xml",
-        "bundle_zip": "feature-%{name}.zip",
+        "feature_zip": "feature-%{name}.zip",
     },
     implementation = _osgi_feature_impl,
 )
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index 342a49a..6283ec8 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -55,7 +55,7 @@
 
     jar = input_file.path
     output = ctx.outputs.osgi_jar.path
-    name = ctx.attr.source.label.name
+    name = ctx.attr.name
     group = ctx.attr.group
     version = ctx.attr.version
     license = ""
diff --git a/tools/package/onos_stage.py b/tools/package/onos_stage.py
index 9221b12..5a44915 100755
--- a/tools/package/onos_stage.py
+++ b/tools/package/onos_stage.py
@@ -1,5 +1,19 @@
 #!/usr/bin/env python
-#FIXME Add license
+"""
+ Copyright 2017-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.
+"""
 
 import re
 import os