Add BaseSchemaTreeStatementSupport.parseArgumentValue()
[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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.input.InputStatementRFC7950Support;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.output.OutputStatementRFC7950Support;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31
32 public final class ActionStatementSupport extends
33         BaseSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
34
35     private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
36             YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
37
38     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
39         YangStmtMapping.ACTION)
40         .addOptional(YangStmtMapping.DESCRIPTION)
41         .addAny(YangStmtMapping.GROUPING)
42         .addAny(YangStmtMapping.IF_FEATURE)
43         .addOptional(YangStmtMapping.INPUT)
44         .addOptional(YangStmtMapping.OUTPUT)
45         .addOptional(YangStmtMapping.REFERENCE)
46         .addOptional(YangStmtMapping.STATUS)
47         .addAny(YangStmtMapping.TYPEDEF)
48         .build();
49     private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
50
51     private ActionStatementSupport() {
52         super(YangStmtMapping.ACTION);
53     }
54
55     public static ActionStatementSupport getInstance() {
56         return INSTANCE;
57     }
58
59     @Override
60     public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
61         super.onFullDefinitionDeclared(stmt);
62
63         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
64             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
65                 InputStatementRFC7950Support.getInstance(), null);
66         }
67         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
68             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
69                 OutputStatementRFC7950Support.getInstance(), null);
70         }
71     }
72
73     @Override
74     protected SubstatementValidator getSubstatementValidator() {
75         return SUBSTATEMENT_VALIDATOR;
76     }
77
78     @Override
79     protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
80             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
81         return new RegularActionStatement(ctx.coerceStatementArgument(), substatements);
82     }
83
84     @Override
85     protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
86         return new EmptyActionStatement(ctx.coerceStatementArgument());
87     }
88
89     @Override
90     protected ActionEffectiveStatement createEffective(
91             final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared,
92             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
93         final QName argument = ctx.coerceStatementArgument();
94         final StatementSourceReference ref = ctx.getStatementSourceReference();
95         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS), ref,
96             "Action %s is defined within a notification, rpc or another action", argument);
97         SourceException.throwIf(
98             !StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
99             "Action %s is defined within a list that has no key statement", argument);
100         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE), ref,
101             "Action %s is defined within a case statement", argument);
102         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE), ref,
103             "Action %s is defined at the top level of a module", argument);
104
105         return new ActionEffectiveStatementImpl(declared, ctx.getSchemaPath().get(),
106             historyAndStatusFlags(ctx, substatements), ctx, substatements);
107     }
108
109     @Override
110     protected ActionEffectiveStatement createEmptyEffective(
111             final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared) {
112         throw new IllegalStateException("Missing implicit input/output statements at "
113             + ctx.getStatementSourceReference());
114     }
115 }