[ONOS-4144] ,[ONOS-4145] Bug fixes for YANG translator.

Change-Id: I6922c01269611b3aec61a9e46cda5166f8fe1ec0
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangTypeDef.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangTypeDef.java
index c8381d0..a679cd9 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangTypeDef.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangTypeDef.java
@@ -333,6 +333,7 @@
             throw new IOException("Failed to create the source files.");
         }
         setFileHandle(handle);
+        getDerivedType().getDataTypeExtendedInfo().getBaseType().setJavaPackage(getPackage());
         addAttributeInfo();
         addAttributeInParent();
     }
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
index 6771421..a2478d9 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/plugin/manager/YangUtilManager.java
@@ -17,7 +17,6 @@
 package org.onosproject.yangutils.plugin.manager;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
@@ -56,6 +55,12 @@
     private String yangFilesDir;
 
     /**
+     * Source directory for generated files.
+     */
+    @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
+    private String genFilesDir;
+
+    /**
      * Base directory for project.
      */
     @Parameter(property = "basedir", defaultValue = "${basedir}")
@@ -105,7 +110,7 @@
             YangIoUtils.clean(baseDir);
 
             searchDir = baseDir + File.separator + yangFilesDir;
-            codeGenDir = baseDir + File.separator + UtilConstants.YANG_GEN_DIR;
+            codeGenDir = baseDir + File.separator + genFilesDir + File.separator;
 
             List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
             Iterator<String> yangFileIterator = yangFiles.iterator();
@@ -129,8 +134,8 @@
             }
 
             YangIoUtils.addToSource(baseDir + File.separator + UtilConstants.YANG_GEN_DIR, project, context);
-        } catch (final IOException e) {
-            getLog().info("IOException occured");
+        } catch (Exception e) {
+            getLog().info(e);
         }
     }
 
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
index 5e2cbe3..338cba3 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandle.java
@@ -305,8 +305,8 @@
     @Override
     public void close() throws IOException {
 
+        List<AttributeInfo> attrList = getCachedAttributeList();
         flushCacheAttrToTempFile();
-
         String className = getYangName();
         className = JavaIdentifierSyntax.getCaptialCase(className);
         String path = getRelativeFilePath();
@@ -322,11 +322,14 @@
 
         for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) {
             importString = UtilConstants.IMPORT;
-            if (importInfo.getPkgInfo() != null) {
+            if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null
+                    && importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) {
                 importString = importString + importInfo.getPkgInfo() + ".";
+                importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN
+                        + UtilConstants.NEW_LINE;
+
+                imports.add(importString);
             }
-            importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
-            imports.add(importString);
         }
         java.util.Collections.sort(imports);
 
@@ -342,7 +345,7 @@
             String interfaceFileName = className;
             File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this);
             interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
-                    getCachedAttributeList(), path.replace('/', '.'), this);
+                    attrList, path.replace('/', '.'), this);
             /**
              * Create temp builder interface file.
              */
@@ -350,7 +353,7 @@
             File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName,
                     TEMP_FILE_EXTENSION, this);
             builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
-                    path.replace('/', '.'), getCachedAttributeList(), this);
+                    path.replace('/', '.'), attrList, this);
             /**
              * Append builder interface file to interface file and close it.
              */
@@ -369,9 +372,11 @@
             JavaFileGenerator.clean(builderInterfaceFile);
         }
 
-        imports.add(UtilConstants.MORE_OBJECT_IMPORT);
-        imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT);
-        java.util.Collections.sort(imports);
+        if (!attrList.isEmpty()) {
+            imports.add(UtilConstants.MORE_OBJECT_IMPORT);
+            imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT);
+            java.util.Collections.sort(imports);
+        }
 
         if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0
                 || fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
@@ -382,7 +387,7 @@
             String builderFileName = className + UtilConstants.BUILDER;
             File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this);
             builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports,
-                    path.replace('/', '.'), getCachedAttributeList(), this);
+                    path.replace('/', '.'), attrList, this);
             /**
              * Create temp impl class file.
              */
@@ -390,7 +395,7 @@
             String implFileName = className + UtilConstants.IMPL;
             File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this);
             implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className,
-                    path.replace('/', '.'), getCachedAttributeList(), this);
+                    path.replace('/', '.'), attrList, this);
             /**
              * Append impl class to builder class and close it.
              */
