3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
11 package org.opendaylight.controller.cluster.datastore.node;
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;
27 import java.util.List;
29 import static junit.framework.Assert.assertEquals;
30 import static junit.framework.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
33 public class NormalizedNodeToNodeCodecTest {
37 private SchemaContext schemaContext;
41 schemaContext = TestModel.createTestContext();
42 assertNotNull("Schema context must not be null.", schemaContext);
45 private YangInstanceIdentifier instanceIdentifierFromString(String s) {
46 return PathUtils.toYangInstanceIdentifier(s);
50 public void testNormalizeNodeAttributesToProtoBuffNode() {
51 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
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";
58 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
59 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
60 PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
62 // Validate the value of id can be retrieved from the normalized node
63 NormalizedNode output = normalizedNodeGetter.getOutput();
64 assertNotNull(output);
67 NormalizedNodeToNodeCodec codec =
68 new NormalizedNodeToNodeCodec(schemaContext);
69 long start = System.currentTimeMillis();
72 long end = System.currentTimeMillis();
74 System.out.println("Timetaken to encode :"+(end-start));
76 assertNotNull(container);
78 // Decode the normalized node from the ProtocolBuffer form
79 // first get the node representation of normalized node
80 final Node node = container.getNormalizedNode();
82 start = System.currentTimeMillis();
83 NormalizedNode<?, ?> normalizedNode =
85 end = System.currentTimeMillis();
87 System.out.println("Timetaken to decode :"+(end-start));
89 assertEquals(normalizedNode.getValue().toString(), output.getValue()
94 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
96 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
98 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
99 new NormalizedNodeToNodeCodec(schemaContext);
101 Container container =
102 normalizedNodeToNodeCodec.encode(documentOne);
105 final NormalizedNode<?, ?> decode =
106 normalizedNodeToNodeCodec
108 container.getNormalizedNode());
109 assertNotNull(decode);
111 // let us ensure that the return decode normalized node encode returns same container
112 Container containerResult =
113 normalizedNodeToNodeCodec.encode(decode);
115 // check first level children are proper
116 List<Node> childrenResult =
117 containerResult.getNormalizedNode().getChildList();
118 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
120 System.out.println("-------------------------------------------------");
122 System.out.println(childrenOriginal.toString());
124 System.out.println("-------------------------------------------------");
126 System.out.println(childrenResult.toString());
129 for (Node resultChild : childrenResult) {
131 for (Node originalChild : childrenOriginal) {
133 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
134 containerResult.getNormalizedNode(),
135 resultChild.getPathArgument());
137 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
138 container.getNormalizedNode(),
139 originalChild.getPathArgument());
141 if (original.equals(result)
142 && resultChild.getIntType() == resultChild.getIntType()) {
153 public void addAugmentations() {
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}]";
159 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
161 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
163 NormalizedNodeToNodeCodec codec =
164 new NormalizedNodeToNodeCodec(schemaContext);
166 Container encode = codec.encode(uno);
168 System.out.println(encode.getNormalizedNode());
170 codec.decode(encode.getNormalizedNode());