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.controller.md.sal.dom.broker.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.Iterator;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 final class ShardingTableEntry implements Identifiable<PathArgument> {
21 private static final Logger LOG = LoggerFactory.getLogger(ShardingTableEntry.class);
22 private final Map<PathArgument, ShardingTableEntry> children = Collections.emptyMap();
23 private final PathArgument identifier;
24 private ShardRegistration<?> registration;
26 ShardingTableEntry() {
30 ShardingTableEntry(final PathArgument identifier) {
31 this.identifier = Preconditions.checkNotNull(identifier);
35 public PathArgument getIdentifier() {
39 public ShardRegistration<?> getRegistration() {
43 ShardingTableEntry lookup(final YangInstanceIdentifier id) {
44 final Iterator<PathArgument> it = id.getPathArguments().iterator();
45 ShardingTableEntry entry = this;
47 while (it.hasNext()) {
48 final PathArgument a = it.next();
49 final ShardingTableEntry child = entry.children.get(a);
51 LOG.debug("Lookup of {} stopped at {}", id, a);
61 void store(final YangInstanceIdentifier id, final ShardRegistration<?> reg) {
62 final Iterator<PathArgument> it = id.getPathArguments().iterator();
63 ShardingTableEntry entry = this;
65 while (it.hasNext()) {
66 final PathArgument a = it.next();
67 ShardingTableEntry child = entry.children.get(a);
69 child = new ShardingTableEntry(a);
70 entry.children.put(a, child);
74 Preconditions.checkState(entry.registration == null);
75 entry.registration = reg;
78 private boolean remove(final Iterator<PathArgument> it) {
80 final PathArgument arg = it.next();
81 final ShardingTableEntry child = children.get(arg);
83 if (child.remove(it)) {
87 LOG.warn("Cannot remove non-existent child {}", arg);
91 return registration == null && children.isEmpty();
94 void remove(final YangInstanceIdentifier id) {
95 this.remove(id.getPathArguments().iterator());