X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2FPathUtils.java;h=930dbceab44b83f98aa54d8a8613ec80d73304f9;hb=4ed6793fcad89fcb4a3c0f7ec230753cb7ed31a9;hp=cf5174319d2128518f70391d0d459ad5af26b2cf;hpb=8e42b08cb626a60919c145b2a46d94114c3905d6;p=controller.git 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 cf5174319d..930dbceab4 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,20 +10,97 @@ package org.opendaylight.controller.cluster.datastore.node.utils; +import com.google.common.base.Splitter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + public class PathUtils { - public static String getParentPath(String currentElementPath){ - String parentPath = ""; - - if(currentElementPath != null){ - String[] parentPaths = currentElementPath.split("/"); - if(parentPaths.length > 2){ - for(int i=0;i 0){ - parentPath += "/" + parentPaths[i]; - } - } + private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings(); + + /** + * Given a YangInstanceIdentifier return a serialized version of the same + * as a String + * + * @param path + * @return + */ + public static String toString(YangInstanceIdentifier path) { + final Iterator it = + path.getPathArguments().iterator(); + if (!it.hasNext()) { + return ""; + } + + final StringBuilder sb = new StringBuilder(); + for (;;) { + sb.append(toString(it.next())); + if (!it.hasNext()) { + break; } + 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){ + List pathArguments = new ArrayList<>(); + for (String segment : SLASH_SPLITTER.split(path)) { + pathArguments.add(NodeIdentifierFactory.getArgument(segment)); } - return parentPath; + 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() + ']'; + } + }