[ONOS-4947][ONOS-4954][ONOS-4952]Defect Fixed: Invalid name in case of qualified info for child nodes

Change-Id: I0b94455333afd9322c1e583a5c3ec311dbe99991
diff --git a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
index 75b92b4..6815a3a 100644
--- a/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
+++ b/utils/yangutils/datamodel/src/main/java/org/onosproject/yangutils/datamodel/YangType.java
@@ -302,8 +302,11 @@
                 break;
             }
             case STRING: {
-                if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value) &&
-                        ((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnPatternRestriction(value))) {
+                if (getDataTypeExtendedInfo() == null) {
+                    break;
+                } else if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value)
+                        && ((YangStringRestriction) getDataTypeExtendedInfo())
+                        .isValidStringOnPatternRestriction(value))) {
                     throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " +
                                                         "string");
                 }
@@ -319,7 +322,7 @@
                 Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator();
                 boolean isValidated = false;
                 while (iterator.hasNext()) {
-                    YangEnum enumTemp = (YangEnum) iterator.next();
+                    YangEnum enumTemp = iterator.next();
                     if (enumTemp.getNamedValue().equals(value)) {
                         isValidated = true;
                         break;
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
index 9c37bb8..2534756 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/JavaCodeGeneratorUtil.java
@@ -19,12 +19,14 @@
 import java.io.IOException;
 
 import org.onosproject.yangutils.datamodel.TraversalType;
+import org.onosproject.yangutils.datamodel.YangInput;
 import org.onosproject.yangutils.datamodel.YangNode;
-import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.YangOutput;
 import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
 import org.onosproject.yangutils.translator.exception.TranslatorException;
 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
-
+import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
 import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
 import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
 import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
@@ -305,4 +307,43 @@
     private static void setRootNode(YangNode rootNode) {
         JavaCodeGeneratorUtil.rootNode = rootNode;
     }
+
+    /**
+     * Searches child node in data model tree.
+     *
+     * @param parentNode parent node
+     * @param nodeType   node type
+     * @param nodeName   node name
+     * @return child node
+     */
+    public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
+        YangNode child = parentNode.getChild();
+        TraversalType curTraversal = ROOT;
+        if (child == null) {
+            throw new IllegalArgumentException("given parent node does not contain any child nodes");
+        }
+
+        while (child != null) {
+            if (curTraversal != PARENT) {
+                if (child instanceof YangInput || child instanceof YangOutput) {
+                    if (child.getNodeType().equals(nodeType)) {
+                        return child;
+                    }
+                } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
+                    return child;
+                }
+            }
+            if (curTraversal != PARENT && child.getChild() != null) {
+                curTraversal = CHILD;
+                child = child.getChild();
+            } else if (child.getNextSibling() != null) {
+                curTraversal = SIBILING;
+                child = child.getNextSibling();
+            } else {
+                curTraversal = PARENT;
+                child = child.getParent();
+            }
+        }
+        return null;
+    }
 }
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaEventFragmentFiles.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaEventFragmentFiles.java
index 01587b7..9ca6b4e 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaEventFragmentFiles.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaEventFragmentFiles.java
@@ -100,7 +100,7 @@
     /**
      * File name for generated class file for special type like union, typedef suffix.
      */
-    private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
+    private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "EventListener";
 
     private static final String JAVA_FILE_EXTENSION = ".java";
 
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
index 480c321..36473ce 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaFragmentFiles.java
@@ -33,8 +33,6 @@
 import org.onosproject.yangutils.translator.exception.TranslatorException;
 import org.onosproject.yangutils.translator.tojava.javamodel.JavaLeafInfoContainer;
 import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
-import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
-import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
 import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
 
 import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
@@ -494,7 +492,7 @@
         JavaFileInfo fileInfo = ((JavaFileInfoContainer) targetNode).getJavaFileInfo();
 
         boolean isQualified;
