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