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