Update yang-parser-api
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / InputStatementSupport.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.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.YangConstants;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
22 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.InputStatement;
24 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
25 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
26 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.ImplicitStatements;
28 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34
35 @Beta
36 public final class InputStatementSupport
37         extends AbstractOperationContainerStatementSupport<InputStatement, InputEffectiveStatement> {
38     private static final SubstatementValidator RFC6020_VALIDATOR = SubstatementValidator.builder(YangStmtMapping.INPUT)
39         .addAny(YangStmtMapping.ANYXML)
40         .addAny(YangStmtMapping.CHOICE)
41         .addAny(YangStmtMapping.CONTAINER)
42         .addAny(YangStmtMapping.GROUPING)
43         .addAny(YangStmtMapping.LEAF)
44         .addAny(YangStmtMapping.LEAF_LIST)
45         .addAny(YangStmtMapping.LIST)
46         .addAny(YangStmtMapping.TYPEDEF)
47         .addAny(YangStmtMapping.USES)
48         .build();
49     private static final SubstatementValidator RFC7950_VALIDATOR = SubstatementValidator.builder(YangStmtMapping.INPUT)
50         .addAny(YangStmtMapping.ANYDATA)
51         .addAny(YangStmtMapping.ANYXML)
52         .addAny(YangStmtMapping.CHOICE)
53         .addAny(YangStmtMapping.CONTAINER)
54         .addAny(YangStmtMapping.GROUPING)
55         .addAny(YangStmtMapping.LEAF)
56         .addAny(YangStmtMapping.LEAF_LIST)
57         .addAny(YangStmtMapping.LIST)
58         .addAny(YangStmtMapping.MUST)
59         .addAny(YangStmtMapping.TYPEDEF)
60         .addAny(YangStmtMapping.USES)
61         .build();
62
63     private final SubstatementValidator validator;
64
65     private InputStatementSupport(final YangParserConfiguration config, final SubstatementValidator validator) {
66         super(YangStmtMapping.INPUT, config, YangConstants::operationInputQName);
67         this.validator = requireNonNull(validator);
68     }
69
70     public static @NonNull InputStatementSupport rfc6020Instance(final YangParserConfiguration config) {
71         return new InputStatementSupport(config, RFC6020_VALIDATOR);
72     }
73
74     public static @NonNull InputStatementSupport rfc7950Instance(final YangParserConfiguration config) {
75         return new InputStatementSupport(config, RFC7950_VALIDATOR);
76     }
77
78     @Override
79     protected SubstatementValidator getSubstatementValidator() {
80         return validator;
81     }
82
83     @Override
84     protected InputStatement createDeclared(final StmtContext<QName, InputStatement, ?> ctx,
85             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
86         final StatementOrigin origin = ctx.origin();
87         switch (origin) {
88             case CONTEXT:
89                 return ImplicitStatements.createInput(ctx.getArgument(), substatements);
90             case DECLARATION:
91                 return DeclaredStatements.createInput(ctx.getArgument(), substatements);
92             default:
93                 throw new IllegalStateException("Unhandled statement origin " + origin);
94         }
95     }
96
97     @Override
98     protected InputStatement attachDeclarationReference(final InputStatement stmt,
99             final DeclarationReference reference) {
100         return DeclaredStatementDecorators.decorateInput(stmt, reference);
101     }
102
103     @Override
104     InputEffectiveStatement copyDeclaredEffective(final int flags, final Current<QName, InputStatement> stmt,
105             final InputEffectiveStatement original) {
106         return EffectiveStatements.copyInput(original, stmt.effectivePath(), flags);
107     }
108
109     @Override
110     InputEffectiveStatement copyUndeclaredEffective(final int flags, final Current<QName, InputStatement> stmt,
111             final InputEffectiveStatement original) {
112         return EffectiveStatements.copyInput(original, stmt.effectivePath(), flags);
113     }
114
115     @Override
116     InputEffectiveStatement createDeclaredEffective(final int flags, final Current<QName, InputStatement> stmt,
117             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
118         try {
119             return EffectiveStatements.createInput(stmt.declared(), stmt.effectivePath(), flags, substatements);
120         } catch (SubstatementIndexingException e) {
121             throw new SourceException(e.getMessage(), stmt, e);
122         }
123     }
124
125     @Override
126     InputEffectiveStatement createUndeclaredEffective(final int flags, final Current<QName, InputStatement> stmt,
127             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
128         try {
129             return EffectiveStatements.createInput(stmt.effectivePath(), flags, substatements);
130         } catch (SubstatementIndexingException e) {
131             throw new SourceException(e.getMessage(), stmt, e);
132         }
133     }
134 }