Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / LeafEffectiveStatementImpl.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 org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import java.util.LinkedList;
15 import java.util.List;
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.LeafSchemaNode;
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.LeafStatement;
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.Utils;
28
29 public class LeafEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafStatement> implements
30         LeafSchemaNode, DerivableSchemaNode {
31     private final QName qname;
32     private final SchemaPath path;
33
34     boolean augmenting;
35     private boolean addedByUses;
36     private LeafSchemaNode original;
37     private boolean configuration = true;
38     private ConstraintDefinition constraintsDef;
39     private TypeDefinition<?> type;
40     private String defaultStr;
41     private String unitsStr;
42
43     private ImmutableList<UnknownSchemaNode> unknownNodes;
44
45     public LeafEffectiveStatementImpl(StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
46         super(ctx);
47         this.qname = ctx.getStatementArgument();
48         this.path = Utils.getSchemaPath(ctx);
49         this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
50
51         initSubstatementCollections(ctx);
52         initCopyType(ctx);
53     }
54
55     private void initCopyType(
56             StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
57
58         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
59
60         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
61             augmenting = true;
62         }
63         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
64             addedByUses = true;
65         }
66         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
67             addedByUses = augmenting = true;
68         }
69
70         if (ctx.getOriginalCtx() != null) {
71             original = (LeafSchemaNode) ctx.getOriginalCtx().buildEffective();
72         }
73     }
74
75     private void initSubstatementCollections(
76             final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
77         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
78
79         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
80
81         boolean configurationInit = false;
82         boolean defaultInit = false;
83         boolean unitsInit = 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 (!defaultInit && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
97                 DefaultEffectiveStatementImpl defStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
98                 this.defaultStr = defStmt.argument();
99                 defaultInit = true;
100             }
101             if (!unitsInit && effectiveStatement instanceof UnitsEffectiveStatementImpl) {
102                 UnitsEffectiveStatementImpl unitStmt = (UnitsEffectiveStatementImpl) effectiveStatement;
103                 this.unitsStr = unitStmt.argument();
104                 unitsInit = true;
105             }
106         }
107
108         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
109     }
110
111     @Override
112     public QName getQName() {
113         return qname;
114     }
115
116     @Override
117     public SchemaPath getPath() {
118         return path;
119     }
120
121     @Override
122     public boolean isAugmenting() {
123         return augmenting;
124     }
125
126     @Override
127     public boolean isAddedByUses() {
128         return addedByUses;
129     }
130
131     @Override
132     public Optional<LeafSchemaNode> getOriginal() {
133         return Optional.fromNullable(original);
134     }
135
136     @Override
137     public boolean isConfiguration() {
138         return configuration;
139     }
140
141     @Override
142     public ConstraintDefinition getConstraints() {
143         return constraintsDef;
144     }
145
146     @Override
147     public TypeDefinition<?> getType() {
148         return type;
149     }
150
151     @Override
152     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
153         return unknownNodes;
154     }
155
156     @Override
157     public String getDefault() {
158         return defaultStr;
159     }
160
161     @Override
162     public String getUnits() {
163         return unitsStr;
164     }
165
166     @Override
167     public int hashCode() {
168         final int prime = 31;
169         int result = 1;
170         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
171         result = prime * result + ((path == null) ? 0 : path.hashCode());
172         return result;
173     }
174
175     @Override
176     public boolean equals(final Object obj) {
177         if (this == obj) {
178             return true;
179         }
180         if (obj == null) {
181             return false;
182         }
183         if (getClass() != obj.getClass()) {
184             return false;
185         }
186         LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
187         if (qname == null) {
188             if (other.qname != null) {
189                 return false;
190             }
191         } else if (!qname.equals(other.qname)) {
192             return false;
193         }
194         if (path == null) {
195             if (other.path != null) {
196                 return false;
197             }
198         } else if (!path.equals(other.path)) {
199             return false;
200         }
201         return true;
202     }
203
204     @Override
205     public String toString() {
206         StringBuilder sb = new StringBuilder(LeafEffectiveStatementImpl.class.getSimpleName());
207         sb.append("[");
208         sb.append("qname=").append(qname);
209         sb.append(", path=").append(path);
210         sb.append("]");
211         return sb.toString();
212     }
213 }