Xml serializer change to remove resource Id for annotation

Change-Id: Iab6d3e52b849f6370d34c51aca6b33f6d6f943fb
diff --git a/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializer.java b/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializer.java
index 5872e51..67bc423 100644
--- a/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializer.java
+++ b/serializers/xml/src/main/java/org/onosproject/yang/serializers/xml/XmlSerializer.java
@@ -43,7 +43,6 @@
 
 import static org.onosproject.yang.runtime.helperutils.DefaultDataNodeWalker.walk;
 import static org.onosproject.yang.runtime.helperutils.SerializerHelper.initializeDataNode;
-import static org.onosproject.yang.runtime.helperutils.SerializerHelper.initializeResourceId;
 import static org.onosproject.yang.serializers.utils.SerializersUtil.addRootElementWithAnnotation;
 import static org.onosproject.yang.serializers.utils.SerializersUtil.convertRidToUri;
 import static org.onosproject.yang.serializers.utils.SerializersUtil.convertUriToRid;
@@ -117,7 +116,8 @@
         ResourceId.Builder builder;
 
         ResourceId resourceId = internal.resourceData().resourceId();
-        if (resourceId != null) {
+        if (resourceId != null && resourceId.nodeKeys() != null &&
+                !resourceId.nodeKeys().isEmpty()) {
             uriString = convertRidToUri(resourceId);
             try {
                 builder = resourceId.copyBuilder();
@@ -131,7 +131,7 @@
              * data node and checked in annotation map for annotations
              * associated with resource id.
              */
-            builder = initializeResourceId(context);
+            builder = ResourceId.builder();
         }
 
         // Store annotations in map with resource id as key
diff --git a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
index 678d88b..3c84ef8 100644
--- a/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
+++ b/serializers/xml/src/test/java/org/onosproject/yang/serializers/xml/XmlSerializerTest.java
@@ -21,16 +21,20 @@
 import org.junit.Test;
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.KeyLeaf;
 import org.onosproject.yang.model.LeafListKey;
 import org.onosproject.yang.model.LeafNode;
 import org.onosproject.yang.model.ListKey;
 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 org.onosproject.yang.runtime.AnnotatedNodeInfo;
 import org.onosproject.yang.runtime.Annotation;
 import org.onosproject.yang.runtime.CompositeData;
 import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
+import org.onosproject.yang.runtime.DefaultCompositeData;
 import org.onosproject.yang.runtime.DefaultCompositeStream;
 import org.onosproject.yang.runtime.YangSerializer;
 import org.onosproject.yang.runtime.YangSerializerContext;
@@ -153,8 +157,8 @@
         validateLeafDataNode(c1, "leaf_c1", "yrt:list", "l1_value1");
 
         // encode test
-        CompositeStream compositeStream = xmlSerializer.encode(compositeData,
-                                                               context);
+        CompositeStream compositeStream = xmlSerializer.encode(
+                getNewCompositeData(compositeData), context);
         InputStream inputStream = compositeStream.resourceData();
         assertThat(convertInputStreamToString(inputStream), is(parseXml(path)));
     }
@@ -180,8 +184,8 @@
                                  "value3");
 
         // encode test
-        CompositeStream compositeStream = xmlSerializer.encode(compositeData,
-                                                               context);
+        CompositeStream compositeStream = xmlSerializer.encode(
+                getNewCompositeData(compositeData), context);
         InputStream inputStream = compositeStream.resourceData();
         assertThat(convertInputStreamToString(inputStream), is(parseXml(path)));
     }
@@ -244,8 +248,8 @@
         assertThat(annotation.value(), is("replace"));
 
         // encode test
-        CompositeStream compositeStream = xmlSerializer.encode(compositeData,
-                                                               context);
+        CompositeStream compositeStream = xmlSerializer.encode(
+                getNewCompositeData(compositeData), context);
         InputStream inputStream = compositeStream.resourceData();
         assertThat(convertInputStreamToString(inputStream), is(parseXml(path)));
     }
@@ -422,4 +426,54 @@
         }
         return sb.toString();
     }
+
+    private CompositeData getNewCompositeData(CompositeData data) {
+        List<AnnotatedNodeInfo> annotatedNodeInfos = data
+                .annotatedNodesInfo();
+        CompositeData.Builder newCompBuilder = DefaultCompositeData.builder()
+                .resourceData(data.resourceData());
+
+        if (annotatedNodeInfos != null && !annotatedNodeInfos.isEmpty()) {
+            for (AnnotatedNodeInfo annotatedNodeInfo : annotatedNodeInfos) {
+                AnnotatedNodeInfo.Builder newAnnoBuilder = DefaultAnnotatedNodeInfo
+                        .builder();
+                ResourceId id = annotatedNodeInfo.resourceId();
+                ResourceId.Builder newIdBuilder = ResourceId.builder();
+                List<NodeKey> keys = id.nodeKeys();
+                if (keys != null && !keys.isEmpty()) {
+                    for (NodeKey key : keys) {
+                        if (!key.schemaId().name().equals("/")) {
+                            SchemaId schemaId = key.schemaId();
+                            if (key instanceof LeafListKey) {
+                                newIdBuilder.addLeafListBranchPoint(
+                                        schemaId.name(), schemaId.namespace(),
+                                        ((LeafListKey) key).value());
+                            } else if (key instanceof ListKey) {
+                                newIdBuilder.addBranchPointSchema(
+                                        schemaId.name(), schemaId.namespace());
+                                List<KeyLeaf> listKeys = ((ListKey) key)
+                                        .keyLeafs();
+                                for (KeyLeaf listKey : listKeys) {
+                                    SchemaId keySchemaId = listKey.leafSchema();
+                                    newIdBuilder = newIdBuilder.addKeyLeaf(
+                                            keySchemaId.name(), keySchemaId.namespace(),
+                                            listKey.leafValue());
+                                }
+                            } else {
+                                newIdBuilder.addBranchPointSchema(
+                                        schemaId.name(), schemaId.namespace());
+                            }
+                        }
+                    }
+                }
+                newAnnoBuilder.resourceId(newIdBuilder.build());
+                List<Annotation> annotations = annotatedNodeInfo.annotations();
+                for (Annotation annotation : annotations) {
+                    newAnnoBuilder.addAnnotation(annotation);
+                }
+                newCompBuilder.addAnnotatedNodeInfo(newAnnoBuilder.build());
+            }
+        }
+        return newCompBuilder.build();
+    }
 }