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