[ONOS-5887] FIXED:compiler exception while using yang buck plugin

Change-Id: Id3de976e4bda4cb96a2de25caf2322e37acfe6cf
diff --git a/compiler/api/src/main/java/org/onosproject/yang/DefaultYangModuleId.java b/compiler/api/src/main/java/org/onosproject/yang/DefaultYangModuleId.java
index cea422e..2159990 100644
--- a/compiler/api/src/main/java/org/onosproject/yang/DefaultYangModuleId.java
+++ b/compiler/api/src/main/java/org/onosproject/yang/DefaultYangModuleId.java
@@ -37,7 +37,6 @@
      */
     public DefaultYangModuleId(String name, String rev) {
         checkNotNull(name);
-        checkNotNull(rev);
         moduleName = name;
         revision = rev;
     }
diff --git a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
index 4a77a09..29354e4 100644
--- a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
+++ b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
@@ -45,6 +45,7 @@
 import java.io.ObjectOutputStream;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
@@ -75,6 +76,7 @@
 public class YangCompilerManager implements YangCompilerService {
 
     private static final Logger log = getLogger(YangCompilerManager.class);
+    private static final String DATE_FORMAT = "yyyy-mm-dd";
     private final YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
     private final YangLinker yangLinker = new YangLinkerManager();
     private final Set<YangNode> yangNodeSet = new HashSet<>();
@@ -87,17 +89,19 @@
     @Override
     public YangCompiledOutput compileYangFiles(YangCompilationParam param)
             throws IOException, YangCompilerException {
+        synchronized (YangCompilerManager.class) {
 
-        YangPluginConfig config = new YangPluginConfig();
-        config.setCodeGenDir(param.getCodeGenDir().toString() + SLASH);
-        config.resourceGenDir(param.getMetadataGenDir().toString() +
-                                      SLASH);
+            YangPluginConfig config = new YangPluginConfig();
+            config.setCodeGenDir(param.getCodeGenDir().toString() + SLASH);
+            config.resourceGenDir(param.getMetadataGenDir().toString() +
+                                          SLASH);
 
-        processYangFiles(createYangFileInfoSet(param.getYangFiles()),
-                         dependentSchema(param.getDependentSchemas()), config);
+            processYangFiles(createYangFileInfoSet(param.getYangFiles()),
+                             dependentSchema(param.getDependentSchemas()), config);
 
-        return new DefaultYangCompiledOutput(
-                processYangModel(config.resourceGenDir()), genJavaPath);
+            return new DefaultYangCompiledOutput(
+                    processYangModel(config.resourceGenDir()), genJavaPath);
+        }
     }
 
     /**
@@ -125,8 +129,24 @@
      * @return YANG module id for a given YANG module node
      */
     private YangModuleId processModuleId(YangNode module) {
-        return new DefaultYangModuleId(module.getName(), module.getRevision()
-                .getRevDate().toString());
+        String rev = getDateInStringFormat(module);
+        return new DefaultYangModuleId(module.getName(), rev);
+    }
+
+    /**
+     * Returns date in string format.
+     *
+     * @param schemaNode schema node
+     * @return date in string format
+     */
+    private String getDateInStringFormat(YangNode schemaNode) {
+        if (schemaNode != null) {
+            if (schemaNode.getRevision() != null) {
+                return new SimpleDateFormat(DATE_FORMAT)
+                        .format(schemaNode.getRevision().getRevDate());
+            }
+        }
+        return null;
     }
 
     /**
diff --git a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangLibrary.java b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangLibrary.java
index 0eed357..434db21 100644
--- a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangLibrary.java
+++ b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangLibrary.java
@@ -121,9 +121,10 @@
      * @return generated sources directory
      */
     public Path getGenSrcsDirectory() {
-        File dir = new File(genSrcsDirectory.toString() + SLASH + YANG);
-        dir.mkdirs();
-        return genSrcsDirectory;
+        synchronized (YangLibrary.class) {
+            File dir = new File(genSrcsDirectory.toString() + SLASH + YANG);
+            dir.mkdirs();
+            return genSrcsDirectory;
+        }
     }
-
 }
