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