Merge "[ONOS-5631] YANG tool independent of tool chain"
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangDecimal64.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangDecimal64.java
index 58fd8ce..c61c69d 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangDecimal64.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangDecimal64.java
@@ -271,10 +271,8 @@
         } else {
             // Check value is in fraction-digits decimal64 value range
             if (!FractionDigits.isValueInDecimal64Range(value, getFractionDigit())) {
-                throw new DataModelException(getErrorMsg(
-                        "YANG file error : decimal64 validation failed.", "decimal64",
-                        getLineNumber(), getCharPosition(), getFileName() + "\""));
-
+                throw new DataModelException(
+                        "YANG file error : value is not in decimal64 range.");
             }
         }
     }
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
index 28aceec..f8ebda1 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeaf.java
@@ -25,6 +25,8 @@
 import java.util.List;
 import java.util.Map;
 
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.validateEmptyDataType;
+
 /*
  * Reference:RFC 6020.
  *  The "leaf" statement is used to define a leaf node in the schema
@@ -312,6 +314,11 @@
         return dataType;
     }
 
+    @Override
+    public boolean isEmptyDataType() {
+        return validateEmptyDataType(dataType);
+    }
+
     /**
      * Sets the data type.
      *
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
index b7f6931..ab1b468 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangLeafList.java
@@ -16,13 +16,16 @@
 
 package org.onosproject.yangutils.datamodel;
 
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.datamodel.utils.Parsable;
+import org.onosproject.yangutils.datamodel.utils.YangConstructType;
+
 import java.io.Serializable;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
-import org.onosproject.yangutils.datamodel.utils.Parsable;
-import org.onosproject.yangutils.datamodel.utils.YangConstructType;
+
+import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.validateEmptyDataType;
 
 /*
  *  Reference:RFC 6020.
@@ -329,6 +332,11 @@
         return dataType;
     }
 
+    @Override
+    public boolean isEmptyDataType() {
+        return validateEmptyDataType(dataType);
+    }
+
     /**
      * Sets the data type.
      *
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
index 26fcbd4..bc665ff 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangNode.java
@@ -690,6 +690,12 @@
         throw new DataModelException("Method is called for node other than module/sub-module.");
     }
 
+    @Override
+    public boolean isEmptyDataType() throws DataModelException {
+        throw new DataModelException("Method is called for node other than " +
+                                             "leaf/leaf-list.");
+    }
+
     /**
      * Adds child schema in child schema map, this is used to add the schema
      * to the map in case of leaf as a child.
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
index 8489d73..b138fc1 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangSchemaNode.java
@@ -146,4 +146,13 @@
      * @return referred schema node
      */
     YangSchemaNode getReferredSchema();
+
+    /**
+     * Checks for the presence of empty data-type in requested schema node.
+     * Exception will be thrown if this is called for other then leaf/leaf-list
+     * node type.
+     *
+     * @return true if empty data-type is present, false otherwise
+     */
+    boolean isEmptyDataType() throws DataModelException;
 }
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
index a0ea239..eb38fb7 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangUnion.java
@@ -16,14 +16,18 @@
 
 package org.onosproject.yangutils.datamodel;
 
-import java.util.LinkedList;
-import java.util.List;
-
 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
 import org.onosproject.yangutils.datamodel.utils.Parsable;
 import org.onosproject.yangutils.datamodel.utils.YangConstructType;
 import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
 
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.onosproject.yangutils.datamodel.YangSchemaNodeType.YANG_NON_DATA_NODE;
+import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsg;
+import static org.onosproject.yangutils.datamodel.utils.YangConstructType.UNION_DATA;
+
 /*
  * Reference RFC 6020.
  *
@@ -91,7 +95,7 @@
 
     @Override
     public YangSchemaNodeType getYangSchemaNodeType() {
-        return YangSchemaNodeType.YANG_NON_DATA_NODE;
+        return YANG_NON_DATA_NODE;
     }
 
     @Override
@@ -99,14 +103,6 @@
         return typeList;
     }
 
-    /**
-     * Sets the list of YANG type.
-     *
-     * @param typeList list of YANG type.
-     */
-    public void setTypeList(List<YangType<?>> typeList) {
-        this.typeList = typeList;
-    }
 
     /**
      * Returns running child union number.
@@ -135,19 +131,24 @@
      */
     public void addType(YangType<?> yangType)
             throws DataModelException {
-        if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) {
-            throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " +
-                    "\"leafref\"" + getName() + " in " +
-                    getLineNumber() + " at " +
-                    getCharPosition()
-                    + " in " + getFileName() + "\"");
+        YangDataTypes type = yangType.getDataType();
+        String msg = "Union member type must not be one of the built-in types" +
+                " \"empty\" or \"leafref\"";
+        switch (type) {
+            case EMPTY:
+            case LEAFREF:
+                throw new DataModelException(getErrorMsg(
+                        msg, getName(), getLineNumber(), getCharPosition(),
+                        getFileName()));
+
+            default:
+                typeList.add(yangType);
         }
