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 {
35 private SchemaContext schemaContext;
39 schemaContext = TestModel.createTestContext();
40 assertNotNull("Schema context must not be null.", schemaContext);
43 private YangInstanceIdentifier instanceIdentifierFromString(String s) {
44 return PathUtils.toYangInstanceIdentifier(s);
48 public void testNormalizeNodeAttributesToProtoBuffNode() {
49 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
51 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
52 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
53 + "/(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}]"
54 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
56 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
57 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
58 PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
60 // Validate the value of id can be retrieved from the normalized node
61 NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
62 assertNotNull(output);
65 NormalizedNodeToNodeCodec codec =
66 new NormalizedNodeToNodeCodec(schemaContext);
67 long start = System.currentTimeMillis();
70 long end = System.currentTimeMillis();
72 System.out.println("Timetaken to encode :"+(end-start));
74 assertNotNull(container);
76 // Decode the normalized node from the ProtocolBuffer form
77 // first get the node representation of normalized node
78 final Node node = container.getNormalizedNode();
80 start = System.currentTimeMillis();
81 NormalizedNode<?, ?> normalizedNode =
83 end = System.currentTimeMillis();
85 System.out.println("Timetaken to decode :"+(end-start));
87 assertEquals(normalizedNode.getValue().toString(), output.getValue()
92 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
94 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
96 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
97 new NormalizedNodeToNodeCodec(schemaContext);
100 normalizedNodeToNodeCodec.encode(documentOne);
103 final NormalizedNode<?, ?> decode =
104 normalizedNodeToNodeCodec
106 container.getNormalizedNode());
107 assertNotNull(decode);
109 // let us ensure that the return decode normalized node encode returns same container
110 Container containerResult =
111 normalizedNodeToNodeCodec.encode(decode);
113 // check first level children are proper
114 List<Node> childrenResult =
115 containerResult.getNormalizedNode().getChildList();
116 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
118 System.out.println("-------------------------------------------------");
120 System.out.println(childrenOriginal.toString());
122 System.out.println("-------------------------------------------------");
124 System.out.println(childrenResult.toString());
127 for (Node resultChild : childrenResult) {
129 for (Node originalChild : childrenOriginal) {
131 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer.deSerialize(
132 containerResult.getNormalizedNode(),
133 resultChild.getPathArgument());
135 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer.deSerialize(
136 container.getNormalizedNode(),
137 originalChild.getPathArgument());
139 if (original.equals(result)
140 && resultChild.getIntType() == resultChild.getIntType()) {
151 public void addAugmentations() {
153 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
154 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
155 + "/(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}]";
157 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
159 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
161 NormalizedNodeToNodeCodec codec =
162 new NormalizedNodeToNodeCodec(schemaContext);
164 Container encode = codec.encode(uno);
166 System.out.println(encode.getNormalizedNode());
168 codec.decode(encode.getNormalizedNode());