Merge "[ONOS-7201][YRT]Defect: Post type is not same as get for union and for type empty is returning null."
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeHelper.java b/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeHelper.java
index 06770db..6380f62 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeHelper.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/RuntimeHelper.java
@@ -21,17 +21,20 @@
 import org.onosproject.yang.compiler.datamodel.YangSchemaNode;
 import org.onosproject.yang.compiler.linker.YangLinker;
 import org.onosproject.yang.compiler.linker.impl.YangLinkerManager;
+import org.onosproject.yang.compiler.tool.YangModuleExtendedInfo;
 import org.onosproject.yang.compiler.utils.io.YangPluginConfig;
 import org.onosproject.yang.model.YangModel;
 import org.onosproject.yang.model.YangModule;
-import org.onosproject.yang.compiler.tool.YangModuleExtendedInfo;
 import org.slf4j.Logger;
 
 import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
 
+import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.getDateInStringFormat;
 import static org.onosproject.yang.compiler.translator.tojava.JavaCodeGeneratorUtil.translate;
+import static org.onosproject.yang.compiler.utils.UtilConstants.AT;
 import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.getYangModel;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -65,20 +68,53 @@
     /**
      * Returns YANG node for given YANG model.
      *
-     * @param model YANG model
+     * @param model           YANG model
+     * @param yangSchemaStore YANG schema store
      * @return YANG nodes for given model
      */
-    public static Set<YangNode> getNodes(YangModel model) {
+    public static Set<YangNode> getNodes(
+            YangModel model,
+            ConcurrentMap<String, ConcurrentMap<String, YangSchemaNode>> yangSchemaStore) {
         Set<YangNode> nodes = new HashSet<>();
         for (YangModule info : model.getYangModules()) {
             YangModuleExtendedInfo ex = (YangModuleExtendedInfo) info;
-            nodes.add(ex.getSchema());
+            YangNode node = ex.getSchema();
+            if (ex.isInterJar()) {
+                String name = node.getName();
+                String date = getDateInStringFormat(node);
+                String revName = name;
+                if (date != null) {
+                    revName = name + AT + date;
+                }
+                node = (YangNode) yangSchemaStore.get(name).get(revName);
+            }
+            nodes.add((YangNode) node);
         }
         //Target linking.
         return addLinkerAndJavaInfo(nodes);
     }
 
     /**
+     * Returns self YANG node for given YANG model.
+     *
+     * @param model           YANG model
+     * @param yangSchemaStore YANG schema store
+     * @return YANG nodes for given model
+     */
+    public static Set<YangNode> getSelfNodes(
+            YangModel model,
+            ConcurrentMap<String, ConcurrentMap<String, YangSchemaNode>> yangSchemaStore) {
+        Set<YangNode> nodes = new HashSet<>();
+        for (YangModule info : model.getYangModules()) {
+            YangModuleExtendedInfo ex = (YangModuleExtendedInfo) info;
+            if (!ex.isInterJar()) {
+                nodes.add(ex.getSchema());
+            }
+        }
+        return nodes;
+    }
+
+    /**
      * Adds linker and translator info for each data model tree.
      *
      * @param nodes YANG node
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
index d846bee..ca75517 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/impl/DefaultYangModelRegistry.java
@@ -50,11 +50,11 @@
 import static java.util.Collections.unmodifiableSet;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.getDateInStringFormat;
 import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.getNodeIdFromSchemaId;
-import static org.onosproject.yang.compiler.tool.YangCompilerManager.processModuleId;
 import static org.onosproject.yang.compiler.utils.UtilConstants.REGEX;
 import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE;
 import static org.onosproject.yang.runtime.RuntimeHelper.getInterfaceClassName;
 import static org.onosproject.yang.runtime.RuntimeHelper.getNodes;
+import static org.onosproject.yang.runtime.RuntimeHelper.getSelfNodes;
 import static org.onosproject.yang.runtime.RuntimeHelper.getServiceName;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -65,13 +65,12 @@
         SingleInstanceNodeContext {
 
     private static final String AT = "@";
-    private final Logger log = getLogger(getClass());
     private static final String E_NEXIST = "node with {} namespace not found.";
     private static final String E_MEXIST =
             "Model with given modelId already exist";
     private static final String E_NULL = "Model must not be null";
     private static final String E_NOT_VAL = "Model id is invalid";
-
+    private final Logger log = getLogger(getClass());
     /*
      * Map for storing YANG schema nodes. Key will be the schema name of
      * module node defined in YANG file.
@@ -127,7 +126,7 @@
     public void registerModel(ModelRegistrationParam param) throws
             IllegalArgumentException {
         YangModel model = checkNotNull(param.getYangModel(), E_NULL);
-        Set<YangNode> curNodes = getNodes(model);
+        Set<YangNode> curNodes = getNodes(model, yangSchemaStore);
 
         //adding class info if added by application.
         AppModuleInfo info = null;
@@ -157,12 +156,7 @@
         //Register all the YANG nodes, excluding nodes from dependent jar.
         if (curNodes != null && !curNodes.isEmpty()) {
             for (YangNode node : curNodes) {
-                YangModuleId mid = processModuleId(node);
-                YangModuleExtendedInfo m =
-                        (YangModuleExtendedInfo) model.getYangModule(mid);
-                if (!m.isInterJar()) {
-                    registerModule(node);
-                }
+                registerModule(node);
             }
         }
 
@@ -179,12 +173,7 @@
         String name;
         //register all the nodes present in YANG model.
         name = getInterfaceClassName(node);
-        if (!regClassNameKeyStore.containsKey(name)) {
-            processApplicationContext(node, name);
-        } else {
-            log.info("class already registered with model registry " +
-                             "{}", name);
-        }
+        processApplicationContext(node, name);
     }
 
     @Override
@@ -193,15 +182,10 @@
             YangModel model = checkNotNull(param.getYangModel(), E_NULL);
             modelIdStore.remove(model.getYangModelId());
             //Unregister all yang files, excluding nodes from dependent jar.
-            Set<YangNode> curNodes = getNodes(model);
+            Set<YangNode> curNodes = getSelfNodes(model, yangSchemaStore);
             if (curNodes != null && !curNodes.isEmpty()) {
                 for (YangNode node : curNodes) {
-                    YangModuleId id = processModuleId(node);
-                    YangModuleExtendedInfo m =
-                            (YangModuleExtendedInfo) model.getYangModule(id);
-                    if (!m.isInterJar()) {
-                        processUnReg(getInterfaceClassName(node));
-                    }
+                    processUnReg(getInterfaceClassName(node));
                 }
             }
         }