2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node;
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;
29 public class NormalizedNodeToNodeCodecTest {
30 private SchemaContext schemaContext;
34 schemaContext = TestModel.createTestContext();
35 assertNotNull("Schema context must not be null.", schemaContext);
38 private static YangInstanceIdentifier instanceIdentifierFromString(String s) {
39 return PathUtils.toYangInstanceIdentifier(s);
43 public void testNormalizeNodeAttributesToProtoBuffNode() {
44 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
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";
51 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
52 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
53 PathUtils.toString(YangInstanceIdentifier.EMPTY), documentOne);
55 // Validate the value of id can be retrieved from the normalized node
56 NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
57 assertNotNull(output);
60 NormalizedNodeToNodeCodec codec =
61 new NormalizedNodeToNodeCodec(schemaContext);
62 long start = System.currentTimeMillis();
65 long end = System.currentTimeMillis();
67 System.out.println("Timetaken to encode :"+(end-start));
69 assertNotNull(container);
71 // Decode the normalized node from the ProtocolBuffer form
72 // first get the node representation of normalized node
73 final Node node = container.getNormalizedNode();
75 start = System.currentTimeMillis();
76 NormalizedNode<?, ?> normalizedNode =
78 end = System.currentTimeMillis();
80 System.out.println("Timetaken to decode :"+(end-start));
82 assertEquals(normalizedNode.getValue().toString(), output.getValue()
87 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
89 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
91 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
92 new NormalizedNodeToNodeCodec(schemaContext);
95 normalizedNodeToNodeCodec.encode(documentOne);
98 final NormalizedNode<?, ?> decode =
99 normalizedNodeToNodeCodec
101 container.getNormalizedNode());
102 assertNotNull(decode);
104 // let us ensure that the return decode normalized node encode returns same container
105 Container containerResult =
106 normalizedNodeToNodeCodec.encode(decode);
108 // check first level children are proper
109 List<Node> childrenResult =
110 containerResult.getNormalizedNode().getChildList();
111 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
113 System.out.println("-------------------------------------------------");
115 System.out.println(childrenOriginal.toString());
117 System.out.println("-------------------------------------------------");
119 System.out.println(childrenResult.toString());
122 for (Node resultChild : childrenResult) {
124 for (Node originalChild : childrenOriginal) {
126 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
127 containerResult.getNormalizedNode(),
128 resultChild.getPathArgument());
130 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
131 container.getNormalizedNode(),
132 originalChild.getPathArgument());
134 if (original.equals(result)
135 && resultChild.getIntType() == resultChild.getIntType()) {
146 public void addAugmentations() {
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}]";
152 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
154 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
156 NormalizedNodeToNodeCodec codec =
157 new NormalizedNodeToNodeCodec(schemaContext);
159 Container encode = codec.encode(uno);
161 System.out.println(encode.getNormalizedNode());
163 codec.decode(encode.getNormalizedNode());