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 org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
14 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
15 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
16 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
17 import org.opendaylight.controller.cluster.datastore.util.TestModel;
18 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
19 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import java.util.List;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 public class NormalizedNodeToNodeCodecTest {
32 private SchemaContext schemaContext;
36 schemaContext = TestModel.createTestContext();
37 assertNotNull("Schema context must not be null.", schemaContext);
40 private static YangInstanceIdentifier instanceIdentifierFromString(String s) {
41 return PathUtils.toYangInstanceIdentifier(s);
45 public void testNormalizeNodeAttributesToProtoBuffNode() {
46 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
48 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
49 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
50 + "/(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}]"
51 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
53 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
54 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
55 PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
57 // Validate the value of id can be retrieved from the normalized node
58 NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
59 assertNotNull(output);
62 NormalizedNodeToNodeCodec codec =
63 new NormalizedNodeToNodeCodec(schemaContext);
64 long start = System.currentTimeMillis();
67 long end = System.currentTimeMillis();
69 System.out.println("Timetaken to encode :"+(end-start));
71 assertNotNull(container);
73 // Decode the normalized node from the ProtocolBuffer form
74 // first get the node representation of normalized node
75 final Node node = container.getNormalizedNode();
77 start = System.currentTimeMillis();
78 NormalizedNode<?, ?> normalizedNode =
80 end = System.currentTimeMillis();
82 System.out.println("Timetaken to decode :"+(end-start));
84 assertEquals(normalizedNode.getValue().toString(), output.getValue()
89 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
91 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
93 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
94 new NormalizedNodeToNodeCodec(schemaContext);
97 normalizedNodeToNodeCodec.encode(documentOne);
100 final NormalizedNode<?, ?> decode =
101 normalizedNodeToNodeCodec
103 container.getNormalizedNode());
104 assertNotNull(decode);
106 // let us ensure that the return decode normalized node encode returns same container
107 Container containerResult =
108 normalizedNodeToNodeCodec.encode(decode);
110 // check first level children are proper
111 List<Node> childrenResult =
112 containerResult.getNormalizedNode().getChildList();
113 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
115 System.out.println("-------------------------------------------------");
117 System.out.println(childrenOriginal.toString());
119 System.out.println("-------------------------------------------------");
121 System.out.println(childrenResult.toString());
124 for (Node resultChild : childrenResult) {
126 for (Node originalChild : childrenOriginal) {
128 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
129 containerResult.getNormalizedNode(),
130 resultChild.getPathArgument());
132 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
133 container.getNormalizedNode(),
134 originalChild.getPathArgument());
136 if (original.equals(result)
137 && resultChild.getIntType() == resultChild.getIntType()) {
148 public void addAugmentations() {
150 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
151 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
152 + "/(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}]";
154 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
156 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
158 NormalizedNodeToNodeCodec codec =
159 new NormalizedNodeToNodeCodec(schemaContext);
161 Container encode = codec.encode(uno);
163 System.out.println(encode.getNormalizedNode());
165 codec.decode(encode.getNormalizedNode());