Add BaseSchemaTreeStatementSupport.parseArgumentValue()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / leaf_list / AbstractLeafListStatementSupport.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.leaf_list;
9
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.source.SourceException;
33
34 abstract class AbstractLeafListStatementSupport
35         extends BaseSchemaTreeStatementSupport<LeafListStatement, LeafListEffectiveStatement> {
36     AbstractLeafListStatementSupport() {
37         super(YangStmtMapping.LEAF_LIST);
38     }
39
40     @Override
41     protected final LeafListStatement createDeclared(final StmtContext<QName, LeafListStatement, ?> ctx,
42             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
43         return new RegularLeafListStatement(ctx.coerceStatementArgument(), substatements);
44     }
45
46     @Override
47     protected final LeafListStatement createEmptyDeclared(final StmtContext<QName, LeafListStatement, ?> ctx) {
48         return new EmptyLeafListStatement(ctx.coerceStatementArgument());
49     }
50
51     @Override
52     protected final LeafListEffectiveStatement createEffective(
53             final StmtContext<QName, LeafListStatement, LeafListEffectiveStatement> ctx,
54             final LeafListStatement declared,
55             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
56         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
57             findFirstStatement(substatements, TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
58                 "Leaf-list is missing a 'type' statement");
59
60         final SchemaPath path = ctx.getSchemaPath().get();
61         final LeafListSchemaNode original = (LeafListSchemaNode) ctx.getOriginalCtx()
62                 .map(StmtContext::buildEffective).orElse(null);
63
64         final int flags = new FlagsBuilder()
65                 .setHistory(ctx.getCopyHistory())
66                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
67                 .setConfiguration(ctx.isConfiguration())
68                 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
69                     .equals(Ordering.USER))
70                 .toFlags();
71         final ImmutableSet<String> defaultValues = substatements.stream()
72                 .filter(DefaultEffectiveStatement.class::isInstance)
73                 .map(DefaultEffectiveStatement.class::cast)
74                 .map(DefaultEffectiveStatement::argument)
75                 .collect(ImmutableSet.toImmutableSet());
76
77         // FIXME: We need to interpret the default value in terms of supplied element type
78         SourceException.throwIf(
79             EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
80             ctx.getStatementSourceReference(),
81             "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
82             ctx.getStatementArgument(), defaultValues);
83
84         // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
85
86         final Optional<ElementCountConstraint> elementCountConstraint =
87                 EffectiveStmtUtils.createElementCountConstraint(substatements);
88
89         if (defaultValues.isEmpty()) {
90             return original == null && !elementCountConstraint.isPresent()
91                     ? new EmptyLeafListEffectiveStatement(declared, path, flags, substatements)
92                             : new SlimLeafListEffectiveStatement(declared, path, flags, substatements, original,
93                                 elementCountConstraint.orElse(null));
94         }
95
96         return new RegularLeafListEffectiveStatement(declared, path, flags, substatements, original, defaultValues,
97             elementCountConstraint.orElse(null));
98     }
99
100     @Override
101     protected final LeafListEffectiveStatement createEmptyEffective(
102             final StmtContext<QName, LeafListStatement, LeafListEffectiveStatement> ctx,
103             final LeafListStatement declared) {
104         throw new UnsupportedOperationException("Leaf statements must have at least one substatement");
105     }
106 }