Centralize must contraint definitions
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / leaf_list / 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.rfc7950.stmt.leaf_list;
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 org.eclipse.jdt.annotation.NonNull;
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.ElementCountConstraint;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
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.rfc7950.stmt.AbstractEffectiveMustConstraintAwareDataSchemaNode;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
36
37 /**
38  * Internal implementation of LeafListEffectiveStatement.
39  *
40  * @deprecated This class is visible only for historical purposes and is going to be hidden.
41  */
42 // FIXME: 4.0.0: hide this class
43 @Deprecated
44 public final class LeafListEffectiveStatementImpl
45         extends AbstractEffectiveMustConstraintAwareDataSchemaNode<LeafListStatement>
46         implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
47
48     private static final String ORDER_BY_USER_KEYWORD = "user";
49
50     private final TypeDefinition<?> type;
51     private final LeafListSchemaNode original;
52     private final boolean userOrdered;
53     private final @NonNull ImmutableSet<String> defaultValues;
54     private final ElementCountConstraint elementCountConstraint;
55
56     LeafListEffectiveStatementImpl(
57             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
58         super(ctx);
59         this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
60
61         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
62             firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
63             "Leaf-list is missing a 'type' statement");
64
65         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
66             ctx.getSchemaPath().get());
67         final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
68         boolean isUserOrdered = false;
69         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
70             if (stmt instanceof OrderedByEffectiveStatement) {
71                 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
72             }
73
74             if (stmt instanceof DefaultEffectiveStatement) {
75                 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
76             } else if (stmt instanceof DescriptionEffectiveStatement) {
77                 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
78             } else if (stmt instanceof ReferenceEffectiveStatement) {
79                 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
80             } else if (stmt instanceof StatusEffectiveStatement) {
81                 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
82             } else if (stmt instanceof UnitsEffectiveStatement) {
83                 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
84             }
85         }
86
87         // FIXME: We need to interpret the default value in terms of supplied element type
88         defaultValues = defaultValuesBuilder.build();
89         SourceException.throwIf(
90             EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
91             ctx.getStatementSourceReference(),
92             "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
93             ctx.getStatementArgument(), defaultValues);
94
95         // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
96
97         type = builder.build();
98         userOrdered = isUserOrdered;
99         elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
100     }
101
102     @Override
103     public Collection<String> getDefaults() {
104         return defaultValues;
105     }
106
107     @Override
108     public Optional<LeafListSchemaNode> getOriginal() {
109         return Optional.ofNullable(original);
110     }
111
112     @Override
113     public TypeDefinition<?> getType() {
114         return type;
115     }
116
117     @Override
118     public boolean isUserOrdered() {
119         return userOrdered;
120     }
121
122     @Override
123     public Optional<ElementCountConstraint> getElementCountConstraint() {
124         return Optional.ofNullable(elementCountConstraint);
125     }
126
127     @Override
128     public int hashCode() {
129         final int prime = 31;
130         int result = 1;
131         result = prime * result + Objects.hashCode(getQName());
132         result = prime * result + Objects.hashCode(getPath());
133         return result;
134     }
135
136     @Override
137     public boolean equals(final Object obj) {
138         if (this == obj) {
139             return true;
140         }
141         if (obj == null) {
142             return false;
143         }
144         if (getClass() != obj.getClass()) {
145             return false;
146         }
147         final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
148         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
149     }
150
151     @Override
152     public String toString() {
153         return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";
154     }
155 }