2 * Copyright (c) 2013 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.binding;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableCollection;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Iterables;
16 import java.io.IOException;
17 import java.io.Serializable;
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import org.opendaylight.yangtools.concepts.Builder;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.concepts.Path;
26 import org.opendaylight.yangtools.util.HashCodeBuilder;
30 * This instance identifier uniquely identifies a specific DataObject in the data tree modeled by YANG.
32 * For Example let's say you were trying to refer to a node in inventory which was modeled in YANG as follows,
35 * module opendaylight-inventory {
41 * ext:context-instance "node-context";
50 * You could create an instance identifier as follows to get to a node with id "openflow:1"
52 * InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
54 * This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
57 public class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>, Immutable, Serializable {
58 private static final Field PATHARGUMENTS_FIELD;
59 private static final long serialVersionUID = 2L;
61 * Protected to differentiate internal and external access. Internal
62 * access is required never to modify the contents. References passed
63 * to outside entities have to be wrapped in an unmodifiable view.
65 protected transient final Iterable<PathArgument> pathArguments;
66 private final Class<T> targetType;
67 private final boolean wildcarded;
68 private final int hash;
73 f = InstanceIdentifier.class.getDeclaredField("pathArguments");
74 } catch (NoSuchFieldException | SecurityException e) {
75 throw new ExceptionInInitializerError(e);
77 f.setAccessible(true);
78 PATHARGUMENTS_FIELD = f;
81 InstanceIdentifier(final Class<T> type, final Iterable<PathArgument> pathArguments, final boolean wildcarded, final int hash) {
82 this.pathArguments = Preconditions.checkNotNull(pathArguments);
83 this.targetType = Preconditions.checkNotNull(type);
84 this.wildcarded = wildcarded;
89 * Return the type of data which this InstanceIdentifier identifies.
93 public final Class<T> getTargetType() {
98 * Return the path argument chain which makes up this instance identifier.
100 * @return Path argument chain. Immutable and does not contain nulls.
102 public final Iterable<PathArgument> getPathArguments() {
103 return Iterables.unmodifiableIterable(pathArguments);
107 * Check whether an instance identifier contains any wildcards. A wildcard
108 * is an path argument which has a null key.
110 * @return true if any of the path arguments has a null key.
112 public final boolean isWildcarded() {
117 public final int hashCode() {
122 public final boolean equals(final Object obj) {
129 if (getClass() != obj.getClass()) {
133 final InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
134 if (pathArguments == other.pathArguments) {
139 * We could now just go and compare the pathArguments, but that
140 * can be potentially expensive. Let's try to avoid that by
141 * checking various things that we have cached from pathArguments
142 * and trying to prove the identifiers are *not* equal.
144 if (hash != other.hash) {
147 if (wildcarded != other.wildcarded) {
150 if (targetType != other.targetType) {
153 if (fastNonEqual(other)) {
157 // Everything checks out so far, so we have to do a full equals
158 return Iterables.elementsEqual(pathArguments, other.pathArguments);
162 * Perform class-specific fast checks for non-equality. This allows
163 * subclasses to avoid iterating over the pathArguments by performing
164 * quick checks on their specific fields.
166 * @param other The other identifier, guaranteed to be the same class
167 * @return true if the other identifier cannot be equal to this one.
169 protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
174 public final String toString() {
175 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
179 * Add class-specific toString attributes.
181 * @param toStringHelper ToStringHelper instance
182 * @return ToStringHelper instance which was passed in
184 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
185 return toStringHelper.add("targetType", targetType).add("path", Iterables.toString(pathArguments));
189 * Return an instance identifier trimmed at the first occurrence of a
190 * specific component type.
192 * For example let's say an instance identifier was built like so,
194 * identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
197 * And you wanted to obtain the Instance identifier which represented Nodes you would do it like so,
200 * identifier.firstIdentifierOf(Nodes.class)
203 * @param type component type
204 * @return trimmed instance identifier, or null if the component type
207 public final <I extends DataObject> InstanceIdentifier<I> firstIdentifierOf(final Class<I> type) {
209 for (final PathArgument a : pathArguments) {
210 if (type.equals(a.getType())) {
211 @SuppressWarnings("unchecked")
212 final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(
213 Iterables.limit(pathArguments, i));
224 * Return the key associated with the first component of specified type in
227 * @param listItem component type
228 * @param listKey component key type
229 * @return key associated with the component, or null if the component type
232 * @deprecated Use {@link #firstKeyOf(Class)} instead.
235 public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
236 return firstKeyOf(listItem);
240 * Return the key associated with the first component of specified type in
243 * @param listItem component type
244 * @return key associated with the component, or null if the component type
247 public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem) {
248 for (final PathArgument i : pathArguments) {
249 if (listItem.equals(i.getType())) {
250 @SuppressWarnings("unchecked")
251 final K ret = ((IdentifiableItem<N, K>)i).getKey();
260 * Check whether an identifier is contained in this identifier. This is a strict subtree check, which requires all
261 * PathArguments to match exactly, e.g.
264 * The contains method checks if the other identifier is fully contained within the current identifier. It does this
265 * by looking at only the types of the path arguments and not by comparing the path arguments themselves.
267 * To illustrate here is an example which explains the working of this API.
269 * Let's say you have two instance identifiers as follows,
271 * this = /nodes/node/openflow:1
272 * other = /nodes/node/openflow:2
274 * then this.contains(other) will return false.
280 public final boolean contains(final InstanceIdentifier<? extends DataObject> other) {
281 Preconditions.checkNotNull(other, "other should not be null");
283 final Iterator<?> lit = pathArguments.iterator();
284 final Iterator<?> oit = other.pathArguments.iterator();
286 while (lit.hasNext()) {
287 if (!oit.hasNext()) {
291 if (!lit.next().equals(oit.next())) {
300 * Check whether this instance identifier contains the other identifier after wildcard expansion. This is similar
301 * to {@link #contains(InstanceIdentifier)}, with the exception that a wildcards are assumed to match the their
302 * non-wildcarded PathArgument counterpart.
304 * @param other Identifier which should be checked for inclusion.
305 * @return true if this identifier contains the other object
307 public final boolean containsWildcarded(final InstanceIdentifier<?> other) {
308 Preconditions.checkNotNull(other, "other should not be null");
310 final Iterator<PathArgument> lit = pathArguments.iterator();
311 final Iterator<PathArgument> oit = other.pathArguments.iterator();
313 while (lit.hasNext()) {
314 if (!oit.hasNext()) {
318 final PathArgument la = lit.next();
319 final PathArgument oa = oit.next();
321 if (!la.getType().equals(oa.getType())) {
324 if (la instanceof IdentifiableItem<?, ?> && oa instanceof IdentifiableItem<?, ?> && !la.equals(oa)) {
333 * Create a builder rooted at this key.
335 * @return A builder instance
337 public InstanceIdentifierBuilder<T> builder() {
338 return new InstanceIdentifierBuilderImpl<>(new Item<T>(targetType), pathArguments, hash, isWildcarded());
341 private InstanceIdentifier<?> childIdentifier(final PathArgument arg) {
342 return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg), isWildcarded());
345 @SuppressWarnings("unchecked")
346 public final <N extends ChildOf<? super T>> InstanceIdentifier<N> child(final Class<N> container) {
347 final PathArgument arg = new Item<>(container);
348 return (InstanceIdentifier<N>) childIdentifier(arg);
351 @SuppressWarnings("unchecked")
352 public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> KeyedInstanceIdentifier<N, K> child(
353 final Class<N> listItem, final K listKey) {
354 final PathArgument arg = new IdentifiableItem<>(listItem, listKey);
355 return (KeyedInstanceIdentifier<N, K>) childIdentifier(arg);
358 @SuppressWarnings("unchecked")
359 public final <N extends DataObject & Augmentation<? super T>> InstanceIdentifier<N> augmentation(
360 final Class<N> container) {
361 final PathArgument arg = new Item<>(container);
362 return (InstanceIdentifier<N>) childIdentifier(arg);
366 private List<PathArgument> legacyCache;
369 * @deprecated Use {@link #getPathArguments()} instead.
372 public final List<PathArgument> getPath() {
373 if (legacyCache == null) {
374 legacyCache = ImmutableList.<PathArgument>copyOf(pathArguments);
381 * Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
387 * @deprecated Use {@link #builder()} instead.
390 public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(final InstanceIdentifier<T> base) {
391 return base.builder();
395 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container
401 public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(final Class<T> container) {
402 return new InstanceIdentifierBuilderImpl<T>().addNode(container);
406 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an IdentifiableItem
414 public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
415 final Class<N> listItem, final K listKey) {
416 return new InstanceIdentifierBuilderImpl<N>().addNode(listItem, listKey);
420 * Create an instance identifier for a very specific object type. This method
421 * implements {@link #create(Iterable)} semantics, except it is used by internal
422 * callers, which have assured that the argument is an immutable Iterable.
425 * @param pathArguments The path to a specific node in the data tree
426 * @return InstanceIdentifier instance
427 * @throws IllegalArgumentException if pathArguments is empty or
428 * contains a null element.
430 private static InstanceIdentifier<?> internalCreate(final Iterable<PathArgument> pathArguments) {
431 final Iterator<? extends PathArgument> it = Preconditions.checkNotNull(pathArguments, "pathArguments may not be null").iterator();
432 final HashCodeBuilder<PathArgument> hashBuilder = new HashCodeBuilder<>();
433 boolean wildcard = false;
434 PathArgument a = null;
436 while (it.hasNext()) {
438 Preconditions.checkArgument(a != null, "pathArguments may not contain null elements");
440 // TODO: sanity check ChildOf<>;
441 hashBuilder.addArgument(a);
443 if (Identifiable.class.isAssignableFrom(a.getType()) && !(a instanceof IdentifiableItem<?, ?>)) {
447 Preconditions.checkArgument(a != null, "pathArguments may not be empty");
449 return trustedCreate(a, pathArguments, hashBuilder.build(), wildcard);
453 * Create an instance identifier for a very specific object type.
457 * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
458 * new InstanceIdentifier(path);
461 * @param pathArguments The path to a specific node in the data tree
462 * @return InstanceIdentifier instance
463 * @throws IllegalArgumentException if pathArguments is empty or
464 * contains a null element.
466 public static InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
467 if (pathArguments instanceof ImmutableCollection<?>) {
468 @SuppressWarnings("unchecked")
469 final Iterable<PathArgument> immutableArguments = (Iterable<PathArgument>) pathArguments;
470 return internalCreate(immutableArguments);
472 return internalCreate(ImmutableList.copyOf(pathArguments));
477 * Create an instance identifier for a very specific object type.
481 * new InstanceIdentifier(Nodes.class)
483 * would create an InstanceIdentifier for an object of type Nodes
485 * @param type The type of the object which this instance identifier represents
486 * @return InstanceIdentifier instance
488 @SuppressWarnings("unchecked")
489 public static <T extends DataObject> InstanceIdentifier<T> create(final Class<T> type) {
490 return (InstanceIdentifier<T>) create(Collections.<PathArgument> singletonList(new Item<>(type)));
494 * Return the key associated with the last component of the specified identifier.
496 * @param id instance identifier
497 * @return key associated with the last component
498 * @throws IllegalArgumentException if the supplied identifier type cannot have a key.
499 * @throws NullPointerException if id is null.
501 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
502 Preconditions.checkNotNull(id);
503 Preconditions.checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
505 @SuppressWarnings("unchecked")
506 final K ret = ((KeyedInstanceIdentifier<N, K>)id).getKey();
510 @SuppressWarnings({ "unchecked", "rawtypes" })
511 static InstanceIdentifier<?> trustedCreate(final PathArgument arg, final Iterable<PathArgument> pathArguments, final int hash, boolean wildcarded) {
512 if (Identifiable.class.isAssignableFrom(arg.getType()) && !(wildcarded)) {
513 Identifier<?> key = null;
514 if (arg instanceof IdentifiableItem<?, ?>) {
515 key = ((IdentifiableItem<?, ?>)arg).key;
520 return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, key);
522 return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
527 * Path argument of {@link InstanceIdentifier}.
529 * Interface which implementations are used as path components of the
530 * path in overall data tree.
532 public interface PathArgument extends Comparable<PathArgument> {
533 Class<? extends DataObject> getType();
536 private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
537 private static final long serialVersionUID = 1L;
538 private final Class<T> type;
540 protected AbstractPathArgument(final Class<T> type) {
541 this.type = Preconditions.checkNotNull(type, "Type may not be null.");
545 public final Class<T> getType() {
550 public int hashCode() {
551 return type.hashCode();
555 public boolean equals(final Object obj) {
562 if (getClass() != obj.getClass()) {
565 final AbstractPathArgument<?> other = (AbstractPathArgument<?>) obj;
566 return type.equals(other.type);
570 public int compareTo(final PathArgument arg) {
571 return type.getCanonicalName().compareTo(arg.getType().getCanonicalName());
576 * An Item represents an object that probably is only one of it's kind. For example a Nodes object is only one of
577 * a kind. In YANG terms this would probably represent a container.
581 public static final class Item<T extends DataObject> extends AbstractPathArgument<T> {
582 private static final long serialVersionUID = 1L;
584 public Item(final Class<T> type) {
589 public String toString() {
590 return getType().getName();
595 * An IdentifiableItem represents a object that is usually present in a collection and can be identified uniquely
596 * by a key. In YANG terms this would probably represent an item in a list.
598 * @param <I> An object that is identifiable by an identifier
599 * @param <T> The identifier of the object
601 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> extends AbstractPathArgument<I> {
602 private static final long serialVersionUID = 1L;
605 public IdentifiableItem(final Class<I> type, final T key) {
607 this.key = Preconditions.checkNotNull(key, "Key may not be null.");
615 public boolean equals(final Object obj) {
616 return super.equals(obj) && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
620 public int hashCode() {
621 return super.hashCode() * 31 + key.hashCode();
625 public String toString() {
626 return getType().getName() + "[key=" + key + "]";
631 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
633 * Append the specified container as a child of the current InstanceIdentifier referenced by the builder.
635 * This method should be used when you want to build an instance identifier by appending top-level
640 * InstanceIdentifier.builder().child(Nodes.class).build();
644 * NOTE :- The above example is only for illustration purposes InstanceIdentifier.builder() has been deprecated
645 * and should not be used. Use InstanceIdentifier.builder(Nodes.class) instead
651 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(
655 * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder.
657 * This method should be used when you want to build an instance identifier by appending a specific list element
666 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
667 Class<N> listItem, K listKey);
670 * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
677 <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
681 * Build the instance identifier.
686 InstanceIdentifier<T> build();
689 * @deprecated use #build()
692 InstanceIdentifier<T> toInstance();
695 private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
696 out.defaultWriteObject();
697 out.writeInt(Iterables.size(pathArguments));
698 for (Object o : pathArguments) {
703 private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
704 in.defaultReadObject();
706 final int size = in.readInt();
707 final List<PathArgument> args = new ArrayList<>(size);
708 for (int i = 0; i < size; ++i) {
709 args.add((PathArgument) in.readObject());
713 PATHARGUMENTS_FIELD.set(this, ImmutableList.copyOf(args));
714 } catch (IllegalArgumentException | IllegalAccessException e) {
715 throw new IOException(e);