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