Remove AbstractStatementSupport.createEmptyDeclared()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / ContainerStatementSupport.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 java.util.Collection;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
28 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
29 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractSchemaTreeStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36
37 @Beta
38 public final class ContainerStatementSupport
39         extends AbstractSchemaTreeStatementSupport<ContainerStatement, ContainerEffectiveStatement> {
40     private static final @NonNull ContainerStatementSupport RFC6020_INSTANCE = new ContainerStatementSupport(
41         SubstatementValidator.builder(YangStmtMapping.CONTAINER)
42             .addAny(YangStmtMapping.ANYXML)
43             .addAny(YangStmtMapping.CHOICE)
44             .addOptional(YangStmtMapping.CONFIG)
45             .addAny(YangStmtMapping.CONTAINER)
46             .addOptional(YangStmtMapping.DESCRIPTION)
47             .addAny(YangStmtMapping.GROUPING)
48             .addAny(YangStmtMapping.IF_FEATURE)
49             .addAny(YangStmtMapping.LEAF)
50             .addAny(YangStmtMapping.LEAF_LIST)
51             .addAny(YangStmtMapping.LIST)
52             .addAny(YangStmtMapping.MUST)
53             .addOptional(YangStmtMapping.PRESENCE)
54             .addOptional(YangStmtMapping.REFERENCE)
55             .addOptional(YangStmtMapping.STATUS)
56             .addAny(YangStmtMapping.TYPEDEF)
57             .addAny(YangStmtMapping.USES)
58             .addOptional(YangStmtMapping.WHEN)
59             .build());
60
61     private static final @NonNull ContainerStatementSupport RFC7950_INSTANCE = new ContainerStatementSupport(
62         SubstatementValidator.builder(YangStmtMapping.CONTAINER)
63             .addAny(YangStmtMapping.ACTION)
64             .addAny(YangStmtMapping.ANYDATA)
65             .addAny(YangStmtMapping.ANYXML)
66             .addAny(YangStmtMapping.CHOICE)
67             .addOptional(YangStmtMapping.CONFIG)
68             .addAny(YangStmtMapping.CONTAINER)
69             .addOptional(YangStmtMapping.DESCRIPTION)
70             .addAny(YangStmtMapping.GROUPING)
71             .addAny(YangStmtMapping.IF_FEATURE)
72             .addAny(YangStmtMapping.LEAF)
73             .addAny(YangStmtMapping.LEAF_LIST)
74             .addAny(YangStmtMapping.LIST)
75             .addAny(YangStmtMapping.MUST)
76             .addAny(YangStmtMapping.NOTIFICATION)
77             .addOptional(YangStmtMapping.PRESENCE)
78             .addOptional(YangStmtMapping.REFERENCE)
79             .addOptional(YangStmtMapping.STATUS)
80             .addAny(YangStmtMapping.TYPEDEF)
81             .addAny(YangStmtMapping.USES)
82             .addOptional(YangStmtMapping.WHEN)
83             .build());
84
85     private final SubstatementValidator validator;
86
87     private ContainerStatementSupport(final SubstatementValidator validator) {
88         super(YangStmtMapping.CONTAINER, instantiatedPolicy());
89         this.validator = requireNonNull(validator);
90     }
91
92     public static @NonNull ContainerStatementSupport rfc6020Instance() {
93         return RFC6020_INSTANCE;
94     }
95
96     public static @NonNull ContainerStatementSupport rfc7950Instance() {
97         return RFC7950_INSTANCE;
98     }
99
100     @Override
101     protected SubstatementValidator getSubstatementValidator() {
102         return validator;
103     }
104
105     @Override
106     protected ContainerStatement createDeclared(final StmtContext<QName, ContainerStatement, ?> ctx,
107             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
108         return DeclaredStatements.createContainer(ctx.getArgument(), substatements);
109     }
110
111     @Override
112     protected ContainerEffectiveStatement createEffective(final Current<QName, ContainerStatement> stmt,
113             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
114         EffectiveStmtUtils.checkUniqueGroupings(stmt, substatements);
115         EffectiveStmtUtils.checkUniqueTypedefs(stmt, substatements);
116         EffectiveStmtUtils.checkUniqueUses(stmt, substatements);
117
118         try {
119             return EffectiveStatements.createContainer(stmt.declared(), stmt.effectivePath(),
120                 createFlags(stmt, substatements), substatements, stmt.original(ContainerSchemaNode.class));
121         } catch (SubstatementIndexingException e) {
122             throw new SourceException(e.getMessage(), stmt, e);
123         }
124     }
125
126     @Override
127     public ContainerEffectiveStatement copyEffective(final Current<QName, ContainerStatement> stmt,
128             final ContainerEffectiveStatement original) {
129         return EffectiveStatements.copyContainer(original, stmt.effectivePath(),
130             createFlags(stmt, original.effectiveSubstatements()), stmt.original(ContainerSchemaNode.class));
131     }
132
133     private static int createFlags(final Current<?, ?> stmt,
134             final Collection<? extends EffectiveStatement<?, ?>> substatements) {
135         return new FlagsBuilder()
136             .setHistory(stmt.history())
137             .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
138             .setConfiguration(stmt.effectiveConfig().asNullable())
139             .setPresence(findFirstStatement(substatements, PresenceEffectiveStatement.class) != null)
140             .toFlags();
141     }
142 }
143