2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore.node.utils;
11 import com.google.common.base.Splitter;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 public class PathUtils {
20 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
23 * Given a YangInstanceIdentifier return a serialized version of the same
29 public static String toString(YangInstanceIdentifier path) {
30 final Iterator<YangInstanceIdentifier.PathArgument> it =
31 path.getPathArguments().iterator();
36 final StringBuilder sb = new StringBuilder();
38 sb.append(toString(it.next()));
49 * Given a YangInstanceIdentifier.PathArgument return a serialized version
50 * of the same as a String
55 public static String toString(YangInstanceIdentifier.PathArgument pathArgument){
56 if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){
57 return toString((YangInstanceIdentifier.NodeIdentifier) pathArgument);
58 } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
59 return toString((YangInstanceIdentifier.AugmentationIdentifier) pathArgument);
60 } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){
61 return toString((YangInstanceIdentifier.NodeWithValue) pathArgument);
62 } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){
63 return toString((YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument);
66 return pathArgument.toString();
70 * Given a serialized string version of a YangInstanceIdentifier convert
71 * to a YangInstanceIdentifier
76 public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
77 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
78 for (String segment : SLASH_SPLITTER.split(path)) {
79 pathArguments.add(NodeIdentifierFactory.getArgument(segment));
81 return YangInstanceIdentifier.create(pathArguments);
84 private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){
85 return pathArgument.getNodeType().toString();
88 private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){
89 Set<QName> childNames = pathArgument.getPossibleChildNames();
90 final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
91 sb.append("childNames=").append(childNames).append('}');
96 private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){
97 return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]";
100 private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){
101 return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']';