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