Generate a test rule to run checkstyle for an onos jar file

Change-Id: I21da8d353d592de847cc019875baa59786500cfe
diff --git a/lib/BUCK b/lib/BUCK
index 12f9b45..2347630 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Wed, 13 Jun 2018 22:38:24 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Tue, 19 Jun 2018 22:30:22 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 pass_thru_pom(
@@ -224,6 +224,15 @@
 )
 
 remote_jar (
+  name = 'commons-cli',
+  out = 'commons-cli-1.3.jar',
+  url = 'mvn:commons-cli:commons-cli:jar:1.3',
+  sha1 = 'a48653b6bcd06b5e61ed63739ca601701fcb6a6c',
+  maven_coords = 'commons-cli:commons-cli:1.3',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
   name = 'commons-collections',
   out = 'commons-collections-3.2.2.jar',
   url = 'mvn:commons-collections:commons-collections:jar:3.2.2',
diff --git a/lib/deps.json b/lib/deps.json
index ea4b309..c8c8512 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -118,6 +118,7 @@
     "asm": "mvn:org.ow2.asm:asm:5.0.4",
     "atomix": "mvn:io.atomix:atomix:2.0.22",
     "commons-codec": "mvn:commons-codec:commons-codec:1.10",
+    "commons-cli": "mvn:commons-cli:commons-cli:1.3",
     "commons-collections": "mvn:commons-collections:commons-collections:3.2.2",
     "commons-configuration": "mvn:commons-configuration:commons-configuration:1.10",
     "commons-io": "mvn:commons-io:commons-io:2.6",
diff --git a/tools/build/bazel/checkstyle.bzl b/tools/build/bazel/checkstyle.bzl
new file mode 100644
index 0000000..d27ac57
--- /dev/null
+++ b/tools/build/bazel/checkstyle.bzl
@@ -0,0 +1,85 @@
+"""
+ Copyright 2018-present Open Networking Foundation
+
+ 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.
+"""
+
+
+"""
+    Implementation of the rule to call checkstyle
+"""
+def _checkstyle_impl(ctx):
+    classpath = ""
+    need_colon = False
+    for file in ctx.files._classpath:
+        if need_colon:
+            classpath += ":"
+        need_colon = True
+        classpath += file.path
+
+    cmd = " ".join(
+            ["java -cp %s com.puppycrawl.tools.checkstyle.Main" % classpath] +
+            ["-c %s" % ctx.attr._config.files.to_list()[0].path] +
+            [src_file.path for src_file in ctx.files.srcs])
+
+    ctx.actions.write(
+        output = ctx.outputs.executable,
+        content = cmd,
+    )
+
+    inputs = (ctx.files.srcs +
+              ctx.files._classpath +
+              ctx.attr._config.files.to_list() +
+              ctx.attr._suppressions.files.to_list() +
+              ctx.attr._java_header.files.to_list())
+
+    runfiles = ctx.runfiles(files = inputs)
+    return [DefaultInfo(runfiles = runfiles)]
+
+
+"""
+    Rule definition for calling checkstyle
+"""
+_execute_checkstyle_test = rule(
+    test = True,
+    attrs = {
+        "_classpath": attr.label_list(default=[
+                    Label("@checkstyle//jar"),
+                    Label("@commons_beanutils//jar"),
+                    Label("@commons_cli//jar"),
+                    Label("@commons_collections//jar"),
+                    Label("@antlr//jar"),
+                    Label("@guava//jar"),
+                    Label("@commons_logging//jar"),
+                ]),
+        "srcs": attr.label_list(allow_files = FileType([".java"])),
+        "_config": attr.label(default=Label("//tools/build/conf:checkstyle_xml")),
+        "_suppressions": attr.label(default=Label("//tools/build/conf:suppressions_xml")),
+        "_java_header": attr.label(default=Label("//tools/build/conf:onos_java_header")),
+    },
+
+    implementation = _checkstyle_impl,
+)
+
+"""
+    Macro to instantiate the checkstyle rule for a given set of sources.
+
+    Args:
+        name: name of the target to generate. Required.
+        srcs: list of source file targets to run checkstyle on. Required.
+        size: test size constraint. Optional, defaults to "small"
+"""
+def checkstyle_test(name, srcs):
+    _execute_checkstyle_test(name = name, srcs = srcs, size = "small")
+
+
diff --git a/tools/build/bazel/generate_workspace.bzl b/tools/build/bazel/generate_workspace.bzl
index c41008e..39ccf93 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, 13 Jun 2018 22:38:28 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Tue, 19 Jun 2018 22:30:34 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 load("//tools/build/bazel:variables.bzl", "ONOS_GROUP_ID", "ONOS_VERSION")
@@ -143,6 +143,12 @@
     )
 
     native.maven_jar(
+        name = "commons_cli",
+        artifact = "commons-cli:commons-cli:1.3",
+        sha1 = "a48653b6bcd06b5e61ed63739ca601701fcb6a6c",
+    )
+
+    native.maven_jar(
         name = "commons_collections",
         artifact = "commons-collections:commons-collections:3.2.2",
         sha1 = "8ad72fe39fa8c91eaaf12aadb21e0c3661fe26d5",
@@ -1141,6 +1147,12 @@
     )
 
     native.java_library(
+        name = "commons_cli",
+        visibility = ["//visibility:public"],
+        exports = ["@commons_cli//jar"],
+    )
+
+    native.java_library(
         name = "commons_collections",
         visibility = ["//visibility:public"],
         exports = ["@commons_collections//jar"],
@@ -2100,6 +2112,7 @@
 artifact_map[str(Label("@asm//jar"))] = "mvn:org.ow2.asm:asm:jar:5.0.4"
 artifact_map[str(Label("@atomix//jar"))] = "mvn:io.atomix:atomix:jar:2.0.22"
 artifact_map[str(Label("@commons_codec//jar"))] = "mvn:commons-codec:commons-codec:jar:1.10"
+artifact_map[str(Label("@commons_cli//jar"))] = "mvn:commons-cli:commons-cli:jar:1.3"
 artifact_map[str(Label("@commons_collections//jar"))] = "mvn:commons-collections:commons-collections:jar:3.2.2"
 artifact_map[str(Label("@commons_configuration//jar"))] = "mvn:commons-configuration:commons-configuration:jar:1.10"
 artifact_map[str(Label("@commons_io//jar"))] = "mvn:commons-io:commons-io:jar:2.6"
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index bb07140..342a49a 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -17,6 +17,7 @@
 load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST")
 load("//tools/build/bazel:variables.bzl", "ONOS_VERSION")
 load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules")
+load("//tools/build/bazel:checkstyle.bzl", "checkstyle_test")
 
 def _all_java_sources():
     return native.glob(["src/main/java/**/*.java"])
@@ -451,6 +452,11 @@
             deps = all_test_deps,
         )
 
