Merge "Bug 1331 - Generate SPIs and yangs to target/generated-sources/ subfolders"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.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.yangtools.yang.parser.builder.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
33
34 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements
35         AugmentationSchemaBuilder {
36     private final int order;
37     private AugmentationSchemaImpl instance;
38     private String whenCondition;
39
40     private final String augmentTargetStr;
41     private final SchemaPath targetPath;
42     private SchemaPath targetNodeSchemaPath;
43
44     private boolean resolved;
45     private AugmentationSchemaBuilder copyOf;
46
47     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
48         super(moduleName, line, null);
49         this.order = order;
50         this.augmentTargetStr = augmentTargetStr;
51         targetPath = BuilderUtils.parseXPathString(augmentTargetStr);
52     }
53
54     @Override
55     protected String getStatementName() {
56         return "augment";
57     }
58
59     @Override
60     public SchemaPath getPath() {
61         return targetNodeSchemaPath;
62     }
63
64     @Override
65     public AugmentationSchema build() {
66         if (instance != null) {
67             return instance;
68         }
69
70         buildChildren();
71         instance = new AugmentationSchemaImpl(targetPath, order,this);
72
73         Builder parent = getParent();
74         if (parent instanceof ModuleBuilder) {
75             ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
76             instance.namespace = moduleBuilder.getNamespace();
77             instance.revision = moduleBuilder.getRevision();
78         }
79
80         if (parent instanceof UsesNodeBuilder) {
81             final ModuleBuilder mb = BuilderUtils.getParentModule(this);
82             final QNameModule qm = QNameModule.create(mb.getNamespace(), mb.getRevision());
83
84             List<QName> newPath = new ArrayList<>();
85             for (QName name : targetPath.getPathFromRoot()) {
86                 newPath.add(QName.create(qm, name.getPrefix(), name.getLocalName()));
87             }
88             instance.targetPath = SchemaPath.create(newPath, false);
89         } else {
90             instance.targetPath = targetNodeSchemaPath;
91         }
92
93         if (copyOf != null) {
94             instance.setCopyOf(copyOf.build());
95         }
96
97         RevisionAwareXPath whenStmt;
98         if (whenCondition == null) {
99             whenStmt = null;
100         } else {
101             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
102         }
103         instance.whenCondition = whenStmt;
104
105         // UNKNOWN NODES
106         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
107             unknownNodes.add(b.build());
108         }
109         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
110
111         return instance;
112     }
113
114     @Override
115     public boolean isResolved() {
116         return resolved;
117     }
118
119     @Override
120     public void setResolved(final boolean resolved) {
121         this.resolved = resolved;
122     }
123
124     @Override
125     public String getWhenCondition() {
126         return whenCondition;
127     }
128
129     @Override
130     public void addWhenCondition(final String whenCondition) {
131         this.whenCondition = whenCondition;
132     }
133
134     @Override
135     public String getTargetPathAsString() {
136         return augmentTargetStr;
137     }
138
139     @Override
140     public SchemaPath getTargetPath() {
141         return targetPath;
142     }
143
144     @Override
145     public SchemaPath getTargetNodeSchemaPath() {
146         return targetNodeSchemaPath;
147     }
148
149     @Override
150     public void setTargetNodeSchemaPath(final SchemaPath path) {
151         this.targetNodeSchemaPath = path;
152     }
153
154     @Override
155     public int getOrder() {
156         return order;
157     }
158
159     @Override
160     public int hashCode() {
161         final int prime = 17;
162         int result = 1;
163         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
164         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
165         result = prime * result + getChildNodeBuilders().hashCode();
166         return result;
167     }
168
169     @Override
170     public boolean equals(final Object obj) {
171         if (this == obj) {
172             return true;
173         }
174         if (obj == null) {
175             return false;
176         }
177         if (getClass() != obj.getClass()) {
178             return false;
179         }
180         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
181         if (augmentTargetStr == null) {
182             if (other.augmentTargetStr != null) {
183                 return false;
184             }
185         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
186             return false;
187         }
188         if (whenCondition == null) {
189             if (other.whenCondition != null) {
190                 return false;
191             }
192         } else if (!whenCondition.equals(other.whenCondition)) {
193             return false;
194         }
195         if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
196             return false;
197         }
198         return true;
199     }
200
201     @Override
202     public String toString() {
203         return "augment " + augmentTargetStr;
204     }
205
206     public void setCopyOf(final AugmentationSchemaBuilder old) {
207         copyOf = old;
208     }
209
210     private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware,
211             Comparable<AugmentationSchemaImpl> {
212         private final int order;
213         private SchemaPath targetPath;
214         private RevisionAwareXPath whenCondition;
215
216         private URI namespace;
217         private Date revision;
218         private ImmutableList<UnknownSchemaNode> unknownNodes;
219         private AugmentationSchema copyOf;
220
221         public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
222             super(builder);
223             this.targetPath = targetPath;
224             this.order = order;
225         }
226
227         public void setCopyOf(final AugmentationSchema build) {
228             this.copyOf = build;
229         }
230
231         @Override
232         public Optional<AugmentationSchema> getOriginalDefinition() {
233             return Optional.fromNullable(this.copyOf);
234         }
235
236         @Override
237         public SchemaPath getTargetPath() {
238             return targetPath;
239         }
240
241         @Override
242         public RevisionAwareXPath getWhenCondition() {
243             return whenCondition;
244         }
245
246         @Override
247         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
248             return unknownNodes;
249         }
250
251         @Override
252         public URI getNamespace() {
253             return namespace;
254         }
255
256         @Override
257         public Date getRevision() {
258             return revision;
259         }
260
261         @Override
262         public int hashCode() {
263             final int prime = 17;
264             int result = 1;
265             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
266             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
267             result = prime * result + getChildNodes().hashCode();
268             return result;
269         }
270
271         @Override
272         public boolean equals(final Object obj) {
273             if (this == obj) {
274                 return true;
275             }
276             if (obj == null) {
277                 return false;
278             }
279             if (getClass() != obj.getClass()) {
280                 return false;
281             }
282             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
283             if (targetPath == null) {
284                 if (other.targetPath != null) {
285                     return false;
286                 }
287             } else if (!targetPath.equals(other.targetPath)) {
288                 return false;
289             }
290             if (whenCondition == null) {
291                 if (other.whenCondition != null) {
292                     return false;
293                 }
294             } else if (!whenCondition.equals(other.whenCondition)) {
295                 return false;
296             }
297             if (!getChildNodes().equals(other.getChildNodes())) {
298                 return false;
299             }
300             return true;
301         }
302
303         @Override
304         public String toString() {
305             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
306             sb.append("[");
307             sb.append("targetPath=").append(targetPath);
308             sb.append(", when=").append(whenCondition);
309             sb.append("]");
310             return sb.toString();
311         }
312
313         @Override
314         public int compareTo(final AugmentationSchemaImpl o) {
315             checkNotNull(o);
316             Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
317             Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
318             while (thisIt.hasNext()) {
319                 if (otherIt.hasNext()) {
320                     int comp = thisIt.next().compareTo(otherIt.next());
321                     if (comp != 0) {
322                         return comp;
323                     }
324                 } else {
325                     return 1;
326                 }
327             }
328             if (otherIt.hasNext()) {
329                 return -1;
330             }
331             return this.order - o.order;
332         }
333     }
334 }