Deprecate org.opendaylight.controller.md.sal.dom.spi.AbstractRegistrationTree
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / md / sal / 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.controller.md.sal.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  * @deprecated Use {@link org.opendaylight.mdsal.dom.spi.RegistrationTreeNode} instead.
38  */
39 @Deprecated
40 public final class RegistrationTreeNode<T> implements Identifiable<PathArgument> {
41     private static final Logger LOG = LoggerFactory.getLogger(RegistrationTreeNode.class);
42
43     private final Map<PathArgument, RegistrationTreeNode<T>> children = new HashMap<>();
44     private final Collection<T> registrations = new ArrayList<>(2);
45     private final Collection<T> publicRegistrations = Collections.unmodifiableCollection(registrations);
46     private final Reference<RegistrationTreeNode<T>> parent;
47     private final PathArgument identifier;
48
49     RegistrationTreeNode(final RegistrationTreeNode<T> parent, final PathArgument identifier) {
50         this.parent = new WeakReference<>(parent);
51         this.identifier = identifier;
52     }
53
54     @Override
55     public PathArgument getIdentifier() {
56         return identifier;
57     }
58
59     /**
60      * Return the child matching a {@link PathArgument} specification.
61      *
62      * @param arg Child identifier
63      * @return Child matching exactly, or null.
64      */
65     public RegistrationTreeNode<T> getExactChild(@Nonnull final PathArgument arg) {
66         return children.get(Preconditions.checkNotNull(arg));
67     }
68
69     /**
70      * Return a collection children which match a {@link PathArgument} specification inexactly.
71      * This explicitly excludes the child returned by {@link #getExactChild(PathArgument)}.
72      *
73      * @param arg Child identifier
74      * @return Collection of children, guaranteed to be non-null.
75      */
76     public @Nonnull Collection<RegistrationTreeNode<T>> getInexactChildren(@Nonnull final PathArgument arg) {
77         Preconditions.checkNotNull(arg);
78         if (arg instanceof NodeWithValue || arg instanceof NodeIdentifierWithPredicates) {
79             /*
80              * TODO: This just all-or-nothing wildcards, which we have historically supported. Given
81              *       that the argument is supposed to have all the elements filled out, we could support
82              *       partial wildcards by iterating over the registrations and matching the maps for
83              *       partial matches.
84              */
85             final RegistrationTreeNode<T> child = children.get(new NodeIdentifier(arg.getNodeType()));
86             if (child == null) {
87                 return Collections.emptyList();
88             } else {
89                 return Collections.singletonList(child);
90             }
91         } else {
92             return Collections.emptyList();
93         }
94     }
95
96     public Collection<T> getRegistrations() {
97         return publicRegistrations;
98     }
99
100     RegistrationTreeNode<T> ensureChild(@Nonnull final PathArgument child) {
101         RegistrationTreeNode<T> potential = children.get(Preconditions.checkNotNull(child));
102         if (potential == null) {
103             potential = new RegistrationTreeNode<>(this, child);
104             children.put(child, potential);
105         }
106         return potential;
107     }
108
109     void addRegistration(@Nonnull final T registration) {
110         registrations.add(Preconditions.checkNotNull(registration));
111         LOG.debug("Registration {} added", registration);
112     }
113
114     void removeRegistration(@Nonnull final T registration) {
115         registrations.remove(Preconditions.checkNotNull(registration));
116         LOG.debug("Registration {} removed", registration);
117
118         // We have been called with the write-lock held, so we can perform some cleanup.
119         removeThisIfUnused();
120     }
121
122     private void removeThisIfUnused() {
123         final RegistrationTreeNode<T> p = parent.get();
124         if (p != null && registrations.isEmpty() && children.isEmpty()) {
125             p.removeChild(identifier);
126         }
127     }
128
129     private void removeChild(final PathArgument arg) {
130         children.remove(arg);
131         removeThisIfUnused();
132     }
133
134     @Override
135     public String toString() {
136         return MoreObjects.toStringHelper(this)
137                 .add("identifier", identifier)
138                 .add("registrations", registrations.size())
139                 .add("children", children.size()).toString();
140     }
141 }