X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2Ftree%2FStoreUtils.java;fp=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2Ftree%2FStoreUtils.java;h=7e783f927d95d9be9f92041074cf6769ef582bac;hb=8d160966fa8752235d01bb8dc57c11391b86f187;hp=0000000000000000000000000000000000000000;hpb=3f00001b2652f56ba9fc73681aeac27cb0e0d7df;p=controller.git diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreUtils.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreUtils.java new file mode 100644 index 0000000000..7e783f927d --- /dev/null +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/StoreUtils.java @@ -0,0 +1,98 @@ +/* + * 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.md.sal.dom.store.impl.tree; + +import java.util.Set; + +import org.opendaylight.yangtools.concepts.Identifiable; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; + +import com.google.common.base.Function; +import com.google.common.base.Strings; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.primitives.UnsignedLong; + +public final class StoreUtils { + private static final int STRINGTREE_INDENT = 4; + + private final static Function, Object> EXTRACT_IDENTIFIER = new Function, Object>() { + @Override + public Object apply(final Identifiable input) { + return input.getIdentifier(); + } + }; + + private StoreUtils() { + throw new UnsupportedOperationException("Utility class should not be instantiated"); + } + + /* + * Suppressing warnings here allows us to fool the compiler enough + * such that we can reuse a single function for all applicable types + * and present it in a type-safe manner to our users. + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static Function, V> identifierExtractor() { + return (Function) EXTRACT_IDENTIFIER; + } + + public static final UnsignedLong increase(final UnsignedLong original) { + return original.plus(UnsignedLong.ONE); + } + + public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) { + return new InstanceIdentifier(ImmutableList. builder().addAll(parent.getPath()).add(arg).build()); + } + + public static Set toIdentifierSet(final Iterable> children) { + return FluentIterable.from(children).transform(StoreUtils. identifierExtractor()).toSet(); + } + + public static String toStringTree(final NormalizedNode node) { + StringBuilder builder = new StringBuilder(); + toStringTree(builder, node, 0); + return builder.toString(); + } + + private static void toStringTree(final StringBuilder builder, final NormalizedNode node, final int offset) { + final String prefix = Strings.repeat(" ", offset); + + builder.append(prefix).append(toStringTree(node.getIdentifier())); + if (node instanceof NormalizedNodeContainer) { + final NormalizedNodeContainer container = (NormalizedNodeContainer) node; + + builder.append(" {\n"); + for (NormalizedNode child : container.getValue()) { + toStringTree(builder, child, offset + STRINGTREE_INDENT); + } + + builder.append(prefix).append('}'); + } else { + builder.append(' ').append(node.getValue()); + } + builder.append('\n'); + } + + private static String toStringTree(final PathArgument identifier) { + if (identifier instanceof NodeIdentifierWithPredicates) { + StringBuilder builder = new StringBuilder(); + builder.append(identifier.getNodeType().getLocalName()); + builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values()); + return builder.toString(); + } else if (identifier instanceof AugmentationIdentifier) { + return "augmentation"; + } + return identifier.getNodeType().getLocalName(); + } +}