0e0ad7dbb24f87e5670900512d6eb68d731d1444
[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 QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
61         return StmtContextUtils.parseIdentifier(ctx, value);
62     }
63
64     @Override
65     public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
66         super.onFullDefinitionDeclared(stmt);
67
68         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
69             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
70                 InputStatementRFC7950Support.getInstance(), null);
71         }
72         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
73             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
74                 OutputStatementRFC7950Support.getInstance(), null);
75         }
76     }
77
78     @Override
79     protected SubstatementValidator getSubstatementValidator() {
80         return SUBSTATEMENT_VALIDATOR;
81     }
82
83     @Override
84     protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
85             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
86         return new RegularActionStatement(ctx.coerceStatementArgument(), substatements);
87     }
88
89     @Override
90     protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
91         return new EmptyActionStatement(ctx.coerceStatementArgument());
92     }
93
94     @Override
95     protected ActionEffectiveStatement createEffective(
96             final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared,
97             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
98         final QName argument = ctx.coerceStatementArgument();
99         final StatementSourceReference ref = ctx.getStatementSourceReference();
100         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS), ref,
101             "Action %s is defined within a notification, rpc or another action", argument);
102         SourceException.throwIf(
103             !StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
104             "Action %s is defined within a list that has no key statement", argument);
105         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE), ref,
106             "Action %s is defined within a case statement", argument);
107         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE), ref,
108             "Action %s is defined at the top level of a module", argument);
109
110         return new ActionEffectiveStatementImpl(declared, ctx.getSchemaPath().get(),
111             historyAndStatusFlags(ctx, substatements), ctx, substatements);
112     }
113
114     @Override
115     protected ActionEffectiveStatement createEmptyEffective(
116             final StmtContext<QName, ActionStatement, ActionEffectiveStatement> ctx, final ActionStatement declared) {
117         throw new IllegalStateException("Missing implicit input/output statements at "
118             + ctx.getStatementSourceReference());
119     }
120 }