[ONOS-7304] YANG Runtime: YANG Push test for anydata

Change-Id: I88661643c813b3bb4779fec227d10df33b60b268
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
index 35244c0..1347e76 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangNode.java
@@ -481,33 +481,51 @@
         if (clonedNode instanceof YangAugmentableNode) {
             ((YangAugmentableNode) clonedNode).cloneAugmentInfo();
             if (isAnyData) {
-                // clone
-                List<YangAugment> clonedYangAugmentedInfo = new ArrayList<>();
-                List<YangAugment> yangAugmentedInfo =
-                        ((YangAugmentableNode) this).getAugmentedInfoList();
-                if (yangAugmentedInfo != null && !yangAugmentedInfo.isEmpty()) {
-                    for (YangAugment info : yangAugmentedInfo) {
-                        try {
-                            YangAugment augment = (YangAugment) info.clone(
-                                    null, false, true);
-                            cloneSubTree(info, augment, null,
-                                         false, null);
-                            augment.setParent(info.getParent());
-                            augment.setAugmentedNode(clonedNode);
-                            clonedYangAugmentedInfo.add(augment);
-                        } catch (DataModelException e) {
-                            throw new IllegalArgumentException(e);
-                        }
-                    }
-                }
-                ((YangAugmentableNode) clonedNode).getAugmentedInfoList()
-                        .addAll(clonedYangAugmentedInfo);
+                cloneAugmentedInfo(clonedNode);
             }
         }
         return clonedNode;
     }
 
     /**
+     * Clones the augmented subtree information and update the same in the
+     * current cloned augmentable node.
+     *
+     * @param clonedNode current cloned node who's augmented info needs to be
+     *                   cloned
+     * @throws CloneNotSupportedException clone is not supported by the referred
+     *                                    node
+     */
+    private void cloneAugmentedInfo(YangNode clonedNode) throws CloneNotSupportedException {
+        // clone
+        List<YangAugment> clonedYangAugmentedInfo = new ArrayList<>();
+        List<YangAugment> yangAugmentedInfo =
+                ((YangAugmentableNode) this).getAugmentedInfoList();
+        if (yangAugmentedInfo != null && !yangAugmentedInfo.isEmpty()) {
+            for (YangAugment info : yangAugmentedInfo) {
+                try {
+                    // clone the top level augment node
+                    YangAugment augment = (YangAugment) info.clone(
+                            null, false, true);
+                    // clone the subtree of top level node and updating the
+                    // same in clonned top level node
+                    cloneSubTree(info, augment, null,
+                                 false, null);
+                    augment.setParent(info.getParent());
+                    augment.setAugmentedNode(clonedNode);
+                    // adding the cloned augmented subtree in augmented info
+                    // list
+                    clonedYangAugmentedInfo.add(augment);
+                } catch (DataModelException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            }
+        }
+        ((YangAugmentableNode) clonedNode).getAugmentedInfoList()
+                .addAll(clonedYangAugmentedInfo);
+    }
+
+    /**
      * Clones the subtree from the specified source node to the mentioned target
      * node. The source and target root node cloning is carried out by the
      * caller.
@@ -547,8 +565,9 @@
         TraversalType curTraversal;
         YangNode clonedTreeCurNode = dstRootNode;
         YangNode newNode = null;
-
+        boolean isAnydata = false;
         if (childToClone != null) {
+            isAnydata = true;
             nextNodeToClone = childToClone;
         } else {
             nextNodeToClone = srcRootNode.getChild();
@@ -580,13 +599,8 @@
                 }
 
                 if (curTraversal != PARENT) {
-                    if (childToClone != null) {
-                        newNode = nextNodeToClone.clone(yangUses, isDeviation,
-                                                        true);
-                    } else {
-                        newNode = nextNodeToClone.clone(yangUses, isDeviation,
-                                                        false);
-                    }
+                    newNode = nextNodeToClone.clone(yangUses, isDeviation,
+                                                    isAnydata);
                     if (newNode instanceof YangUses) {
                         ((YangUses) newNode).setCloned(true);
                     }
@@ -632,7 +646,7 @@
                      */
                     nextNodeToClone = nextNodeToClone.getChild();
                 } else if (nextNodeToClone.getNextSibling() != null) {
-                    if (childToClone != null &&
+                    if (isAnydata &&
                             nextNodeToClone.getNextSibling().getParent() == srcRootNode) {
                         curTraversal = PARENT;
                         nextNodeToClone = nextNodeToClone.getParent();
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
index b00e84d..2640a8b 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java
@@ -117,8 +117,6 @@
 public final class DataModelUtils {
     public static final String TRUE = "true";
     public static final String FALSE = "false";
-    public static final String TYPEDEF = "Typedef";
-    public static final String IDENTITY = "Identity";
     public static final String FMT_NOT_EXIST =
             "Requested %s is not child in %s.";
     public static final String E_ID = "Schema id should not be null.";
@@ -127,11 +125,6 @@
                     " avoid the %s extension in the name.";
     public static final String E_INVALID = "This call is not valid for " +
             "YANG leaf/leaf-list";
-    public static final String DOT_REGEX = "\\.";
-    public static final String QNAME_PRE = "org.onosproject.yang.gen";
-    public static final String DEFAULT = "Default";
-    public static final String INVAL_ANYDATA =
-            "Requested %s is not valid node for anydata.";
     private static final String SLASH = File.separator;
     private static final String E_DATATYPE = "Data type not supported.";
     private static final String DATE_FORMAT = "yyyy-MM-dd";
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/JavaCodeGeneratorUtil.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/JavaCodeGeneratorUtil.java
index f6f8e37..194ef46 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/JavaCodeGeneratorUtil.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/JavaCodeGeneratorUtil.java
@@ -148,6 +148,8 @@
                             if (codeGenNode instanceof YangLeavesHolder ||
                                     codeGenNode instanceof SchemaDataNode) {
                                 codeGenNode.setParentContext();
+                                // updating the parent context and Ysn
+                                // context info map for augmented node
                                 if (!translateComplete && codeGenNode
                                         instanceof YangAugmentableNode) {
                                     List<YangAugment> augList =
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 380c9cf..3d6f507 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
@@ -169,20 +169,31 @@
     }
 
     @Override
-    public void registerAnydataSchema(ModelObjectId ac, ModelObjectId cc) throws
+    public void registerAnydataSchema(ModelObjectId aid, ModelObjectId cid) throws
             IllegalArgumentException {
-        ModIdToRscIdConverter idConverter = new ModIdToRscIdConverter(this);
-        YangSchemaNode anySchema = ((YangSchemaNode) idConverter.fetchResourceId(ac).appInfo());
-        if (anySchema != null && anySchema.getYangSchemaNodeType() == YANG_ANYDATA_NODE) {
-            YangSchemaNode cSchema = ((YangSchemaNode) idConverter.fetchResourceId(cc).appInfo());
-            if (cSchema != null) {
-                YangSchemaNode clonedNode = anySchema.addSchema(cSchema);
-                updateTreeContext(clonedNode, null, false, false);
+        YangSchemaNode anySchema = null;
+        try {
+            ModIdToRscIdConverter conv = new ModIdToRscIdConverter(this);
+            anySchema = ((YangSchemaNode) conv.fetchResourceId(aid).appInfo());
+            if (anySchema != null &&
+                    anySchema.getYangSchemaNodeType() == YANG_ANYDATA_NODE) {
+                YangSchemaNode cSchema = ((YangSchemaNode) conv
+                        .fetchResourceId(cid).appInfo());
+                if (cSchema != null) {
+                    YangSchemaNode clonedNode = anySchema.addSchema(cSchema);
+                    updateTreeContext(clonedNode, null, false, false);
+                } else {
+                    throw new IllegalArgumentException(errorMsg(FMT_INV, cid));
+                }
             } else {
-                throw new IllegalArgumentException(errorMsg(FMT_INV, cc));
+                throw new IllegalArgumentException(errorMsg(FMT_INV, aid));
             }
-        } else {
-            throw new IllegalArgumentException(errorMsg(FMT_INV, ac));
+        } catch (ModelConverterException e) {
+            ModelObjectId id = cid;
+            if (anySchema == null) {
+                id = aid;
+            }
+            throw new IllegalArgumentException(errorMsg(FMT_INV, id));
         }
     }
 
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobYangPushAnydataTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobYangPushAnydataTest.java
new file mode 100644
index 0000000..fb7af4f
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobYangPushAnydataTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.yang.runtime.impl;
+
+import org.junit.Test;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.DefaultInterfacesState;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.interfacesstate.YangAutoPrefixInterface;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.DefaultYangPatch;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.DefaultEdit;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.Edit;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.edit.DefaultValue;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.DefaultPushChangeUpdate;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.pushchangeupdate.DefaultDatastoreChanges;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.DefaultResourceData;
+import org.onosproject.yang.model.InnerModelObject;
+import org.onosproject.yang.model.ModelObject;
+import org.onosproject.yang.model.ModelObjectData;
+import org.onosproject.yang.model.ModelObjectId;
+import org.onosproject.yang.model.ResourceData;
+
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.onosproject.yang.runtime.SerializerHelper.initializeDataNode;
+import static org.onosproject.yang.runtime.impl.serializerhelper.YangPushPatchAnydataTest.yangPushDataTree;
+
+/**
+ * Tests the YANG object building for the YANG data nodes based on the non
+ * schema augmented nodes.
+ */
+public class YobYangPushAnydataTest {
+    TestYangSerializerContext context = new TestYangSerializerContext();
+
+    @Test
+    public void anydataTest() {
+        DataNode.Builder dBlr = initializeDataNode(context);
+        ModelObjectId id = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .build();
+        ModelObjectId id2 = new ModelObjectId.Builder()
+                .addChild(DefaultYangPatch.class).build();
+
+        ModelObjectId id1 = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .addChild(DefaultYangPatch.class)
+                .addChild(DefaultEdit.class, null)
+                .addChild(DefaultValue.class)
+                .build();
+        ModelObjectId id3 = new ModelObjectId.Builder()
+                .addChild(DefaultInterfacesState.class)
+                .build();
+        context.getRegistry().registerAnydataSchema(id, id2);
+        context.getRegistry().registerAnydataSchema(id1, id3);
+        DataNode dataNode = yangPushDataTree(dBlr);
+
+        ResourceData data = DefaultResourceData.builder()
+                .addDataNode(dataNode).build();
+        DefaultYobBuilder builder = new DefaultYobBuilder(context.getRegistry());
+        ModelObjectData modelObjectData = builder.getYangObject(data);
+        List<ModelObject> modelObjectList = modelObjectData.modelObjects();
+        ModelObject modelObject = modelObjectList.get(0);
+        DefaultPushChangeUpdate update = ((DefaultPushChangeUpdate) modelObject);
+        assertThat(update.subscriptionId().toString(), is("89"));
+        DefaultDatastoreChanges changes = ((DefaultDatastoreChanges) update
+                .datastoreChanges());
+
+        List<InnerModelObject> patch = changes.anydata(DefaultYangPatch.class);
+        assertThat(((DefaultYangPatch) patch.get(0)).patchId().toString(), is("1"));
+
+        Edit edit = ((DefaultYangPatch) patch.get(0)).edit().get(0);
+        assertThat(edit.editId().toString(), is("edit1"));
+        assertThat(edit.operation().toString(), is("merge"));
+        assertThat(edit.target().toString(), is("/ietf-interfaces/interfaces-state"));
+
+        DefaultValue val = (DefaultValue) edit.value();
+        List<InnerModelObject> interState = val.anydata(DefaultInterfacesState.class);
+        YangAutoPrefixInterface interfaces =
+                ((DefaultInterfacesState) interState.get(0)).yangAutoPrefixInterface().get(0);
+        assertThat(interfaces.name(), is("eth0"));
+        assertThat(interfaces.operStatus().toString(), is("down"));
+    }
+}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/YtbYangPushAnydataTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YtbYangPushAnydataTest.java
new file mode 100644
index 0000000..f8246bf
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YtbYangPushAnydataTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.yang.runtime.impl;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onosproject.yang.gen.v1.yrtietfeventnotifications.rev20161027.yrtietfeventnotifications.SubscriptionId;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.DefaultInterfacesState;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.interfacesstate.DefaultYangAutoPrefixInterface;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.interfacesstate.YangAutoPrefixInterface;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.TargetResourceOffset;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.DefaultYangPatch;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.DefaultEdit;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.edit.DefaultValue;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.DefaultPushChangeUpdate;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.pushchangeupdate.DefaultDatastoreChanges;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.DefaultModelObjectData.Builder;
+import org.onosproject.yang.model.ModelObjectId;
+import org.onosproject.yang.model.NodeKey;
+import org.onosproject.yang.model.ResourceData;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.SchemaId;
+
+import java.util.List;
+
+import static org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.interfacesstate.yangautoprefixinterface.OperStatusEnum.DOWN;
+import static org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.edit.OperationEnum.MERGE;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.processSchemaRegistry;
+import static org.onosproject.yang.runtime.impl.MockYangSchemaNodeProvider.registry;
+import static org.onosproject.yang.runtime.impl.serializerhelper.YangPushPatchAnydataTest.validatePushDataNodeTree;
+
+/**
+ * Unit test cases for resource id conversion from model object id.
+ */
+public class YtbYangPushAnydataTest {
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+    private ResourceData rscData;
+    private DefaultDataTreeBuilder treeBuilder;
+    private ResourceId id;
+    private List<NodeKey> keys;
+    private SchemaId sid;
+    private ModelObjectId mid;
+    private Builder data;
+    DefaultYangModelRegistry reg;
+
+    /**
+     * Prior setup for each UT.
+     */
+    @Before
+    public void setUp() {
+        processSchemaRegistry();
+        reg = registry();
+        treeBuilder = new DefaultDataTreeBuilder(reg);
+    }
+
+
+    /**
+     * Processes anydata with augmented node as child.
+     */
+    @Test
+    public void processAnydataTest() {
+        ModelObjectId id = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .build();
+        ModelObjectId id2 = new ModelObjectId.Builder()
+                .addChild(DefaultYangPatch.class).build();
+
+        ModelObjectId id1 = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .addChild(DefaultYangPatch.class)
+                .addChild(DefaultEdit.class, null)
+                .addChild(DefaultValue.class)
+                .build();
+        ModelObjectId id3 = new ModelObjectId.Builder()
+                .addChild(DefaultInterfacesState.class)
+                .build();
+        reg.registerAnydataSchema(id, id2);
+        reg.registerAnydataSchema(id1, id3);
+
+        DefaultPushChangeUpdate update = new DefaultPushChangeUpdate();
+        long val = 89;
+        update.subscriptionId(new SubscriptionId(val));
+
+        DefaultDatastoreChanges changes = new DefaultDatastoreChanges();
+        DefaultYangPatch patch = new DefaultYangPatch();
+        patch.patchId("1");
+        DefaultEdit edit = new DefaultEdit();
+        edit.editId("edit1");
+        edit.operation(MERGE);
+        edit.target(new TargetResourceOffset("/ietf-interfaces/interfaces-state"));
+
+        DefaultInterfacesState state = new DefaultInterfacesState();
+        YangAutoPrefixInterface inter = new DefaultYangAutoPrefixInterface();
+        inter.name("eth0");
+        inter.operStatus(DOWN);
+        state.addToYangAutoPrefixInterface(inter);
+
+        DefaultValue value = new DefaultValue();
+        value.addAnydata(state);
+        edit.value(value);
+        patch.addToEdit(edit);
+        changes.addAnydata(patch);
+        update.datastoreChanges(changes);
+
+        data = new Builder();
+        data.addModelObject(update);
+        rscData = treeBuilder.getResourceData(data.build());
+
+        List<DataNode> nodes = rscData.dataNodes();
+        DataNode n = nodes.get(0);
+        validatePushDataNodeTree(n, true);
+    }
+}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AnydataNegativeScenarioTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AnydataNegativeScenarioTest.java
index 644cabc..b6646dc 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AnydataNegativeScenarioTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/AnydataNegativeScenarioTest.java
@@ -16,12 +16,22 @@
 
 package org.onosproject.yang.runtime.impl.serializerhelper;
 
-import org.onosproject.yang.gen.v11.listanydata.rev20160624.ListAnydataOpParam;
+import org.junit.Test;
+import org.onosproject.yang.gen.v1.check.check.DefaultList52;
+import org.onosproject.yang.gen.v1.yrtietfnetwork.rev20151208.yrtietfnetwork.DefaultNetworks;
+import org.onosproject.yang.gen.v1.yrtietfnetwork.rev20151208.yrtietfnetwork.networks.DefaultNetwork;
+import org.onosproject.yang.gen.v1.yrtietfnetwork.rev20151208.yrtietfnetwork.networks.network.DefaultNode;
+import org.onosproject.yang.gen.v11.anytest.rev20160624.AnyTestOpParam;
+import org.onosproject.yang.gen.v11.anytest.rev20160624.anytest.DefaultC1;
+import org.onosproject.yang.gen.v11.anytest.rev20160624.anytest.c1.DefaultMydata2;
+import org.onosproject.yang.gen.v11.listanydata.rev20160624.listanydata.DefaultL1;
+import org.onosproject.yang.gen.v11.listanydata.rev20160624.listanydata.l1.DefaultMydata;
 import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.ModelObjectId;
 import org.onosproject.yang.runtime.impl.TestYangSerializerContext;
 
 import static org.junit.Assert.assertEquals;
-import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.INVAL_ANYDATA;
+import static org.onosproject.yang.runtime.impl.UtilsConstants.FMT_INV;
 
 /**
  * Tests the serializer helper methods.
@@ -39,16 +49,22 @@
      * Test anydata add to data node negative test scenario when given
      * referenced node is not of type anydata.
      */
-//    @Test
+    @Test
     public void addToDataTest() {
         boolean isExpOccurred = false;
-//        context.getContext();
-//        try {
-//            context.getRegistry().registerAnydataSchema(Node.class, Node.class);
-//        } catch (IllegalArgumentException e) {
-//            isExpOccurred = true;
-//            assertEquals(e.getMessage(), String.format(FMT_INV, Node.class));
-//        }
+        context.getContext();
+        ModelObjectId id1 = null;
+        try {
+            id1 = new ModelObjectId.Builder()
+                    .addChild(DefaultNetworks.class)
+                    .addChild(DefaultNetwork.class, null)
+                    .addChild(DefaultNode.class, null)
+                    .build();
+            context.getRegistry().registerAnydataSchema(id1, id1);
+        } catch (IllegalArgumentException e) {
+            isExpOccurred = true;
+            assertEquals(e.getMessage(), String.format(FMT_INV, id1));
+        }
         assertEquals(isExpOccurred, true);
     }
 
@@ -56,17 +72,23 @@
      * Test anydata add to data node negative test scenario when given
      * referenced node module is not registered.
      */
-//    @Test
+    @Test
     public void addToData2Test() {
         boolean isExpOccurred = false;
         context.getContext();
+        ModelObjectId id1 = null;
         try {
-//            context.getRegistry().registerAnydataSchema(Mydata.class,
-//                                                        ListAnydataOpParam.class);
+            ModelObjectId id = new ModelObjectId.Builder()
+                    .addChild(DefaultC1.class).addChild(DefaultMydata2.class)
+                    .build();
+            id1 = new ModelObjectId.Builder()
+                    .addChild(AnyTestOpParam.class)
+                    .build();
+            context.getRegistry().registerAnydataSchema(id, id1);
         } catch (IllegalArgumentException e) {
             isExpOccurred = true;
             assertEquals(e.getMessage(), String.format(
-                    INVAL_ANYDATA, ListAnydataOpParam.class));
+                    FMT_INV, id1));
         }
         assertEquals(isExpOccurred, true);
     }
@@ -76,18 +98,24 @@
      * Test anydata add to data node negative test scenario when given
      * referenced node is not of type list/container.
      */
-//    @Test
+    @Test
     public void addToData3Test() {
         boolean isExpOccurred = false;
         context.getContext();
-//        try {
-//            context.getRegistry().registerAnydataSchema(Mydata.class,
-//                                                        ListAnydata.class);
-//        } catch (IllegalArgumentException e) {
-//            isExpOccurred = true;
-//            assertEquals(e.getMessage(), String.format(
-//                    INVAL_ANYDATA, ListAnydata.class));
-//        }
+        ModelObjectId id = null;
+        try {
+            id = new ModelObjectId.Builder()
+                    .addChild(AnyTestOpParam.class)
+                    .build();
+            ModelObjectId id1 = new ModelObjectId.Builder()
+                    .addChild(DefaultL1.class, null)
+                    .addChild(DefaultMydata.class)
+                    .build();
+            context.getRegistry().registerAnydataSchema(id1, id);
+        } catch (IllegalArgumentException e) {
+            isExpOccurred = true;
+            assertEquals(e.getMessage(), String.format(FMT_INV, id));
+        }
         assertEquals(isExpOccurred, true);
     }
 
@@ -95,18 +123,50 @@
      * Test anydata add to data node negative test scenario when given
      * referenced node is not of type list/container.
      */
-//    @Test
+    @Test
     public void addToData4Test() {
         boolean isExpOccurred = false;
-//        context.getContext();
-//        try {
-//            context.getRegistry().registerAnydataSchema(Mydata.class,
-//                                                        List52Keys.class);
-//        } catch (IllegalArgumentException e) {
-//            isExpOccurred = true;
-//            assertEquals(e.getMessage(), String.format(
-//                    INVAL_ANYDATA, List52Keys.class.getCanonicalName()));
-//        }
+        context.getContext();
+        ModelObjectId id = null;
+        try {
+            id = new ModelObjectId.Builder()
+                    .addChild(DefaultList52.class)
+                    .build();
+            ModelObjectId id1 = new ModelObjectId.Builder()
+                    .addChild(DefaultL1.class, null)
+                    .addChild(DefaultMydata.class)
+                    .build();
+            context.getRegistry().registerAnydataSchema(id1, id);
+        } catch (IllegalArgumentException e) {
+            isExpOccurred = true;
+            assertEquals(e.getMessage(), String.format(
+                    FMT_INV, id));
+        }
+        assertEquals(isExpOccurred, true);
+    }
+
+    /**
+     * Test anydata add to data node negative test scenario when given
+     * referenced identifer is invalid for anydata.
+     */
+    @Test
+    public void addToData5Test() {
+        boolean isExpOccurred = false;
+        context.getContext();
+        ModelObjectId id = null;
+        try {
+            id = new ModelObjectId.Builder()
+                    .addChild(DefaultC1.class).addChild(DefaultMydata.class)
+                    .build();
+            ModelObjectId id1 = new ModelObjectId.Builder()
+                    .addChild(AnyTestOpParam.class)
+                    .build();
+            context.getRegistry().registerAnydataSchema(id, id1);
+        } catch (IllegalArgumentException e) {
+            isExpOccurred = true;
+            assertEquals(e.getMessage(), String.format(
+                    FMT_INV, id));
+        }
         assertEquals(isExpOccurred, true);
     }
 }
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/YangPushPatchAnydataTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/YangPushPatchAnydataTest.java
new file mode 100644
index 0000000..b0aa212
--- /dev/null
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/serializerhelper/YangPushPatchAnydataTest.java
@@ -0,0 +1,251 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.yang.runtime.impl.serializerhelper;
+
+import org.junit.Test;
+import org.onosproject.yang.gen.v1.yrtietfinterfaces.rev20140508.yrtietfinterfaces.DefaultInterfacesState;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.DefaultYangPatch;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.DefaultEdit;
+import org.onosproject.yang.gen.v11.yrtietfyangpatch.rev20170222.yrtietfyangpatch.yangpatch.yangpatch.edit.DefaultValue;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.DefaultPushChangeUpdate;
+import org.onosproject.yang.gen.v11.yrtietfyangpush.rev20161028.yrtietfyangpush.pushchangeupdate.DefaultDatastoreChanges;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.ModelObjectId;
+import org.onosproject.yang.runtime.impl.TestYangSerializerContext;
+
+import java.util.Iterator;
+
+import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE;
+import static org.onosproject.yang.runtime.SerializerHelper.addDataNode;
+import static org.onosproject.yang.runtime.SerializerHelper.exitDataNode;
+import static org.onosproject.yang.runtime.SerializerHelper.initializeDataNode;
+import static org.onosproject.yang.runtime.impl.TestUtils.INTERF_NS;
+import static org.onosproject.yang.runtime.impl.TestUtils.PATCH_NS;
+import static org.onosproject.yang.runtime.impl.TestUtils.PUSH_NS;
+import static org.onosproject.yang.runtime.impl.TestUtils.validateDataNode;
+import static org.onosproject.yang.runtime.impl.TestUtils.walkINTree;
+
+/**
+ * Tests the serializer helper methods.
+ */
+public class YangPushPatchAnydataTest {
+
+
+    private static final String[] EXPECTED = {
+            "Entry Node is /.",
+            "Entry Node is push-change-update.",
+            "Entry Node is subscription-id.",
+            "Exit Node is subscription-id.",
+            "Entry Node is datastore-changes.",
+            "Entry Node is yang-patch.",
+            "Entry Node is patch-id.",
+            "Exit Node is patch-id.",
+            "Entry Node is edit.",
+            "Entry Node is edit-id.",
+            "Exit Node is edit-id.",
+            "Entry Node is operation.",
+            "Exit Node is operation.",
+            "Entry Node is target.",
+            "Exit Node is target.",
+            "Entry Node is value.",
+            "Entry Node is interfaces-state.",
+            "Entry Node is interface.",
+            "Entry Node is name.",
+            "Exit Node is name.",
+            "Entry Node is oper-status.",
+            "Exit Node is oper-status.",
+            "Exit Node is interface.",
+            "Exit Node is interfaces-state.",
+            "Exit Node is value.",
+            "Exit Node is edit.",
+            "Exit Node is yang-patch.",
+            "Exit Node is datastore-changes.",
+            "Exit Node is push-change-update.",
+            "Exit Node is /."
+    };
+
+    /**
+     * Test anydata add to data node builder.
+     */
+    @Test
+    public void yangPushDataNodeTest() {
+
+        TestYangSerializerContext context = new TestYangSerializerContext();
+        DataNode.Builder dBlr = initializeDataNode(context);
+        ModelObjectId id = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .build();
+        ModelObjectId id2 = new ModelObjectId.Builder()
+                .addChild(DefaultYangPatch.class).build();
+
+        ModelObjectId id1 = new ModelObjectId.Builder()
+                .addChild(DefaultPushChangeUpdate.class)
+                .addChild(DefaultDatastoreChanges.class)
+                .addChild(DefaultYangPatch.class)
+                .addChild(DefaultEdit.class, null)
+                .addChild(DefaultValue.class)
+                .build();
+        ModelObjectId id3 = new ModelObjectId.Builder()
+                .addChild(DefaultInterfacesState.class)
+                .build();
+        context.getRegistry().registerAnydataSchema(id, id2);
+        context.getRegistry().registerAnydataSchema(id1, id3);
+        DataNode n = yangPushDataTree(dBlr);
+        validatePushDataNodeTree(n, false);
+        walkINTree(n, EXPECTED);
+    }
+
+    /**
+     * Creates the data node tree for actn anydata test case.
+     *
+     * @param dBlr data node builder
+     */
+    public static DataNode yangPushDataTree(DataNode.Builder dBlr) {
+
+        String value = null;
+        // Adding container configuration-schedules
+        dBlr = addDataNode(dBlr, "push-change-update", PUSH_NS, value, null);
+        value = "89";
+        dBlr = addDataNode(dBlr, "subscription-id", null, value, null);
+        dBlr = exitDataNode(dBlr);
+        // Adding anydata container
+        value = null;
+        dBlr = addDataNode(dBlr, "datastore-changes", null, value, null);
+
+        dBlr = addDataNode(dBlr, "yang-patch", PATCH_NS, value, null);
+        value = "1";
+        dBlr = addDataNode(dBlr, "patch-id", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        value = null;
+        dBlr = addDataNode(dBlr, "edit", null, value, null);
+        value = "edit1";
+        dBlr = addDataNode(dBlr, "edit-id", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        value = "merge";
+        dBlr = addDataNode(dBlr, "operation", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        value = "/ietf-interfaces/interfaces-state";
+        dBlr = addDataNode(dBlr, "target", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        // Adding anydata container
+        value = null;
+        dBlr = addDataNode(dBlr, "value", null, value, null);
+        dBlr = addDataNode(dBlr, "interfaces-state", INTERF_NS, value, null);
+        dBlr = addDataNode(dBlr, "interface", null, value, null);
+        value = "eth0";
+        dBlr = addDataNode(dBlr, "name", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        value = "down";
+        dBlr = addDataNode(dBlr, "oper-status", null, value, null);
+        dBlr = exitDataNode(dBlr);
+
+        dBlr = exitDataNode(dBlr); // interface
+        dBlr = exitDataNode(dBlr); // interfaces-state
+        dBlr = exitDataNode(dBlr); // value
+        dBlr = exitDataNode(dBlr); // edit
+        dBlr = exitDataNode(dBlr); // yang-patch
+        dBlr = exitDataNode(dBlr); // datastore-changes
+        dBlr = exitDataNode(dBlr); // push-change-update
+
+        return dBlr.build();
+    }
+
+    /**
+     * Validates the given data node sub-tree.
+     *
+     * @param node  data node which needs to be validated
+     * @param isYtb is it YTB call as tree comes from YTB will not be having
+     *              "/" as parent node
+     */
+    public static void validatePushDataNodeTree(DataNode node, boolean isYtb) {
+        // Validating the data node.
+        DataNode n = node;
+        Iterator<DataNode> it;
+        if (!isYtb) {
+            validateDataNode(n, "/", null,
+                             SINGLE_INSTANCE_NODE, true, null);
+            it = ((InnerNode) n).childNodes().values().iterator();
+            n = it.next();
+        }
+        validateDataNode(n, "push-change-update", PUSH_NS,
+                         SINGLE_INSTANCE_NODE, true, null);
+        it = ((InnerNode) n).childNodes().values().iterator();
+        n = it.next();
+        validateDataNode(n, "subscription-id", PUSH_NS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "89");
+        n = it.next();
+        validateDataNode(n, "datastore-changes", PUSH_NS,
+                         SINGLE_INSTANCE_NODE, true, null);
+
+        Iterator<DataNode> it1 = ((InnerNode) n).childNodes().values()
+                .iterator();
+        n = it1.next();
+        validateDataNode(n, "yang-patch", PATCH_NS, SINGLE_INSTANCE_NODE,
+                         true, null);
+        it1 = ((InnerNode) n).childNodes().values().iterator();
+        n = it1.next();
+        validateDataNode(n, "patch-id", PATCH_NS, SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "1");
+
+        n = it1.next();
+        validateDataNode(n, "edit", PATCH_NS, MULTI_INSTANCE_NODE,
+                         true, null);
+
+        Iterator<DataNode> it3 = ((InnerNode) n).childNodes().values()
+                .iterator();
+        n = it3.next();
+        validateDataNode(n, "edit-id", PATCH_NS, SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "edit1");
+        n = it3.next();
+        validateDataNode(n, "operation", PATCH_NS, SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "merge");
+        n = it3.next();
+        validateDataNode(n, "target", PATCH_NS,
+                         SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "/ietf-interfaces/interfaces-state");
+
+        n = it3.next();
+        validateDataNode(n, "value", PATCH_NS, SINGLE_INSTANCE_NODE,
+                         true, null);
+
+        it3 = ((InnerNode) n).childNodes().values().iterator();
+        n = it3.next();
+        validateDataNode(n, "interfaces-state", INTERF_NS, SINGLE_INSTANCE_NODE,
+                         true, null);
+        it3 = ((InnerNode) n).childNodes().values().iterator();
+        n = it3.next();
+        validateDataNode(n, "interface", INTERF_NS, MULTI_INSTANCE_NODE,
+                         true, null);
+        it3 = ((InnerNode) n).childNodes().values().iterator();
+        n = it3.next();
+        validateDataNode(n, "name", INTERF_NS, SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "eth0");
+        n = it3.next();
+        validateDataNode(n, "oper-status", INTERF_NS, SINGLE_INSTANCE_LEAF_VALUE_NODE,
+                         false, "down");
+    }
+}