Do not use ListenerRegistration
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / GeneratorContext.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.api.PathExpression;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19
20 /**
21  * Abstract view on generation tree as viewed by a particular {@link Generator}.
22  */
23 abstract class GeneratorContext {
24     private final @NonNull EffectiveModelContext modelContext;
25
26     GeneratorContext(final EffectiveModelContext modelContext) {
27         this.modelContext = requireNonNull(modelContext);
28     }
29
30     final @NonNull EffectiveModelContext modelContext() {
31         return modelContext;
32     }
33
34     /**
35      * Resolve generator for the type object pointed to by a {@code path} expression, or {@code null} it the if cannot
36      * the current generator is nested inside a {@code grouping} and the generator cannot be found.
37      *
38      * @param path A {@code path} expression
39      * @return Resolved generator, or {@code null} if legally not found
40      * @throws NullPointerException if {@code path} is {@code null}
41      * @throws IllegalStateException if this generator is not inside a {@code grouping} and the path cannot be resolved
42      */
43     abstract @Nullable AbstractTypeObjectGenerator<?, ?> resolveLeafref(@NonNull PathExpression path);
44
45     /**
46      * Resolve a tree-scoped namespace reference. This covers {@code typedef} and {@code grouping} statements, as per
47      * bullets 5 and 6 of <a href="https://tools.ietf.org/html/rfc6020#section-6.2.1">RFC6020, section 6.2.1</a>.
48      *
49      * @param <E> {@link EffectiveStatement} type
50      * @param type EffectiveStatement class
51      * @param argument Statement argument
52      * @return Resolved {@link Generator}
53      * @throws NullPointerException if any argument is null
54      * @throws IllegalStateException if the generator cannot be found
55      */
56     abstract <E extends EffectiveStatement<QName, ?>, G extends AbstractExplicitGenerator<E, ?>>
57         @NonNull G resolveTreeScoped(@NonNull Class<G> type, @NonNull QName argument);
58
59     abstract @NonNull ModuleGenerator resolveModule(@NonNull QNameModule namespace);
60
61     final @NonNull IdentityGenerator resolveIdentity(final @NonNull QName name) {
62         for (Generator gen : resolveModule(name.getModule())) {
63             if (gen instanceof final IdentityGenerator idgen) {
64                 if (name.equals(idgen.statement().argument())) {
65                     return idgen;
66                 }
67             }
68         }
69         throw new IllegalStateException("Failed to find identity " + name);
70     }
71
72     final @NonNull TypedefGenerator resolveTypedef(final @NonNull QName qname) {
73         return resolveTreeScoped(TypedefGenerator.class, qname);
74     }
75 }