Make getOriginalCtx() give out an Optional
[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> implements
29         LeafListSchemaNode, DerivableSchemaNode {
30
31     private static final String ORDER_BY_USER_KEYWORD = "user";
32     private final TypeDefinition<?> type;
33     private final LeafListSchemaNode original;
34     private final boolean userOrdered;
35     private final Set<String> defaultValues;
36
37     public LeafListEffectiveStatementImpl(
38             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
39         super(ctx);
40         this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
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 ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.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         try {
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         } catch (final IllegalStateException e) {
76             throw new SourceException(ctx.getStatementSourceReference(), e,
77                     "Unable to find a default value for leaf-list '%s'", ctx.getStatementArgument());
78         }
79
80         type = builder.build();
81         userOrdered = isUserOrdered;
82     }
83
84     @Override
85     public Collection<String> getDefaults() {
86         return defaultValues;
87     }
88
89     @Override
90     public Optional<LeafListSchemaNode> getOriginal() {
91         return Optional.fromNullable(original);
92     }
93
94     @Override
95     public TypeDefinition<?> getType() {
96         return type;
97     }
98
99     @Override
100     public boolean isUserOrdered() {
101         return userOrdered;
102     }
103
104     @Override
105     public int hashCode() {
106         final int prime = 31;
107         int result = 1;
108         result = prime * result + Objects.hashCode(getQName());
109         result = prime * result + Objects.hashCode(getPath());
110         return result;
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
125         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
126     }
127
128     @Override
129     public String toString() {
130         return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" +
131                 getQName() +
132                 "]";
133     }
134 }