Sketching out packaging via Bazel

- modified onos-gen-lib to generate http_file properly
- fixed deps.json entries for apache-karaf & patches .tar.gz files
- sketching tools/package/BUILD file

Change-Id: I311d66095e8d744b8f61a2f3b53a635a2a42732e
diff --git a/BUILD b/BUILD
index 5934e06..03d2993 100644
--- a/BUILD
+++ b/BUILD
@@ -300,20 +300,14 @@
 APPS = ONOS_DRIVERS + ONOS_PROVIDERS + ONOS_APPS + MODELS + PIPELINES + \
        PROTOCOL_APPS
 
-FEATURES = [
-    '//features:onos-thirdparty-base',
-    '//features:onos-thirdparty-web',
-    '//features:onos-api',
-    '//features:onos-core',
-#    '//features:onos-incubator',
-#    '//features:onos-rest',
-#    '//features:onos-gui',
-#    '//features:onos-gui2',
-#    '//features:onos-cli',
-#    '//features:onos-security',
- ]
+PACKAGES = [
+    "//tools/package:onos-karaf",
+#    "//tools/package:onos-package",
+#    "//tools/package:onos-admin-tools",
+#    "//tools/package:onos-test-tools",
+]
 
 filegroup(
     name = "onos",
-    srcs = CORE + APPS + FEATURES,
+    srcs = CORE + APPS + PACKAGES,
 )
diff --git a/features/BUILD b/features/BUILD
index f188294..1957787 100644
--- a/features/BUILD
+++ b/features/BUILD
@@ -1,4 +1,4 @@
-load("//tools/build/bazel:osgi_features.bzl", "osgi_feature")
+load("//tools/build/bazel:osgi_features.bzl", "osgi_feature", "osgi_feature_repo")
 
 osgi_feature(
     name = "onos-thirdparty-base",
@@ -46,7 +46,6 @@
         "@sigar//jar",
     ],
     required_features = [],
-    visibility = ["//visibility:public"],
 )
 
 osgi_feature(
@@ -75,7 +74,6 @@
         "war",
         "onos-thirdparty-base",
     ],
-    visibility = ["//visibility:public"],
 )
 
 osgi_feature(
@@ -91,7 +89,6 @@
         "scr",
         "onos-thirdparty-base",
     ],
-    visibility = ["//visibility:public"],
 )
 
 osgi_feature(
@@ -106,6 +103,25 @@
         #        "//core/store/serializers:onos-core-serializers",
     ],
     required_features = ["onos-api"],
+)
+
+
+FEATURES = [
+    "//features:onos-thirdparty-base",
+    "//features:onos-thirdparty-web",
+    "//features:onos-api",
+    "//features:onos-core",
+#    "//features:onos-incubator",
+#    "//features:onos-rest",
+#    "//features:onos-gui",
+#    "//features:onos-gui2",
+#    "//features:onos-cli",
+#    "//features:onos-security",
+]
+
+osgi_feature_repo(
+    name = "onos-features",
+    exported_features = FEATURES,
     visibility = ["//visibility:public"],
 )
 
