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 java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
18 import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement.SchemaTreeNamespace;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
28 * Generator corresponding to a {@code augment} statement used as a child of a {@code uses} statement.
30 final class UsesAugmentGenerator extends AbstractAugmentGenerator {
31 private final UsesEffectiveStatement uses;
33 private GroupingGenerator grouping;
35 UsesAugmentGenerator(final AugmentEffectiveStatement statement, final UsesEffectiveStatement uses,
36 final AbstractCompositeGenerator<?, ?> parent) {
37 super(statement, parent);
38 this.uses = requireNonNull(uses);
40 // FIXME: use SchemaTreeAwareEffectiveStatement
41 var stmt = parent.statement();
42 for (var qname : statement.argument().getNodeIdentifiers()) {
44 stmt = stmt.streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class)
45 .filter(child -> qname.equals(child.argument()))
47 .orElseThrow(() -> new IllegalStateException("Failed to find " + qname + " in " + tmp));
49 setTargetStatement(stmt);
52 void resolveGrouping(final UsesEffectiveStatement resolvedUses, final GroupingGenerator resolvedGrouping) {
53 if (resolvedUses == uses) {
54 verify(grouping == null, "Attempted to re-resolve grouping of %s", this);
55 grouping = requireNonNull(resolvedGrouping);
59 @NonNull AugmentRequirement startLinkage() {
60 // Here we are going in the opposite direction of RFC7950, section 7.13:
62 // The effect of a "uses" reference to a grouping is that the nodes
63 // defined by the grouping are copied into the current schema tree and
64 // are then updated according to the "refine" and "augment" statements.
66 // Our parent here is *not* the 'uses' statement, but rather the statement which contains it.
67 return new AugmentRequirement(this, verifyNotNull(grouping, "Unresolved grouping in %s", this));
71 List<CaseRuntimeType> augmentedCasesIn(final ChildLookup lookup, final ChoiceEffectiveStatement stmt) {
72 final var result = super.augmentedCasesIn(lookup, stmt);
76 final var augment = statement();
77 if (!lookup.contains(augment)) {
81 final var effectiveStatement = effectiveStatement(augment, stmt);
82 return createBuilder(effectiveStatement)
83 .fillTypes(lookup.inStatement(effectiveStatement), this)
88 AugmentRuntimeType runtimeTypeIn(final ChildLookup lookup, final EffectiveStatement<?, ?> target) {
89 final var result = super.runtimeTypeIn(lookup, target);
93 final var augment = statement();
94 if (!lookup.contains(augment)) {
98 verify(target instanceof SchemaTreeAwareEffectiveStatement && target instanceof SchemaTreeEffectiveStatement,
99 "Unexpected statement %s", target);
100 final var effectiveStatement = effectiveStatement(augment, (SchemaTreeAwareEffectiveStatement<?, ?>) target);
101 return verifyNotNull(createInternalRuntimeType(lookup.inStatement(effectiveStatement), effectiveStatement));
104 private static @NonNull AugmentEffectiveStatement effectiveStatement(final AugmentEffectiveStatement augment,
105 final SchemaTreeAwareEffectiveStatement<?, ?> target) {
106 verify(target instanceof SchemaTreeEffectiveStatement, "Unexpected statement %s", target);
107 // 'uses'/'augment': our children are binding to target's namespace
108 final var targetNamespace = ((SchemaTreeEffectiveStatement<?>) target).argument().getModule();
110 final var stmts = augment.effectiveSubstatements();
111 final var builder = ImmutableList.<EffectiveStatement<?, ?>>builderWithExpectedSize(stmts.size());
112 for (var stmt : stmts) {
113 if (stmt instanceof SchemaTreeEffectiveStatement) {
114 final var qname = ((SchemaTreeEffectiveStatement<?>) stmt).getIdentifier().bindTo(targetNamespace);
115 target.get(SchemaTreeNamespace.class, qname).ifPresent(builder::add);
121 return new TargetAugmentEffectiveStatement(augment, target, builder.build());