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();
71 codec.encode(instanceIdentifierFromString(id), output);
72 long end = System.currentTimeMillis();
74 System.out.println("Timetaken to encode :"+(end-start));
76 assertNotNull(container);
77 assertEquals(id, container.getParentPath() + "/"
78 + NormalizedNodeSerializer.deSerialize(container.getNormalizedNode(),
79 container.getNormalizedNode().getPathArgument()));
81 // Decode the normalized node from the ProtocolBuffer form
82 // first get the node representation of normalized node
83 final Node node = container.getNormalizedNode();
85 start = System.currentTimeMillis();
86 NormalizedNode<?, ?> normalizedNode =
87 codec.decode(instanceIdentifierFromString(id), node);
88 end = System.currentTimeMillis();
90 System.out.println("Timetaken to decode :"+(end-start));
92 assertEquals(normalizedNode.getValue().toString(), output.getValue()
97 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
99 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
101 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
102 new NormalizedNodeToNodeCodec(schemaContext);
104 Container container =
105 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
106 .build(), documentOne);
109 final NormalizedNode<?, ?> decode =
110 normalizedNodeToNodeCodec
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);
116 // let us ensure that the return decode normalized node encode returns same container
117 Container containerResult =
118 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
121 assertEquals(container.getParentPath(), containerResult.getParentPath());
123 assertEquals(containerResult.getNormalizedNode().getChildCount(),
124 container.getNormalizedNode().getChildCount());
126 // check first level children are proper
127 List<Node> childrenResult =
128 containerResult.getNormalizedNode().getChildList();
129 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
131 System.out.println("-------------------------------------------------");
133 System.out.println(childrenOriginal.toString());
135 System.out.println("-------------------------------------------------");
137 System.out.println(childrenResult.toString());
140 for (Node resultChild : childrenResult) {
142 for (Node originalChild : childrenOriginal) {
144 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
145 containerResult.getNormalizedNode(),
146 resultChild.getPathArgument());
148 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
149 container.getNormalizedNode(),
150 originalChild.getPathArgument());
152 if (original.equals(result)
153 && resultChild.getIntType() == resultChild.getIntType()) {
164 public void addAugmentations() {
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}]";
170 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
172 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
174 NormalizedNodeToNodeCodec codec =
175 new NormalizedNodeToNodeCodec(schemaContext);
177 Container encode = codec.encode(identifier, uno);
179 System.out.println(encode.getNormalizedNode());
181 codec.decode(identifier, encode.getNormalizedNode());