YANG model parser refactoring
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / ExtensionBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.model.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
20
21 public class ExtensionBuilder implements SchemaNodeBuilder {
22
23     private final ExtensionDefinitionImpl instance;
24     private final QName qname;
25     private final List<UnknownSchemaNodeBuilder> addedExtensions = new ArrayList<UnknownSchemaNodeBuilder>();
26     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
27
28     ExtensionBuilder(QName qname) {
29         this.qname = qname;
30         instance = new ExtensionDefinitionImpl(qname);
31     }
32
33     @Override
34     public ExtensionDefinition build() {
35         List<UnknownSchemaNode> extensions = new ArrayList<UnknownSchemaNode>();
36         for (UnknownSchemaNodeBuilder e : addedExtensions) {
37             extensions.add(e.build());
38         }
39         instance.setUnknownSchemaNodes(extensions);
40         return instance;
41     }
42
43     public void addExtension(UnknownSchemaNodeBuilder extension) {
44         addedExtensions.add(extension);
45     }
46
47     public void setYinElement(boolean yin) {
48         instance.setYinElement(yin);
49     }
50
51     public void setArgument(String argument) {
52         instance.setArgument(argument);
53     }
54
55     @Override
56     public QName getQName() {
57         return qname;
58     }
59
60     @Override
61     public void setPath(SchemaPath schemaPath) {
62         instance.setPath(schemaPath);
63     }
64
65     @Override
66     public void setDescription(String description) {
67         instance.setDescription(description);
68     }
69
70     @Override
71     public void setReference(String reference) {
72         instance.setReference(reference);
73     }
74
75     @Override
76     public void setStatus(Status status) {
77         instance.setStatus(status);
78     }
79
80     @Override
81     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) {
82         addedUnknownNodes.add(unknownSchemaNodeBuilder);
83     }
84
85     private static class ExtensionDefinitionImpl implements ExtensionDefinition {
86         private final QName qname;
87         private String argument;
88         private SchemaPath schemaPath;
89         private String description;
90         private String reference;
91         private Status status = Status.CURRENT;
92         private List<UnknownSchemaNode> unknownSchemaNodes = Collections
93                 .emptyList();
94         private boolean yin;
95
96         private ExtensionDefinitionImpl(QName qname) {
97             this.qname = qname;
98         }
99
100         @Override
101         public QName getQName() {
102             return qname;
103         }
104
105         @Override
106         public SchemaPath getPath() {
107             return schemaPath;
108         }
109
110         private void setPath(SchemaPath schemaPath) {
111             this.schemaPath = schemaPath;
112         }
113
114         @Override
115         public String getDescription() {
116             return description;
117         }
118
119         private void setDescription(String description) {
120             this.description = description;
121         }
122
123         @Override
124         public String getReference() {
125             return reference;
126         }
127
128         private void setReference(String reference) {
129             this.reference = reference;
130         }
131
132         @Override
133         public Status getStatus() {
134             return status;
135         }
136
137         private void setStatus(Status status) {
138             if (status != null) {
139                 this.status = status;
140             }
141         }
142
143         @Override
144         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
145             return unknownSchemaNodes;
146         }
147
148         private void setUnknownSchemaNodes(
149                 List<UnknownSchemaNode> unknownSchemaNodes) {
150             if(unknownSchemaNodes != null) {
151                 this.unknownSchemaNodes = unknownSchemaNodes;
152             }
153         }
154
155         @Override
156         public String getArgument() {
157             return argument;
158         }
159
160         private void setArgument(String argument) {
161             this.argument = argument;
162         }
163
164         @Override
165         public boolean isYinElement() {
166             return yin;
167         }
168
169         private void setYinElement(boolean yin) {
170             this.yin = yin;
171         }
172
173         @Override
174         public int hashCode() {
175             final int prime = 31;
176             int result = 1;
177             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
178             result = prime * result
179                     + ((schemaPath == null) ? 0 : schemaPath.hashCode());
180             result = prime * result
181                     + ((description == null) ? 0 : description.hashCode());
182             result = prime * result
183                     + ((reference == null) ? 0 : reference.hashCode());
184             result = prime * result
185                     + ((status == null) ? 0 : status.hashCode());
186             result = prime
187                     * result
188                     + ((unknownSchemaNodes == null) ? 0
189                             : unknownSchemaNodes.hashCode());
190             result = prime * result + (yin ? 1231 : 1237);
191             return result;
192         }
193
194         @Override
195         public boolean equals(Object obj) {
196             if (this == obj) {
197                 return true;
198             }
199             if (obj == null) {
200                 return false;
201             }
202             if (getClass() != obj.getClass()) {
203                 return false;
204             }
205             ExtensionDefinitionImpl other = (ExtensionDefinitionImpl) obj;
206             if (qname == null) {
207                 if (other.qname != null) {
208                     return false;
209                 }
210             } else if (!qname.equals(other.qname)) {
211                 return false;
212             }
213             if (schemaPath == null) {
214                 if (other.schemaPath != null) {
215                     return false;
216                 }
217             } else if (!schemaPath.equals(other.schemaPath)) {
218                 return false;
219             }
220             if (description == null) {
221                 if (other.description != null) {
222                     return false;
223                 }
224             } else if (!description.equals(other.description)) {
225                 return false;
226             }
227             if (reference == null) {
228                 if (other.reference != null) {
229                     return false;
230                 }
231             } else if (!reference.equals(other.reference)) {
232                 return false;
233             }
234             if (status == null) {
235                 if (other.status != null) {
236                     return false;
237                 }
238             } else if (!status.equals(other.status)) {
239                 return false;
240             }
241             if (unknownSchemaNodes == null) {
242                 if (other.unknownSchemaNodes != null) {
243                     return false;
244                 }
245             } else if (!unknownSchemaNodes.equals(other.unknownSchemaNodes)) {
246                 return false;
247             }
248             if (yin != other.yin) {
249                 return false;
250             }
251             return true;
252         }
253
254         @Override
255         public String toString() {
256             StringBuilder sb = new StringBuilder(
257                     ExtensionDefinitionImpl.class.getSimpleName());
258             sb.append("[");
259             sb.append("argument="+ argument);
260             sb.append(", qname=" + qname);
261             sb.append(", schemaPath=" + schemaPath);
262             sb.append(", description=" + description);
263             sb.append(", reference=" + reference);
264             sb.append(", status=" + status);
265             sb.append(", extensionSchemaNodes=" + unknownSchemaNodes);
266             sb.append(", yin=" + yin);
267             sb.append("]");
268             return sb.toString();
269         }
270     }
271
272 }