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.List;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
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.parser.builder.api.ConstraintsBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
25 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
27 import com.google.common.base.Optional;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableList;
31 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
32 private LeafSchemaNodeImpl instance;
33 private String defaultStr;
34 private String unitsStr;
36 private SchemaPath schemaPath;
37 private String description;
38 private String reference;
39 private Status status = Status.CURRENT;
40 // DataSchemaNode args
41 private boolean augmenting;
42 private boolean addedByUses;
43 private LeafSchemaNode originalNode;
44 private LeafSchemaNodeBuilder originalBuilder;
45 private boolean configuration;
46 private final ConstraintsBuilder constraints;
48 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
49 super(moduleName, line, qname);
50 this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
51 constraints = new ConstraintsBuilderImpl(moduleName, line);
54 public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
55 super(moduleName, line, qname);
56 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
57 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
59 description = base.getDescription();
60 reference = base.getReference();
61 status = base.getStatus();
62 augmenting = base.isAugmenting();
63 addedByUses = base.isAddedByUses();
65 configuration = base.isConfiguration();
66 this.type = base.getType();
67 unknownNodes.addAll(base.getUnknownSchemaNodes());
69 defaultStr = base.getDefault();
70 unitsStr = base.getUnits();
74 public LeafSchemaNode build() {
75 if (instance != null) {
79 instance = new LeafSchemaNodeImpl(qname, schemaPath);
81 instance.description = description;
82 instance.reference = reference;
83 instance.status = status;
84 instance.augmenting = augmenting;
85 instance.addedByUses = addedByUses;
86 instance.configuration = configuration;
87 instance.constraintsDef = constraints.toInstance();
88 instance.defaultStr = defaultStr;
89 instance.unitsStr = unitsStr;
91 if (type == null && typedef == null) {
92 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
97 instance.type = typedef.build();
103 if (originalNode == null && originalBuilder != null) {
104 originalNode = originalBuilder.build();
106 instance.original = originalNode;
109 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
110 unknownNodes.add(b.build());
112 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
118 public SchemaPath getPath() {
123 public void setPath(final SchemaPath path) {
124 this.schemaPath = path;
128 public ConstraintsBuilder getConstraints() {
133 public String getDescription() {
138 public void setDescription(final String description) {
139 this.description = description;
143 public String getReference() {
148 public void setReference(final String reference) {
149 this.reference = reference;
153 public Status getStatus() {
158 public void setStatus(final Status status) {
159 this.status = Preconditions.checkNotNull(status, "status cannot be null");
163 public boolean isAugmenting() {
168 public void setAugmenting(final boolean augmenting) {
169 this.augmenting = augmenting;
173 public boolean isAddedByUses() {
178 public void setAddedByUses(final boolean addedByUses) {
179 this.addedByUses = addedByUses;
183 public LeafSchemaNodeBuilder getOriginal() {
184 return originalBuilder;
188 public void setOriginal(final SchemaNodeBuilder builder) {
189 Preconditions.checkArgument(builder instanceof LeafSchemaNodeBuilder, "Original of leaf cannot be " + builder);
190 this.originalBuilder = (LeafSchemaNodeBuilder) builder;
194 public boolean isConfiguration() {
195 return configuration;
199 public void setConfiguration(final boolean configuration) {
200 this.configuration = configuration;
203 public String getDefaultStr() {
207 public void setDefaultStr(final String defaultStr) {
208 this.defaultStr = defaultStr;
211 public String getUnits() {
215 public void setUnits(final String unitsStr) {
216 this.unitsStr = unitsStr;
220 public int hashCode() {
221 final int prime = 31;
223 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
228 public boolean equals(final Object obj) {
235 if (getClass() != obj.getClass()) {
238 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
239 if (schemaPath == null) {
240 if (other.schemaPath != null) {
243 } else if (!schemaPath.equals(other.schemaPath)) {
246 if (getParent() == null) {
247 if (other.getParent() != null) {
250 } else if (!getParent().equals(other.getParent())) {
257 public String toString() {
258 return "leaf " + qname.getLocalName();
261 private static final class LeafSchemaNodeImpl implements LeafSchemaNode, DerivableSchemaNode {
262 private final QName qname;
263 private final SchemaPath path;
264 private String description;
265 private String reference;
266 private Status status;
267 private boolean augmenting;
268 private boolean addedByUses;
269 private LeafSchemaNode original;
270 private boolean configuration;
271 private ConstraintDefinition constraintsDef;
272 private TypeDefinition<?> type;
273 private ImmutableList<UnknownSchemaNode> unknownNodes;
274 private String defaultStr;
275 private String unitsStr;
277 private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
283 public QName getQName() {
288 public SchemaPath getPath() {
293 public String getDescription() {
298 public String getReference() {
303 public Status getStatus() {
308 public boolean isAugmenting() {
313 public boolean isAddedByUses() {
318 public Optional<LeafSchemaNode> getOriginal() {
319 return Optional.fromNullable(original);
323 public boolean isConfiguration() {
324 return configuration;
328 public ConstraintDefinition getConstraints() {
329 return constraintsDef;
333 public TypeDefinition<?> getType() {
338 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
343 public String getDefault() {
348 public String getUnits() {
353 public int hashCode() {
354 final int prime = 31;
356 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
357 result = prime * result + ((path == null) ? 0 : path.hashCode());
362 public boolean equals(final Object obj) {
369 if (getClass() != obj.getClass()) {
372 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
374 if (other.qname != null) {
377 } else if (!qname.equals(other.qname)) {
381 if (other.path != null) {
384 } else if (!path.equals(other.path)) {
391 public String toString() {
392 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
394 sb.append("qname=").append(qname);
395 sb.append(", path=").append(path);
397 return sb.toString();