ea00dafb630515b6a0ec8ef44ba1f3a3fc8b8d69
[yangtools.git] / yang / yang-parser-impl / 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.StatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
27 import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementDefinitionContext;
31
32 public final class ActionStatementSupport
33         extends AbstractQNameStatementSupport<ActionStatement, EffectiveStatement<QName, ActionStatement>> {
34     private static final Set<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(YangStmtMapping.NOTIFICATION,
35             YangStmtMapping.RPC, YangStmtMapping.ACTION);
36     private static final StatementSupport<?, ?, ?> IMPLICIT_INPUT = new InputStatementRFC7950Support();
37     private static final StatementSupport<?, ?, ?> IMPLICIT_OUTPUT = new OutputStatementRFC7950Support();
38
39     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
40         YangStmtMapping.ACTION)
41         .addOptional(YangStmtMapping.DESCRIPTION)
42         .addAny(YangStmtMapping.GROUPING)
43         .addAny(YangStmtMapping.IF_FEATURE)
44         .addOptional(YangStmtMapping.INPUT)
45         .addOptional(YangStmtMapping.OUTPUT)
46         .addOptional(YangStmtMapping.REFERENCE)
47         .addOptional(YangStmtMapping.STATUS)
48         .addAny(YangStmtMapping.TYPEDEF)
49         .build();
50
51     public ActionStatementSupport() {
52         super(YangStmtMapping.ACTION);
53     }
54
55     @Override
56     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
57         return StmtContextUtils.qnameFromArgument(ctx, value);
58     }
59
60     @Override
61     public void onStatementAdded(
62             final StmtContext.Mutable<QName, ActionStatement, EffectiveStatement<QName, ActionStatement>> stmt) {
63         stmt.getParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.getStatementArgument(), 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             addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_INPUT);
97         }
98
99         if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
100             addImplicitStatement((StatementContextBase<?, ?, ?>) stmt, IMPLICIT_OUTPUT);
101         }
102     }
103
104     @Override
105     protected SubstatementValidator getSubstatementValidator() {
106         return SUBSTATEMENT_VALIDATOR;
107     }
108
109     private static void addImplicitStatement(final StatementContextBase<?, ?, ?> parent,
110         final StatementSupport<?, ?, ?> statementToAdd) {
111         final StatementDefinitionContext<?, ?, ?> stmtDefCtx = new StatementDefinitionContext<>(statementToAdd);
112
113         parent.createSubstatement(parent.declaredSubstatements().size(), stmtDefCtx,
114                 ImplicitSubstatement.of(parent.getStatementSourceReference()), null);
115     }
116 }