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

©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.