Implement bazel support for web context

- add web context to OSGI rules
- build ACL app
- build DHCP app

Change-Id: I03687d109eb44621458ad7269a435e03eec47495
diff --git a/apps/acl/BUILD b/apps/acl/BUILD
new file mode 100644
index 0000000..58e48ae
--- /dev/null
+++ b/apps/acl/BUILD
@@ -0,0 +1,22 @@
+COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + [
+    "@javax_ws_rs_api//jar",
+    "//utils/rest:onlab-rest",
+    "//core/store/serializers:onos-core-serializers",
+]
+
+TEST_DEPS = TEST_REST + [
+    "@jersey_server//jar",
+]
+
+osgi_jar_with_tests(
+    test_deps = TEST_DEPS,
+    web_context = "/onos/v1/acl",
+    deps = COMPILE_DEPS,
+)
+
+onos_app(
+    category = "Security",
+    description = "ONOS ACL application.",
+    title = "Access Control Lists",
+    url = "http://onosproject.org",
+)
diff --git a/apps/dhcp/BUILD b/apps/dhcp/BUILD
new file mode 100644
index 0000000..dbf2ac4
--- /dev/null
+++ b/apps/dhcp/BUILD
@@ -0,0 +1,11 @@
+BUNDLES = [
+    "//apps/dhcp/api:onos-apps-dhcp-api",
+    "//apps/dhcp/app:onos-apps-dhcp-app",
+]
+
+onos_app(
+    category = "Utility",
+    included_bundles = BUNDLES,
+    title = "DHCP Server",
+    url = "http://onosproject.org",
+)
diff --git a/apps/dhcp/api/BUILD b/apps/dhcp/api/BUILD
new file mode 100644
index 0000000..f72e3bc
--- /dev/null
+++ b/apps/dhcp/api/BUILD
@@ -0,0 +1,3 @@
+osgi_jar_with_tests(
+    deps = CORE_DEPS,
+)
diff --git a/apps/dhcp/app/BUILD b/apps/dhcp/app/BUILD
new file mode 100644
index 0000000..5d1f800
--- /dev/null
+++ b/apps/dhcp/app/BUILD
@@ -0,0 +1,18 @@
+COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + [
+    "@javax_ws_rs_api//jar",
+    "@org_apache_karaf_shell_console//jar",
+    "//apps/dhcp/api:onos-apps-dhcp-api",
+    "//utils/rest:onlab-rest",
+    "//core/store/serializers:onos-core-serializers",
+    "//cli:onos-cli",
+]
+
+osgi_jar_with_tests(
+    api_description = "REST API for DHCP Server",
+    api_package = "org.onosproject.dhcp.rest",
+    api_title = "DHCP Server",
+    api_version = "1.0",
+    test_deps = TEST_ADAPTERS,
+    web_context = "/onos/dhcp",
+    deps = COMPILE_DEPS,
+)
diff --git a/modules.bzl b/modules.bzl
index 33064c5..30ed0a6 100644
--- a/modules.bzl
+++ b/modules.bzl
@@ -147,11 +147,11 @@
 
 ONOS_APPS = [
     # Apps
-    #"//apps/dhcp:onos-apps-dhcp-oar",
+    "//apps/dhcp:onos-apps-dhcp-oar",
     #"//apps/dhcprelay:onos-apps-dhcprelay-oar",
     #"//apps/fwd:onos-apps-fwd-oar",
     #"//apps/packet-stats:onos-apps-packet-stats-oar",
-    #"//apps/acl:onos-apps-acl-oar",
+    "//apps/acl:onos-apps-acl-oar",
     #"//apps/bgprouter:onos-apps-bgprouter-oar",
     #"//apps/cip:onos-apps-cip-oar",
     #"//apps/drivermatrix:onos-apps-drivermatrix-oar",
@@ -280,8 +280,8 @@
 APP_JARS = [
     #"//apps/cpman/api:onos-apps-cpman-api",
     #"//apps/routing-api:onos-apps-routing-api",
-    #"//apps/dhcp/api:onos-apps-dhcp-api",
-    #"//apps/dhcp/app:onos-apps-dhcp-app",
+    "//apps/dhcp/api:onos-apps-dhcp-api",
+    "//apps/dhcp/app:onos-apps-dhcp-app",
     #"//apps/imr/api:onos-apps-imr-api",
     #"//apps/imr/app:onos-apps-imr-app",
     #"//apps/dhcprelay:onos-apps-dhcprelay",
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index 6c8a381..c490960 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -33,6 +33,9 @@
     else:
         return native.glob([resources_root + "**"])
 
+def _webapp():
+    return native.glob(["src/main/webapp/**"])
+
 # Implementation of the rule to call bnd to make an OSGI jar file
 def _bnd_impl(ctx):
     if (len(ctx.files.source) == 1):
@@ -56,7 +59,10 @@
     import_packages = ctx.attr.import_packages
     exportPackages = "*"
     includeResources = ""
-    webContext = "NONE"
+    web_context = ctx.attr.web_context
+    if web_context == None or web_context == "":
+        web_context = "NONE"
+    web_xml = ctx.attr.web_xml
     dynamicimportPackages = ""
     cp = ""
 
@@ -83,6 +89,11 @@
         progress_message = "Expanding jar file: %s" % jar,
     )
     inputDependencies += [classes]
+    web_xml_root_path = ""
+    if len(web_xml) != 0:
+        web_xml_root = web_xml[0].files.to_list()[0]
+        inputDependencies += [web_xml_root]
+        web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "")
 
     # call bnd to make the OSGI jar file
     arguments = [
@@ -96,7 +107,8 @@
         import_packages,
         exportPackages,
         includeResources,
-        webContext,
+        web_context,
+        web_xml_root_path,
         dynamicimportPackages,
         classesPath,
     ]
