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 com.google.common.base.Preconditions;
12 import java.util.HashMap;
13 import java.util.Iterator;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
24 public final class DOMDataTreePrefixTableEntry<V> implements Identifiable<PathArgument> {
25 private static final Logger LOG = LoggerFactory.getLogger(DOMDataTreePrefixTableEntry.class);
26 // FIXME: We do probably want to adapt map
27 private final Map<PathArgument, DOMDataTreePrefixTableEntry<V>> children = new HashMap<>();
28 private final PathArgument identifier;
29 private V value = null;
31 DOMDataTreePrefixTableEntry() {
35 DOMDataTreePrefixTableEntry(final PathArgument identifier) {
36 this.identifier = Preconditions.checkNotNull(identifier);
40 public PathArgument getIdentifier() {
48 DOMDataTreePrefixTableEntry<V> lookup(final YangInstanceIdentifier id) {
49 final Iterator<PathArgument> it = id.getPathArguments().iterator();
50 DOMDataTreePrefixTableEntry<V> entry = this;
51 DOMDataTreePrefixTableEntry<V> lastPresentEntry = entry;
53 while (it.hasNext()) {
54 final PathArgument a = it.next();
55 final DOMDataTreePrefixTableEntry<V> child = entry.children.get(a);
57 LOG.debug("Lookup of {} stopped at {}", id, a);
63 if (child.getValue() != null) {
64 lastPresentEntry = child;
68 return lastPresentEntry;
71 void store(final YangInstanceIdentifier id, final V reg) {
72 final Iterator<PathArgument> it = id.getPathArguments().iterator();
73 DOMDataTreePrefixTableEntry<V> entry = this;
75 while (it.hasNext()) {
76 final PathArgument a = it.next();
77 DOMDataTreePrefixTableEntry<V> child = entry.children.get(a);
79 child = new DOMDataTreePrefixTableEntry<>(a);
80 entry.children.put(a, child);
82 // TODO: Is this correct? We want to enter child
86 Preconditions.checkState(entry.value == null);
90 private boolean remove(final Iterator<PathArgument> it) {
92 final PathArgument arg = it.next();
93 final DOMDataTreePrefixTableEntry<V> child = children.get(arg);
95 if (child.remove(it)) {
99 LOG.warn("Cannot remove non-existent child {}", arg);
103 * Iterator is empty, this effectively means is table entry to remove registration.
104 * FIXME: We probably want to compare value to make sure we are removing correct value.
108 return value == null && children.isEmpty();
111 void remove(final YangInstanceIdentifier id) {
112 this.remove(id.getPathArguments().iterator());