getting javadoc to build

Change-Id: I7f214c6ce5a876f458626e12c8cfdd3072ad107f
diff --git a/core/api/BUCK b/core/api/BUCK
index b066908..800a7f1 100644
--- a/core/api/BUCK
+++ b/core/api/BUCK
@@ -8,4 +8,6 @@
 osgi_jar_with_tests (
     name = 'onos-api',
     deps = COMPILE_DEPS,
+    javadoc_files = glob(['src/main/javadoc/**/*']),
+    javadoc_files_root = 'src/main/javadoc',
 )
\ No newline at end of file
diff --git a/docs/BUCK b/docs/BUCK
new file mode 100644
index 0000000..b82dc82
--- /dev/null
+++ b/docs/BUCK
@@ -0,0 +1,198 @@
+external_deps = UTILS + API + [
+  '//apps/cpman/api:onos-apps-cpman-api',
+  '//apps/routing-api:onos-apps-routing-api',
+  '//apps/dhcp/api:onos-apps-dhcp-api',
+]
+
+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.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.intf.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',
+  project_title = 'ONOS Java API (%s)' % ONOS_VERSION,
+  javadoc_files = glob(['src/main/javadoc/doc-files/*']),
+  overview = 'src/main/javadoc/overview.html',
+  deps = external_deps,
+  groups = external_groups,
+)
+
+project_javadoc(
+  name = 'internal',
+  project_title = 'ONOS Java API (%s)' % ONOS_VERSION,
+  javadoc_files = glob(['src/main/javadoc/doc-files/*']),
+  overview = 'src/main/javadoc/overview.html',
+  deps = CORE,
+  groups = internal_groups,
+)
diff --git a/modules.defs b/modules.defs
index 4fca095..2208271 100644
--- a/modules.defs
+++ b/modules.defs
@@ -1,10 +1,16 @@
-CORE = [
+UTILS = [
     '//utils/osgi:onlab-osgi',
     '//utils/junit:onlab-junit',
     '//utils/misc:onlab-misc',
     '//utils/rest:onlab-rest',
+]
 
+API = [
     '//core/api:onos-api',
+    '//incubator/api:onos-incubator-api',
+]
+
+CORE = UTILS + API + [
     '//core/net:onos-core-net',
     '//core/common:onos-core-common',
     '//core/store/primitives:onos-core-primitives',
@@ -13,7 +19,6 @@
     '//core/security:onos-security',
     '//core/store/persistence:onos-core-persistence',
 
-    '//incubator/api:onos-incubator-api',
     '//incubator/net:onos-incubator-net',
     '//incubator/core:onos-incubator-core',
     '//incubator/store:onos-incubator-store',
diff --git a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
index dff37c0..83e68f8 100644
--- a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
+++ b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/OnosJarDescription.java
@@ -42,6 +42,7 @@
 import com.facebook.buck.rules.BuildRules;
 import com.facebook.buck.rules.BuildTargetSourcePath;
 import com.facebook.buck.rules.Description;
+import com.facebook.buck.rules.SourcePath;
 import com.facebook.buck.rules.SourcePathResolver;
 import com.facebook.buck.rules.SourcePaths;
 import com.facebook.buck.rules.TargetGraph;
@@ -49,6 +50,7 @@
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedMap;
 import com.google.common.collect.ImmutableSortedSet;
 import com.google.common.collect.Iterables;
 
@@ -149,17 +151,35 @@
                         }
                     });
 
+            JavadocJar.JavadocArgs.Builder javadocArgs = JavadocJar.JavadocArgs.builder()
+                    .addArg("-windowtitle", target.getShortName())
+                    .addArg("-link", "http://docs.oracle.com/javase/8/docs/api")
+                    .addArg("-tag", "onos.rsModel:a:\"onos model\""); //FIXME from buckconfig + rule
+
+            final ImmutableSortedMap.Builder<SourcePath, Path> javadocFiles = ImmutableSortedMap.naturalOrder();
+            if (args.javadocFiles.isPresent()) {
+                for (SourcePath path : args.javadocFiles.get()) {
+                    javadocFiles.put(path,
+                                     JavadocJar.getDocfileWithPath(pathResolver, path, args.javadocFilesRoot.orNull()));
+                }
+            }
+
+
             if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
                 return new JavadocJar(
                         params,
                         pathResolver,
                         args.srcs.get(),
+                        javadocFiles.build(),
+                        javadocArgs.build(),
                         args.mavenCoords);
             } else {
                 return MavenUberJar.MavenJavadocJar.create(
                         Preconditions.checkNotNull(paramsWithMavenFlavor),
                         pathResolver,
                         args.srcs.get(),
+                        javadocFiles.build(),
+                        javadocArgs.build(),
                         args.mavenCoords);
             }
         }
