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