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.leaf_list;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
15 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.stmt.DefaultEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByStatement.Ordering;
26 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 abstract class AbstractLeafListStatementSupport
36 extends BaseSchemaTreeStatementSupport<LeafListStatement, LeafListEffectiveStatement> {
37 AbstractLeafListStatementSupport() {
38 super(YangStmtMapping.LEAF_LIST);
42 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43 return StmtContextUtils.parseIdentifier(ctx, value);
47 protected final LeafListStatement createDeclared(final StmtContext<QName, LeafListStatement, ?> ctx,
48 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
49 return new RegularLeafListStatement(ctx.coerceStatementArgument(), substatements);
53 protected final LeafListStatement createEmptyDeclared(final StmtContext<QName, LeafListStatement, ?> ctx) {
54 return new EmptyLeafListStatement(ctx.coerceStatementArgument());
58 protected final LeafListEffectiveStatement createEffective(
59 final StmtContext<QName, LeafListStatement, LeafListEffectiveStatement> ctx,
60 final LeafListStatement declared,
61 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
63 findFirstStatement(substatements, TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
64 "Leaf-list is missing a 'type' statement");
66 final SchemaPath path = ctx.getSchemaPath().get();
67 final LeafListSchemaNode original = (LeafListSchemaNode) ctx.getOriginalCtx()
68 .map(StmtContext::buildEffective).orElse(null);
70 final int flags = new FlagsBuilder()
71 .setHistory(ctx.getCopyHistory())
72 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
73 .setConfiguration(ctx.isConfiguration())
74 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
75 .equals(Ordering.USER))
77 final ImmutableSet<String> defaultValues = substatements.stream()
78 .filter(DefaultEffectiveStatement.class::isInstance)
79 .map(DefaultEffectiveStatement.class::cast)
80 .map(DefaultEffectiveStatement::argument)
81 .collect(ImmutableSet.toImmutableSet());
83 // FIXME: We need to interpret the default value in terms of supplied element type
84 SourceException.throwIf(
85 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
86 ctx.getStatementSourceReference(),
87 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
88 ctx.getStatementArgument(), defaultValues);
90 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
92 final Optional<ElementCountConstraint> elementCountConstraint =
93 EffectiveStmtUtils.createElementCountConstraint(substatements);
95 if (defaultValues.isEmpty()) {
96 return original == null && !elementCountConstraint.isPresent()
97 ? new EmptyLeafListEffectiveStatement(declared, path, flags, substatements)
98 : new SlimLeafListEffectiveStatement(declared, path, flags, substatements, original,
99 elementCountConstraint.orElse(null));
102 return new RegularLeafListEffectiveStatement(declared, path, flags, substatements, original, defaultValues,
103 elementCountConstraint.orElse(null));
107 protected final LeafListEffectiveStatement createEmptyEffective(
108 final StmtContext<QName, LeafListStatement, LeafListEffectiveStatement> ctx,
109 final LeafListStatement declared) {
110 throw new UnsupportedOperationException("Leaf statements must have at least one substatement");