Retain DeclarationReference in DeclaredStatements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / CaseStatementSupport.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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
28 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
32 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
33 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
34 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
35 import org.opendaylight.yangtools.yang.model.ri.stmt.ImplicitStatements;
36 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
37 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent.EffectiveConfig;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43
44 @Beta
45 public final class CaseStatementSupport
46         extends AbstractImplicitStatementSupport<CaseStatement, CaseEffectiveStatement> {
47     private static final SubstatementValidator RFC6020_VALIDATOR = SubstatementValidator.builder(YangStmtMapping.CASE)
48             .addAny(YangStmtMapping.ANYXML)
49             .addAny(YangStmtMapping.CHOICE)
50             .addAny(YangStmtMapping.CONTAINER)
51             .addOptional(YangStmtMapping.DESCRIPTION)
52             .addAny(YangStmtMapping.IF_FEATURE)
53             .addAny(YangStmtMapping.LEAF)
54             .addAny(YangStmtMapping.LEAF_LIST)
55             .addAny(YangStmtMapping.LIST)
56             .addOptional(YangStmtMapping.REFERENCE)
57             .addOptional(YangStmtMapping.STATUS)
58             .addAny(YangStmtMapping.USES)
59             .addOptional(YangStmtMapping.WHEN)
60             .build();
61     private static final SubstatementValidator RFC7950_VALIDATOR = SubstatementValidator.builder(YangStmtMapping.CASE)
62             .addAny(YangStmtMapping.ANYDATA)
63             .addAny(YangStmtMapping.ANYXML)
64             .addAny(YangStmtMapping.CHOICE)
65             .addAny(YangStmtMapping.CONTAINER)
66             .addOptional(YangStmtMapping.DESCRIPTION)
67             .addAny(YangStmtMapping.IF_FEATURE)
68             .addAny(YangStmtMapping.LEAF)
69             .addAny(YangStmtMapping.LEAF_LIST)
70             .addAny(YangStmtMapping.LIST)
71             .addOptional(YangStmtMapping.REFERENCE)
72             .addOptional(YangStmtMapping.STATUS)
73             .addAny(YangStmtMapping.USES)
74             .addOptional(YangStmtMapping.WHEN)
75             .build();
76
77     private final SubstatementValidator validator;
78
79     private CaseStatementSupport(final YangParserConfiguration config, final SubstatementValidator validator) {
80         super(YangStmtMapping.CASE, instantiatedPolicy(), config);
81         this.validator = requireNonNull(validator);
82     }
83
84     public static @NonNull CaseStatementSupport rfc6020Instance(final YangParserConfiguration config) {
85         return new CaseStatementSupport(config, RFC6020_VALIDATOR);
86     }
87
88     public static @NonNull CaseStatementSupport rfc7950Instance(final YangParserConfiguration config) {
89         return new CaseStatementSupport(config, RFC7950_VALIDATOR);
90     }
91
92     @Override
93     protected SubstatementValidator getSubstatementValidator() {
94         return validator;
95     }
96
97     @Override
98     protected CaseStatement createDeclared(final StmtContext<QName, CaseStatement, ?> ctx,
99             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
100         final StatementOrigin origin = ctx.origin();
101         switch (origin) {
102             case CONTEXT:
103                 return ImplicitStatements.createCase(ctx.getArgument(), substatements);
104             case DECLARATION:
105                 return DeclaredStatements.createCase(ctx.getArgument(), substatements);
106             default:
107                 throw new IllegalStateException("Unhandled statement origin " + origin);
108         }
109     }
110
111     @Override
112     protected CaseStatement attachDeclarationReference(final CaseStatement stmt, final DeclarationReference reference) {
113         return DeclaredStatementDecorators.decorateCase(stmt, reference);
114     }
115
116     @Override
117     protected CaseEffectiveStatement copyDeclaredEffective(final Current<QName, CaseStatement> stmt,
118             final CaseEffectiveStatement original) {
119         return EffectiveStatements.copyCase(original, stmt.effectivePath(),
120             computeFlags(stmt, original.effectiveSubstatements()), stmt.original(CaseSchemaNode.class));
121     }
122
123     @Override
124     protected CaseEffectiveStatement copyUndeclaredEffective(final Current<QName, CaseStatement> stmt,
125             final CaseEffectiveStatement original) {
126         return EffectiveStatements.copyCase(original, stmt.effectivePath(),
127             computeFlags(stmt, original.effectiveSubstatements()), stmt.original(CaseSchemaNode.class));
128     }
129
130     @Override
131     protected CaseEffectiveStatement createDeclaredEffective(final Current<QName, CaseStatement> stmt,
132             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
133         try {
134             return EffectiveStatements.createCase(stmt.declared(), stmt.effectivePath(),
135                 computeFlags(stmt, substatements), substatements, stmt.original(CaseSchemaNode.class));
136         } catch (SubstatementIndexingException e) {
137             throw new SourceException(e.getMessage(), stmt, e);
138         }
139     }
140
141     @Override
142     protected CaseEffectiveStatement createUndeclaredEffective(final Current<QName, CaseStatement> stmt,
143             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
144         try {
145             return EffectiveStatements.createCase(stmt.effectivePath(), computeFlags(stmt, substatements),
146                 substatements, stmt.original(CaseSchemaNode.class));
147         } catch (SubstatementIndexingException e) {
148             throw new SourceException(e.getMessage(), stmt, e);
149         }
150     }
151
152     private static int computeFlags(final Current<?, ?> stmt,
153             final Collection<? extends EffectiveStatement<?, ?>> substatements) {
154         final Boolean config;
155         final EffectiveConfig effective = stmt.effectiveConfig();
156         switch (effective) {
157             case FALSE:
158                 config = Boolean.FALSE;
159                 break;
160             case IGNORED:
161                 config = null;
162                 break;
163             case TRUE:
164                 final Boolean sub = substatementEffectiveConfig(substatements);
165                 config = sub != null ? sub : Boolean.TRUE;
166                 break;
167             case UNDETERMINED:
168                 config = substatementEffectiveConfig(substatements);
169                 break;
170             default:
171                 throw new IllegalStateException("Unhandled effective config " + effective);
172         }
173
174         return new FlagsBuilder()
175                 .setHistory(stmt.history())
176                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
177                 .setConfiguration(config)
178                 .toFlags();
179     }
180
181     @SuppressFBWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "Internal use tagged with @Nullable")
182     private static @Nullable Boolean substatementEffectiveConfig(
183             final Collection<? extends EffectiveStatement<?, ?>> substatements) {
184         for (EffectiveStatement<?, ?> stmt : substatements) {
185             if (stmt instanceof DataSchemaNode) {
186                 final Optional<Boolean> opt = ((DataSchemaNode) stmt).effectiveConfig();
187                 if (opt.isPresent()) {
188                     return opt.orElseThrow();
189                 }
190             }
191         }
192         return null;
193     }
194 }