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;
15 import java.util.TreeSet;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
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;
45 private final String augmentTargetStr;
46 private SchemaPath targetPath;
47 private SchemaPath targetNodeSchemaPath;
49 private boolean resolved;
51 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
52 super(moduleName, line, null);
53 this.augmentTargetStr = augmentTargetStr;
54 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
55 instance = new AugmentationSchemaImpl(targetPath);
59 public Set<GroupingDefinition> getGroupings() {
60 return Collections.emptySet();
64 public Set<GroupingBuilder> getGroupingBuilders() {
65 return Collections.emptySet();
69 public void addGrouping(GroupingBuilder grouping) {
70 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
74 public SchemaPath getPath() {
75 return targetNodeSchemaPath;
79 public AugmentationSchema build() {
81 instance.setTargetPath(targetNodeSchemaPath);
83 RevisionAwareXPath whenStmt;
84 if (whenCondition == null) {
87 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
89 instance.setWhenCondition(whenStmt);
92 for (DataSchemaNodeBuilder node : addedChildNodes) {
93 DataSchemaNode child = node.build();
94 childNodes.add(child);
96 instance.addChildNodes(childNodes);
99 for (UsesNodeBuilder builder : addedUsesNodes) {
100 usesNodes.add(builder.build());
102 instance.addUses(usesNodes);
105 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
106 unknownNodes.add(b.build());
108 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
109 instance.addUnknownSchemaNodes(unknownNodes);
117 public boolean isResolved() {
122 public void setResolved(boolean resolved) {
123 this.resolved = resolved;
126 public String getWhenCondition() {
127 return whenCondition;
130 public void addWhenCondition(String whenCondition) {
131 this.whenCondition = whenCondition;
135 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
136 return Collections.emptySet();
140 public void addTypedef(TypeDefinitionBuilder type) {
141 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
145 public String getDescription() {
146 return instance.description;
150 public void setDescription(final String description) {
151 instance.description = description;
155 public String getReference() {
156 return instance.reference;
160 public void setReference(final String reference) {
161 instance.reference = reference;
165 public Status getStatus() {
166 return instance.status;
170 public void setStatus(Status status) {
171 if (status != null) {
172 instance.status = status;
177 public String getTargetPathAsString() {
178 return augmentTargetStr;
182 public SchemaPath getTargetPath() {
187 public SchemaPath getTargetNodeSchemaPath() {
188 return targetNodeSchemaPath;
192 public void setTargetNodeSchemaPath(SchemaPath path) {
193 this.targetNodeSchemaPath = path;
197 public int hashCode() {
198 final int prime = 17;
200 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
201 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
202 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
207 public boolean equals(Object obj) {
214 if (getClass() != obj.getClass()) {
217 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
218 if (augmentTargetStr == null) {
219 if (other.augmentTargetStr != null) {
222 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
225 if (whenCondition == null) {
226 if (other.whenCondition != null) {
229 } else if (!whenCondition.equals(other.whenCondition)) {
232 if (addedChildNodes == null) {
233 if (other.addedChildNodes != null) {
236 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
242 public String toString() {
243 return "augment " + augmentTargetStr;
246 private final class AugmentationSchemaImpl implements AugmentationSchema {
247 private SchemaPath targetPath;
248 private RevisionAwareXPath whenCondition;
249 private final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
250 private final Set<UsesNode> uses = new HashSet<>();
251 private String description;
252 private String reference;
253 private Status status;
254 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
256 private AugmentationSchemaImpl(SchemaPath targetPath) {
257 this.targetPath = targetPath;
261 public SchemaPath getTargetPath() {
265 private void setTargetPath(SchemaPath path) {
266 this.targetPath = path;
270 public RevisionAwareXPath getWhenCondition() {
271 return whenCondition;
274 private void setWhenCondition(RevisionAwareXPath whenCondition) {
275 this.whenCondition = whenCondition;
279 public Set<DataSchemaNode> getChildNodes() {
280 return Collections.unmodifiableSet(childNodes);
283 private void addChildNodes(Set<DataSchemaNode> childNodes) {
284 if (childNodes != null) {
285 this.childNodes.addAll(childNodes);
290 * Always returns an empty set, because augment can not contains
291 * grouping statement.
294 public Set<GroupingDefinition> getGroupings() {
295 return Collections.emptySet();
299 public Set<UsesNode> getUses() {
300 return Collections.unmodifiableSet(uses);
303 private void addUses(Set<UsesNode> uses) {
305 this.uses.addAll(uses);
310 * Always returns an empty set, because augment can not contains type
314 public Set<TypeDefinition<?>> getTypeDefinitions() {
315 return Collections.emptySet();
319 public String getDescription() {
324 public String getReference() {
329 public Status getStatus() {
334 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
335 return Collections.unmodifiableList(unknownNodes);
338 private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
339 if (unknownSchemaNodes != null) {
340 this.unknownNodes.addAll(unknownSchemaNodes);
345 public DataSchemaNode getDataChildByName(QName name) {
346 return getChildNode(childNodes, name);
350 public DataSchemaNode getDataChildByName(String name) {
351 return getChildNode(childNodes, name);
355 public int hashCode() {
356 final int prime = 17;
358 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
359 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
360 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
365 public boolean equals(Object obj) {
372 if (getClass() != obj.getClass()) {
375 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
376 if (targetPath == null) {
377 if (other.targetPath != null) {
380 } else if (!targetPath.equals(other.targetPath)) {
383 if (whenCondition == null) {
384 if (other.whenCondition != null) {
387 } else if (!whenCondition.equals(other.whenCondition)) {
390 if (childNodes == null) {
391 if (other.childNodes != null) {
394 } else if (!childNodes.equals(other.childNodes)) {
401 public String toString() {
402 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
404 sb.append("targetPath=" + targetPath);
405 sb.append(", when=" + whenCondition);
407 return sb.toString();