BUG-6757: revert fix for BUG-4456
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveModule.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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.net.URI;
14 import java.util.ArrayList;
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.List;
21 import java.util.Map;
22 import java.util.Set;
23 import org.opendaylight.yangtools.concepts.Immutable;
24 import org.opendaylight.yangtools.concepts.SemVer;
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.DeclaredStatement;
44 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
46 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
49 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
50 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
52
53 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
54         AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
55
56     private final String name;
57     private final String sourcePath;
58     private final String prefix;
59     private final String yangVersion;
60     private final String organization;
61     private final String contact;
62     private final Set<ModuleImport> imports;
63     private final Set<Module> submodules;
64     private final Set<FeatureDefinition> features;
65     private final Set<NotificationDefinition> notifications;
66     private final Set<AugmentationSchema> augmentations;
67     private final Set<RpcDefinition> rpcs;
68     private final Set<Deviation> deviations;
69     private final List<ExtensionDefinition> extensionNodes;
70     private final Set<IdentitySchemaNode> identities;
71     private final List<UnknownSchemaNode> unknownNodes;
72     private final Map<QName, DataSchemaNode> childNodes;
73     private final Set<GroupingDefinition> groupings;
74     private final Set<UsesNode> uses;
75     private final Set<TypeDefinition<?>> typeDefinitions;
76     private final Set<DataSchemaNode> publicChildNodes;
77     private final SemVer semanticVersion;
78
79     AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
80         super(ctx);
81
82         this.name = argument();
83
84         PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
85         this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
86
87         YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
88         this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
89
90         SemanticVersionEffectiveStatementImpl semanticVersionStmt = firstEffective(SemanticVersionEffectiveStatementImpl.class);
91         this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
92
93         OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
94         this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
95
96         ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
97         this.contact = (contactStmt == null) ? null : contactStmt.argument();
98
99         if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
100             this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
101         } else {
102             this.sourcePath = null;
103         }
104
105         // init submodules and substatements of submodules
106         final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
107         final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
108                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
109
110         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
111             this.submodules = ImmutableSet.of();
112             substatementsOfSubmodules = ImmutableList.of();
113         } else {
114             Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
115             Set<Module> submodulesInit = new HashSet<>();
116             List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
117             for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
118                 @SuppressWarnings("unchecked")
119                 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
120                     (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
121                         ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
122                 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
123                         .buildEffective();
124                 submodulesInit.add(submodule);
125                 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
126             }
127
128             this.submodules = ImmutableSet.copyOf(submodulesInit);
129             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
130         }
131
132         // init substatements collections
133         List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
134         effectiveSubstatements.addAll(effectiveSubstatements());
135         effectiveSubstatements.addAll(substatementsOfSubmodules);
136
137         List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
138         Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
139         Set<ModuleImport> importsInit = new HashSet<>();
140         Set<NotificationDefinition> notificationsInit = new HashSet<>();
141         Set<RpcDefinition> rpcsInit = new HashSet<>();
142         Set<Deviation> deviationsInit = new HashSet<>();
143         Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
144         Set<FeatureDefinition> featuresInit = new HashSet<>();
145         List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
146
147         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
148         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
149         Set<UsesNode> mutableUses = new HashSet<>();
150         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
151         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
152
153         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
154             if (effectiveStatement instanceof UnknownSchemaNode) {
155                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
156             }
157             if (effectiveStatement instanceof AugmentationSchema) {
158                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
159             }
160             if (effectiveStatement instanceof ModuleImport) {
161                 importsInit.add((ModuleImport) effectiveStatement);
162             }
163             if (effectiveStatement instanceof NotificationDefinition) {
164                 notificationsInit.add((NotificationDefinition) effectiveStatement);
165             }
166             if (effectiveStatement instanceof RpcDefinition) {
167                 rpcsInit.add((RpcDefinition) effectiveStatement);
168             }
169             if (effectiveStatement instanceof Deviation) {
170                 deviationsInit.add((Deviation) effectiveStatement);
171             }
172             if (effectiveStatement instanceof IdentitySchemaNode) {
173                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
174             }
175             if (effectiveStatement instanceof FeatureDefinition) {
176                 featuresInit.add((FeatureDefinition) effectiveStatement);
177             }
178             if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
179                 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
180             }
181             if (effectiveStatement instanceof DataSchemaNode) {
182                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
183                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
184                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
185                     mutablePublicChildNodes.add(dataSchemaNode);
186                 } else {
187                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
188                 }
189             }
190             if (effectiveStatement instanceof UsesNode) {
191                 UsesNode usesNode = (UsesNode) effectiveStatement;
192                 if (!mutableUses.contains(usesNode)) {
193                     mutableUses.add(usesNode);
194                 } else {
195                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
196                 }
197             }
198             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
199                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
200                 TypeDefinition<?> type = typeDef.getTypeDefinition();
201                 if (!mutableTypeDefinitions.contains(type)) {
202                     mutableTypeDefinitions.add(type);
203                 } else {
204                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
205                 }
206             }
207             if (effectiveStatement instanceof GroupingDefinition) {
208                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
209                 if (!mutableGroupings.contains(grp)) {
210                     mutableGroupings.add(grp);
211                 } else {
212                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
213                 }
214             }
215         }
216
217         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
218         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
219         if (ctx.isEnabledSemanticVersioning()) {
220             this.imports = ImmutableSet.copyOf(importsInit);
221         } else {
222             this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
223         }
224         this.notifications = ImmutableSet.copyOf(notificationsInit);
225         this.rpcs = ImmutableSet.copyOf(rpcsInit);
226         this.deviations = ImmutableSet.copyOf(deviationsInit);
227         this.identities = ImmutableSet.copyOf(identitiesInit);
228         this.features = ImmutableSet.copyOf(featuresInit);
229         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
230
231         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
232         this.groupings = ImmutableSet.copyOf(mutableGroupings);
233         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
234         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
235         this.uses = ImmutableSet.copyOf(mutableUses);
236
237     }
238
239     private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
240             final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
241         Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
242         for (ModuleImport moduleImport : importsInit) {
243             if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
244                 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
245                 if (!impModuleQName.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
246                     ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
247                             impModuleQName.getRevision(), moduleImport.getPrefix());
248                     resolvedModuleImports.add(resolvedModuleImport);
249                 }
250             } else {
251                 resolvedModuleImports.add(moduleImport);
252             }
253         }
254         return resolvedModuleImports;
255     }
256
257     @Override
258     public String getModuleSourcePath() {
259         return sourcePath;
260     }
261
262     @Override
263     public String getSource() {
264         return null;
265     }
266
267     @Override
268     public URI getNamespace() {
269         return getQNameModule().getNamespace();
270     }
271
272     @Override
273     public String getName() {
274         return name;
275     }
276
277     @Override
278     public Date getRevision() {
279         return getQNameModule().getRevision();
280     }
281
282     @Override
283     public String getPrefix() {
284         return prefix;
285     }
286
287     @Override
288     public String getYangVersion() {
289         return yangVersion;
290     }
291
292     @Override
293     public String getOrganization() {
294         return organization;
295     }
296
297     @Override
298     public String getContact() {
299         return contact;
300     }
301
302     @Override
303     public Set<ModuleImport> getImports() {
304         return imports;
305     }
306
307     @Override
308     public Set<Module> getSubmodules() {
309         return submodules;
310     }
311
312     @Override
313     public Set<FeatureDefinition> getFeatures() {
314         return features;
315     }
316
317     @Override
318     public Set<NotificationDefinition> getNotifications() {
319         return notifications;
320     }
321
322     @Override
323     public Set<AugmentationSchema> getAugmentations() {
324         return augmentations;
325     }
326
327     @Override
328     public Set<RpcDefinition> getRpcs() {
329         return rpcs;
330     }
331
332     @Override
333     public Set<Deviation> getDeviations() {
334         return deviations;
335     }
336
337     @Override
338     public List<ExtensionDefinition> getExtensionSchemaNodes() {
339         return extensionNodes;
340     }
341
342     @Override
343     public Set<IdentitySchemaNode> getIdentities() {
344         return identities;
345     }
346
347     @Override
348     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
349         return unknownNodes;
350     }
351
352     @Override
353     public final Set<TypeDefinition<?>> getTypeDefinitions() {
354         return typeDefinitions;
355     }
356
357     @Override
358     public final Set<DataSchemaNode> getChildNodes() {
359         return publicChildNodes;
360     }
361
362     @Override
363     public final Set<GroupingDefinition> getGroupings() {
364         return groupings;
365     }
366
367     @Override
368     public final DataSchemaNode getDataChildByName(final QName name) {
369         // Child nodes are keyed by their container name, so we can do a direct
370         // lookup
371         return childNodes.get(name);
372     }
373
374     @Override
375     public final DataSchemaNode getDataChildByName(final String name) {
376         for (DataSchemaNode node : childNodes.values()) {
377             if (node.getQName().getLocalName().equals(name)) {
378                 return node;
379             }
380         }
381         return null;
382     }
383
384     @Override
385     public Set<UsesNode> getUses() {
386         return uses;
387     }
388
389     @Override
390     public SemVer getSemanticVersion() {
391         return semanticVersion;
392     }
393
394     @Override
395     public String toString() {
396         return this.getClass().getSimpleName() + "[" +
397                 "name=" + name +
398                 ", namespace=" + getNamespace() +
399                 ", revision=" + getRevision() +
400                 ", prefix=" + prefix +
401                 ", yangVersion=" + yangVersion +
402                 "]";
403     }
404
405 }