[ONOS-4583] Union defect fix.

Change-Id: Ic31866b9a1b7bd5d8209d5d22f4292ab9c79a118
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
index 04bb2e2..79857eb 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
@@ -17,6 +17,7 @@
 package org.onosproject.yangutils.translator.tojava;
 
 import java.io.IOException;
+
 import org.onosproject.yangutils.datamodel.TraversalType;
 import org.onosproject.yangutils.datamodel.YangNode;
 import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
@@ -27,6 +28,7 @@
 import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
 import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
 import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
 
 /**
  * Representation of java code generator based on application schema.
@@ -39,6 +41,11 @@
     private static YangNode curNode;
 
     /**
+     * Root node.
+     */
+    private static YangNode rootNode;
+
+    /**
      * Creates a java code generator utility object.
      */
     private JavaCodeGeneratorUtil() {
@@ -67,13 +74,14 @@
      *
      * @param rootNode   root node of the data model tree
      * @param yangPlugin YANG plugin config
-     * @throws TranslatorException when fails to generate java code file the current
-     *                             node
+     * @throws TranslatorException when fails to generate java code file the current node
+     * @throws IOException         when fails to do IO operations
      */
     public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
-            throws TranslatorException {
+            throws TranslatorException, IOException {
 
         YangNode codeGenNode = rootNode;
+        setRootNode(rootNode);
         TraversalType curTraversal = ROOT;
 
         while (codeGenNode != null) {
@@ -94,6 +102,7 @@
                     }
                     continue;
                 } catch (Exception e) {
+                    close(codeGenNode, yangPlugin);
                     throw new TranslatorException(e.getMessage());
                 }
 
@@ -103,16 +112,18 @@
                 codeGenNode = codeGenNode.getChild();
             } else if (codeGenNode.getNextSibling() != null) {
                 try {
-                    generateCodeExit(codeGenNode);
+                    generateCodeExit(codeGenNode, yangPlugin);
                 } catch (Exception e) {
+                    close(codeGenNode, yangPlugin);
                     throw new TranslatorException(e.getMessage());
                 }
                 curTraversal = SIBILING;
                 codeGenNode = codeGenNode.getNextSibling();
             } else {
                 try {
-                    generateCodeExit(codeGenNode);
+                    generateCodeExit(codeGenNode, yangPlugin);
                 } catch (Exception e) {
+                    close(codeGenNode, yangPlugin);
                     throw new TranslatorException(e.getMessage());
                 }
                 curTraversal = PARENT;
@@ -124,18 +135,18 @@
     /**
      * Generates the current nodes code snippet.
      *
-     * @param codeGenNode current data model node for which the code needs to be
-     *                    generated
+     * @param codeGenNode current data model node for which the code needs to be generated
      * @param yangPlugin  YANG plugin config
-     * @throws TranslatorException when fails to generate java code file the current
-     *                             node
+     * @throws TranslatorException when fails to generate java code file the current node
+     * @throws IOException         when fails to do IO operations
      */
     private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
-            throws TranslatorException {
+            throws TranslatorException, IOException {
 
         if (codeGenNode instanceof JavaCodeGenerator) {
             ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
         } else {
+            close(codeGenNode, yangPlugin);
             throw new TranslatorException(
                     "Generated data model node cannot be translated to target language code");
         }
@@ -144,25 +155,25 @@
     /**
      * Generates the current nodes code target code from the snippet.
      *
-     * @param codeGenNode current data model node for which the code needs to be
-     *                    generated
-     * @throws TranslatorException when fails to generate java code file the current
-     *                             node
+     * @param codeGenNode  current data model node for which the code needs to be generated
+     * @param pluginConfig plugin configurations
+     * @throws TranslatorException when fails to generate java code file the current node
+     * @throws IOException         when fails to do IO operations
      */
-    private static void generateCodeExit(YangNode codeGenNode)
-            throws TranslatorException {
+    private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
+            throws TranslatorException, IOException {
 
         if (codeGenNode instanceof JavaCodeGenerator) {
             ((JavaCodeGenerator) codeGenNode).generateCodeExit();
         } else {
+            close(codeGenNode, pluginConfig);
             throw new TranslatorException(
                     "Generated data model node cannot be translated to target language code");
         }
     }
 
     /**
-     * Free other YANG nodes of data-model tree when error occurs while file
-     * generation of current node.
+     * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
      */
     private static void freeRestResources() {
 
@@ -214,21 +225,18 @@
     /**
      * Delete Java code files corresponding to the YANG schema.
      *
-     * @param rootNode root node of data-model tree
+     * @param rootNode         root node of data-model tree
+     * @param yangPluginConfig plugin configurations
      * @throws IOException when fails to delete java code file the current node
      */
-    public static void translatorErrorHandler(YangNode rootNode)
+    public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
             throws IOException {
 
         if (rootNode != null) {
-            /**
-             * Free other resources where translator has failed.
-             */
+            //Free other resources where translator has failed.
             freeRestResources();
 
-            /**
-             * Start removing all open files.
-             */
+            // Start removing all open files.
             YangNode tempNode = rootNode;
             setCurNode(tempNode.getChild());
             TraversalType curTraversal = ROOT;
@@ -236,7 +244,7 @@
             while (tempNode != null) {
 
                 if (curTraversal != PARENT) {
-                    close(tempNode);
+                    close(tempNode, yangPluginConfig);
                 }
                 if (curTraversal != PARENT && tempNode.getChild() != null) {
                     curTraversal = CHILD;
@@ -255,17 +263,45 @@
     }
 
     /**
-     * Closes all the current open file handles of node and delete all generated
-     * files.
+     * Closes all the current open file handles of node and delete all generated files.
      *
-     * @param node current YANG node
+     * @param node       current YANG node
+     * @param yangPlugin plugin configurations
      * @throws IOException when fails to do IO operations
      */
-    private static void close(YangNode node)
+    private static void close(YangNode node, YangPluginConfig yangPlugin)
             throws IOException {
         if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
                 .getTempJavaCodeFragmentFiles() != null) {
             ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
+        } else {
+
+            JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
+            if (javaFileInfo != null) {
+                searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
+                        javaFileInfo.getPackageFilePath());
+            } else {
+                searchAndDeleteTempDir(yangPlugin.getManagerCodeGenDir());
+            }
+
         }
     }
+
+    /**
+     * Returns root node.
+     *
+     * @return root node
+     */
+    private static YangNode getRootNode() {
+        return rootNode;
+    }
+
+    /**
+     * Sets root node.
+     *
+     * @param rootNode root node
+     */
+    private static void setRootNode(YangNode rootNode) {
+        JavaCodeGeneratorUtil.rootNode = rootNode;
+    }
 }