diff --git a/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/ProjectJavadocDescription.java b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/ProjectJavadocDescription.java
new file mode 100644
index 0000000..d159762
--- /dev/null
+++ b/tools/build/buck-plugin/src/main/java/org/onosproject/onosjar/ProjectJavadocDescription.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+package org.onosproject.onosjar;
+
+import com.facebook.buck.cli.BuckConfig;
+import com.facebook.buck.jvm.java.JavaLibrary;
+import com.facebook.buck.jvm.java.JavadocJar;
+import com.facebook.buck.model.BuildTarget;
+import com.facebook.buck.model.Flavor;
+import com.facebook.buck.model.Flavored;
+import com.facebook.buck.model.Pair;
+import com.facebook.buck.parser.NoSuchBuildTargetException;
+import com.facebook.buck.rules.BuildRule;
+import com.facebook.buck.rules.BuildRuleParams;
+import com.facebook.buck.rules.BuildRuleResolver;
+import com.facebook.buck.rules.BuildRuleType;
+import com.facebook.buck.rules.Description;
+import com.facebook.buck.rules.SourcePath;
+import com.facebook.buck.rules.SourcePathResolver;
+import com.facebook.buck.rules.TargetGraph;
+import com.google.common.base.Optional;
+import com.google.common.base.Suppliers;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedMap;
+import com.google.common.collect.ImmutableSortedSet;
+import com.google.common.collect.Ordering;
+
+import java.nio.file.Path;
+import java.util.Map;
+
+/**
+ * Description for the onos_jar rules.
+ *
+ * Currently, this only does Swagger generation.
+ */
+public class ProjectJavadocDescription implements Description<ProjectJavadocDescription.Arg>, Flavored {
+    public static final BuildRuleType TYPE = BuildRuleType.of("project_javadoc");
+
+    public ProjectJavadocDescription(BuckConfig config) {
+        //TODO
+    }
+
+    @Override
+    public BuildRuleType getBuildRuleType() {
+        return TYPE;
+    }
+
+    @Override
+    public Arg createUnpopulatedConstructorArg() {
+        return new Arg();
+    }
+
+    @Override
+    public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph,
+                                                     BuildRuleParams params,
+                                                     BuildRuleResolver resolver,
+                                                     A args)
+            throws NoSuchBuildTargetException {
+
+        ImmutableSet.Builder<SourcePath> srcs = ImmutableSet.builder();
+        ImmutableSet.Builder<BuildRule> deps = ImmutableSet.builder();
+        ImmutableSortedMap.Builder<SourcePath, Path> docfiles = ImmutableSortedMap.naturalOrder();
+        for(BuildTarget dep : args.deps) {
+            BuildRule rule = resolver.requireRule(dep.withFlavors(JavaLibrary.JAVADOC_JAR));
+            if (rule instanceof JavadocJar) {
+                JavadocJar jarRule = (JavadocJar) rule;
+                srcs.addAll(jarRule.getSources());
+                deps.addAll(jarRule.getDeps());
+                docfiles.putAll(jarRule.getDocFiles());
+            } else {
+                throw new RuntimeException("rule is not a javalib"); //FIXME
+            }
+        }
+
+        BuildRuleParams newParams = params.copyWithDeps(
+                Suppliers.ofInstance(
+                    FluentIterable.from(deps.build())
+                                  .toSortedSet(Ordering.<BuildRule>natural())),
+                Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>of()));
+
+        SourcePathResolver sourceResolver = new SourcePathResolver(resolver);
+        ImmutableList.Builder<SourcePath> auxSources = ImmutableList.builder();
+
+        JavadocJar.JavadocArgs.Builder javadocArgs = JavadocJar.JavadocArgs.builder()
+                .addArg("-windowtitle", args.projectTitle)
+                .addArg("-doctitle", args.projectTitle)
+                .addArg("-link", "http://docs.oracle.com/javase/8/docs/api")
+                .addArg("-tag", "onos.rsModel:a:\"onos model\""); //FIXME from buckconfig + rule
+
+        if (args.groups.isPresent()) {
+            for (Pair<String, ImmutableList<String>> pair : args.groups.get()) {
+                javadocArgs.addArg("-group", pair.getFirst(), pair.getSecond());
+            }
+        }
+
+        if (args.excludePackages.isPresent() &&
+                !args.excludePackages.get().isEmpty()) {
+            javadocArgs.addArg("-exclude", args.excludePackages.get());
+        }
+
+        if (args.overview.isPresent()) {
+            javadocArgs.addArg("-overview",
+                   sourceResolver.getAbsolutePath(args.overview.get()).toString());
+
+        }
+
+        if (args.javadocFiles.isPresent()) {
+            for (SourcePath path : args.javadocFiles.get()) {
+                docfiles.put(path,
+                             JavadocJar.getDocfileWithPath(sourceResolver, path, args.javadocFilesRoot.orNull()));
+            }
+        }
+
+
+        JavadocJar javadocJar =  new JavadocJar(newParams,
+                              sourceResolver,
+                              ImmutableSortedSet.copyOf(srcs.build()),
+                              docfiles.build(),
+                              javadocArgs.build(),
+                              /* mavenCoords */ Optional.absent());
+        return javadocJar;
+
+    }
+
+    @Override
+    public boolean hasFlavors(ImmutableSet<Flavor> flavors) {
+        return false;
+    }
+
+    public static class Arg {
+        public ImmutableSortedSet<BuildTarget> deps;
+        public String projectTitle;
+
+        public Optional<SourcePath> overview;
+        public Optional<ImmutableSortedSet<SourcePath>> javadocFiles;
+        public Optional<Path> javadocFilesRoot;
+
+        public Optional<ImmutableList<String>> excludePackages;
+        public Optional<ImmutableList<Pair<String, ImmutableList<String>>>> groups;
+    }
+}
\ No newline at end of file