e1da9a738140c073fa26f4cf5d25d84d6d58c63a
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / StoreUtils.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.dom.store.impl;
9
10 import java.util.Set;
11
12 import org.opendaylight.yangtools.concepts.Identifiable;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19
20 import com.google.common.base.Function;
21 import com.google.common.base.Strings;
22 import com.google.common.collect.FluentIterable;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.primitives.UnsignedLong;
25
26 public final class StoreUtils {
27     private static final int STRINGTREE_INDENT = 4;
28
29     private final static Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
30         @Override
31         public Object apply(final Identifiable<Object> input) {
32             return input.getIdentifier();
33         }
34     };
35
36     private StoreUtils() {
37         throw new UnsupportedOperationException("Utility class should not be instantiated");
38     }
39
40     /*
41      * Suppressing warnings here allows us to fool the compiler enough
42      * such that we can reuse a single function for all applicable types
43      * and present it in a type-safe manner to our users.
44      */
45     @SuppressWarnings({ "unchecked", "rawtypes" })
46     public static <V> Function<Identifiable<V>, V> identifierExtractor() {
47         return (Function) EXTRACT_IDENTIFIER;
48     }
49
50     public static final UnsignedLong increase(final UnsignedLong original) {
51         return original.plus(UnsignedLong.ONE);
52     }
53
54     public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) {
55         return new InstanceIdentifier(ImmutableList.<PathArgument> builder().addAll(parent.getPath()).add(arg).build());
56     }
57
58     public static <V> Set<V> toIdentifierSet(final Iterable<? extends Identifiable<V>> children) {
59         return FluentIterable.from(children).transform(StoreUtils.<V> identifierExtractor()).toSet();
60     }
61
62     public static String toStringTree(final NormalizedNode<?, ?> node) {
63         StringBuilder builder = new StringBuilder();
64         toStringTree(builder, node, 0);
65         return builder.toString();
66     }
67
68     private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
69         final String prefix = Strings.repeat(" ", offset);
70
71         builder.append(prefix).append(toStringTree(node.getIdentifier()));
72         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
73             final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
74
75             builder.append(" {\n");
76             for (NormalizedNode<?, ?> child : container.getValue()) {
77                 toStringTree(builder, child, offset + STRINGTREE_INDENT);
78             }
79
80             builder.append(prefix).append('}');
81         } else {
82             builder.append(' ').append(node.getValue());
83         }
84         builder.append('\n');
85     }
86
87     private static String toStringTree(final PathArgument identifier) {
88         if (identifier instanceof NodeIdentifierWithPredicates) {
89             StringBuilder builder = new StringBuilder();
90             builder.append(identifier.getNodeType().getLocalName());
91             builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values());
92             return builder.toString();
93         } else if (identifier instanceof AugmentationIdentifier) {
94             return "augmentation";
95         }
96         return identifier.getNodeType().getLocalName();
97     }
98 }