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