Bug 6880: [Yang 1.1] Allow leaf-lists to have default values
[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.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Objects;
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.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
23 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26
27 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement> implements
28         LeafListSchemaNode, DerivableSchemaNode {
29
30     private static final String ORDER_BY_USER_KEYWORD = "user";
31     private final TypeDefinition<?> type;
32     private final LeafListSchemaNode original;
33     private final boolean userOrdered;
34     private final List<String> defaultValues;
35
36     public LeafListEffectiveStatementImpl(
37             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
38         super(ctx);
39         this.original = ctx.getOriginalCtx() == null ? null : (LeafListSchemaNode) ctx.getOriginalCtx()
40                 .buildEffective();
41
42         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
43             firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
44             "Leaf-list is missing a 'type' statement");
45
46         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
47             ctx.getSchemaPath().get());
48         final ImmutableList.Builder<String> defaultValuesBuilder = ImmutableList.builder();
49         boolean isUserOrdered = false;
50         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
51             if (stmt instanceof OrderedByEffectiveStatementImpl) {
52                 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
53             }
54
55             if (stmt instanceof DefaultEffectiveStatementImpl) {
56                 defaultValuesBuilder.add(((DefaultEffectiveStatementImpl) stmt).argument());
57             } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
58                 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
59             } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
60                 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
61             } else if (stmt instanceof StatusEffectiveStatementImpl) {
62                 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
63             } else if (stmt instanceof UnitsEffectiveStatementImpl) {
64                 builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument());
65             }
66         }
67
68         defaultValues = defaultValuesBuilder.build();
69         type = builder.build();
70         userOrdered = isUserOrdered;
71     }
72
73     @Override
74     public Collection<String> getDefaults() {
75         return defaultValues;
76     }
77
78     @Override
79     public Optional<LeafListSchemaNode> getOriginal() {
80         return Optional.fromNullable(original);
81     }
82
83     @Override
84     public TypeDefinition<?> getType() {
85         return type;
86     }
87
88     @Override
89     public boolean isUserOrdered() {
90         return userOrdered;
91     }
92
93     @Override
94     public int hashCode() {
95         final int prime = 31;
96         int result = 1;
97         result = prime * result + Objects.hashCode(getQName());
98         result = prime * result + Objects.hashCode(getPath());
99         return result;
100     }
101
102     @Override
103     public boolean equals(final Object obj) {
104         if (this == obj) {
105             return true;
106         }
107         if (obj == null) {
108             return false;
109         }
110         if (getClass() != obj.getClass()) {
111             return false;
112         }
113         final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
114         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
115     }
116
117     @Override
118     public String toString() {
119         return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" +
120                 getQName() +
121                 "]";
122     }
123 }