/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.controller.cluster.datastore.node;
-import junit.framework.Assert;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
+import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
+import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
import org.opendaylight.controller.cluster.datastore.util.TestModel;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class NormalizedNodeToNodeCodecTest {
+ private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeToNodeCodecTest.class);
+ private SchemaContext schemaContext;
-
- private SchemaContext schemaContext;
-
- @Before
- public void setUp() {
- schemaContext = TestModel.createTestContext();
- assertNotNull("Schema context must not be null.", schemaContext);
- }
-
- private YangInstanceIdentifier instanceIdentifierFromString(String s) {
-
- String[] ids = s.split("/");
-
- List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
- for (String nodeId : ids) {
- if (!"".equals(nodeId)) {
- pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
- }
+ @Before
+ public void setUp() {
+ schemaContext = TestModel.createTestContext();
+ assertNotNull("Schema context must not be null.", schemaContext);
}
- final YangInstanceIdentifier instanceIdentifier =
- YangInstanceIdentifier.create(pathArguments);
- return instanceIdentifier;
- }
+ private static YangInstanceIdentifier instanceIdentifierFromString(String str) {
+ return PathUtils.toYangInstanceIdentifier(str);
+ }
- @Test
- public void testNormalizeNodeAttributesToProtoBuffNode() {
- final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
- String id =
- "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
+ @Test
+ public void testNormalizeNodeAttributesToProtoBuffNode() {
+ final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
+ String id = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
+ "/(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)outer-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]"
+ + "/(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}]"
+ "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
- NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
- new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
- YangInstanceIdentifier.builder().build().toString(), documentOne);
-
- // Validate the value of id can be retrieved from the normalized node
- NormalizedNode output = normalizedNodeGetter.getOutput();
- assertNotNull(output);
+ NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
+ new NormalizedNodeNavigator(normalizedNodeGetter).navigate(PathUtils.toString(YangInstanceIdentifier.EMPTY),
+ documentOne);
+ // Validate the value of id can be retrieved from the normalized node
+ NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
+ assertNotNull(output);
- NormalizedNodeToNodeCodec codec =
- new NormalizedNodeToNodeCodec(schemaContext);
- Container container =
- codec.encode(instanceIdentifierFromString(id), output);
+ NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
+ long start = System.currentTimeMillis();
+ Container container = codec.encode(output);
+ long end = System.currentTimeMillis();
- assertNotNull(container);
- assertEquals(id, container.getParentPath() + "/"
- + container.getNormalizedNode().getPath());
+ LOG.info("Time taken to encode: {}", end - start);
- // Decode the normalized node from the ProtocolBuffer form
- // first get the node representation of normalized node
- final Node node = container.getNormalizedNode();
+ assertNotNull(container);
- NormalizedNode<?, ?> normalizedNode =
- codec.decode(instanceIdentifierFromString(id), node);
+ // Decode the normalized node from the ProtocolBuffer form
+ // first get the node representation of normalized node
+ final Node node = container.getNormalizedNode();
- assertEquals(normalizedNode.getValue().toString(), output.getValue()
- .toString());
- }
+ start = System.currentTimeMillis();
+ NormalizedNode<?, ?> normalizedNode = codec.decode(node);
+ end = System.currentTimeMillis();
- @Test
- public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
- throws Exception {
- final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
+ LOG.info("Time taken to decode: {}", end - start);
- final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
- new NormalizedNodeToNodeCodec(schemaContext);
-
- Container container =
- normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
- .build(), documentOne);
+ assertEquals(normalizedNode.getValue().toString(), output.getValue().toString());
+ }
+ @Test
+ public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode() throws Exception {
+ final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
- final NormalizedNode<?, ?> decode =
- normalizedNodeToNodeCodec
- .decode(
- instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
- container.getNormalizedNode());
- assertNotNull(decode != null);
+ final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec();
- // let us ensure that the return decode normalized node encode returns same container
- Container containerResult =
- normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
- .build(), decode);
+ Container container = normalizedNodeToNodeCodec.encode(documentOne);
- assertEquals(container.getParentPath(), containerResult.getParentPath());
- assertEquals(container.getNormalizedNode().getChildCount(), container
- .getNormalizedNode().getChildCount());
+ final NormalizedNode<?, ?> decode = normalizedNodeToNodeCodec.decode(container.getNormalizedNode());
+ assertNotNull(decode);
- Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),
- container.getNormalizedNode().getChildCount());
+ // let us ensure that the return decode normalized node encode returns
+ // same container
+ Container containerResult = normalizedNodeToNodeCodec.encode(decode);
- // check first level children are proper
- List<Node> childrenResult =
- containerResult.getNormalizedNode().getChildList();
- List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
+ // check first level children are proper
+ List<Node> childrenResult = containerResult.getNormalizedNode().getChildList();
+ List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
- System.out.println("-------------------------------------------------");
+ LOG.info("\n-------------------------------------------------\n" + childrenOriginal
+ + "\n-------------------------------------------------\n" + childrenResult);
- System.out.println(childrenOriginal.toString());
+ boolean found;
+ for (Node resultChild : childrenResult) {
+ found = false;
+ for (Node originalChild : childrenOriginal) {
- System.out.println("-------------------------------------------------");
+ YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer
+ .deSerialize(containerResult.getNormalizedNode(), resultChild.getPathArgument());
- System.out.println(childrenResult.toString());
+ YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer
+ .deSerialize(container.getNormalizedNode(), originalChild.getPathArgument());
- boolean bFound;
- for (Node resultChild : childrenResult) {
- bFound = false;
- for (Node originalChild : childrenOriginal) {
- if (originalChild.getPath().equals(resultChild.getPath())
- && resultChild.getType().equals(resultChild.getType())) {
- bFound = true;
- break;
+ if (original.equals(result) && resultChild.getIntType() == resultChild.getIntType()) {
+ found = true;
+ break;
+ }
+ }
+ assertTrue(found);
}
- }
- Assert.assertTrue(bFound);
- }
- }
-
- @Test
- public void addAugmentations() {
- String stringId =
- "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
- + "/(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)augmented-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=1}]";
-
- YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
-
- MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
+ }
- NormalizedNodeToNodeCodec codec =
- new NormalizedNodeToNodeCodec(schemaContext);
+ @Test
+ public void addAugmentations() {
+ MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
- Container encode = codec.encode(identifier, uno);
+ NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
- System.out.println(encode.getNormalizedNode());
+ Container encode = codec.encode(uno);
- codec.decode(identifier, encode.getNormalizedNode());
- }
+ LOG.info(encode.getNormalizedNode().toString());
+ codec.decode(encode.getNormalizedNode());
+ }
}