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 {
34 private final AugmentationSchemaImpl instance;
35 private final int line;
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, final int line) {
44 this.augmentTargetStr = augmentTargetStr;
46 final SchemaPath targetPath = YangModelBuilderUtil
47 .parseAugmentPath(augmentTargetStr);
48 augmentTarget = targetPath;
49 instance = new AugmentationSchemaImpl(targetPath);
53 public int getLine() {
58 public void addChildNode(DataSchemaNodeBuilder childNode) {
59 childNodes.add(childNode);
63 public Set<DataSchemaNodeBuilder> getChildNodes() {
68 public void addGrouping(GroupingBuilder grouping) {
69 groupings.add(grouping);
73 public void addUsesNode(UsesNodeBuilder usesBuilder) {
74 usesNodes.add(usesBuilder);
78 * Always returns null.
81 public QName getQName() {
86 public AugmentationSchema build() {
87 RevisionAwareXPath whenStmt;
88 if (whenCondition == null) {
91 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
93 instance.setWhenCondition(whenStmt);
96 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
97 for (DataSchemaNodeBuilder node : childNodes) {
98 childs.put(node.getQName(), node.build());
100 instance.setChildNodes(childs);
103 final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
104 for (GroupingBuilder builder : groupings) {
105 groupingDefinitions.add(builder.build());
107 instance.setGroupings(groupingDefinitions);
110 final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
111 for (UsesNodeBuilder builder : usesNodes) {
112 usesNodeDefinitions.add(builder.build());
114 instance.setUses(usesNodeDefinitions);
119 public String getWhenCondition() {
120 return whenCondition;
123 public void addWhenCondition(String whenCondition) {
124 this.whenCondition = whenCondition;
128 public void addTypedef(TypeDefinitionBuilder type) {
129 throw new UnsupportedOperationException(
130 "Augmentation can not contains type definitions");
134 public void setDescription(String description) {
135 instance.setDescription(description);
139 public void setReference(String reference) {
140 instance.setReference(reference);
144 public void setStatus(Status status) {
145 instance.setStatus(status);
149 public SchemaPath getTargetPath() {
150 return augmentTarget;
154 public String getTargetPathAsString() {
155 return augmentTargetStr;
159 public int hashCode() {
160 final int prime = 17;
162 result = prime * result
163 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
164 result = prime * result
165 + ((childNodes == null) ? 0 : childNodes.hashCode());
166 result = prime * result
167 + ((groupings == null) ? 0 : groupings.hashCode());
168 result = prime * result + ((usesNodes == null) ? 0 : usesNodes.hashCode());
169 result = prime * result
170 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
175 public boolean equals(Object obj) {
182 if (getClass() != obj.getClass()) {
185 AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
186 if (augmentTargetStr == null) {
187 if (other.augmentTargetStr != null) {
190 } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
193 if (childNodes == null) {
194 if (other.childNodes != null) {
197 } else if (!childNodes.equals(other.childNodes)) {
200 if (groupings == null) {
201 if (other.groupings != null) {
204 } else if (!groupings.equals(other.groupings)) {
207 if (usesNodes == null) {
208 if (other.usesNodes != null) {
211 } else if (!usesNodes.equals(other.usesNodes)) {
214 if (whenCondition == null) {
215 if (other.whenCondition != null) {
218 } else if (!whenCondition.equals(other.whenCondition)) {
225 private static class AugmentationSchemaImpl implements AugmentationSchema {
226 private final SchemaPath targetPath;
227 private RevisionAwareXPath whenCondition;
228 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
229 private Set<GroupingDefinition> groupings = Collections.emptySet();
230 private Set<UsesNode> uses = Collections.emptySet();
232 private String description;
233 private String reference;
234 private Status status;
236 private AugmentationSchemaImpl(SchemaPath targetPath) {
237 this.targetPath = targetPath;
241 public SchemaPath getTargetPath() {
246 public RevisionAwareXPath getWhenCondition() {
247 return whenCondition;
250 private void setWhenCondition(RevisionAwareXPath whenCondition) {
251 this.whenCondition = whenCondition;
255 public Set<DataSchemaNode> getChildNodes() {
256 return new HashSet<DataSchemaNode>(childNodes.values());
259 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
260 if (childNodes != null) {
261 this.childNodes = childNodes;
266 public Set<GroupingDefinition> getGroupings() {
270 private void setGroupings(Set<GroupingDefinition> groupings) {
271 if (groupings != null) {
272 this.groupings = groupings;
277 public Set<UsesNode> getUses() {
281 private void setUses(Set<UsesNode> uses) {
288 * Always returns an empty set, because augmentation can not contains
292 public Set<TypeDefinition<?>> getTypeDefinitions() {
293 return Collections.emptySet();
297 public String getDescription() {
301 private void setDescription(String description) {
302 this.description = description;
306 public String getReference() {
310 private void setReference(String reference) {
311 this.reference = reference;
315 public Status getStatus() {
319 private void setStatus(Status status) {
320 this.status = status;
324 public DataSchemaNode getDataChildByName(QName name) {
325 return childNodes.get(name);
329 public DataSchemaNode getDataChildByName(String name) {
330 DataSchemaNode result = null;
331 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
332 if (entry.getKey().getLocalName().equals(name)) {
333 result = entry.getValue();
341 public int hashCode() {
342 final int prime = 17;
344 result = prime * result
345 + ((targetPath == null) ? 0 : targetPath.hashCode());
346 result = prime * result
347 + ((childNodes == null) ? 0 : childNodes.hashCode());
348 result = prime * result
349 + ((groupings == null) ? 0 : groupings.hashCode());
350 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
351 result = prime * result
352 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
357 public boolean equals(Object obj) {
364 if (getClass() != obj.getClass()) {
367 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
368 if (targetPath == null) {
369 if (other.targetPath != null) {
372 } else if (!targetPath.equals(other.targetPath)) {
375 if (childNodes == null) {
376 if (other.childNodes != null) {
379 } else if (!childNodes.equals(other.childNodes)) {
382 if (groupings == null) {
383 if (other.groupings != null) {
386 } else if (!groupings.equals(other.groupings)) {
390 if (other.uses != null) {
393 } else if (!uses.equals(other.uses)) {
396 if (whenCondition == null) {
397 if (other.whenCondition != null) {
400 } else if (!whenCondition.equals(other.whenCondition)) {
407 public String toString() {
408 StringBuilder sb = new StringBuilder(
409 AugmentationSchemaImpl.class.getSimpleName());
411 sb.append("targetPath=" + targetPath);
412 sb.append(", childNodes=" + childNodes.values());
413 sb.append(", groupings=" + groupings);
414 sb.append(", uses=" + uses);
416 return sb.toString();