Fix StmtContext nullness problems
[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.ImmutableSet;
11 import java.util.Set;
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.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.input.InputStatementRFC7950Support;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.output.OutputStatementRFC7950Support;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
28
29 public final class ActionStatementSupport
30         extends AbstractQNameStatementSupport<ActionStatement, EffectiveStatement<QName, ActionStatement>> {
31     private static final Set<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(YangStmtMapping.NOTIFICATION,
32             YangStmtMapping.RPC, YangStmtMapping.ACTION);
33
34     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
35         YangStmtMapping.ACTION)
36         .addOptional(YangStmtMapping.DESCRIPTION)
37         .addAny(YangStmtMapping.GROUPING)
38         .addAny(YangStmtMapping.IF_FEATURE)
39         .addOptional(YangStmtMapping.INPUT)
40         .addOptional(YangStmtMapping.OUTPUT)
41         .addOptional(YangStmtMapping.REFERENCE)
42         .addOptional(YangStmtMapping.STATUS)
43         .addAny(YangStmtMapping.TYPEDEF)
44         .build();
45     private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
46
47     private ActionStatementSupport() {
48         super(YangStmtMapping.ACTION);
49     }
50
51     public static ActionStatementSupport getInstance() {
52         return INSTANCE;
53     }
54
55     @Override
56     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
57         return StmtContextUtils.parseIdentifier(ctx, value);
58     }
59
60     @Override
61     public void onStatementAdded(
62             final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
63         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
64     }
65
66     @Override
67     public ActionStatement createDeclared(
68             final StmtContext<QName, ActionStatement, ?> ctx) {
69         return new ActionStatementImpl(ctx);
70     }
71
72     @Override
73     public EffectiveStatement<QName, ActionStatement> createEffective(
74             final StmtContext<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> ctx) {
75         SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS),
76                 ctx.getStatementSourceReference(),
77                 "Action %s is defined within a notification, rpc or another action", ctx.getStatementArgument());
78         SourceException.throwIf(!StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST,
79                 YangStmtMapping.KEY), ctx.getStatementSourceReference(),
80                 "Action %s is defined within a list that has no key statement", ctx.getStatementArgument());
81         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE),
82                 ctx.getStatementSourceReference(), "Action %s is defined within a case statement",
83                 ctx.getStatementArgument());
84         SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.MODULE),
85                 ctx.getStatementSourceReference(), "Action %s is defined at the top level of a module",
86                 ctx.getStatementArgument());
87         return new ActionEffectiveStatementImpl(ctx);
88     }
89
90     @Override
91     public void onFullDefinitionDeclared(final StmtContext.Mutable<QName, ActionStatement,
92             EffectiveStatement<QName, ActionStatement>> stmt) {
93         super.onFullDefinitionDeclared(stmt);
94
95         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
96             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(InputStatementRFC7950Support.getInstance());
97         }
98
99         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
100             ((StatementContextBase<?, ?, ?>) stmt).appendImplicitStatement(OutputStatementRFC7950Support.getInstance());
101         }
102     }
103
104     @Override
105     protected SubstatementValidator getSubstatementValidator() {
106         return SUBSTATEMENT_VALIDATOR;
107     }
108 }