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;
33 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
34 private final AugmentationSchemaImpl instance;
35 private final int line;
36 private final String augmentTargetStr;
37 private SchemaPath augmentTarget;
38 private SchemaPath finalAugmentTarget;
39 private String whenCondition;
40 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
41 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
42 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
43 private boolean resolved;
45 AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
46 this.augmentTargetStr = augmentTargetStr;
48 final SchemaPath targetPath = YangModelBuilderUtil
49 .parseAugmentPath(augmentTargetStr);
50 augmentTarget = targetPath;
51 instance = new AugmentationSchemaImpl(targetPath);
55 public int getLine() {
60 public void addChildNode(DataSchemaNodeBuilder childNode) {
61 childNodes.add(childNode);
65 public Set<DataSchemaNodeBuilder> getChildNodes() {
70 public void addGrouping(GroupingBuilder grouping) {
71 groupings.add(grouping);
75 public void addUsesNode(UsesNodeBuilder usesBuilder) {
76 usesNodes.add(usesBuilder);
80 * Always returns null.
83 public QName getQName() {
88 * Always returns null.
91 public SchemaPath getPath() {
96 public AugmentationSchema build() {
97 instance.setTargetPath(finalAugmentTarget);
99 RevisionAwareXPath whenStmt;
100 if (whenCondition == null) {
103 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
105 instance.setWhenCondition(whenStmt);
108 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
109 for (DataSchemaNodeBuilder node : childNodes) {
110 childs.put(node.getQName(), node.build());
112 instance.setChildNodes(childs);
115 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
116 for (GroupingBuilder builder : groupings) {
117 groupingDefinitions.add(builder.build());
119 instance.setGroupings(groupingDefinitions);
122 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
123 for (UsesNodeBuilder builder : usesNodes) {
124 usesNodeDefinitions.add(builder.build());
126 instance.setUses(usesNodeDefinitions);
132 public boolean isResolved() {
137 public void setResolved(boolean resolved) {
138 this.resolved = resolved;
141 public String getWhenCondition() {
142 return whenCondition;
145 public void addWhenCondition(String whenCondition) {
146 this.whenCondition = whenCondition;
150 public void addTypedef(TypeDefinitionBuilder type) {
151 throw new UnsupportedOperationException(
152 "Augmentation can not contains type definitions");
156 public void setDescription(String description) {
157 instance.setDescription(description);
161 public void setReference(String reference) {
162 instance.setReference(reference);
166 public void setStatus(Status status) {
167 instance.setStatus(status);
171 public SchemaPath getTargetPath() {
172 return augmentTarget;
176 public void setTargetPath(SchemaPath path) {
177 this.finalAugmentTarget = path;
181 public String getTargetPathAsString() {
182 return augmentTargetStr;
186 public int hashCode() {
187 final int prime = 17;
189 result = prime * result
190 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
191 result = prime * result
192 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
193 result = prime * result
194 + ((childNodes == null) ? 0 : childNodes.hashCode());
199 public boolean equals(Object obj) {
206 if (getClass() != obj.getClass()) {
209 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
210 if (augmentTargetStr == null) {
211 if (other.augmentTargetStr != null) {
214 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
217 if (whenCondition == null) {
218 if (other.whenCondition != null) {
221 } else if (!whenCondition.equals(other.whenCondition)) {
224 if (childNodes == null) {
225 if (other.childNodes != null) {
228 } else if (!childNodes.equals(other.childNodes)) {
235 private static class AugmentationSchemaImpl implements AugmentationSchema {
236 private SchemaPath targetPath;
237 private RevisionAwareXPath whenCondition;
238 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
239 private Set<GroupingDefinition> groupings = Collections.emptySet();
240 private Set<UsesNode> uses = Collections.emptySet();
242 private String description;
243 private String reference;
244 private Status status;
246 private AugmentationSchemaImpl(SchemaPath targetPath) {
247 this.targetPath = targetPath;
251 public SchemaPath getTargetPath() {
255 private void setTargetPath(SchemaPath path) {
256 this.targetPath = path;
260 public RevisionAwareXPath getWhenCondition() {
261 return whenCondition;
264 private void setWhenCondition(RevisionAwareXPath whenCondition) {
265 this.whenCondition = whenCondition;
269 public Set<DataSchemaNode> getChildNodes() {
270 return new HashSet<DataSchemaNode>(childNodes.values());
273 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
274 if (childNodes != null) {
275 this.childNodes = childNodes;
280 public Set<GroupingDefinition> getGroupings() {
284 private void setGroupings(Set<GroupingDefinition> groupings) {
285 if (groupings != null) {
286 this.groupings = groupings;
291 public Set<UsesNode> getUses() {
295 private void setUses(Set<UsesNode> uses) {
302 * Always returns an empty set, because augmentation can not contains
306 public Set<TypeDefinition<?>> getTypeDefinitions() {
307 return Collections.emptySet();
311 public String getDescription() {
315 private void setDescription(String description) {
316 this.description = description;
320 public String getReference() {
324 private void setReference(String reference) {
325 this.reference = reference;
329 public Status getStatus() {
333 private void setStatus(Status status) {
334 this.status = status;
338 public DataSchemaNode getDataChildByName(QName name) {
339 return childNodes.get(name);
343 public DataSchemaNode getDataChildByName(String name) {
344 DataSchemaNode result = null;
345 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
346 if (entry.getKey().getLocalName().equals(name)) {
347 result = entry.getValue();
355 public int hashCode() {
356 final int prime = 17;
358 result = prime * result
359 + ((targetPath == null) ? 0 : targetPath.hashCode());
360 result = prime * result
361 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
362 result = prime * result
363 + ((childNodes == null) ? 0 : childNodes.hashCode());
368 public boolean equals(Object obj) {
375 if (getClass() != obj.getClass()) {
378 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
379 if (targetPath == null) {
380 if (other.targetPath != null) {
383 } else if (!targetPath.equals(other.targetPath)) {
386 if (whenCondition == null) {
387 if (other.whenCondition != null) {
390 } else if (!whenCondition.equals(other.whenCondition)) {
393 if (childNodes == null) {
394 if (other.childNodes != null) {
397 } else if (!childNodes.equals(other.childNodes)) {
404 public String toString() {
405 StringBuilder sb = new StringBuilder(
406 AugmentationSchemaImpl.class.getSimpleName());
408 sb.append("targetPath=" + targetPath);
409 sb.append(", childNodes=" + childNodes.values());
410 sb.append(", groupings=" + groupings);
411 sb.append(", uses=" + uses);
413 return sb.toString();