Adding Bazel packaging of admin tools and test tools.

Change-Id: I321c79264cf5f3ca0d6c7a52e300c916acbe463a
diff --git a/BUILD b/BUILD
index 03d2993..c4daba4 100644
--- a/BUILD
+++ b/BUILD
@@ -301,13 +301,19 @@
        PROTOCOL_APPS
 
 PACKAGES = [
-    "//tools/package:onos-karaf",
-#    "//tools/package:onos-package",
-#    "//tools/package:onos-admin-tools",
-#    "//tools/package:onos-test-tools",
+    "//tools/package:onos-package-admin",
+    "//tools/package:onos-package-test",
+    "//tools/package:onos-package",
 ]
 
 filegroup(
     name = "onos",
     srcs = CORE + APPS + PACKAGES,
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "onos-env-defaults",
+    srcs = ["tools/build/envDefaults"],
+    visibility = ["//visibility:public"],
 )
diff --git a/tools/dev/BUILD b/tools/dev/BUILD
new file mode 100644
index 0000000..966d711
--- /dev/null
+++ b/tools/dev/BUILD
@@ -0,0 +1,17 @@
+
+filegroup(
+    name = "onos-admin",
+    srcs = [
+        "bin/onos-create-app",
+    ],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "onos-test",
+    srcs = [
+        "bash_profile",
+        "bin/onos-create-app",
+    ],
+    visibility = ["//visibility:public"],
+)
\ No newline at end of file
diff --git a/tools/package/BUILD b/tools/package/BUILD
index 1a65642..26638d4 100644
--- a/tools/package/BUILD
+++ b/tools/package/BUILD
@@ -4,40 +4,63 @@
 PATCHES = "@apache_karaf_patches//file"
 BRANDING = "//tools/package/branding:onos-tools-package-branding"
 
-# FIXME: This is still work in progress
+BRANDED_KARAF_REQUIREMENTS = [
+    KARAF,
+    PATCHES,
+    BRANDING,
+]
+
+# Generates auxiliary karaf.zip file; branded and augmented with ONOS runtime tools
 genrule(
     name = "onos-karaf",
-    srcs = [
-        KARAF,
-        PATCHES,
-        BRANDING,
-      ] + glob(["bin/*", "etc/*", "init/*", "runtime/bin/*"]),
+    srcs = BRANDED_KARAF_REQUIREMENTS + glob(["bin/*", "etc/*", "init/*", "runtime/bin/*"]),
     outs = ["karaf.zip"],
-    cmd = "echo $(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s) > $(location karaf.zip)" \
+    cmd = "$(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s) tools/package" \
               % (KARAF, ONOS_VERSION, BRANDING, PATCHES),
     tools = ["onos-prep-karaf"],
-    visibility = ["//visibility:public"],
 )
-#    cmd = "$(location onos-prep-karaf) $(location karaf.zip) $(location %s) %s $(location %s) $(location %s)" \
-#              % (KARAF, ONOS_VERSION), BRANDING, PATCHES),
 
+# FIXME: Move /features to under tools/package/features
 PACKAGING_REQUIREMENTS = [
     "//features:onos-features",
     ":onos-karaf",
 ]
 
-# FIXME: This is still work in progress
+# FIXME: Need to add OAR files as dependencies
+# Generates the principal distributable onos.tar.gz file
 genrule(
     name = "onos-package",
     srcs = PACKAGING_REQUIREMENTS + glob(["bin/*", "etc/*", "init/*", "config/*", "runtime/bin/*"]),
     outs = ["onos.tar.gz"],
-    cmd = "echo $(location onos_stage.py) $(location onos.tar.gz) $(location :onos-karaf) $(location //features:onos-features) $(SRCS) >$(location onos.tar.gz)",
+    cmd = "$(location onos_stage.py) $(location onos.tar.gz) %s $(location :onos-karaf) $(SRCS)" % ONOS_VERSION,
     visibility = ["//visibility:public"],
     tools = ["onos_stage.py"],
 )
-#    cmd = "$(location onos_stage.py) $(location onos.tar.gz) $(location :onos-karaf) $(SRCS)",
-#    cmd = "$(exe //buck-tools:onos-stage) $OUT " + ONOS_VERSION + " $(location :onos-karaf) " + " ".join(sources),
 
