Refactor mdsal-binding-generator artifacts
[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.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
17
18 /**
19  * Generator corresponding to a {@code augment} statement used as a child of a {@code uses} statement.
20  */
21 final class UsesAugmentGenerator extends AbstractAugmentGenerator {
22     private final UsesEffectiveStatement uses;
23
24     private GroupingGenerator grouping;
25
26     UsesAugmentGenerator(final AugmentEffectiveStatement statement, final AbstractCompositeGenerator<?> parent,
27             final UsesEffectiveStatement uses) {
28         super(statement, parent);
29         this.uses = requireNonNull(uses);
30     }
31
32     void linkGroupingDependency(final UsesEffectiveStatement checkUses, final GroupingGenerator resolvedGrouping) {
33         if (uses.equals(checkUses)) {
34             verify(grouping == null, "Attempted to relink %s from %s to %s", this, grouping, resolvedGrouping);
35             this.grouping = requireNonNull(resolvedGrouping);
36         }
37     }
38
39     @Override
40     void loadTargetGenerator() {
41         final GroupingGenerator grp = verifyNotNull(grouping, "No grouping linked in %s", this);
42         final SchemaNodeIdentifier path = statement().argument();
43
44         /*
45          *  Here we are going in the opposite direction of RFC7950, section 3.13:
46          *
47          *        The effect of a "uses" reference to a grouping is that the nodes
48          *        defined by the grouping are copied into the current schema tree and
49          *        are then updated according to the "refine" and "augment" statements.
50          *
51          *  Our argument is composed of QNames in the current schema tree's namespace, but the grouping may have been
52          *  defined in a different module -- and therefore it knows those children under its namespace. Adjust the path
53          *  we are searching if that happens to be the case.
54          */
55         setTargetGenerator(grp.resolveSchemaNode(path, grp.statement().argument().getModule()));
56     }
57 }