Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodecTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import java.util.List;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
18 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
19 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
20 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
21 import org.opendaylight.controller.cluster.datastore.util.TestModel;
22 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28
29 public class NormalizedNodeToNodeCodecTest {
30   private SchemaContext schemaContext;
31
32   @Before
33   public void setUp() {
34     schemaContext = TestModel.createTestContext();
35     assertNotNull("Schema context must not be null.", schemaContext);
36   }
37
38   private static YangInstanceIdentifier instanceIdentifierFromString(String s) {
39       return PathUtils.toYangInstanceIdentifier(s);
40   }
41
42   @Test
43   public void testNormalizeNodeAttributesToProtoBuffNode() {
44     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
45     String id =
46         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
47             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
48             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]"
49             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
50
51     NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
52     new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
53         PathUtils.toString(YangInstanceIdentifier.EMPTY), documentOne);
54
55     // Validate the value of id can be retrieved from the normalized node
56     NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
57     assertNotNull(output);
58
59
60     NormalizedNodeToNodeCodec codec =
61         new NormalizedNodeToNodeCodec(schemaContext);
62     long start = System.currentTimeMillis();
63     Container container =
64         codec.encode(output);
65     long end = System.currentTimeMillis();
66
67     System.out.println("Timetaken to encode :"+(end-start));
68
69     assertNotNull(container);
70
71     // Decode the normalized node from the ProtocolBuffer form
72     // first get the node representation of normalized node
73     final Node node = container.getNormalizedNode();
74
75     start = System.currentTimeMillis();
76     NormalizedNode<?, ?> normalizedNode =
77         codec.decode(node);
78     end = System.currentTimeMillis();
79
80     System.out.println("Timetaken to decode :"+(end-start));
81
82     assertEquals(normalizedNode.getValue().toString(), output.getValue()
83         .toString());
84   }
85
86   @Test
87   public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
88       throws Exception {
89     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
90
91     final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
92         new NormalizedNodeToNodeCodec(schemaContext);
93
94     Container container =
95         normalizedNodeToNodeCodec.encode(documentOne);
96
97
98     final NormalizedNode<?, ?> decode =
99         normalizedNodeToNodeCodec
100             .decode(
101                 container.getNormalizedNode());
102     assertNotNull(decode);
103
104     // let us ensure that the return decode normalized node encode returns same container
105     Container containerResult =
106         normalizedNodeToNodeCodec.encode(decode);
107
108     // check first level children are proper
109     List<Node> childrenResult =
110         containerResult.getNormalizedNode().getChildList();
111     List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
112
113     System.out.println("-------------------------------------------------");
114
115     System.out.println(childrenOriginal.toString());
116
117     System.out.println("-------------------------------------------------");
118
119     System.out.println(childrenResult.toString());
120
121     boolean bFound;
122     for (Node resultChild : childrenResult) {
123       bFound = false;
124       for (Node originalChild : childrenOriginal) {
125
126         YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
127               containerResult.getNormalizedNode(),
128               resultChild.getPathArgument());
129
130           YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
131               container.getNormalizedNode(),
132               originalChild.getPathArgument());
133
134         if (original.equals(result)
135             && resultChild.getIntType() == resultChild.getIntType()) {
136           bFound = true;
137           break;
138         }
139       }
140       assertTrue(bFound);
141     }
142
143   }
144
145   @Test
146   public void addAugmentations() {
147     String stringId =
148         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
149             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
150             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=1}]";
151
152     YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
153
154     MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
155
156     NormalizedNodeToNodeCodec codec =
157         new NormalizedNodeToNodeCodec(schemaContext);
158
159     Container encode = codec.encode(uno);
160
161     System.out.println(encode.getNormalizedNode());
162
163     codec.decode(encode.getNormalizedNode());
164   }
165
166 }