[ONOS-5632] inter file linking support in onos-yang-tools buck plugin.

Change-Id: I446b9ffd1a11fc2eb1f1572a32c00280524f8617
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
index 6bcbb1b..39e75c1 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/AttributesJavaDataType.java
@@ -320,7 +320,7 @@
                                                   + " in " + type.getFileName());
         }
 
-        if (!(((YangDerivedInfo<?>) var).getReferredTypeDef() != null)) {
+        if ((((YangDerivedInfo<?>) var).getReferredTypeDef() == null)) {
             throw new TranslatorException("derived info is not an instance of typedef. " +
                                                   type.getDataTypeName() + " in " +
                                                   type.getLineNumber() + " at " +
diff --git a/plugin/buck/pom.xml b/plugin/buck/pom.xml
index fd1cca9..97f2f0b 100644
--- a/plugin/buck/pom.xml
+++ b/plugin/buck/pom.xml
@@ -34,6 +34,11 @@
             <version>0.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-yang-tool</artifactId>
+            <version>1.10-SNAPSHOT</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
index 583ecaf..49b583c 100644
--- a/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
+++ b/plugin/buck/src/main/java/org/onosproject/yangutils/plugin/buck/YangGenerator.java
@@ -16,41 +16,74 @@
 
 package org.onosproject.yangutils.plugin.buck;
 
+import org.onosproject.yangutils.tool.CallablePlugin;
+import org.onosproject.yangutils.tool.YangToolManager;
+import org.onosproject.yangutils.utils.io.YangPluginConfig;
+
 import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
-import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
-import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil;
-import org.onosproject.yangutils.utils.io.YangPluginConfig;
-import org.onosproject.yangutils.datamodel.YangNode;
+import static java.util.stream.Collectors.toList;
+import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
+import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
 
 /**
  * Generates Java sources from a Yang model.
  */
-public class YangGenerator {
+public class YangGenerator implements CallablePlugin {
 
     private final List<File> models;
     private String outputDirectory;
+    private final String DEFAULT_JAR_RES_PATH = SLASH + YANG_RESOURCES + SLASH;
 
-    public YangGenerator(List<File> models, String outputDirectory) {
+    YangGenerator(List<File> models, String outputDirectory) {
         this.models = models;
         this.outputDirectory = outputDirectory + "/";
     }
 
     public void execute() throws YangParsingException {
-        for (File model : models) {
+        List<String> files = getListOfFile();
+        synchronized (files) {
             try {
                 YangPluginConfig config = new YangPluginConfig();
                 config.setCodeGenDir(outputDirectory);
+                config.resourceGenDir(outputDirectory + DEFAULT_JAR_RES_PATH);
 
-                YangNode yangNode = new YangUtilsParserManager()
-                        .getDataModel(model.toString());
-
-                JavaCodeGeneratorUtil.generateJavaCode(yangNode, config);
+                //intra jar file linking.
+                YangToolManager manager = new YangToolManager();
+                manager.compileYangFiles(manager.createYangFileInfoSet(files),
+                                         null, config, this);
             } catch (Exception e) {
                 throw new YangParsingException(e);
             }
         }
     }
 
+    private List<String> getListOfFile() {
+        List<String> files = new ArrayList<>();
+        if (models != null) {
+            synchronized (models) {
+                files.addAll(models.stream().map(File::toString)
+                                     .collect(toList()));
+            }
+        }
+        return files;
+    }
+
+    @Override
+    public void addGeneratedCodeToBundle() {
+        //TODO: add functionality.
+    }
+
+    @Override
+    public void addCompiledSchemaToBundle() throws IOException {
+        //TODO: add functionality.
+    }
+
+    @Override
+    public void addYangFilesToBundle() throws IOException {
+        //TODO: add functionality.
+    }
 }
diff --git a/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java b/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
index 1e38d88..9285edd 100644
--- a/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
+++ b/plugin/maven/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
@@ -40,7 +40,6 @@
 import org.onosproject.yangutils.tool.exception.YangToolException;
 import org.onosproject.yangutils.utils.io.YangPluginConfig;
 import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
-import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 import java.io.IOException;
@@ -69,6 +68,7 @@
 import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
 import static org.onosproject.yangutils.utils.UtilConstants.VERSION_ERROR;
 import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
+import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
 import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
 import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
 import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
@@ -222,9 +222,11 @@
              * Obtain the YANG files at a path mentioned in plugin and creates
              * YANG file information set.
              */
-            createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
 
             YangToolManager toolManager = new YangToolManager();
+
+            yangFileInfoSet = toolManager.createYangFileInfoSet(
+                    getYangFiles(searchDir));
             List<YangNode> interJarResolvedNodes =
                     resolveInterJarDependencies(project, localRepository,
                                                 remoteRepository, outputDir);
diff --git a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
index 0853775..ac0123b 100644
--- a/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
+++ b/plugin/tool/src/main/java/org/onosproject/yangutils/tool/YangToolManager.java
@@ -94,6 +94,23 @@
     }
 
     /**
+     * Creates a YANG file info set.
+     *
+     * @param yangFileList YANG files list
+     */
+    public Set<YangFileInfo> createYangFileInfoSet(List<String> yangFileList) {
+        if (yangFileInfoSet == null) {
+            yangFileInfoSet = new HashSet<>();
+        }
+        for (String yangFile : yangFileList) {
+            YangFileInfo yangFileInfo = new YangFileInfo();
+            yangFileInfo.setYangFileName(yangFile);
+            yangFileInfoSet.add(yangFileInfo);
+        }
+        return yangFileInfoSet;
+    }
+
+    /**
      * Compile te YANG files and generate the corresponding Java files.
      * Update the generated bundle with the schema metadata.
      *
@@ -106,87 +123,89 @@
                                  YangPluginConfig config,
                                  CallablePlugin plugin) throws IOException {
 
-        try {
+        synchronized (yangFiles) {
+            try {
 
-            if (config == null || yangFiles == null) {
-                throw new YangToolException(E_MISSING_INPUT);
-            }
-            yangFileInfoSet = yangFiles;
-
-            if (config.getCodeGenDir() == null) {
-                throw new YangToolException(E_CODE_GEN_PATH);
-            }
-
-            // Check if there are any file to translate, if not return.
-            if (yangFileInfoSet == null || yangFileInfoSet.isEmpty()) {
-                // No files to translate
-                return;
-            }
-
-            createDirectories(config.resourceGenDir());
-
-            // Resolve inter jar dependency.
-            addSchemaToFileSet(dependentSchema);
-
-
-            // Carry out the parsing for all the YANG files.
-            parseYangFileInfoSet();
-
-            // Resolve dependencies using linker.
-            resolveDependenciesUsingLinker();
-
-            // Perform translation to JAVA.
-            translateToJava(config);
-
-            // Serialize data model.
-            Set<YangNode> compiledSchemas = new HashSet<>();
-            for (YangFileInfo fileInfo : yangFileInfoSet) {
-                compiledSchemas.add(fileInfo.getRootNode());
-            }
-
-            String serFileName = config.resourceGenDir() + YANG_META_DATA + SERIALIZED_FILE_EXTENSION;
-            FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
-            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
-            objectOutputStream.writeObject(compiledSchemas);
-            objectOutputStream.close();
-            fileOutputStream.close();
-
-
-            //add YANG files to JAR
-            List<File> files = getListOfFile(yangFileInfoSet);
-            String path = config.resourceGenDir();
-            File targetDir = new File(path);
-            targetDir.mkdirs();
-
-            for (File file : files) {
-                Files.copy(file.toPath(),
-                           new File(path + file.getName()).toPath(),
-                           StandardCopyOption.REPLACE_EXISTING);
-            }
-
-            if (plugin != null) {
-                plugin.addCompiledSchemaToBundle();
-                plugin.addGeneratedCodeToBundle();
-                plugin.addYangFilesToBundle();
-            }
-
-        } catch (IOException | ParserException e) {
-            YangToolException exception =
-                    new YangToolException(e.getMessage(), e);
-            exception.setCurYangFile(curYangFileInfo);
-
-            if (curYangFileInfo != null &&
-                    curYangFileInfo.getRootNode() != null) {
-                try {
-                    translatorErrorHandler(curYangFileInfo.getRootNode(),
-                                           config);
-                } catch (IOException ex) {
-                    e.printStackTrace();
-                    throw ex;
+                if (config == null || yangFiles == null) {
+                    throw new YangToolException(E_MISSING_INPUT);
                 }
-            }
+                yangFileInfoSet = yangFiles;
 
-            throw exception;
+                if (config.getCodeGenDir() == null) {
+                    throw new YangToolException(E_CODE_GEN_PATH);
+                }
+
+                // Check if there are any file to translate, if not return.
+                if (yangFileInfoSet == null || yangFileInfoSet.isEmpty()) {
+                    // No files to translate
+                    return;
+                }
+
+                createDirectories(config.resourceGenDir());
+
+                // Resolve inter jar dependency.
+                addSchemaToFileSet(dependentSchema);
+
+
+                // Carry out the parsing for all the YANG files.
+                parseYangFileInfoSet();
+
+                // Resolve dependencies using linker.
+                resolveDependenciesUsingLinker();
+
+                // Perform translation to JAVA.
+                translateToJava(config);
+
+                // Serialize data model.
+                Set<YangNode> compiledSchemas = new HashSet<>();
+                for (YangFileInfo fileInfo : yangFileInfoSet) {
+                    compiledSchemas.add(fileInfo.getRootNode());
+                }
+
+                String serFileName = config.resourceGenDir() + YANG_META_DATA + SERIALIZED_FILE_EXTENSION;
+                FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
+                ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
+                objectOutputStream.writeObject(compiledSchemas);
+                objectOutputStream.close();
+                fileOutputStream.close();
+
+
+                //add YANG files to JAR
+                List<File> files = getListOfFile(yangFileInfoSet);
+                String path = config.resourceGenDir();
+                File targetDir = new File(path);
+                targetDir.mkdirs();
+
+                for (File file : files) {
+                    Files.copy(file.toPath(),
+                               new File(path + file.getName()).toPath(),
+                               StandardCopyOption.REPLACE_EXISTING);
+                }
+
+                if (plugin != null) {
+                    plugin.addCompiledSchemaToBundle();
+                    plugin.addGeneratedCodeToBundle();
+                    plugin.addYangFilesToBundle();
+                }
+
+            } catch (IOException | ParserException e) {
+                YangToolException exception =
+                        new YangToolException(e.getMessage(), e);
+                exception.setCurYangFile(curYangFileInfo);
+
+                if (curYangFileInfo != null &&
+                        curYangFileInfo.getRootNode() != null) {
+                    try {
+                        translatorErrorHandler(curYangFileInfo.getRootNode(),
+                                               config);
+                    } catch (IOException ex) {
+                        e.printStackTrace();
+                        throw ex;
+                    }
+                }
+
+                throw exception;
+            }
         }
     }