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 static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.input.InputStatementRFC7950Support;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.output.OutputStatementRFC7950Support;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
37 public final class ActionStatementSupport extends
38 BaseSchemaTreeStatementSupport<ActionStatement, ActionEffectiveStatement> {
40 private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
41 YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
43 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
44 YangStmtMapping.ACTION)
45 .addOptional(YangStmtMapping.DESCRIPTION)
46 .addAny(YangStmtMapping.GROUPING)
47 .addAny(YangStmtMapping.IF_FEATURE)
48 .addOptional(YangStmtMapping.INPUT)
49 .addOptional(YangStmtMapping.OUTPUT)
50 .addOptional(YangStmtMapping.REFERENCE)
51 .addOptional(YangStmtMapping.STATUS)
52 .addAny(YangStmtMapping.TYPEDEF)
54 private static final ActionStatementSupport INSTANCE = new ActionStatementSupport();
56 private ActionStatementSupport() {
57 super(YangStmtMapping.ACTION, CopyPolicy.DECLARED_COPY);
60 public static ActionStatementSupport getInstance() {
65 public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
66 super.onFullDefinitionDeclared(stmt);
68 verify(stmt instanceof StatementContextBase);
69 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
70 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
71 InputStatementRFC7950Support.getInstance(), null);
73 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
74 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
75 OutputStatementRFC7950Support.getInstance(), null);
80 protected SubstatementValidator getSubstatementValidator() {
81 return SUBSTATEMENT_VALIDATOR;
85 protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
86 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
87 return new RegularActionStatement(ctx.getArgument(), substatements);
91 protected ActionStatement createEmptyDeclared(final StmtContext<QName, ActionStatement, ?> ctx) {
92 return new EmptyActionStatement(ctx.getArgument());
96 protected ActionEffectiveStatement createEffective(final Current<QName, ActionStatement> stmt,
97 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
98 final StatementSourceReference ref = stmt.sourceReference();
99 checkState(!substatements.isEmpty(), "Missing implicit input/output statements at %s", ref);
100 final QName argument = stmt.getArgument();
101 SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), stmt,
102 "Action %s is defined within a notification, rpc or another action", argument);
103 SourceException.throwIf(
104 !StmtContextUtils.hasAncestorOfTypeWithChildOfType(stmt, YangStmtMapping.LIST, YangStmtMapping.KEY), stmt,
105 "Action %s is defined within a list that has no key statement", argument);
106 SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), stmt,
107 "Action %s is defined within a case statement", argument);
108 SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.MODULE), stmt,
109 "Action %s is defined at the top level of a module", argument);
112 return new ActionEffectiveStatementImpl(stmt.declared(), stmt.wrapSchemaPath(),
113 historyAndStatusFlags(stmt.history(), substatements), substatements);
114 } catch (SubstatementIndexingException e) {
115 throw new SourceException(e.getMessage(), stmt, e);