2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.data.api;
9 import com.google.common.base.Optional;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Lists;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.io.ObjectStreamException;
20 import java.io.Serializable;
21 import java.lang.reflect.Array;
22 import java.lang.reflect.Field;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.Objects;
33 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
34 import org.opendaylight.yangtools.concepts.Builder;
35 import org.opendaylight.yangtools.concepts.Immutable;
36 import org.opendaylight.yangtools.concepts.Path;
37 import org.opendaylight.yangtools.util.HashCodeBuilder;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
43 * Unique identifier of a particular node instance in the data tree.
46 * Java representation of YANG Built-in type <code>instance-identifier</code>,
47 * which conceptually is XPath expression minimized to uniquely identify element
48 * in data tree which conforms to constraints maintained by YANG Model,
49 * effectively this makes Instance Identifier a path to element in data tree.
51 * Constraints put in YANG specification on instance-identifier allowed it to be
52 * effectively represented in Java and it's evaluation does not require
53 * full-blown XPath processor.
55 * <h3>Path Arguments</h3>
56 * Path to the node represented in instance identifier consists of
57 * {@link PathArgument} which carries necessary information to uniquely identify
58 * node on particular level in the subtree.
61 * <li>{@link NodeIdentifier} - Identifier of node, which has cardinality
62 * <code>0..1</code> in particular subtree in data tree.</li>
63 * <li>{@link NodeIdentifierWithPredicates} - Identifier of node (list item),
64 * which has cardinality <code>0..n</code>.</li>
65 * <li>{@link NodeWithValue} - Identifier of instance <code>leaf</code> node or
66 * <code>leaf-list</code> node.</li>
67 * <li>{@link AugmentationIdentifier} - Identifier of instance of
68 * <code>augmentation</code> node.</li>
72 * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
74 public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
76 * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual
77 * root of the YANG namespace.
79 public static final YangInstanceIdentifier EMPTY = trustedCreate(Collections.<PathArgument>emptyList());
80 @SuppressWarnings("rawtypes")
81 private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, ImmutableList> LEGACYPATH_UPDATER =
82 AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, ImmutableList.class, "legacyPath");
83 private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
84 AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
85 private static final Field PATHARGUMENTS_FIELD;
87 private static final long serialVersionUID = 3L;
88 private final transient Iterable<PathArgument> pathArguments;
89 private final int hash;
91 private transient volatile ImmutableList<PathArgument> legacyPath = null;
92 private transient volatile String toStringCache = null;
97 f = YangInstanceIdentifier.class.getDeclaredField("pathArguments");
98 } catch (NoSuchFieldException | SecurityException e) {
99 throw new ExceptionInInitializerError(e);
101 f.setAccessible(true);
103 PATHARGUMENTS_FIELD = f;
106 private ImmutableList<PathArgument> getLegacyPath() {
107 // Temporary variable saves a volatile read
108 ImmutableList<PathArgument> ret = legacyPath;
110 // We could have used a synchronized block, but the window is quite
111 // small and worst that can happen is duplicate object construction.
112 ret = ImmutableList.copyOf(pathArguments);
113 LEGACYPATH_UPDATER.lazySet(this, ret);
120 * Returns a list of path arguments.
122 * @deprecated Use {@link #getPathArguments()} instead.
123 * @return Immutable list of path arguments.
126 public List<PathArgument> getPath() {
127 return getLegacyPath();
131 * Returns an ordered iteration of path arguments.
133 * @return Immutable iteration of path arguments.
135 public Iterable<PathArgument> getPathArguments() {
136 return pathArguments;
140 * Returns an iterable of path arguments in reverse order. This is useful
141 * when walking up a tree organized this way.
143 * @return Immutable iterable of path arguments in reverse order.
145 public Iterable<PathArgument> getReversePathArguments() {
146 return getLegacyPath().reverse();
150 * Returns the last PathArgument. This is equivalent of iterating
151 * to the last element of the iterable returned by {@link #getPathArguments()}.
153 * @return The last past argument, or null if there are no PathArguments.
155 public PathArgument getLastPathArgument() {
156 return Iterables.getFirst(getReversePathArguments(), null);
159 private YangInstanceIdentifier(final Iterable<PathArgument> path, final int hash) {
160 this.pathArguments = Preconditions.checkNotNull(path, "path must not be null.");
164 private static YangInstanceIdentifier trustedCreate(final Iterable<PathArgument> path) {
165 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
166 for (PathArgument a : path) {
170 return new YangInstanceIdentifier(path, hash.build());
173 public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
174 if (Iterables.isEmpty(path)) {
178 return trustedCreate(ImmutableList.copyOf(path));
181 public static YangInstanceIdentifier create(final PathArgument... path) {
182 // We are forcing a copy, since we cannot trust the user
183 return create(Arrays.asList(path));
187 public int hashCode() {
189 * The caching is safe, since the object contract requires
190 * immutability of the object and all objects referenced from this
192 * Used lists, maps are immutable. Path Arguments (elements) are also
193 * immutable, since the PathArgument contract requires immutability.
199 public boolean equals(final Object obj) {
206 if (getClass() != obj.getClass()) {
209 YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
210 if (this.hashCode() != obj.hashCode()) {
213 return Iterables.elementsEqual(pathArguments, other.pathArguments);
217 * Constructs a new Instance Identifier with new {@link NodeIdentifier} added to the end of path arguments
219 * @param name QName of {@link NodeIdentifier}
220 * @return Instance Identifier with additional path argument added to the end.
222 public YangInstanceIdentifier node(final QName name) {
223 return node(new NodeIdentifier(name));
228 * Constructs a new Instance Identifier with new {@link PathArgument} added to the end of path arguments
230 * @param arg Path argument which should be added to the end
231 * @return Instance Identifier with additional path argument added to the end.
233 public YangInstanceIdentifier node(final PathArgument arg) {
234 return new YangInstanceIdentifier(Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg));
238 * Get the relative path from an ancestor. This method attempts to perform
239 * the reverse of concatenating a base (ancestor) and a path.
242 * Ancestor against which the relative path should be calculated
243 * @return This object's relative path from parent, or Optional.absent() if
244 * the specified parent is not in fact an ancestor of this object.
246 public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
247 final Iterator<?> lit = pathArguments.iterator();
248 final Iterator<?> oit = ancestor.pathArguments.iterator();
251 while (oit.hasNext()) {
252 // Ancestor is not really an ancestor
253 if (!lit.hasNext() || !lit.next().equals(oit.next())) {
254 return Optional.absent();
261 return Optional.of(this);
263 if (!lit.hasNext()) {
264 return Optional.of(EMPTY);
266 return Optional.of(trustedCreate(Iterables.skip(pathArguments, common)));
269 private static int hashCode(final Object value) {
274 if (value.getClass().equals(byte[].class)) {
275 return Arrays.hashCode((byte[]) value);
278 if (value.getClass().isArray()) {
280 int length = Array.getLength(value);
281 for (int i = 0; i < length; i++) {
282 hash += Objects.hashCode(Array.get(value, i));
288 return Objects.hashCode(value);
291 // Static factories & helpers
295 * Returns a new InstanceIdentifier with only one path argument of type {@link NodeIdentifier} with supplied QName
297 * @param name QName of first node identifier
298 * @return Instance Identifier with only one path argument of type {@link NodeIdentifier}
300 public static YangInstanceIdentifier of(final QName name) {
301 return create(new NodeIdentifier(name));
306 * Returns new builder for InstanceIdentifier with empty path arguments.
308 * @return new builder for InstanceIdentifier with empty path arguments.
310 public static InstanceIdentifierBuilder builder() {
311 return new BuilderImpl();
316 * Returns new builder for InstanceIdentifier with path arguments copied from original instance identifier.
318 * @param origin Instace Identifier from which path arguments are copied.
319 * @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
321 public static InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
322 return new BuilderImpl(origin.getPathArguments(), origin.hashCode());
326 * Path argument / component of InstanceIdentifier
328 * Path argument uniquely identifies node in data tree on particular
331 * This interface itself is used as common parent for actual
332 * path arguments types and should not be implemented by user code.
334 * Path arguments SHOULD contain only minimum of information
335 * required to uniquely identify node on particular subtree level.
337 * For actual path arguments types see:
339 * <li>{@link NodeIdentifier} - Identifier of container or leaf
340 * <li>{@link NodeIdentifierWithPredicates} - Identifier of list entries, which have key defined
341 * <li>{@link AugmentationIdentifier} - Identifier of augmentation
342 * <li>{@link NodeWithValue} - Identifier of leaf-list entry
345 public interface PathArgument extends Comparable<PathArgument>, Immutable, Serializable {
347 * If applicable returns unique QName of data node as defined in YANG
350 * This method may return null, if the corresponding schema node, does
351 * not have QName associated, such as in cases of augmentations.
358 * Return the string representation of this object for use in context
359 * provided by a previous object. This method can be implemented in
360 * terms of {@link #toString()}, but implementations are encourage to
361 * reuse any context already emitted by the previous object.
363 * @param previous Previous path argument
364 * @return String representation
366 String toRelativeString(PathArgument previous);
369 private static abstract class AbstractPathArgument implements PathArgument {
370 private static final long serialVersionUID = -4546547994250849340L;
371 private final QName nodeType;
372 private transient int hashValue;
373 private volatile transient boolean hashGuard = false;
375 protected AbstractPathArgument(final QName nodeType) {
376 this.nodeType = Preconditions.checkNotNull(nodeType);
380 public final QName getNodeType() {
385 public int compareTo(final PathArgument o) {
386 return nodeType.compareTo(o.getNodeType());
389 protected int hashCodeImpl() {
390 return 31 + getNodeType().hashCode();
394 public final int hashCode() {
396 hashValue = hashCodeImpl();
404 public boolean equals(final Object obj) {
408 if (obj == null || this.getClass() != obj.getClass()) {
412 return getNodeType().equals(((AbstractPathArgument)obj).getNodeType());
416 public String toString() {
417 return getNodeType().toString();
421 public String toRelativeString(final PathArgument previous) {
422 if (previous instanceof AbstractPathArgument) {
423 final QNameModule mod = ((AbstractPathArgument)previous).getNodeType().getModule();
424 if (getNodeType().getModule().equals(mod)) {
425 return getNodeType().getLocalName();
429 return getNodeType().toString();
434 * Fluent Builder of Instance Identifier instances
436 public interface InstanceIdentifierBuilder extends Builder<YangInstanceIdentifier> {
438 * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
440 * @param nodeType QName of {@link NodeIdentifier} which will be added
441 * @return this builder
443 InstanceIdentifierBuilder node(QName nodeType);
446 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting instance identifier.
448 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
449 * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
450 * @return this builder
452 InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
455 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
457 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
458 * @param key QName of key which will be added
459 * @param value value of key which will be added
460 * @return this builder
462 InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
466 * Builds an {@link YangInstanceIdentifier} with path arguments from this builder
468 * @return {@link YangInstanceIdentifier}
471 YangInstanceIdentifier build();
474 * @deprecated use #build()
477 YangInstanceIdentifier toInstance();
481 * Simple path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.ContainerNode} or
482 * {@link org.opendaylight.yangtools.yang.data.api.schema.LeafNode} leaf in particular subtree.
484 public static final class NodeIdentifier extends AbstractPathArgument {
485 private static final long serialVersionUID = -2255888212390871347L;
487 public NodeIdentifier(final QName node) {
493 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode} leaf
496 public static final class NodeIdentifierWithPredicates extends AbstractPathArgument {
497 private static final long serialVersionUID = -4787195606494761540L;
499 private final Map<QName, Object> keyValues;
501 public NodeIdentifierWithPredicates(final QName node, final Map<QName, Object> keyValues) {
503 this.keyValues = ImmutableMap.copyOf(keyValues);
506 public NodeIdentifierWithPredicates(final QName node, final QName key, final Object value) {
507 this(node, ImmutableMap.of(key, value));
510 public Map<QName, Object> getKeyValues() {
515 protected int hashCodeImpl() {
516 final int prime = 31;
517 int result = super.hashCodeImpl();
518 result = prime * result;
520 for (Entry<QName, Object> entry : keyValues.entrySet()) {
521 result += Objects.hashCode(entry.getKey()) + YangInstanceIdentifier.hashCode(entry.getValue());
527 public boolean equals(final Object obj) {
528 if (!super.equals(obj)) {
532 final Map<QName, Object> otherKeyValues = ((NodeIdentifierWithPredicates) obj).keyValues;
533 if (keyValues.size() != otherKeyValues.size()) {
537 for (Entry<QName, Object> entry : keyValues.entrySet()) {
538 if (!otherKeyValues.containsKey(entry.getKey())
539 || !Objects.deepEquals(entry.getValue(), otherKeyValues.get(entry.getKey()))) {
549 public String toString() {
550 return super.toString() + '[' + keyValues + ']';
554 public String toRelativeString(final PathArgument previous) {
555 return super.toRelativeString(previous) + '[' + keyValues + ']';
560 * Simple path argument identifying a {@link LeafSetEntryNode} leaf
563 public static final class NodeWithValue extends AbstractPathArgument {
564 private static final long serialVersionUID = -3637456085341738431L;
566 private final Object value;
568 public NodeWithValue(final QName node, final Object value) {
573 public Object getValue() {
578 protected int hashCodeImpl() {
579 final int prime = 31;
580 int result = super.hashCodeImpl();
581 result = prime * result + ((value == null) ? 0 : YangInstanceIdentifier.hashCode(value));
586 public boolean equals(final Object obj) {
587 if (!super.equals(obj)) {
590 final NodeWithValue other = (NodeWithValue) obj;
591 return Objects.deepEquals(value, other.value);
595 public String toString() {
596 return super.toString() + '[' + value + ']';
600 public String toRelativeString(final PathArgument previous) {
601 return super.toRelativeString(previous) + '[' + value + ']';
606 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode} node in
607 * particular subtree.
609 * Augmentation is uniquely identified by set of all possible child nodes.
611 * to identify instance of augmentation,
612 * since RFC6020 states that <code>augment</code> that augment
613 * statement must not add multiple nodes from same namespace
614 * / module to the target node.
617 * @see <a href="http://tools.ietf.org/html/rfc6020#section-7.15">RFC6020</a>
619 public static final class AugmentationIdentifier implements PathArgument {
620 private static final long serialVersionUID = -8122335594681936939L;
621 private final ImmutableSet<QName> childNames;
624 public QName getNodeType() {
625 // This should rather throw exception than return always null
626 throw new UnsupportedOperationException("Augmentation node has no QName");
631 * Construct new augmentation identifier using supplied set of possible
635 * Set of possible child nodes.
637 public AugmentationIdentifier(final Set<QName> childNames) {
638 this.childNames = ImmutableSet.copyOf(childNames);
642 * Returns set of all possible child nodes
644 * @return set of all possible child nodes.
646 public Set<QName> getPossibleChildNames() {
651 public String toString() {
652 final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
653 sb.append("childNames=").append(childNames).append('}');
654 return sb.toString();
658 public String toRelativeString(final PathArgument previous) {
663 public boolean equals(final Object o) {
667 if (!(o instanceof AugmentationIdentifier)) {
671 AugmentationIdentifier that = (AugmentationIdentifier) o;
672 return childNames.equals(that.childNames);
676 public int hashCode() {
677 return childNames.hashCode();
681 public int compareTo(final PathArgument o) {
682 if (!(o instanceof AugmentationIdentifier)) {
685 AugmentationIdentifier other = (AugmentationIdentifier) o;
686 Set<QName> otherChildNames = other.getPossibleChildNames();
687 int thisSize = childNames.size();
688 int otherSize = otherChildNames.size();
689 if (thisSize == otherSize) {
690 Iterator<QName> otherIterator = otherChildNames.iterator();
691 for (QName name : childNames) {
692 int c = name.compareTo(otherIterator.next());
698 } else if (thisSize < otherSize) {
706 private static class BuilderImpl implements InstanceIdentifierBuilder {
707 private final HashCodeBuilder<PathArgument> hash;
708 private final List<PathArgument> path;
710 public BuilderImpl() {
711 this.hash = new HashCodeBuilder<>();
712 this.path = new ArrayList<>();
715 public BuilderImpl(final Iterable<PathArgument> prefix, final int hash) {
716 this.path = Lists.newArrayList(prefix);
717 this.hash = new HashCodeBuilder<>(hash);
721 public InstanceIdentifierBuilder node(final QName nodeType) {
722 final PathArgument arg = new NodeIdentifier(nodeType);
724 hash.addArgument(arg);
729 public InstanceIdentifierBuilder nodeWithKey(final QName nodeType, final QName key, final Object value) {
730 final PathArgument arg = new NodeIdentifierWithPredicates(nodeType, key, value);
732 hash.addArgument(arg);
737 public InstanceIdentifierBuilder nodeWithKey(final QName nodeType, final Map<QName, Object> keyValues) {
738 final PathArgument arg = new NodeIdentifierWithPredicates(nodeType, keyValues);
740 hash.addArgument(arg);
746 public YangInstanceIdentifier toInstance() {
751 public YangInstanceIdentifier build() {
752 return new YangInstanceIdentifier(ImmutableList.copyOf(path), hash.build());
757 public boolean contains(final YangInstanceIdentifier other) {
758 Preconditions.checkArgument(other != null, "other should not be null");
760 final Iterator<?> lit = pathArguments.iterator();
761 final Iterator<?> oit = other.pathArguments.iterator();
763 while (lit.hasNext()) {
764 if (!oit.hasNext()) {
768 if (!lit.next().equals(oit.next())) {
777 public String toString() {
779 * The toStringCache is safe, since the object contract requires
780 * immutability of the object and all objects referenced from this
782 * Used lists, maps are immutable. Path Arguments (elements) are also
783 * immutable, since the PathArgument contract requires immutability.
784 * The cache is thread-safe - if multiple computations occurs at the
785 * same time, cache will be overwritten with same result.
787 String ret = toStringCache;
789 final StringBuilder builder = new StringBuilder("/");
790 PathArgument prev = null;
791 for (PathArgument argument : getPathArguments()) {
795 builder.append(argument.toRelativeString(prev));
799 ret = builder.toString();
800 TOSTRINGCACHE_UPDATER.lazySet(this, ret);
805 private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
806 inputStream.defaultReadObject();
807 legacyPath = ImmutableList.copyOf((Collection<PathArgument>)inputStream.readObject());
810 PATHARGUMENTS_FIELD.set(this, legacyPath);
811 } catch (IllegalArgumentException | IllegalAccessException e) {
812 throw new IOException(e);
816 private Object readResolve() throws ObjectStreamException {
817 return legacyPath.isEmpty() ? EMPTY : this;
820 private void writeObject(final ObjectOutputStream outputStream) throws IOException {
822 * This may look strange, but what we are doing here is side-stepping the fact
823 * that pathArguments is not generally serializable. We are forcing instantiation
824 * of the legacy path, which is an ImmutableList (thus Serializable) and write
825 * it out. The read path does the opposite -- it reads the legacyPath and then
826 * uses invocation API to set the field.
828 ImmutableList<PathArgument> pathArguments = getLegacyPath();
829 outputStream.defaultWriteObject();
830 outputStream.writeObject(pathArguments);