2 * Copyright (c) 2015 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.mdsal.dom.spi;
10 import com.google.common.annotations.Beta;
11 import java.util.EnumMap;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.NotThreadSafe;
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;
22 * Prefix table indexed by {@link DOMDataTreeIdentifier}.
23 * Stores values in tree and provides lookup of closest ancestor
25 * @param <V> Value type
29 public final class DOMDataTreePrefixTable<V> {
31 private static final Logger LOG = LoggerFactory.getLogger(DOMDataTreePrefixTable.class);
32 private final Map<LogicalDatastoreType, DOMDataTreePrefixTableEntry<V>> roots =
33 new EnumMap<>(LogicalDatastoreType.class);
35 private DOMDataTreePrefixTable() {
39 public static <V> DOMDataTreePrefixTable<V> create() {
40 return new DOMDataTreePrefixTable<>();
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.
47 * @param prefix Prefix for lookup
48 * @return closest non-null entry towards root or null if no entry towards root exists.
51 public DOMDataTreePrefixTableEntry<V> lookup(@Nonnull final DOMDataTreeIdentifier prefix) {
52 final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
57 return t.lookup(prefix.getRootIdentifier());
61 * Stores value associated to the prefix.
63 * @param prefix DOM prefix of value
64 * @param value Value to be stored
65 * @throws IllegalStateException If value is already stored for provided prefix
67 public void store(@Nonnull final DOMDataTreeIdentifier prefix, @Nonnull final V value) {
68 DOMDataTreePrefixTableEntry<V> domDataTreePrefixTableEntry = roots.get(prefix.getDatastoreType());
69 if (domDataTreePrefixTableEntry == null) {
70 domDataTreePrefixTableEntry = new DOMDataTreePrefixTableEntry<>();
71 roots.put(prefix.getDatastoreType(), domDataTreePrefixTableEntry);
74 domDataTreePrefixTableEntry.store(prefix.getRootIdentifier(), value);
78 * Removes value associated to the prefix.
79 * Value is removed only and only if full prefix match for stored value. Removal of prefix does
80 * not remove child prefixes.
82 * @param prefix to be removed
84 public void remove(@Nonnull final DOMDataTreeIdentifier prefix) {
85 final DOMDataTreePrefixTableEntry<V> t = roots.get(prefix.getDatastoreType());
87 LOG.warn("Shard registration {} points to non-existent table", t);
91 t.remove(prefix.getRootIdentifier());