92cb8de401392849c5c4c9f4b6139008efbab837
[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.LinkedList;
14 import java.util.List;
15 import java.util.Objects;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
18 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
29
30 public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
31         LeafListSchemaNode, DerivableSchemaNode {
32     private final QName qname;
33     private final SchemaPath path;
34
35     // FIXME: should be private
36     boolean augmenting;
37     private boolean addedByUses;
38     private LeafListSchemaNode original;
39     private boolean configuration = true;
40     private final ConstraintDefinition constraintsDef;
41     private TypeDefinition<?> type;
42     private boolean userOrdered;
43
44     private ImmutableList<UnknownSchemaNode> unknownNodes;
45
46     public LeafListEffectiveStatementImpl(
47             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
48         super(ctx);
49         this.qname = ctx.getStatementArgument();
50         this.path = Utils.getSchemaPath(ctx);
51         this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
52
53         // :TODO init TypeDefinition
54
55         initSubstatementCollections();
56         initCopyType(ctx);
57     }
58
59     private void initCopyType(
60             final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
61
62         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
63
64         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
65             augmenting = true;
66         }
67         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
68             addedByUses = true;
69         }
70         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
71             addedByUses = augmenting = true;
72         }
73
74         if (ctx.getOriginalCtx() != null) {
75             original = (LeafListSchemaNode) ctx.getOriginalCtx().buildEffective();
76         }
77     }
78     private void initSubstatementCollections() {
79         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
80
81         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
82
83         boolean configurationInit = false;
84         boolean userOrderedInit = false;
85         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
86             if (effectiveStatement instanceof UnknownSchemaNode) {
87                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
88             }
89             if (effectiveStatement instanceof TypeDefinition) {
90                 type = TypeUtils.getTypeFromEffectiveStatement(effectiveStatement);
91             }
92             if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
93                 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
94                 this.configuration = configStmt.argument();
95                 configurationInit = true;
96             }
97             if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
98                 OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
99                 this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
100                 userOrderedInit = true;
101             }
102         }
103
104         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
105     }
106
107     @Override
108     public QName getQName() {
109         return qname;
110     }
111
112     @Override
113     public SchemaPath getPath() {
114         return path;
115     }
116
117     @Override
118     public boolean isAugmenting() {
119         return augmenting;
120     }
121
122     @Override
123     public boolean isAddedByUses() {
124         return addedByUses;
125     }
126
127     @Override
128     public Optional<LeafListSchemaNode> getOriginal() {
129         return Optional.fromNullable(original);
130     }
131
132     @Override
133     public boolean isConfiguration() {
134         return configuration;
135     }
136
137     @Override
138     public ConstraintDefinition getConstraints() {
139         return constraintsDef;
140     }
141
142     @Override
143     public TypeDefinition<?> getType() {
144         return type;
145     }
146
147     @Override
148     public boolean isUserOrdered() {
149         return userOrdered;
150     }
151
152     @Override
153     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
154         return unknownNodes;
155     }
156
157     @Override
158     public int hashCode() {
159         final int prime = 31;
160         int result = 1;
161         result = prime * result + Objects.hashCode(qname);
162         result = prime * result + Objects.hashCode(path);
163         return result;
164     }
165
166     @Override
167     public boolean equals(final Object obj) {
168         if (this == obj) {
169             return true;
170         }
171         if (obj == null) {
172             return false;
173         }
174         if (getClass() != obj.getClass()) {
175             return false;
176         }
177         LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
178         return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
179     }
180
181     @Override
182     public String toString() {
183         StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
184         sb.append("[");
185         sb.append(qname);
186         sb.append("]");
187         return sb.toString();
188     }
189 }