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 final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
56 private boolean resolved;
58 AugmentationSchemaBuilderImpl(final int line, final String augmentTargetStr) {
60 this.augmentTargetStr = augmentTargetStr;
61 final SchemaPath targetPath = ParserListenerUtils.parseAugmentPath(augmentTargetStr);
62 dirtyAugmentTarget = targetPath;
63 instance = new AugmentationSchemaImpl(targetPath);
67 public Set<GroupingDefinition> getGroupings() {
68 return Collections.emptySet();
72 public Set<GroupingBuilder> getGroupingBuilders() {
73 return Collections.emptySet();
77 public void addGrouping(GroupingBuilder grouping) {
78 throw new YangParseException(line, "augment can not contains grouping statement");
82 public void addUsesNode(UsesNodeBuilder usesBuilder) {
83 usesNodes.add(usesBuilder);
87 * Always returns null.
90 public SchemaPath getPath() {
95 public AugmentationSchema build() {
97 instance.setDescription(description);
98 instance.setReference(reference);
99 instance.setStatus(status);
100 instance.setTargetPath(finalAugmentTarget);
102 RevisionAwareXPath whenStmt;
103 if (whenCondition == null) {
106 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
108 instance.setWhenCondition(whenStmt);
111 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
112 for (DataSchemaNodeBuilder node : addedChildNodes) {
113 childs.put(node.getQName(), node.build());
115 instance.setChildNodes(childs);
118 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
119 for (UsesNodeBuilder builder : usesNodes) {
120 usesNodeDefinitions.add(builder.build());
122 instance.setUses(usesNodeDefinitions);
125 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
126 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
127 unknownNodes.add(b.build());
129 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
130 instance.setUnknownSchemaNodes(unknownNodes);
138 public boolean isResolved() {
143 public void setResolved(boolean resolved) {
144 this.resolved = resolved;
147 public String getWhenCondition() {
148 return whenCondition;
151 public void addWhenCondition(String whenCondition) {
152 this.whenCondition = whenCondition;
156 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
157 return Collections.emptySet();
161 public void addTypedef(TypeDefinitionBuilder type) {
162 throw new YangParseException(line, "Augmentation can not contains typedef statement.");
166 public void setDescription(String description) {
167 this.description = description;
171 public void setReference(String reference) {
172 this.reference = reference;
176 public void setStatus(Status status) {
177 if (status != null) {
178 this.status = status;
183 public SchemaPath getTargetPath() {
184 return dirtyAugmentTarget;
188 public void setTargetPath(SchemaPath path) {
189 this.finalAugmentTarget = path;
193 public String getTargetPathAsString() {
194 return augmentTargetStr;
197 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
198 return addedUnknownNodes;
202 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
203 addedUnknownNodes.add(unknownNode);
207 public int hashCode() {
208 final int prime = 17;
210 result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
211 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
212 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
217 public boolean equals(Object obj) {
224 if (getClass() != obj.getClass()) {
227 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
228 if (augmentTargetStr == null) {
229 if (other.augmentTargetStr != null) {
232 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
235 if (whenCondition == null) {
236 if (other.whenCondition != null) {
239 } else if (!whenCondition.equals(other.whenCondition)) {
242 if (childNodes == null) {
243 if (other.childNodes != null) {
246 } else if (!childNodes.equals(other.childNodes)) {
252 public String toString() {
253 return "augment " + augmentTargetStr;
256 private final class AugmentationSchemaImpl implements AugmentationSchema {
257 private SchemaPath targetPath;
258 private RevisionAwareXPath whenCondition;
259 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
260 private Set<UsesNode> uses = Collections.emptySet();
261 private String description;
262 private String reference;
263 private Status status;
264 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
266 private AugmentationSchemaImpl(SchemaPath targetPath) {
267 this.targetPath = targetPath;
271 public SchemaPath getTargetPath() {
275 private void setTargetPath(SchemaPath path) {
276 this.targetPath = path;
280 public RevisionAwareXPath getWhenCondition() {
281 return whenCondition;
284 private void setWhenCondition(RevisionAwareXPath whenCondition) {
285 this.whenCondition = whenCondition;
289 public Set<DataSchemaNode> getChildNodes() {
290 final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
291 result.addAll(childNodes.values());
295 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
296 if (childNodes != null) {
297 this.childNodes = childNodes;
302 * Always returns an empty set, because augment can not contains
303 * grouping statement.
306 public Set<GroupingDefinition> getGroupings() {
307 return Collections.emptySet();
311 public Set<UsesNode> getUses() {
315 private void setUses(Set<UsesNode> uses) {
322 * Always returns an empty set, because augment can not contains type
326 public Set<TypeDefinition<?>> getTypeDefinitions() {
327 return Collections.emptySet();
331 public String getDescription() {
335 private void setDescription(String description) {
336 this.description = description;
340 public String getReference() {
344 private void setReference(String reference) {
345 this.reference = reference;
349 public Status getStatus() {
353 private void setStatus(Status status) {
354 this.status = status;
358 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
362 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
363 if (unknownSchemaNodes != null) {
364 this.unknownNodes = unknownSchemaNodes;
369 public DataSchemaNode getDataChildByName(QName name) {
370 return childNodes.get(name);
374 public DataSchemaNode getDataChildByName(String name) {
375 DataSchemaNode result = null;
376 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
377 if (entry.getKey().getLocalName().equals(name)) {
378 result = entry.getValue();
386 public int hashCode() {
387 final int prime = 17;
389 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
390 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
391 result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
396 public boolean equals(Object obj) {
403 if (getClass() != obj.getClass()) {
406 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
407 if (targetPath == null) {
408 if (other.targetPath != null) {
411 } else if (!targetPath.equals(other.targetPath)) {
414 if (whenCondition == null) {
415 if (other.whenCondition != null) {
418 } else if (!whenCondition.equals(other.whenCondition)) {
421 if (childNodes == null) {
422 if (other.childNodes != null) {
425 } else if (!childNodes.equals(other.childNodes)) {
432 public String toString() {
433 StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
435 sb.append("targetPath=" + targetPath);
436 sb.append(", when=" + whenCondition);
438 return sb.toString();