diff --git a/lib/BUCK b/lib/BUCK
index bf0b06e..a6ecd88 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Wed, 6 Jun 2018 21:12:44 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Mon, 11 Jun 2018 22:35:36 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 pass_thru_pom(
@@ -1198,7 +1198,7 @@
 remote_file (
   name = 'apache-karaf',
   out = 'apache-karaf-offline-3.0.8.tar.gz',
-  url = 'mvn:org.onosproject:apache-karaf-offline:tar.gz:3.0.8',
+  url = 'http://repo1.maven.org/maven2/org/onosproject/apache-karaf-offline/3.0.8/apache-karaf-offline-3.0.8.tar.gz',
   sha1 = '2503c5853285e3414da15aef8d8f3f629747c927',
   visibility = [ 'PUBLIC' ],
 )
@@ -1206,7 +1206,7 @@
 remote_file (
   name = 'apache-karaf-patches',
   out = 'onos-apache-karaf-patches-3.0.8.tar.gz',
-  url = 'mvn:org.onosproject:onos-apache-karaf-patches:tar.gz:3.0.8',
+  url = 'http://repo1.maven.org/maven2/org/onosproject/onos-apache-karaf-patches/3.0.8/onos-apache-karaf-patches-3.0.8.tar.gz',
   sha1 = 'be5d620a2c4f4c5faa520d2c1b949ad0cc29c84f',
   visibility = [ 'PUBLIC' ],
 )
diff --git a/lib/deps.json b/lib/deps.json
index 9fbcb98..cf76971 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -226,8 +226,8 @@
     "typesafe-config": "mvn:com.typesafe:config:1.2.1",
     "validation-api": "mvn:javax.validation:validation-api:1.1.0.Final",
     "checkstyle": "mvn:com.puppycrawl.tools:checkstyle:8.10",
-    "apache-karaf": "mvn:org.onosproject:apache-karaf-offline:tar.gz:3.0.8",
-    "apache-karaf-patches": "mvn:org.onosproject:onos-apache-karaf-patches:tar.gz:3.0.8",
+    "apache-karaf": "http://repo1.maven.org/maven2/org/onosproject/apache-karaf-offline/3.0.8/apache-karaf-offline-3.0.8.tar.gz",
+    "apache-karaf-patches": "http://repo1.maven.org/maven2/org/onosproject/onos-apache-karaf-patches/3.0.8/onos-apache-karaf-patches-3.0.8.tar.gz",
     "bndlib": "mvn:biz.aQute.bnd:biz.aQute.bndlib:jar:3.1.0",
     "bndexe": "mvn:biz.aQute.bnd:biz.aQute.bnd:3.1.0",
     "libthrift": "mvn:org.apache.thrift:libthrift:0.9.3",
diff --git a/tools/build/bazel/generate_workspace.bzl b/tools/build/bazel/generate_workspace.bzl
index 6e3a5d4..d899c70 100644
--- a/tools/build/bazel/generate_workspace.bzl
+++ b/tools/build/bazel/generate_workspace.bzl
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Wed, 6 Jun 2018 21:12:50 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Mon, 11 Jun 2018 22:35:42 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION")
@@ -790,16 +790,16 @@
         sha1 = "d7cd09c4d1240b3acc3041fcb47fd74ae351698b",
     )
 
