Merge "Bug 2265: Address comments from 12448"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / PathUtils.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node.utils;
12
13 import com.google.common.base.Splitter;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 public class PathUtils {
22     private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
23
24     /**
25      * Given a YangInstanceIdentifier return a serialized version of the same
26      * as a String
27      *
28      * @param path
29      * @return
30      */
31     public static String toString(YangInstanceIdentifier path) {
32         final Iterator<YangInstanceIdentifier.PathArgument> it =
33             path.getPathArguments().iterator();
34         if (!it.hasNext()) {
35             return "";
36         }
37
38         final StringBuilder sb = new StringBuilder();
39         for (;;) {
40             sb.append(toString(it.next()));
41             if (!it.hasNext()) {
42                 break;
43             }
44             sb.append('/');
45         }
46
47         return sb.toString();
48     }
49
50     /**
51      * Given a YangInstanceIdentifier.PathArgument return a serialized version
52      * of the same as a String
53      *
54      * @param pathArgument
55      * @return
56      */
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);
66         }
67
68         return pathArgument.toString();
69     }
70
71     /**
72      * Given a serialized string version of a YangInstanceIdentifier convert
73      * to a YangInstanceIdentifier
74      *
75      * @param path
76      * @return
77      */
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));
82         }
83         return YangInstanceIdentifier.create(pathArguments);
84     }
85
86     private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){
87         return pathArgument.getNodeType().toString();
88     }
89
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('}');
94         return sb.toString();
95
96     }
97
98     private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){
99         return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]";
100     }
101
102     private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){
103         return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']';
104     }
105
106 }