3990c7fb0babdc0be624ab9988f53ba08a369d8c
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / ShardingTableEntry.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.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.Map;
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;
19
20 /**
21  * @deprecated To be removed with {@link ShardedDOMDataTree}.
22  */
23 @Deprecated
24 final class ShardingTableEntry implements Identifiable<PathArgument> {
25     private static final Logger LOG = LoggerFactory.getLogger(ShardingTableEntry.class);
26     private final Map<PathArgument, ShardingTableEntry> children = Collections.emptyMap();
27     private final PathArgument identifier;
28     private ShardRegistration<?> registration;
29
30     ShardingTableEntry() {
31         identifier = null;
32     }
33
34     ShardingTableEntry(final PathArgument identifier) {
35         this.identifier = Preconditions.checkNotNull(identifier);
36     }
37
38     @Override
39     public PathArgument getIdentifier() {
40         return identifier;
41     }
42
43     public ShardRegistration<?> getRegistration() {
44         return registration;
45     }
46
47     ShardingTableEntry lookup(final YangInstanceIdentifier id) {
48         final Iterator<PathArgument> it = id.getPathArguments().iterator();
49         ShardingTableEntry entry = this;
50
51         while (it.hasNext()) {
52             final PathArgument a = it.next();
53             final ShardingTableEntry child = entry.children.get(a);
54             if (child == null) {
55                 LOG.debug("Lookup of {} stopped at {}", id, a);
56                 break;
57             }
58
59             entry = child;
60         }
61
62         return entry;
63     }
64
65     void store(final YangInstanceIdentifier id, final ShardRegistration<?> reg) {
66         final Iterator<PathArgument> it = id.getPathArguments().iterator();
67         ShardingTableEntry entry = this;
68
69         while (it.hasNext()) {
70             final PathArgument a = it.next();
71             ShardingTableEntry child = entry.children.computeIfAbsent(a, ShardingTableEntry::new);
72         }
73
74         Preconditions.checkState(entry.registration == null);
75         entry.registration = reg;
76     }
77
78     private boolean remove(final Iterator<PathArgument> it) {
79         if (it.hasNext()) {
80             final PathArgument arg = it.next();
81             final ShardingTableEntry child = children.get(arg);
82             if (child != null) {
83                 if (child.remove(it)) {
84                     children.remove(arg);
85                 }
86             } else {
87                 LOG.warn("Cannot remove non-existent child {}", arg);
88             }
89         }
90
91         return registration == null && children.isEmpty();
92     }
93
94     void remove(final YangInstanceIdentifier id) {
95         this.remove(id.getPathArguments().iterator());
96     }
97 }