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