Merge "Package names for enclosed Types of TOs were changed to fully qualified. Fully...
[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 import org.opendaylight.controller.yang.parser.util.Comparators;
21
22 public final class ExtensionBuilder extends AbstractSchemaNodeBuilder {
23     private boolean isBuilt;
24     private final ExtensionDefinitionImpl instance;
25
26     ExtensionBuilder(final String moduleName, final int line, final QName qname) {
27         super(moduleName, line, qname);
28         instance = new ExtensionDefinitionImpl(qname);
29     }
30
31     @Override
32     public ExtensionDefinition build() {
33         if (!isBuilt) {
34             instance.setPath(schemaPath);
35             instance.setDescription(description);
36             instance.setReference(reference);
37             instance.setStatus(status);
38
39             // UNKNOWN NODES
40             if (unknownNodes == null) {
41                 unknownNodes = new ArrayList<UnknownSchemaNode>();
42                 for (UnknownSchemaNodeBuilder un : addedUnknownNodes) {
43                     unknownNodes.add(un.build());
44                 }
45                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
46             }
47             instance.setUnknownSchemaNodes(unknownNodes);
48
49             isBuilt = true;
50         }
51
52         return instance;
53     }
54
55     public void setYinElement(boolean yin) {
56         instance.setYinElement(yin);
57     }
58
59     public void setArgument(String argument) {
60         instance.setArgument(argument);
61     }
62
63     @Override
64     public String toString() {
65         return "extension " + qname.getLocalName();
66     }
67
68     private final class ExtensionDefinitionImpl implements ExtensionDefinition {
69         private final QName qname;
70         private String argument;
71         private SchemaPath schemaPath;
72         private String description;
73         private String reference;
74         private Status status = Status.CURRENT;
75         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
76         private boolean yin;
77
78         private ExtensionDefinitionImpl(QName qname) {
79             this.qname = qname;
80         }
81
82         @Override
83         public QName getQName() {
84             return qname;
85         }
86
87         @Override
88         public SchemaPath getPath() {
89             return schemaPath;
90         }
91
92         private void setPath(SchemaPath schemaPath) {
93             this.schemaPath = schemaPath;
94         }
95
96         @Override
97         public String getDescription() {
98             return description;
99         }
100
101         private void setDescription(String description) {
102             this.description = description;
103         }
104
105         @Override
106         public String getReference() {
107             return reference;
108         }
109
110         private void setReference(String reference) {
111             this.reference = reference;
112         }
113
114         @Override
115         public Status getStatus() {
116             return status;
117         }
118
119         private void setStatus(Status status) {
120             if (status != null) {
121                 this.status = status;
122             }
123         }
124
125         @Override
126         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
127             return unknownNodes;
128         }
129
130         private void setUnknownSchemaNodes(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 + ((schemaPath == null) ? 0 : schemaPath.hashCode());
160             return result;
161         }
162
163         @Override
164         public boolean equals(Object obj) {
165             if (this == obj) {
166                 return true;
167             }
168             if (obj == null) {
169                 return false;
170             }
171             if (getClass() != obj.getClass()) {
172                 return false;
173             }
174             ExtensionDefinitionImpl other = (ExtensionDefinitionImpl) obj;
175             if (qname == null) {
176                 if (other.qname != null) {
177                     return false;
178                 }
179             } else if (!qname.equals(other.qname)) {
180                 return false;
181             }
182             if (schemaPath == null) {
183                 if (other.schemaPath != null) {
184                     return false;
185                 }
186             } else if (!schemaPath.equals(other.schemaPath)) {
187                 return false;
188             }
189             return true;
190         }
191
192         @Override
193         public String toString() {
194             StringBuilder sb = new StringBuilder(ExtensionDefinitionImpl.class.getSimpleName());
195             sb.append("[");
196             sb.append("argument=" + argument);
197             sb.append(", qname=" + qname);
198             sb.append(", schemaPath=" + schemaPath);
199             sb.append(", extensionSchemaNodes=" + unknownNodes);
200             sb.append(", yin=" + yin);
201             sb.append("]");
202             return sb.toString();
203         }
204     }
205
206 }