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