Ignore empty augmentations at runtime
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / UsesAugmentGenerator.java
1 /*
2  * Copyright (c) 2020 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 com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
20
21 /**
22  * Generator corresponding to a {@code augment} statement used as a child of a {@code uses} statement.
23  */
24 final class UsesAugmentGenerator extends AbstractAugmentGenerator {
25     private final UsesEffectiveStatement uses;
26
27     private GroupingGenerator grouping;
28
29     UsesAugmentGenerator(final AugmentEffectiveStatement statement, final UsesEffectiveStatement uses,
30             final AbstractCompositeGenerator<?, ?> parent) {
31         super(statement, parent);
32         this.uses = requireNonNull(uses);
33     }
34
35     void resolveGrouping(final UsesEffectiveStatement resolvedUses, final GroupingGenerator resolvedGrouping) {
36         if (resolvedUses == uses) {
37             verify(grouping == null, "Attempted to re-resolve grouping of %s", this);
38             grouping = requireNonNull(resolvedGrouping);
39         }
40     }
41
42     @NonNull AugmentRequirement startLinkage() {
43         // Here we are going in the opposite direction of RFC7950, section 7.13:
44         //
45         //    The effect of a "uses" reference to a grouping is that the nodes
46         //    defined by the grouping are copied into the current schema tree and
47         //    are then updated according to the "refine" and "augment" statements.
48         //
49         // Our parent here is *not* the 'uses' statement, but rather the statement which contains it.
50         return new AugmentRequirement(this, verifyNotNull(grouping, "Unresolved grouping in %s", this));
51     }
52
53     @Override
54     boolean matchesInstantiated(final AugmentEffectiveStatement statement) {
55         return super.matchesInstantiated(statement) || declared(statement()).equals(declared(statement));
56     }
57
58     private static AugmentStatement declared(final AugmentEffectiveStatement statement) {
59         // We are generating Augmentation interfaces on declared model, hence this is accurate
60         return verifyNotNull(statement.getDeclared(), " %s does not have a declared view", statement);
61     }
62
63     @Override
64     TargetAugmentEffectiveStatement effectiveIn(final SchemaTreeAwareEffectiveStatement<?, ?> target) {
65         verify(target instanceof SchemaTreeEffectiveStatement, "Unexpected statement %s", target);
66         // 'uses'/'augment': our children are binding to target's namespace
67
68         final var targetNamespace = ((SchemaTreeEffectiveStatement<?>) target).argument().getModule();
69         return effectiveIn(target, qname -> qname.bindTo(targetNamespace));
70     }
71 }