2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
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;
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)
55 protected ActionStatementImpl(final StmtContext<QName,ActionStatement, ?> context) {
59 public static class Definition
60 extends AbstractStatementSupport<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> {
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();
68 super(YangStmtMapping.ACTION);
72 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
73 return StmtContextUtils.qnameFromArgument(ctx, value);
77 public void onStatementAdded(
78 final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
79 stmt.getParentContext().addToNs(ChildSchemaNodes.class, stmt.getStatementArgument(), stmt);
83 public ActionStatement createDeclared(
84 final StmtContext<QName, ActionStatement, ?> ctx) {
85 return new ActionStatementImpl(ctx);
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);
107 public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement,
108 EffectiveStatement<QName, ActionStatement>> stmt) {
109 super.onFullDefinitionDeclared(stmt);
111 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
112 addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_INPUT);
115 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
116 addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_OUTPUT);
121 protected SubstatementValidator getSubstatementValidator() {
122 return SUBSTATEMENT_VALIDATOR;
125 private static void addImplicitStatement(final StatementContextBase<?, ?, ?> parent,
126 final StatementSupport<?, ?, ?> statementToAdd) {
127 final StatementDefinitionContext<?, ?, ?> stmtDefCtx = new StatementDefinitionContext<>(statementToAdd);
129 parent.createSubstatement(parent.declaredSubstatements().size(), stmtDefCtx,
130 ImplicitSubstatement.of(parent.getStatementSourceReference()), null);
135 public QName getName() {
141 public Collection<? extends TypedefStatement> getTypedefs() {
142 return allDeclared(TypedefStatement.class);
147 public Collection<? extends GroupingStatement> getGroupings() {
148 return allDeclared(GroupingStatement.class);
153 public StatusStatement getStatus() {
154 return firstDeclared(StatusStatement.class);
159 public DescriptionStatement getDescription() {
160 return firstDeclared(DescriptionStatement.class);
165 public ReferenceStatement getReference() {
166 return firstDeclared(ReferenceStatement.class);
170 public InputStatement getInput() {
171 return firstDeclared(InputStatement.class);
175 public OutputStatement getOutput() {
176 return firstDeclared(OutputStatement.class);
181 public Collection<? extends IfFeatureStatement> getIfFeatures() {
182 return allDeclared(IfFeatureStatement.class);