Adding ability to build project ├╝ber javadocs.

Change-Id: If57e8dc7a6bea62490c33bbcf635c8dab5eadba2
diff --git a/docs/BUILD b/docs/BUILD
new file mode 100644
index 0000000..883397d
--- /dev/null
+++ b/docs/BUILD
@@ -0,0 +1,312 @@
+load("//tools/build/bazel:project_javadoc.bzl", "project_javadoc")
+load("//tools/build/bazel:generate_workspace.bzl", "ONOS_VERSION", "CORE_DEPS")
+
+SOURCES = glob(["src/main/javadoc/doc-files/*"]) + [
+    "src/main/javadoc/overview.html",
+    "//utils/osgi:onlab-osgi-sources",
+    "//utils/junit:onlab-junit-sources",
+    "//utils/misc:onlab-misc-sources",
+    "//utils/rest:onlab-rest-sources",
+    "//core/api:onos-api-sources",
+    "//incubator/api:onos-incubator-api-sources",
+    "//core/net:onos-core-net-sources",
+    "//core/common:onos-core-common-sources",
+    "//core/store/primitives:onos-core-primitives-sources",
+    "//core/store/serializers:onos-core-serializers-sources",
+    "//core/store/dist:onos-core-dist-sources",
+    "//core/security:onos-security-sources",
+    "//core/store/persistence:onos-core-persistence-sources",
+    "//incubator/net:onos-incubator-net-sources",
+    "//incubator/store:onos-incubator-store-sources",
+    "//incubator/rpc:onos-incubator-rpc-sources",
+    "//cli:onos-cli-sources",
+    "//web/api:onos-rest",
+
+    "//apps/cpman/api:onos-apps-cpman-api-sources",
+    "//apps/routing-api:onos-apps-routing-api-sources",
+    "//apps/dhcp/api:onos-apps-dhcp-api-sources",
+]
+
+INTERNAL_SOURCES = [
+    "//protocols/rest/api:onos-protocols-rest-api-sources",
+    "//protocols/rest/ctl:onos-protocols-rest-ctl-sources",
+    "//protocols/bgp/bgpio:onos-protocols-bgp-bgpio-sources",
+    "//protocols/bgp/api:onos-protocols-bgp-api-sources",
+    "//protocols/bgp/ctl:onos-protocols-bgp-ctl-sources",
+    "//protocols/netconf/api:onos-protocols-netconf-api-sources",
+    "//protocols/netconf/ctl:onos-protocols-netconf-ctl-sources",
+    "//protocols/openflow/api:onos-protocols-openflow-api-sources",
+    "//protocols/openflow/ctl:onos-protocols-openflow-ctl-sources",
+    "//protocols/ospf/api:onos-protocols-ospf-api-sources",
+    "//protocols/ospf/protocol:onos-protocols-ospf-protocol-sources",
+    "//protocols/ospf/ctl:onos-protocols-ospf-ctl-sources",
+    "//protocols/ovsdb/rfc:onos-protocols-ovsdb-rfc-sources",
+    "//protocols/ovsdb/api:onos-protocols-ovsdb-api-sources",
+    "//protocols/ovsdb/ctl:onos-protocols-ovsdb-ctl-sources",
+#    "//protocols/p4runtime/api:onos-protocols-p4runtime-api-sources",
+#    "//protocols/p4runtime/model:onos-protocols-p4runtime-model-sources",
+    "//protocols/pcep/pcepio:onos-protocols-pcep-pcepio-sources",
+    "//protocols/pcep/server/api:onos-protocols-pcep-server-api-sources",
+    "//protocols/pcep/server/ctl:onos-protocols-pcep-server-ctl-sources",
+    "//protocols/snmp/api:onos-protocols-snmp-api-sources",
+    "//protocols/snmp/ctl:onos-protocols-snmp-ctl-sources",
+    "//protocols/isis/api:onos-protocols-isis-api-sources",
+    "//protocols/isis/ctl:onos-protocols-isis-ctl-sources",
+    "//protocols/isis/isisio:onos-protocols-isis-isisio-sources",
+    "//protocols/lisp/api:onos-protocols-lisp-api-sources",
+    "//protocols/lisp/ctl:onos-protocols-lisp-ctl-sources",
+    "//protocols/lisp/msg:onos-protocols-lisp-msg-sources",
+    "//protocols/tl1/api:onos-protocols-tl1-api-sources",
+    "//protocols/tl1/ctl:onos-protocols-tl1-ctl-sources",
+    "//protocols/restconf/client/api:onos-protocols-restconf-client-api-sources",
+    "//protocols/restconf/client/ctl:onos-protocols-restconf-client-ctl-sources",
+    "//protocols/xmpp/core/api:onos-protocols-xmpp-core-api-sources",
+    "//protocols/xmpp/core/ctl:onos-protocols-xmpp-core-ctl-sources",
+
+    "//drivers/utilities:onos-drivers-utilities-sources",
+
+    "//providers/netconf/device:onos-providers-netconf-device-sources",
+    "//providers/openflow/device:onos-providers-openflow-device-sources",
+    "//providers/openflow/packet:onos-providers-openflow-packet-sources",
+    "//providers/openflow/flow:onos-providers-openflow-flow-sources",
+    "//providers/openflow/group:onos-providers-openflow-group-sources",
+    "//providers/openflow/meter:onos-providers-openflow-meter-sources",
+    "//providers/ovsdb/device:onos-providers-ovsdb-device-sources",
+    "//providers/ovsdb/tunnel:onos-providers-ovsdb-tunnel-sources",
+    "//providers/rest/device:onos-providers-rest-device-sources",
+    "//providers/snmp/device:onos-providers-snmp-device-sources",
+    "//providers/isis/cfg:onos-providers-isis-cfg-sources",
+    "//providers/isis/topology:onos-providers-isis-topology-sources",
+    "//providers/lisp/device:onos-providers-lisp-device-sources",
+    "//providers/tl1/device:onos-providers-tl1-device-sources",
+    "//providers/general/device:onos-providers-general-device-sources",
+#    "//providers/p4runtime/packet:onos-providers-p4runtime-packet-sources",
+
+    "//apps/pcep-api:onos-apps-pcep-api-sources",
+    "//apps/optical-model:onos-apps-optical-model-sources",
+    "//apps/mappingmanagement/api:onos-apps-mappingmanagement-api-sources",
+]
+
+JAVADOC_DEPS = CORE_DEPS + KRYO + REST + CLI + JACKSON + TEST + NETTY + METRICS + ONOS_YANG + [
+    "@netty//jar",
+    "@netty_codec//jar",
+    "@netty_transport//jar",
+    "@netty_handler//jar",
+    "@netty_transport_native_epoll//jar",
+    "@atomix//jar",
+    "@commons_math3//jar",
+    "@joda_time//jar",
+    "@mapdb//jar",
+    "@httpclient_osgi//jar",
+    "@httpcore_osgi//jar",
+    "@bndlib//jar",
+    "@jersey_client//jar",
+    "@jersey_server//jar",
+    "@jersey_security//jar",
+    "@org_apache_felix_scr_bnd//jar",
+    "@openflowj//jar",
+    "@org_apache_servicemix_bundles_snmp4j//jar",
+    "@snmp_core//jar",
+    "@mibs_net_snmp//jar",
+    "@mibs_rfc//jar",
+    "@tinder_xmpp//jar",
+    "@aalto_xml//jar",
+    "@stax2_api//jar",
+    "@bcprov_jdk15on//jar",
+    "@bcpkix_jdk15on//jar",
+    "@sshd_core//jar",
+    "@ganymed_ssh2//jar",
+    "@concurrent_trees//jar",
+]
+
+EXTERNAL_GROUPS = {
+    "Network Model & Services": [ "org.onosproject.*" ],
+    "Incubator for Network Model & Services": [ "org.onosproject.incubator.*" ],
+    "Utilities": [ "org.onlab.*" ],
+    "App & Extensions": [
+        "org.onosproject.dhcp",
+        "org.onosproject.cpman",
+        "org.onosproject.routing",
+    ],
+}
+
+INTERNAL_GROUPS = {
+    "Network Model & Services": [ "org.onosproject.*" ],
+    "Incubator for Network Model & Services": [ "org.onosproject.incubator.*" ],
+    "Core Subsystems": [
+        "org.onosproject.cfg.impl",
+        "org.onosproject.cluster.impl",
+        "org.onosproject.core.impl",
+        "org.onosproject.event.impl",
+        "org.onosproject.net.config.impl",
+        "org.onosproject.net.device.impl",
+        "org.onosproject.net.driver.impl",
+        "org.onosproject.net.edgeservice.impl",
+        "org.onosproject.net.flow.impl",
+        "org.onosproject.net.flowobjective.impl",
+        "org.onosproject.net.flowobjective.impl.composition",
+        "org.onosproject.net.group.impl",
+        "org.onosproject.net.host.impl",
+        "org.onosproject.net.intent.impl",
+        "org.onosproject.net.intent.impl.compiler",
+        "org.onosproject.net.intent.impl.phase",
+        "org.onosproject.net.intf.impl",
+        "org.onosproject.net.link.impl",
+        "org.onosproject.net.newresource.impl",
+        "org.onosproject.net.packet.impl",
+        "org.onosproject.net.proxyarp.impl",
+        "org.onosproject.net.resource.impl",
+        "org.onosproject.net.statistic.impl",
+        "org.onosproject.net.topology.impl",
+        "org.onosproject.json",
+        "org.onosproject.json.*",
+        "org.onosproject.common.*",
+        "org.onosproject.security.impl",
+        "org.onosproject.security.store",
+    ],
+    "Distributed Stores": [
+        "org.onosproject.store.*",
+        "org.onosproject.persistence.impl",
+    ],
+    "Incubator for Core Subsystems & Distributed Stores": [
+        "org.onosproject.incubator.net.impl",
+        "org.onosproject.incubator.store.impl",
+        "org.onosproject.incubator.net.resource.label.impl",
+        "org.onosproject.incubator.store.resource.impl",
+        "org.onosproject.incubator.net.tunnel.impl",
+        "org.onosproject.incubator.store.tunnel.impl",
+        "org.onosproject.incubator.net.config.impl",
+        "org.onosproject.incubator.net.domain.impl",
+        "org.onosproject.incubator.store.config.impl",
+        "org.onosproject.incubator.net.meter.impl",
+        "org.onosproject.incubator.net.resource.label",
+        "org.onosproject.incubator.store.meter.impl",
+    ],
+    "Utilities": [ "org.onlab.*" ],
+    "GUI, REST & Command-Line": [
+        "org.onosproject.ui.impl*",
+        "org.onosproject.rest.*",
+        "org.onosproject.cli*",
+        "org.onosproject.codec.impl",
+    ],
+    "Builtin Applications": [
+        "org.onosproject.app.*",
+        "org.onosproject.acl*",
+        "org.onosproject.aaa",
+        "org.onosproject.fwd",
+        "org.onosproject.dhcp*",
+        "org.onosproject.flowanalyzer",
+        "org.onosproject.cpman*",
+        "org.onosproject.mobility",
+        "org.onosproject.proxyarp",
+        "org.onosproject.calendar",
+        "org.onosproject.olt*",
+        "org.onosproject.optical*",
+        "org.onosproject.newoptical*",
+        "org.onosproject.sdnip",
+        "org.onosproject.sdnip.*",
+        "org.onosproject.config",
+        "org.onosproject.routing",
+        "org.onosproject.routing*",
+        "org.onosproject.bgprouter",
+        "org.onosproject.segmentrouting*",
+        "org.onosproject.reactive.routing*",
+        "org.onosproject.messagingperf",
+        "org.onosproject.virtualbng*",
+        "org.onosproject.cordfabric*",
+        "org.onosproject.xosintegration*",
+        "org.onosproject.cip*",
+        "org.onosproject.vtn*",
+        "org.onosproject.cord*",
+        "org.onosproject.mcast*",
+        "org.onosproject.mfwd*",
+        "org.onosproject.mlb*",
+        "org.onosproject.igmp*",
+        "org.onosproject.pim*",
+        "org.onosproject.vpls*",
+        "org.onosproject.vrouter*",
+        "org.onosproject.faultmanagement*",
+        "org.onosproject.iptopology*",
+        "org.onosproject.drivermatrix*",
+        "org.onosproject.pathpainter*",
+        "org.onosproject.openstack*",
+        "org.onosproject.scalablegateway*",
+        "org.onosproject.forwarder*",
+        "org.onosproject.sfc*",
+        "org.onosproject.netcfgmonitor*",
+        "org.onosproject.flowperf",
+        "org.onosproject.loadtest",
+        "org.onosproject.events",
+        "org.onosproject.bmv2*",
+        "org.onosproject.xosclient*",
+        "org.onosproject.kafkaintegration*",
+        "org.onosproject.gangliametrics*",
+        "org.onosproject.graphitemetrics*",
+        "org.onosproject.influxdbmetrics*",
+        "org.onosproject.yms*",
+    ],
+    "Test Instrumentation & Applications": [
+        "org.onosproject.metrics.*",
+        "org.onosproject.demo*",
+        "org.onosproject.election*",
+        "org.onosproject.distributedprimitives*",
+        "org.onosproject.intentperf*",
+        "org.onosproject.messagingperf*",
+        "org.onosproject.optical.testapp*",
+    ],
+    "OpenFlow Providers & Controller": [
+        "org.onosproject.openflow.*",
+        "org.onosproject.provider.of.*",
+    ],
+    "NetConf Protocol & Providers": [
+        "org.onosproject.netconf*",
+        "org.onosproject.provider.netconf*",
+    ],
+    "OVSDB Protocol & Providers": [
+        "org.onosproject.provider.ovsdb*",
+        "org.onosproject.ovsdb*",
+    ],
+    "PCEP Protocol & Providers": [
+        "org.onosproject.pce.*",
+        "org.onosproject.pcerest*",
+        "org.onosproject.pceweb*",
+        "org.onosproject.pcep*",
+        "org.onosproject.pcepio*",
+        "org.onosproject.provider.pcep*",
+    ],
+    "BGP-LS Protocol & Providers": [
+        "org.onosproject.bgp.controller*",
+        "org.onosproject.bgp*",
+        "org.onosproject.provider.bgp*",
+    ],
+    "OSPF Protocol & Providers": [ "org.onosproject.ospf*" ],
+    "ISIS Protocol & Providers": [ "org.onosproject.isis*" ],
+    "LISP Protocol & Providers": [ "org.onosproject.lisp*" ],
+    "SNMP Protocol & Providers": [ "org.onosproject.snmp*" ],
+    "REST Protocol & Providers": [
+        "org.onosproject.protocol.rest*",
+        "org.onosproject.provider.rest*",
+    ],
+    "Other Providers": [
+        "org.onosproject.provider.*",
+        "org.onosproject.protocol.*",
+    ],
+    "Device Drivers": [ "org.onosproject.driver*" ],
+}
+
+project_javadoc(
+    name = "external",
+    title = "ONOS Java API (%s)" % ONOS_VERSION,
+    srcs = SOURCES,
+    deps = JAVADOC_DEPS,
+    groups = EXTERNAL_GROUPS,
+)
+
+project_javadoc(
+  name = "internal",
+  title = "ONOS Java API (%s)" % ONOS_VERSION,
+  srcs = SOURCES + INTERNAL_SOURCES,
+  deps = JAVADOC_DEPS,
+  groups = INTERNAL_GROUPS,
+  internal = True,
+)
diff --git a/tools/build/bazel/javadoc.bzl b/tools/build/bazel/javadoc.bzl
index 09d3d0f..ad27ef8 100644
--- a/tools/build/bazel/javadoc.bzl
+++ b/tools/build/bazel/javadoc.bzl
@@ -12,6 +12,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+JAVA_DOCS="-link https://docs.oracle.com/javase/8/docs/api/"
+
 def _impl(ctx):
   dir = ctx.label.name
   jar = ctx.outputs.jar
