2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
10 import java.util.Collections;
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;
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;
31 public final class StoreUtils {
33 private final static Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
35 public Object apply(final Identifiable<Object> input) {
36 return input.getIdentifier();
40 public static final UnsignedLong increase(final UnsignedLong original) {
41 return original.plus(UnsignedLong.ONE);
44 public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) {
46 return new InstanceIdentifier(ImmutableList.<PathArgument> builder().addAll(parent.getPath()).add(arg).build());
49 public static AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> initialChangeEvent(
50 final InstanceIdentifier path, final StoreMetadataNode data) {
51 return new InitialDataChangeEvent(path, data.getData());
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.
59 @SuppressWarnings({ "unchecked", "rawtypes" })
60 public static <V> Function<Identifiable<V>, V> identifierExtractor() {
61 return (Function) EXTRACT_IDENTIFIER;
64 private static final class InitialDataChangeEvent implements
65 AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
67 private final ImmutableMap<InstanceIdentifier, NormalizedNode<?, ?>> payload;
68 private final NormalizedNode<?, ?> data;
70 public InitialDataChangeEvent(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
71 payload = ImmutableMap.<InstanceIdentifier, NormalizedNode<?, ?>> of(path, data);
76 public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
81 public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
82 return Collections.emptyMap();
86 public NormalizedNode<?, ?> getOriginalSubtree() {
91 public Set<InstanceIdentifier> getRemovedPaths() {
92 return Collections.emptySet();
96 public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
101 public NormalizedNode<?, ?> getUpdatedSubtree() {
106 public static <V> Set<V> toIdentifierSet(final Iterable<? extends Identifiable<V>> children) {
107 return FluentIterable.from(children).transform(StoreUtils.<V> identifierExtractor()).toSet();
110 public static String toStringTree(final StoreMetadataNode metaNode) {
111 StringBuilder builder = new StringBuilder();
112 toStringTree(builder, metaNode, 0);
113 return builder.toString();
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);
125 builder.append(prefix).append('}');
127 builder.append(' ').append(dataNode.getValue());
129 builder.append('\n');
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";
141 return identifier.getNodeType().getLocalName();