2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.mdsal.binding.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.concepts.Mutable;
18 import org.opendaylight.yangtools.yang.binding.Action;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.ChildOf;
21 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.DataRoot;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
28 * A combination of an {@link Action} class and its corresponding instantiation wildcard, expressed as
29 * an {@link InstanceIdentifier}. This means that {@code list}s are treated exactly as @{code container}s are, e.g.
30 * without a key value specification.
33 * This glue is required because action interfaces are generated at the place of their
34 * definition, most importantly in {@code grouping} and we actually need to bind to a particular instantiation (e.g. a
35 * place where {@code uses} references that grouping).
37 * @param <A> Generated Action interface type
38 * @param <P> Action parent type
41 public final class ActionSpec<A extends Action<? extends InstanceIdentifier<P>, ?, ?>, P extends DataObject>
42 implements Immutable {
43 private final @NonNull InstanceIdentifier<P> path;
44 private final @NonNull Class<A> type;
46 private ActionSpec(final Class<A> type, final InstanceIdentifier<P> path) {
47 this.type = requireNonNull(type);
48 this.path = requireNonNull(path);
51 public static <P extends ChildOf<? extends DataRoot>> @NonNull Builder<P> builder(final Class<P> container) {
52 return new Builder<>(InstanceIdentifier.builder(container));
55 public static <C extends ChoiceIn<? extends DataRoot> & DataObject, P extends ChildOf<? super C>>
56 @NonNull Builder<P> builder(final Class<C> caze, final Class<P> container) {
57 return new Builder<>(InstanceIdentifier.builder(caze, container));
60 public @NonNull InstanceIdentifier<P> path() {
64 public @NonNull Class<A> type() {
69 public int hashCode() {
70 return Objects.hash(type, path);
74 public boolean equals(final Object obj) {
78 if (!(obj instanceof ActionSpec)) {
81 final var other = (ActionSpec<?, ?>) obj;
82 return type.equals(other.type) && path.equals(other.path);
86 public String toString() {
87 return MoreObjects.toStringHelper(this).add("action", type).add("path", path).toString();
91 public static final class Builder<P extends DataObject> implements Mutable {
92 private final InstanceIdentifierBuilder<P> pathBuilder;
94 Builder(final InstanceIdentifierBuilder<P> pathBuilder) {
95 this.pathBuilder = requireNonNull(pathBuilder);
98 public <N extends ChildOf<? super P>> @NonNull Builder<N> withPathChild(final Class<N> container) {
99 pathBuilder.child(container);
103 public <C extends ChoiceIn<? super P> & DataObject, N extends ChildOf<? super C>>
104 @NonNull Builder<N> withPathChild(final Class<C> caze, final Class<N> container) {
105 pathBuilder.child(caze, container);
109 public <N extends DataObject & Augmentation<? super P>> @NonNull Builder<N> withPathAugmentation(
110 final Class<N> container) {
111 pathBuilder.augmentation(container);
115 public <A extends Action<? extends InstanceIdentifier<P>, ?, ?>> @NonNull ActionSpec<A, P> build(
116 final Class<A> type) {
117 return new ActionSpec<>(type, pathBuilder.build());
120 @SuppressWarnings("unchecked")
121 private <N extends DataObject> @NonNull Builder<N> castThis() {
122 return (Builder<N>) this;