Integrated new style of cfgdef generation into the Bazel build.

Change-Id: Ic7b030504285a59f715790396bc04335df8fbd15
diff --git a/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
index 5958708..d15ad63 100644
--- a/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
+++ b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
@@ -107,15 +107,16 @@
 
     @Override
     public void registerProperties(Class<?> componentClass) {
-        if (true) {
-            return;
-        }
-
         checkPermission(CONFIG_WRITE);
 
         String componentName = componentClass.getName();
         String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
         try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
+            // FIXME: Eliminate the following soft-fail after property refactoring is complete.
+            if (ris == null) {
+                log.info("Property definitions not found at resource {}; please fix this", resourceName);
+                return;
+            }
             checkArgument(ris != null, "Property definitions not found at resource %s",
                           resourceName);
 
diff --git a/tools/build/bazel/osgi_java_library.bzl b/tools/build/bazel/osgi_java_library.bzl
index 0014eb8..b3497c2 100644
--- a/tools/build/bazel/osgi_java_library.bzl
+++ b/tools/build/bazel/osgi_java_library.bzl
@@ -175,12 +175,13 @@
 def _cfgdef_impl(ctx):
     output_jar = ctx.outputs.cfgdef_jar.path
 
-    # call swagger generator to make the swagger JSON and java files
     arguments = [
         output_jar,
-        ctx.files.srcs,
     ]
 
+    for src in ctx.files.srcs:
+        arguments.append(src.path)
+
     ctx.actions.run(
         inputs = ctx.files.srcs,
         outputs = [ctx.outputs.cfgdef_jar],
@@ -225,11 +226,9 @@
 
     srcs_arg = ""
     resources_arg = ""
-    input_dependencies = []
 
     for file in ctx.files.srcs:
         srcs_arg += file.path + ","
-        input_dependencies.append(file)
 
     for resource in resources_arg:
         resources_arg += resource.path + ","
@@ -275,11 +274,9 @@
 
     srcs_arg = ""
     resources_arg = ""
-    input_dependencies = []
 
     for file in ctx.files.srcs:
         srcs_arg += file.path + ","
-        input_dependencies.append(file)
 
     for resource in resources_arg:
         resources_arg += resource.path + ","
@@ -454,13 +451,6 @@
     native_srcs = srcs
     native_resources = resources
 
-    #    _cfgdef(
-    #        name = name + "_cfgdef",
-    #        srcs = native_srcs,
-    #        visibility = visibility,
-    #    )
-    #    native_resources.append(name + "_cfgdef_jar")
-
     if web_context != None and api_title != "" and len(resources) != 0:
         # generate Swagger files if needed
         _swagger_java(
@@ -496,12 +486,19 @@
 
     javacopts = ["-XepDisableAllChecks"] if suppress_errorprone else []
 
+    _cfgdef(
+        name = name + "_cfgdef_jar",
+        srcs = native_srcs,
+        visibility = visibility,
+        cfgdef_jar = name + "_cfgdef.jar",
+    )
+
     # compile the Java code
     if len(resource_jars) > 0:
         native.java_library(
             name = name + "-native",
             srcs = native_srcs,
-            resource_jars = resource_jars,
+            resource_jars = resource_jars + [name + "_cfgdef_jar"],
             deps = deps,
             visibility = visibility,
             javacopts = javacopts,
@@ -510,6 +507,7 @@
         native.java_library(
             name = name + "-native",
             srcs = native_srcs,
+            resource_jars = [name + "_cfgdef_jar"],
             resources = native_resources,
             deps = deps,
             visibility = visibility,
diff --git a/tools/build/cfgdef/src/main/java/org/onosproject/cfgdef/CfgDefGenerator.java b/tools/build/cfgdef/src/main/java/org/onosproject/cfgdef/CfgDefGenerator.java
index 696918d..89954f2 100644
--- a/tools/build/cfgdef/src/main/java/org/onosproject/cfgdef/CfgDefGenerator.java
+++ b/tools/build/cfgdef/src/main/java/org/onosproject/cfgdef/CfgDefGenerator.java
@@ -24,6 +24,7 @@
 import com.thoughtworks.qdox.model.expression.AnnotationValue;
 import com.thoughtworks.qdox.model.expression.AnnotationValueList;
 import com.thoughtworks.qdox.model.expression.FieldRef;
+import com.thoughtworks.qdox.parser.ParseException;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -45,8 +46,10 @@
     private static final String PROPERTY = "property";
     private static final String SEP = "|";
     private static final String UTF_8 = "UTF-8";
+    private static final String JAVA = ".java";
     private static final String EXT = ".cfgdef";
     private static final String STRING = "STRING";
+    private static final String NO_DESCRIPTION = "no description provided";
 
     private final File resourceJar;
     private final JavaProjectBuilder builder;
@@ -68,7 +71,10 @@
         this.builder = new JavaProjectBuilder();
         Arrays.stream(sourceFilePaths).forEach(filename -> {
             try {
+                if (filename.endsWith(JAVA))
                 builder.addSource(new File(filename));
+            } catch (ParseException e) {
+                // When unable to parse, skip the source; leave it to javac to fail.
             } catch (IOException e) {
                 throw new IllegalArgumentException("Unable to open file", e);
             }
@@ -127,19 +133,19 @@
 
             String line = name + SEP + type + SEP + def + SEP + desc + "\n";
             lines.add(line);
-            System.err.print(line);
         }
     }
 
-    // Retrieve description from a comment preceeding the field named the same
+    // Retrieve description from a comment preceding the field named the same
     // as the property or
     // TODO: from an annotated comment.
     private String description(JavaClass javaClass, String name) {
         JavaField field = javaClass.getFieldByName(name);
         if (field != null) {
-            return field.getComment();
+            String comment = field.getComment();
+            return comment != null ? comment : NO_DESCRIPTION;
         }
-        return "no description provided";
+        return NO_DESCRIPTION;
     }
 
     private String elaborate(AnnotationValue value) {
@@ -147,8 +153,10 @@
             return elaborate(((Add) value).getLeft()) + elaborate(((Add) value).getRight());
         } else if (value instanceof FieldRef) {
             return stripped(constants.get(((FieldRef) value).getName()));
-        } else {
+        } else if (value != null) {
             return stripped(value.toString());
+        } else {
+            return "";
         }
     }