Merge "BUG-2288: deprecate old binding Notification API elements"
[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 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;
25
26     ShardingTableEntry() {
27         identifier = null;
28     }
29
30     ShardingTableEntry(final PathArgument identifier) {
31         this.identifier = Preconditions.checkNotNull(identifier);
32     }
33
34     @Override
35     public PathArgument getIdentifier() {
36         return identifier;
37     }
38
39     public ShardRegistration<?> getRegistration() {
40         return registration;
41     }
42
43     ShardingTableEntry lookup(final YangInstanceIdentifier id) {
44         final Iterator<PathArgument> it = id.getPathArguments().iterator();
45         ShardingTableEntry entry = this;
46
47         while (it.hasNext()) {
48             final PathArgument a = it.next();
49             final ShardingTableEntry child = entry.children.get(a);
50             if (child == null) {
51                 LOG.debug("Lookup of {} stopped at {}", id, a);
52                 break;
53             }
54
55             entry = child;
56         }
57
58         return entry;
59     }
60
61     void store(final YangInstanceIdentifier id, final ShardRegistration<?> reg) {
62         final Iterator<PathArgument> it = id.getPathArguments().iterator();
63         ShardingTableEntry entry = this;
64
65         while (it.hasNext()) {
66             final PathArgument a = it.next();
67             ShardingTableEntry child = entry.children.get(a);
68             if (child == null) {
69                 child = new ShardingTableEntry(a);
70                 entry.children.put(a, child);
71             }
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 }