Remove yang.parser.spi.meta.AbstractDeclaredStatement
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / BaseOperationContainerStatementSupport.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.yangtools.yang.parser.rfc7950.stmt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.function.Function;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 /**
29  * Specialization of {@link AbstractQNameStatementSupport} for {@code input} and {@code output} statements.
30  *
31  * @param <D> Declared Statement representation
32  * @param <E> Effective Statement representation
33  */
34 @Beta
35 public abstract class BaseOperationContainerStatementSupport<D extends DeclaredStatement<QName>,
36         E extends SchemaTreeEffectiveStatement<D>> extends BaseImplicitStatementSupport<D, E> {
37     private final Function<QNameModule, QName> createArgument;
38
39     protected BaseOperationContainerStatementSupport(final StatementDefinition publicDefinition,
40             final Function<QNameModule, QName> createArgument) {
41         super(publicDefinition, uninstantiatedPolicy());
42         this.createArgument = requireNonNull(createArgument);
43     }
44
45     @Override
46     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47         return createArgument.apply(StmtContextUtils.getRootModuleQName(ctx));
48     }
49
50     @Override
51     protected final @NonNull E copyDeclaredEffective(final Current<QName, D> stmt, final E original) {
52         return copyDeclaredEffective(
53             EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), original.effectiveSubstatements()),
54             stmt, original);
55     }
56
57     protected abstract @NonNull E copyDeclaredEffective(int flags, @NonNull Current<QName, D> stmt,
58         @NonNull E original);
59
60     @Override
61     protected final @NonNull E copyUndeclaredEffective(final Current<QName, D> stmt, final E original) {
62         return copyUndeclaredEffective(
63             EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), original.effectiveSubstatements()),
64             stmt, original);
65     }
66
67     protected abstract @NonNull E copyUndeclaredEffective(int flags, @NonNull Current<QName, D> stmt,
68         @NonNull E original);
69
70     @Override
71     protected final @NonNull E createDeclaredEffective(final Current<QName, D> stmt,
72             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
73         try {
74             return createDeclaredEffective(
75                 EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements), stmt, substatements);
76         } catch (SubstatementIndexingException e) {
77             throw new SourceException(e.getMessage(), stmt, e);
78         }
79     }
80
81     protected abstract @NonNull E createDeclaredEffective(int flags, @NonNull Current<QName, D> stmt,
82             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
83
84     @Override
85     protected final E createUndeclaredEffective(final Current<QName, D> stmt,
86             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
87         return createUndeclaredEffective(EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements),
88             stmt, substatements);
89     }
90
91     protected abstract @NonNull E createUndeclaredEffective(int flags, @NonNull Current<QName, D> stmt,
92             @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements);
93 }