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