Merge branch 'master' of ../controller
[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.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 final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
39         implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
40
41     private static final String ORDER_BY_USER_KEYWORD = "user";
42
43     private final TypeDefinition<?> type;
44     private final LeafListSchemaNode original;
45     private final boolean userOrdered;
46     private final @NonNull ImmutableSet<String> defaultValues;
47     private final ImmutableSet<MustDefinition> mustConstraints;
48     private final ElementCountConstraint elementCountConstraint;
49
50     LeafListEffectiveStatementImpl(
51             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
52         super(ctx);
53         this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
54
55         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
56             firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
57             "Leaf-list is missing a 'type' statement");
58
59         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
60             ctx.getSchemaPath().get());
61         final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
62         boolean isUserOrdered = false;
63         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
64             if (stmt instanceof OrderedByEffectiveStatement) {
65                 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
66             }
67
68             if (stmt instanceof DefaultEffectiveStatement) {
69                 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
70             } else if (stmt instanceof DescriptionEffectiveStatement) {
71                 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
72             } else if (stmt instanceof ReferenceEffectiveStatement) {
73                 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
74             } else if (stmt instanceof StatusEffectiveStatement) {
75                 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
76             } else if (stmt instanceof UnitsEffectiveStatement) {
77                 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
78             }
79         }
80
81         // FIXME: We need to interpret the default value in terms of supplied element type
82         defaultValues = defaultValuesBuilder.build();
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         type = builder.build();
92         userOrdered = isUserOrdered;
93         elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
94         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
95     }
96
97     @Override
98     public Collection<String> getDefaults() {
99         return defaultValues;
100     }
101
102     @Override
103     public Optional<LeafListSchemaNode> getOriginal() {
104         return Optional.ofNullable(original);
105     }
106
107     @Override
108     public TypeDefinition<?> getType() {
109         return type;
110     }
111
112     @Override
113     public boolean isUserOrdered() {
114         return userOrdered;
115     }
116
117     @Override
118     public Optional<ElementCountConstraint> getElementCountConstraint() {
119         return Optional.ofNullable(elementCountConstraint);
120     }
121
122     @Override
123     public Collection<MustDefinition> getMustConstraints() {
124         return mustConstraints;
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 }