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