+# Generates auxiliary tar file with ONOS runtime tools
+filegroup(
+    name = "onos-runtime-tools",
+    srcs = glob(["runtime/bin/*"]),
+)
+
+# Generates the onos-admin.tar.gz file with remote admin tools
+genrule(
+    name = "onos-package-admin",
+    srcs = ["//tools/dev:onos-admin", "//tools/test:onos-admin", ":onos-runtime-tools"],
+    outs = ["onos-admin.tar.gz"],
+    cmd = "mkdir onos-admin-%s; cp $(SRCS) onos-admin-%s; tar zcf $(location onos-admin.tar.gz) onos-admin-%s"\
+          % (ONOS_VERSION, ONOS_VERSION, ONOS_VERSION),
+    visibility = ["//visibility:public"],
+)
+
+genrule(
+    name = "onos-package-test",
+    srcs = ["//tools/dev:onos-test", "//tools/test:onos-test", ":onos-runtime-tools", "//:onos-env-defaults"],
+    outs = ["onos-test.tar.gz"],
+    cmd = "mkdir onos-test-%s; cp -r tools onos-test-%s; tar zcf $(location onos-test.tar.gz) onos-test-%s"\
+          % (ONOS_VERSION, ONOS_VERSION, ONOS_VERSION),
+    visibility = ["//visibility:public"],
+)
 
 #staged_repos = ['$(location %s-repo)' % f for f in FEATURES]
 #staged_apps = ['$(location %s)' % a for a in APPS]
@@ -46,22 +69,6 @@
 #sources = [ '$(location :onos-features)', ]
 #sources += staged_repos + staged_apps
 #
-#tar_file(
-#  name = 'onos-package-runtime',
-#  srcs = glob(['runtime/bin/*']),
-#  root = 'tools/package',
-#  out = 'package-runtime.tar.gz',
-#  visibility = [ 'PUBLIC' ],
-#)
-#
-#genrule(
-#  name = 'onos-package',
-#  srcs = glob(['bin/*', 'etc/*', 'init/*', 'config/*', 'runtime/bin/*']),
-#  out = 'onos.tar.gz',
-#  bash = '$(exe //buck-tools:onos-stage) $OUT ' + ONOS_VERSION + ' $(location :onos-karaf) ' + ' '.join(sources),
-#  visibility = [ 'PUBLIC' ],
-#)
-#
 #genrule(
 #  name = 'onos-run',
 #  out = 'onos-run',
diff --git a/tools/package/onos-prep-karaf b/tools/package/onos-prep-karaf
index a466858..cb30a43 100755
--- a/tools/package/onos-prep-karaf
+++ b/tools/package/onos-prep-karaf
@@ -10,6 +10,8 @@
 ONOS_VERSION=$3
 BRANDING=$4
 KARAF_PATCHES=$5
+SANDBOX=${6:-.}
+
 #FIXME karaf version
 KARAF_VERSION="3.0.8"
 ONOS_SECURITY_MODE="false"
@@ -33,9 +35,9 @@
     $KARAF_DIR/etc/org.ops4j.pax.logging.cfg
 
 # Patch-in proper Karaf version into the startup script
-sed -i.bk "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" bin/onos-service
+sed -i.bk "s/apache-karaf-\$KARAF_VERSION/$KARAF_DIR/g" $SANDBOX/bin/onos-service
 rm -f bin/*.bk
-chmod a+x bin/onos-service bin/onos
+chmod a+x $SANDBOX/bin/onos-service $SANDBOX/bin/onos
 
 export BOOT_FEATURES="standard,ssh,scr,war,webconsole,onos-api,onos-core,onos-incubator,onos-cli,onos-rest,onos-gui"
 
@@ -56,10 +58,10 @@
 mv $KARAF_DIR $PREFIX
 
 # Stage the ONOS admin scripts and patch in Karaf service wrapper extras
-cp -r bin $PREFIX
-cp -r runtime/bin/* $PREFIX/bin/
-cp -r init $PREFIX
-cp -r etc/* $PREFIX/$KARAF_DIR/etc/
+cp -r $SANDBOX/bin $PREFIX
+cp -r $SANDBOX/runtime/bin/* $PREFIX/bin/
+cp -r $SANDBOX/init $PREFIX
+cp -r $SANDBOX/etc/* $PREFIX/$KARAF_DIR/etc/
 
 if [ "$ONOS_SECURITY_MODE" = true ]
 then
diff --git a/tools/test/BUILD b/tools/test/BUILD
new file mode 100644
index 0000000..db476dd
--- /dev/null
+++ b/tools/test/BUILD
@@ -0,0 +1,14 @@
+
+filegroup(
+    name = "onos-admin",
+    srcs = [
+        "bin/onos",
+      ],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "onos-test",
+    srcs = glob(["**/*"]),
+    visibility = ["//visibility:public"],
+)
\ No newline at end of file