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.SchemaPath;
21 import org.opendaylight.controller.yang.model.api.Status;
22 import org.opendaylight.controller.yang.model.api.TypeDefinition;
23 import org.opendaylight.controller.yang.model.api.UsesNode;
24 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
31 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
33 private final AugmentationSchemaImpl instance;
34 private final SchemaPath augmentTarget;
35 final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
36 final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
37 private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
39 AugmentationSchemaBuilderImpl(String augmentPath) {
40 SchemaPath targetPath = YangModelBuilderUtil.parseAugmentPath(augmentPath);
41 augmentTarget = targetPath;
42 instance = new AugmentationSchemaImpl(targetPath);
46 public void addChildNode(DataSchemaNodeBuilder childNode) {
47 childNodes.add(childNode);
51 public Set<DataSchemaNodeBuilder> getChildNodes() {
56 public void addGrouping(GroupingBuilder grouping) {
57 groupings.add(grouping);
61 public void addUsesNode(UsesNodeBuilder usesBuilder) {
62 usesNodes.add(usesBuilder);
66 * Always returns null.
69 public QName getQName() {
74 public AugmentationSchema build() {
77 Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
78 for (DataSchemaNodeBuilder node : childNodes) {
79 childs.put(node.getQName(), node.build());
81 instance.setChildNodes(childs);
84 Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
85 for (GroupingBuilder builder : groupings) {
86 groupingDefinitions.add(builder.build());
88 instance.setGroupings(groupingDefinitions);
91 Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
92 for (UsesNodeBuilder builder : usesNodes) {
93 usesNodeDefinitions.add(builder.build());
95 instance.setUses(usesNodeDefinitions);
101 public void addTypedef(TypeDefinitionBuilder type) {
102 throw new UnsupportedOperationException(
103 "Augmentation can not contains type definitions");
107 public void setDescription(String description) {
108 instance.setDescription(description);
112 public void setReference(String reference) {
113 instance.setReference(reference);
117 public void setStatus(Status status) {
118 instance.setStatus(status);
122 public SchemaPath getTargetPath() {
123 return augmentTarget;
126 private static class AugmentationSchemaImpl implements AugmentationSchema {
128 private final SchemaPath targetPath;
129 private Map<QName, DataSchemaNode> childNodes;
130 private Set<GroupingDefinition> groupings;
131 private Set<UsesNode> uses;
133 private String description;
134 private String reference;
135 private Status status;
137 private AugmentationSchemaImpl(SchemaPath targetPath) {
138 this.targetPath = targetPath;
142 public SchemaPath getTargetPath() {
147 public Set<DataSchemaNode> getChildNodes() {
148 return new HashSet<DataSchemaNode>(childNodes.values());
151 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
152 this.childNodes = childNodes;
156 public Set<GroupingDefinition> getGroupings() {
160 private void setGroupings(Set<GroupingDefinition> groupings) {
161 this.groupings = groupings;
165 public Set<UsesNode> getUses() {
169 private void setUses(Set<UsesNode> uses) {
174 * Always returns an empty set, because augmentation can not contains
178 public Set<TypeDefinition<?>> getTypeDefinitions() {
179 return Collections.emptySet();
183 public String getDescription() {
187 private void setDescription(String description) {
188 this.description = description;
192 public String getReference() {
196 private void setReference(String reference) {
197 this.reference = reference;
201 public Status getStatus() {
205 private void setStatus(Status status) {
206 this.status = status;
210 public DataSchemaNode getDataChildByName(QName name) {
211 return childNodes.get(name);
215 public DataSchemaNode getDataChildByName(String name) {
216 DataSchemaNode result = null;
217 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
218 if (entry.getKey().getLocalName().equals(name)) {
219 result = entry.getValue();
227 public int hashCode() {
228 final int prime = 17;
230 result = prime * result
231 + ((targetPath == null) ? 0 : targetPath.hashCode());
232 result = prime * result
233 + ((childNodes == null) ? 0 : childNodes.hashCode());
234 result = prime * result
235 + ((groupings == null) ? 0 : groupings.hashCode());
236 result = prime * result + ((uses == null) ? 0 : uses.hashCode());
237 result = prime * result
238 + ((description == null) ? 0 : description.hashCode());
239 result = prime * result
240 + ((reference == null) ? 0 : reference.hashCode());
241 result = prime * result
242 + ((status == null) ? 0 : status.hashCode());
247 public boolean equals(Object obj) {
254 if (getClass() != obj.getClass()) {
257 AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
258 if (targetPath == null) {
259 if (other.targetPath != null) {
262 } else if (!targetPath.equals(other.targetPath)) {
265 if (childNodes == null) {
266 if (other.childNodes != null) {
269 } else if (!childNodes.equals(other.childNodes)) {
272 if (groupings == null) {
273 if (other.groupings != null) {
276 } else if (!groupings.equals(other.groupings)) {
280 if (other.uses != null) {
283 } else if (!uses.equals(other.uses)) {
286 if (description == null) {
287 if (other.description != null) {
290 } else if (!description.equals(other.description)) {
293 if (reference == null) {
294 if (other.reference != null) {
297 } else if (!reference.equals(other.reference)) {
300 if (status == null) {
301 if (other.status != null) {
304 } else if (!status.equals(other.status)) {
311 public String toString() {
312 StringBuilder sb = new StringBuilder(
313 AugmentationSchemaImpl.class.getSimpleName());
315 sb.append("targetPath=" + targetPath);
316 sb.append(", childNodes=" + childNodes.values());
317 sb.append(", groupings=" + groupings);
318 sb.append(", uses=" + uses);
320 return sb.toString();