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.yangtools.yang.parser.builder.impl;
10 import java.util.Collections;
11 import java.util.List;
14 import java.util.TreeSet;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.UsesNode;
26 import org.opendaylight.yangtools.yang.model.api.YangNode;
27 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.Comparators;
35 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
36 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
38 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
39 AugmentationSchemaBuilder {
40 private boolean built;
41 private final AugmentationSchemaImpl instance;
43 private String whenCondition;
44 private String description;
45 private String reference;
46 private Status status = Status.CURRENT;
48 private final String augmentTargetStr;
49 private SchemaPath targetPath;
50 private SchemaPath targetNodeSchemaPath;
52 private boolean resolved;
54 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
55 super(moduleName, line, null);
56 this.augmentTargetStr = augmentTargetStr;
57 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
58 instance = new AugmentationSchemaImpl(targetPath);
62 public Set<GroupingDefinition> getGroupings() {
63 return Collections.emptySet();
67 public Set<GroupingBuilder> getGroupingBuilders() {
68 return Collections.emptySet();
72 public void addGrouping(GroupingBuilder grouping) {
73 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
77 public SchemaPath getPath() {
78 return targetNodeSchemaPath;
82 public AugmentationSchema build(YangNode parent) {
84 instance.setParent(parent);
85 instance.setDescription(description);
86 instance.setReference(reference);
87 instance.setStatus(status);
88 instance.setTargetPath(targetNodeSchemaPath);
90 RevisionAwareXPath whenStmt;
91 if (whenCondition == null) {
94 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
96 instance.setWhenCondition(whenStmt);
99 for (DataSchemaNodeBuilder node : addedChildNodes) {
100 DataSchemaNode child = node.build(instance);
101 childNodes.put(child.getQName(), child);
103 instance.setChildNodes(childNodes);
106 for (UsesNodeBuilder builder : addedUsesNodes) {
107 usesNodes.add(builder.build(instance));
109 instance.setUses(usesNodes);
112 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
113 unknownNodes.add(b.build(instance));
115 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
116 instance.setUnknownSchemaNodes(unknownNodes);
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 Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
143 return Collections.emptySet();
147 public void addTypedef(TypeDefinitionBuilder type) {
148 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
152 public String getDescription() {
157 public void setDescription(String description) {
158 this.description = description;
162 public String getReference() {
167 public void setReference(String reference) {
168 this.reference = reference;
172 public Status getStatus() {
177 public void setStatus(Status status) {
178 if (status != null) {
179 this.status = status;
184 public String getTargetPathAsString() {
185 return augmentTargetStr;
189 public SchemaPath getTargetPath() {
194 public SchemaPath getTargetNodeSchemaPath() {
195 return targetNodeSchemaPath;
199 public void setTargetNodeSchemaPath(SchemaPath path) {
200 this.targetNodeSchemaPath = path;
204 public int hashCode() {
205 final int prime = 17;
207 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
208 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
209 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
214 public boolean equals(Object obj) {
221 if (getClass() != obj.getClass()) {
224 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
225 if (augmentTargetStr == null) {
226 if (other.augmentTargetStr != null) {
229 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
232 if (whenCondition == null) {
233 if (other.whenCondition != null) {
236 } else if (!whenCondition.equals(other.whenCondition)) {
239 if (addedChildNodes == null) {
240 if (other.addedChildNodes != null) {
243 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
249 public String toString() {
250 return "augment " + augmentTargetStr;
253 private final class AugmentationSchemaImpl implements AugmentationSchema {
254 private YangNode parent;
255 private SchemaPath targetPath;
256 private RevisionAwareXPath whenCondition;
257 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
258 private Set<UsesNode> uses = Collections.emptySet();
259 private String description;
260 private String reference;
261 private Status status;
262 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
264 private AugmentationSchemaImpl(SchemaPath targetPath) {
265 this.targetPath = targetPath;
269 public YangNode getParent() {
273 private void setParent(YangNode parent) {
274 this.parent = parent;
278 public SchemaPath getTargetPath() {
282 private void setTargetPath(SchemaPath path) {
283 this.targetPath = path;
287 public RevisionAwareXPath getWhenCondition() {
288 return whenCondition;
291 private void setWhenCondition(RevisionAwareXPath whenCondition) {
292 this.whenCondition = whenCondition;
296 public Set<DataSchemaNode> getChildNodes() {
297 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
298 result.addAll(childNodes.values());
302 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
303 if (childNodes != null) {
304 this.childNodes = childNodes;
309 * Always returns an empty set, because augment can not contains
310 * grouping statement.
313 public Set<GroupingDefinition> getGroupings() {
314 return Collections.emptySet();
318 public Set<UsesNode> getUses() {
322 private void setUses(Set<UsesNode> uses) {
329 * Always returns an empty set, because augment can not contains type
333 public Set<TypeDefinition<?>> getTypeDefinitions() {
334 return Collections.emptySet();
338 public String getDescription() {
342 private void setDescription(String description) {
343 this.description = description;
347 public String getReference() {
351 private void setReference(String reference) {
352 this.reference = reference;
356 public Status getStatus() {
360 private void setStatus(Status status) {
361 this.status = status;
365 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
369 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
370 if (unknownSchemaNodes != null) {
371 this.unknownNodes = unknownSchemaNodes;
376 public DataSchemaNode getDataChildByName(QName name) {
377 return childNodes.get(name);
381 public DataSchemaNode getDataChildByName(String name) {
382 DataSchemaNode result = null;
383 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
384 if (entry.getKey().getLocalName().equals(name)) {
385 result = entry.getValue();
393 public int hashCode() {
394 final int prime = 17;
396 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
397 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
398 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
403 public boolean equals(Object obj) {
410 if (getClass() != obj.getClass()) {
413 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
414 if (targetPath == null) {
415 if (other.targetPath != null) {
418 } else if (!targetPath.equals(other.targetPath)) {
421 if (whenCondition == null) {
422 if (other.whenCondition != null) {
425 } else if (!whenCondition.equals(other.whenCondition)) {
428 if (childNodes == null) {
429 if (other.childNodes != null) {
432 } else if (!childNodes.equals(other.childNodes)) {
439 public String toString() {
440 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
442 sb.append("targetPath=" + targetPath);
443 sb.append(", when=" + whenCondition);
445 return sb.toString();