-        if ((targetNode instanceof YangJavaModuleTranslator || targetNode instanceof YangJavaSubModuleTranslator)
+        if ((tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles)
                 && (qualifiedTypeInfo.getClassInfo().contentEquals(SERVICE)
                 || qualifiedTypeInfo.getClassInfo().contentEquals(COMPONENT)
                 || qualifiedTypeInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
@@ -507,7 +505,7 @@
             isQualified = true;
         } else {
             String className;
-            if (targetNode instanceof YangJavaModuleTranslator || targetNode instanceof YangJavaSubModuleTranslator) {
+            if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) {
                 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
             } else {
                 className = getCapitalCase(fileInfo.getJavaName());
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
index ce97834..a4109c5 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
@@ -386,7 +386,7 @@
             throw new RuntimeException("Event listener interface name is error");
         }
         intfDef = intfDef.substring(0, intfDef.length() - 8);
-        intfDef = intfDef + "Event>" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
+        intfDef = intfDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
 
         return intfDef;
     }
diff --git a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index d570fa7..0d76d7b 100644
--- a/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/utils/yangutils/plugin/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -1191,7 +1191,7 @@
     /**
      * For event listener file generation.
      */
-    public static final String EVENT_LISTENER_STRING = "Listener";
+    public static final String EVENT_LISTENER_STRING = "EventListener";
 
     /**
      * For event subject file generation.
diff --git a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
index dc728c4..b674d26 100644
--- a/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
+++ b/utils/yangutils/plugin/src/test/java/org/onosproject/yangutils/plugin/manager/TypeDefTranslatorTest.java
@@ -41,7 +41,7 @@
     @Test
     public void processTypeDefTranslator() throws IOException, ParserException, MojoExecutionException {
 
-        String searchDir = "src/test/resources/typedefTranslator";
+        String searchDir = "src/test/resources/typedefTranslator/without";
         utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
         utilManager.parseYangFileInfoSet();
         utilManager.createYangNodeSet();
@@ -55,4 +55,27 @@
         deleteDirectory("target/typedefTranslator/");
     }
 
+    /**
+     * Checks typedef translation should not result in any exception.
+     *
+     * @throws MojoExecutionException
+     */
+    @Test
+    public void processTypeDefWithRestrictionsTranslator() throws IOException, ParserException, MojoExecutionException {
+
+        /*FIXME: After typedef with leafref is fixed.
+        String searchDir = "src/test/resources/typedefTranslator/with";
+        utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
+        utilManager.parseYangFileInfoSet();
+        utilManager.createYangNodeSet();
+        utilManager.resolveDependenciesUsingLinker();
+
+        YangPluginConfig yangPluginConfig = new YangPluginConfig();
+        yangPluginConfig.setCodeGenDir("target/typedefTranslator/");
+        yangPluginConfig.setManagerCodeGenDir("target/typedefTranslator/");
+        utilManager.translateToJava(yangPluginConfig);
+
+        deleteDirectory("target/typedefTranslator/");
+        */
+    }
 }
diff --git a/utils/yangutils/plugin/src/test/resources/typedefTranslator/Onos_Yang_1.yang b/utils/yangutils/plugin/src/test/resources/typedefTranslator/wihtout/Onos_Yang_1.yang
similarity index 100%
rename from utils/yangutils/plugin/src/test/resources/typedefTranslator/Onos_Yang_1.yang
rename to utils/yangutils/plugin/src/test/resources/typedefTranslator/wihtout/Onos_Yang_1.yang
diff --git a/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/Onos_Yang_1.yang b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/Onos_Yang_1.yang
new file mode 100644
index 0000000..c09e372
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/Onos_Yang_1.yang
@@ -0,0 +1,201 @@
+module typedef {
+
+    yang-version "1";
+    namespace "http://rob.sh/yang/test/list";
+    prefix "foo";
+
+    import remote { prefix defn; }
+
+    organization "BugReports Inc";
+    contact "A bug reporter";
+
+    description
+        "A test module";
+    revision 2014-01-01 {
+        description "april-fools";
+        reference "fooled-you";
+    }
+
+    typedef derived-string-type {
+        type string;
+    }
+
+    typedef restricted-integer-type {
+        type uint16 {
+            range 0..64;
+        }
+    }
+
+    typedef bgp-session-direction {
+        type enumeration {
+            enum INBOUND;
+            enum OUTBOUND;
+        }
+    }
+
+    typedef new-string-type {
+        type string;
+        default "defaultValue";
+    }
+
+    typedef restricted-inherit {
+        type string {
+            pattern "^a.*";
+        }
+    }
+
+    typedef restricted-int-inherit {
+        type int8 {
+            range 0..100;
+        }
+    }
+
+    typedef parent-union {
+        type union {
+            type string {
+                pattern "a.*";
+            }
+            type string {
+                pattern "b.*";
+            }
+        }
+    }
+
+    typedef child-union {
+        type union {
+            type parent-union;
+            type string {
+                pattern "z.*";
+            }
+        }
+    }
+
+    typedef union-included {
+        type union {
+            type string {
+                pattern "a.*";
+            }
+            type string {
+                pattern "b.*";
+            }
+        }
+    }
+
+    identity identity_base;
+    identity IDONE {
+        base "identity_base";
+    }
+
+    identity IDTWO {
+        base "identity_base";
+    }
+
+    typedef identity_one {
+        type identityref {
+            base identity_base;
+        }
+    }
+
+    typedef referenced-leaf {
+        type leafref {
+            path "/container/target";
+            require-instance false;
+        }
+    }
+
+    grouping scoped-typedef {
+        typedef scoped-type {
+            type string {
+                pattern "a.*";
+            }
+        }
+
+        leaf scoped-leaf {
+            type scoped-type;
+        }
+    }
+
+    container container {
+        description
+            "A container";
+
+        leaf-list target {
+            type string;
+            description
+                "A target leaf for leafref checks";
+        }
+
+        leaf string {
+            type derived-string-type;
+        }
+
+        leaf integer {
+            type restricted-integer-type;
+        }
+
+        leaf stringdefault {
+            type derived-string-type;
+            default "aDefaultValue";
+        }
+
+        leaf integerdefault {
+            type restricted-integer-type;
+            default 10;
+        }
+
+        leaf new-string {
+            type new-string-type;
+        }
+
+        leaf remote-new-type {
+            type defn:remote-definition;
+        }
+
+        leaf session-dir {
+            type bgp-session-direction;
+        }
+
+        leaf remote-local-type {
+            type defn:remote-local-definition;
+        }
+
+        leaf inheritance {
+            type restricted-inherit {
+                pattern ".*k";
+            }
+        }
+
+        leaf int-inheritance {
+            type restricted-int-inherit {
+                range 2..5;
+            }
+        }
+
+        leaf-list stacked-union {
+            type child-union;
+        }
+
+        leaf include-of-include-definition {
+            type defn:hybrid-definition;
+        }
+
+        leaf identity-one-typedef {
+            type identity_one;
+        }
+
+        leaf union-with-union {
+            type union {
+                type union-included;
+                type string {
+                    pattern "q.*";
+                }
+            }
+        }
+
+        leaf reference {
+            type referenced-leaf;
+        }
+
+        uses scoped-typedef;
+    }
+}
diff --git a/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/remote.yang b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/remote.yang
new file mode 100644
index 0000000..409f04f
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/remote.yang
@@ -0,0 +1,33 @@
+module remote {
+    yang-version "1";
+    namespace "http://rob.sh/yang/test/typedef/remote";
+    prefix "remote";
+
+    import second-remote { prefix sr; }
+
+    organization "BugReports Inc";
+    contact "A bug reporter";
+
+    description
+        "A test module";
+    revision 2014-01-01 {
+        description "april-fools";
+        reference "fooled-you";
+    }
+
+    typedef remote-definition {
+        type string;
+    }
+
+    typedef remote-local-definition {
+        type local-definition;
+    }
+
+    typedef local-definition {
+        type string;
+    }
+
+    typedef hybrid-definition {
+        type sr:second-remote-definition;
+    }
+}
diff --git a/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/second-remote.yang b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/second-remote.yang
new file mode 100644
index 0000000..c106d89
--- /dev/null
+++ b/utils/yangutils/plugin/src/test/resources/typedefTranslator/with/second-remote.yang
@@ -0,0 +1,21 @@
+module second-remote {
+    yang-version "1";
+    namespace "http://rob.sh/yang/test/typedef/second-remote";
+    prefix "second-remote";
+
+    organization "BugReports Inc";
+    contact "A bug reporter";
+
+    description
+        "A test module";
+    revision 2014-01-01 {
+        description "april-fools";
+        reference "fooled-you";
+    }
+
+    typedef second-remote-definition {
+        type string {
+            pattern "z.*";
+        }
+    }
+}