/*
 *
 *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 *  This program and the accompanying materials are made available under the
 *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
 *  and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 */

package org.opendaylight.controller.cluster.datastore.node.utils;

import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class PathUtils {

    /**
     * Given a YangInstanceIdentifier return a serialized version of the same
     * as a String
     *
     * @param path
     * @return
     */
    public static String toString(YangInstanceIdentifier path){
        StringBuilder sb = new StringBuilder();
        Iterator<YangInstanceIdentifier.PathArgument> iterator =
            path.getPathArguments().iterator();

        while(iterator.hasNext()){
            sb.append(toString(iterator.next()));
            if(iterator.hasNext()){
                sb.append("/");
            }
        }
        return sb.toString();
    }

    /**
     * Given a YangInstanceIdentifier.PathArgument return a serialized version
     * of the same as a String
     *
     * @param pathArgument
     * @return
     */
    public static String toString(YangInstanceIdentifier.PathArgument pathArgument){
        if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){
            return toString((YangInstanceIdentifier.NodeIdentifier) pathArgument);
        } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
            return toString((YangInstanceIdentifier.AugmentationIdentifier) pathArgument);
        } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){
            return toString((YangInstanceIdentifier.NodeWithValue) pathArgument);
        } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){
            return toString((YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument);
        }

        return pathArgument.toString();
    }

    /**
     * Given a serialized string version of a YangInstanceIdentifier convert
     * to a YangInstanceIdentifier
     *
     * @param path
     * @return
     */
    public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
        String[] segments = path.split("/");

        List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
        for (String segment : segments) {
            if (!"".equals(segment)) {
                pathArguments.add(NodeIdentifierFactory.getArgument(segment));
            }
        }
        return YangInstanceIdentifier.create(pathArguments);
    }

    private static String toString(YangInstanceIdentifier.NodeIdentifier pathArgument){
        return pathArgument.getNodeType().toString();
    }

    private static String toString(YangInstanceIdentifier.AugmentationIdentifier pathArgument){
        Set<QName> childNames = pathArgument.getPossibleChildNames();
        final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
        sb.append("childNames=").append(childNames).append('}');
        return sb.toString();

    }

    private static String toString(YangInstanceIdentifier.NodeWithValue pathArgument){
        return pathArgument.getNodeType().toString() + "[" + pathArgument.getValue() + "]";
    }

    private static String toString(YangInstanceIdentifier.NodeIdentifierWithPredicates pathArgument){
        return pathArgument.getNodeType().toString() + '[' + pathArgument.getKeyValues() + ']';
    }

}

@@ -390,252 +390,379 @@
 ort 
-org.opendaylight.yangtools.yang.common.QName;%0Aimport org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Apublic class PathUtils %7B
+com.google.common.base.Splitter;%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport org.opendaylight.yangtools.yang.common.QName;%0Aimport org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;%0A%0Apublic class PathUtils %7B%0A    private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
 %0A%0A  
@@ -2619,54 +2619,8 @@
 h)%7B%0A
-        String%5B%5D segments = path.split(%22/%22);%0A%0A
     
@@ -2734,63 +2734,38 @@
 t : 
-segments) %7B%0A            if (!%22%22.equals(segment
+SLASH_SPLITTER.split(path
 )) %7B%0A
-    
     
@@ -2835,30 +2835,16 @@
 ment));%0A
-            %7D%0A
         

Optimize PathUtils.toYangInstanceIdentifier()