2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.cache.CacheLoader;
17 import com.google.common.cache.LoadingCache;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Sets;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22 import java.io.Serializable;
23 import java.lang.reflect.Array;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.Objects;
32 import java.util.Optional;
34 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.concepts.Immutable;
39 import org.opendaylight.yangtools.concepts.Path;
40 import org.opendaylight.yangtools.util.HashCodeBuilder;
41 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
42 import org.opendaylight.yangtools.util.SharedSingletonMap;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.QNameModule;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
48 * Unique identifier of a particular node instance in the data tree.
51 * Java representation of YANG Built-in type <code>instance-identifier</code>,
52 * which conceptually is XPath expression minimized to uniquely identify element
53 * in data tree which conforms to constraints maintained by YANG Model,
54 * effectively this makes Instance Identifier a path to element in data tree.
57 * Constraints put in YANG specification on instance-identifier allowed it to be
58 * effectively represented in Java and it's evaluation does not require
59 * full-blown XPath processor.
62 * <h3>Path Arguments</h3>
63 * Path to the node represented in instance identifier consists of
64 * {@link PathArgument} which carries necessary information to uniquely identify
65 * node on particular level in the subtree.
68 * <li>{@link NodeIdentifier} - Identifier of node, which has cardinality
69 * <code>0..1</code> in particular subtree in data tree.</li>
70 * <li>{@link NodeIdentifierWithPredicates} - Identifier of node (list item),
71 * which has cardinality <code>0..n</code>.</li>
72 * <li>{@link NodeWithValue} - Identifier of instance <code>leaf</code> node or
73 * <code>leaf-list</code> node.</li>
74 * <li>{@link AugmentationIdentifier} - Identifier of instance of
75 * <code>augmentation</code> node.</li>
78 * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
80 // FIXME: 3.0.0: this concept needs to be moved to yang-common, as parser components need the ability to refer
81 // to data nodes -- most notably XPath expressions and {@code default} statement arguments need to be able
82 // to represent these.
83 // FIXME: FixedYangInstanceIdentifier needs YangInstanceIdentifier initialized, but that includes initializing
84 // this field. Figure out a way out of this pickle.
85 @SuppressFBWarnings("IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION")
86 public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
88 * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual root of the YANG namespace.
90 public static final YangInstanceIdentifier EMPTY = FixedYangInstanceIdentifier.EMPTY_INSTANCE;
92 private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
93 AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
94 private static final long serialVersionUID = 4L;
96 private final int hash;
97 private transient volatile String toStringCache = null;
99 // Package-private to prevent outside subclassing
100 YangInstanceIdentifier(final int hash) {
104 @Nonnull abstract YangInstanceIdentifier createRelativeIdentifier(int skipFromRoot);
106 @Nonnull abstract Collection<PathArgument> tryPathArguments();
108 @Nonnull abstract Collection<PathArgument> tryReversePathArguments();
111 * Check if this instance identifier has empty path arguments, e.g. it is
112 * empty and corresponds to {@link #EMPTY}.
114 * @return True if this instance identifier is empty, false otherwise.
116 public abstract boolean isEmpty();
119 * Return an optimized version of this identifier, useful when the identifier
120 * will be used very frequently.
122 * @return A optimized equivalent instance.
125 public abstract YangInstanceIdentifier toOptimized();
128 * Return the conceptual parent {@link YangInstanceIdentifier}, which has
129 * one item less in {@link #getPathArguments()}.
131 * @return Parent {@link YangInstanceIdentifier}, or null if this object is {@link #EMPTY}.
133 @Nullable public abstract YangInstanceIdentifier getParent();
136 * Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
138 * @param depth Ancestor depth
139 * @return Ancestor {@link YangInstanceIdentifier}
140 * @throws IllegalArgumentException if the specified depth is negative or is greater than the depth of this object.
142 @Nonnull public abstract YangInstanceIdentifier getAncestor(int depth);
145 * Returns an ordered iteration of path arguments.
147 * @return Immutable iteration of path arguments.
149 public abstract List<PathArgument> getPathArguments();
152 * Returns an iterable of path arguments in reverse order. This is useful
153 * when walking up a tree organized this way.
155 * @return Immutable iterable of path arguments in reverse order.
157 public abstract List<PathArgument> getReversePathArguments();
160 * Returns the last PathArgument. This is equivalent of iterating
161 * to the last element of the iterable returned by {@link #getPathArguments()}.
163 * @return The last past argument, or null if there are no PathArguments.
165 public abstract PathArgument getLastPathArgument();
167 public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
168 if (Iterables.isEmpty(path)) {
172 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
173 for (PathArgument a : path) {
177 return FixedYangInstanceIdentifier.create(path, hash.build());
180 public static YangInstanceIdentifier create(final PathArgument... path) {
181 // We are forcing a copy, since we cannot trust the user
182 return create(Arrays.asList(path));
185 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
186 return Iterables.elementsEqual(getPathArguments(), other.getPathArguments());
190 public boolean equals(final Object obj) {
194 if (!(obj instanceof YangInstanceIdentifier)) {
197 YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
198 if (this.hashCode() != obj.hashCode()) {
202 return pathArgumentsEqual(other);
206 * Constructs a new Instance Identifier with new {@link NodeIdentifier} added to the end of path arguments.
208 * @param name QName of {@link NodeIdentifier}
209 * @return Instance Identifier with additional path argument added to the end.
211 public final YangInstanceIdentifier node(final QName name) {
212 return node(new NodeIdentifier(name));
216 * Constructs a new Instance Identifier with new {@link PathArgument} added to the end of path arguments.
218 * @param arg Path argument which should be added to the end
219 * @return Instance Identifier with additional path argument added to the end.
221 public final YangInstanceIdentifier node(final PathArgument arg) {
222 return new StackedYangInstanceIdentifier(this, arg, HashCodeBuilder.nextHashCode(hash, arg));
226 * Get the relative path from an ancestor. This method attempts to perform
227 * the reverse of concatenating a base (ancestor) and a path.
230 * Ancestor against which the relative path should be calculated
231 * @return This object's relative path from parent, or Optional.absent() if
232 * the specified parent is not in fact an ancestor of this object.
234 public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
235 if (this == ancestor) {
236 return Optional.of(EMPTY);
238 if (ancestor.isEmpty()) {
239 return Optional.of(this);
242 final Iterator<PathArgument> lit = getPathArguments().iterator();
243 final Iterator<PathArgument> oit = ancestor.getPathArguments().iterator();
246 while (oit.hasNext()) {
247 // Ancestor is not really an ancestor
248 if (!lit.hasNext() || !lit.next().equals(oit.next())) {
249 return Optional.empty();
256 return Optional.of(this);
258 if (!lit.hasNext()) {
259 return Optional.of(EMPTY);
262 return Optional.of(createRelativeIdentifier(common));
266 public final boolean contains(@Nonnull final YangInstanceIdentifier other) {
271 checkArgument(other != null, "other should not be null");
272 final Iterator<PathArgument> lit = getPathArguments().iterator();
273 final Iterator<PathArgument> oit = other.getPathArguments().iterator();
275 while (lit.hasNext()) {
276 if (!oit.hasNext()) {
280 if (!lit.next().equals(oit.next())) {
289 public final String toString() {
291 * The toStringCache is safe, since the object contract requires
292 * immutability of the object and all objects referenced from this
294 * Used lists, maps are immutable. Path Arguments (elements) are also
295 * immutable, since the PathArgument contract requires immutability.
296 * The cache is thread-safe - if multiple computations occurs at the
297 * same time, cache will be overwritten with same result.
299 String ret = toStringCache;
301 final StringBuilder builder = new StringBuilder("/");
302 PathArgument prev = null;
303 for (PathArgument argument : getPathArguments()) {
307 builder.append(argument.toRelativeString(prev));
311 ret = builder.toString();
312 TOSTRINGCACHE_UPDATER.lazySet(this, ret);
318 public final int hashCode() {
320 * The caching is safe, since the object contract requires
321 * immutability of the object and all objects referenced from this
323 * Used lists, maps are immutable. Path Arguments (elements) are also
324 * immutable, since the PathArgument contract requires immutability.
329 private static int hashCode(final Object value) {
334 if (byte[].class.equals(value.getClass())) {
335 return Arrays.hashCode((byte[]) value);
338 if (value.getClass().isArray()) {
340 int length = Array.getLength(value);
341 for (int i = 0; i < length; i++) {
342 hash += Objects.hashCode(Array.get(value, i));
348 return Objects.hashCode(value);
351 // Static factories & helpers
354 * Returns a new InstanceIdentifier with only one path argument of type {@link NodeIdentifier} with supplied
357 * @param name QName of first node identifier
358 * @return Instance Identifier with only one path argument of type {@link NodeIdentifier}
360 public static YangInstanceIdentifier of(final QName name) {
361 return create(new NodeIdentifier(name));
365 * Returns new builder for InstanceIdentifier with empty path arguments.
367 * @return new builder for InstanceIdentifier with empty path arguments.
369 public static InstanceIdentifierBuilder builder() {
370 return new YangInstanceIdentifierBuilder();
374 * Returns new builder for InstanceIdentifier with path arguments copied from original instance identifier.
376 * @param origin InstanceIdentifier from which path arguments are copied.
377 * @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
379 public static InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
380 return new YangInstanceIdentifierBuilder(origin.getPathArguments(), origin.hashCode());
384 * Path argument / component of InstanceIdentifier.
385 * Path argument uniquely identifies node in data tree on particular
389 * This interface itself is used as common parent for actual
390 * path arguments types and should not be implemented by user code.
393 * Path arguments SHOULD contain only minimum of information
394 * required to uniquely identify node on particular subtree level.
397 * For actual path arguments types see:
399 * <li>{@link NodeIdentifier} - Identifier of container or leaf
400 * <li>{@link NodeIdentifierWithPredicates} - Identifier of list entries, which have key defined
401 * <li>{@link AugmentationIdentifier} - Identifier of augmentation
402 * <li>{@link NodeWithValue} - Identifier of leaf-list entry
405 public interface PathArgument extends Comparable<PathArgument>, Immutable, Serializable {
407 * If applicable returns unique QName of data node as defined in YANG
411 * This method may return null, if the corresponding schema node, does
412 * not have QName associated, such as in cases of augmentations.
419 * Return the string representation of this object for use in context
420 * provided by a previous object. This method can be implemented in
421 * terms of {@link #toString()}, but implementations are encourage to
422 * reuse any context already emitted by the previous object.
424 * @param previous Previous path argument
425 * @return String representation
427 String toRelativeString(PathArgument previous);
430 private abstract static class AbstractPathArgument implements PathArgument {
431 private static final long serialVersionUID = -4546547994250849340L;
432 private final QName nodeType;
433 private transient int hashValue;
434 private transient volatile boolean hashGuard = false;
436 protected AbstractPathArgument(final QName nodeType) {
437 this.nodeType = requireNonNull(nodeType);
441 public final QName getNodeType() {
446 @SuppressWarnings("checkstyle:parameterName")
447 public int compareTo(@Nonnull final PathArgument o) {
448 return nodeType.compareTo(o.getNodeType());
451 protected int hashCodeImpl() {
452 return 31 + getNodeType().hashCode();
456 public final int hashCode() {
458 hashValue = hashCodeImpl();
466 public boolean equals(final Object obj) {
470 if (obj == null || this.getClass() != obj.getClass()) {
474 return getNodeType().equals(((AbstractPathArgument)obj).getNodeType());
478 public String toString() {
479 return getNodeType().toString();
483 public String toRelativeString(final PathArgument previous) {
484 if (previous instanceof AbstractPathArgument) {
485 final QNameModule mod = previous.getNodeType().getModule();
486 if (getNodeType().getModule().equals(mod)) {
487 return getNodeType().getLocalName();
491 return getNodeType().toString();
496 * Simple path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.ContainerNode} or
497 * {@link org.opendaylight.yangtools.yang.data.api.schema.LeafNode} leaf in particular subtree.
499 public static final class NodeIdentifier extends AbstractPathArgument {
500 private static final long serialVersionUID = -2255888212390871347L;
501 private static final LoadingCache<QName, NodeIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
502 .build(new CacheLoader<QName, NodeIdentifier>() {
504 public NodeIdentifier load(@Nonnull final QName key) {
505 return new NodeIdentifier(key);
509 public NodeIdentifier(final QName node) {
514 * Return a NodeIdentifier for a particular QName. Unlike the constructor, this factory method uses a global
515 * instance cache, resulting in object reuse for equal inputs.
517 * @param node Node's QName
518 * @return A {@link NodeIdentifier}
520 public static NodeIdentifier create(final QName node) {
521 return CACHE.getUnchecked(node);
526 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode} leaf
529 public static final class NodeIdentifierWithPredicates extends AbstractPathArgument {
530 private static final long serialVersionUID = -4787195606494761540L;
532 private final Map<QName, Object> keyValues;
534 public NodeIdentifierWithPredicates(final QName node, final Map<QName, Object> keyValues) {
536 // Retains ImmutableMap for empty maps. For larger sizes uses a shared key set.
537 this.keyValues = ImmutableOffsetMap.unorderedCopyOf(keyValues);
540 public NodeIdentifierWithPredicates(final QName node, final ImmutableOffsetMap<QName, Object> keyValues) {
542 this.keyValues = requireNonNull(keyValues);
545 public NodeIdentifierWithPredicates(final QName node, final SharedSingletonMap<QName, Object> keyValues) {
547 this.keyValues = requireNonNull(keyValues);
550 public NodeIdentifierWithPredicates(final QName node, final QName key, final Object value) {
551 this(node, SharedSingletonMap.unorderedOf(key, value));
554 public Map<QName, Object> getKeyValues() {
559 protected int hashCodeImpl() {
560 final int prime = 31;
561 int result = super.hashCodeImpl();
562 result = prime * result;
564 for (Entry<QName, Object> entry : keyValues.entrySet()) {
565 result += Objects.hashCode(entry.getKey()) + YangInstanceIdentifier.hashCode(entry.getValue());
571 @SuppressWarnings("checkstyle:equalsHashCode")
572 public boolean equals(final Object obj) {
573 if (!super.equals(obj)) {
577 final Map<QName, Object> otherKeyValues = ((NodeIdentifierWithPredicates) obj).keyValues;
579 // TODO: benchmark to see if just calling equals() on the two maps is not faster
580 if (keyValues == otherKeyValues) {
583 if (keyValues.size() != otherKeyValues.size()) {
587 for (Entry<QName, Object> entry : keyValues.entrySet()) {
588 if (!otherKeyValues.containsKey(entry.getKey())
589 || !Objects.deepEquals(entry.getValue(), otherKeyValues.get(entry.getKey()))) {
599 public String toString() {
600 return super.toString() + '[' + keyValues + ']';
604 public String toRelativeString(final PathArgument previous) {
605 return super.toRelativeString(previous) + '[' + keyValues + ']';
610 * Simple path argument identifying a {@link LeafSetEntryNode} leaf
613 public static final class NodeWithValue<T> extends AbstractPathArgument {
614 private static final long serialVersionUID = -3637456085341738431L;
616 private final T value;
618 public NodeWithValue(final QName node, final T value) {
623 public T getValue() {
628 protected int hashCodeImpl() {
629 final int prime = 31;
630 int result = super.hashCodeImpl();
631 result = prime * result + YangInstanceIdentifier.hashCode(value);
636 @SuppressWarnings("checkstyle:equalsHashCode")
637 public boolean equals(final Object obj) {
638 if (!super.equals(obj)) {
641 final NodeWithValue<?> other = (NodeWithValue<?>) obj;
642 return Objects.deepEquals(value, other.value);
646 public String toString() {
647 return super.toString() + '[' + value + ']';
651 public String toRelativeString(final PathArgument previous) {
652 return super.toRelativeString(previous) + '[' + value + ']';
657 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode}
658 * node in particular subtree.
661 * Augmentation is uniquely identified by set of all possible child nodes.
663 * to identify instance of augmentation,
664 * since RFC6020 states that <code>augment</code> that augment
665 * statement must not add multiple nodes from same namespace
666 * / module to the target node.
668 * @see <a href="http://tools.ietf.org/html/rfc6020#section-7.15">RFC6020</a>
670 public static final class AugmentationIdentifier implements PathArgument {
671 private static final long serialVersionUID = -8122335594681936939L;
672 private final ImmutableSet<QName> childNames;
675 public QName getNodeType() {
676 // This should rather throw exception than return always null
677 throw new UnsupportedOperationException("Augmentation node has no QName");
681 * Construct new augmentation identifier using supplied set of possible
685 * Set of possible child nodes.
687 public AugmentationIdentifier(final Set<QName> childNames) {
688 this.childNames = ImmutableSet.copyOf(childNames);
692 * Returns set of all possible child nodes.
694 * @return set of all possible child nodes.
696 public Set<QName> getPossibleChildNames() {
701 public String toString() {
702 return "AugmentationIdentifier{" + "childNames=" + childNames + '}';
706 public String toRelativeString(final PathArgument previous) {
711 public boolean equals(final Object obj) {
715 if (!(obj instanceof AugmentationIdentifier)) {
719 AugmentationIdentifier that = (AugmentationIdentifier) obj;
720 return childNames.equals(that.childNames);
724 public int hashCode() {
725 return childNames.hashCode();
729 @SuppressWarnings("checkstyle:parameterName")
730 public int compareTo(@Nonnull final PathArgument o) {
731 if (!(o instanceof AugmentationIdentifier)) {
734 AugmentationIdentifier other = (AugmentationIdentifier) o;
735 Set<QName> otherChildNames = other.getPossibleChildNames();
736 int thisSize = childNames.size();
737 int otherSize = otherChildNames.size();
738 if (thisSize == otherSize) {
739 // Quick Set-based comparison
740 if (childNames.equals(otherChildNames)) {
744 // We already know the sets are not equal, but have equal size, hence the sets differ in their elements,
745 // but potentially share a common set of elements. The most consistent way of comparing them is using
746 // total ordering defined by QName's compareTo. Hence convert both sets to lists ordered
747 // by QName.compareTo() and decide on the first differing element.
748 final List<QName> diff = new ArrayList<>(Sets.symmetricDifference(childNames, otherChildNames));
749 verify(!diff.isEmpty(), "Augmentation identifiers %s and %s report no difference", this, o);
750 diff.sort(QName::compareTo);
751 return childNames.contains(diff.get(0)) ? -1 : 1;
752 } else if (thisSize < otherSize) {
761 * Fluent Builder of Instance Identifier instances.
763 public interface InstanceIdentifierBuilder extends Builder<YangInstanceIdentifier> {
765 * Adds a {@link PathArgument} to path arguments of resulting instance identifier.
767 * @param arg A {@link PathArgument} to be added
768 * @return this builder
770 InstanceIdentifierBuilder node(PathArgument arg);
773 * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
775 * @param nodeType QName of {@link NodeIdentifier} which will be added
776 * @return this builder
778 InstanceIdentifierBuilder node(QName nodeType);
781 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting
782 * instance identifier.
784 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
785 * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
786 * @return this builder
788 InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
791 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
793 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
794 * @param key QName of key which will be added
795 * @param value value of key which will be added
796 * @return this builder
798 InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
801 * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
803 * @param args {@link PathArgument}s to be added
804 * @return this builder
805 * @throws NullPointerException if any of the arguments is null
808 InstanceIdentifierBuilder append(Collection<? extends PathArgument> args);
811 * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
813 * @param args {@link PathArgument}s to be added
814 * @return this builder
815 * @throws NullPointerException if any of the arguments is null
818 default InstanceIdentifierBuilder append(final PathArgument... args) {
819 return append(Arrays.asList(args));
823 * Builds an {@link YangInstanceIdentifier} with path arguments from this builder.
825 * @return {@link YangInstanceIdentifier}
828 YangInstanceIdentifier build();