17ee582aad255bc146680c7471428d52894164a8
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / ActionStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.meta;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
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.ActionEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
25 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
26 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
28 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins;
29 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractSchemaTreeStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
40
41 public final class ActionStatementSupport extends
42         AbstractSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
43
44     private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
45             YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
46
47     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
48         SubstatementValidator.builder(YangStmtMapping.ACTION)
49             .addOptional(YangStmtMapping.DESCRIPTION)
50             .addAny(YangStmtMapping.GROUPING)
51             .addAny(YangStmtMapping.IF_FEATURE)
52             .addOptional(YangStmtMapping.INPUT)
53             .addOptional(YangStmtMapping.OUTPUT)
54             .addOptional(YangStmtMapping.REFERENCE)
55             .addOptional(YangStmtMapping.STATUS)
56             .addAny(YangStmtMapping.TYPEDEF)
57             .build();
58
59     private final InputStatementSupport implicitInput;
60     private final OutputStatementSupport implicitOutput;
61
62     public ActionStatementSupport(final YangParserConfiguration config, final InputStatementSupport implicitInput,
63              final OutputStatementSupport implicitOutput) {
64         super(YangStmtMapping.ACTION, uninstantiatedPolicy(), config, SUBSTATEMENT_VALIDATOR);
65         this.implicitInput = requireNonNull(implicitInput);
66         this.implicitOutput = requireNonNull(implicitOutput);
67     }
68
69     @Override
70     public void onStatementAdded(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
71         final QName argument = stmt.getArgument();
72         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), stmt,
73             "Action %s is defined within a notification, rpc or another action", argument);
74         SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), stmt,
75             "Action %s is defined within a case statement", argument);
76         SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.MODULE), stmt,
77             "Action %s is defined at the top level of a module", stmt.getArgument());
78         StmtContextUtils.validateNoKeylessListAncestorOf(stmt, "Action");
79
80         super.onStatementAdded(stmt);
81     }
82
83     @Override
84     public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
85         super.onFullDefinitionDeclared(stmt);
86
87         verify(stmt instanceof StatementContextBase);
88         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
89             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(implicitInput, null);
90         }
91         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
92             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(implicitOutput, null);
93         }
94     }
95
96     @Override
97     protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
98             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
99         return DeclaredStatements.createAction(ctx.getArgument(), substatements);
100     }
101
102     @Override
103     protected ActionStatement attachDeclarationReference(final ActionStatement stmt,
104             final DeclarationReference reference) {
105         return DeclaredStatementDecorators.decorateAction(stmt, reference);
106     }
107
108     @Override
109     protected ActionEffectiveStatement createEffective(final Current<QName, ActionStatement> stmt,
110             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
111         final StatementSourceReference ref = stmt.sourceReference();
112         verify(!substatements.isEmpty(), "Missing implicit input/output statements at %s", ref);
113
114         try {
115             return EffectiveStatements.createAction(stmt.declared(), stmt.effectivePath(),
116                 EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements), substatements);
117         } catch (SubstatementIndexingException e) {
118             throw new SourceException(e.getMessage(), stmt, e);
119         }
120     }
121
122     @Override
123     public ActionEffectiveStatement copyEffective(final Current<QName, ActionStatement> stmt,
124             final ActionEffectiveStatement original) {
125         return EffectiveStatements.copyAction(original, stmt.effectivePath(),
126             EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), original.effectiveSubstatements()));
127     }
128 }