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 java.util.Collections;
11 import java.util.List;
13 import org.opendaylight.yangtools.concepts.Builder;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.concepts.Path;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
22 * This instance identifier uniquely identifies a specific DataObject in the data tree modeled by YANG.
24 * For Example let's say you were trying to refer to a node in inventory which was modeled in YANG as follows,
27 * module opendaylight-inventory {
33 * ext:context-instance "node-context";
42 * You could create an instance identifier as follows to get to a node with id "openflow:1"
44 * InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
46 * This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
49 public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>,Immutable {
51 private final List<PathArgument> path;
52 private final Class<T> targetType;
55 * Create an instance identifier for a very specific object type.
59 * new InstanceIdentifier(Nodes.class)
61 * would create an InstanceIdentifier for an object of type Nodes
63 * @param type The type of the object which this instance identifier represents
65 public InstanceIdentifier(Class<T> type) {
66 this(Collections.<PathArgument> singletonList(new Item<>(type)), type);
70 * Create an instance identifier for a very specific object type.
74 * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
75 * new InstanceIdentifier(path, Nodes.class);
78 * @param path The path to a specific node in the data tree
79 * @param type The type of the object which this instance identifier represents
81 public InstanceIdentifier(List<PathArgument> path, Class<T> type) {
82 this.path = ImmutableList.copyOf(path);
83 this.targetType = type;
88 * @return A list of the elements of the path
90 public List<PathArgument> getPath() {
91 return getPathArguments();
96 * @return A list of the elements of the path
99 public List<PathArgument> getPathArguments() {
105 * @return The target type of this instance identifier
107 public Class<T> getTargetType() {
108 return this.targetType;
112 public String toString() {
113 return "InstanceIdentifier [path=" + path + "]";
117 * Return an instance identifier trimmed at the first occurrence of a
118 * specific component type.
120 * For example let's say an instance identifier was built like so,
122 * identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
125 * And you wanted to obtain the Instance identifier which represented Nodes you would do it like so,
128 * identifier.firstIdentifierOf(Nodes.class)
131 * @param type component type
132 * @return trimmed instance identifier, or null if the component type
135 @SuppressWarnings("hiding")
136 public <T extends DataObject> InstanceIdentifier<T> firstIdentifierOf(final Class<T> type) {
138 for (final PathArgument a : path) {
139 if (type.equals(a.getType())) {
140 return new InstanceIdentifier<>(path.subList(0, i), type);
150 * Return the key associated with the first component of specified type in
153 * @param listItem component type
154 * @param listKey component key type
155 * @return key associated with the component, or null if the component type
158 public <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
159 for (PathArgument i : path) {
160 if (listItem.equals(i.getType())) {
161 @SuppressWarnings("unchecked")
162 final K ret = ((IdentifiableItem<N, K>)i).getKey();
171 * Return the key associated with the last component of the specified identifier.
173 * @param id instance identifier
174 * @return key associated with the last component
176 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
177 @SuppressWarnings("unchecked")
178 final K ret = ((IdentifiableItem<N, K>)Iterables.getLast(id.getPath())).getKey();
183 * Path argument of {@link InstanceIdentifier}.
185 * Interface which implementations are used as path components of the
186 * path in overall data tree.
189 public interface PathArgument {
191 Class<? extends DataObject> getType();
196 * An Item represents an object that probably is only one of it's kind. For example a Nodes object is only one of
197 * a kind. In YANG terms this would probably represent a container.
201 public static final class Item<T extends DataObject> implements PathArgument {
202 private final Class<T> type;
204 public Item(Class<T> type) {
209 public Class<T> getType() {
214 public int hashCode() {
215 final int prime = 31;
217 result = prime * result + ((type == null) ? 0 : type.hashCode());
222 public boolean equals(Object obj) {
227 if (getClass() != obj.getClass())
229 Item<?> other = (Item<?>) obj;
231 if (other.type != null)
233 } else if (!type.equals(other.type))
239 public String toString() {
240 return type.getName();
245 * An IdentifiableItem represents a object that is usually present in a collection and can be identified uniquely
246 * by a key. In YANG terms this would probably represent an item in a list.
248 * @param <I> An object that is identifiable by an identifier
249 * @param <T> The identifier of the object
251 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
255 private final Class<I> type;
257 public IdentifiableItem(Class<I> type, T key) {
259 throw new IllegalArgumentException("Type must not be null.");
261 throw new IllegalArgumentException("Key must not be null.");
271 public Class<I> getType() {
276 public boolean equals(Object obj) {
280 if (obj.hashCode() != hashCode()) {
283 if (!(obj instanceof IdentifiableItem<?, ?>)) {
286 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
287 return key.equals(foreign.getKey());
291 public int hashCode() {
292 return key.hashCode();
296 public String toString() {
297 return type.getName() + "[key=" + key + "]";
301 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
303 * @deprecated use {@link child(Class)} or {@link augmentation(Class)} instead.
306 <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
309 * @deprecated use {@link child(Class,Identifier)} or {@link augmentation(Class,Identifier)} instead.
312 <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
313 Class<N> listItem, K listKey);
316 * Append the specified container as a child of the current InstanceIdentifier referenced by the builder.
318 * This method should be used when you want to build an instance identifier by appending top-level
323 * InstanceIdentifier.builder().child(Nodes.class).build();
327 * NOTE :- The above example is only for illustration purposes InstanceIdentifier.builder() has been deprecated
328 * and should not be used. Use InstanceIdentifier.builder(Nodes.class) instead
334 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
337 * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder.
339 * This method should be used when you want to build an instance identifier by appending a specific list element
348 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
349 Class<N> listItem, K listKey);
352 * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
359 <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
362 * Build the instance identifier.
366 InstanceIdentifier<T> build();
371 * @deprecated use {@link builder(Class)} or {@link builder(Class,Identifier)} instead.
374 @SuppressWarnings("rawtypes")
375 public static InstanceIdentifierBuilder<?> builder() {
376 return new BuilderImpl();
380 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container
386 public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(Class<T> container) {
387 return new BuilderImpl<T>().addNode(container);
391 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an IdentifiableItem
399 public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
400 Class<N> listItem, K listKey) {
401 return new BuilderImpl<N>().addNode(listItem, listKey);
405 * Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
411 public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(InstanceIdentifier<T> basePath) {
412 return new BuilderImpl<T>(basePath.path,basePath.targetType);
415 private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
417 private final ImmutableList.Builder<PathArgument> path;
418 private Class<? extends DataObject> target = null;
420 public BuilderImpl() {
421 this.path = ImmutableList.builder();
424 public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
425 this.path = ImmutableList.<PathArgument>builder().addAll(prefix);
426 this.target = target;
429 @SuppressWarnings("unchecked")
430 private <N extends DataObject> InstanceIdentifierBuilder<N> addNode(Class<N> container) {
432 path.add(new Item<N>(container));
433 return (InstanceIdentifierBuilder<N>) this;
436 @SuppressWarnings("unchecked")
437 private <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> addNode(
438 Class<N> listItem, K listKey) {
440 path.add(new IdentifiableItem<N, K>(listItem, listKey));
441 return (InstanceIdentifierBuilder<N>) this;
444 @SuppressWarnings({ "unchecked", "rawtypes" })
446 public InstanceIdentifier<T> toInstance() {
447 return new InstanceIdentifier(path.build(), target);
451 public InstanceIdentifier<T> build() {
456 public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
457 return addNode(container);
461 public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
462 Class<N> listItem, K listKey) {
463 return addNode(listItem, listKey);
467 public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
468 return addNode(container);
472 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
473 Class<N> listItem, K listKey) {
474 return addNode(listItem,listKey);
478 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
479 Class<N> container) {
480 return addNode(container);
484 public int hashCode() {
485 final int prime = 31;
487 result = prime * result + ((path == null) ? 0 : path.hashCode());
493 public int hashCode() {
494 final int prime = 31;
496 result = prime * result + ((path == null) ? 0 : path.hashCode());
501 public boolean equals(Object obj) {
508 if (getClass() != obj.getClass()) {
511 InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
513 if (other.path != null) {
516 } else if (!path.equals(other.path)) {
523 * The contains method checks if the other identifier is fully contained within the current identifier. It does this
524 * by looking at only the types of the path arguments and not by comparing the path arguments themselse.
525 * If you want to compare path arguments you must use containsWildcarded
527 * To illustrate here is an example which explains the working of this api.
529 * Let's say you have two instance identifiers as follows,
531 * this = /nodes/node/openflow:1
532 * other = /nodes/node/openflow:2
534 * then this.contains(other) will return true. To ensure that this and other are compared properly you must use
541 public boolean contains(final InstanceIdentifier<?> other) {
543 throw new IllegalArgumentException("other should not be null");
545 final int localSize = this.path.size();
546 final List<PathArgument> otherPath = other.getPath();
547 if(localSize > other.path.size()) {
550 for(int i = 0;i<localSize;i++ ) {
551 if(!path.get(i).equals(otherPath.get(i))) {
559 * The containsWildcarded method checks if the other identifier is fully contained within the current identifier.
560 * It does this by looking at both the type and identity of the path arguments.
565 public boolean containsWildcarded(final InstanceIdentifier<?> other) {
567 throw new IllegalArgumentException("other should not be null");
569 final int localSize = this.path.size();
570 final List<PathArgument> otherPath = other.getPath();
571 if(localSize > other.path.size()) {
574 for(int i = 0;i<localSize;i++ ) {
575 final PathArgument localArgument = path.get(i);
576 final PathArgument otherArgument = otherPath.get(i);
577 if(!localArgument.getType().equals(otherArgument.getType())) {
580 if(localArgument instanceof IdentifiableItem<?, ?> && otherArgument instanceof IdentifiableItem<?, ?> && !localArgument.equals(otherPath.get(i))) {
587 public boolean isWildcarded() {
588 for(PathArgument pathArgument : path) {
589 if(Identifiable.class.isAssignableFrom(pathArgument.getType()) && !(pathArgument instanceof IdentifiableItem<?, ?>)) {