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