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.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
32 import org.opendaylight.controller.yang.parser.util.YangParseException;
34 public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
35 private boolean built;
36 private final AugmentationSchemaImpl instance;
37 private final int line;
38 private final String augmentTargetStr;
39 private SchemaPath augmentTarget;
40 private SchemaPath finalAugmentTarget;
41 private String whenCondition;
42 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
43 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
44 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
45 private boolean resolved;
47 AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
48 this.augmentTargetStr = augmentTargetStr;
50 final SchemaPath targetPath = YangModelBuilderUtil
51 .parseAugmentPath(augmentTargetStr);
52 augmentTarget = targetPath;
53 instance = new AugmentationSchemaImpl(targetPath);
57 public int getLine() {
62 public void addChildNode(DataSchemaNodeBuilder childNode) {
63 childNodes.add(childNode);
67 public Set<DataSchemaNodeBuilder> getChildNodes() {
72 public void addGrouping(GroupingBuilder grouping) {
73 groupings.add(grouping);
77 public void addUsesNode(UsesNodeBuilder usesBuilder) {
78 usesNodes.add(usesBuilder);
82 * Always returns null.
85 public QName getQName() {
90 * Always returns null.
93 public SchemaPath getPath() {
98 public AugmentationSchema build() {
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 HashMap<QName, DataSchemaNode>();
112 for (DataSchemaNodeBuilder node : childNodes) {
113 childs.put(node.getQName(), node.build());
115 instance.setChildNodes(childs);
118 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
119 for (GroupingBuilder builder : groupings) {
120 groupingDefinitions.add(builder.build());
122 instance.setGroupings(groupingDefinitions);
125 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
126 for (UsesNodeBuilder builder : usesNodes) {
127 usesNodeDefinitions.add(builder.build());
129 instance.setUses(usesNodeDefinitions);
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> getTypeDefinitions() {
156 return Collections.emptySet();
160 public void addTypedef(TypeDefinitionBuilder type) {
161 throw new YangParseException(line,
162 "Augmentation can not contains type definitions");
166 public void setDescription(String description) {
167 instance.setDescription(description);
171 public void setReference(String reference) {
172 instance.setReference(reference);
176 public void setStatus(Status status) {
177 instance.setStatus(status);
181 public SchemaPath getTargetPath() {
182 return augmentTarget;
186 public void setTargetPath(SchemaPath path) {
187 this.finalAugmentTarget = path;
191 public String getTargetPathAsString() {
192 return augmentTargetStr;
196 public int hashCode() {
197 final int prime = 17;
201 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
202 result = prime * result
203 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
204 result = prime * result
205 + ((childNodes == null) ? 0 : childNodes.hashCode());
210 public boolean equals(Object obj) {
217 if (getClass() != obj.getClass()) {
220 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
221 if (augmentTargetStr == null) {
222 if (other.augmentTargetStr != null) {
225 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
228 if (whenCondition == null) {
229 if (other.whenCondition != null) {
232 } else if (!whenCondition.equals(other.whenCondition)) {
235 if (childNodes == null) {
236 if (other.childNodes != null) {
239 } else if (!childNodes.equals(other.childNodes)) {
245 private final class AugmentationSchemaImpl implements AugmentationSchema {
246 private SchemaPath targetPath;
247 private RevisionAwareXPath whenCondition;
248 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
249 private Set<GroupingDefinition> groupings = Collections.emptySet();
250 private Set<UsesNode> uses = Collections.emptySet();
252 private String description;
253 private String reference;
254 private Status status;
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 new HashSet<DataSchemaNode>(childNodes.values());
283 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
284 if (childNodes != null) {
285 this.childNodes = childNodes;
290 public Set<GroupingDefinition> getGroupings() {
294 private void setGroupings(Set<GroupingDefinition> groupings) {
295 if (groupings != null) {
296 this.groupings = groupings;
301 public Set<UsesNode> getUses() {
305 private void setUses(Set<UsesNode> uses) {
312 * Always returns an empty set, because augmentation can not contains
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 DataSchemaNode getDataChildByName(QName name) {
349 return childNodes.get(name);
353 public DataSchemaNode getDataChildByName(String name) {
354 DataSchemaNode result = null;
355 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
356 if (entry.getKey().getLocalName().equals(name)) {
357 result = entry.getValue();
365 public int hashCode() {
366 final int prime = 17;
368 result = prime * result
369 + ((targetPath == null) ? 0 : targetPath.hashCode());
370 result = prime * result
371 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
372 result = prime * result
373 + ((childNodes == null) ? 0 : childNodes.hashCode());
378 public boolean equals(Object obj) {
385 if (getClass() != obj.getClass()) {
388 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
389 if (targetPath == null) {
390 if (other.targetPath != null) {
393 } else if (!targetPath.equals(other.targetPath)) {
396 if (whenCondition == null) {
397 if (other.whenCondition != null) {
400 } else if (!whenCondition.equals(other.whenCondition)) {
403 if (childNodes == null) {
404 if (other.childNodes != null) {
407 } else if (!childNodes.equals(other.childNodes)) {
414 public String toString() {
415 StringBuilder sb = new StringBuilder(
416 AugmentationSchemaImpl.class.getSimpleName());
418 sb.append("targetPath=" + targetPath);
419 sb.append(", childNodes=" + childNodes.values());
420 sb.append(", groupings=" + groupings);
421 sb.append(", uses=" + uses);
423 return sb.toString();