-        getTypeList().add(yangType);
     }
 
     @Override
     public YangConstructType getYangConstructType() {
-        return YangConstructType.UNION_DATA;
+        return UNION_DATA;
     }
 
     /**
diff --git a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
index c169250..67e05b7 100644
--- a/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
+++ b/datamodel/src/main/java/org/onosproject/yangutils/datamodel/utils/DataModelUtils.java
@@ -22,6 +22,7 @@
 import org.onosproject.yangutils.datamodel.YangAugment;
 import org.onosproject.yangutils.datamodel.YangBase;
 import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
+import org.onosproject.yangutils.datamodel.YangDerivedInfo;
 import org.onosproject.yangutils.datamodel.YangEntityToResolveInfoImpl;
 import org.onosproject.yangutils.datamodel.YangEnumeration;
 import org.onosproject.yangutils.datamodel.YangIdentityRef;
@@ -59,6 +60,8 @@
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
+
 /**
  * Represents utilities for data model tree.
  */
@@ -808,4 +811,35 @@
         jar.close();
         return nodes;
     }
+
+    /**
+     * Validates the requested data-type resolve type in empty or not.
+     *
+     * @param dataType the data type
+     * @return true, for empty resolved data-type; false otherwise
+     */
+    public static boolean validateEmptyDataType(YangType dataType) {
+        switch (dataType.getDataType()) {
+            case DERIVED:
+                return ((YangDerivedInfo) dataType.getDataTypeExtendedInfo())
+                        .getEffectiveBuiltInType().equals(EMPTY);
+
+            case LEAFREF:
+                YangType type = ((YangLeafRef) dataType
+                        .getDataTypeExtendedInfo())
+                        .getEffectiveDataType();
+                if (type.getDataType() == YangDataTypes.DERIVED) {
+                    return ((YangDerivedInfo) type.getDataTypeExtendedInfo())
+                            .getEffectiveBuiltInType().equals(EMPTY);
+                }
+                return ((YangLeafRef) dataType.getDataTypeExtendedInfo())
+                        .getEffectiveDataType().getDataType().equals(EMPTY);
+
+            case UNION:
+                return ((YangUnion) dataType.getDataTypeExtendedInfo())
+                        .getTypeList().contains(EMPTY);
+            default:
+                return dataType.getDataType().equals(EMPTY);
+        }
+    }
 }
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
index 548dbac..913220b 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
@@ -690,7 +690,7 @@
             qualified = parentImportData.addImportInfo(typeInfo, className,
                                                        fileInfo.getPackage());
             if (!qualified && !(curNode instanceof YangChoice) &&
-                    curNode.isOpTypeReq()) {
+                    targetNode.isOpTypeReq()) {
                 String name = DEFAULT_CAPS + typeInfo.getClassInfo();
                 JavaQualifiedTypeInfoTranslator qInfo =
                         new JavaQualifiedTypeInfoTranslator();
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
index cb44ca3..7c9dfa5 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
@@ -20,6 +20,7 @@
 import org.onosproject.yangutils.datamodel.YangType;
 import org.onosproject.yangutils.datamodel.YangTypeDef;
 import org.onosproject.yangutils.datamodel.YangTypeHolder;
+import org.onosproject.yangutils.datamodel.YangUnion;
 import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
 import org.onosproject.yangutils.translator.exception.TranslatorException;
 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
@@ -30,6 +31,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.onosproject.yangutils.datamodel.exceptions.ErrorMessages.getErrorMsg;
 import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
 import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
 import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16;
@@ -245,6 +247,32 @@
         return ofStringImplTempFileHandle;
     }
 
