Bug 499: Added support for change listeners.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / StoreUtils.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import java.util.Collections;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
8 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
9 import org.opendaylight.yangtools.concepts.Identifiable;
10 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13
14 import com.google.common.base.Function;
15 import com.google.common.collect.FluentIterable;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.primitives.UnsignedLong;
19
20 public final class StoreUtils {
21
22     private final static Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
23
24         @Override
25         public Object apply(final Identifiable<Object> input) {
26             return input.getIdentifier();
27         }
28     };
29
30     public static final UnsignedLong increase(final UnsignedLong original) {
31         return original.plus(UnsignedLong.ONE);
32     }
33
34     public static final InstanceIdentifier append(final InstanceIdentifier parent, final PathArgument arg) {
35
36         return new InstanceIdentifier(ImmutableList.<PathArgument> builder().addAll(parent.getPath()).add(arg).build());
37     }
38
39     public static AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> initialChangeEvent(
40             final InstanceIdentifier path, final StoreMetadataNode data) {
41         return new InitialDataChangeEvent(path, data.getData());
42     }
43
44     private static final class InitialDataChangeEvent implements
45             AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
46
47         private final ImmutableMap<InstanceIdentifier, NormalizedNode<?, ?>> payload;
48         private final NormalizedNode<?, ?> data;
49
50         public InitialDataChangeEvent(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
51             payload = ImmutableMap.<InstanceIdentifier, NormalizedNode<?, ?>> of(path, data);
52             this.data = data;
53         }
54
55         @Override
56         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
57             return payload;
58         }
59
60         @Override
61         public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
62             return Collections.emptyMap();
63         }
64
65         @Override
66         public NormalizedNode<?, ?> getOriginalSubtree() {
67             return null;
68         }
69
70         @Override
71         public Set<InstanceIdentifier> getRemovedPaths() {
72             return Collections.emptySet();
73         }
74
75         @Override
76         public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
77             return payload;
78         }
79
80         @Override
81         public NormalizedNode<?, ?> getUpdatedSubtree() {
82             return data;
83         }
84
85     }
86
87     @SuppressWarnings({ "unchecked", "rawtypes" })
88     public static <V> Function<Identifiable<V>,V> identifierExtractor() {
89         return (Function) EXTRACT_IDENTIFIER;
90     }
91
92     public static <V> Set<V> toIdentifierSet(final Iterable<? extends Identifiable<V>> children) {
93         return FluentIterable.from(children).transform(StoreUtils.<V>identifierExtractor()).toSet();
94     }
95
96 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.