@@ -418,7 +423,7 @@
             String typeDefFileName = className;
             File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this);
             typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
-                    path.replace('/', '.'), getCachedAttributeList(), this);
+                    path.replace('/', '.'), attrList, this);
             JavaFileGenerator.insert(typeDefFile,
                     JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName));
 
@@ -428,10 +433,17 @@
             JavaFileGenerator.closeFileHandles(typeDefFile);
         }
 
-        closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath());
-        JavaFileGenerator
-                .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className
-                        + TEMP_FOLDER_NAME_SUFIX));
+        if (!getCachedAttributeList().isEmpty()) {
+            closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath());
+            JavaFileGenerator
+                    .cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className
+                            + TEMP_FOLDER_NAME_SUFIX));
+        }
+
+        /*
+         * clear the contents from the cached attribute list.
+         */
+        getCachedAttributeList().clear();
     }
 
     @Override
@@ -512,7 +524,12 @@
                 + File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator;
 
         try {
-            return readFile(path + fileName + TEMP_FILE_EXTENSION);
+            String file = path + fileName + TEMP_FILE_EXTENSION;
+            if (new File(file).exists()) {
+                return readFile(path + fileName + TEMP_FILE_EXTENSION);
+            } else {
+                return "";
+            }
 
         } catch (FileNotFoundException e) {
             throw new FileNotFoundException("No such file or directory.");
@@ -533,8 +550,14 @@
             String line = bufferReader.readLine();
 
             while (line != null) {
-                stringBuilder.append(line);
-                stringBuilder.append("\n");
+                if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION)
+                        || line.equals(UtilConstants.EIGHT_SPACE_INDENTATION)
+                        || line.equals(UtilConstants.SPACE) || line.equals("") || line.equals(UtilConstants.NEW_LINE)) {
+                    stringBuilder.append("\n");
+                } else {
+                    stringBuilder.append(line);
+                    stringBuilder.append("\n");
+                }
                 line = bufferReader.readLine();
             }
             return stringBuilder.toString();
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
index 58a0896..c0f4a15 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
@@ -84,7 +84,7 @@
      */
     private static String getBuilderInterfaceDefinition(String yangName) {
         return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
-                + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
+                + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
     }
 
     /**
@@ -112,8 +112,7 @@
         return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
                 + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE
                 + UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
-                + UtilConstants.OPEN_CURLY_BRACKET
-                + UtilConstants.SPACE + UtilConstants.NEW_LINE;
+                + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
     }
 
     /**
@@ -126,7 +125,7 @@
 
         return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
                 + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
-                + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.SPACE + UtilConstants.NEW_LINE;
+                + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
     }
 
 }
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
index 0ae81f7..221f757 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -36,6 +36,7 @@
 import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
 import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
 import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
+import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
 import org.slf4j.Logger;
 
 /**
@@ -81,19 +82,21 @@
         String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
         initiateFile(file, className, INTERFACE_MASK, imports, pkg);
 
-        List<String> methods = new ArrayList<>();
-        try {
-            methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
-        } catch (ClassNotFoundException | IOException e) {
-            log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
-            throw new IOException("Fail to read data from temp file.");
-        }
+        if (!attrList.isEmpty()) {
+            List<String> methods = new ArrayList<>();
+            try {
+                methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
 
-        /**
-         * Add getter methods to interface file.
-         */
-        for (String method : methods) {
-            appendMethod(file, method);
+            /**
+             * Add getter methods to interface file.
+             */
+            for (String method : methods) {
+                appendMethod(file, method);
+            }
         }
         return file;
     }
@@ -115,14 +118,16 @@
         initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
         List<String> methods = new ArrayList<>();
 
