[ONOS-6960] Identity referred in sub-module of imported module.

Change-Id: I66bef7b5eec5859c94ecd0ced9bf139a32fa70f0
diff --git a/compiler/base/linker/src/main/java/org/onosproject/yang/compiler/linker/impl/YangResolutionInfoImpl.java b/compiler/base/linker/src/main/java/org/onosproject/yang/compiler/linker/impl/YangResolutionInfoImpl.java
index 220f19a..37f2542 100644
--- a/compiler/base/linker/src/main/java/org/onosproject/yang/compiler/linker/impl/YangResolutionInfoImpl.java
+++ b/compiler/base/linker/src/main/java/org/onosproject/yang/compiler/linker/impl/YangResolutionInfoImpl.java
@@ -1685,7 +1685,11 @@
          * typedef/grouping at the root level.
          */
         for (YangInclude yangInclude : curRefResolver.getIncludeList()) {
-            YangNode linkedNode = getLinkedNode(yangInclude.getIncludedNode());
+            YangNode inc = yangInclude.getIncludedNode();
+            YangNode linkedNode = getLinkedNode(inc);
+            if (linkedNode == null) {
+                linkedNode = getFromIncludeList(inc);
+            }
             if (linkedNode != null) {
                 return addUnResolvedRefToStack(linkedNode);
             }
@@ -1710,7 +1714,11 @@
              * for the referred typedef/grouping at the root level.
              */
             if (yangImport.getPrefixId().contentEquals(getRefPrefix())) {
-                YangNode linkedNode = getLinkedNode(yangImport.getImportedNode());
+                YangNode inc = yangImport.getImportedNode();
+                YangNode linkedNode = getLinkedNode(inc);
+                if (linkedNode == null) {
+                    linkedNode = getFromIncludeList(inc);
+                }
                 if (linkedNode != null) {
                     return addUnResolvedRefToStack(linkedNode);
                 }
@@ -1725,6 +1733,28 @@
         return false;
     }
 
+    /**
+     * Returns the referred node, by finding in the list of included nodes,
+     * inside the given node.
+     *
+     * @param node YANG node
+     * @return referred node inside included node
+     */
+    private YangNode getFromIncludeList(YangNode node) {
+        List<YangInclude> incList = ((YangReferenceResolver) node)
+                .getIncludeList();
+        YangNode refNode = null;
+        if (incList != null && !incList.isEmpty()) {
+            for (YangInclude inc : incList) {
+                refNode = getLinkedNode(inc.getIncludedNode());
+                if (refNode != null) {
+                    break;
+                }
+            }
+        }
+        return refNode;
+    }
+
     //Add unresolved constructs to stack.
     private boolean addUnResolvedRefToStack(YangNode linkedNode)
             throws DataModelException {
diff --git a/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/AugmentTranslatorTest.java b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/AugmentTranslatorTest.java
index e233495..225b0c5 100644
--- a/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/AugmentTranslatorTest.java
+++ b/compiler/plugin/maven/src/test/java/org/onosproject/yang/compiler/plugin/maven/AugmentTranslatorTest.java
@@ -593,4 +593,35 @@
         YangPluginConfig.compileCode(COMP);
         deleteDirectory(DIR);
     }
+
+    /**
+     * Checks identity linked in sub-module of imported module.
+     *
+     * @throws IOException            if any error occurs during IO on files
+     * @throws ParserException        if any error occurs during parsing
+     * @throws MojoExecutionException if any mojo operation fail
+     */
+    @Test
+    public void processSubmoduleId() throws IOException,
+            ParserException, MojoExecutionException {
+
+        deleteDirectory(DIR);
+        String dir = "src/test/resources/identityinsubmod";
+
+        Set<Path> paths = new HashSet<>();
+        for (String file : getYangFiles(dir)) {
+            paths.add(Paths.get(file));
+        }
+
+        utilManager.createYangFileInfoSet(paths);
+        utilManager.parseYangFileInfoSet();
+        utilManager.createYangNodeSet();
+        utilManager.resolveDependenciesUsingLinker();
+
+        YangPluginConfig yangPluginConfig = new YangPluginConfig();
+        yangPluginConfig.setCodeGenDir(DIR);
+        utilManager.translateToJava(yangPluginConfig);
+        YangPluginConfig.compileCode(COMP);
+        deleteDirectory(DIR);
+    }
 }
diff --git a/compiler/plugin/maven/src/test/resources/identityinsubmod/module1.yang b/compiler/plugin/maven/src/test/resources/identityinsubmod/module1.yang
new file mode 100644
index 0000000..8dbcd30
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityinsubmod/module1.yang
@@ -0,0 +1,16 @@
+module module1 {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:1";
+
+    prefix mod-a;
+
+    import module2 {
+        prefix mod2;
+    }
+
+    leaf name {
+        type identityref {
+            base mod2:BGP_ERROR_CODE;
+        }
+    }
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityinsubmod/module2.yang b/compiler/plugin/maven/src/test/resources/identityinsubmod/module2.yang
new file mode 100644
index 0000000..5a2d85d
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityinsubmod/module2.yang
@@ -0,0 +1,8 @@
+module module2 {
+
+    namespace "urn:ietf:params:xml:ns:aug:module:2";
+
+    prefix mod-b;
+
+    include submodule2;
+}
diff --git a/compiler/plugin/maven/src/test/resources/identityinsubmod/submodule.yang b/compiler/plugin/maven/src/test/resources/identityinsubmod/submodule.yang
new file mode 100644
index 0000000..b6b8b10
--- /dev/null
+++ b/compiler/plugin/maven/src/test/resources/identityinsubmod/submodule.yang
@@ -0,0 +1,9 @@
+submodule submodule2 {
+
+    belongs-to module2 {
+        prefix mod2;
+    }
+
+    identity BGP_ERROR_CODE {
+    }
+}