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 junit.framework.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
18 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
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;
32 public class NormalizedNodeToNodeCodecTest {
36 private SchemaContext schemaContext;
40 schemaContext = TestModel.createTestContext();
41 assertNotNull("Schema context must not be null.", schemaContext);
44 private YangInstanceIdentifier instanceIdentifierFromString(String s) {
45 return PathUtils.toYangInstanceIdentifier(s);
49 public void testNormalizeNodeAttributesToProtoBuffNode() {
50 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
52 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
53 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
54 + "/(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}]"
55 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
57 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
58 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
59 PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
61 // Validate the value of id can be retrieved from the normalized node
62 NormalizedNode output = normalizedNodeGetter.getOutput();
63 assertNotNull(output);
66 NormalizedNodeToNodeCodec codec =
67 new NormalizedNodeToNodeCodec(schemaContext);
68 long start = System.currentTimeMillis();
70 codec.encode(instanceIdentifierFromString(id), output);
71 long end = System.currentTimeMillis();
73 System.out.println("Timetaken to encode :"+(end-start));
75 assertNotNull(container);
76 assertEquals(id, container.getParentPath() + "/"
77 + container.getNormalizedNode().getPath());
79 // Decode the normalized node from the ProtocolBuffer form
80 // first get the node representation of normalized node
81 final Node node = container.getNormalizedNode();
83 start = System.currentTimeMillis();
84 NormalizedNode<?, ?> normalizedNode =
85 codec.decode(instanceIdentifierFromString(id), node);
86 end = System.currentTimeMillis();
88 System.out.println("Timetaken to decode :"+(end-start));
90 assertEquals(normalizedNode.getValue().toString(), output.getValue()
95 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
97 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
99 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
100 new NormalizedNodeToNodeCodec(schemaContext);
102 Container container =
103 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
104 .build(), documentOne);
107 final NormalizedNode<?, ?> decode =
108 normalizedNodeToNodeCodec
110 instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
111 container.getNormalizedNode());
112 assertNotNull(decode != null);
114 // let us ensure that the return decode normalized node encode returns same container
115 Container containerResult =
116 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
119 assertEquals(container.getParentPath(), containerResult.getParentPath());
120 assertEquals(container.getNormalizedNode().getChildCount(), container
121 .getNormalizedNode().getChildCount());
123 Assert.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) {
143 if (originalChild.getPath().equals(resultChild.getPath())
144 && resultChild.getType().equals(resultChild.getType())) {
149 Assert.assertTrue(bFound);
155 public void addAugmentations() {
157 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
158 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
159 + "/(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}]";
161 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
163 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
165 NormalizedNodeToNodeCodec codec =
166 new NormalizedNodeToNodeCodec(schemaContext);
168 Container encode = codec.encode(identifier, uno);
170 System.out.println(encode.getNormalizedNode());
172 codec.decode(identifier, encode.getNormalizedNode());