Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / SubmoduleEffectiveStatementImpl.java
1 /**
2  * Copyright (c) 2015 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.stmt.rfc6020.effective;
9
10 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
11
12 import java.util.LinkedHashSet;
13 import java.util.LinkedHashMap;
14 import com.google.common.collect.ImmutableMap;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UsesNode;
20 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
21 import java.util.Map;
22 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
25 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
26 import java.net.URI;
27 import java.util.Collection;
28 import java.util.Date;
29 import java.util.HashSet;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Set;
33 import org.opendaylight.yangtools.concepts.Immutable;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
36 import org.opendaylight.yangtools.yang.model.api.Deviation;
37 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
38 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
39 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
44 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
49 import com.google.common.collect.ImmutableList;
50 import com.google.common.collect.ImmutableSet;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
52 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
53
54 public class SubmoduleEffectiveStatementImpl
55         extends
56         AbstractEffectiveDocumentedNode<String, SubmoduleStatement>
57         implements Module, Immutable {
58
59     private final QNameModule qNameModule;
60     private final String name;
61     private String sourcePath;
62     private String prefix;
63     private String yangVersion;
64     private String organization;
65     private String contact;
66     private ImmutableSet<ModuleImport> imports;
67     private ImmutableSet<Module> submodules;
68     private ImmutableSet<FeatureDefinition> features;
69     private ImmutableSet<NotificationDefinition> notifications;
70     private ImmutableSet<AugmentationSchema> augmentations;
71     private ImmutableSet<RpcDefinition> rpcs;
72     private ImmutableSet<Deviation> deviations;
73     private ImmutableList<ExtensionDefinition> extensionNodes;
74     private ImmutableSet<IdentitySchemaNode> identities;
75     private ImmutableList<UnknownSchemaNode> unknownNodes;
76     private String source;
77     private ImmutableList<EffectiveStatement<?,?>> substatementsOfSubmodules;
78
79     private ImmutableMap<QName, DataSchemaNode> childNodes;
80     private ImmutableSet<GroupingDefinition> groupings;
81     private ImmutableSet<UsesNode> uses;
82     private ImmutableSet<TypeDefinition<?>> typeDefinitions;
83     private ImmutableSet<DataSchemaNode> publicChildNodes;
84
85     public SubmoduleEffectiveStatementImpl(
86             StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
87         super(ctx);
88
89         name = argument();
90
91         String belongsToModuleName = firstAttributeOf(
92                 ctx.declaredSubstatements(), BelongsToStatement.class);
93         final QNameModule belongsToModuleQName = ctx.getFromNamespace(
94                 ModuleNameToModuleQName.class, belongsToModuleName);
95         RevisionEffectiveStatementImpl submoduleRevision = firstEffective(RevisionEffectiveStatementImpl.class);
96
97         qNameModule = submoduleRevision == null ? QNameModule.create(
98                 belongsToModuleQName.getNamespace(),
99                 SimpleDateFormatUtil.DEFAULT_DATE_REV) : QNameModule.create(
100                 belongsToModuleQName.getNamespace(),
101                 submoduleRevision.argument());
102
103         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
104             if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
105                 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
106                         .argument();
107             }
108             if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
109                 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
110                         .argument();
111             }
112             if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
113                 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
114                         .argument();
115             }
116             if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
117                 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
118                         .argument();
119             }
120         }
121
122         //:TODO init source, sourcePath
123         source = ctx.getStatementSource().name();
124
125         initSubmodules(ctx);
126         initSubstatementCollections(ctx);
127     }
128
129     private void initSubmodules(
130             StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
131         Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
132                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
133
134         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
135             this.submodules = ImmutableSet.of();
136             this.substatementsOfSubmodules = ImmutableList.of();
137             return;
138         }
139
140         Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
141                 .values();
142
143         Set<Module> submodulesInit = new HashSet<>();
144         List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
145         for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
146             Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
147                     .getFromNamespace(SubmoduleNamespace.class,
148                             submoduleIdentifier);
149             SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
150             submodulesInit.add(submodule);
151             substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
152         }
153
154         this.submodules = ImmutableSet.copyOf(submodulesInit);
155         this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
156     }
157
158     private void initSubstatementCollections(StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
159         List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
160
161         effectiveSubstatements.addAll(effectiveSubstatements());
162         effectiveSubstatements.addAll(this.substatementsOfSubmodules);
163
164         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
165         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
166         Set<ModuleImport> importsInit = new HashSet<>();
167         Set<NotificationDefinition> notificationsInit = new HashSet<>();
168         Set<RpcDefinition> rpcsInit = new HashSet<>();
169         Set<Deviation> deviationsInit = new HashSet<>();
170         Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
171         Set<FeatureDefinition> featuresInit = new HashSet<>();
172         List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
173
174         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
175         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
176         Set<UsesNode> mutableUses = new HashSet<>();
177         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
178         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
179
180         //:TODO add validation also for other node types
181         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
182             if (effectiveStatement instanceof UnknownSchemaNode) {
183                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
184             }
185             if (effectiveStatement instanceof AugmentationSchema) {
186                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
187             }
188             if (effectiveStatement instanceof ModuleImport) {
189                 importsInit.add((ModuleImport) effectiveStatement);
190             }
191             if (effectiveStatement instanceof NotificationDefinition) {
192                 notificationsInit
193                         .add((NotificationDefinition) effectiveStatement);
194             }
195             if (effectiveStatement instanceof RpcDefinition) {
196                 rpcsInit.add((RpcDefinition) effectiveStatement);
197             }
198             if (effectiveStatement instanceof Deviation) {
199                 deviationsInit.add((Deviation) effectiveStatement);
200             }
201             if (effectiveStatement instanceof IdentitySchemaNode) {
202                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
203             }
204             if (effectiveStatement instanceof FeatureDefinition) {
205                 featuresInit.add((FeatureDefinition) effectiveStatement);
206             }
207             if (effectiveStatement instanceof ExtensionDefinition) {
208                 extensionNodesInit
209                         .add((ExtensionDefinition) effectiveStatement);
210             }
211             if (effectiveStatement instanceof DataSchemaNode) {
212                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
213                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
214                     mutableChildNodes.put(dataSchemaNode.getQName(),
215                             dataSchemaNode);
216                     mutablePublicChildNodes.add(dataSchemaNode);
217                 } else {
218                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
219                 }
220             }
221             if (effectiveStatement instanceof UsesNode) {
222                 UsesNode usesNode = (UsesNode) effectiveStatement;
223                 if (!mutableUses.contains(usesNode)) {
224                     mutableUses.add(usesNode);
225                 } else {
226                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
227                 }
228             }
229             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
230                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
231                 ExtendedType extendedType = typeDef.buildType();
232                 if (!mutableTypeDefinitions.contains(extendedType)) {
233                     mutableTypeDefinitions.add(extendedType);
234                 } else {
235                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
236                 }
237             }
238             if (effectiveStatement instanceof GroupingDefinition) {
239                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
240                 if (!mutableGroupings.contains(grp)) {
241                     mutableGroupings.add(grp);
242                 } else {
243                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
244                 }
245             }
246         }
247
248         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
249         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
250         this.imports = ImmutableSet.copyOf(importsInit);
251         this.notifications = ImmutableSet.copyOf(notificationsInit);
252         this.rpcs = ImmutableSet.copyOf(rpcsInit);
253         this.deviations = ImmutableSet.copyOf(deviationsInit);
254         this.identities = ImmutableSet.copyOf(identitiesInit);
255         this.features = ImmutableSet.copyOf(featuresInit);
256         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
257
258         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
259         this.groupings = ImmutableSet.copyOf(mutableGroupings);
260         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
261         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
262         this.uses = ImmutableSet.copyOf(mutableUses);
263     }
264
265     @Override
266     public String getModuleSourcePath() {
267         return sourcePath;
268     }
269
270     @Override
271     public URI getNamespace() {
272         return qNameModule.getNamespace();
273     }
274
275     @Override
276     public String getName() {
277         return name;
278     }
279
280     @Override
281     public Date getRevision() {
282         return qNameModule.getRevision();
283     }
284
285     @Override
286     public String getPrefix() {
287         return prefix;
288     }
289
290     @Override
291     public String getYangVersion() {
292         return yangVersion;
293     }
294
295     @Override
296     public String getOrganization() {
297         return organization;
298     }
299
300     @Override
301     public String getContact() {
302         return contact;
303     }
304
305     @Override
306     public Set<ModuleImport> getImports() {
307         return imports;
308     }
309
310     @Override
311     public Set<Module> getSubmodules() {
312         return submodules;
313     }
314
315     @Override
316     public Set<FeatureDefinition> getFeatures() {
317         return features;
318     }
319
320     @Override
321     public Set<NotificationDefinition> getNotifications() {
322         return notifications;
323     }
324
325     @Override
326     public Set<AugmentationSchema> getAugmentations() {
327         return augmentations;
328     }
329
330     @Override
331     public Set<RpcDefinition> getRpcs() {
332         return rpcs;
333     }
334
335     @Override
336     public Set<Deviation> getDeviations() {
337         return deviations;
338     }
339
340     @Override
341     public List<ExtensionDefinition> getExtensionSchemaNodes() {
342         return extensionNodes;
343     }
344
345     @Override
346     public Set<IdentitySchemaNode> getIdentities() {
347         return identities;
348     }
349
350     @Override
351     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
352         return unknownNodes;
353     }
354
355     @Override
356     public final Set<TypeDefinition<?>> getTypeDefinitions() {
357         return typeDefinitions;
358     }
359
360     @Override
361     public final Set<DataSchemaNode> getChildNodes() {
362         return publicChildNodes;
363     }
364
365     @Override
366     public final Set<GroupingDefinition> getGroupings() {
367         return groupings;
368     }
369
370     @Override
371     public final DataSchemaNode getDataChildByName(final QName name) {
372         // Child nodes are keyed by their container name, so we can do a direct
373         // lookup
374         return childNodes.get(name);
375     }
376
377     @Override
378     public final DataSchemaNode getDataChildByName(final String name) {
379         for (DataSchemaNode node : childNodes.values()) {
380             if (node.getQName().getLocalName().equals(name)) {
381                 return node;
382             }
383         }
384         return null;
385     }
386
387     @Override
388     public Set<UsesNode> getUses() {
389         return uses;
390     }
391
392     @Override
393     public String getSource() {
394         return source;
395     }
396
397     @Override
398     public int hashCode() {
399         final int prime = 31;
400         int result = 1;
401         result = prime * result + ((name == null) ? 0 : name.hashCode());
402         result = prime * result
403                 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
404         result = prime * result + qNameModule.hashCode();
405         return result;
406     }
407
408     @Override
409     public boolean equals(final Object obj) {
410         if (this == obj) {
411             return true;
412         }
413         if (obj == null) {
414             return false;
415         }
416         if (getClass() != obj.getClass()) {
417             return false;
418         }
419         SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
420         if (name == null) {
421             if (other.name != null) {
422                 return false;
423             }
424         } else if (!name.equals(other.name)) {
425             return false;
426         }
427         if (!qNameModule.equals(other.qNameModule)) {
428             return false;
429         }
430         if (yangVersion == null) {
431             if (other.yangVersion != null) {
432                 return false;
433             }
434         } else if (!yangVersion.equals(other.yangVersion)) {
435             return false;
436         }
437         return true;
438     }
439
440     @Override
441     public String toString() {
442
443         StringBuilder sb = new StringBuilder(SubmoduleEffectiveStatementImpl.class.getSimpleName());
444         sb.append("[");
445         sb.append("name=").append(name);
446         sb.append(", namespace=").append(getNamespace());
447         sb.append(", revision=").append(getRevision());
448         sb.append(", prefix=").append(prefix);
449         sb.append(", yangVersion=").append(yangVersion);
450         sb.append("]");
451         return sb.toString();
452     }
453
454     @Override
455     public QNameModule getQNameModule() {
456         return qNameModule;
457     }
458 }