2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
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;
14 import com.google.common.collect.ImmutableList;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement.SchemaTreeNamespace;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
25 * Generator corresponding to a {@code augment} statement used as a child of a {@code uses} statement.
27 final class UsesAugmentGenerator extends AbstractAugmentGenerator {
28 private final UsesEffectiveStatement uses;
30 private GroupingGenerator grouping;
32 UsesAugmentGenerator(final AugmentEffectiveStatement statement, final UsesEffectiveStatement uses,
33 final AbstractCompositeGenerator<?, ?> parent) {
34 super(statement, parent);
35 this.uses = requireNonNull(uses);
38 void resolveGrouping(final UsesEffectiveStatement resolvedUses, final GroupingGenerator resolvedGrouping) {
39 if (resolvedUses == uses) {
40 verify(grouping == null, "Attempted to re-resolve grouping of %s", this);
41 grouping = requireNonNull(resolvedGrouping);
45 @NonNull AugmentRequirement startLinkage() {
46 // Here we are going in the opposite direction of RFC7950, section 7.13:
48 // The effect of a "uses" reference to a grouping is that the nodes
49 // defined by the grouping are copied into the current schema tree and
50 // are then updated according to the "refine" and "augment" statements.
52 // Our parent here is *not* the 'uses' statement, but rather the statement which contains it.
53 return new AugmentRequirement(this, verifyNotNull(grouping, "Unresolved grouping in %s", this));
57 boolean matchesInstantiated(final AugmentEffectiveStatement statement) {
58 return super.matchesInstantiated(statement) || declared(statement()).equals(declared(statement));
61 private static AugmentStatement declared(final AugmentEffectiveStatement statement) {
62 // We are generating Augmentation interfaces on declared model, hence this is accurate
63 return verifyNotNull(statement.getDeclared(), " %s does not have a declared view", statement);
67 TargetAugmentEffectiveStatement effectiveIn(final SchemaTreeAwareEffectiveStatement<?, ?> target) {
68 verify(target instanceof SchemaTreeEffectiveStatement, "Unexpected statement %s", target);
69 // 'uses'/'augment': our children are binding to target's namespace
70 final var targetNamespace = ((SchemaTreeEffectiveStatement<?>) target).argument().getModule();
72 final var augment = statement();
73 final var stmts = augment.effectiveSubstatements();
74 final var builder = ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(stmts.size());
75 for (var stmt : stmts) {
76 if (stmt instanceof SchemaTreeEffectiveStatement) {
77 final var qname = ((SchemaTreeEffectiveStatement<?>) stmt).getIdentifier().bindTo(targetNamespace);
78 // FIXME: orElseThrow()?
79 target.get(SchemaTreeNamespace.class, qname).ifPresent(builder::add);
85 return new TargetAugmentEffectiveStatement(augment, target, builder.build());