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