5778e8a9cd4b5ecce88c9cf48aa5ef4e386a0dbd
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / LeafSchemaNodeBuilder.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
18 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
19 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
22 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
23
24 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
25     private LeafSchemaNodeImpl instance;
26     private String defaultStr;
27     private String unitsStr;
28     // SchemaNode args
29     private SchemaPath schemaPath;
30     private String description;
31     private String reference;
32     private Status status = Status.CURRENT;
33     // DataSchemaNode args
34     private boolean augmenting;
35     private boolean addedByUses;
36     private LeafSchemaNode originalNode;
37     private LeafSchemaNodeBuilder originalBuilder;
38     private boolean configuration;
39     private final ConstraintsBuilder constraints;
40
41     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
42         super(moduleName, line, qname);
43         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
44         constraints = new ConstraintsBuilderImpl(moduleName, line);
45     }
46
47     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
48         super(moduleName, line, qname);
49         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
50         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
51
52         description = base.getDescription();
53         reference = base.getReference();
54         status = base.getStatus();
55         augmenting = base.isAugmenting();
56         addedByUses = base.isAddedByUses();
57         originalNode =base;
58         configuration = base.isConfiguration();
59         this.type = base.getType();
60         unknownNodes.addAll(base.getUnknownSchemaNodes());
61
62         defaultStr = base.getDefault();
63         unitsStr = base.getUnits();
64     }
65
66     @Override
67     public LeafSchemaNode build() {
68         if (instance != null) {
69             return instance;
70         }
71
72         instance = new LeafSchemaNodeImpl(qname, schemaPath);
73
74         instance.description = description;
75         instance.reference = reference;
76         instance.status = status;
77         instance.augmenting = augmenting;
78         instance.addedByUses = addedByUses;
79         instance.configuration = configuration;
80         instance.constraintsDef = constraints.build();
81         instance.defaultStr = defaultStr;
82         instance.unitsStr = unitsStr;
83
84         if (type == null && typedef == null) {
85             throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
86         }
87
88         // TYPE
89         if (type == null) {
90             instance.type = typedef.build();
91         } else {
92             instance.type = type;
93         }
94
95         // ORIGINAL NODE
96         if (originalNode == null && originalBuilder != null) {
97             originalNode = originalBuilder.build();
98         }
99         instance.original = originalNode;
100
101         // UNKNOWN NODES
102         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
103             unknownNodes.add(b.build());
104         }
105         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
106
107         return instance;
108     }
109
110     @Override
111     public SchemaPath getPath() {
112         return schemaPath;
113     }
114
115     @Override
116     public void setPath(final SchemaPath path) {
117         this.schemaPath = path;
118     }
119
120     @Override
121     public ConstraintsBuilder getConstraints() {
122         return constraints;
123     }
124
125     @Override
126     public String getDescription() {
127         return description;
128     }
129
130     @Override
131     public void setDescription(final String description) {
132         this.description = description;
133     }
134
135     @Override
136     public String getReference() {
137         return reference;
138     }
139
140     @Override
141     public void setReference(final String reference) {
142         this.reference = reference;
143     }
144
145     @Override
146     public Status getStatus() {
147         return status;
148     }
149
150     @Override
151     public void setStatus(final Status status) {
152         this.status = Preconditions.checkNotNull(status, "status cannot be null");
153     }
154
155     @Override
156     public boolean isAugmenting() {
157         return augmenting;
158     }
159
160     @Override
161     public void setAugmenting(final boolean augmenting) {
162         this.augmenting = augmenting;
163     }
164
165     @Override
166     public boolean isAddedByUses() {
167         return addedByUses;
168     }
169
170     @Override
171     public void setAddedByUses(final boolean addedByUses) {
172         this.addedByUses = addedByUses;
173     }
174
175     @Override
176     public LeafSchemaNodeBuilder getOriginal() {
177         return originalBuilder;
178     }
179
180     @Override
181     public void setOriginal(final SchemaNodeBuilder builder) {
182         Preconditions.checkArgument(builder instanceof LeafSchemaNodeBuilder, "Original of leaf cannot be " + builder);
183         this.originalBuilder = (LeafSchemaNodeBuilder) builder;
184     }
185
186     @Override
187     public boolean isConfiguration() {
188         return configuration;
189     }
190
191     @Override
192     public void setConfiguration(final boolean configuration) {
193         this.configuration = configuration;
194     }
195
196     public String getDefaultStr() {
197         return defaultStr;
198     }
199
200     public void setDefaultStr(final String defaultStr) {
201         this.defaultStr = defaultStr;
202     }
203
204     public String getUnits() {
205         return unitsStr;
206     }
207
208     public void setUnits(final String unitsStr) {
209         this.unitsStr = unitsStr;
210     }
211
212     @Override
213     public int hashCode() {
214         final int prime = 31;
215         int result = 1;
216         result = prime * result + Objects.hashCode(schemaPath);
217         return result;
218     }
219
220     @Override
221     public boolean equals(final Object obj) {
222         if (this == obj) {
223             return true;
224         }
225         if (obj == null) {
226             return false;
227         }
228         if (getClass() != obj.getClass()) {
229             return false;
230         }
231         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
232         if (schemaPath == null) {
233             if (other.schemaPath != null) {
234                 return false;
235             }
236         } else if (!schemaPath.equals(other.schemaPath)) {
237             return false;
238         }
239         if (getParent() == null) {
240             if (other.getParent() != null) {
241                 return false;
242             }
243         } else if (!getParent().equals(other.getParent())) {
244             return false;
245         }
246         return true;
247     }
248
249     @Override
250     public String toString() {
251         return "leaf " + qname.getLocalName();
252     }
253
254 }