Merge "BUG-1568 Remove ganymed Ssh client adapter"
[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 org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Set;
20
21 public class PathUtils {
22
23     public static String getParentPath(String currentElementPath){
24         StringBuilder parentPath = new StringBuilder();
25
26         if(currentElementPath != null){
27             String[] parentPaths = currentElementPath.split("/");
28             if(parentPaths.length > 2){
29                 for(int i=0;i<parentPaths.length-1;i++){
30                     if(parentPaths[i].length() > 0){
31                         parentPath.append("/");
32                         parentPath.append(parentPaths[i]);
33                     }
34                 }
35             }
36         }
37         return parentPath.toString();
38     }
39
40     /**
41      * Given a YangInstanceIdentifier return a serialized version of the same
42      * as a String
43      *
44      * @param path
45      * @return
46      */
47     public static String toString(YangInstanceIdentifier path){
48         StringBuilder sb = new StringBuilder();
49         Iterator<YangInstanceIdentifier.PathArgument> iterator =
50             path.getPathArguments().iterator();
51
52         while(iterator.hasNext()){
53             sb.append(toString(iterator.next()));
54             if(iterator.hasNext()){
55                 sb.append("/");
56             }
57         }
58         return sb.toString();
59     }
60
61     /**
62      * Given a YangInstanceIdentifier.PathArgument return a serialized version
63      * of the same as a String
64      *
65      * @param pathArgument
66      * @return
67      */
68     public static String toString(YangInstanceIdentifier.PathArgument pathArgument){
69         if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){
70             return toString((YangInstanceIdentifier.NodeIdentifier) pathArgument);
71         } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
72             return toString((YangInstanceIdentifier.AugmentationIdentifier) pathArgument);
73         } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){
74             return toString((YangInstanceIdentifier.NodeWithValue) pathArgument);
75         } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){
76             return toString((YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument);
77         }
78
79         return pathArgument.toString();
80     }
81
82     /**
83      * Given a serialized string version of a YangInstanceIdentifier convert
84      * to a YangInstanceIdentifier
85      *
86      * @param path
87      * @return
88      */
89     public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
90         String[] segments = path.split("/");
91
92         List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
93         for (String segment : segments) {
94             if (!"".equals(segment)) {
95                 pathArguments.add(NodeIdentifierFactory.getArgument(segment));
96             }
97         }
98         return YangInstanceIdentifier.create(pathArguments);
99     }
100
101     private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){
102         return pathArgument.getNodeType().toString();
103     }
104
105     private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){
106         Set<QName> childNames = pathArgument.getPossibleChildNames();
107         final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
108         sb.append("childNames=").append(childNames).append('}');
109         return sb.toString();
110
111     }
112
113     private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){
114         return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]";
115     }
116
117     private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){
118         return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']';
119     }
120
121 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.