Removed sonar warnings.
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / RegistrationTreeNode.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.mdsal.dom.spi;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.lang.ref.Reference;
13 import java.lang.ref.WeakReference;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.concepts.Identifiable;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * This is a single node within the registration tree. Note that the data returned from
30  * and instance of this class is guaranteed to have any relevance or consistency
31  * only as long as the {@link RegistrationTreeSnapshot} instance through which it is reached
32  * remains unclosed.
33  *
34  * @param <T> registration type
35  * @author Robert Varga
36  */
37 public final class RegistrationTreeNode<T> implements Identifiable<PathArgument> {
38     private static final Logger LOG = LoggerFactory.getLogger(RegistrationTreeNode.class);
39
40     private final Map<PathArgument, RegistrationTreeNode<T>> children = new HashMap<>();
41     private final Collection<T> registrations = new ArrayList<>(2);
42     private final Collection<T> publicRegistrations = Collections.unmodifiableCollection(registrations);
43     private final Reference<RegistrationTreeNode<T>> parent;
44     private final PathArgument identifier;
45
46     RegistrationTreeNode(final RegistrationTreeNode<T> parent, final PathArgument identifier) {
47         this.parent = new WeakReference<>(parent);
48         this.identifier = identifier;
49     }
50
51     @Override
52     public PathArgument getIdentifier() {
53         return identifier;
54     }
55
56     /**
57      * Return the child matching a {@link PathArgument} specification.
58      *
59      * @param arg Child identifier
60      * @return Child matching exactly, or null.
61      */
62     public RegistrationTreeNode<T> getExactChild(@Nonnull final PathArgument arg) {
63         return children.get(Preconditions.checkNotNull(arg));
64     }
65
66     /**
67      * Return a collection children which match a {@link PathArgument} specification inexactly.
68      * This explicitly excludes the child returned by {@link #getExactChild(PathArgument)}.
69      *
70      * @param arg Child identifier
71      * @return Collection of children, guaranteed to be non-null.
72      */
73     @Nonnull
74     public Collection<RegistrationTreeNode<T>> getInexactChildren(@Nonnull final PathArgument arg) {
75         Preconditions.checkNotNull(arg);
76         if (arg instanceof NodeWithValue || arg instanceof NodeIdentifierWithPredicates) {
77             /*
78              * TODO: This just all-or-nothing wildcards, which we have historically supported. Given
79              *       that the argument is supposed to have all the elements filled out, we could support
80              *       partial wildcards by iterating over the registrations and matching the maps for
81              *       partial matches.
82              */
83             final RegistrationTreeNode<T> child = children.get(new NodeIdentifier(arg.getNodeType()));
84             if (child == null) {
85                 return Collections.emptyList();
86             } else {
87                 return Collections.singletonList(child);
88             }
89         } else {
90             return Collections.emptyList();
91         }
92     }
93
94     public Collection<T> getRegistrations() {
95         return publicRegistrations;
96     }
97
98     RegistrationTreeNode<T> ensureChild(@Nonnull final PathArgument child) {
99         RegistrationTreeNode<T> potential = children.get(Preconditions.checkNotNull(child));
100         if (potential == null) {
101             potential = new RegistrationTreeNode<>(this, child);
102             children.put(child, potential);
103         }
104         return potential;
105     }
106
107     void addRegistration(@Nonnull final T registration) {
108         registrations.add(Preconditions.checkNotNull(registration));
109         LOG.debug("Registration {} added", registration);
110     }
111
112     void removeRegistration(@Nonnull final T registration) {
113         registrations.remove(Preconditions.checkNotNull(registration));
114         LOG.debug("Registration {} removed", registration);
115
116         // We have been called with the write-lock held, so we can perform some cleanup.
117         removeThisIfUnused();
118     }
119
120     private void removeThisIfUnused() {
121         final RegistrationTreeNode<T> p = parent.get();
122         if (p != null && registrations.isEmpty() && children.isEmpty()) {
123             p.removeChild(identifier);
124         }
125     }
126
127     private void removeChild(final PathArgument arg) {
128         children.remove(arg);
129         removeThisIfUnused();
130     }
131
132     @Override
133     public String toString() {
134         return MoreObjects.toStringHelper(this)
135                 .add("identifier", identifier)
136                 .add("registrations", registrations.size())
137                 .add("children", children.size()).toString();
138     }
139 }