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.case_;
10 import com.google.common.collect.ImmutableList;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.StatementSource;
22 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseImplicitStatementSupport;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent.EffectiveConfig;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 abstract class AbstractCaseStatementSupport
34 extends BaseImplicitStatementSupport<CaseStatement, CaseEffectiveStatement> {
35 AbstractCaseStatementSupport() {
36 super(YangStmtMapping.CASE);
40 protected final CaseStatement createDeclared(final StmtContext<QName, CaseStatement, ?> ctx,
41 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
42 final StatementSource source = ctx.source();
45 return new RegularUndeclaredCaseStatement(ctx.getArgument(), substatements);
47 return new RegularCaseStatement(ctx.getArgument(), substatements);
49 throw new IllegalStateException("Unhandled statement source " + source);
54 protected final CaseStatement createEmptyDeclared(final StmtContext<QName, CaseStatement, ?> ctx) {
55 final StatementSource source = ctx.source();
58 return new EmptyUndeclaredCaseStatement(ctx.getArgument());
60 return new EmptyCaseStatement(ctx.getArgument());
62 throw new IllegalStateException("Unhandled statement source " + source);
67 protected final CaseEffectiveStatement createDeclaredEffective(final Current<QName, CaseStatement> stmt,
68 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
70 return new DeclaredCaseEffectiveStatement(stmt.declared(), substatements, computeFlags(stmt, substatements),
71 stmt.wrapSchemaPath(), findOriginal(stmt));
72 } catch (SubstatementIndexingException e) {
73 throw new SourceException(e.getMessage(), stmt, e);
78 protected final CaseEffectiveStatement createUndeclaredEffective(final Current<QName, CaseStatement> stmt,
79 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
81 return new UndeclaredCaseEffectiveStatement(substatements, computeFlags(stmt, substatements),
82 stmt.wrapSchemaPath(), findOriginal(stmt));
83 } catch (SubstatementIndexingException e) {
84 throw new SourceException(e.getMessage(), stmt, e);
88 private static @Nullable CaseSchemaNode findOriginal(final Current<?, ?> stmt) {
89 return (CaseSchemaNode) stmt.original();
92 private static int computeFlags(final Current<?, ?> stmt,
93 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
95 final EffectiveConfig effective = stmt.effectiveConfig();
98 config = Boolean.FALSE;
104 final Boolean sub = substatementEffectiveConfig(substatements);
105 config = sub != null ? sub : Boolean.TRUE;
108 config = substatementEffectiveConfig(substatements);
111 throw new IllegalStateException("Unhandled effective config " + effective);
114 return new FlagsBuilder()
115 .setHistory(stmt.history())
116 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
117 .setConfiguration(config)
121 @SuppressFBWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "Internal use tagged with @Nullable")
122 private static @Nullable Boolean substatementEffectiveConfig(
123 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
124 for (EffectiveStatement<?, ?> stmt : substatements) {
125 if (stmt instanceof DataSchemaNode) {
126 final Optional<Boolean> opt = ((DataSchemaNode) stmt).effectiveConfig();
127 if (opt.isPresent()) {
128 return opt.orElseThrow();