e933dbaaed7a20b35acaa0524700e18ff9f09841
[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.SchemaTreeEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
18
19 /**
20  * Generator corresponding to a {@code augment} statement used as a child of a {@code uses} statement.
21  */
22 final class UsesAugmentGenerator extends AbstractAugmentGenerator {
23     private final UsesEffectiveStatement uses;
24
25     private GroupingGenerator grouping;
26
27     UsesAugmentGenerator(final AugmentEffectiveStatement statement, final UsesEffectiveStatement uses,
28             final AbstractCompositeGenerator<?, ?> parent) {
29         super(statement, parent);
30         this.uses = requireNonNull(uses);
31
32         // FIXME: use SchemaTreeAwareEffectiveStatement
33         var stmt = parent.statement();
34         for (var qname : statement.argument().getNodeIdentifiers()) {
35             final var tmp = stmt;
36             stmt = stmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class)
37                 .filter(child -> qname.equals(child.argument()))
38                 .findFirst()
39                 .orElseThrow(() -> new IllegalStateException("Failed to find " + qname + " in " + tmp));
40         }
41         setTargetStatement(stmt);
42     }
43
44     void resolveGrouping(final UsesEffectiveStatement resolvedUses, final GroupingGenerator resolvedGrouping) {
45         if (resolvedUses == uses) {
46             verify(grouping == null, "Attempted to re-resolve grouping of %s", this);
47             grouping = requireNonNull(resolvedGrouping);
48         }
49     }
50
51     @NonNull AugmentRequirement startLinkage() {
52         // Here we are going in the opposite direction of RFC7950, section 7.13:
53         //
54         //    The effect of a "uses" reference to a grouping is that the nodes
55         //    defined by the grouping are copied into the current schema tree and
56         //    are then updated according to the "refine" and "augment" statements.
57         //
58         // Our parent here is *not* the 'uses' statement, but rather the statement which contains it.
59         return new AugmentRequirement(this, verifyNotNull(grouping, "Unresolved grouping in %s", this));
60     }
61 }