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 private static final SchemaPath PATH = new SchemaPath(Collections.<QName> emptyList(), true);
59 public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
60 super(moduleName, line, null);
61 this.augmentTargetStr = augmentTargetStr;
62 final SchemaPath targetPath = ParserUtils.parseXPathString(augmentTargetStr);
63 dirtyAugmentTarget = targetPath;
64 instance = new AugmentationSchemaImpl(targetPath);
68 public Set<GroupingDefinition> getGroupings() {
69 return Collections.emptySet();
73 public Set<GroupingBuilder> getGroupingBuilders() {
74 return Collections.emptySet();
78 public void addGrouping(GroupingBuilder grouping) {
79 throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
83 public Set<UsesNodeBuilder> getUsesNodes() {
88 public void addUsesNode(UsesNodeBuilder usesBuilder) {
89 usesNodes.add(usesBuilder);
93 public SchemaPath getPath() {
98 public AugmentationSchema build() {
100 instance.setDescription(description);
101 instance.setReference(reference);
102 instance.setStatus(status);
103 instance.setTargetPath(finalAugmentTarget);
105 RevisionAwareXPath whenStmt;
106 if (whenCondition == null) {
109 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
111 instance.setWhenCondition(whenStmt);
114 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
115 for (DataSchemaNodeBuilder node : addedChildNodes) {
116 childs.put(node.getQName(), node.build());
118 instance.setChildNodes(childs);
121 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
122 for (UsesNodeBuilder builder : usesNodes) {
123 usesNodeDefinitions.add(builder.build());
125 instance.setUses(usesNodeDefinitions);
128 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
129 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
130 unknownNodes.add(b.build());
132 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
133 instance.setUnknownSchemaNodes(unknownNodes);
141 public boolean isResolved() {
146 public void setResolved(boolean resolved) {
147 this.resolved = resolved;
150 public String getWhenCondition() {
151 return whenCondition;
154 public void addWhenCondition(String whenCondition) {
155 this.whenCondition = whenCondition;
159 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
160 return Collections.emptySet();
164 public void addTypedef(TypeDefinitionBuilder type) {
165 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
169 public String getDescription() {
174 public void setDescription(String description) {
175 this.description = description;
179 public String getReference() {
184 public void setReference(String reference) {
185 this.reference = reference;
189 public Status getStatus() {
194 public void setStatus(Status status) {
195 if (status != null) {
196 this.status = status;
201 public SchemaPath getTargetPath() {
202 return dirtyAugmentTarget;
206 public void setTargetPath(SchemaPath path) {
207 this.finalAugmentTarget = path;
211 public String getTargetPathAsString() {
212 return augmentTargetStr;
216 public int hashCode() {
217 final int prime = 17;
219 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
220 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
221 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
226 public boolean equals(Object obj) {
233 if (getClass() != obj.getClass()) {
236 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
237 if (augmentTargetStr == null) {
238 if (other.augmentTargetStr != null) {
241 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
244 if (whenCondition == null) {
245 if (other.whenCondition != null) {
248 } else if (!whenCondition.equals(other.whenCondition)) {
251 if (childNodes == null) {
252 if (other.childNodes != null) {
255 } else if (!childNodes.equals(other.childNodes)) {
261 public String toString() {
262 return "augment " + augmentTargetStr;
265 private final class AugmentationSchemaImpl implements AugmentationSchema {
266 private SchemaPath targetPath;
267 private RevisionAwareXPath whenCondition;
268 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
269 private Set<UsesNode> uses = Collections.emptySet();
270 private String description;
271 private String reference;
272 private Status status;
273 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
275 private AugmentationSchemaImpl(SchemaPath targetPath) {
276 this.targetPath = targetPath;
280 public SchemaPath getTargetPath() {
284 private void setTargetPath(SchemaPath path) {
285 this.targetPath = path;
289 public RevisionAwareXPath getWhenCondition() {
290 return whenCondition;
293 private void setWhenCondition(RevisionAwareXPath whenCondition) {
294 this.whenCondition = whenCondition;
298 public Set<DataSchemaNode> getChildNodes() {
299 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
300 result.addAll(childNodes.values());
304 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
305 if (childNodes != null) {
306 this.childNodes = childNodes;
311 * Always returns an empty set, because augment can not contains
312 * grouping statement.
315 public Set<GroupingDefinition> getGroupings() {
316 return Collections.emptySet();
320 public Set<UsesNode> getUses() {
324 private void setUses(Set<UsesNode> uses) {
331 * Always returns an empty set, because augment can not contains type
335 public Set<TypeDefinition<?>> getTypeDefinitions() {
336 return Collections.emptySet();
340 public String getDescription() {
344 private void setDescription(String description) {
345 this.description = description;
349 public String getReference() {
353 private void setReference(String reference) {
354 this.reference = reference;
358 public Status getStatus() {
362 private void setStatus(Status status) {
363 this.status = status;
367 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
371 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
372 if (unknownSchemaNodes != null) {
373 this.unknownNodes = unknownSchemaNodes;
378 public DataSchemaNode getDataChildByName(QName name) {
379 return childNodes.get(name);
383 public DataSchemaNode getDataChildByName(String name) {
384 DataSchemaNode result = null;
385 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
386 if (entry.getKey().getLocalName().equals(name)) {
387 result = entry.getValue();
395 public int hashCode() {
396 final int prime = 17;
398 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
399 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
400 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
405 public boolean equals(Object obj) {
412 if (getClass() != obj.getClass()) {
415 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
416 if (targetPath == null) {
417 if (other.targetPath != null) {
420 } else if (!targetPath.equals(other.targetPath)) {
423 if (whenCondition == null) {
424 if (other.whenCondition != null) {
427 } else if (!whenCondition.equals(other.whenCondition)) {
430 if (childNodes == null) {
431 if (other.childNodes != null) {
434 } else if (!childNodes.equals(other.childNodes)) {
441 public String toString() {
442 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
444 sb.append("targetPath=" + targetPath);
445 sb.append(", when=" + whenCondition);
447 return sb.toString();