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.model.parser.builder.impl;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
30 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
31 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
33 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
34 private final AugmentationSchemaImpl instance;
35 private final int line;
36 private final String augmentTargetStr;
37 private SchemaPath augmentTarget;
38 private SchemaPath finalAugmentTarget;
39 private String whenCondition;
40 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
41 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
42 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
43 private boolean resolved;
45 AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
46 this.augmentTargetStr = augmentTargetStr;
48 final SchemaPath targetPath = YangModelBuilderUtil
49 .parseAugmentPath(augmentTargetStr);
50 augmentTarget = targetPath;
51 instance = new AugmentationSchemaImpl(targetPath);
55 public int getLine() {
60 public void addChildNode(DataSchemaNodeBuilder childNode) {
61 childNodes.add(childNode);
65 public Set<DataSchemaNodeBuilder> getChildNodes() {
70 public void addGrouping(GroupingBuilder grouping) {
71 groupings.add(grouping);
75 public void addUsesNode(UsesNodeBuilder usesBuilder) {
76 usesNodes.add(usesBuilder);
80 * Always returns null.
83 public QName getQName() {
88 public AugmentationSchema build() {
89 instance.setTargetPath(finalAugmentTarget);
91 RevisionAwareXPath whenStmt;
92 if (whenCondition == null) {
95 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
97 instance.setWhenCondition(whenStmt);
100 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
101 for (DataSchemaNodeBuilder node : childNodes) {
102 childs.put(node.getQName(), node.build());
104 instance.setChildNodes(childs);
107 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
108 for (GroupingBuilder builder : groupings) {
109 groupingDefinitions.add(builder.build());
111 instance.setGroupings(groupingDefinitions);
114 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
115 for (UsesNodeBuilder builder : usesNodes) {
116 usesNodeDefinitions.add(builder.build());
118 instance.setUses(usesNodeDefinitions);
124 public boolean isResolved() {
129 public void setResolved(boolean resolved) {
130 this.resolved = resolved;
133 public String getWhenCondition() {
134 return whenCondition;
137 public void addWhenCondition(String whenCondition) {
138 this.whenCondition = whenCondition;
142 public void addTypedef(TypeDefinitionBuilder type) {
143 throw new UnsupportedOperationException(
144 "Augmentation can not contains type definitions");
148 public void setDescription(String description) {
149 instance.setDescription(description);
153 public void setReference(String reference) {
154 instance.setReference(reference);
158 public void setStatus(Status status) {
159 instance.setStatus(status);
163 public SchemaPath getTargetPath() {
164 return augmentTarget;
168 public void setTargetPath(SchemaPath path) {
169 this.finalAugmentTarget = path;
173 public String getTargetPathAsString() {
174 return augmentTargetStr;
178 public int hashCode() {
179 final int prime = 17;
181 result = prime * result
182 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
183 result = prime * result
184 + ((childNodes == null) ? 0 : childNodes.hashCode());
185 result = prime * result
186 + ((groupings == null) ? 0 : groupings.hashCode());
187 result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
188 result = prime * result
189 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
194 public boolean equals(Object obj) {
201 if (getClass() != obj.getClass()) {
204 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
205 if (augmentTargetStr == null) {
206 if (other.augmentTargetStr != null) {
209 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
212 if (childNodes == null) {
213 if (other.childNodes != null) {
216 } else if (!childNodes.equals(other.childNodes)) {
219 if (groupings == null) {
220 if (other.groupings != null) {
223 } else if (!groupings.equals(other.groupings)) {
226 if (usesNodes == null) {
227 if (other.usesNodes != null) {
230 } else if (!usesNodes.equals(other.usesNodes)) {
233 if (whenCondition == null) {
234 if (other.whenCondition != null) {
237 } else if (!whenCondition.equals(other.whenCondition)) {
244 private static class AugmentationSchemaImpl implements AugmentationSchema {
245 private SchemaPath targetPath;
246 private RevisionAwareXPath whenCondition;
247 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
248 private Set<GroupingDefinition> groupings = Collections.emptySet();
249 private Set<UsesNode> uses = Collections.emptySet();
251 private String description;
252 private String reference;
253 private Status status;
255 private AugmentationSchemaImpl(SchemaPath targetPath) {
256 this.targetPath = targetPath;
260 public SchemaPath getTargetPath() {
264 private void setTargetPath(SchemaPath path) {
265 this.targetPath = path;
269 public RevisionAwareXPath getWhenCondition() {
270 return whenCondition;
273 private void setWhenCondition(RevisionAwareXPath whenCondition) {
274 this.whenCondition = whenCondition;
278 public Set<DataSchemaNode> getChildNodes() {
279 return new HashSet<DataSchemaNode>(childNodes.values());
282 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
283 if (childNodes != null) {
284 this.childNodes = childNodes;
289 public Set<GroupingDefinition> getGroupings() {
293 private void setGroupings(Set<GroupingDefinition> groupings) {
294 if (groupings != null) {
295 this.groupings = groupings;
300 public Set<UsesNode> getUses() {
304 private void setUses(Set<UsesNode> uses) {
311 * Always returns an empty set, because augmentation can not contains
315 public Set<TypeDefinition<?>> getTypeDefinitions() {
316 return Collections.emptySet();
320 public String getDescription() {
324 private void setDescription(String description) {
325 this.description = description;
329 public String getReference() {
333 private void setReference(String reference) {
334 this.reference = reference;
338 public Status getStatus() {
342 private void setStatus(Status status) {
343 this.status = status;
347 public DataSchemaNode getDataChildByName(QName name) {
348 return childNodes.get(name);
352 public DataSchemaNode getDataChildByName(String name) {
353 DataSchemaNode result = null;
354 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
355 if (entry.getKey().getLocalName().equals(name)) {
356 result = entry.getValue();
364 public int hashCode() {
365 final int prime = 17;
367 result = prime * result
368 + ((targetPath == null) ? 0 : targetPath.hashCode());
369 result = prime * result
370 + ((childNodes == null) ? 0 : childNodes.hashCode());
371 result = prime * result
372 + ((groupings == null) ? 0 : groupings.hashCode());
373 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
374 result = prime * result
375 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
380 public boolean equals(Object obj) {
387 if (getClass() != obj.getClass()) {
390 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
391 if (targetPath == null) {
392 if (other.targetPath != null) {
395 } else if (!targetPath.equals(other.targetPath)) {
398 if (childNodes == null) {
399 if (other.childNodes != null) {
402 } else if (!childNodes.equals(other.childNodes)) {
405 if (groupings == null) {
406 if (other.groupings != null) {
409 } else if (!groupings.equals(other.groupings)) {
413 if (other.uses != null) {
416 } else if (!uses.equals(other.uses)) {
419 if (whenCondition == null) {
420 if (other.whenCondition != null) {
423 } else if (!whenCondition.equals(other.whenCondition)) {
430 public String toString() {
431 StringBuilder sb = new StringBuilder(
432 AugmentationSchemaImpl.class.getSimpleName());
434 sb.append("targetPath=" + targetPath);
435 sb.append(", childNodes=" + childNodes.values());
436 sb.append(", groupings=" + groupings);
437 sb.append(", uses=" + uses);
439 return sb.toString();