+    checkstyle_test(
+        name = name + "_checkstyle_test",
+        srcs = srcs,
+    )
+
 """
     Creates an OSGI jar file from a set of source files.
 
diff --git a/tools/build/conf/BUILD b/tools/build/conf/BUILD
new file mode 100644
index 0000000..22a1ecf
--- /dev/null
+++ b/tools/build/conf/BUILD
@@ -0,0 +1,23 @@
+checkstyle_source = "src/main/resources/onos/checkstyle.xml"
+
+suppression_source = "src/main/resources/onos/suppressions.xml"
+
+header_source = "src/main/resources/onos/onos-java.header"
+
+filegroup(
+    name = "checkstyle_xml",
+    srcs = [checkstyle_source],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "suppressions_xml",
+    srcs = [suppression_source],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "onos_java_header",
+    srcs = [header_source],
+    visibility = ["//visibility:public"],
+)
diff --git a/tools/build/conf/src/main/resources/onos/checkstyle.xml b/tools/build/conf/src/main/resources/onos/checkstyle.xml
index d3cc730..21ac8da 100644
--- a/tools/build/conf/src/main/resources/onos/checkstyle.xml
+++ b/tools/build/conf/src/main/resources/onos/checkstyle.xml
@@ -122,6 +122,10 @@
     <!--   <property name="fileExtensions" value="java"/> -->
     <!-- </module> -->
 
+    <module name="SuppressionFilter">
+        <property name="file" value="tools/build/conf/src/main/resources/onos/suppressions.xml"/>
+    </module>
+
     <module name="RegexpHeader">
         <!-- The following line is different for maven due to how the maven checkstyle plugin works -->
         <property name="headerFile" value="tools/build/conf/src/main/resources/onos/onos-java.header"/>
@@ -164,13 +168,13 @@
             <property name="suppressLoadErrors" value="true"/>
         </module>
         <module name="JavadocType">
-            <property name="severity" value="warning"/>
+            <property name="severity" value="ignore"/>
         </module>
         <module name="JavadocVariable">
             <!-- Suppress check for private member Javadocs.
              Possibly revist fixing these. -->
             <property name="scope" value="public"/>
-            <property name="severity" value="warning"/>
+            <property name="severity" value="ignore"/>
         </module>
         <module name="JavadocStyle"/>
         <!-- @author tag should not be used -->
@@ -309,7 +313,7 @@
         <module name="InterfaceIsType"/>
 
         <module name="VisibilityModifier">
-            <property name="severity" value="warning"/>
+            <property name="severity" value="ignore"/>
         </module>