2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.action;
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;
32 public final class ActionStatementSupport extends
33 BaseSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
35 private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
36 YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
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)
49 private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
51 private ActionStatementSupport() {
52 super(YangStmtMapping.ACTION);
55 public static ActionStatementSupport getInstance() {
60 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
61 return StmtContextUtils.parseIdentifier(ctx, value);
65 public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
66 super.onFullDefinitionDeclared(stmt);
68 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
69 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
70 InputStatementRFC7950Support.getInstance(), null);
72 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
73 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
74 OutputStatementRFC7950Support.getInstance(), null);
79 protected SubstatementValidator getSubstatementValidator() {
80 return SUBSTATEMENT_VALIDATOR;
84 protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
85 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
86 return new RegularActionStatement(ctx.coerceStatementArgument(), substatements);
90 protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
91 return new EmptyActionStatement(ctx.coerceStatementArgument());
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);
110 return new ActionEffectiveStatementImpl(declared, ctx.getSchemaPath().get(),
111 historyAndStatusFlags(ctx, substatements), ctx, substatements);
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());