Adding Java binary to invoke YANG compiler from command line.

Change-Id: Iab19e3b91cfb44da67650f857a9045c30c2dda7b
diff --git a/compiler/base/parser/pom.xml b/compiler/base/parser/pom.xml
index 95451d9..3154af4 100644
--- a/compiler/base/parser/pom.xml
+++ b/compiler/base/parser/pom.xml
@@ -25,7 +25,6 @@
     </parent>
 
     <artifactId>onos-yang-compiler-parser</artifactId>
-    <version>2.5-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <dependencies>
diff --git a/compiler/base/tool/pom.xml b/compiler/base/tool/pom.xml
index fe57a58..0f91492 100644
--- a/compiler/base/tool/pom.xml
+++ b/compiler/base/tool/pom.xml
@@ -27,7 +27,6 @@
     </parent>
 
     <artifactId>onos-yang-compiler-tool</artifactId>
-    <version>2.5-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <dependencies>
diff --git a/compiler/plugin/main/pom.xml b/compiler/plugin/main/pom.xml
new file mode 100644
index 0000000..f21a149
--- /dev/null
+++ b/compiler/plugin/main/pom.xml
@@ -0,0 +1,62 @@
+<!--
+  ~ Copyright 2018-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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-yang-compiler-plugin</artifactId>
+        <version>2.5-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-yang-compiler-main</artifactId>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-yang-compiler-plugin-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>2.4.3</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <transformers>
+                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                            <mainClass>org.onosproject.yang.compiler.main.YangCompilerMain</mainClass>
+                        </transformer>
+                    </transformers>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/YangCompilerMain.java b/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/YangCompilerMain.java
new file mode 100644
index 0000000..faaaf4c
--- /dev/null
+++ b/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/YangCompilerMain.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2018-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.yang.compiler.main;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.yang.compiler.tool.DefaultYangCompilationParam;
+import org.onosproject.yang.compiler.tool.YangCompilerManager;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.file.Path;
+import java.util.List;
+
+import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.parseDepSchemaPath;
+
+/**
+ * Provides Java binary to invoke the YANG compiler as an executable.
+ */
+public final class YangCompilerMain {
+
+    private static final String USAGE =
+            "usage: main modelId outputDir [-d dependencyJar1 -d dependencyJar2...] " +
+                    "inputYangFile1 inputYangFile2...";
+
+    private static final String DEPENDENCY_FLAG = "-d";
+    private static final String STDIN_FLAG = "-";
+
+    // No use creating an instance
+    private YangCompilerMain() {
+    }
+
+    private static void usage() {
+        System.err.println(USAGE);
+        System.exit(1);
+    }
+
+    /**
+     * Main executable entry point.
+     * <p>
+     * usage: main modelId outputDir [-d dependencyJar1 -d dependencyJar2...] \
+     *                      inputYangFile1 inputYangFile2...
+     *
+     * @param args see usage above
+     * @throws IOException if issues arise when writing out generated classes
+     */
+    public static void main(String[] args) throws IOException {
+        if (args.length < 3) {
+            usage();
+        }
+
+        ImmutableList.Builder<Path> yangFiles = ImmutableList.builder();
+        ImmutableList.Builder<Path> depJars = ImmutableList.builder();
+
+        // Scan tail arguments and separate dependencies from input sources
+        for (int i = 2; i < args.length; i++) {
+            if (DEPENDENCY_FLAG.equals(args[i])) {
+                i++;
+                if (i >= args.length) {
+                    usage();
+                }
+                depJars.add(new File(args[i]).toPath());
+            } else if (STDIN_FLAG.equals(args[i])) {
+                if (i != args.length - 1) {
+                    usage();
+                }
+                try (BufferedReader input = new BufferedReader(new InputStreamReader(System.in))) {
+                    String line;
+                    while ((line = input.readLine()) != null) {
+                        yangFiles.add(new File(line).toPath());
+                    }
+                }
+
+            } else {
+                yangFiles.add(new File(args[i]).toPath());
+            }
+        }
+
+        runYangCompiler(args[0], args[1], depJars.build(), yangFiles.build());
+    }
+
+    // Runs the YANG compiler on the YANG sources in the specified directory.
+    private static void runYangCompiler(String modelId, String outputDir,
+                                        List<Path> depJars, List<Path> yangFiles)
+            throws IOException {
+        File dir = new File(outputDir);
+        if (!dir.exists() && !dir.mkdirs()) {
+            throw new IOException("Unable to create output directory");
+        }
+
+        // Prepare the compilation parameter
+        DefaultYangCompilationParam.Builder param = DefaultYangCompilationParam.builder()
+                .setCodeGenDir(new File(outputDir, "src").toPath())
+                .setMetadataGenDir(new File(outputDir, "schema").toPath())
+                .setModelId(modelId);
+
+        for (Path d : depJars) {
+            File jar = parseDepSchemaPath(d.toString(), outputDir);
+            if (jar != null) {
+                param.addDependentSchema(jar.toPath());
+            }
+        }
+
+        // Enumerate all input YANG source files.
+        yangFiles.forEach(param::addYangFile);
+
+        // Run the YANG compiler and collect the results
+        new YangCompilerManager().compileYangFiles(param.build());
+    }
+}
diff --git a/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/package-info.java b/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/package-info.java
new file mode 100644
index 0000000..aa2a2da
--- /dev/null
+++ b/compiler/plugin/main/src/main/java/org/onosproject/yang/compiler/main/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-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.
+ */
+
+/**
+ * Provides Java binary to invoke the YANG compiler as an executable.
+ */
+package org.onosproject.yang.compiler.main;
\ No newline at end of file
diff --git a/compiler/plugin/maven/pom.xml b/compiler/plugin/maven/pom.xml
index a7bbdd8..f2fcb0b 100644
--- a/compiler/plugin/maven/pom.xml
+++ b/compiler/plugin/maven/pom.xml
@@ -25,7 +25,6 @@
     </parent>
 
     <artifactId>onos-yang-compiler-maven-plugin</artifactId>
-    <version>2.5-SNAPSHOT</version>
     <packaging>maven-plugin</packaging>
 
     <dependencies>
@@ -100,7 +99,7 @@
         <dependency>
             <groupId>org.onosproject</groupId>
             <artifactId>onos-yang-compiler-plugin-utils</artifactId>
-            <version>2.5-SNAPSHOT</version>
+            <version>${project.version}</version>
         </dependency>
     </dependencies>
 
diff --git a/compiler/plugin/pom.xml b/compiler/plugin/pom.xml
index 4b7c062..edffc36 100644
--- a/compiler/plugin/pom.xml
+++ b/compiler/plugin/pom.xml
@@ -34,6 +34,7 @@
     <modules>
         <module>maven</module>
         <module>buck</module>
+        <module>main</module>
         <module>utils</module>
     </modules>