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.meta;
10 import static com.google.common.base.Verify.verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ActionStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
23 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
24 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
25 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins;
26 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractSchemaTreeStatementSupport;
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 AbstractSchemaTreeStatementSupport<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, uninstantiatedPolicy());
60 public static ActionStatementSupport getInstance() {
65 public void onStatementAdded(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
66 final QName argument = stmt.getArgument();
67 SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), stmt,
68 "Action %s is defined within a notification, rpc or another action", argument);
69 SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), stmt,
70 "Action %s is defined within a case statement", argument);
71 SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.MODULE), stmt,
72 "Action %s is defined at the top level of a module", stmt.getArgument());
73 StmtContextUtils.validateNoKeylessListAncestorOf(stmt, "Action");
75 super.onStatementAdded(stmt);
79 public void onFullDefinitionDeclared(final Mutable<QName, ActionStatement, ActionEffectiveStatement> stmt) {
80 super.onFullDefinitionDeclared(stmt);
82 verify(stmt instanceof StatementContextBase);
83 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, InputStatement.class) == null) {
84 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
85 InputStatementSupport.rfc7950Instance(), null);
87 if (StmtContextUtils.findFirstDeclaredSubstatement(stmt, OutputStatement.class) == null) {
88 ((StatementContextBase<?, ?, ?>) stmt).appendImplicitSubstatement(
89 OutputStatementSupport.rfc7950Instance(), null);
94 protected SubstatementValidator getSubstatementValidator() {
95 return SUBSTATEMENT_VALIDATOR;
99 protected ActionStatement createDeclared(final StmtContext<QName, ActionStatement, ?> ctx,
100 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
101 return DeclaredStatements.createAction(ctx.getArgument(), substatements);
105 protected ActionEffectiveStatement createEffective(final Current<QName, ActionStatement> stmt,
106 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
107 final StatementSourceReference ref = stmt.sourceReference();
108 verify(!substatements.isEmpty(), "Missing implicit input/output statements at %s", ref);
111 return EffectiveStatements.createAction(stmt.declared(), stmt.effectivePath(),
112 EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements), substatements);
113 } catch (SubstatementIndexingException e) {
114 throw new SourceException(e.getMessage(), stmt, e);
119 public ActionEffectiveStatement copyEffective(final Current<QName, ActionStatement> stmt,
120 final ActionEffectiveStatement original) {
121 return EffectiveStatements.copyAction(original, stmt.effectivePath(),
122 EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), original.effectiveSubstatements()));