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.controller.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.controller.yang.common.QName;
20 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
21 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
22 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
23 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.controller.yang.model.api.SchemaPath;
25 import org.opendaylight.controller.yang.model.api.Status;
26 import org.opendaylight.controller.yang.model.api.TypeDefinition;
27 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.controller.yang.model.api.UsesNode;
29 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
30 import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
36 import org.opendaylight.controller.yang.parser.util.Comparators;
37 import org.opendaylight.controller.yang.parser.util.ParserListenerUtils;
38 import org.opendaylight.controller.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 AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
58 super(moduleName, line, null);
59 this.augmentTargetStr = augmentTargetStr;
60 final SchemaPath targetPath = ParserListenerUtils.parseAugmentPath(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 void addUsesNode(UsesNodeBuilder usesBuilder) {
82 usesNodes.add(usesBuilder);
86 * Always returns null.
89 public SchemaPath getPath() {
94 public AugmentationSchema build() {
96 instance.setDescription(description);
97 instance.setReference(reference);
98 instance.setStatus(status);
99 instance.setTargetPath(finalAugmentTarget);
101 RevisionAwareXPath whenStmt;
102 if (whenCondition == null) {
105 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
107 instance.setWhenCondition(whenStmt);
110 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
111 for (DataSchemaNodeBuilder node : addedChildNodes) {
112 childs.put(node.getQName(), node.build());
114 instance.setChildNodes(childs);
117 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
118 for (UsesNodeBuilder builder : usesNodes) {
119 usesNodeDefinitions.add(builder.build());
121 instance.setUses(usesNodeDefinitions);
124 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
125 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
126 unknownNodes.add(b.build());
128 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
129 instance.setUnknownSchemaNodes(unknownNodes);
137 public boolean isResolved() {
142 public void setResolved(boolean resolved) {
143 this.resolved = resolved;
146 public String getWhenCondition() {
147 return whenCondition;
150 public void addWhenCondition(String whenCondition) {
151 this.whenCondition = whenCondition;
155 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
156 return Collections.emptySet();
160 public void addTypedef(TypeDefinitionBuilder type) {
161 throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
165 public void setDescription(String description) {
166 this.description = description;
170 public void setReference(String reference) {
171 this.reference = reference;
175 public void setStatus(Status status) {
176 if (status != null) {
177 this.status = status;
182 public SchemaPath getTargetPath() {
183 return dirtyAugmentTarget;
187 public void setTargetPath(SchemaPath path) {
188 this.finalAugmentTarget = path;
192 public String getTargetPathAsString() {
193 return augmentTargetStr;
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 + ((childNodes == null) ? 0 : childNodes.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 (childNodes == null) {
233 if (other.childNodes != null) {
236 } else if (!childNodes.equals(other.childNodes)) {
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 Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
250 private Set<UsesNode> uses = Collections.emptySet();
251 private String description;
252 private String reference;
253 private Status status;
254 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
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 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
281 result.addAll(childNodes.values());
285 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
286 if (childNodes != null) {
287 this.childNodes = childNodes;
292 * Always returns an empty set, because augment can not contains
293 * grouping statement.
296 public Set<GroupingDefinition> getGroupings() {
297 return Collections.emptySet();
301 public Set<UsesNode> getUses() {
305 private void setUses(Set<UsesNode> uses) {
312 * Always returns an empty set, because augment can not contains type
316 public Set<TypeDefinition<?>> getTypeDefinitions() {
317 return Collections.emptySet();
321 public String getDescription() {
325 private void setDescription(String description) {
326 this.description = description;
330 public String getReference() {
334 private void setReference(String reference) {
335 this.reference = reference;
339 public Status getStatus() {
343 private void setStatus(Status status) {
344 this.status = status;
348 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
352 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
353 if (unknownSchemaNodes != null) {
354 this.unknownNodes = unknownSchemaNodes;
359 public DataSchemaNode getDataChildByName(QName name) {
360 return childNodes.get(name);
364 public DataSchemaNode getDataChildByName(String name) {
365 DataSchemaNode result = null;
366 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
367 if (entry.getKey().getLocalName().equals(name)) {
368 result = entry.getValue();
376 public int hashCode() {
377 final int prime = 17;
379 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
380 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
381 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
386 public boolean equals(Object obj) {
393 if (getClass() != obj.getClass()) {
396 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
397 if (targetPath == null) {
398 if (other.targetPath != null) {
401 } else if (!targetPath.equals(other.targetPath)) {
404 if (whenCondition == null) {
405 if (other.whenCondition != null) {
408 } else if (!whenCondition.equals(other.whenCondition)) {
411 if (childNodes == null) {
412 if (other.childNodes != null) {
415 } else if (!childNodes.equals(other.childNodes)) {
422 public String toString() {
423 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
425 sb.append("targetPath=" + targetPath);
426 sb.append(", when=" + whenCondition);
428 return sb.toString();