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.model.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.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
30 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
31 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
33 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
35 private final AugmentationSchemaImpl instance;
36 private final String augmentTargetStr;
37 private final SchemaPath augmentTarget;
38 private String whenCondition;
39 private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
40 private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
41 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
43 AugmentationSchemaBuilderImpl(final String augmentTargetStr) {
44 this.augmentTargetStr = augmentTargetStr;
45 final SchemaPath targetPath = YangModelBuilderUtil
46 .parseAugmentPath(augmentTargetStr);
47 augmentTarget = targetPath;
48 instance = new AugmentationSchemaImpl(targetPath);
52 public void addChildNode(DataSchemaNodeBuilder childNode) {
53 childNodes.add(childNode);
57 public Set<DataSchemaNodeBuilder> getChildNodes() {
62 public void addGrouping(GroupingBuilder grouping) {
63 groupings.add(grouping);
67 public void addUsesNode(UsesNodeBuilder usesBuilder) {
68 usesNodes.add(usesBuilder);
72 * Always returns null.
75 public QName getQName() {
80 public AugmentationSchema build() {
81 RevisionAwareXPath whenStmt;
82 if (whenCondition == null) {
85 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
87 instance.setWhenCondition(whenStmt);
90 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
91 for (DataSchemaNodeBuilder node : childNodes) {
92 childs.put(node.getQName(), node.build());
94 instance.setChildNodes(childs);
97 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
98 for (GroupingBuilder builder : groupings) {
99 groupingDefinitions.add(builder.build());
101 instance.setGroupings(groupingDefinitions);
104 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
105 for (UsesNodeBuilder builder : usesNodes) {
106 usesNodeDefinitions.add(builder.build());
108 instance.setUses(usesNodeDefinitions);
113 public String getWhenCondition() {
114 return whenCondition;
117 public void addWhenCondition(String whenCondition) {
118 this.whenCondition = whenCondition;
122 public void addTypedef(TypeDefinitionBuilder type) {
123 throw new UnsupportedOperationException(
124 "Augmentation can not contains type definitions");
128 public void setDescription(String description) {
129 instance.setDescription(description);
133 public void setReference(String reference) {
134 instance.setReference(reference);
138 public void setStatus(Status status) {
139 instance.setStatus(status);
143 public SchemaPath getTargetPath() {
144 return augmentTarget;
148 public String getTargetPathAsString() {
149 return augmentTargetStr;
153 public int hashCode() {
154 final int prime = 17;
156 result = prime * result
157 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
158 result = prime * result
159 + ((childNodes == null) ? 0 : childNodes.hashCode());
160 result = prime * result
161 + ((groupings == null) ? 0 : groupings.hashCode());
162 result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
163 result = prime * result
164 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
169 public boolean equals(Object obj) {
176 if (getClass() != obj.getClass()) {
179 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
180 if (augmentTargetStr == null) {
181 if (other.augmentTargetStr != null) {
184 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
187 if (childNodes == null) {
188 if (other.childNodes != null) {
191 } else if (!childNodes.equals(other.childNodes)) {
194 if (groupings == null) {
195 if (other.groupings != null) {
198 } else if (!groupings.equals(other.groupings)) {
201 if (usesNodes == null) {
202 if (other.usesNodes != null) {
205 } else if (!usesNodes.equals(other.usesNodes)) {
208 if (whenCondition == null) {
209 if (other.whenCondition != null) {
212 } else if (!whenCondition.equals(other.whenCondition)) {
219 private static class AugmentationSchemaImpl implements AugmentationSchema {
220 private final SchemaPath targetPath;
221 private RevisionAwareXPath whenCondition;
222 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
223 private Set<GroupingDefinition> groupings = Collections.emptySet();
224 private Set<UsesNode> uses = Collections.emptySet();
226 private String description;
227 private String reference;
228 private Status status;
230 private AugmentationSchemaImpl(SchemaPath targetPath) {
231 this.targetPath = targetPath;
235 public SchemaPath getTargetPath() {
240 public RevisionAwareXPath getWhenCondition() {
241 return whenCondition;
244 private void setWhenCondition(RevisionAwareXPath whenCondition) {
245 this.whenCondition = whenCondition;
249 public Set<DataSchemaNode> getChildNodes() {
250 return new HashSet<DataSchemaNode>(childNodes.values());
253 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
254 if (childNodes != null) {
255 this.childNodes = childNodes;
260 public Set<GroupingDefinition> getGroupings() {
264 private void setGroupings(Set<GroupingDefinition> groupings) {
265 if (groupings != null) {
266 this.groupings = groupings;
271 public Set<UsesNode> getUses() {
275 private void setUses(Set<UsesNode> uses) {
282 * Always returns an empty set, because augmentation can not contains
286 public Set<TypeDefinition<?>> getTypeDefinitions() {
287 return Collections.emptySet();
291 public String getDescription() {
295 private void setDescription(String description) {
296 this.description = description;
300 public String getReference() {
304 private void setReference(String reference) {
305 this.reference = reference;
309 public Status getStatus() {
313 private void setStatus(Status status) {
314 this.status = status;
318 public DataSchemaNode getDataChildByName(QName name) {
319 return childNodes.get(name);
323 public DataSchemaNode getDataChildByName(String name) {
324 DataSchemaNode result = null;
325 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
326 if (entry.getKey().getLocalName().equals(name)) {
327 result = entry.getValue();
335 public int hashCode() {
336 final int prime = 17;
338 result = prime * result
339 + ((targetPath == null) ? 0 : targetPath.hashCode());
340 result = prime * result
341 + ((childNodes == null) ? 0 : childNodes.hashCode());
342 result = prime * result
343 + ((groupings == null) ? 0 : groupings.hashCode());
344 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
345 result = prime * result
346 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
351 public boolean equals(Object obj) {
358 if (getClass() != obj.getClass()) {
361 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
362 if (targetPath == null) {
363 if (other.targetPath != null) {
366 } else if (!targetPath.equals(other.targetPath)) {
369 if (childNodes == null) {
370 if (other.childNodes != null) {
373 } else if (!childNodes.equals(other.childNodes)) {
376 if (groupings == null) {
377 if (other.groupings != null) {
380 } else if (!groupings.equals(other.groupings)) {
384 if (other.uses != null) {
387 } else if (!uses.equals(other.uses)) {
390 if (whenCondition == null) {
391 if (other.whenCondition != null) {
394 } else if (!whenCondition.equals(other.whenCondition)) {
401 public String toString() {
402 StringBuilder sb = new StringBuilder(
403 AugmentationSchemaImpl.class.getSimpleName());
405 sb.append("targetPath=" + targetPath);
406 sb.append(", childNodes=" + childNodes.values());
407 sb.append(", groupings=" + groupings);
408 sb.append(", uses=" + uses);
410 return sb.toString();