91708c00da26ccae197cc35e3c1855ed3ab548ce
[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 java.net.URI;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.Iterator;
14 import java.util.List;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
20 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
30
31 import com.google.common.base.Optional;
32 import com.google.common.collect.ImmutableList;
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 (other.getChildNodeBuilders() == null) {
196             if (other.getChildNodeBuilders() != null) {
197                 return false;
198             }
199         } else if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
200             return false;
201         }
202         return true;
203     }
204
205     @Override
206     public String toString() {
207         return "augment " + augmentTargetStr;
208     }
209
210     public void setCopyOf(final AugmentationSchemaBuilder old) {
211         copyOf = old;
212     }
213
214     private static final class AugmentationSchemaImpl extends AbstractDocumentedDataNodeContainer implements AugmentationSchema, NamespaceRevisionAware,
215             Comparable<AugmentationSchemaImpl> {
216         private final int order;
217         private SchemaPath targetPath;
218         private RevisionAwareXPath whenCondition;
219
220         private URI namespace;
221         private Date revision;
222         private ImmutableList<UnknownSchemaNode> unknownNodes;
223         private AugmentationSchema copyOf;
224
225         public AugmentationSchemaImpl(final SchemaPath targetPath, final int order, final AugmentationSchemaBuilderImpl builder) {
226             super(builder);
227             this.targetPath = targetPath;
228             this.order = order;
229         }
230
231         public void setCopyOf(final AugmentationSchema build) {
232             this.copyOf = build;
233         }
234
235         @Override
236         public Optional<AugmentationSchema> getOriginalDefinition() {
237             return Optional.fromNullable(this.copyOf);
238         }
239
240         @Override
241         public SchemaPath getTargetPath() {
242             return targetPath;
243         }
244
245         @Override
246         public RevisionAwareXPath getWhenCondition() {
247             return whenCondition;
248         }
249
250         @Override
251         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
252             return unknownNodes;
253         }
254
255         @Override
256         public URI getNamespace() {
257             return namespace;
258         }
259
260         @Override
261         public Date getRevision() {
262             return revision;
263         }
264
265         @Override
266         public int hashCode() {
267             final int prime = 17;
268             int result = 1;
269             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
270             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
271             result = getChildNodes().hashCode();
272             return result;
273         }
274
275         @Override
276         public boolean equals(final Object obj) {
277             if (this == obj) {
278                 return true;
279             }
280             if (obj == null) {
281                 return false;
282             }
283             if (getClass() != obj.getClass()) {
284                 return false;
285             }
286             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
287             if (targetPath == null) {
288                 if (other.targetPath != null) {
289                     return false;
290                 }
291             } else if (!targetPath.equals(other.targetPath)) {
292                 return false;
293             }
294             if (whenCondition == null) {
295                 if (other.whenCondition != null) {
296                     return false;
297                 }
298             } else if (!whenCondition.equals(other.whenCondition)) {
299                 return false;
300             }
301             if (!getChildNodes().equals(other.getChildNodes())) {
302                 return false;
303             }
304             return true;
305         }
306
307         @Override
308         public String toString() {
309             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
310             sb.append("[");
311             sb.append("targetPath=" + targetPath);
312             sb.append(", when=" + whenCondition);
313             sb.append("]");
314             return sb.toString();
315         }
316
317         @Override
318         public int compareTo(final AugmentationSchemaImpl o) {
319             Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
320             Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
321             while (thisIt.hasNext()) {
322                 if (otherIt.hasNext()) {
323                     int comp = thisIt.next().compareTo(otherIt.next());
324                     if (comp != 0) {
325                         return comp;
326                     }
327                 } else {
328                     return 1;
329                 }
330             }
331             if (otherIt.hasNext()) {
332                 return -1;
333             }
334             return this.order - o.order;
335         }
336     }
337 }