Merge "BUG-704 Remove pax from netconf identity-ref test."
[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.Collections;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23
24 import com.google.common.base.Function;
25 import com.google.common.base.Strings;
26 import com.google.common.collect.FluentIterable;
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.primitives.UnsignedLong;
30
31 public final class StoreUtils {
32
33     private final static Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
34         @Override
35         public Object apply(final Identifiable<Object> input) {
36             return input.getIdentifier();
37         }
38     };
39
40     public static final UnsignedLong increase(final UnsignedLong original) {
41         return original.plus(UnsignedLong.ONE);
42     }
43
44     public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) {
45
46         return new InstanceIdentifier(ImmutableList.<PathArgument> builder().addAll(parent.getPath()).add(arg).build());
47     }
48
49     public static AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> initialChangeEvent(
50             final InstanceIdentifier path, final StoreMetadataNode data) {
51         return new InitialDataChangeEvent(path, data.getData());
52     }
53
54     /*
55      * Suppressing warnings here allows us to fool the compiler enough
56      * such that we can reuse a single function for all applicable types
57      * and present it in a type-safe manner to our users.
58      */
59     @SuppressWarnings({ "unchecked", "rawtypes" })
60     public static <V> Function<Identifiable<V>, V> identifierExtractor() {
61         return (Function) EXTRACT_IDENTIFIER;
62     }
63
64     private static final class InitialDataChangeEvent implements
65             AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
66
67         private final ImmutableMap<InstanceIdentifier, NormalizedNode<?, ?>> payload;
68         private final NormalizedNode<?, ?> data;
69
70         public InitialDataChangeEvent(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
71             payload = ImmutableMap.<InstanceIdentifier, NormalizedNode<?, ?>> of(path, data);
72             this.data = data;
73         }
74
75         @Override
76         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
77             return payload;
78         }
79
80         @Override
81         public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
82             return Collections.emptyMap();
83         }
84
85         @Override
86         public NormalizedNode<?, ?> getOriginalSubtree() {
87             return null;
88         }
89
90         @Override
91         public Set<InstanceIdentifier> getRemovedPaths() {
92             return Collections.emptySet();
93         }
94
95         @Override
96         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
97             return payload;
98         }
99
100         @Override
101         public NormalizedNode<?, ?> getUpdatedSubtree() {
102             return data;
103         }
104     }
105
106     public static <V> Set<V> toIdentifierSet(final Iterable<? extends Identifiable<V>> children) {
107         return FluentIterable.from(children).transform(StoreUtils.<V> identifierExtractor()).toSet();
108     }
109
110     public static String toStringTree(final StoreMetadataNode metaNode) {
111         StringBuilder builder = new StringBuilder();
112         toStringTree(builder, metaNode, 0);
113         return builder.toString();
114     }
115
116     private static void toStringTree(final StringBuilder builder, final StoreMetadataNode metaNode, final int offset) {
117         String prefix = Strings.repeat(" ", offset);
118         builder.append(prefix).append(toStringTree(metaNode.getIdentifier()));
119         NormalizedNode<?, ?> dataNode = metaNode.getData();
120         if (dataNode instanceof NormalizedNodeContainer<?, ?, ?>) {
121             builder.append(" {\n");
122             for (StoreMetadataNode child : metaNode.getChildren()) {
123                 toStringTree(builder, child, offset + 4);
124             }
125             builder.append(prefix).append('}');
126         } else {
127             builder.append(' ').append(dataNode.getValue());
128         }
129         builder.append('\n');
130     }
131
132     private static String toStringTree(final PathArgument identifier) {
133         if (identifier instanceof NodeIdentifierWithPredicates) {
134             StringBuilder builder = new StringBuilder();
135             builder.append(identifier.getNodeType().getLocalName());
136             builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values());
137             return builder.toString();
138         } else if (identifier instanceof AugmentationIdentifier) {
139             return "augmentation";
140         }
141         return identifier.getNodeType().getLocalName();
142     }
143 }