Peel MustConstraintAware from ConstraintDefinition
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafListEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020.effective;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.Collection;
12 import java.util.Objects;
13 import java.util.Optional;
14 import java.util.Set;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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.DescriptionEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
31 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
35
36 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
37         implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
38
39     private static final String ORDER_BY_USER_KEYWORD = "user";
40
41     private final TypeDefinition<?> type;
42     private final LeafListSchemaNode original;
43     private final boolean userOrdered;
44     private final Set<String> defaultValues;
45     private final Collection<MustDefinition> mustConstraints;
46
47     public LeafListEffectiveStatementImpl(
48             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
49         super(ctx);
50         this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
51
52         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
53             firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
54             "Leaf-list is missing a 'type' statement");
55
56         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
57             ctx.getSchemaPath().get());
58         final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
59         boolean isUserOrdered = false;
60         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
61             if (stmt instanceof OrderedByEffectiveStatement) {
62                 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
63             }
64
65             if (stmt instanceof DefaultEffectiveStatement) {
66                 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
67             } else if (stmt instanceof DescriptionEffectiveStatement) {
68                 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
69             } else if (stmt instanceof ReferenceEffectiveStatement) {
70                 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
71             } else if (stmt instanceof StatusEffectiveStatement) {
72                 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
73             } else if (stmt instanceof UnitsEffectiveStatement) {
74                 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
75             }
76         }
77
78         // FIXME: We need to interpret the default value in terms of supplied element type
79         defaultValues = defaultValuesBuilder.build();
80         SourceException.throwIf(
81                 TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
82                 ctx.getStatementSourceReference(),
83                 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
84                 ctx.getStatementArgument(), defaultValues);
85
86         // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
87
88         type = builder.build();
89         userOrdered = isUserOrdered;
90         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
91     }
92
93     @Override
94     public Collection<String> getDefaults() {
95         return defaultValues;
96     }
97
98     @Override
99     public Optional<LeafListSchemaNode> getOriginal() {
100         return Optional.ofNullable(original);
101     }
102
103     @Override
104     public TypeDefinition<?> getType() {
105         return type;
106     }
107
108     @Override
109     public boolean isUserOrdered() {
110         return userOrdered;
111     }
112
113     @Override
114     public Collection<MustDefinition> getMustConstraints() {
115         return mustConstraints;
116     }
117
118     @Override
119     public int hashCode() {
120         final int prime = 31;
121         int result = 1;
122         result = prime * result + Objects.hashCode(getQName());
123         result = prime * result + Objects.hashCode(getPath());
124         return result;
125     }
126
127     @Override
128     public boolean equals(final Object obj) {
129         if (this == obj) {
130             return true;
131         }
132         if (obj == null) {
133             return false;
134         }
135         if (getClass() != obj.getClass()) {
136             return false;
137         }
138         final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
139         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
140     }
141
142     @Override
143     public String toString() {
144         return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";
145     }
146 }