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.controller.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.controller.yang.parser.builder.api.ConfigNode;
23 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
25 import org.opendaylight.controller.yang.parser.util.Comparators;
27 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder,
28 GroupingMember, ConfigNode {
29 private boolean isBuilt;
30 private final LeafSchemaNodeImpl instance;
32 private SchemaPath schemaPath;
33 private String description;
34 private String reference;
35 private Status status = Status.CURRENT;
36 private List<UnknownSchemaNode> unknownNodes;
37 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
38 // DataSchemaNode args
39 private boolean augmenting;
40 private boolean addedByUses;
41 private Boolean configuration;
42 private final ConstraintsBuilder constraints;
44 private String defaultStr;
45 private String unitsStr;
47 public LeafSchemaNodeBuilder(final QName qname, final SchemaPath schemaPath, final int line) {
49 this.schemaPath = schemaPath;
50 instance = new LeafSchemaNodeImpl(qname);
51 constraints = new ConstraintsBuilder(line);
54 public LeafSchemaNodeBuilder(final LeafSchemaNodeBuilder b) {
55 super(b.getLine(), b.getQName());
56 instance = new LeafSchemaNodeImpl(qname);
57 constraints = b.getConstraints();
58 schemaPath = b.getPath();
61 typedef = b.getTypedef();
63 description = b.getDescription();
64 reference = b.getReference();
65 status = b.getStatus();
66 augmenting = b.isAugmenting();
67 addedByUses = b.isAddedByUses();
68 configuration = b.isConfiguration();
69 unknownNodes = b.unknownNodes;
70 addedUnknownNodes.addAll(b.getUnknownNodes());
72 defaultStr = b.getDefaultStr();
73 unitsStr = b.getUnits();
77 public LeafSchemaNode build() {
79 instance.setPath(schemaPath);
80 instance.setConstraints(constraints.build());
81 instance.setDescription(description);
82 instance.setReference(reference);
83 instance.setStatus(status);
84 instance.setAugmenting(augmenting);
85 instance.setAddedByUses(addedByUses);
86 instance.setConfiguration(configuration);
87 instance.setDefault(defaultStr);
88 instance.setUnits(unitsStr);
92 instance.setType(typedef.build());
94 instance.setType(type);
98 if (unknownNodes == null) {
99 unknownNodes = new ArrayList<UnknownSchemaNode>();
100 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
101 unknownNodes.add(b.build());
103 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
105 instance.setUnknownSchemaNodes(unknownNodes);
112 public SchemaPath getPath() {
117 public void setPath(final SchemaPath path) {
118 this.schemaPath = path;
122 public ConstraintsBuilder getConstraints() {
127 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
128 addedUnknownNodes.add(unknownNode);
131 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
132 return addedUnknownNodes;
135 public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
136 this.unknownNodes = unknownNodes;
139 public String getDescription() {
144 public void setDescription(final String description) {
145 this.description = description;
148 public String getReference() {
153 public void setReference(final String reference) {
154 this.reference = reference;
157 public Status getStatus() {
162 public void setStatus(final Status status) {
163 if (status != null) {
164 this.status = status;
168 public boolean isAugmenting() {
173 public void setAugmenting(final boolean augmenting) {
174 this.augmenting = augmenting;
178 public boolean isAddedByUses() {
183 public void setAddedByUses(final boolean addedByUses) {
184 this.addedByUses = addedByUses;
187 public Boolean isConfiguration() {
188 return configuration;
192 public void setConfiguration(final Boolean configuration) {
193 this.configuration = configuration;
196 public String getDefaultStr() {
200 public void setDefaultStr(String defaultStr) {
201 this.defaultStr = defaultStr;
204 public String getUnits() {
208 public void setUnits(String unitsStr) {
209 this.unitsStr = unitsStr;
213 public int hashCode() {
214 final int prime = 31;
216 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
221 public boolean equals(Object obj) {
228 if (getClass() != obj.getClass()) {
231 LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
232 if (schemaPath == null) {
233 if (other.schemaPath != null) {
236 } else if (!schemaPath.equals(other.schemaPath)) {
239 if (parent == null) {
240 if (other.parent != null) {
243 } else if (!parent.equals(other.parent)) {
250 public String toString() {
251 return "leaf " + qname.getLocalName();
254 private final class LeafSchemaNodeImpl implements LeafSchemaNode {
255 private final QName qname;
256 private SchemaPath path;
257 private String description;
258 private String reference;
259 private Status status = Status.CURRENT;
260 private boolean augmenting;
261 private boolean addedByUses;
262 private boolean configuration;
263 private ConstraintDefinition constraintsDef;
264 private TypeDefinition<?> type;
265 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
266 private String defaultStr;
267 private String unitsStr;
269 private LeafSchemaNodeImpl(final QName qname) {
274 public QName getQName() {
279 public SchemaPath getPath() {
283 private void setPath(final SchemaPath path) {
288 public String getDescription() {
292 private void setDescription(String description) {
293 this.description = description;
297 public String getReference() {
301 private void setReference(String reference) {
302 this.reference = reference;
306 public Status getStatus() {
310 private void setStatus(Status status) {
311 if (status != null) {
312 this.status = status;
317 public boolean isAugmenting() {
321 private void setAugmenting(boolean augmenting) {
322 this.augmenting = augmenting;
326 public boolean isAddedByUses() {
330 private void setAddedByUses(final boolean addedByUses) {
331 this.addedByUses = addedByUses;
335 public boolean isConfiguration() {
336 return configuration;
339 private void setConfiguration(boolean configuration) {
340 this.configuration = configuration;
344 public ConstraintDefinition getConstraints() {
345 return constraintsDef;
348 private void setConstraints(ConstraintDefinition constraintsDef) {
349 this.constraintsDef = constraintsDef;
353 public TypeDefinition<?> getType() {
357 private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
362 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
366 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
367 if (unknownNodes != null) {
368 this.unknownNodes = unknownNodes;
373 public String getDefault() {
377 private void setDefault(String defaultStr) {
378 this.defaultStr = defaultStr;
382 public String getUnits() {
386 public void setUnits(String unitsStr) {
387 this.unitsStr = unitsStr;
391 public int hashCode() {
392 final int prime = 31;
394 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
395 result = prime * result + ((path == null) ? 0 : path.hashCode());
400 public boolean equals(Object obj) {
407 if (getClass() != obj.getClass()) {
410 LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
412 if (other.qname != null) {
415 } else if (!qname.equals(other.qname)) {
419 if (other.path != null) {
422 } else if (!path.equals(other.path)) {
429 public String toString() {
430 StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
432 sb.append("qname=" + qname);
433 sb.append(", path=" + path);
435 return sb.toString();