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