Added YANG typedef support in the YANG parser
[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     @Override
52     public QName getQName() {
53         return qname;
54     }
55
56     @Override
57     public void setPath(SchemaPath schemaPath) {
58         instance.setPath(schemaPath);
59     }
60
61     @Override
62     public void setDescription(String description) {
63         instance.setDescription(description);
64     }
65
66     @Override
67     public void setReference(String reference) {
68         instance.setReference(reference);
69     }
70
71     @Override
72     public void setStatus(Status status) {
73         instance.setStatus(status);
74     }
75
76     @Override
77     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) {
78         addedUnknownNodes.add(unknownSchemaNodeBuilder);
79     }
80
81     private static class ExtensionDefinitionImpl implements ExtensionDefinition {
82         private final QName qname;
83         private SchemaPath schemaPath;
84         private String description;
85         private String reference;
86         private Status status = Status.CURRENT;
87         private List<UnknownSchemaNode> unknownSchemaNodes = Collections
88                 .emptyList();
89         private boolean yin;
90
91         private ExtensionDefinitionImpl(QName qname) {
92             this.qname = qname;
93         }
94
95         @Override
96         public QName getQName() {
97             return qname;
98         }
99
100         @Override
101         public SchemaPath getPath() {
102             return schemaPath;
103         }
104
105         private void setPath(SchemaPath schemaPath) {
106             this.schemaPath = schemaPath;
107         }
108
109         @Override
110         public String getDescription() {
111             return description;
112         }
113
114         private void setDescription(String description) {
115             this.description = description;
116         }
117
118         @Override
119         public String getReference() {
120             return reference;
121         }
122
123         private void setReference(String reference) {
124             this.reference = reference;
125         }
126
127         @Override
128         public Status getStatus() {
129             return status;
130         }
131
132         private void setStatus(Status status) {
133             if (status != null) {
134                 this.status = status;
135             }
136         }
137
138         @Override
139         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
140             return unknownSchemaNodes;
141         }
142
143         private void setUnknownSchemaNodes(
144                 List<UnknownSchemaNode> unknownSchemaNodes) {
145             if(unknownSchemaNodes != null) {
146                 this.unknownSchemaNodes = unknownSchemaNodes;
147             }
148         }
149
150         @Override
151         public String getArgument() {
152             return qname.getLocalName();
153         }
154
155         @Override
156         public boolean isYinElement() {
157             return yin;
158         }
159
160         private void setYinElement(boolean yin) {
161             this.yin = yin;
162         }
163
164         @Override
165         public int hashCode() {
166             final int prime = 31;
167             int result = 1;
168             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
169             result = prime * result
170                     + ((schemaPath == null) ? 0 : schemaPath.hashCode());
171             result = prime * result
172                     + ((description == null) ? 0 : description.hashCode());
173             result = prime * result
174                     + ((reference == null) ? 0 : reference.hashCode());
175             result = prime * result
176                     + ((status == null) ? 0 : status.hashCode());
177             result = prime
178                     * result
179                     + ((unknownSchemaNodes == null) ? 0
180                             : unknownSchemaNodes.hashCode());
181             result = prime * result + (yin ? 1231 : 1237);
182             return result;
183         }
184
185         @Override
186         public boolean equals(Object obj) {
187             if (this == obj) {
188                 return true;
189             }
190             if (obj == null) {
191                 return false;
192             }
193             if (getClass() != obj.getClass()) {
194                 return false;
195             }
196             ExtensionDefinitionImpl other = (ExtensionDefinitionImpl) obj;
197             if (qname == null) {
198                 if (other.qname != null) {
199                     return false;
200                 }
201             } else if (!qname.equals(other.qname)) {
202                 return false;
203             }
204             if (schemaPath == null) {
205                 if (other.schemaPath != null) {
206                     return false;
207                 }
208             } else if (!schemaPath.equals(other.schemaPath)) {
209                 return false;
210             }
211             if (description == null) {
212                 if (other.description != null) {
213                     return false;
214                 }
215             } else if (!description.equals(other.description)) {
216                 return false;
217             }
218             if (reference == null) {
219                 if (other.reference != null) {
220                     return false;
221                 }
222             } else if (!reference.equals(other.reference)) {
223                 return false;
224             }
225             if (status == null) {
226                 if (other.status != null) {
227                     return false;
228                 }
229             } else if (!status.equals(other.status)) {
230                 return false;
231             }
232             if (unknownSchemaNodes == null) {
233                 if (other.unknownSchemaNodes != null) {
234                     return false;
235                 }
236             } else if (!unknownSchemaNodes.equals(other.unknownSchemaNodes)) {
237                 return false;
238             }
239             if (yin != other.yin) {
240                 return false;
241             }
242             return true;
243         }
244
245         @Override
246         public String toString() {
247             StringBuilder sb = new StringBuilder(
248                     ExtensionDefinitionImpl.class.getSimpleName());
249             sb.append("[");
250             sb.append("qname=" + qname);
251             sb.append(", schemaPath=" + schemaPath);
252             sb.append(", description=" + description);
253             sb.append(", reference=" + reference);
254             sb.append(", status=" + status);
255             sb.append(", extensionSchemaNodes=" + unknownSchemaNodes);
256             sb.append(", yin=" + yin);
257             sb.append("]");
258             return sb.toString();
259         }
260     }
261
262 }