@@ -26,8 +28,8 @@
 
   cmd = [
       "mkdir %s" % dir,
-      "javadoc -quiet -tag onos.rsModel:a:\"onos model\" -d %s -cp %s %s" \
-          % (dir, ":".join(dep_list), " ".join(src_list)),
+      "javadoc -quiet -tag onos.rsModel:a:\"onos model\" %s -d %s -cp %s %s" \
+          % (JAVA_DOCS, dir, ":".join(dep_list), " ".join(src_list)),
       "jar cf %s -C %s ." % (jar.path, dir),
   ]
 
diff --git a/tools/build/bazel/project_javadoc.bzl b/tools/build/bazel/project_javadoc.bzl
new file mode 100644
index 0000000..7a09a93
--- /dev/null
+++ b/tools/build/bazel/project_javadoc.bzl
@@ -0,0 +1,82 @@
+# Copyright 2015 The Bazel Authors. All rights reserved.
+#
+# 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.
+
+JAVA_DOCS="-link https://docs.oracle.com/javase/8/docs/api/"
+
+def dump(obj):
+    print (dir(obj))
+    for attr in dir(obj):
+        print("obj.%s = %r" % (attr, getattr(obj, attr)))
+
+def _impl(ctx):
+    dir = ctx.label.name
+    jar = ctx.outputs.jar
+
+    classpath = ""
+    for dep in ctx.files.deps:
+        classpath += ":" + dep.path
+
+    src_list = ""
+    for src in ctx.files.srcs:
+        if src.path.endswith(".jar"):
+            src_list += " " + src.path
+
+    group_list = ""
+    for group in ctx.attr.groups:
+        packages = ""
+        for p in ctx.attr.groups[group]:
+            packages += ":" + p
+        group_list += " -group \"%s\" %s" % (group, packages.replace(":", "", 1))
+
+    cmd = [
+        "mkdir src; cd src",
+        "for s in %s; do jar xf ../$s; done" % src_list,
+        "rm -f META-INF/MANIFEST.MF",
+        "cd ..",
+        "cp -r docs/src/main/javadoc/* .",
+        "ls -lR doc-files overview.html",
+    ]
+
+    if ctx.attr.internal:
+        cmd += ["find src -type f | egrep -v 'src/(OSGI|WEB)-INF' >> FILES"]
+    else:
+        cmd += ["find src -type f | egrep -v 'src/(OSGI|WEB)-INF' | egrep -v '/(impl|internal)/' >> FILES"]
+
+    cmd += [
+        "javadoc -overview overview.html -doctitle '%s' -windowtitle '%s' %s -d apidocs -classpath %s -sourcepath src %s @FILES" \
+              % (ctx.attr.title, ctx.attr.title, group_list, classpath.replace(":", "", 1), JAVA_DOCS),
+        "cp -r doc-files apidocs/doc-files",
+        "jar cf %s apidocs" % jar.path,
+    ]
+
+    ctx.action(
+        inputs = ctx.files.srcs + ctx.files.deps,
+        outputs = [jar],
+        progress_message = "Generating javadocs jar for %s" %  ctx.attr.name,
+        command = ";\n".join(cmd)
+    )
+
+project_javadoc = rule(
+    attrs = {
+        "title": attr.string(),
+        "overview": attr.string(default = "src/main/javadoc/overview.html"),
+        "groups": attr.string_list_dict(),
+        "deps": attr.label_list(allow_files = True),
+        "srcs": attr.label_list(allow_files = True),
+        "internal": attr.bool(default = False),
+    },
+    implementation = _impl,
+    outputs = {"jar" : "%{name}.jar"},
+)
+