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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
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;
47 private final String augmentTargetStr;
48 private SchemaPath targetPath;
49 private SchemaPath targetNodeSchemaPath;
51 private boolean resolved;
53 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
54 super(moduleName, line, null);
55 this.augmentTargetStr = augmentTargetStr;
56 targetPath = ParserUtils.parseXPathString(augmentTargetStr);
57 instance = new AugmentationSchemaImpl(targetPath);
61 public Set<GroupingDefinition> getGroupings() {
62 return Collections.emptySet();
66 public Set<GroupingBuilder> getGroupingBuilders() {
67 return Collections.emptySet();
71 public void addGrouping(GroupingBuilder grouping) {
72 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
76 public SchemaPath getPath() {
77 return targetNodeSchemaPath;
81 public AugmentationSchema build() {
83 instance.setTargetPath(targetNodeSchemaPath);
85 RevisionAwareXPath whenStmt;
86 if (whenCondition == null) {
89 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
91 instance.setWhenCondition(whenStmt);
94 for (DataSchemaNodeBuilder node : addedChildNodes) {
95 DataSchemaNode child = node.build();
96 childNodes.put(child.getQName(), child);
98 instance.addChildNodes(childNodes);
101 for (UsesNodeBuilder builder : addedUsesNodes) {
102 usesNodes.add(builder.build());
104 instance.addUses(usesNodes);
107 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
108 unknownNodes.add(b.build());
110 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
111 instance.addUnknownSchemaNodes(unknownNodes);
119 public boolean isResolved() {
124 public void setResolved(boolean resolved) {
125 this.resolved = resolved;
128 public String getWhenCondition() {
129 return whenCondition;
132 public void addWhenCondition(String whenCondition) {
133 this.whenCondition = whenCondition;
137 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
138 return Collections.emptySet();
142 public void addTypedef(TypeDefinitionBuilder type) {
143 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
147 public String getDescription() {
148 return instance.description;
152 public void setDescription(final String description) {
153 instance.description = description;
157 public String getReference() {
158 return instance.reference;
162 public void setReference(final String reference) {
163 instance.reference = reference;
167 public Status getStatus() {
168 return instance.status;
172 public void setStatus(Status status) {
173 if (status != null) {
174 instance.status = status;
179 public String getTargetPathAsString() {
180 return augmentTargetStr;
184 public SchemaPath getTargetPath() {
189 public SchemaPath getTargetNodeSchemaPath() {
190 return targetNodeSchemaPath;
194 public void setTargetNodeSchemaPath(SchemaPath path) {
195 this.targetNodeSchemaPath = path;
199 public int hashCode() {
200 final int prime = 17;
202 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
203 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
204 result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
209 public boolean equals(Object obj) {
216 if (getClass() != obj.getClass()) {
219 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
220 if (augmentTargetStr == null) {
221 if (other.augmentTargetStr != null) {
224 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
227 if (whenCondition == null) {
228 if (other.whenCondition != null) {
231 } else if (!whenCondition.equals(other.whenCondition)) {
234 if (addedChildNodes == null) {
235 if (other.addedChildNodes != null) {
238 } else if (!addedChildNodes.equals(other.addedChildNodes)) {
244 public String toString() {
245 return "augment " + augmentTargetStr;
248 private final class AugmentationSchemaImpl implements AugmentationSchema {
249 private SchemaPath targetPath;
250 private RevisionAwareXPath whenCondition;
251 private final Map<QName, DataSchemaNode> childNodes = new HashMap<>();
252 private final Set<UsesNode> uses = new HashSet<>();
253 private String description;
254 private String reference;
255 private Status status;
256 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
258 private AugmentationSchemaImpl(SchemaPath targetPath) {
259 this.targetPath = targetPath;
263 public SchemaPath getTargetPath() {
267 private void setTargetPath(SchemaPath path) {
268 this.targetPath = path;
272 public RevisionAwareXPath getWhenCondition() {
273 return whenCondition;
276 private void setWhenCondition(RevisionAwareXPath whenCondition) {
277 this.whenCondition = whenCondition;
281 public Set<DataSchemaNode> getChildNodes() {
282 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
283 result.addAll(childNodes.values());
284 return Collections.unmodifiableSet(result);
287 private void addChildNodes(Map<QName, DataSchemaNode> childNodes) {
288 if (childNodes != null) {
289 this.childNodes.putAll(childNodes);
294 * Always returns an empty set, because augment can not contains
295 * grouping statement.
298 public Set<GroupingDefinition> getGroupings() {
299 return Collections.emptySet();
303 public Set<UsesNode> getUses() {
304 return Collections.unmodifiableSet(uses);
307 private void addUses(Set<UsesNode> uses) {
309 this.uses.addAll(uses);
314 * Always returns an empty set, because augment can not contains type
318 public Set<TypeDefinition<?>> getTypeDefinitions() {
319 return Collections.emptySet();
323 public String getDescription() {
328 public String getReference() {
333 public Status getStatus() {
338 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
339 return Collections.unmodifiableList(unknownNodes);
342 private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
343 if (unknownSchemaNodes != null) {
344 this.unknownNodes.addAll(unknownSchemaNodes);
349 public DataSchemaNode getDataChildByName(QName name) {
350 return childNodes.get(name);
354 public DataSchemaNode getDataChildByName(String name) {
355 DataSchemaNode result = null;
356 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
357 if (entry.getKey().getLocalName().equals(name)) {
358 result = entry.getValue();
366 public int hashCode() {
367 final int prime = 17;
369 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
370 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
371 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
376 public boolean equals(Object obj) {
383 if (getClass() != obj.getClass()) {
386 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
387 if (targetPath == null) {
388 if (other.targetPath != null) {
391 } else if (!targetPath.equals(other.targetPath)) {
394 if (whenCondition == null) {
395 if (other.whenCondition != null) {
398 } else if (!whenCondition.equals(other.whenCondition)) {
401 if (childNodes == null) {
402 if (other.childNodes != null) {
405 } else if (!childNodes.equals(other.childNodes)) {
412 public String toString() {
413 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
415 sb.append("targetPath=" + targetPath);
416 sb.append(", when=" + whenCondition);
418 return sb.toString();