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.Collections;
11 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.YangNode;
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(YangNode parent) {
53 instance.setParent(parent);
54 instance.setPath(schemaPath);
55 instance.setConstraints(constraints.build());
56 instance.setDescription(description);
57 instance.setReference(reference);
58 instance.setStatus(status);
59 instance.setAugmenting(augmenting);
60 instance.setAddedByUses(addedByUses);
61 instance.setConfiguration(configuration);
62 instance.setDefault(defaultStr);
63 instance.setUnits(unitsStr);
65 if (type == null && typedef == null) {
66 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
71 instance.setType(typedef.build(instance));
73 instance.setType(type);
77 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
78 unknownNodes.add(b.build(instance));
80 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
81 instance.setUnknownSchemaNodes(unknownNodes);
89 public void setQName(QName qname) {
94 public SchemaPath getPath() {
99 public void setPath(final SchemaPath path) {
100 this.schemaPath = path;
104 public ConstraintsBuilder getConstraints() {
109 public String getDescription() {
114 public void setDescription(final String description) {
115 this.description = description;
119 public String getReference() {
124 public void setReference(final String reference) {
125 this.reference = reference;
129 public Status getStatus() {
134 public void setStatus(final Status status) {
135 if (status != null) {
136 this.status = status;
141 public boolean isAugmenting() {
146 public void setAugmenting(final boolean augmenting) {
147 this.augmenting = augmenting;
151 public boolean isAddedByUses() {
156 public void setAddedByUses(final boolean addedByUses) {
157 this.addedByUses = addedByUses;
161 public Boolean isConfiguration() {
162 return configuration;
166 public void setConfiguration(final Boolean configuration) {
167 this.configuration = configuration;
170 public String getDefaultStr() {
174 public void setDefaultStr(String defaultStr) {
175 this.defaultStr = defaultStr;
178 public String getUnits() {
182 public void setUnits(String unitsStr) {
183 this.unitsStr = unitsStr;
187 public int hashCode() {
188 final int prime = 31;
190 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
195 public boolean equals(Object obj) {
202 if (getClass() != obj.getClass()) {
205 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
206 if (schemaPath == null) {
207 if (other.schemaPath != null) {
210 } else if (!schemaPath.equals(other.schemaPath)) {
213 if (parentBuilder == null) {
214 if (other.parentBuilder != null) {
217 } else if (!parentBuilder.equals(other.parentBuilder)) {
224 public String toString() {
225 return "leaf " + qname.getLocalName();
228 private final class LeafSchemaNodeImpl implements LeafSchemaNode {
229 private final QName qname;
230 private SchemaPath path;
231 private YangNode parent;
232 private String description;
233 private String reference;
234 private Status status = Status.CURRENT;
235 private boolean augmenting;
236 private boolean addedByUses;
237 private boolean configuration;
238 private ConstraintDefinition constraintsDef;
239 private TypeDefinition<?> type;
240 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
241 private String defaultStr;
242 private String unitsStr;
244 private LeafSchemaNodeImpl(final QName qname) {
249 public QName getQName() {
254 public SchemaPath getPath() {
258 private void setPath(final SchemaPath path) {
263 public YangNode getParent() {
267 private void setParent(YangNode parent) {
268 this.parent = parent;
272 public String getDescription() {
276 private void setDescription(String description) {
277 this.description = description;
281 public String getReference() {
285 private void setReference(String reference) {
286 this.reference = reference;
290 public Status getStatus() {
294 private void setStatus(Status status) {
295 if (status != null) {
296 this.status = status;
301 public boolean isAugmenting() {
305 private void setAugmenting(boolean augmenting) {
306 this.augmenting = augmenting;
310 public boolean isAddedByUses() {
314 private void setAddedByUses(final boolean addedByUses) {
315 this.addedByUses = addedByUses;
319 public boolean isConfiguration() {
320 return configuration;
323 private void setConfiguration(boolean configuration) {
324 this.configuration = configuration;
328 public ConstraintDefinition getConstraints() {
329 return constraintsDef;
332 private void setConstraints(ConstraintDefinition constraintsDef) {
333 this.constraintsDef = constraintsDef;
337 public TypeDefinition<?> getType() {
341 private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
346 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
350 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
351 if (unknownNodes != null) {
352 this.unknownNodes = unknownNodes;
357 public String getDefault() {
361 private void setDefault(String defaultStr) {
362 this.defaultStr = defaultStr;
366 public String getUnits() {
370 public void setUnits(String unitsStr) {
371 this.unitsStr = unitsStr;
375 public int hashCode() {
376 final int prime = 31;
378 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
379 result = prime * result + ((path == null) ? 0 : path.hashCode());
384 public boolean equals(Object obj) {
391 if (getClass() != obj.getClass()) {
394 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
396 if (other.qname != null) {
399 } else if (!qname.equals(other.qname)) {
403 if (other.path != null) {
406 } else if (!path.equals(other.path)) {
413 public String toString() {
414 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
416 sb.append("qname=" + qname);
417 sb.append(", path=" + path);
419 return sb.toString();