String parentPath = "";
if(id != null){
- parentPath = PathUtils.getParentPath(id.toString());
+ parentPath = PathUtils.getParentPath(PathUtils.toString(id));
}
@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;
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();
}
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<YangInstanceIdentifier.PathArgument> 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<YangInstanceIdentifier.PathArgument> 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<QName> 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() + ']';
+ }
+
}
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;
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;
}
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));
- }
- }
- final YangInstanceIdentifier instanceIdentifier =
- YangInstanceIdentifier.create(pathArguments);
- return instanceIdentifier;
+ return PathUtils.toYangInstanceIdentifier(s);
}
-
@Test
public void testNormalizeNodeAttributesToProtoBuffNode() {
final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
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();
--- /dev/null
+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<QName> 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<QName, Object> keys = new HashMap<>();
+
+ keys.put(TestModel.ID_QNAME, Integer.valueOf(100));
+
+ return new YangInstanceIdentifier.NodeIdentifierWithPredicates(TestModel.TEST_QNAME, keys);
+ }
+}