@@ -124,6 +136,8 @@
         "package_name_root": attr.string(),
         "source": attr.label(),
         "import_packages": attr.string(),
+        "web_context": attr.string(),
+        "web_xml": attr.label_list(allow_files = True),
         "_bnd_exe": attr.label(
             executable = True,
             cfg = "host",
@@ -138,8 +152,26 @@
     implementation = _bnd_impl,
 )
 
-def wrapped_osgi_jar(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", import_packages = "*", visibility = ["//visibility:private"]):
-    _bnd(name = name, source = jar, deps = deps, version = version, package_name_root = package_name_root, visibility = visibility, import_packages = import_packages)
+def wrapped_osgi_jar(
+        name,
+        jar,
+        deps,
+        version = ONOS_VERSION,
+        package_name_root = "org.onosproject",
+        import_packages = "*",
+        web_context = None,
+        web_xml = None,
+        visibility = ["//visibility:private"]):
+    _bnd(
+        name = name,
+        source = jar,
+        deps = deps,
+        version = version,
+        package_name_root = package_name_root,
+        visibility = visibility,
+        import_packages = import_packages,
+        web_xml = web_xml,
+    )
 
 def osgi_jar_with_tests(
         name = None,
@@ -154,6 +186,11 @@
         test_resources = None,
         visibility = ["//visibility:public"],
         version = ONOS_VERSION,
+        web_context = None,
+        api_title = "",
+        api_version = "",
+        api_description = "",
+        api_package = "",
         import_packages = None):
     if name == None:
         name = "onos-" + native.package_name().replace("/", "-")
@@ -176,8 +213,11 @@
     tests_name = name + "-tests"
     tests_jar_deps = list(depset(deps + test_deps)) + [name]
     all_test_deps = tests_jar_deps + [tests_name]
+    web_xml = _webapp()
 
+    # compile the Java code
     native.java_library(name = name + "-native", srcs = srcs, resources = resources, deps = deps, visibility = visibility)
+
     _bnd(
         name = name,
         source = name + "-native",
@@ -186,6 +226,8 @@
         package_name_root = package_name_root,
         visibility = visibility,
         import_packages = import_packages,
+        web_context = web_context,
+        web_xml = web_xml,
     )
     if test_srcs != []:
         native.java_library(
@@ -214,7 +256,7 @@
         visibility = ["//visibility:public"],
         version = ONOS_VERSION,
         # TODO - implement these for swagger and web.xml
-        web_context = "",
+        web_context = None,
         api_title = "",
         api_version = "",
         api_description = "",
@@ -238,4 +280,5 @@
         visibility = visibility,
         version = version,
         import_packages = import_packages,
+        web_context = web_context,
     )
diff --git a/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java b/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
index fe286cb..c819ebd 100644
--- a/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
+++ b/utils/osgiwrap/src/main/java/org/onlab/osgiwrap/OSGiWrapper.java
@@ -74,6 +74,7 @@
     private String bundleLicense;
 
     private String webContext;
+    private String webXmlRoot;
     private String destdir;
 
     // FIXME should consider using Commons CLI, etc.
@@ -94,8 +95,9 @@
         String exportPackages = args[8];
         String includeResources = args[9];
         String webContext = args[10];
-        String dynamicimportPackages = args[11];
-        String destdir = args[12];
+        String webXmlRoot = args[11];
+        String dynamicimportPackages = args[12];
+        String destdir = args[13];
         String desc = Joiner.on(' ').join(Arrays.copyOfRange(args, 12, args.length));
 
         OSGiWrapper wrapper = new OSGiWrapper(jar, output, cp,
@@ -104,6 +106,7 @@
                                               importPackages, exportPackages,
                                               includeResources,
                                               webContext,
+                                              webXmlRoot,
                                               dynamicimportPackages,
                                               desc,
                                               destdir);
@@ -126,6 +129,7 @@
                        String exportPackages,
                        String includeResources,
                        String webContext,
+                       String webXmlRoot,
                        String dynamicimportPackages,
                        String bundleDescription,
                        String destdir) {
@@ -155,6 +159,7 @@
         }
 
         this.webContext = webContext;
+        this.webXmlRoot = webXmlRoot;
         this.destdir = destdir;
     }
 
@@ -184,7 +189,7 @@
         }
 
         if (isWab()) {
-            analyzer.setProperty(Analyzer.WAB, "src/main/webapp/");
+            analyzer.setProperty(Analyzer.WAB, webXmlRoot);
             analyzer.setProperty("Web-ContextPath", webContext);
             analyzer.setProperty(Analyzer.IMPORT_PACKAGE, "*,org.glassfish.jersey.servlet,org.jvnet.mimepull\n");
         }
@@ -281,6 +286,7 @@
         }
 
         Path wabRoot = Paths.get(wab);
+        log("wab root " + wabRoot.toString());
         includeFiles(dot, null, wabRoot.toString());
     }