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