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