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 void addTypedef(TypeDefinitionBuilder type) {
156 throw new YangParseException(line,
157 "Augmentation can not contains type definitions");
161 public void setDescription(String description) {
162 instance.setDescription(description);
166 public void setReference(String reference) {
167 instance.setReference(reference);
171 public void setStatus(Status status) {
172 instance.setStatus(status);
176 public SchemaPath getTargetPath() {
177 return augmentTarget;
181 public void setTargetPath(SchemaPath path) {
182 this.finalAugmentTarget = path;
186 public String getTargetPathAsString() {
187 return augmentTargetStr;
191 public int hashCode() {
192 final int prime = 17;
196 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
197 result = prime * result
198 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
199 result = prime * result
200 + ((childNodes == null) ? 0 : childNodes.hashCode());
205 public boolean equals(Object obj) {
212 if (getClass() != obj.getClass()) {
215 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
216 if (augmentTargetStr == null) {
217 if (other.augmentTargetStr != null) {
220 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
223 if (whenCondition == null) {
224 if (other.whenCondition != null) {
227 } else if (!whenCondition.equals(other.whenCondition)) {
230 if (childNodes == null) {
231 if (other.childNodes != null) {
234 } else if (!childNodes.equals(other.childNodes)) {
240 private final class AugmentationSchemaImpl implements AugmentationSchema {
241 private SchemaPath targetPath;
242 private RevisionAwareXPath whenCondition;
243 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
244 private Set<GroupingDefinition> groupings = Collections.emptySet();
245 private Set<UsesNode> uses = Collections.emptySet();
247 private String description;
248 private String reference;
249 private Status status;
251 private AugmentationSchemaImpl(SchemaPath targetPath) {
252 this.targetPath = targetPath;
256 public SchemaPath getTargetPath() {
260 private void setTargetPath(SchemaPath path) {
261 this.targetPath = path;
265 public RevisionAwareXPath getWhenCondition() {
266 return whenCondition;
269 private void setWhenCondition(RevisionAwareXPath whenCondition) {
270 this.whenCondition = whenCondition;
274 public Set<DataSchemaNode> getChildNodes() {
275 return new HashSet<DataSchemaNode>(childNodes.values());
278 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
279 if (childNodes != null) {
280 this.childNodes = childNodes;
285 public Set<GroupingDefinition> getGroupings() {
289 private void setGroupings(Set<GroupingDefinition> groupings) {
290 if (groupings != null) {
291 this.groupings = groupings;
296 public Set<UsesNode> getUses() {
300 private void setUses(Set<UsesNode> uses) {
307 * Always returns an empty set, because augmentation can not contains
311 public Set<TypeDefinition<?>> getTypeDefinitions() {
312 return Collections.emptySet();
316 public String getDescription() {
320 private void setDescription(String description) {
321 this.description = description;
325 public String getReference() {
329 private void setReference(String reference) {
330 this.reference = reference;
334 public Status getStatus() {
338 private void setStatus(Status status) {
339 this.status = status;
343 public DataSchemaNode getDataChildByName(QName name) {
344 return childNodes.get(name);
348 public DataSchemaNode getDataChildByName(String name) {
349 DataSchemaNode result = null;
350 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
351 if (entry.getKey().getLocalName().equals(name)) {
352 result = entry.getValue();
360 public int hashCode() {
361 final int prime = 17;
363 result = prime * result
364 + ((targetPath == null) ? 0 : targetPath.hashCode());
365 result = prime * result
366 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
367 result = prime * result
368 + ((childNodes == null) ? 0 : childNodes.hashCode());
373 public boolean equals(Object obj) {
380 if (getClass() != obj.getClass()) {
383 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
384 if (targetPath == null) {
385 if (other.targetPath != null) {
388 } else if (!targetPath.equals(other.targetPath)) {
391 if (whenCondition == null) {
392 if (other.whenCondition != null) {
395 } else if (!whenCondition.equals(other.whenCondition)) {
398 if (childNodes == null) {
399 if (other.childNodes != null) {
402 } else if (!childNodes.equals(other.childNodes)) {
409 public String toString() {
410 StringBuilder sb = new StringBuilder(
411 AugmentationSchemaImpl.class.getSimpleName());
413 sb.append("targetPath=" + targetPath);
414 sb.append(", childNodes=" + childNodes.values());
415 sb.append(", groupings=" + groupings);
416 sb.append(", uses=" + uses);
418 return sb.toString();