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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
19 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
20 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
21 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
22 import org.opendaylight.controller.cluster.datastore.util.TestModel;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
24 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class NormalizedNodeToNodeCodecTest {
33 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeToNodeCodecTest.class);
35 private SchemaContext schemaContext;
39 schemaContext = TestModel.createTestContext();
40 assertNotNull("Schema context must not be null.", schemaContext);
43 private static YangInstanceIdentifier instanceIdentifierFromString(String str) {
44 return PathUtils.toYangInstanceIdentifier(str);
48 public void testNormalizeNodeAttributesToProtoBuffNode() {
49 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
50 String id = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
51 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
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)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(PathUtils.toString(YangInstanceIdentifier.EMPTY),
60 // Validate the value of id can be retrieved from the normalized node
61 NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
62 assertNotNull(output);
64 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
65 long start = System.currentTimeMillis();
66 Container container = codec.encode(output);
67 long end = System.currentTimeMillis();
69 LOG.info("Time taken 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 = codec.decode(node);
79 end = System.currentTimeMillis();
81 LOG.info("Time taken to decode: {}", end - start);
83 assertEquals(normalizedNode.getValue().toString(), output.getValue().toString());
87 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode() throws Exception {
88 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
90 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec();
92 Container container = normalizedNodeToNodeCodec.encode(documentOne);
94 final NormalizedNode<?, ?> decode = normalizedNodeToNodeCodec.decode(container.getNormalizedNode());
95 assertNotNull(decode);
97 // let us ensure that the return decode normalized node encode returns
99 Container containerResult = normalizedNodeToNodeCodec.encode(decode);
101 // check first level children are proper
102 List<Node> childrenResult = containerResult.getNormalizedNode().getChildList();
103 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
105 LOG.info("\n-------------------------------------------------\n" + childrenOriginal
106 + "\n-------------------------------------------------\n" + childrenResult);
109 for (Node resultChild : childrenResult) {
111 for (Node originalChild : childrenOriginal) {
113 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer
114 .deSerialize(containerResult.getNormalizedNode(), resultChild.getPathArgument());
116 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer
117 .deSerialize(container.getNormalizedNode(), originalChild.getPathArgument());
119 if (original.equals(result) && resultChild.getIntType() == resultChild.getIntType()) {
130 public void addAugmentations() {
131 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
133 NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
135 Container encode = codec.encode(uno);
137 LOG.info(encode.getNormalizedNode().toString());
139 codec.decode(encode.getNormalizedNode());