BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ChoiceBuilder.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 com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Objects;
17 import java.util.Set;
18 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
33 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
34
35 /**
36  * @deprecated Pre-Beryllium implementation, scheduled for removal.
37  */
38 @Deprecated
39 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder, AugmentationTargetBuilder {
40     private ChoiceSchemaNodeImpl instance;
41
42     // DataSchemaNode args
43     private boolean augmenting;
44     private boolean addedByUses;
45     private boolean configuration;
46     private ChoiceSchemaNode originalNode;
47     private ChoiceBuilder originalBuilder;
48     private final ConstraintsBuilder constraints;
49     // AugmentationTarget args
50     private final Set<AugmentationSchema> augmentations = new HashSet<>();
51     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
52     // ChoiceNode args
53     private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
54     private String defaultCase;
55
56     public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
57         super(moduleName, line, qname);
58         this.schemaPath = path;
59         constraints = new ConstraintsBuilderImpl(moduleName, line);
60     }
61
62     public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
63             final ChoiceSchemaNode base) {
64         super(moduleName, line, qname);
65         this.schemaPath = path;
66         constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
67
68         description = base.getDescription();
69         reference = base.getReference();
70         status = base.getStatus();
71         augmenting = base.isAugmenting();
72         addedByUses = base.isAddedByUses();
73         originalNode =  base;
74         configuration = base.isConfiguration();
75         augmentations.addAll(base.getAvailableAugmentations());
76
77         Set<DataSchemaNodeBuilder> wrapped = BuilderUtils.wrapChildNodes(moduleName, line, new HashSet<DataSchemaNode>(
78                 base.getCases()), path, qname);
79         for (DataSchemaNodeBuilder wrap : wrapped) {
80             if (wrap instanceof ChoiceCaseBuilder) {
81                 caseBuilders.add((ChoiceCaseBuilder) wrap);
82             }
83         }
84
85         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
86                 qname));
87     }
88
89     @Override
90     public ChoiceSchemaNode build() {
91         if (instance != null) {
92             return instance;
93         }
94
95         instance = new ChoiceSchemaNodeImpl(qname, schemaPath);
96
97         instance.description = description;
98         instance.reference = reference;
99         instance.status = status;
100         instance.augmenting = augmenting;
101         instance.addedByUses = addedByUses;
102         instance.configuration = configuration;
103
104         instance.constraints = constraints.build();
105         instance.defaultCase = defaultCase;
106
107         // ORIGINAL NODE
108         if (originalNode == null && originalBuilder != null) {
109             originalNode = originalBuilder.build();
110         }
111         instance.original = originalNode;
112
113         // CASES
114         final Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
115         for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
116             cases.add(caseBuilder.build());
117         }
118         instance.cases = ImmutableSet.copyOf(cases);
119
120         // AUGMENTATIONS
121         for (AugmentationSchemaBuilder builder : augmentationBuilders) {
122             augmentations.add(builder.build());
123         }
124         instance.augmentations = ImmutableSet.copyOf(augmentations);
125
126         // UNKNOWN NODES
127         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
128             unknownNodes.add(b.build());
129         }
130         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
131
132         return instance;
133     }
134
135     public Set<ChoiceCaseBuilder> getCases() {
136         return caseBuilders;
137     }
138
139     /**
140      * Get case by name.
141      *
142      * @param caseName
143      *            name of case to search
144      * @return case with given name if present, null otherwise
145      */
146     public ChoiceCaseBuilder getCaseNodeByName(final String caseName) {
147         for (ChoiceCaseBuilder addedCase : caseBuilders) {
148             if (addedCase.getQName().getLocalName().equals(caseName)) {
149                 return addedCase;
150             }
151         }
152         return null;
153     }
154
155     /**
156      * Add case node to this choice.
157      *
158      * If node is not declared with 'case' keyword, create new case builder and
159      * make this node child of newly created case.
160      *
161      * @param caseNode
162      *            case node
163      */
164     public void addCase(final DataSchemaNodeBuilder caseNode) {
165         QName caseQName = caseNode.getQName();
166         String caseName = caseQName.getLocalName();
167
168         for (ChoiceCaseBuilder existingCase : caseBuilders) {
169             if (existingCase.getQName().getLocalName().equals(caseName)) {
170                 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
171                         + "' to node '" + qname.getLocalName() + "' in module '" + getModuleName()
172                         + "': case with same name already declared at line " + existingCase.getLine());
173             }
174         }
175
176         if (caseNode instanceof ChoiceCaseBuilder) {
177             caseBuilders.add((ChoiceCaseBuilder) caseNode);
178         } else {
179             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
180                     caseQName, caseNode.getPath());
181             if (caseNode.isAugmenting()) {
182                 // if node is added by augmentation, set case builder augmenting
183                 // as true and node augmenting as false
184                 caseBuilder.setAugmenting(true);
185                 caseNode.setAugmenting(false);
186             }
187             SchemaPath newPath = caseNode.getPath().createChild(caseQName);
188             caseNode.setPath(newPath);
189             caseBuilder.addChildNode(caseNode);
190             caseBuilders.add(caseBuilder);
191         }
192     }
193
194     @Override
195     public boolean isAugmenting() {
196         return augmenting;
197     }
198
199     @Override
200     public void setAugmenting(final boolean augmenting) {
201         this.augmenting = augmenting;
202     }
203
204     @Override
205     public boolean isAddedByUses() {
206         return addedByUses;
207     }
208
209     @Override
210     public void setAddedByUses(final boolean addedByUses) {
211         this.addedByUses = addedByUses;
212     }
213
214     @Override
215     public ChoiceBuilder getOriginal() {
216         return originalBuilder;
217     }
218
219     @Override
220     public void setOriginal(final SchemaNodeBuilder builder) {
221         Preconditions.checkArgument(builder instanceof ChoiceBuilder, "Original of choice cannot be " + builder);
222         this.originalBuilder = (ChoiceBuilder) builder;
223     }
224
225     @Override
226     public boolean isConfiguration() {
227         return configuration;
228     }
229
230     @Override
231     public void setConfiguration(final boolean configuration) {
232         this.configuration = configuration;
233     }
234
235     @Override
236     public ConstraintsBuilder getConstraints() {
237         return constraints;
238     }
239
240     @Override
241     public void addAugmentation(final AugmentationSchemaBuilder augment) {
242         augmentationBuilders.add(augment);
243     }
244
245     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
246         return augmentationBuilders;
247     }
248
249     public String getDefaultCase() {
250         return defaultCase;
251     }
252
253     public void setDefaultCase(final String defaultCase) {
254         this.defaultCase = defaultCase;
255     }
256
257     @Override
258     public int hashCode() {
259         final int prime = 31;
260         int result = 1;
261         result = prime * result + Objects.hashCode(schemaPath);
262         return result;
263     }
264
265     @Override
266     public boolean equals(final Object obj) {
267         if (this == obj) {
268             return true;
269         }
270         if (obj == null) {
271             return false;
272         }
273         if (getClass() != obj.getClass()) {
274             return false;
275         }
276         ChoiceBuilder other = (ChoiceBuilder) obj;
277         if (schemaPath == null) {
278             if (other.schemaPath != null) {
279                 return false;
280             }
281         } else if (!schemaPath.equals(other.schemaPath)) {
282             return false;
283         }
284         if (getParent() == null) {
285             if (other.getParent() != null) {
286                 return false;
287             }
288         } else if (!getParent().equals(other.getParent())) {
289             return false;
290         }
291         return true;
292     }
293
294     @Override
295     public String toString() {
296         return "choice " + qname.getLocalName();
297     }
298
299 }