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.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.TreeMap;
17 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.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
36 import org.opendaylight.yangtools.yang.parser.util.Comparators;
37 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
38 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
40 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
41 AugmentationSchemaBuilder {
42 private boolean built;
43 private final AugmentationSchemaImpl instance;
45 private String whenCondition;
46 private String description;
47 private String reference;
48 private Status status = Status.CURRENT;
50 private final String augmentTargetStr;
51 private SchemaPath dirtyAugmentTarget;
52 private SchemaPath finalAugmentTarget;
54 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
55 private boolean resolved;
57 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
58 super(moduleName, line, null);
59 this.augmentTargetStr = augmentTargetStr;
60 final SchemaPath targetPath = ParserUtils.parseXPathString(augmentTargetStr);
61 dirtyAugmentTarget = targetPath;
62 instance = new AugmentationSchemaImpl(targetPath);
66 public Set<GroupingDefinition> getGroupings() {
67 return Collections.emptySet();
71 public Set<GroupingBuilder> getGroupingBuilders() {
72 return Collections.emptySet();
76 public void addGrouping(GroupingBuilder grouping) {
77 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
81 public Set<UsesNodeBuilder> getUsesNodes() {
86 public void addUsesNode(UsesNodeBuilder usesBuilder) {
87 usesNodes.add(usesBuilder);
91 public SchemaPath getPath() {
92 return finalAugmentTarget;
96 public AugmentationSchema build() {
98 instance.setDescription(description);
99 instance.setReference(reference);
100 instance.setStatus(status);
101 instance.setTargetPath(finalAugmentTarget);
103 RevisionAwareXPath whenStmt;
104 if (whenCondition == null) {
107 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
109 instance.setWhenCondition(whenStmt);
112 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
113 for (DataSchemaNodeBuilder node : addedChildNodes) {
114 childs.put(node.getQName(), node.build());
116 instance.setChildNodes(childs);
119 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
120 for (UsesNodeBuilder builder : usesNodes) {
121 usesNodeDefinitions.add(builder.build());
123 instance.setUses(usesNodeDefinitions);
126 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
127 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
128 unknownNodes.add(b.build());
130 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
131 instance.setUnknownSchemaNodes(unknownNodes);
139 public boolean isResolved() {
144 public void setResolved(boolean resolved) {
145 this.resolved = resolved;
148 public String getWhenCondition() {
149 return whenCondition;
152 public void addWhenCondition(String whenCondition) {
153 this.whenCondition = whenCondition;
157 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
158 return Collections.emptySet();
162 public void addTypedef(TypeDefinitionBuilder type) {
163 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
167 public String getDescription() {
172 public void setDescription(String description) {
173 this.description = description;
177 public String getReference() {
182 public void setReference(String reference) {
183 this.reference = reference;
187 public Status getStatus() {
192 public void setStatus(Status status) {
193 if (status != null) {
194 this.status = status;
199 public SchemaPath getTargetPath() {
200 if(finalAugmentTarget == null) {
201 return dirtyAugmentTarget;
203 return finalAugmentTarget;
208 public void setTargetPath(SchemaPath path) {
209 this.finalAugmentTarget = path;
213 public String getTargetPathAsString() {
214 return augmentTargetStr;
218 public int hashCode() {
219 final int prime = 17;
221 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
222 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
223 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
228 public boolean equals(Object obj) {
235 if (getClass() != obj.getClass()) {
238 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
239 if (augmentTargetStr == null) {
240 if (other.augmentTargetStr != null) {
243 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
246 if (whenCondition == null) {
247 if (other.whenCondition != null) {
250 } else if (!whenCondition.equals(other.whenCondition)) {
253 if (childNodes == null) {
254 if (other.childNodes != null) {
257 } else if (!childNodes.equals(other.childNodes)) {
263 public String toString() {
264 return "augment " + augmentTargetStr;
267 private final class AugmentationSchemaImpl implements AugmentationSchema {
268 private SchemaPath targetPath;
269 private RevisionAwareXPath whenCondition;
270 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
271 private Set<UsesNode> uses = Collections.emptySet();
272 private String description;
273 private String reference;
274 private Status status;
275 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
277 private AugmentationSchemaImpl(SchemaPath targetPath) {
278 this.targetPath = targetPath;
282 public SchemaPath getTargetPath() {
286 private void setTargetPath(SchemaPath path) {
287 this.targetPath = path;
291 public RevisionAwareXPath getWhenCondition() {
292 return whenCondition;
295 private void setWhenCondition(RevisionAwareXPath whenCondition) {
296 this.whenCondition = whenCondition;
300 public Set<DataSchemaNode> getChildNodes() {
301 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
302 result.addAll(childNodes.values());
306 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
307 if (childNodes != null) {
308 this.childNodes = childNodes;
313 * Always returns an empty set, because augment can not contains
314 * grouping statement.
317 public Set<GroupingDefinition> getGroupings() {
318 return Collections.emptySet();
322 public Set<UsesNode> getUses() {
326 private void setUses(Set<UsesNode> uses) {
333 * Always returns an empty set, because augment can not contains type
337 public Set<TypeDefinition<?>> getTypeDefinitions() {
338 return Collections.emptySet();
342 public String getDescription() {
346 private void setDescription(String description) {
347 this.description = description;
351 public String getReference() {
355 private void setReference(String reference) {
356 this.reference = reference;
360 public Status getStatus() {
364 private void setStatus(Status status) {
365 this.status = status;
369 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
373 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
374 if (unknownSchemaNodes != null) {
375 this.unknownNodes = unknownSchemaNodes;
380 public DataSchemaNode getDataChildByName(QName name) {
381 return childNodes.get(name);
385 public DataSchemaNode getDataChildByName(String name) {
386 DataSchemaNode result = null;
387 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
388 if (entry.getKey().getLocalName().equals(name)) {
389 result = entry.getValue();
397 public int hashCode() {
398 final int prime = 17;
400 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
401 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
402 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
407 public boolean equals(Object obj) {
414 if (getClass() != obj.getClass()) {
417 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
418 if (targetPath == null) {
419 if (other.targetPath != null) {
422 } else if (!targetPath.equals(other.targetPath)) {
425 if (whenCondition == null) {
426 if (other.whenCondition != null) {
429 } else if (!whenCondition.equals(other.whenCondition)) {
432 if (childNodes == null) {
433 if (other.childNodes != null) {
436 } else if (!childNodes.equals(other.childNodes)) {
443 public String toString() {
444 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
446 sb.append("targetPath=" + targetPath);
447 sb.append(", when=" + whenCondition);
449 return sb.toString();