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