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