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