107b8123c386345fe1457a3a38b6b1c0fb3d10a2
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DOMDataTreePrefixTable.java
1 /*
2  * Copyright (c) 2015 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.mdsal.dom.spi;
9
10 import com.google.common.annotations.Beta;
11 import java.util.EnumMap;
12 import java.util.Map;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Prefix table indexed by {@link DOMDataTreeIdentifier}.
23  * Stores values in tree and provides lookup of closest ancestor
24  *
25  * @param <V> Value type
26  */
27 @Beta
28 @NotThreadSafe
29 public final class DOMDataTreePrefixTable<V> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(DOMDataTreePrefixTable.class);
32     private final Map<LogicalDatastoreType, DOMDataTreePrefixTableEntry<V>> roots =
33         new EnumMap<>(LogicalDatastoreType.class);
34
35     private DOMDataTreePrefixTable() {
36
37     }
38
39     public static <V> DOMDataTreePrefixTable<V> create() {
40         return new DOMDataTreePrefixTable<>();
41     }
42
43     /**
44      * Lookups entry by provided {@link DOMDataTreeIdentifier}, if entry is not present returns
45      * closest non-null entry towards root or null if no entry towards root exists.
46      *
47      * @param prefix Prefix for lookup
48      * @return closest non-null entry towards root or null if no entry towards root exists.
49      */
50     public @Nullable DOMDataTreePrefixTableEntry<V> lookup(final @NonNull DOMDataTreeIdentifier prefix) {
51         final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
52         return t == null ? null : t.lookup(prefix.getRootIdentifier());
53     }
54
55     /**
56      * Stores value associated to the prefix.
57      *
58      * @param prefix DOM prefix of value
59      * @param value Value to be stored
60      * @throws IllegalStateException If value is already stored for provided prefix
61      */
62     public void store(final @NonNull DOMDataTreeIdentifier prefix, final @NonNull V value) {
63         DOMDataTreePrefixTableEntry<V> domDataTreePrefixTableEntry = roots.get(prefix.getDatastoreType());
64         if (domDataTreePrefixTableEntry == null) {
65             domDataTreePrefixTableEntry = new DOMDataTreePrefixTableEntry<>();
66             roots.put(prefix.getDatastoreType(), domDataTreePrefixTableEntry);
67         }
68
69         domDataTreePrefixTableEntry.store(prefix.getRootIdentifier(), value);
70     }
71
72     /**
73      * Removes value associated to the prefix.
74      * Value is removed only and only if full prefix match for stored value. Removal of prefix does
75      * not remove child prefixes.
76      *
77      * @param prefix to be removed
78      */
79     public void remove(final @NonNull DOMDataTreeIdentifier prefix) {
80         final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
81         if (t == null) {
82             LOG.warn("Shard registration {} points to non-existent table", t);
83             return;
84         }
85
86         t.remove(prefix.getRootIdentifier());
87     }
88 }