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