diff --git a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangStep.java b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangStep.java
index 076fd0a..58e8eb9 100644
--- a/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangStep.java
+++ b/compiler/plugin/buck/src/main/java/org/onosproject/yang/compiler/plugin/buck/YangStep.java
@@ -30,6 +30,7 @@
 import java.util.List;
 import java.util.stream.Collectors;
 
+import static org.onosproject.yang.compiler.utils.UtilConstants.COLON;
 import static org.onosproject.yang.compiler.utils.UtilConstants.JAR;
 import static org.onosproject.yang.compiler.utils.UtilConstants.LIB;
 import static org.onosproject.yang.compiler.utils.UtilConstants.LIB_PATH;
@@ -62,15 +63,17 @@
     public StepExecutionResult execute(ExecutionContext executionContext)
             throws IOException, InterruptedException {
 
-        List<File> sourceFiles = srcs.stream().map(Path::toFile)
-                .collect(Collectors.toList());
-        try {
-            new YangGenerator(sourceFiles, output.toString(), getJarPaths())
-                    .execute();
-            return StepExecutionResult.SUCCESS;
-        } catch (YangParsingException e) {
-            executionContext.getConsole().printErrorText(e.getMessage());
-            return StepExecutionResult.ERROR;
+        synchronized (YangStep.class) {
+            List<File> sourceFiles = srcs.stream().map(Path::toFile)
+                    .collect(Collectors.toList());
+            try {
+                new YangGenerator(sourceFiles, output.toString(), getJarPaths())
+                        .execute();
+                return StepExecutionResult.SUCCESS;
+            } catch (YangParsingException e) {
+                executionContext.getConsole().printErrorText(e.getMessage());
+                return StepExecutionResult.ERROR;
+            }
         }
     }
 
@@ -81,22 +84,34 @@
         UnflavoredBuildTarget uBt;
         if (deps != null) {
             for (BuildRule rule : deps) {
-                if (!rule.getBuildTarget().getFullyQualifiedName()
-                        .contains(LIB_PATH)) {
+                String name = rule.getBuildTarget().getFullyQualifiedName();
+                if (!name.contains(LIB_PATH)) {
                     builder = new StringBuilder();
-                    //build absolute path for jar file
-                    builder.append(filesystem.getRootPath().toString()).append(SLASH)
-                            .append(filesystem.getBuckPaths().getGenDir())
-                            .append(SLASH);
-                    uBt = rule.getBuildTarget().getUnflavoredBuildTarget();
-                    array = uBt.getBaseName().split(SLASH);
-                    for (int i = 2; i < array.length; i++) {
-                        builder.append(array[i]).append(SLASH);
+                    if (name.contains(COLON)) {
+                        //when you have prebuilt jar in your directory
+                        Path thisPath = rule.getPathToOutput();
+                        if (thisPath != null) {
+                            depJarPaths.add(thisPath.toString());
+                        }
+                    } else {
+                        //when dependent on other package
+                        //build absolute path for jar file
+                        builder.append(filesystem.getRootPath().toString()).append(SLASH)
+                                .append(filesystem.getBuckPaths().getGenDir())
+                                .append(SLASH);
+                        uBt = rule.getBuildTarget().getUnflavoredBuildTarget();
+                        array = uBt.getBaseName().split(SLASH);
+                        for (int i = 2; i < array.length; i++) {
+                            builder.append(array[i]).append(SLASH);
+                        }
+
+                        builder.append(LIB).append(uBt.getShortName())
+                                .append(OUT).append(SLASH)
+                                .append(uBt.getShortName()).append(PERIOD + JAR);
                     }
-                    builder.append(LIB).append(uBt.getShortName())
-                            .append(OUT).append(SLASH)
-                            .append(uBt.getShortName()).append(PERIOD + JAR);
-                    depJarPaths.add(builder.toString());
+                    if (builder.length() != 0) {
+                        depJarPaths.add(builder.toString());
+                    }
                 }
             }
         }