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