1fdbb6d2e42e18f6173485c994a25d22ff9651dd
[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.computeIfAbsent(a, ShardingTableEntry::new);
68         }
69
70         Preconditions.checkState(entry.registration == null);
71         entry.registration = reg;
72     }
73
74     private boolean remove(final Iterator<PathArgument> it) {
75         if (it.hasNext()) {
76             final PathArgument arg = it.next();
77             final ShardingTableEntry child = children.get(arg);
78             if (child != null) {
79                 if (child.remove(it)) {
80                     children.remove(arg);
81                 }
82             } else {
83                 LOG.warn("Cannot remove non-existent child {}", arg);
84             }
85         }
86
87         return registration == null && children.isEmpty();
88     }
89
90     void remove(final YangInstanceIdentifier id) {
91         this.remove(id.getPathArguments().iterator());
92     }
93 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.