X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2FPathUtils.java;h=588fc648fd79f511cf617d3ddd504cf78f168906;hp=25b65f01681ac4b9dcaa6354ec001c85bc531981;hb=a58c23b491f665e6d5449e97d430a7e15d1ecda6;hpb=56a9f42fe2b4a62cd2fed644e5c71d7a21176c7f 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 25b65f0168..588fc648fd 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 @@ -1,108 +1,95 @@ /* + * 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.utils; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; - +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; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; public class PathUtils { + private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings(); - public static String getParentPath(String currentElementPath){ - StringBuilder parentPath = new StringBuilder(); - - if(currentElementPath != null){ - String[] parentPaths = currentElementPath.split("/"); - if(parentPaths.length > 2){ - for(int i=0;i 0){ - parentPath.append("/"); - parentPath.append(parentPaths[i]); - } - } - } + /** + * Given a serialized string version of a YangInstanceIdentifier convert + * to a YangInstanceIdentifier. + * + * @param path the path + * @return a YangInstanceIdentifier + */ + public static YangInstanceIdentifier toYangInstanceIdentifier(String path) { + List pathArguments = new ArrayList<>(); + for (String segment : SLASH_SPLITTER.split(path)) { + pathArguments.add(NodeIdentifierFactory.getArgument(segment)); } - return parentPath.toString(); + return YangInstanceIdentifier.create(pathArguments); } /** * Given a YangInstanceIdentifier return a serialized version of the same - * as a String + * as a String. * - * @param path - * @return + * @param path the path + * @return the path as a String */ - public static String toString(YangInstanceIdentifier path){ - StringBuilder sb = new StringBuilder(); - Iterator iterator = - path.getPathArguments().iterator(); + public static String toString(YangInstanceIdentifier path) { + final Iterator it = path.getPathArguments().iterator(); + if (!it.hasNext()) { + return ""; + } - while(iterator.hasNext()){ - sb.append(toString(iterator.next())); - if(iterator.hasNext()){ - sb.append("/"); + 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 + * of the same as a String. * - * @param pathArgument - * @return + * @param pathArgument the path argument + * @return the path argument as a String */ - 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); + public static String toString(PathArgument pathArgument) { + if (pathArgument instanceof NodeIdentifier) { + return toString((NodeIdentifier) pathArgument); + } else if (pathArgument instanceof AugmentationIdentifier) { + return toString((AugmentationIdentifier) pathArgument); + } else if (pathArgument instanceof NodeWithValue) { + return toString((NodeWithValue) pathArgument); + } else if (pathArgument instanceof NodeIdentifierWithPredicates) { + return toString((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){ + private static String toString(NodeIdentifier pathArgument) { return pathArgument.getNodeType().toString(); } - private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){ + private static String toString(AugmentationIdentifier pathArgument) { Set childNames = pathArgument.getPossibleChildNames(); final StringBuilder sb = new StringBuilder("AugmentationIdentifier{"); sb.append("childNames=").append(childNames).append('}'); @@ -110,12 +97,11 @@ public class PathUtils { } - private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){ + private static String toString(NodeWithValue pathArgument) { return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]"; } - private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){ + private static String toString(NodeIdentifierWithPredicates pathArgument) { return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']'; } - }