3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 package org.opendaylight.controller.cluster.datastore.node.utils;
13 import com.google.common.base.Splitter;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 public class PathUtils {
22 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
25 * Given a YangInstanceIdentifier return a serialized version of the same
31 public static String toString(YangInstanceIdentifier path) {
32 final Iterator<YangInstanceIdentifier.PathArgument> it =
33 path.getPathArguments().iterator();
38 final StringBuilder sb = new StringBuilder();
40 sb.append(toString(it.next()));
51 * Given a YangInstanceIdentifier.PathArgument return a serialized version
52 * of the same as a String
57 public static String toString(YangInstanceIdentifier.PathArgument pathArgument){
58 if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){
59 return toString((YangInstanceIdentifier.NodeIdentifier) pathArgument);
60 } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
61 return toString((YangInstanceIdentifier.AugmentationIdentifier) pathArgument);
62 } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){
63 return toString((YangInstanceIdentifier.NodeWithValue) pathArgument);
64 } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){
65 return toString((YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument);
68 return pathArgument.toString();
72 * Given a serialized string version of a YangInstanceIdentifier convert
73 * to a YangInstanceIdentifier
78 public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
79 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
80 for (String segment : SLASH_SPLITTER.split(path)) {
81 pathArguments.add(NodeIdentifierFactory.getArgument(segment));
83 return YangInstanceIdentifier.create(pathArguments);
86 private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){
87 return pathArgument.getNodeType().toString();
90 private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){
91 Set<QName> childNames = pathArgument.getPossibleChildNames();
92 final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
93 sb.append("childNames=").append(childNames).append('}');
98 private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){
99 return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]";
102 private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){
103 return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']';