-        try {
-            methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
-            methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
-        } catch (ClassNotFoundException | IOException e) {
-            log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
-            throw new IOException("Fail to read data from temp file.");
-        }
+        if (!attrList.isEmpty()) {
 
+            try {
+                methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
+                methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
+        }
         /**
          * Add build method to builder interface file.
          */
@@ -156,38 +161,41 @@
         String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
         initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
 
-        /**
-         * Add attribute strings.
-         */
-        List<String> attributes = new ArrayList<>();
-        try {
-            attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
-        } catch (ClassNotFoundException | IOException e) {
-            log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
-            throw new IOException("Fail to read data from temp file.");
-        }
-        /**
-         * Add attributes to the file.
-         */
-        for (String attribute : attributes) {
-            insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
-        }
-
         List<String> methods = new ArrayList<>();
-        try {
-            methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
-            methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
-        } catch (ClassNotFoundException | IOException e) {
-            log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
-            throw new IOException("Fail to read data from temp file.");
-        }
+        if (!attrList.isEmpty()) {
+            /**
+             * Add attribute strings.
+             */
+            List<String> attributes = new ArrayList<>();
+            try {
+                attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
+            /**
+             * Add attributes to the file.
+             */
+            for (String attribute : attributes) {
+                insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
+            }
 
+            try {
+                methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
+                methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
+
+        }
         /**
          * Add default constructor and build method impl.
          */
-        methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
-                + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
-                        UtilConstants.PUBLIC));
+        methods.add(
+                UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+                        + MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
+                                UtilConstants.PUBLIC));
         methods.add(MethodsGenerator.getBuildString(className));
 
         /**
@@ -216,47 +224,55 @@
         String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
         initiateFile(file, className, IMPL_CLASS_MASK, null, path);
 
-        List<String> attributes = new ArrayList<>();
-        try {
-            attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
-        } catch (ClassNotFoundException | IOException e) {
-            log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
-            throw new IOException("Fail to read data from temp file.");
-        }
-
-        /**
-         * Add attributes to the file.
-         */
-        for (String attribute : attributes) {
-            insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
-        }
-
         List<String> methods = new ArrayList<>();
+        if (!attrList.isEmpty()) {
+            List<String> attributes = new ArrayList<>();
+            try {
+                attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
+
+            /**
+             * Add attributes to the file.
+             */
+            for (String attribute : attributes) {
+                insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
+            }
+
+            try {
+
+                methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
+
+                methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
+                        + YangIoUtils
+                                .partString(handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(
+                                        UtilConstants.NEW_LINE, ""))));
+
+                methods.add(MethodsGenerator
+                        .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
+                                + handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
+
+                methods.add(MethodsGenerator.getToStringMethodOpen()
+                        + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
+                        + MethodsGenerator.getToStringMethodClose());
+
+            } catch (ClassNotFoundException | IOException e) {
+                log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
+                throw new IOException("Fail to read data from temp file.");
+            }
+
+        }
+
         try {
-
-            methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
-
             methods.add(getConstructorString(className)
                     + handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path)
                     + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
-
-            methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
-                    + handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(UtilConstants.NEW_LINE,
-                            "")));
-
-            methods.add(MethodsGenerator
-                    .getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
-                            + handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
-
-            methods.add(MethodsGenerator.getToStringMethodOpen()
-                    + handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
-                    + MethodsGenerator.getToStringMethodClose());
-
         } catch (ClassNotFoundException | IOException e) {
             log.info("There is no attribute info of " + className + " YANG file in the temporary files.");
             throw new IOException("Fail to read data from temp file.");
         }
-
         /**
          * Add methods in impl class.
          */
@@ -315,8 +331,9 @@
         String javadoc = MethodsGenerator.getConstructorString(yangName);
         String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
                 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
-                + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
-                + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
+                + UtilConstants.SPACE + builderAttribute + UtilConstants.BUILDER + UtilConstants.OBJECT
+                + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+                + UtilConstants.NEW_LINE;
         return javadoc + constructor;
     }
 
@@ -514,15 +531,16 @@
                         + MethodsGenerator.getSetterForTypeDefClass(attr)
                         + UtilConstants.NEW_LINE;
 
-                hashCodeString = hashCodeString + MethodsGenerator.getHashCodeMethodOpen()
-                        + MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, "");
+                hashCodeString = MethodsGenerator.getHashCodeMethodOpen()
+                        + YangIoUtils.partString(
+                                MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, ""));
                 hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE;
 
-                equalsString = equalsString + MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
+                equalsString = MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
                         + MethodsGenerator.getEqualsMethod(attr);
                 equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE;
 
-                toString = toString + MethodsGenerator.getToStringMethodOpen()
+                toString = MethodsGenerator.getToStringMethodOpen()
                         + MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE
                         + MethodsGenerator.getToStringMethodClose()
                         + UtilConstants.NEW_LINE;
