From 0052eccf24fe02d693313cb68a1f6ee066211616 Mon Sep 17 00:00:00 2001 From: Moiz Raja Date: Sat, 23 Aug 2014 03:34:43 -0700 Subject: [PATCH 1/1] BUG 1597 - Do not use toString as serialized version of YangInstanceIdentifier/PathArgument The code has been modified to stop interpreting YangInstanceIdentifier.toString and PathArgument.toString as a serialized versions of those objects. The toString implementations of those objects has now been moved into PathUtils. - NormalizedNodeGetter and NormalizedNodeToNodeCodecTest have been modified to use PathUtils.toString instead of YangInstanceIdentifier.toString or PathArgument.toString - Added tests to cover all the utilities in PathUtils Change-Id: I1ed2c452e15a45229c36532d59db7fa1a77d283d Signed-off-by: Moiz Raja --- .../node/NormalizedNodeToNodeCodec.java | 2 +- .../node/utils/NormalizedNodeGetter.java | 2 +- .../datastore/node/utils/PathUtils.java | 91 +++++++++++++ .../node/NormalizedNodeToNodeCodecTest.java | 19 +-- .../datastore/node/utils/PathUtilsTest.java | 121 ++++++++++++++++++ 5 files changed, 217 insertions(+), 18 deletions(-) create mode 100644 opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtilsTest.java diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java index 17bdb36e56..d1ae264c3b 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodec.java @@ -32,7 +32,7 @@ public class NormalizedNodeToNodeCodec { String parentPath = ""; if(id != null){ - parentPath = PathUtils.getParentPath(id.toString()); + parentPath = PathUtils.getParentPath(PathUtils.toString(id)); } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/NormalizedNodeGetter.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/NormalizedNodeGetter.java index 31e6521a28..32f3be82fc 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/NormalizedNodeGetter.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/NormalizedNodeGetter.java @@ -25,7 +25,7 @@ public class NormalizedNodeGetter implements @Override public void visitNode(int level, String parentPath, NormalizedNode normalizedNode) { - String nodePath = parentPath + "/"+ normalizedNode.getIdentifier().toString(); + String nodePath = parentPath + "/"+ PathUtils.toString(normalizedNode.getIdentifier()); if(nodePath.toString().equals(path)){ output = normalizedNode; diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtils.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtils.java index 1dd0f3b827..25b65f0168 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtils.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtils.java @@ -10,7 +10,16 @@ package org.opendaylight.controller.cluster.datastore.node.utils; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + public class PathUtils { + public static String getParentPath(String currentElementPath){ StringBuilder parentPath = new StringBuilder(); @@ -27,4 +36,86 @@ public class PathUtils { } return parentPath.toString(); } + + /** + * Given a YangInstanceIdentifier return a serialized version of the same + * as a String + * + * @param path + * @return + */ + public static String toString(YangInstanceIdentifier path){ + StringBuilder sb = new StringBuilder(); + Iterator iterator = + path.getPathArguments().iterator(); + + while(iterator.hasNext()){ + sb.append(toString(iterator.next())); + if(iterator.hasNext()){ + sb.append("/"); + } + } + return sb.toString(); + } + + /** + * Given a YangInstanceIdentifier.PathArgument return a serialized version + * of the same as a String + * + * @param pathArgument + * @return + */ + public static String toString(YangInstanceIdentifier.PathArgument pathArgument){ + if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){ + return toString((YangInstanceIdentifier.NodeIdentifier) pathArgument); + } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){ + return toString((YangInstanceIdentifier.AugmentationIdentifier) pathArgument); + } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){ + return toString((YangInstanceIdentifier.NodeWithValue) pathArgument); + } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){ + return toString((YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument); + } + + return pathArgument.toString(); + } + + /** + * Given a serialized string version of a YangInstanceIdentifier convert + * to a YangInstanceIdentifier + * + * @param path + * @return + */ + public static YangInstanceIdentifier toYangInstanceIdentifier(String path){ + String[] segments = path.split("/"); + + List pathArguments = new ArrayList<>(); + for (String segment : segments) { + if (!"".equals(segment)) { + pathArguments.add(NodeIdentifierFactory.getArgument(segment)); + } + } + return YangInstanceIdentifier.create(pathArguments); + } + + private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){ + return pathArgument.getNodeType().toString(); + } + + private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){ + Set childNames = pathArgument.getPossibleChildNames(); + final StringBuilder sb = new StringBuilder("AugmentationIdentifier{"); + sb.append("childNames=").append(childNames).append('}'); + return sb.toString(); + + } + + private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){ + return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]"; + } + + private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){ + return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']'; + } + } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodecTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodecTest.java index bdad86ddc1..c42865c659 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodecTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/NormalizedNodeToNodeCodecTest.java @@ -13,9 +13,9 @@ package org.opendaylight.controller.cluster.datastore.node; import junit.framework.Assert; 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.util.TestModel; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container; import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node; @@ -24,7 +24,6 @@ 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; @@ -43,21 +42,9 @@ public class NormalizedNodeToNodeCodecTest { } private YangInstanceIdentifier instanceIdentifierFromString(String s) { - - String[] ids = s.split("/"); - - List pathArguments = new ArrayList<>(); - for (String nodeId : ids) { - if (!"".equals(nodeId)) { - pathArguments.add(NodeIdentifierFactory.getArgument(nodeId)); - } - } - final YangInstanceIdentifier instanceIdentifier = - YangInstanceIdentifier.create(pathArguments); - return instanceIdentifier; + return PathUtils.toYangInstanceIdentifier(s); } - @Test public void testNormalizeNodeAttributesToProtoBuffNode() { final NormalizedNode documentOne = TestModel.createTestContainer(); @@ -69,7 +56,7 @@ public class NormalizedNodeToNodeCodecTest { NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id); new NormalizedNodeNavigator(normalizedNodeGetter).navigate( - YangInstanceIdentifier.builder().build().toString(), documentOne); + PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne); // Validate the value of id can be retrieved from the normalized node NormalizedNode output = normalizedNodeGetter.getOutput(); diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtilsTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtilsTest.java new file mode 100644 index 0000000000..ffa8a1059e --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/PathUtilsTest.java @@ -0,0 +1,121 @@ +package org.opendaylight.controller.cluster.datastore.node.utils; + +import org.junit.Test; +import org.opendaylight.controller.cluster.datastore.util.TestModel; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static junit.framework.TestCase.assertEquals; + +public class PathUtilsTest { + + @Test + public void getParentPath(){ + assertEquals("", PathUtils.getParentPath("foobar")); + assertEquals("", PathUtils.getParentPath("/a")); + assertEquals("/a", PathUtils.getParentPath("/a/b")); + assertEquals("/a/b", PathUtils.getParentPath("/a/b/c")); + assertEquals("/a/b", PathUtils.getParentPath("a/b/c")); + } + + @Test + public void toStringNodeIdentifier(){ + YangInstanceIdentifier.PathArgument pathArgument = nodeIdentifier(); + + String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"; + + assertEquals(expected , PathUtils.toString(pathArgument)); + } + + @Test + public void toStringAugmentationIdentifier(){ + String expected = "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}"; + + YangInstanceIdentifier.PathArgument pathArgument = augmentationIdentifier(); + + assertEquals(expected, PathUtils.toString(pathArgument)); + } + + @Test + public void toStringNodeWithValue(){ + + YangInstanceIdentifier.PathArgument pathArgument = nodeWithValue(); + + String expected = "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]"; + + assertEquals(expected, PathUtils.toString(pathArgument)); + } + + + @Test + public void toStringNodeIdentifierWithPredicates(){ + + YangInstanceIdentifier.PathArgument pathArgument = nodeIdentifierWithPredicates(); + + String expected = "(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)id=100}]"; + + assertEquals(expected, PathUtils.toString(pathArgument)); + } + + @Test + public void toStringYangInstanceIdentifier(){ + + YangInstanceIdentifier path = + YangInstanceIdentifier.create(nodeIdentifier()) + .node(nodeIdentifierWithPredicates()) + .node(augmentationIdentifier()).node(nodeWithValue()); + + + String expected = "(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)test[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=100}]/" + + "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}/" + + "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]"; + + assertEquals(expected, PathUtils.toString(path)); + + } + + @Test + public void toYangInstanceIdentifier(){ + String expected = "(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)test[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=100}]/" + + "AugmentationIdentifier{childNames=[(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics]}/" + + "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test[100]"; + + YangInstanceIdentifier yangInstanceIdentifier = + PathUtils.toYangInstanceIdentifier(expected); + + String actual = PathUtils.toString(yangInstanceIdentifier); + + assertEquals(expected, actual); + + } + + private YangInstanceIdentifier.NodeIdentifier nodeIdentifier(){ + return new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME); + } + + private YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier(){ + Set childNames = new HashSet(); + childNames.add(QNameFactory.create("(urn:opendaylight:flow:table:statistics?revision=2013-12-15)flow-table-statistics")); + + return new YangInstanceIdentifier.AugmentationIdentifier(childNames); + } + + private YangInstanceIdentifier.NodeWithValue nodeWithValue(){ + return new YangInstanceIdentifier.NodeWithValue(TestModel.TEST_QNAME, Integer.valueOf(100)); + } + + private YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates(){ + Map keys = new HashMap<>(); + + keys.put(TestModel.ID_QNAME, Integer.valueOf(100)); + + return new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.TEST_QNAME, keys); + } +} -- 2.36.6