+    private void verifyUnionTypes(List<YangType<?>> typeList,
+                                  YangTypeHolder yangTypeHolder) {
+        String msg;
+        YangUnion union = (YangUnion) yangTypeHolder;
+        for (YangType<?> yangType : typeList) {
+            YangDataTypes type = yangType.getDataType();
+            switch (type) {
+                case EMPTY:
+                    msg = "Union member derived type must not be one of the " +
+                            "type whose built-in types is \"empty\"";
+                    break;
+                case LEAFREF:
+                    msg = "Union member derived type must not be one of the " +
+                            "type whose built-in types is \"leafref\"";
+                    break;
+                default:
+                    msg = null;
+            }
+            if (msg != null) {
+                throw new TranslatorException(getErrorMsg(
+                        msg, union.getName(), union.getLineNumber(), union
+                                .getCharPosition(), union.getFileName()));
+            }
+        }
+    }
+
     /**
      * Adds all the type in the current data model node as part of the generated temporary file.
      *
@@ -254,8 +282,10 @@
      */
     void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig config)
             throws IOException {
-
         List<YangType<?>> typeList = yangTypeHolder.getTypeList();
+        if (yangTypeHolder instanceof YangUnion) {
+            verifyUnionTypes(typeList, yangTypeHolder);
+        }
         if (typeList != null) {
             List<YangType<?>> types = validateTypes(typeList);
             for (YangType<?> type : types) {
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
index b7928ce..116c905 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -760,7 +760,7 @@
                                    imports, path);
 
         List<String> methods = new ArrayList<>();
-
+        insertDataIntoJavaFile(file, NEW_LINE);
         //Add attribute strings.
         try {
             insertDataIntoJavaFile(file, getDataFromTempFileHandle(
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
index 66950b5..fe6a40b 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -459,7 +459,8 @@
                 break;
             case DEFAULT_CLASS_MASK:
                 appendHeaderContents(file, pkgString, importsList);
-                write(file, genType, DEFAULT_CLASS, curNode, className, true);
+                write(file, genType, DEFAULT_CLASS, curNode, className,
+                      curNode.isOpTypeReq());
                 break;
             case BUILDER_CLASS_MASK:
                 write(file, genType, BUILDER_CLASS, curNode, className, false);
diff --git a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index 0b11fda..edf0216 100644
--- a/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/generator/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -2028,6 +2028,7 @@
      */
     public static String getToStringMethodForIdentity(String name) {
         StringBuilder builder = new StringBuilder(NEW_LINE);
+        builder.append(getJavaDoc(GETTER_METHOD, name, false, null));
         String returnVal = getQuotedString(name);
         String methodName = getCamelCase(name, null) + TO_CAPS + STRING_DATA_TYPE;
         builder.append(methodSignature(methodName, null, PUBLIC + SPACE + STATIC,
@@ -2047,6 +2048,7 @@
     public static String getFromStringMethodForIdentity(String name,
                                                         String schemaName) {
         StringBuilder builder = new StringBuilder(NEW_LINE);
+        builder.append(getJavaDoc(FROM_METHOD, name, false, null));
         String caps = getCapitalCase(name);
         String returnVal = caps + PERIOD + CLASS;
         String cond = getTwoParaEqualsString(FROM_STRING_PARAM_NAME,
diff --git a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index 2b2a203..5c499db 100644
--- a/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/generator/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -406,7 +406,7 @@
     private static String generateForFromString(String attribute) {
         return getJavaDocStartLine(attribute, JAVA_DOC_OF
                 + attribute + SPACE + FROM_STRING_METHOD_NAME + SPACE + INPUT +
-                SPACE + STRING_DATA_TYPE) +
+                SPACE + STRING_DATA_TYPE + SPACE) +
                 getJavaDocEmptyAsteriskLine() +
                 getJavaDocParamLine(INPUT + SPACE + STRING_DATA_TYPE,
                                     FROM_STRING_PARAM_NAME) +
diff --git a/plugin/maven/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java b/plugin/maven/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java
index 30e61e3..9a0373e 100644
--- a/plugin/maven/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java
+++ b/plugin/maven/src/test/java/org/onosproject/yangutils/parser/impl/listeners/Decimal64ListenerTest.java
@@ -156,7 +156,8 @@
     @Test
     public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException {
         thrown.expect(DataModelException.class);
-        thrown.expectMessage("YANG file error : decimal64 validation failed.");
+        thrown.expectMessage(
+                "YANG file error : value is not in decimal64 range.");
 
         YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
 
diff --git a/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java b/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
index a7320de..1c5c56c 100644
--- a/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
+++ b/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
@@ -105,6 +105,6 @@
         yangPluginConfig.setCodeGenDir(DIR);
         utilManager.translateToJava(yangPluginConfig);
         compileCode(DIR1);
-        //deleteDirectory(DIR);
+        deleteDirectory(DIR);
     }
 }
diff --git a/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/YangXpathLinkerTest.java b/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/YangXpathLinkerTest.java
index 3cf52b5..d4bea49 100644
--- a/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/YangXpathLinkerTest.java
+++ b/plugin/maven/src/test/java/org/onosproject/yangutils/plugin/manager/YangXpathLinkerTest.java
@@ -758,7 +758,5 @@
         compileCode(dir);
         deleteDirectory("target/xpath/");
         assertThat(true, is(targetNode.getName().equals(targetNodeName)));
-
-        deleteDirectory("target/xpath/");
     }
 }
diff --git a/plugin/maven/src/test/resources/typedefTranslator/union/typedefs.yang b/plugin/maven/src/test/resources/typedefTranslator/union/typedefs.yang
index 4a3d150..276efa2 100644
--- a/plugin/maven/src/test/resources/typedefTranslator/union/typedefs.yang
+++ b/plugin/maven/src/test/resources/typedefTranslator/union/typedefs.yang
@@ -9,7 +9,6 @@
      type union {
          type ipv4-address;
          type ipv6-address;
-         type leaf;
      }
   }