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