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.NodeIdentifierFactory;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
18 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
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.ArrayList;
28 import java.util.List;
30 import static junit.framework.Assert.assertEquals;
31 import static junit.framework.Assert.assertNotNull;
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) {
47 String[] ids = s.split("/");
49 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
50 for (String nodeId : ids) {
51 if (!"".equals(nodeId)) {
52 pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
55 final YangInstanceIdentifier instanceIdentifier =
56 YangInstanceIdentifier.create(pathArguments);
57 return instanceIdentifier;
62 public void testNormalizeNodeAttributesToProtoBuffNode() {
63 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
65 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
66 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
67 + "/(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}]"
68 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
70 NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
71 new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
72 YangInstanceIdentifier.builder().build().toString(), documentOne);
74 // Validate the value of id can be retrieved from the normalized node
75 NormalizedNode output = normalizedNodeGetter.getOutput();
76 assertNotNull(output);
79 NormalizedNodeToNodeCodec codec =
80 new NormalizedNodeToNodeCodec(schemaContext);
82 codec.encode(instanceIdentifierFromString(id), output);
84 assertNotNull(container);
85 assertEquals(id, container.getParentPath() + "/"
86 + container.getNormalizedNode().getPath());
88 // Decode the normalized node from the ProtocolBuffer form
89 // first get the node representation of normalized node
90 final Node node = container.getNormalizedNode();
92 NormalizedNode<?, ?> normalizedNode =
93 codec.decode(instanceIdentifierFromString(id), node);
95 assertEquals(normalizedNode.getValue().toString(), output.getValue()
100 public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
102 final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
104 final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
105 new NormalizedNodeToNodeCodec(schemaContext);
107 Container container =
108 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
109 .build(), documentOne);
112 final NormalizedNode<?, ?> decode =
113 normalizedNodeToNodeCodec
115 instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
116 container.getNormalizedNode());
117 assertNotNull(decode != null);
119 // let us ensure that the return decode normalized node encode returns same container
120 Container containerResult =
121 normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
124 assertEquals(container.getParentPath(), containerResult.getParentPath());
125 assertEquals(container.getNormalizedNode().getChildCount(), container
126 .getNormalizedNode().getChildCount());
128 Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),
129 container.getNormalizedNode().getChildCount());
131 // check first level children are proper
132 List<Node> childrenResult =
133 containerResult.getNormalizedNode().getChildList();
134 List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
136 System.out.println("-------------------------------------------------");
138 System.out.println(childrenOriginal.toString());
140 System.out.println("-------------------------------------------------");
142 System.out.println(childrenResult.toString());
145 for (Node resultChild : childrenResult) {
147 for (Node originalChild : childrenOriginal) {
148 if (originalChild.getPath().equals(resultChild.getPath())
149 && resultChild.getType().equals(resultChild.getType())) {
154 Assert.assertTrue(bFound);
160 public void addAugmentations() {
162 "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
163 + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
164 + "/(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}]";
166 YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
168 MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
170 NormalizedNodeToNodeCodec codec =
171 new NormalizedNodeToNodeCodec(schemaContext);
173 Container encode = codec.encode(identifier, uno);
175 System.out.println(encode.getNormalizedNode());
177 codec.decode(identifier, encode.getNormalizedNode());