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