@@ -568,7 +586,6 @@
                 for (String imports : importsList) {
                     insert(file, imports);
                 }
-                insert(file, UtilConstants.NEW_LINE);
             }
             write(file, fileName, type, JavaDocType.IMPL_CLASS);
         } else {
@@ -581,7 +598,6 @@
                     for (String imports : importsList) {
                         insert(file, imports);
                     }
-                    insert(file, UtilConstants.NEW_LINE);
                 }
                 write(file, fileName, type, JavaDocType.INTERFACE);
             } else if ((type & BUILDER_CLASS_MASK) != 0) {
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index 6ca6829..576ebff 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -434,7 +434,7 @@
         String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
                 + UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName)
                 + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute
-                + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+                + UtilConstants.BUILDER + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
                 + JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName))
                 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
                 + UtilConstants.NEW_LINE;
@@ -539,7 +539,6 @@
     public static String getHashCodeMethodClose(String hashcodeString) {
         hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA);
         hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE);
-        hashcodeString = YangIoUtils.partString(hashcodeString);
         return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
                 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
     }
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index 821f2b3..740acd6 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -119,7 +119,7 @@
     /**
      * For directories.
      */
-    public static final String YANG_GEN_DIR = "src/main/yangmodel/";
+    public static final String YANG_GEN_DIR = "src/main/java/";
     public static final String DEFAULT_BASE_PKG = "org.onosproject.yang.gen";
     public static final String REVISION_PREFIX = "rev";
     public static final String VERSION_PREFIX = "v";
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
index 271d698..a5f0e88 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/FileSystemUtil.java
@@ -122,8 +122,14 @@
             String line = bufferReader.readLine();
 
             while (line != null) {
-                stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
-                stringBuilder.append("\n");
+                if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION)
+                        || line.equals(UtilConstants.EIGHT_SPACE_INDENTATION)
+                        || line.equals(UtilConstants.SPACE) || line.equals("") || line.equals(UtilConstants.NEW_LINE)) {
+                    stringBuilder.append("\n");
+                } else {
+                    stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
+                    stringBuilder.append("\n");
+                }
                 line = bufferReader.readLine();
             }
             return stringBuilder.toString();
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index 72bedf9..6461aa5 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -245,8 +245,8 @@
      * @return javaDocs
      */
     private static String generateForImplClass(String className) {
-        return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC + className + UtilConstants.PERIOD
-                + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
+        return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC
+                + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
     }
 
     /**
@@ -310,13 +310,15 @@
      * @return javaDocs
      */
     private static String generateForConstructors(String className) {
-        return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.FOUR_SPACE_INDENTATION
-                + UtilConstants.JAVA_DOC_CONSTRUCTOR + className + UtilConstants.IMPL + UtilConstants.PERIOD
-                + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+        return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+                + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_CONSTRUCTOR + className
+                + UtilConstants.IMPL + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+                + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
                 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
-                + className.substring(0, 1).toLowerCase() + className.substring(1) + UtilConstants.OBJECT
-                + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className
-                + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE;
+                + className.substring(0, 1).toLowerCase() + className.substring(1) + UtilConstants.BUILDER
+                + UtilConstants.OBJECT + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE
+                + className + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+                + UtilConstants.JAVA_DOC_END_LINE;
     }
 
     /**
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java
deleted file mode 100644
index b3e9ff5..0000000
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/TempDataStore.java
+++ /dev/null
@@ -1,216 +0,0 @@
-/*
- * Copyright 2016 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.yangutils.utils.io.impl;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInput;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutput;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Provides storage for Temp data while traversing data model tree for code
- * generation.
- */
-public final class TempDataStore {
-
-    /**
-     * Data Store types.
-     */
-    public static enum TempDataStoreType {
-
-        /**
-         * Methods.
-         */
-        GETTER_METHODS,
-
-        /**
-         * Methods.
-         */
-        BUILDER_METHODS,
-
-        /**
-         * Methods.
-         */
-        BUILDER_INTERFACE_METHODS,
-
-        /**
-         * Methods.
-         */
-        IMPL_METHODS,
-
-        /**
-         * Attributes.
-         */
-        ATTRIBUTE,
-
-        /**
-         * Imports.
-         */
-        IMPORT
-    }
-
-    /**
-     * File name string for Temp files of methods.
-     */
-    private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore";
-
-    /**
-     * File name string for Temp files of methods.
-     */
-    private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore";
-
-    /**
-     * File name string for Temp files of methods.
-     */
-    private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore";
-
-    /**
-     * File name string for Temp files of methods.
-     */
-    private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore";
-
-    /**
-     * File name string for Temp files of attributes.
-     */
-    private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore";
-
-    /**
-     * File name string for Temp files of imports.
-     */
-    private static final String IMPORT_FILE_NAME = "TempImportDataStore";
-
-    /**
-     * File extension of Temp files.
-     */
-    private static final String FILE_EXTENSION = ".tmp";
-
-    /**
-     * Directory for generating Temp files.
-     */
-    private static final String GEN_DIR = "target/";
-
-    /**
-     * Buffer size.
-     */
-    private static final int BUFFER_SIZE = 8 * 1024;
-
-    /**
-     * Default constructor.
-     */
-    private TempDataStore() {
-    }
-
-    /**
-     * Writes specific info to a Temp file.
-     *
-     * @param data data to be stored
-     * @param type type of Temp data store
-     * @param className class name
-     * @throws IOException when fails to create a Temp data file
-     */
-    public static void setTempData(String data, TempDataStoreType type, String className) throws IOException {
-
-        String fileName = "";
-        if (type.equals(TempDataStoreType.ATTRIBUTE)) {
-            fileName = ATTRIBUTE_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
-            fileName = GETTER_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
-            fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
-            fileName = BUILDER_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
-            fileName = IMPL_METHOD_FILE_NAME;
-        } else {
-            fileName = IMPORT_FILE_NAME;
-        }
-
-        File dir = new File(GEN_DIR + className + File.separator);
-        dir.mkdirs();
-        try {
-            OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
-            OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
-
-            ObjectOutput output = new ObjectOutputStream(buffer);
-            try {
-                output.writeObject(data);
-            } finally {
-                output.close();
-            }
-        } catch (IOException ex) {
-            throw new IOException("failed to serialize data");
-        }
-    }
-
-    /**
-     * Get the Temp data.
-     *
-     * @param type type of Temp data store
-     * @param className name of the class
-     * @return list of attribute info
-     * @throws IOException when fails to read from the file
-     * @throws ClassNotFoundException when class is missing
-     * @throws FileNotFoundException when file is missing
-     */
-    public static List<String> getTempData(TempDataStoreType type, String className)
-            throws IOException, FileNotFoundException, ClassNotFoundException {
-
-        String fileName = "";
-        if (type.equals(TempDataStoreType.ATTRIBUTE)) {
-            fileName = ATTRIBUTE_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
-            fileName = GETTER_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
-            fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
-            fileName = BUILDER_METHOD_FILE_NAME;
-        } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
-            fileName = IMPL_METHOD_FILE_NAME;
-        } else {
-            fileName = IMPORT_FILE_NAME;
-        }
-        try {
-            InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
-            InputStream buffer = new BufferedInputStream(file);
-            ObjectInput input = new ObjectInputStream(buffer);
-            try {
-                String data = (String) input.readObject();
-                List<String> recoveredData = new ArrayList<>();
-                recoveredData.add(data);
-                return recoveredData;
-            } finally {
-                input.close();
-                file.close();
-            }
-        } catch (FileNotFoundException ex) {
-            throw new FileNotFoundException("No such file or directory.");
-        } catch (ClassNotFoundException ex) {
-            throw new ClassNotFoundException("failed to fetch the Temp data file.");
-        }
-    }
-}
diff --git a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
index d410cfb..dfbb234 100644
--- a/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
+++ b/utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -94,7 +94,8 @@
      * @param baseDir generated directory in previous build
      */
     public static void clean(String baseDir) {
-        File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
+        File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR
+                + UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
         if (generatedDirectory.exists()) {
             List<String> javafiles;
             try {
@@ -151,10 +152,11 @@
         String[] strArray = partString.split(UtilConstants.COMMA);
         String newString = "";
         for (int i = 0; i < strArray.length; i++) {
-            if (i % 4 != 0) {
+            if (i % 4 != 0 || i == 0) {
                 newString = newString + strArray[i] + UtilConstants.COMMA;
             } else {
-                newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + strArray[i]
+                newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION
+                        + strArray[i]
                         + UtilConstants.COMMA;
             }
         }