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