-    native.http_jar(
+    native.http_file(
         name = "apache_karaf",
-        url = "mvn:org.onosproject:apache-karaf-offline:tar.gz:3.0.8",
-        sha256 = "2503c5853285e3414da15aef8d8f3f629747c927",
+        url = "http://repo1.maven.org/maven2/org/onosproject/apache-karaf-offline/3.0.8/apache-karaf-offline-3.0.8.tar.gz",
+        sha256 = "2f20bb25ded39f1e9e4148bb840473c89249bbb592106f5451b5a829a5ec8603",
     )
 
-    native.http_jar(
+    native.http_file(
         name = "apache_karaf_patches",
-        url = "mvn:org.onosproject:onos-apache-karaf-patches:tar.gz:3.0.8",
-        sha256 = "be5d620a2c4f4c5faa520d2c1b949ad0cc29c84f",
+        url = "http://repo1.maven.org/maven2/org/onosproject/onos-apache-karaf-patches/3.0.8/onos-apache-karaf-patches-3.0.8.tar.gz",
+        sha256 = "1f9e3e8a53a462f776a931fe851819a43dfe7728b31fbde339b98d04908e702b",
     )
 
     native.maven_jar(
@@ -2208,8 +2208,8 @@
 artifact_map[str(Label("@typesafe_config//jar"))] = "mvn:com.typesafe:config:jar:1.2.1"
 artifact_map[str(Label("@validation_api//jar"))] = "mvn:javax.validation:validation-api:jar:1.1.0.Final"
 artifact_map[str(Label("@checkstyle//jar"))] = "mvn:com.puppycrawl.tools:checkstyle:jar:8.10"
-artifact_map[str(Label("@apache_karaf//jar"))] = "mvn:org.onosproject:apache-karaf-offline:tar.gz:3.0.8"
-artifact_map[str(Label("@apache_karaf_patches//jar"))] = "mvn:org.onosproject:onos-apache-karaf-patches:tar.gz:3.0.8"
+artifact_map[str(Label("@apache_karaf//jar"))] = "http://repo1.maven.org/maven2/org/onosproject/apache-karaf-offline/3.0.8/apache-karaf-offline-3.0.8.tar.gz"
+artifact_map[str(Label("@apache_karaf_patches//jar"))] = "http://repo1.maven.org/maven2/org/onosproject/onos-apache-karaf-patches/3.0.8/onos-apache-karaf-patches-3.0.8.tar.gz"
 artifact_map[str(Label("@bndlib//jar"))] = "mvn:biz.aQute.bnd:biz.aQute.bndlib:jar:3.1.0"
 artifact_map[str(Label("@bndexe//jar"))] = "mvn:biz.aQute.bnd:biz.aQute.bnd:jar:3.1.0"
 artifact_map[str(Label("@libthrift//jar"))] = "mvn:org.apache.thrift:libthrift:jar:0.9.3"
diff --git a/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckArtifact.java b/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckArtifact.java
index b2c7f15..b6665d5 100644
--- a/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckArtifact.java
+++ b/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckArtifact.java
@@ -132,7 +132,7 @@
             return String.format(format, jarTarget(), mavenCoords(), sha, repoAttribute);
         } else {
             String format =
-                    "\n    native.http_jar(\n" +
+                    "\n    native.http_file(\n" +
                             "        name = \"%s\",\n" +
                             "        url = \"%s\",\n" +
                             "        sha256 = \"%s\",\n" +
diff --git a/tools/package/BUILD b/tools/package/BUILD
new file mode 100644
index 0000000..1a65642
--- /dev/null
+++ b/tools/package/BUILD
@@ -0,0 +1,72 @@
+load("//tools/build/bazel:generate_workspace.bzl", "ONOS_VERSION")
+
+KARAF = "@apache_karaf//file"
+PATCHES = "@apache_karaf_patches//file"
+BRANDING = "//tools/package/branding:onos-tools-package-branding"
+
+# FIXME: This is still work in progress
+genrule(
+    name = "onos-karaf",
+    srcs = [
+        KARAF,
+        PATCHES,
+        BRANDING,
+      ] + 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)" \
+              % (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),
+
+PACKAGING_REQUIREMENTS = [
+    "//features:onos-features",
+    ":onos-karaf",
+]
+
+# FIXME: This is still work in progress
+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)",
+    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),
+
+
+#staged_repos = ['$(location %s-repo)' % f for f in FEATURES]
+#staged_apps = ['$(location %s)' % a for a in APPS]
+#
+## feature_coords = 'foo:bar:1.3'
+#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',
+#  srcs = [ 'onos-run-karaf' ],
+#  bash = 'sed "s#ONOS_TAR=#ONOS_TAR=$(location :onos-package)#" $SRCS > $OUT; chmod +x $OUT',
+#  executable = True,
+#  visibility = [ 'PUBLIC' ],
+#)
diff --git a/tools/package/branding/BUILD b/tools/package/branding/BUILD
new file mode 100644
index 0000000..72fa352
--- /dev/null
+++ b/tools/package/branding/BUILD
@@ -0,0 +1,5 @@
+
+java_library(
+    name = "onos-tools-package-branding",
+    visibility = ["//visibility:public"],
+)
\ No newline at end of file
diff --git a/tools/build/bazel/onos_stage.py b/tools/package/onos_stage.py
similarity index 100%
rename from tools/build/bazel/onos_stage.py
rename to tools/package/onos_stage.py