2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.util.Comparators;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
27 private boolean isBuilt;
28 private final LeafSchemaNodeImpl instance;
30 private SchemaPath schemaPath;
31 private String description;
32 private String reference;
33 private Status status = Status.CURRENT;
34 // DataSchemaNode args
35 private boolean augmenting;
36 private boolean addedByUses;
37 private Boolean configuration;
38 private final ConstraintsBuilder constraints;
40 private String defaultStr;
41 private String unitsStr;
43 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
44 super(moduleName, line, qname);
45 this.schemaPath = schemaPath;
46 instance = new LeafSchemaNodeImpl(qname);
47 constraints = new ConstraintsBuilder(moduleName, line);
51 public LeafSchemaNode build() {
53 instance.setPath(schemaPath);
54 instance.setConstraints(constraints.build());
55 instance.setDescription(description);
56 instance.setReference(reference);
57 instance.setStatus(status);
58 instance.setAugmenting(augmenting);
59 instance.setAddedByUses(addedByUses);
60 instance.setConfiguration(configuration);
61 instance.setDefault(defaultStr);
62 instance.setUnits(unitsStr);
64 if (type == null && typedef == null) {
65 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
70 instance.setType(typedef.build());
72 instance.setType(type);
76 if (unknownNodes == null) {
77 unknownNodes = new ArrayList<>();
78 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
79 unknownNodes.add(b.build());
81 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
83 instance.setUnknownSchemaNodes(unknownNodes);
91 public void setQName(QName qname) {
96 public SchemaPath getPath() {
101 public void setPath(final SchemaPath path) {
102 this.schemaPath = path;
106 public ConstraintsBuilder getConstraints() {
111 public String getDescription() {
116 public void setDescription(final String description) {
117 this.description = description;
121 public String getReference() {
126 public void setReference(final String reference) {
127 this.reference = reference;
131 public Status getStatus() {
136 public void setStatus(final Status status) {
137 if (status != null) {
138 this.status = status;
143 public boolean isAugmenting() {
148 public void setAugmenting(final boolean augmenting) {
149 this.augmenting = augmenting;
153 public boolean isAddedByUses() {
158 public void setAddedByUses(final boolean addedByUses) {
159 this.addedByUses = addedByUses;
163 public Boolean isConfiguration() {
164 return configuration;
168 public void setConfiguration(final Boolean configuration) {
169 this.configuration = configuration;
172 public String getDefaultStr() {
176 public void setDefaultStr(String defaultStr) {
177 this.defaultStr = defaultStr;
180 public String getUnits() {
184 public void setUnits(String unitsStr) {
185 this.unitsStr = unitsStr;
189 public int hashCode() {
190 final int prime = 31;
192 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
197 public boolean equals(Object obj) {
204 if (getClass() != obj.getClass()) {
207 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
208 if (schemaPath == null) {
209 if (other.schemaPath != null) {
212 } else if (!schemaPath.equals(other.schemaPath)) {
215 if (parent == null) {
216 if (other.parent != null) {
219 } else if (!parent.equals(other.parent)) {
226 public String toString() {
227 return "leaf " + qname.getLocalName();
230 private final class LeafSchemaNodeImpl implements LeafSchemaNode {
231 private final QName qname;
232 private SchemaPath path;
233 private String description;
234 private String reference;
235 private Status status = Status.CURRENT;
236 private boolean augmenting;
237 private boolean addedByUses;
238 private boolean configuration;
239 private ConstraintDefinition constraintsDef;
240 private TypeDefinition<?> type;
241 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
242 private String defaultStr;
243 private String unitsStr;
245 private LeafSchemaNodeImpl(final QName qname) {
250 public QName getQName() {
255 public SchemaPath getPath() {
259 private void setPath(final SchemaPath path) {
264 public String getDescription() {
268 private void setDescription(String description) {
269 this.description = description;
273 public String getReference() {
277 private void setReference(String reference) {
278 this.reference = reference;
282 public Status getStatus() {
286 private void setStatus(Status status) {
287 if (status != null) {
288 this.status = status;
293 public boolean isAugmenting() {
297 private void setAugmenting(boolean augmenting) {
298 this.augmenting = augmenting;
302 public boolean isAddedByUses() {
306 private void setAddedByUses(final boolean addedByUses) {
307 this.addedByUses = addedByUses;
311 public boolean isConfiguration() {
312 return configuration;
315 private void setConfiguration(boolean configuration) {
316 this.configuration = configuration;
320 public ConstraintDefinition getConstraints() {
321 return constraintsDef;
324 private void setConstraints(ConstraintDefinition constraintsDef) {
325 this.constraintsDef = constraintsDef;
329 public TypeDefinition<?> getType() {
333 private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
338 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
342 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
343 if (unknownNodes != null) {
344 this.unknownNodes = unknownNodes;
349 public String getDefault() {
353 private void setDefault(String defaultStr) {
354 this.defaultStr = defaultStr;
358 public String getUnits() {
362 public void setUnits(String unitsStr) {
363 this.unitsStr = unitsStr;
367 public int hashCode() {
368 final int prime = 31;
370 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
371 result = prime * result + ((path == null) ? 0 : path.hashCode());
376 public boolean equals(Object obj) {
383 if (getClass() != obj.getClass()) {
386 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
388 if (other.qname != null) {
391 } else if (!qname.equals(other.qname)) {
395 if (other.path != null) {
398 } else if (!path.equals(other.path)) {
405 public String toString() {
406 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
408 sb.append("qname=" + qname);
409 sb.append(", path=" + path);
411 return sb.toString();