Bug 9241: Action definition should implicitly define input/output
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / ActionStatementImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.Set;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
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.ImplicitSubstatement;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementDefinitionContext;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ChildSchemaNodes;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc7950.effective.ActionEffectiveStatementImpl;
41
42 public class ActionStatementImpl extends AbstractDeclaredStatement<QName> implements ActionStatement {
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
55     protected ActionStatementImpl(final StmtContext<QName,ActionStatement, ?> context) {
56         super(context);
57     }
58
59     public static class Definition
60             extends AbstractStatementSupport<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> {
61
62         private static final Set<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(YangStmtMapping.NOTIFICATION,
63                 YangStmtMapping.RPC, YangStmtMapping.ACTION);
64         private static final StatementSupport<?, ?, ?> IMPLICIT_INPUT = new InputStatementRfc7950Support();
65         private static final StatementSupport<?, ?, ?> IMPLICIT_OUTPUT = new OutputStatementRfc7950Support();
66
67         public Definition() {
68             super(YangStmtMapping.ACTION);
69         }
70
71         @Override
72         public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
73             return StmtContextUtils.qnameFromArgument(ctx, value);
74         }
75
76         @Override
77         public void onStatementAdded(
78                 final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
79             stmt.getParentContext().addToNs(ChildSchemaNodes.class, stmt.getStatementArgument(), stmt);
80         }
81
82         @Override
83         public ActionStatement createDeclared(
84                 final StmtContext<QName, ActionStatement, ?> ctx) {
85             return new ActionStatementImpl(ctx);
86         }
87
88         @Override
89         public EffectiveStatement<QName, ActionStatement> createEffective(
90                 final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> ctx) {
91             SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS),
92                     ctx.getStatementSourceReference(), "Action %s is defined within a notification, rpc or another action",
93                     ctx.getStatementArgument());
94             SourceException.throwIf(!StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST,
95                     YangStmtMapping.KEY), ctx.getStatementSourceReference(),
96                     "Action %s is defined within a list that has no key statement", ctx.getStatementArgument());
97             SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE),
98                     ctx.getStatementSourceReference(), "Action %s is defined within a case statement",
99                     ctx.getStatementArgument());
100             SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE),
101                     ctx.getStatementSourceReference(), "Action %s is defined at the top level of a module",
102                     ctx.getStatementArgument());
103             return new ActionEffectiveStatementImpl(ctx);
104         }
105
106         @Override
107         public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement,
108                 EffectiveStatement<QName, ActionStatement>> stmt) {
109             super.onFullDefinitionDeclared(stmt);
110
111             if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
112                 addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_INPUT);
113             }
114
115             if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
116                 addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_OUTPUT);
117             }
118         }
119
120         @Override
121         protected SubstatementValidator getSubstatementValidator() {
122             return SUBSTATEMENT_VALIDATOR;
123         }
124
125         private static void addImplicitStatement(final StatementContextBase<?, ?, ?> parent,
126             final StatementSupport<?, ?, ?> statementToAdd) {
127             final StatementDefinitionContext<?, ?, ?> stmtDefCtx = new StatementDefinitionContext<>(statementToAdd);
128
129             parent.createSubstatement(parent.declaredSubstatements().size(), stmtDefCtx,
130                     ImplicitSubstatement.of(parent.getStatementSourceReference()), null);
131         }
132     }
133
134     @Override
135     public QName getName() {
136         return argument();
137     }
138
139     @Nonnull
140     @Override
141     public Collection<? extends TypedefStatement> getTypedefs() {
142         return allDeclared(TypedefStatement.class);
143     }
144
145     @Nonnull
146     @Override
147     public Collection<? extends GroupingStatement> getGroupings() {
148         return allDeclared(GroupingStatement.class);
149     }
150
151     @Nullable
152     @Override
153     public StatusStatement getStatus() {
154         return firstDeclared(StatusStatement.class);
155     }
156
157     @Nullable
158     @Override
159     public DescriptionStatement getDescription() {
160         return firstDeclared(DescriptionStatement.class);
161     }
162
163     @Nullable
164     @Override
165     public ReferenceStatement getReference() {
166         return firstDeclared(ReferenceStatement.class);
167     }
168
169     @Override
170     public InputStatement getInput() {
171         return firstDeclared(InputStatement.class);
172     }
173
174     @Override
175     public OutputStatement getOutput() {
176         return firstDeclared(OutputStatement.class);
177     }
178
179     @Nonnull
180     @Override
181     public Collection<? extends IfFeatureStatement> getIfFeatures() {
182         return allDeclared(IfFeatureStatement.class);
183     }
184 }