2 * Copyright (c) 2022 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.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.ChildOf;
20 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.DataRoot;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
27 * A combination of an {@link InstanceNotification} class and its corresponding instantiation wildcard, expressed as
28 * an {@link InstanceIdentifier}. This glue is required because instance notification interfaces are generated at the
29 * place of their definition, most importantly in {@code grouping} and we actually need to bind to a particular
30 * instantiation (e.g. a place where {@code uses} references that grouping).
32 * @param <N> Generated InstanceNotification interface type
33 * @param <P> Instance notification parent type
36 public final class InstanceNotificationSpec<N extends InstanceNotification<N, P>, P extends DataObject>
37 implements Immutable {
38 private final @NonNull InstanceIdentifier<P> path;
39 private final @NonNull Class<N> type;
41 private InstanceNotificationSpec(final Class<N> type, final InstanceIdentifier<P> path) {
42 this.type = requireNonNull(type);
43 this.path = requireNonNull(path);
46 public static <P extends ChildOf<? extends DataRoot>> @NonNull Builder<P> builder(final Class<P> container) {
47 return new Builder<>(InstanceIdentifier.builder(container));
50 public static <C extends ChoiceIn<? extends DataRoot> & DataObject, P extends ChildOf<? super C>>
51 @NonNull Builder<P> builder(final Class<C> caze, final Class<P> container) {
52 return new Builder<>(InstanceIdentifier.builder(caze, container));
55 public @NonNull InstanceIdentifier<P> path() {
59 public @NonNull Class<N> type() {
64 public int hashCode() {
65 return Objects.hash(type, path);
69 public boolean equals(final Object obj) {
73 if (!(obj instanceof InstanceNotificationSpec)) {
76 final var other = (InstanceNotificationSpec<?, ?>) obj;
77 return type.equals(other.type) && path.equals(other.path);
81 public String toString() {
82 return MoreObjects.toStringHelper(this).add("action", type).add("path", path).toString();
86 public static final class Builder<P extends DataObject> implements Mutable {
87 private final InstanceIdentifier.Builder<P> pathBuilder;
89 Builder(final InstanceIdentifier.Builder<P> pathBuilder) {
90 this.pathBuilder = requireNonNull(pathBuilder);
93 public <N extends ChildOf<? super P>> @NonNull Builder<N> withPathChild(final Class<N> container) {
94 pathBuilder.child(container);
98 public <C extends ChoiceIn<? super P> & DataObject, N extends ChildOf<? super C>>
99 @NonNull Builder<N> withPathChild(final Class<C> caze, final Class<N> container) {
100 pathBuilder.child(caze, container);
104 public <N extends DataObject & Augmentation<? super P>> @NonNull Builder<N> withPathAugmentation(
105 final Class<N> container) {
106 pathBuilder.augmentation(container);
110 public <N extends InstanceNotification<N, P>> @NonNull InstanceNotificationSpec<N, P> build(
111 final Class<N> type) {
112 return new InstanceNotificationSpec<>(type, pathBuilder.build());
115 @SuppressWarnings("unchecked")
116 private <N extends DataObject> @NonNull Builder<N> castThis() {
117 return (Builder<N>) this;