Merge "Bug 1763: Fixed illegal state in Binding Transaction Chain."
[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(instanceIdentifierFromString(id), output);
72     long end = System.currentTimeMillis();
73
74     System.out.println("Timetaken to encode :"+(end-start));
75
76     assertNotNull(container);
77     assertEquals(id, container.getParentPath() + "/"
78         + NormalizedNodeSerializer.deSerialize(container.getNormalizedNode(),
79         container.getNormalizedNode().getPathArgument()));
80
81     // Decode the normalized node from the ProtocolBuffer form
82     // first get the node representation of normalized node
83     final Node node = container.getNormalizedNode();
84
85     start = System.currentTimeMillis();
86     NormalizedNode<?, ?> normalizedNode =
87         codec.decode(instanceIdentifierFromString(id), node);
88     end = System.currentTimeMillis();
89
90     System.out.println("Timetaken to decode :"+(end-start));
91
92     assertEquals(normalizedNode.getValue().toString(), output.getValue()
93         .toString());
94   }
95
96   @Test
97   public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
98       throws Exception {
99     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
100
101     final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
102         new NormalizedNodeToNodeCodec(schemaContext);
103
104     Container container =
105         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
106             .build(), documentOne);
107
108
109     final NormalizedNode<?, ?> decode =
110         normalizedNodeToNodeCodec
111             .decode(
112                 instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
113                 container.getNormalizedNode());
114     assertNotNull(decode);
115
116     // let us ensure that the return decode normalized node encode returns same container
117     Container containerResult =
118         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
119             .build(), decode);
120
121     assertEquals(container.getParentPath(), containerResult.getParentPath());
122
123     assertEquals(containerResult.getNormalizedNode().getChildCount(),
124         container.getNormalizedNode().getChildCount());
125
126     // check first level children are proper
127     List<Node> childrenResult =
128         containerResult.getNormalizedNode().getChildList();
129     List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
130
131     System.out.println("-------------------------------------------------");
132
133     System.out.println(childrenOriginal.toString());
134
135     System.out.println("-------------------------------------------------");
136
137     System.out.println(childrenResult.toString());
138
139     boolean bFound;
140     for (Node resultChild : childrenResult) {
141       bFound = false;
142       for (Node originalChild : childrenOriginal) {
143
144         YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
145               containerResult.getNormalizedNode(),
146               resultChild.getPathArgument());
147
148           YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
149               container.getNormalizedNode(),
150               originalChild.getPathArgument());
151
152         if (original.equals(result)
153             && resultChild.getIntType() == resultChild.getIntType()) {
154           bFound = true;
155           break;
156         }
157       }
158       assertTrue(bFound);
159     }
160
161   }
162
163   @Test
164   public void addAugmentations() {
165     String stringId =
166         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
167             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
168             + "/(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}]";
169
170     YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
171
172     MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
173
174     NormalizedNodeToNodeCodec codec =
175         new NormalizedNodeToNodeCodec(schemaContext);
176
177     Container encode = codec.encode(identifier, uno);
178
179     System.out.println(encode.getNormalizedNode());
180
181     codec.decode(identifier, encode.getNormalizedNode());
182   }
183
184 }