Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / model / parser / builder / AugmentationSchemaBuilderImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.model.parser.builder;\r
9 \r
10 import java.util.HashMap;\r
11 import java.util.HashSet;\r
12 import java.util.Map;\r
13 import java.util.Set;\r
14 \r
15 import org.opendaylight.controller.model.parser.api.AugmentationSchemaBuilder;\r
16 import org.opendaylight.controller.model.parser.api.DataSchemaNodeBuilder;\r
17 import org.opendaylight.controller.model.parser.api.GroupingBuilder;\r
18 import org.opendaylight.controller.model.parser.api.TypeDefinitionBuilder;\r
19 import org.opendaylight.controller.model.parser.api.UsesNodeBuilder;\r
20 import org.opendaylight.controller.model.parser.util.YangModelBuilderHelper;\r
21 import org.opendaylight.controller.yang.common.QName;\r
22 import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
23 import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
24 import org.opendaylight.controller.yang.model.api.GroupingDefinition;\r
25 import org.opendaylight.controller.yang.model.api.SchemaPath;\r
26 import org.opendaylight.controller.yang.model.api.Status;\r
27 import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
28 import org.opendaylight.controller.yang.model.api.UsesNode;\r
29 \r
30 \r
31 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {\r
32 \r
33         private final AugmentationSchemaImpl instance;\r
34         private final SchemaPath augmentTarget;\r
35         final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();\r
36         final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();\r
37         private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();\r
38 \r
39         AugmentationSchemaBuilderImpl(String augmentPath) {\r
40                 SchemaPath targetPath = YangModelBuilderHelper.parsePath(augmentPath);\r
41                 augmentTarget = targetPath;\r
42                 instance = new AugmentationSchemaImpl(targetPath);\r
43         }\r
44 \r
45         @Override\r
46         public void addChildNode(DataSchemaNodeBuilder childNode) {\r
47                 childNodes.add(childNode);\r
48         }\r
49 \r
50         @Override\r
51         public void addGrouping(GroupingBuilder grouping) {\r
52                 groupings.add(grouping);\r
53         }\r
54 \r
55         /**\r
56          * Always returns null.\r
57          */\r
58         @Override\r
59         public QName getQName() {\r
60                 return null;\r
61         }\r
62 \r
63         @Override\r
64         public AugmentationSchema build() {\r
65 \r
66                 // CHILD NODES\r
67                 Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();\r
68                 for(DataSchemaNodeBuilder node : childNodes) {\r
69                         childs.put(node.getQName(), node.build());\r
70                 }\r
71                 instance.setChildNodes(childs);\r
72 \r
73                 // GROUPINGS\r
74                 Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();\r
75                 for(GroupingBuilder builder : groupings) {\r
76                         groupingDefinitions.add(builder.build());\r
77                 }\r
78                 instance.setGroupings(groupingDefinitions);\r
79 \r
80                 // USES\r
81                 Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();\r
82                 for(UsesNodeBuilder builder : usesNodes) {\r
83                         usesNodeDefinitions.add(builder.build());\r
84                 }\r
85                 instance.setUses(usesNodeDefinitions);\r
86 \r
87                 return instance;\r
88         }\r
89 \r
90         @Override\r
91         public void addUsesNode(UsesNodeBuilder usesBuilder) {\r
92                 usesNodes.add(usesBuilder);\r
93         }\r
94 \r
95         @Override\r
96         public void addTypedef(TypeDefinitionBuilder type) {\r
97                 throw new UnsupportedOperationException("Augmentation can not contains type definitions");\r
98         }\r
99 \r
100         @Override\r
101         public void setDescription(String description) {\r
102                 instance.setDescription(description);\r
103         }\r
104 \r
105         @Override\r
106         public void setReference(String reference) {\r
107                 instance.setReference(reference);\r
108         }\r
109 \r
110         @Override\r
111         public void setStatus(Status status) {\r
112                 instance.setStatus(status);\r
113         }\r
114 \r
115         @Override\r
116         public SchemaPath getTargetPath() {\r
117                 return augmentTarget;\r
118         }\r
119 \r
120 \r
121         private static class AugmentationSchemaImpl implements AugmentationSchema {\r
122 \r
123                 private final SchemaPath targetPath;\r
124                 private Map<QName, DataSchemaNode> childNodes;\r
125                 private Set<GroupingDefinition> groupings;\r
126                 private Set<UsesNode> uses;\r
127 \r
128                 private String description;\r
129                 private String reference;\r
130                 private Status status;\r
131 \r
132                 private AugmentationSchemaImpl(SchemaPath targetPath) {\r
133                         this.targetPath = targetPath;\r
134                 }\r
135 \r
136 \r
137                 @Override\r
138                 public SchemaPath getTargetPath() {\r
139                         return targetPath;\r
140                 }\r
141 \r
142                 @Override\r
143                 public Set<DataSchemaNode> getChildNodes() {\r
144                         return new HashSet<DataSchemaNode>(childNodes.values());\r
145                 }\r
146                 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {\r
147                         this.childNodes = childNodes;\r
148                 }\r
149 \r
150                 @Override\r
151                 public Set<GroupingDefinition> getGroupings() {\r
152                         return groupings;\r
153                 }\r
154                 private void setGroupings(Set<GroupingDefinition> groupings) {\r
155                         this.groupings = groupings;\r
156                 }\r
157 \r
158                 @Override\r
159                 public Set<UsesNode> getUses() {\r
160                         return uses;\r
161                 }\r
162                 private void setUses(Set<UsesNode> uses) {\r
163                         this.uses = uses;\r
164                 }\r
165 \r
166                 /**\r
167                  * This method always returns null, because augmentation can not contains type definitions.\r
168                  */\r
169                 @Override\r
170                 public Set<TypeDefinition<?>> getTypeDefinitions() {\r
171                         return null;\r
172                 }\r
173 \r
174                 @Override\r
175                 public String getDescription() {\r
176                         return description;\r
177                 }\r
178                 private void setDescription(String description) {\r
179                         this.description = description;\r
180                 }\r
181 \r
182                 @Override\r
183                 public String getReference() {\r
184                         return reference;\r
185                 }\r
186                 private void setReference(String reference) {\r
187                         this.reference = reference;\r
188                 }\r
189 \r
190                 @Override\r
191                 public Status getStatus() {\r
192                         return status;\r
193                 }\r
194                 private void setStatus(Status status) {\r
195                         this.status = status;\r
196                 }\r
197 \r
198                 @Override\r
199                 public DataSchemaNode getDataChildByName(QName name) {\r
200                         return childNodes.get(name);\r
201                 }\r
202 \r
203                 @Override\r
204                 public DataSchemaNode getDataChildByName(String name) {\r
205                         DataSchemaNode result = null;\r
206                         for(Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {\r
207                                 if(entry.getKey().getLocalName().equals(name)) {\r
208                                         result = entry.getValue();\r
209                                         break;\r
210                                 }\r
211                         }\r
212                         return result;\r
213                 }\r
214 \r
215                 @Override\r
216                 public int hashCode() {\r
217                         final int prime = 17;\r
218             int result = 1;\r
219             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());\r
220             result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());\r
221             result = prime * result + ((groupings == null) ? 0 : groupings.hashCode());\r
222             result = prime * result + ((uses == null) ? 0 : uses.hashCode());\r
223             result = prime * result + ((description == null) ? 0 : description.hashCode());\r
224             result = prime * result + ((reference == null) ? 0 : reference.hashCode());\r
225             result = prime * result + ((status == null) ? 0 : status.hashCode());\r
226             return result;\r
227                 }\r
228 \r
229                 @Override\r
230         public boolean equals(Object obj) {\r
231             if (this == obj) {\r
232                 return true;\r
233             }\r
234             if (obj == null) {\r
235                 return false;\r
236             }\r
237             if (getClass() != obj.getClass()) {\r
238                 return false;\r
239             }\r
240             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;\r
241             if (targetPath == null) {\r
242                 if (other.targetPath != null) {\r
243                     return false;\r
244                 }\r
245             } else if (!targetPath.equals(other.targetPath)) {\r
246                 return false;\r
247             }\r
248             if (childNodes == null) {\r
249                 if (other.childNodes != null) {\r
250                     return false;\r
251                 }\r
252             } else if (!childNodes.equals(other.childNodes)) {\r
253                 return false;\r
254             }\r
255             if (groupings == null) {\r
256                 if (other.groupings != null) {\r
257                     return false;\r
258                 }\r
259             } else if (!groupings.equals(other.groupings)) {\r
260                 return false;\r
261             }\r
262             if (uses == null) {\r
263                 if (other.uses != null) {\r
264                     return false;\r
265                 }\r
266             } else if (!uses.equals(other.uses)) {\r
267                 return false;\r
268             }\r
269             if (description == null) {\r
270                 if (other.description != null) {\r
271                     return false;\r
272                 }\r
273             } else if (!description.equals(other.description)) {\r
274                 return false;\r
275             }\r
276             if (reference == null) {\r
277                 if (other.reference != null) {\r
278                     return false;\r
279                 }\r
280             } else if (!reference.equals(other.reference)) {\r
281                 return false;\r
282             }\r
283             if (status == null) {\r
284                 if (other.status != null) {\r
285                     return false;\r
286                 }\r
287             } else if (!status.equals(other.status)) {\r
288                 return false;\r
289             }\r
290             return true;\r
291         }\r
292 \r
293                 @Override\r
294                 public String toString() {\r
295                         StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());\r
296                         sb.append("[");\r
297                         sb.append("targetPath="+ targetPath);\r
298                         sb.append(", childNodes="+ childNodes.values());\r
299                         sb.append(", groupings="+ groupings);\r
300                         sb.append(", uses="+ uses);\r
301                         sb.append("]");\r
302                         return sb.toString();\r
303                 }\r
304         }\r
305 \r
306 }\r